syscall_linux_arm.go 7.7 KB

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