syscall_linux_s390x.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright 2016 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 s390x,linux
  5. package unix
  6. import (
  7. "unsafe"
  8. )
  9. //sys Dup2(oldfd int, newfd int) (err error)
  10. //sysnb EpollCreate(size int) (fd int, err error)
  11. //sys EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
  12. //sys Fadvise(fd int, offset int64, length int64, advice int) (err error) = SYS_FADVISE64
  13. //sys Fchown(fd int, uid int, gid int) (err error)
  14. //sys Fstat(fd int, stat *Stat_t) (err error)
  15. //sys Fstatat(dirfd int, path string, stat *Stat_t, flags int) (err error) = SYS_NEWFSTATAT
  16. //sys Fstatfs(fd int, buf *Statfs_t) (err error)
  17. //sys Ftruncate(fd int, length int64) (err error)
  18. //sysnb Getegid() (egid int)
  19. //sysnb Geteuid() (euid int)
  20. //sysnb Getgid() (gid int)
  21. //sysnb Getrlimit(resource int, rlim *Rlimit) (err error)
  22. //sysnb Getuid() (uid int)
  23. //sysnb InotifyInit() (fd int, err error)
  24. //sys Lchown(path string, uid int, gid int) (err error)
  25. //sys Lstat(path string, stat *Stat_t) (err error)
  26. //sys Pause() (err error)
  27. //sys Pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
  28. //sys Pwrite(fd int, p []byte, offset int64) (n int, err error) = SYS_PWRITE64
  29. //sys Seek(fd int, offset int64, whence int) (off int64, err error) = SYS_LSEEK
  30. //sys Select(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timeval) (n int, err error)
  31. //sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error)
  32. //sys Setfsgid(gid int) (err error)
  33. //sys Setfsuid(uid int) (err error)
  34. //sysnb Setregid(rgid int, egid int) (err error)
  35. //sysnb Setresgid(rgid int, egid int, sgid int) (err error)
  36. //sysnb Setresuid(ruid int, euid int, suid int) (err error)
  37. //sysnb Setrlimit(resource int, rlim *Rlimit) (err error)
  38. //sysnb Setreuid(ruid int, euid int) (err error)
  39. //sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
  40. //sys Stat(path string, stat *Stat_t) (err error)
  41. //sys Statfs(path string, buf *Statfs_t) (err error)
  42. //sys SyncFileRange(fd int, off int64, n int64, flags int) (err error)
  43. //sys Truncate(path string, length int64) (err error)
  44. //sys Ustat(dev int, ubuf *Ustat_t) (err error)
  45. //sysnb getgroups(n int, list *_Gid_t) (nn int, err error)
  46. //sysnb setgroups(n int, list *_Gid_t) (err error)
  47. //sys futimesat(dirfd int, path string, times *[2]Timeval) (err error)
  48. //sysnb Gettimeofday(tv *Timeval) (err error)
  49. func Time(t *Time_t) (tt Time_t, err error) {
  50. var tv Timeval
  51. err = Gettimeofday(&tv)
  52. if err != nil {
  53. return 0, err
  54. }
  55. if t != nil {
  56. *t = Time_t(tv.Sec)
  57. }
  58. return Time_t(tv.Sec), nil
  59. }
  60. //sys Utime(path string, buf *Utimbuf) (err error)
  61. //sys utimes(path string, times *[2]Timeval) (err error)
  62. func setTimespec(sec, nsec int64) Timespec {
  63. return Timespec{Sec: sec, Nsec: nsec}
  64. }
  65. func setTimeval(sec, usec int64) Timeval {
  66. return Timeval{Sec: sec, Usec: usec}
  67. }
  68. //sysnb pipe2(p *[2]_C_int, flags int) (err error)
  69. func Pipe(p []int) (err error) {
  70. if len(p) != 2 {
  71. return EINVAL
  72. }
  73. var pp [2]_C_int
  74. err = pipe2(&pp, 0) // pipe2 is the same as pipe when flags are set to 0.
  75. p[0] = int(pp[0])
  76. p[1] = int(pp[1])
  77. return
  78. }
  79. func Pipe2(p []int, flags int) (err error) {
  80. if len(p) != 2 {
  81. return EINVAL
  82. }
  83. var pp [2]_C_int
  84. err = pipe2(&pp, flags)
  85. p[0] = int(pp[0])
  86. p[1] = int(pp[1])
  87. return
  88. }
  89. func Ioperm(from int, num int, on int) (err error) {
  90. return ENOSYS
  91. }
  92. func Iopl(level int) (err error) {
  93. return ENOSYS
  94. }
  95. func (r *PtraceRegs) PC() uint64 { return r.Psw.Addr }
  96. func (r *PtraceRegs) SetPC(pc uint64) { r.Psw.Addr = pc }
  97. func (iov *Iovec) SetLen(length int) {
  98. iov.Len = uint64(length)
  99. }
  100. func (msghdr *Msghdr) SetControllen(length int) {
  101. msghdr.Controllen = uint64(length)
  102. }
  103. func (cmsg *Cmsghdr) SetLen(length int) {
  104. cmsg.Len = uint64(length)
  105. }
  106. // Linux on s390x uses the old mmap interface, which requires arguments to be passed in a struct.
  107. // mmap2 also requires arguments to be passed in a struct; it is currently not exposed in <asm/unistd.h>.
  108. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  109. mmap_args := [6]uintptr{addr, length, uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset)}
  110. r0, _, e1 := Syscall(SYS_MMAP, uintptr(unsafe.Pointer(&mmap_args[0])), 0, 0)
  111. xaddr = uintptr(r0)
  112. if e1 != 0 {
  113. err = errnoErr(e1)
  114. }
  115. return
  116. }
  117. // On s390x Linux, all the socket calls go through an extra indirection.
  118. // The arguments to the underlying system call (SYS_SOCKETCALL) are the
  119. // number below and a pointer to an array of uintptr.
  120. const (
  121. // see linux/net.h
  122. netSocket = 1
  123. netBind = 2
  124. netConnect = 3
  125. netListen = 4
  126. netAccept = 5
  127. netGetSockName = 6
  128. netGetPeerName = 7
  129. netSocketPair = 8
  130. netSend = 9
  131. netRecv = 10
  132. netSendTo = 11
  133. netRecvFrom = 12
  134. netShutdown = 13
  135. netSetSockOpt = 14
  136. netGetSockOpt = 15
  137. netSendMsg = 16
  138. netRecvMsg = 17
  139. netAccept4 = 18
  140. netRecvMMsg = 19
  141. netSendMMsg = 20
  142. )
  143. func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (int, error) {
  144. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  145. fd, _, err := Syscall(SYS_SOCKETCALL, netAccept, uintptr(unsafe.Pointer(&args)), 0)
  146. if err != 0 {
  147. return 0, err
  148. }
  149. return int(fd), nil
  150. }
  151. func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (int, error) {
  152. args := [4]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags)}
  153. fd, _, err := Syscall(SYS_SOCKETCALL, netAccept4, uintptr(unsafe.Pointer(&args)), 0)
  154. if err != 0 {
  155. return 0, err
  156. }
  157. return int(fd), nil
  158. }
  159. func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error {
  160. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  161. _, _, err := RawSyscall(SYS_SOCKETCALL, netGetSockName, uintptr(unsafe.Pointer(&args)), 0)
  162. if err != 0 {
  163. return err
  164. }
  165. return nil
  166. }
  167. func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error {
  168. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  169. _, _, err := RawSyscall(SYS_SOCKETCALL, netGetPeerName, uintptr(unsafe.Pointer(&args)), 0)
  170. if err != 0 {
  171. return err
  172. }
  173. return nil
  174. }
  175. func socketpair(domain int, typ int, flags int, fd *[2]int32) error {
  176. args := [4]uintptr{uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd))}
  177. _, _, err := RawSyscall(SYS_SOCKETCALL, netSocketPair, uintptr(unsafe.Pointer(&args)), 0)
  178. if err != 0 {
  179. return err
  180. }
  181. return nil
  182. }
  183. func bind(s int, addr unsafe.Pointer, addrlen _Socklen) error {
  184. args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)}
  185. _, _, err := Syscall(SYS_SOCKETCALL, netBind, uintptr(unsafe.Pointer(&args)), 0)
  186. if err != 0 {
  187. return err
  188. }
  189. return nil
  190. }
  191. func connect(s int, addr unsafe.Pointer, addrlen _Socklen) error {
  192. args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)}
  193. _, _, err := Syscall(SYS_SOCKETCALL, netConnect, uintptr(unsafe.Pointer(&args)), 0)
  194. if err != 0 {
  195. return err
  196. }
  197. return nil
  198. }
  199. func socket(domain int, typ int, proto int) (int, error) {
  200. args := [3]uintptr{uintptr(domain), uintptr(typ), uintptr(proto)}
  201. fd, _, err := RawSyscall(SYS_SOCKETCALL, netSocket, uintptr(unsafe.Pointer(&args)), 0)
  202. if err != 0 {
  203. return 0, err
  204. }
  205. return int(fd), nil
  206. }
  207. func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) error {
  208. args := [5]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen))}
  209. _, _, err := Syscall(SYS_SOCKETCALL, netGetSockOpt, uintptr(unsafe.Pointer(&args)), 0)
  210. if err != 0 {
  211. return err
  212. }
  213. return nil
  214. }
  215. func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) error {
  216. args := [4]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val)}
  217. _, _, err := Syscall(SYS_SOCKETCALL, netSetSockOpt, uintptr(unsafe.Pointer(&args)), 0)
  218. if err != 0 {
  219. return err
  220. }
  221. return nil
  222. }
  223. func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (int, error) {
  224. var base uintptr
  225. if len(p) > 0 {
  226. base = uintptr(unsafe.Pointer(&p[0]))
  227. }
  228. args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen))}
  229. n, _, err := Syscall(SYS_SOCKETCALL, netRecvFrom, uintptr(unsafe.Pointer(&args)), 0)
  230. if err != 0 {
  231. return 0, err
  232. }
  233. return int(n), nil
  234. }
  235. func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) error {
  236. var base uintptr
  237. if len(p) > 0 {
  238. base = uintptr(unsafe.Pointer(&p[0]))
  239. }
  240. args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen)}
  241. _, _, err := Syscall(SYS_SOCKETCALL, netSendTo, uintptr(unsafe.Pointer(&args)), 0)
  242. if err != 0 {
  243. return err
  244. }
  245. return nil
  246. }
  247. func recvmsg(s int, msg *Msghdr, flags int) (int, error) {
  248. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)}
  249. n, _, err := Syscall(SYS_SOCKETCALL, netRecvMsg, uintptr(unsafe.Pointer(&args)), 0)
  250. if err != 0 {
  251. return 0, err
  252. }
  253. return int(n), nil
  254. }
  255. func sendmsg(s int, msg *Msghdr, flags int) (int, error) {
  256. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)}
  257. n, _, err := Syscall(SYS_SOCKETCALL, netSendMsg, uintptr(unsafe.Pointer(&args)), 0)
  258. if err != 0 {
  259. return 0, err
  260. }
  261. return int(n), nil
  262. }
  263. func Listen(s int, n int) error {
  264. args := [2]uintptr{uintptr(s), uintptr(n)}
  265. _, _, err := Syscall(SYS_SOCKETCALL, netListen, uintptr(unsafe.Pointer(&args)), 0)
  266. if err != 0 {
  267. return err
  268. }
  269. return nil
  270. }
  271. func Shutdown(s, how int) error {
  272. args := [2]uintptr{uintptr(s), uintptr(how)}
  273. _, _, err := Syscall(SYS_SOCKETCALL, netShutdown, uintptr(unsafe.Pointer(&args)), 0)
  274. if err != 0 {
  275. return err
  276. }
  277. return nil
  278. }
  279. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  280. func Poll(fds []PollFd, timeout int) (n int, err error) {
  281. if len(fds) == 0 {
  282. return poll(nil, 0, timeout)
  283. }
  284. return poll(&fds[0], len(fds), timeout)
  285. }