syscall_linux_arm.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build arm,linux
  5. package unix
  6. import (
  7. "syscall"
  8. "unsafe"
  9. )
  10. func setTimespec(sec, nsec int64) Timespec {
  11. return Timespec{Sec: int32(sec), Nsec: int32(nsec)}
  12. }
  13. func setTimeval(sec, usec int64) Timeval {
  14. return Timeval{Sec: int32(sec), Usec: int32(usec)}
  15. }
  16. //sysnb pipe(p *[2]_C_int) (err error)
  17. func Pipe(p []int) (err error) {
  18. if len(p) != 2 {
  19. return EINVAL
  20. }
  21. var pp [2]_C_int
  22. // Try pipe2 first for Android O, then try pipe for kernel 2.6.23.
  23. err = pipe2(&pp, 0)
  24. if err == ENOSYS {
  25. err = pipe(&pp)
  26. }
  27. p[0] = int(pp[0])
  28. p[1] = int(pp[1])
  29. return
  30. }
  31. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  32. func Pipe2(p []int, flags int) (err error) {
  33. if len(p) != 2 {
  34. return EINVAL
  35. }
  36. var pp [2]_C_int
  37. err = pipe2(&pp, flags)
  38. p[0] = int(pp[0])
  39. p[1] = int(pp[1])
  40. return
  41. }
  42. // Underlying system call writes to newoffset via pointer.
  43. // Implemented in assembly to avoid allocation.
  44. func seek(fd int, offset int64, whence int) (newoffset int64, err syscall.Errno)
  45. func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
  46. newoffset, errno := seek(fd, offset, whence)
  47. if errno != 0 {
  48. return 0, errno
  49. }
  50. return newoffset, nil
  51. }
  52. //sys accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error)
  53. //sys accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error)
  54. //sys bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  55. //sys connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error)
  56. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32
  57. //sysnb setgroups(n int, list *_Gid_t) (err error) = SYS_SETGROUPS32
  58. //sys getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error)
  59. //sys setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error)
  60. //sysnb socket(domain int, typ int, proto int) (fd int, err error)
  61. //sysnb getpeername(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  62. //sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
  63. //sys recvfrom(fd int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error)
  64. //sys sendto(s int, buf []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error)
  65. //sysnb socketpair(domain int, typ int, flags int, fd *[2]int32) (err error)
  66. //sys recvmsg(s int, msg *Msghdr, flags int) (n int, err error)
  67. //sys sendmsg(s int, msg *Msghdr, flags int) (n int, err error)
  68. // 64-bit file system and 32-bit uid calls
  69. // (16-bit uid calls are not always supported in newer kernels)
  70. //sys Dup2(oldfd int, newfd int) (err error)
  71. //sysnb EpollCreate(size int) (fd int, err error)
  72. //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
  73. //sys Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32
  74. //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
  75. //sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_FSTATAT64
  76. //sysnb Getegid() (egid int) = SYS_GETEGID32
  77. //sysnb Geteuid() (euid int) = SYS_GETEUID32
  78. //sysnb Getgid() (gid int) = SYS_GETGID32
  79. //sysnb Getuid() (uid int) = SYS_GETUID32
  80. //sysnb InotifyInit() (fd int, err error)
  81. //sys Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32
  82. //sys Listen(s int, n int) (err error)
  83. //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
  84. //sys Pause() (err error)
  85. //sys Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error)
  86. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64
  87. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT
  88. //sys Setfsgid(gid int) (err error) = SYS_SETFSGID32
  89. //sys Setfsuid(uid int) (err error) = SYS_SETFSUID32
  90. //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID32
  91. //sysnb Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32
  92. //sysnb Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32
  93. //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32
  94. //sys Shutdown(fd int, how int) (err error)
  95. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
  96. //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
  97. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  98. //sys futimesat(dirfd int, path string, times *[2]Timeval) (err error)
  99. //sysnb Gettimeofday(tv *Timeval) (err error)
  100. func Time(t *Time_t) (Time_t, error) {
  101. var tv Timeval
  102. err := Gettimeofday(&tv)
  103. if err != nil {
  104. return 0, err
  105. }
  106. if t != nil {
  107. *t = Time_t(tv.Sec)
  108. }
  109. return Time_t(tv.Sec), nil
  110. }
  111. func Utime(path string, buf *Utimbuf) error {
  112. tv := []Timeval{
  113. {Sec: buf.Actime},
  114. {Sec: buf.Modtime},
  115. }
  116. return Utimes(path, tv)
  117. }
  118. //sys utimes(path string, times *[2]Timeval) (err error)
  119. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
  120. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
  121. //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
  122. //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
  123. func Fadvise(fd int, offset int64, length int64, advice int) (err error) {
  124. _, _, e1 := Syscall6(SYS_ARM_FADVISE64_64, uintptr(fd), uintptr(advice), uintptr(offset), uintptr(offset>>32), uintptr(length), uintptr(length>>32))
  125. if e1 != 0 {
  126. err = errnoErr(e1)
  127. }
  128. return
  129. }
  130. //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
  131. func Fstatfs(fd int, buf *Statfs_t) (err error) {
  132. _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  133. if e != 0 {
  134. err = e
  135. }
  136. return
  137. }
  138. func Statfs(path string, buf *Statfs_t) (err error) {
  139. pathp, err := BytePtrFromString(path)
  140. if err != nil {
  141. return err
  142. }
  143. _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  144. if e != 0 {
  145. err = e
  146. }
  147. return
  148. }
  149. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  150. page := uintptr(offset / 4096)
  151. if offset != int64(page)*4096 {
  152. return 0, EINVAL
  153. }
  154. return mmap2(addr, length, prot, flags, fd, page)
  155. }
  156. type rlimit32 struct {
  157. Cur uint32
  158. Max uint32
  159. }
  160. //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_UGETRLIMIT
  161. const rlimInf32 = ^uint32(0)
  162. const rlimInf64 = ^uint64(0)
  163. func Getrlimit(resource int, rlim *Rlimit) (err error) {
  164. err = prlimit(0, resource, nil, rlim)
  165. if err != ENOSYS {
  166. return err
  167. }
  168. rl := rlimit32{}
  169. err = getrlimit(resource, &rl)
  170. if err != nil {
  171. return
  172. }
  173. if rl.Cur == rlimInf32 {
  174. rlim.Cur = rlimInf64
  175. } else {
  176. rlim.Cur = uint64(rl.Cur)
  177. }
  178. if rl.Max == rlimInf32 {
  179. rlim.Max = rlimInf64
  180. } else {
  181. rlim.Max = uint64(rl.Max)
  182. }
  183. return
  184. }
  185. //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
  186. func Setrlimit(resource int, rlim *Rlimit) (err error) {
  187. err = prlimit(0, resource, rlim, nil)
  188. if err != ENOSYS {
  189. return err
  190. }
  191. rl := rlimit32{}
  192. if rlim.Cur == rlimInf64 {
  193. rl.Cur = rlimInf32
  194. } else if rlim.Cur < uint64(rlimInf32) {
  195. rl.Cur = uint32(rlim.Cur)
  196. } else {
  197. return EINVAL
  198. }
  199. if rlim.Max == rlimInf64 {
  200. rl.Max = rlimInf32
  201. } else if rlim.Max < uint64(rlimInf32) {
  202. rl.Max = uint32(rlim.Max)
  203. } else {
  204. return EINVAL
  205. }
  206. return setrlimit(resource, &rl)
  207. }
  208. func (r *PtraceRegs) PC() uint64 { return uint64(r.Uregs[15]) }
  209. func (r *PtraceRegs) SetPC(pc uint64) { r.Uregs[15] = uint32(pc) }
  210. func (iov *Iovec) SetLen(length int) {
  211. iov.Len = uint32(length)
  212. }
  213. func (msghdr *Msghdr) SetControllen(length int) {
  214. msghdr.Controllen = uint32(length)
  215. }
  216. func (msghdr *Msghdr) SetIovlen(length int) {
  217. msghdr.Iovlen = uint32(length)
  218. }
  219. func (cmsg *Cmsghdr) SetLen(length int) {
  220. cmsg.Len = uint32(length)
  221. }
  222. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  223. func Poll(fds []PollFd, timeout int) (n int, err error) {
  224. if len(fds) == 0 {
  225. return poll(nil, 0, timeout)
  226. }
  227. return poll(&fds[0], len(fds), timeout)
  228. }
  229. //sys armSyncFileRange(fd int, flags int, off int64, n int64) (err error) = SYS_ARM_SYNC_FILE_RANGE
  230. func SyncFileRange(fd int, off int64, n int64, flags int) error {
  231. // The sync_file_range and arm_sync_file_range syscalls differ only in the
  232. // order of their arguments.
  233. return armSyncFileRange(fd, flags, off, n)
  234. }
  235. //sys kexecFileLoad(kernelFd int, initrdFd int, cmdlineLen int, cmdline string, flags int) (err error)
  236. func KexecFileLoad(kernelFd int, initrdFd int, cmdline string, flags int) error {
  237. cmdlineLen := len(cmdline)
  238. if cmdlineLen > 0 {
  239. // Account for the additional NULL byte added by
  240. // BytePtrFromString in kexecFileLoad. The kexec_file_load
  241. // syscall expects a NULL-terminated string.
  242. cmdlineLen++
  243. }
  244. return kexecFileLoad(kernelFd, initrdFd, cmdlineLen, cmdline, flags)
  245. }