| 1 |
//go:build linux && cgo |
| 2 |
// +build linux,cgo |
| 3 |
|
| 4 |
package psx // import "kernel.org/pub/linux/libs/security/libcap/psx" |
| 5 |
|
| 6 |
import ( |
| 7 |
"runtime" |
| 8 |
"sync" |
| 9 |
"syscall" |
| 10 |
) |
| 11 |
|
| 12 |
// #include <errno.h> |
| 13 |
// #include "psx_syscall.h" |
| 14 |
// |
| 15 |
// long __errno_too(long set_errno) { |
| 16 |
// long v = errno; |
| 17 |
// if (set_errno >= 0) { |
| 18 |
// errno = set_errno; |
| 19 |
// } |
| 20 |
// return v; |
| 21 |
// } |
| 22 |
import "C" |
| 23 |
|
| 24 |
// setErrno returns the current C.errno value and, if v >= 0, sets the |
| 25 |
// CGo errno for a random pthread to value v. If you want some |
| 26 |
// consistency, this needs to be called from runtime.LockOSThread() |
| 27 |
// code. This function is only defined for testing purposes. The psx.c |
| 28 |
// code should properly handle the case that a non-zero errno is saved |
| 29 |
// and restored independently of what these Syscall[36]() functions |
| 30 |
// observe. |
| 31 |
func setErrno(v int) int { |
| 32 |
return int(C.__errno_too(C.long(v))) |
| 33 |
} |
| 34 |
|
| 35 |
var makeFatal sync.Once |
| 36 |
|
| 37 |
// forceFatal configures the psx_syscall mechanism to PSX_ERROR. |
| 38 |
func forceFatal() { |
| 39 |
makeFatal.Do(func() { |
| 40 |
C.psx_set_sensitivity(C.PSX_ERROR) |
| 41 |
}) |
| 42 |
} |
| 43 |
|
| 44 |
//go:uintptrescapes |
| 45 |
|
| 46 |
// Syscall3 performs a 3 argument syscall. Syscall3 differs from |
| 47 |
// syscall.[Raw]Syscall() insofar as it is simultaneously executed on |
| 48 |
// every thread of the combined Go and CGo runtimes. If any of the |
| 49 |
// return values of these sumultaneous system calls differs, the |
| 50 |
// runtime will cause the program to exit. |
| 51 |
// |
| 52 |
// Syscall3 works differently depending on whether CGO_ENABLED is 1 or |
| 53 |
// 0 at compile time. |
| 54 |
// |
| 55 |
// If CGO_ENABLED=1 it uses the libpsx function C.psx_syscall3(), with |
| 56 |
// libpsx:psx_set_sensitivity(PSX_ERROR) - which means the program |
| 57 |
// will be signo=33 killed when inconsistent return values are |
| 58 |
// returned for any pthread. |
| 59 |
// |
| 60 |
// If CGO_ENABLED=0 it redirects to the go1.16+ |
| 61 |
// syscall.AllThreadsSyscall() function. This function panics if |
| 62 |
// inconsistent return values are returned for any pthread. |
| 63 |
func Syscall3(syscallnr, arg1, arg2, arg3 uintptr) (uintptr, uintptr, syscall.Errno) { |
| 64 |
forceFatal() |
| 65 |
// We lock to the OSThread here because we may need errno to |
| 66 |
// be the one for this thread. |
| 67 |
runtime.LockOSThread() |
| 68 |
defer runtime.UnlockOSThread() |
| 69 |
|
| 70 |
v := C.psx_syscall3(C.long(syscallnr), C.long(arg1), C.long(arg2), C.long(arg3)) |
| 71 |
var errno syscall.Errno |
| 72 |
if v < 0 { |
| 73 |
errno = syscall.Errno(C.__errno_too(-1)) |
| 74 |
} |
| 75 |
return uintptr(v), uintptr(v), errno |
| 76 |
} |
| 77 |
|
| 78 |
//go:uintptrescapes |
| 79 |
|
| 80 |
// Syscall6 performs a 6 argument syscall on every thread of the |
| 81 |
// combined Go and CGo runtimes. Other than the number of syscall |
| 82 |
// arguments, its behavior is identical to that of Syscall3() - see |
| 83 |
// above for the full documentation. |
| 84 |
func Syscall6(syscallnr, arg1, arg2, arg3, arg4, arg5, arg6 uintptr) (uintptr, uintptr, syscall.Errno) { |
| 85 |
forceFatal() |
| 86 |
// We lock to the OSThread here because we may need errno to |
| 87 |
// be the one for this thread. |
| 88 |
runtime.LockOSThread() |
| 89 |
defer runtime.UnlockOSThread() |
| 90 |
|
| 91 |
v := C.psx_syscall6(C.long(syscallnr), C.long(arg1), C.long(arg2), C.long(arg3), C.long(arg4), C.long(arg5), C.long(arg6)) |
| 92 |
var errno syscall.Errno |
| 93 |
if v < 0 { |
| 94 |
errno = syscall.Errno(C.__errno_too(-1)) |
| 95 |
} |
| 96 |
return uintptr(v), uintptr(v), errno |
| 97 |
} |