| 1 |
// Copyright 2022 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 |
// Package syscall provides the syscall primitives required for the runtime. |
| 6 |
package syscall |
| 7 |
|
| 8 |
import ( |
| 9 |
"internal/goarch" |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
// TODO(https://go.dev/issue/51087): This package is incomplete and currently |
| 14 |
// only contains very minimal support for Linux. |
| 15 |
|
| 16 |
// Syscall6 calls system call number 'num' with arguments a1-6. |
| 17 |
func Syscall6(num, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, errno uintptr) |
| 18 |
|
| 19 |
func EpollCreate1(flags int32) (fd int32, errno uintptr) { |
| 20 |
r1, _, e := Syscall6(SYS_EPOLL_CREATE1, uintptr(flags), 0, 0, 0, 0, 0) |
| 21 |
return int32(r1), e |
| 22 |
} |
| 23 |
|
| 24 |
var _zero uintptr |
| 25 |
|
| 26 |
func EpollWait(epfd int32, events []EpollEvent, maxev, waitms int32) (n int32, errno uintptr) { |
| 27 |
var ev unsafe.Pointer |
| 28 |
if len(events) > 0 { |
| 29 |
ev = unsafe.Pointer(&events[0]) |
| 30 |
} else { |
| 31 |
ev = unsafe.Pointer(&_zero) |
| 32 |
} |
| 33 |
r1, _, e := Syscall6(SYS_EPOLL_PWAIT, uintptr(epfd), uintptr(ev), uintptr(maxev), uintptr(waitms), 0, 0) |
| 34 |
return int32(r1), e |
| 35 |
} |
| 36 |
|
| 37 |
func EpollCtl(epfd, op, fd int32, event *EpollEvent) (errno uintptr) { |
| 38 |
_, _, e := Syscall6(SYS_EPOLL_CTL, uintptr(epfd), uintptr(op), uintptr(fd), uintptr(unsafe.Pointer(event)), 0, 0) |
| 39 |
return e |
| 40 |
} |
| 41 |
|
| 42 |
func Eventfd(initval, flags int32) (fd int32, errno uintptr) { |
| 43 |
r1, _, e := Syscall6(SYS_EVENTFD2, uintptr(initval), uintptr(flags), 0, 0, 0, 0) |
| 44 |
return int32(r1), e |
| 45 |
} |
| 46 |
|
| 47 |
func Open(path *byte, mode int, perm uint32) (fd int, errno uintptr) { |
| 48 |
// Use SYS_OPENAT to match the syscall package. |
| 49 |
dfd := AT_FDCWD |
| 50 |
r1, _, e := Syscall6(SYS_OPENAT, uintptr(dfd), uintptr(unsafe.Pointer(path)), uintptr(mode|O_LARGEFILE), uintptr(perm), 0, 0) |
| 51 |
return int(r1), e |
| 52 |
} |
| 53 |
|
| 54 |
func Close(fd int) (errno uintptr) { |
| 55 |
_, _, e := Syscall6(SYS_CLOSE, uintptr(fd), 0, 0, 0, 0, 0) |
| 56 |
return e |
| 57 |
} |
| 58 |
|
| 59 |
func Read(fd int, p []byte) (n int, errno uintptr) { |
| 60 |
var p0 unsafe.Pointer |
| 61 |
if len(p) > 0 { |
| 62 |
p0 = unsafe.Pointer(&p[0]) |
| 63 |
} else { |
| 64 |
p0 = unsafe.Pointer(&_zero) |
| 65 |
} |
| 66 |
r1, _, e := Syscall6(SYS_READ, uintptr(fd), uintptr(p0), uintptr(len(p)), 0, 0, 0) |
| 67 |
return int(r1), e |
| 68 |
} |
| 69 |
|
| 70 |
func Pread(fd int, p []byte, offset int64) (n int, errno uintptr) { |
| 71 |
var p0 unsafe.Pointer |
| 72 |
if len(p) > 0 { |
| 73 |
p0 = unsafe.Pointer(&p[0]) |
| 74 |
} else { |
| 75 |
p0 = unsafe.Pointer(&_zero) |
| 76 |
} |
| 77 |
var r1, e uintptr |
| 78 |
switch goarch.GOARCH { |
| 79 |
case "386": |
| 80 |
r1, _, e = Syscall6(SYS_PREAD64, uintptr(fd), uintptr(p0), uintptr(len(p)), uintptr(offset), uintptr(offset>>32), 0) |
| 81 |
case "arm", "mipsle": |
| 82 |
r1, _, e = Syscall6(SYS_PREAD64, uintptr(fd), uintptr(p0), uintptr(len(p)), 0, uintptr(offset), uintptr(offset>>32)) |
| 83 |
case "mips": |
| 84 |
r1, _, e = Syscall6(SYS_PREAD64, uintptr(fd), uintptr(p0), uintptr(len(p)), 0, uintptr(offset>>32), uintptr(offset)) |
| 85 |
default: |
| 86 |
r1, _, e = Syscall6(SYS_PREAD64, uintptr(fd), uintptr(p0), uintptr(len(p)), uintptr(offset), 0, 0) |
| 87 |
} |
| 88 |
return int(r1), e |
| 89 |
} |