syscall_linux_s390x.go 11 KB

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