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