syscall_linux_s390x.go 9.8 KB

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