hybridgroup.gobot/system/syscall.go

57 lines
2.0 KiB
Go
Raw Normal View History

2022-11-21 02:22:26 +08:00
package system
2014-11-08 08:56:13 +08:00
import (
2022-10-27 00:21:34 +08:00
"unsafe"
"golang.org/x/sys/unix"
)
// SyscallErrno wraps the "unix.Errno"
type SyscallErrno unix.Errno
// wrapping for used constants of unix package
const (
Syscall_SYS_IOCTL = unix.SYS_IOCTL
Syscall_EINVAL = unix.EINVAL
Syscall_EBUSY = unix.EBUSY
Syscall_EFAULT = unix.EFAULT
2014-11-08 08:56:13 +08:00
)
2022-11-05 14:42:28 +08:00
// nativeSyscall represents the native Syscall
type nativeSyscall struct{}
2014-11-08 08:56:13 +08:00
// Syscall calls the native unix.Syscall, implements the SystemCaller interface
// Note: It would be possible to transfer the address as an unsafe.Pointer to e.g. a byte, uint16 or integer variable.
// The unpack process here would be as follows:
// * convert the payload back to the pointer: addrPtr := (*byte)(payload)
// * call with the content converted to uintptr: r1, r2, errNo = unix.Syscall(trap, f.Fd(), signal, uintptr(*addrPtr))
// This has the main disadvantage, that if someone change the type of the address at caller side, the compiler will not
// detect this problem and this unpack procedure would cause unpredictable results.
// So the decision was taken to give the address here as a separate parameter, although it is not used in every call.
// Note also, that the size of the address variable at Kernel side is u16, therefore uint16 is used here.
//
//nolint:nonamedreturns // useful here
func (sys *nativeSyscall) syscall(
trap uintptr,
f File,
signal uintptr,
payload unsafe.Pointer,
address uint16,
) (r1, r2 uintptr, err SyscallErrno) {
var errNo unix.Errno
if signal == I2C_SLAVE {
// this is the setup for the address, it just needs to be converted to an uintptr,
// the given payload is not used in this case, see the comment on the function
r1, r2, errNo = unix.Syscall(trap, f.Fd(), signal, uintptr(address))
} else {
r1, r2, errNo = unix.Syscall(trap, f.Fd(), signal, uintptr(payload))
}
return r1, r2, SyscallErrno(errNo)
}
// Error implements the error interface. It wraps the "unix.Errno.Error()".
func (e SyscallErrno) Error() string {
return unix.Errno(e).Error()
2014-11-08 08:56:13 +08:00
}