syscall_linux_arm.go 8.1 KB

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