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