syscall_linux_arm.go 7.6 KB

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