syscall_linux_s390x.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 (cmsg *Cmsghdr) SetLen(length int) {
  105. cmsg.Len = uint64(length)
  106. }
  107. // Linux on s390x uses the old mmap interface, which requires arguments to be passed in a struct.
  108. // mmap2 also requires arguments to be passed in a struct; it is currently not exposed in <asm/unistd.h>.
  109. func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int64) (xaddr uintptr, err error) {
  110. mmap_args := [6]uintptr{addr, length, uintptr(prot), uintptr(flags), uintptr(fd), uintptr(offset)}
  111. r0, _, e1 := Syscall(SYS_MMAP, uintptr(unsafe.Pointer(&mmap_args[0])), 0, 0)
  112. xaddr = uintptr(r0)
  113. if e1 != 0 {
  114. err = errnoErr(e1)
  115. }
  116. return
  117. }
  118. // On s390x Linux, all the socket calls go through an extra indirection.
  119. // The arguments to the underlying system call (SYS_SOCKETCALL) are the
  120. // number below and a pointer to an array of uintptr.
  121. const (
  122. // see linux/net.h
  123. netSocket = 1
  124. netBind = 2
  125. netConnect = 3
  126. netListen = 4
  127. netAccept = 5
  128. netGetSockName = 6
  129. netGetPeerName = 7
  130. netSocketPair = 8
  131. netSend = 9
  132. netRecv = 10
  133. netSendTo = 11
  134. netRecvFrom = 12
  135. netShutdown = 13
  136. netSetSockOpt = 14
  137. netGetSockOpt = 15
  138. netSendMsg = 16
  139. netRecvMsg = 17
  140. netAccept4 = 18
  141. netRecvMMsg = 19
  142. netSendMMsg = 20
  143. )
  144. func accept(s int, rsa *RawSockaddrAny, addrlen *_Socklen) (int, error) {
  145. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  146. fd, _, err := Syscall(SYS_SOCKETCALL, netAccept, uintptr(unsafe.Pointer(&args)), 0)
  147. if err != 0 {
  148. return 0, err
  149. }
  150. return int(fd), nil
  151. }
  152. func accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (int, error) {
  153. args := [4]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags)}
  154. fd, _, err := Syscall(SYS_SOCKETCALL, netAccept4, uintptr(unsafe.Pointer(&args)), 0)
  155. if err != 0 {
  156. return 0, err
  157. }
  158. return int(fd), nil
  159. }
  160. func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error {
  161. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  162. _, _, err := RawSyscall(SYS_SOCKETCALL, netGetSockName, uintptr(unsafe.Pointer(&args)), 0)
  163. if err != 0 {
  164. return err
  165. }
  166. return nil
  167. }
  168. func getpeername(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error {
  169. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  170. _, _, err := RawSyscall(SYS_SOCKETCALL, netGetPeerName, uintptr(unsafe.Pointer(&args)), 0)
  171. if err != 0 {
  172. return err
  173. }
  174. return nil
  175. }
  176. func socketpair(domain int, typ int, flags int, fd *[2]int32) error {
  177. args := [4]uintptr{uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd))}
  178. _, _, err := RawSyscall(SYS_SOCKETCALL, netSocketPair, uintptr(unsafe.Pointer(&args)), 0)
  179. if err != 0 {
  180. return err
  181. }
  182. return nil
  183. }
  184. func bind(s int, addr unsafe.Pointer, addrlen _Socklen) error {
  185. args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)}
  186. _, _, err := Syscall(SYS_SOCKETCALL, netBind, uintptr(unsafe.Pointer(&args)), 0)
  187. if err != 0 {
  188. return err
  189. }
  190. return nil
  191. }
  192. func connect(s int, addr unsafe.Pointer, addrlen _Socklen) error {
  193. args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)}
  194. _, _, err := Syscall(SYS_SOCKETCALL, netConnect, uintptr(unsafe.Pointer(&args)), 0)
  195. if err != 0 {
  196. return err
  197. }
  198. return nil
  199. }
  200. func socket(domain int, typ int, proto int) (int, error) {
  201. args := [3]uintptr{uintptr(domain), uintptr(typ), uintptr(proto)}
  202. fd, _, err := RawSyscall(SYS_SOCKETCALL, netSocket, uintptr(unsafe.Pointer(&args)), 0)
  203. if err != 0 {
  204. return 0, err
  205. }
  206. return int(fd), nil
  207. }
  208. func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) error {
  209. args := [5]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen))}
  210. _, _, err := Syscall(SYS_SOCKETCALL, netGetSockOpt, uintptr(unsafe.Pointer(&args)), 0)
  211. if err != 0 {
  212. return err
  213. }
  214. return nil
  215. }
  216. func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) error {
  217. args := [4]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val)}
  218. _, _, err := Syscall(SYS_SOCKETCALL, netSetSockOpt, uintptr(unsafe.Pointer(&args)), 0)
  219. if err != 0 {
  220. return err
  221. }
  222. return nil
  223. }
  224. func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (int, error) {
  225. var base uintptr
  226. if len(p) > 0 {
  227. base = uintptr(unsafe.Pointer(&p[0]))
  228. }
  229. args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen))}
  230. n, _, err := Syscall(SYS_SOCKETCALL, netRecvFrom, uintptr(unsafe.Pointer(&args)), 0)
  231. if err != 0 {
  232. return 0, err
  233. }
  234. return int(n), nil
  235. }
  236. func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) error {
  237. var base uintptr
  238. if len(p) > 0 {
  239. base = uintptr(unsafe.Pointer(&p[0]))
  240. }
  241. args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen)}
  242. _, _, err := Syscall(SYS_SOCKETCALL, netSendTo, uintptr(unsafe.Pointer(&args)), 0)
  243. if err != 0 {
  244. return err
  245. }
  246. return nil
  247. }
  248. func recvmsg(s int, msg *Msghdr, flags int) (int, error) {
  249. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)}
  250. n, _, err := Syscall(SYS_SOCKETCALL, netRecvMsg, uintptr(unsafe.Pointer(&args)), 0)
  251. if err != 0 {
  252. return 0, err
  253. }
  254. return int(n), nil
  255. }
  256. func sendmsg(s int, msg *Msghdr, flags int) (int, error) {
  257. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)}
  258. n, _, err := Syscall(SYS_SOCKETCALL, netSendMsg, uintptr(unsafe.Pointer(&args)), 0)
  259. if err != 0 {
  260. return 0, err
  261. }
  262. return int(n), nil
  263. }
  264. func Listen(s int, n int) error {
  265. args := [2]uintptr{uintptr(s), uintptr(n)}
  266. _, _, err := Syscall(SYS_SOCKETCALL, netListen, uintptr(unsafe.Pointer(&args)), 0)
  267. if err != 0 {
  268. return err
  269. }
  270. return nil
  271. }
  272. func Shutdown(s, how int) error {
  273. args := [2]uintptr{uintptr(s), uintptr(how)}
  274. _, _, err := Syscall(SYS_SOCKETCALL, netShutdown, uintptr(unsafe.Pointer(&args)), 0)
  275. if err != 0 {
  276. return err
  277. }
  278. return nil
  279. }
  280. //sys poll(fds *PollFd, nfds int, timeout int) (n int, err error)
  281. func Poll(fds []PollFd, timeout int) (n int, err error) {
  282. if len(fds) == 0 {
  283. return poll(nil, 0, timeout)
  284. }
  285. return poll(&fds[0], len(fds), timeout)
  286. }
  287. //sys kexecFileLoad(kernelFd int, initrdFd int, cmdlineLen int, cmdline string, flags int) (err error)
  288. func KexecFileLoad(kernelFd int, initrdFd int, cmdline string, flags int) error {
  289. cmdlineLen := len(cmdline)
  290. if cmdlineLen > 0 {
  291. // Account for the additional NULL byte added by
  292. // BytePtrFromString in kexecFileLoad. The kexec_file_load
  293. // syscall expects a NULL-terminated string.
  294. cmdlineLen++
  295. }
  296. return kexecFileLoad(kernelFd, initrdFd, cmdlineLen, cmdline, flags)
  297. }