syscall_linux_s390x.go 9.9 KB

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