syscall_linux_386.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. // TODO(rsc): Rewrite all nn(SP) references into name+(nn-8)(FP)
  5. // so that go vet can check that they are correct.
  6. // +build 386,linux
  7. package unix
  8. import (
  9. "syscall"
  10. "unsafe"
  11. )
  12. func Getpagesize() int { return 4096 }
  13. func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
  14. func NsecToTimespec(nsec int64) (ts Timespec) {
  15. ts.Sec = int32(nsec / 1e9)
  16. ts.Nsec = int32(nsec % 1e9)
  17. return
  18. }
  19. func TimevalToNsec(tv Timeval) int64 { return int64(tv.Sec)*1e9 + int64(tv.Usec)*1e3 }
  20. func NsecToTimeval(nsec int64) (tv Timeval) {
  21. nsec += 999 // round up to microsecond
  22. tv.Sec = int32(nsec / 1e9)
  23. tv.Usec = int32(nsec % 1e9 / 1e3)
  24. return
  25. }
  26. //sysnb pipe(p *[2]_C_int) (err error)
  27. func Pipe(p []int) (err error) {
  28. if len(p) != 2 {
  29. return EINVAL
  30. }
  31. var pp [2]_C_int
  32. err = pipe(&pp)
  33. p[0] = int(pp[0])
  34. p[1] = int(pp[1])
  35. return
  36. }
  37. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  38. func Pipe2(p []int, flags int) (err error) {
  39. if len(p) != 2 {
  40. return EINVAL
  41. }
  42. var pp [2]_C_int
  43. err = pipe2(&pp, flags)
  44. p[0] = int(pp[0])
  45. p[1] = int(pp[1])
  46. return
  47. }
  48. // 64-bit file system and 32-bit uid calls
  49. // (386 default is 32-bit file system and 16-bit uid).
  50. //sys Dup2(oldfd int, newfd int) (err error)
  51. //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64_64
  52. //sys Fchown(fd int, uid int, gid int) (err error) = SYS_FCHOWN32
  53. //sys Fstat(fd int, stat *Stat_t) (err error) = SYS_FSTAT64
  54. //sys Ftruncate(fd int, length int64) (err error) = SYS_FTRUNCATE64
  55. //sysnb Getegid() (egid int) = SYS_GETEGID32
  56. //sysnb Geteuid() (euid int) = SYS_GETEUID32
  57. //sysnb Getgid() (gid int) = SYS_GETGID32
  58. //sysnb Getuid() (uid int) = SYS_GETUID32
  59. //sysnb InotifyInit() (fd int, err error)
  60. //sys Ioperm(from int, num int, on int) (err error)
  61. //sys Iopl(level int) (err error)
  62. //sys Lchown(path string, uid int, gid int) (err error) = SYS_LCHOWN32
  63. //sys Lstat(path string, stat *Stat_t) (err error) = SYS_LSTAT64
  64. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
  65. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
  66. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) = SYS_SENDFILE64
  67. //sys Setfsgid(gid int) (err error) = SYS_SETFSGID32
  68. //sys Setfsuid(uid int) (err error) = SYS_SETFSUID32
  69. //sysnb Setregid(rgid int, egid int) (err error) = SYS_SETREGID32
  70. //sysnb Setresgid(rgid int, egid int, sgid int) (err error) = SYS_SETRESGID32
  71. //sysnb Setresuid(ruid int, euid int, suid int) (err error) = SYS_SETRESUID32
  72. //sysnb Setreuid(ruid int, euid int) (err error) = SYS_SETREUID32
  73. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error)
  74. //sys Stat(path string, stat *Stat_t) (err error) = SYS_STAT64
  75. //sys SyncFileRange(fd int, off int64, n int64, flags int) (err error)
  76. //sys Truncate(path string, length int64) (err error) = SYS_TRUNCATE64
  77. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error) = SYS_GETGROUPS32
  78. //sysnb setgroups(n int, list *_Gid_t) (err error) = SYS_SETGROUPS32
  79. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error) = SYS__NEWSELECT
  80. //sys mmap2(addr uintptr, length uintptr, prot int, flags int, fd int, pageOffset uintptr) (xaddr uintptr, err error)
  81. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  82. page := uintptr(offset / 4096)
  83. if offset != int64(page)*4096 {
  84. return 0, EINVAL
  85. }
  86. return mmap2(addr, length, prot, flags, fd, page)
  87. }
  88. type rlimit32 struct {
  89. Cur uint32
  90. Max uint32
  91. }
  92. //sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT
  93. const rlimInf32 = ^uint32(0)
  94. const rlimInf64 = ^uint64(0)
  95. func Getrlimit(resource int, rlim *Rlimit) (err error) {
  96. err = prlimit(0, resource, nil, rlim)
  97. if err != ENOSYS {
  98. return err
  99. }
  100. rl := rlimit32{}
  101. err = getrlimit(resource, &rl)
  102. if err != nil {
  103. return
  104. }
  105. if rl.Cur == rlimInf32 {
  106. rlim.Cur = rlimInf64
  107. } else {
  108. rlim.Cur = uint64(rl.Cur)
  109. }
  110. if rl.Max == rlimInf32 {
  111. rlim.Max = rlimInf64
  112. } else {
  113. rlim.Max = uint64(rl.Max)
  114. }
  115. return
  116. }
  117. //sysnb setrlimit(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
  118. func Setrlimit(resource int, rlim *Rlimit) (err error) {
  119. err = prlimit(0, resource, rlim, nil)
  120. if err != ENOSYS {
  121. return err
  122. }
  123. rl := rlimit32{}
  124. if rlim.Cur == rlimInf64 {
  125. rl.Cur = rlimInf32
  126. } else if rlim.Cur < uint64(rlimInf32) {
  127. rl.Cur = uint32(rlim.Cur)
  128. } else {
  129. return EINVAL
  130. }
  131. if rlim.Max == rlimInf64 {
  132. rl.Max = rlimInf32
  133. } else if rlim.Max < uint64(rlimInf32) {
  134. rl.Max = uint32(rlim.Max)
  135. } else {
  136. return EINVAL
  137. }
  138. return setrlimit(resource, &rl)
  139. }
  140. // Underlying system call writes to newoffset via pointer.
  141. // Implemented in assembly to avoid allocation.
  142. func seek(fd int, offset int64, whence int) (newoffset int64, err syscall.Errno)
  143. func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
  144. newoffset, errno := seek(fd, offset, whence)
  145. if errno != 0 {
  146. return 0, errno
  147. }
  148. return newoffset, nil
  149. }
  150. // Vsyscalls on amd64.
  151. //sysnb Gettimeofday(tv *Timeval) (err error)
  152. //sysnb Time(t *Time_t) (tt Time_t, err error)
  153. // On x86 Linux, all the socket calls go through an extra indirection,
  154. // I think because the 5-register system call interface can't handle
  155. // the 6-argument calls like sendto and recvfrom. Instead the
  156. // arguments to the underlying system call are the number below
  157. // and a pointer to an array of uintptr. We hide the pointer in the
  158. // socketcall assembly to avoid allocation on every system call.
  159. const (
  160. // see linux/net.h
  161. _SOCKET = 1
  162. _BIND = 2
  163. _CONNECT = 3
  164. _LISTEN = 4
  165. _ACCEPT = 5
  166. _GETSOCKNAME = 6
  167. _GETPEERNAME = 7
  168. _SOCKETPAIR = 8
  169. _SEND = 9
  170. _RECV = 10
  171. _SENDTO = 11
  172. _RECVFROM = 12
  173. _SHUTDOWN = 13
  174. _SETSOCKOPT = 14
  175. _GETSOCKOPT = 15
  176. _SENDMSG = 16
  177. _RECVMSG = 17
  178. _ACCEPT4 = 18
  179. _RECVMMSG = 19
  180. _SENDMMSG = 20
  181. )
  182. func socketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err syscall.Errno)
  183. func rawsocketcall(call int, a0, a1, a2, a3, a4, a5 uintptr) (n int, err syscall.Errno)
  184. func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (fd int, err error) {
  185. fd, e := socketcall(_ACCEPT, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
  186. if e != 0 {
  187. err = e
  188. }
  189. return
  190. }
  191. func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (fd int, err error) {
  192. fd, e := socketcall(_ACCEPT4, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags), 0, 0)
  193. if e != 0 {
  194. err = e
  195. }
  196. return
  197. }
  198. func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
  199. _, e := rawsocketcall(_GETSOCKNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
  200. if e != 0 {
  201. err = e
  202. }
  203. return
  204. }
  205. func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error) {
  206. _, e := rawsocketcall(_GETPEERNAME, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), 0, 0, 0)
  207. if e != 0 {
  208. err = e
  209. }
  210. return
  211. }
  212. func socketpair(domain int, typ int, flags int, fd *[2]int32) (err error) {
  213. _, e := rawsocketcall(_SOCKETPAIR, uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd)), 0, 0)
  214. if e != 0 {
  215. err = e
  216. }
  217. return
  218. }
  219. func bind(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
  220. _, e := socketcall(_BIND, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
  221. if e != 0 {
  222. err = e
  223. }
  224. return
  225. }
  226. func connect(s int, addr unsafe.Pointer, addrlen _Socklen) (err error) {
  227. _, e := socketcall(_CONNECT, uintptr(s), uintptr(addr), uintptr(addrlen), 0, 0, 0)
  228. if e != 0 {
  229. err = e
  230. }
  231. return
  232. }
  233. func socket(domain int, typ int, proto int) (fd int, err error) {
  234. fd, e := rawsocketcall(_SOCKET, uintptr(domain), uintptr(typ), uintptr(proto), 0, 0, 0)
  235. if e != 0 {
  236. err = e
  237. }
  238. return
  239. }
  240. func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) (err error) {
  241. _, e := socketcall(_GETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen)), 0)
  242. if e != 0 {
  243. err = e
  244. }
  245. return
  246. }
  247. func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) (err error) {
  248. _, e := socketcall(_SETSOCKOPT, uintptr(s), uintptr(level), uintptr(name), uintptr(val), vallen, 0)
  249. if e != 0 {
  250. err = e
  251. }
  252. return
  253. }
  254. func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (n int, err error) {
  255. var base uintptr
  256. if len(p) > 0 {
  257. base = uintptr(unsafe.Pointer(&p[0]))
  258. }
  259. n, e := socketcall(_RECVFROM, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen)))
  260. if e != 0 {
  261. err = e
  262. }
  263. return
  264. }
  265. func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) (err error) {
  266. var base uintptr
  267. if len(p) > 0 {
  268. base = uintptr(unsafe.Pointer(&p[0]))
  269. }
  270. _, e := socketcall(_SENDTO, uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen))
  271. if e != 0 {
  272. err = e
  273. }
  274. return
  275. }
  276. func recvmsg(s int, msg *Msghdr, flags int) (n int, err error) {
  277. n, e := socketcall(_RECVMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
  278. if e != 0 {
  279. err = e
  280. }
  281. return
  282. }
  283. func sendmsg(s int, msg *Msghdr, flags int) (n int, err error) {
  284. n, e := socketcall(_SENDMSG, uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags), 0, 0, 0)
  285. if e != 0 {
  286. err = e
  287. }
  288. return
  289. }
  290. func Listen(s int, n int) (err error) {
  291. _, e := socketcall(_LISTEN, uintptr(s), uintptr(n), 0, 0, 0, 0)
  292. if e != 0 {
  293. err = e
  294. }
  295. return
  296. }
  297. func Shutdown(s, how int) (err error) {
  298. _, e := socketcall(_SHUTDOWN, uintptr(s), uintptr(how), 0, 0, 0, 0)
  299. if e != 0 {
  300. err = e
  301. }
  302. return
  303. }
  304. func Fstatfs(fd int, buf *Statfs_t) (err error) {
  305. _, _, e := Syscall(SYS_FSTATFS64, uintptr(fd), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  306. if e != 0 {
  307. err = e
  308. }
  309. return
  310. }
  311. func Statfs(path string, buf *Statfs_t) (err error) {
  312. pathp, err := BytePtrFromString(path)
  313. if err != nil {
  314. return err
  315. }
  316. _, _, e := Syscall(SYS_STATFS64, uintptr(unsafe.Pointer(pathp)), unsafe.Sizeof(*buf), uintptr(unsafe.Pointer(buf)))
  317. if e != 0 {
  318. err = e
  319. }
  320. return
  321. }
  322. func (r *PtraceRegs) PC() uint64 { return uint64(uint32(r.Eip)) }
  323. func (r *PtraceRegs) SetPC(pc uint64) { r.Eip = int32(pc) }
  324. func (iov *Iovec) SetLen(length int) {
  325. iov.Len = uint32(length)
  326. }
  327. func (msghdr *Msghdr) SetControllen(length int) {
  328. msghdr.Controllen = uint32(length)
  329. }
  330. func (cmsg *Cmsghdr) SetLen(length int) {
  331. cmsg.Len = uint32(length)
  332. }