You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

303 lines
9.6 KiB

  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 accept4(s int, rsa *RawSockaddrAny, addrlen *_Socklen, flags int) (int, error) {
  128. args := [4]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)), uintptr(flags)}
  129. fd, _, err := Syscall(SYS_SOCKETCALL, netAccept4, uintptr(unsafe.Pointer(&args)), 0)
  130. if err != 0 {
  131. return 0, err
  132. }
  133. return int(fd), nil
  134. }
  135. func getsockname(s int, rsa *RawSockaddrAny, addrlen *_Socklen) error {
  136. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen))}
  137. _, _, err := RawSyscall(SYS_SOCKETCALL, netGetSockName, uintptr(unsafe.Pointer(&args)), 0)
  138. if err != 0 {
  139. return err
  140. }
  141. return nil
  142. }
  143. func getpeername(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, netGetPeerName, uintptr(unsafe.Pointer(&args)), 0)
  146. if err != 0 {
  147. return err
  148. }
  149. return nil
  150. }
  151. func socketpair(domain int, typ int, flags int, fd *[2]int32) error {
  152. args := [4]uintptr{uintptr(domain), uintptr(typ), uintptr(flags), uintptr(unsafe.Pointer(fd))}
  153. _, _, err := RawSyscall(SYS_SOCKETCALL, netSocketPair, uintptr(unsafe.Pointer(&args)), 0)
  154. if err != 0 {
  155. return err
  156. }
  157. return nil
  158. }
  159. func bind(s int, addr unsafe.Pointer, addrlen _Socklen) error {
  160. args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)}
  161. _, _, err := Syscall(SYS_SOCKETCALL, netBind, uintptr(unsafe.Pointer(&args)), 0)
  162. if err != 0 {
  163. return err
  164. }
  165. return nil
  166. }
  167. func connect(s int, addr unsafe.Pointer, addrlen _Socklen) error {
  168. args := [3]uintptr{uintptr(s), uintptr(addr), uintptr(addrlen)}
  169. _, _, err := Syscall(SYS_SOCKETCALL, netConnect, uintptr(unsafe.Pointer(&args)), 0)
  170. if err != 0 {
  171. return err
  172. }
  173. return nil
  174. }
  175. func socket(domain int, typ int, proto int) (int, error) {
  176. args := [3]uintptr{uintptr(domain), uintptr(typ), uintptr(proto)}
  177. fd, _, err := RawSyscall(SYS_SOCKETCALL, netSocket, uintptr(unsafe.Pointer(&args)), 0)
  178. if err != 0 {
  179. return 0, err
  180. }
  181. return int(fd), nil
  182. }
  183. func getsockopt(s int, level int, name int, val unsafe.Pointer, vallen *_Socklen) error {
  184. args := [5]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val), uintptr(unsafe.Pointer(vallen))}
  185. _, _, err := Syscall(SYS_SOCKETCALL, netGetSockOpt, uintptr(unsafe.Pointer(&args)), 0)
  186. if err != 0 {
  187. return err
  188. }
  189. return nil
  190. }
  191. func setsockopt(s int, level int, name int, val unsafe.Pointer, vallen uintptr) error {
  192. args := [5]uintptr{uintptr(s), uintptr(level), uintptr(name), uintptr(val), vallen}
  193. _, _, err := Syscall(SYS_SOCKETCALL, netSetSockOpt, uintptr(unsafe.Pointer(&args)), 0)
  194. if err != 0 {
  195. return err
  196. }
  197. return nil
  198. }
  199. func recvfrom(s int, p []byte, flags int, from *RawSockaddrAny, fromlen *_Socklen) (int, error) {
  200. var base uintptr
  201. if len(p) > 0 {
  202. base = uintptr(unsafe.Pointer(&p[0]))
  203. }
  204. args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(fromlen))}
  205. n, _, err := Syscall(SYS_SOCKETCALL, netRecvFrom, uintptr(unsafe.Pointer(&args)), 0)
  206. if err != 0 {
  207. return 0, err
  208. }
  209. return int(n), nil
  210. }
  211. func sendto(s int, p []byte, flags int, to unsafe.Pointer, addrlen _Socklen) error {
  212. var base uintptr
  213. if len(p) > 0 {
  214. base = uintptr(unsafe.Pointer(&p[0]))
  215. }
  216. args := [6]uintptr{uintptr(s), base, uintptr(len(p)), uintptr(flags), uintptr(to), uintptr(addrlen)}
  217. _, _, err := Syscall(SYS_SOCKETCALL, netSendTo, uintptr(unsafe.Pointer(&args)), 0)
  218. if err != 0 {
  219. return err
  220. }
  221. return nil
  222. }
  223. func recvmsg(s int, msg *Msghdr, flags int) (int, error) {
  224. args := [3]uintptr{uintptr(s), uintptr(unsafe.Pointer(msg)), uintptr(flags)}
  225. n, _, err := Syscall(SYS_SOCKETCALL, netRecvMsg, uintptr(unsafe.Pointer(&args)), 0)
  226. if err != 0 {
  227. return 0, err
  228. }
  229. return int(n), nil
  230. }
  231. func sendmsg(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, netSendMsg, uintptr(unsafe.Pointer(&args)), 0)
  234. if err != 0 {
  235. return 0, err
  236. }
  237. return int(n), nil
  238. }
  239. func Listen(s int, n int) error {
  240. args := [2]uintptr{uintptr(s), uintptr(n)}
  241. _, _, err := Syscall(SYS_SOCKETCALL, netListen, uintptr(unsafe.Pointer(&args)), 0)
  242. if err != 0 {
  243. return err
  244. }
  245. return nil
  246. }
  247. func Shutdown(s, how int) error {
  248. args := [2]uintptr{uintptr(s), uintptr(how)}
  249. _, _, err := Syscall(SYS_SOCKETCALL, netShutdown, uintptr(unsafe.Pointer(&args)), 0)
  250. if err != 0 {
  251. return err
  252. }
  253. return nil
  254. }
  255. //sys kexecFileLoad(kernelFd int, initrdFd int, cmdlineLen int, cmdline string, flags int) (err error)
  256. func KexecFileLoad(kernelFd int, initrdFd int, cmdline string, flags int) error {
  257. cmdlineLen := len(cmdline)
  258. if cmdlineLen > 0 {
  259. // Account for the additional NULL byte added by
  260. // BytePtrFromString in kexecFileLoad. The kexec_file_load
  261. // syscall expects a NULL-terminated string.
  262. cmdlineLen++
  263. }
  264. return kexecFileLoad(kernelFd, initrdFd, cmdlineLen, cmdline, flags)
  265. }