syscall_linux_arm.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. //sys Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32
  67. //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
  68. //sysnb Getegid() (egid int) = SYS_GETEGID32
  69. //sysnb Geteuid() (euid int) = SYS_GETEUID32
  70. //sysnb Getgid() (gid int) = SYS_GETGID32
  71. //sysnb Getuid() (uid int) = SYS_GETUID32
  72. //sysnb InotifyInit() (fd int, err error)
  73. //sys Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32
  74. //sys Listen(s int, n int) (err error)
  75. //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
  76. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64
  77. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT
  78. //sys Setfsgid(gid int) (err error) = SYS_SETFSGID32
  79. //sys Setfsuid(uid int) (err error) = SYS_SETFSUID32
  80. //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID32
  81. //sysnb Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32
  82. //sysnb Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32
  83. //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32
  84. //sys Shutdown(fd int, how int) (err error)
  85. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
  86. //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
  87. // Vsyscalls on amd64.
  88. //sysnb Gettimeofday(tv *Timeval) (err error)
  89. //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
  90. //sys Pause() (err error)
  91. func Time(t *Time_t) (Time_t, error) {
  92. var tv Timeval
  93. err := Gettimeofday(&tv)
  94. if err != nil {
  95. return 0, err
  96. }
  97. if t != nil {
  98. *t = Time_t(tv.Sec)
  99. }
  100. return Time_t(tv.Sec), nil
  101. }
  102. func Utime(path string, buf *Utimbuf) error {
  103. tv := []Timeval{
  104. {Sec: buf.Actime},
  105. {Sec: buf.Modtime},
  106. }
  107. return Utimes(path, tv)
  108. }
  109. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
  110. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
  111. //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
  112. //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
  113. func Fadvise(fd int, offset int64, length int64, advice int) (err error) {
  114. _, _, e1 := Syscall6(SYS_ARM_FADVISE64_64, uintptr(fd), uintptr(advice), uintptr(offset), uintptr(offset>>32), uintptr(length), uintptr(length>>32))
  115. if e1 != 0 {
  116. err = errnoErr(e1)
  117. }
  118. return
  119. }
  120. //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
  121. func Fstatfs(fd int, buf *Statfs_t) (err error) {
  122. _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  123. if e != 0 {
  124. err = e
  125. }
  126. return
  127. }
  128. func Statfs(path string, buf *Statfs_t) (err error) {
  129. pathp, err := BytePtrFromString(path)
  130. if err != nil {
  131. return err
  132. }
  133. _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  134. if e != 0 {
  135. err = e
  136. }
  137. return
  138. }
  139. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  140. page := uintptr(offset / 4096)
  141. if offset != int64(page)*4096 {
  142. return 0, EINVAL
  143. }
  144. return mmap2(addr, length, prot, flags, fd, page)
  145. }
  146. type rlimit32 struct {
  147. Cur uint32
  148. Max uint32
  149. }
  150. //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_UGETRLIMIT
  151. const rlimInf32 = ^uint32(0)
  152. const rlimInf64 = ^uint64(0)
  153. func Getrlimit(resource int, rlim *Rlimit) (err error) {
  154. err = prlimit(0, resource, nil, rlim)
  155. if err != ENOSYS {
  156. return err
  157. }
  158. rl := rlimit32{}
  159. err = getrlimit(resource, &rl)
  160. if err != nil {
  161. return
  162. }
  163. if rl.Cur == rlimInf32 {
  164. rlim.Cur = rlimInf64
  165. } else {
  166. rlim.Cur = uint64(rl.Cur)
  167. }
  168. if rl.Max == rlimInf32 {
  169. rlim.Max = rlimInf64
  170. } else {
  171. rlim.Max = uint64(rl.Max)
  172. }
  173. return
  174. }
  175. //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
  176. func Setrlimit(resource int, rlim *Rlimit) (err error) {
  177. err = prlimit(0, resource, rlim, nil)
  178. if err != ENOSYS {
  179. return err
  180. }
  181. rl := rlimit32{}
  182. if rlim.Cur == rlimInf64 {
  183. rl.Cur = rlimInf32
  184. } else if rlim.Cur < uint64(rlimInf32) {
  185. rl.Cur = uint32(rlim.Cur)
  186. } else {
  187. return EINVAL
  188. }
  189. if rlim.Max == rlimInf64 {
  190. rl.Max = rlimInf32
  191. } else if rlim.Max < uint64(rlimInf32) {
  192. rl.Max = uint32(rlim.Max)
  193. } else {
  194. return EINVAL
  195. }
  196. return setrlimit(resource, &rl)
  197. }
  198. func (r *PtraceRegs) PC() uint64 { return uint64(r.Uregs[15]) }
  199. func (r *PtraceRegs) SetPC(pc uint64) { r.Uregs[15] = uint32(pc) }
  200. func (iov *Iovec) SetLen(length int) {
  201. iov.Len = uint32(length)
  202. }
  203. func (msghdr *Msghdr) SetControllen(length int) {
  204. msghdr.Controllen = uint32(length)
  205. }
  206. func (cmsg *Cmsghdr) SetLen(length int) {
  207. cmsg.Len = uint32(length)
  208. }
  209. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  210. func Poll(fds []PollFd, timeout int) (n int, err error) {
  211. if len(fds) == 0 {
  212. return poll(nil, 0, timeout)
  213. }
  214. return poll(&fds[0], len(fds), timeout)
  215. }