syscall_linux_arm.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. //sysnb Time(t *Time_t) (tt Time_t, err error)
  97. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
  98. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
  99. //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
  100. //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
  101. func Fadvise(fd int, offset int64, length int64, advice int) (err error) {
  102. _, _, e1 := Syscall6(SYS_ARM_FADVISE64_64, uintptr(fd), uintptr(advice), uintptr(offset), uintptr(offset>>32), uintptr(length), uintptr(length>>32))
  103. if e1 != 0 {
  104. err = errnoErr(e1)
  105. }
  106. return
  107. }
  108. //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
  109. func Fstatfs(fd int, buf *Statfs_t) (err error) {
  110. _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  111. if e != 0 {
  112. err = e
  113. }
  114. return
  115. }
  116. func Statfs(path string, buf *Statfs_t) (err error) {
  117. pathp, err := BytePtrFromString(path)
  118. if err != nil {
  119. return err
  120. }
  121. _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  122. if e != 0 {
  123. err = e
  124. }
  125. return
  126. }
  127. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  128. page := uintptr(offset / 4096)
  129. if offset != int64(page)*4096 {
  130. return 0, EINVAL
  131. }
  132. return mmap2(addr, length, prot, flags, fd, page)
  133. }
  134. type rlimit32 struct {
  135. Cur uint32
  136. Max uint32
  137. }
  138. //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT
  139. const rlimInf32 = ^uint32(0)
  140. const rlimInf64 = ^uint64(0)
  141. func Getrlimit(resource int, rlim *Rlimit) (err error) {
  142. err = prlimit(0, resource, nil, rlim)
  143. if err != ENOSYS {
  144. return err
  145. }
  146. rl := rlimit32{}
  147. err = getrlimit(resource, &rl)
  148. if err != nil {
  149. return
  150. }
  151. if rl.Cur == rlimInf32 {
  152. rlim.Cur = rlimInf64
  153. } else {
  154. rlim.Cur = uint64(rl.Cur)
  155. }
  156. if rl.Max == rlimInf32 {
  157. rlim.Max = rlimInf64
  158. } else {
  159. rlim.Max = uint64(rl.Max)
  160. }
  161. return
  162. }
  163. //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
  164. func Setrlimit(resource int, rlim *Rlimit) (err error) {
  165. err = prlimit(0, resource, rlim, nil)
  166. if err != ENOSYS {
  167. return err
  168. }
  169. rl := rlimit32{}
  170. if rlim.Cur == rlimInf64 {
  171. rl.Cur = rlimInf32
  172. } else if rlim.Cur < uint64(rlimInf32) {
  173. rl.Cur = uint32(rlim.Cur)
  174. } else {
  175. return EINVAL
  176. }
  177. if rlim.Max == rlimInf64 {
  178. rl.Max = rlimInf32
  179. } else if rlim.Max < uint64(rlimInf32) {
  180. rl.Max = uint32(rlim.Max)
  181. } else {
  182. return EINVAL
  183. }
  184. return setrlimit(resource, &rl)
  185. }
  186. func (r *PtraceRegs) PC() uint64 { return uint64(r.Uregs[15]) }
  187. func (r *PtraceRegs) SetPC(pc uint64) { r.Uregs[15] = uint32(pc) }
  188. func (iov *Iovec) SetLen(length int) {
  189. iov.Len = uint32(length)
  190. }
  191. func (msghdr *Msghdr) SetControllen(length int) {
  192. msghdr.Controllen = uint32(length)
  193. }
  194. func (cmsg *Cmsghdr) SetLen(length int) {
  195. cmsg.Len = uint32(length)
  196. }