blob: c17b4f373fe3cab7c0a333f315e2dbfd627738b5 [file] [log] [blame]
Andrew G. Morgan90192cd2020-12-11 23:27:50 -08001// +build linux,cgo
2
3package psx // import "kernel.org/pub/linux/libs/security/libcap/psx"
4
5import (
6 "runtime"
7 "syscall"
8)
9
10// #cgo LDFLAGS: -lpthread -Wl,-wrap,pthread_create
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// }
22import "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.
31func setErrno(v int) int {
32 return int(C.__errno_too(C.long(v)))
33}
34
35// Syscall3 performs a 3 argument syscall using the libpsx C function
36// psx_syscall3(). Syscall3 differs from syscall.[Raw]Syscall()
37// insofar as it is simultaneously executed on every pthread of the
38// combined Go and CGo runtimes.
39func Syscall3(syscallnr, arg1, arg2, arg3 uintptr) (uintptr, uintptr, syscall.Errno) {
40 runtime.LockOSThread()
41 defer runtime.UnlockOSThread()
42
43 v := C.psx_syscall3(C.long(syscallnr), C.long(arg1), C.long(arg2), C.long(arg3))
44 var errno syscall.Errno
45 if v < 0 {
46 errno = syscall.Errno(C.__errno_too(-1))
47 }
48 return uintptr(v), uintptr(v), errno
49}
50
51// Syscall6 performs a 6 argument syscall using the libpsx C function
52// psx_syscall6(). Syscall6 differs from syscall.[Raw]Syscall6() insofar as
53// it is simultaneously executed on every pthread of the combined Go
54// and CGo runtimes.
55func Syscall6(syscallnr, arg1, arg2, arg3, arg4, arg5, arg6 uintptr) (uintptr, uintptr, syscall.Errno) {
56 runtime.LockOSThread()
57 defer runtime.UnlockOSThread()
58
59 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))
60 var errno syscall.Errno
61 if v < 0 {
62 errno = syscall.Errno(C.__errno_too(-1))
63 }
64 return uintptr(v), uintptr(v), errno
65}