1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4//go:build !386 && !arm && !ppc64le && (darwin || freebsd || linux || netbsd || windows)
5
6package purego
7
8import (
9 "runtime"
10 "unsafe"
11)
12
13const (
14 maxArgs = 32
15)
16
17type syscallArgs struct {
18 fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr
19 a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, a31, a32 uintptr
20 f1, f2, f3, f4, f5, f6, f7, f8 uintptr
21 arm64_r8 uintptr
22}
23
24func syscall_SyscallN(fn uintptr, sysargs []uintptr, floats []uintptr, r8 uintptr) *syscallArgs {
25 s := &syscallArgs{
26 fn: fn,
27 a1: sysargs[0], a2: sysargs[1], a3: sysargs[2], a4: sysargs[3],
28 a5: sysargs[4], a6: sysargs[5], a7: sysargs[6], a8: sysargs[7],
29 a9: sysargs[8], a10: sysargs[9], a11: sysargs[10], a12: sysargs[11],
30 a13: sysargs[12], a14: sysargs[13], a15: sysargs[14], a16: sysargs[15],
31 a17: sysargs[16], a18: sysargs[17], a19: sysargs[18], a20: sysargs[19],
32 a21: sysargs[20], a22: sysargs[21], a23: sysargs[22], a24: sysargs[23],
33 a25: sysargs[24], a26: sysargs[25], a27: sysargs[26], a28: sysargs[27],
34 a29: sysargs[28], a30: sysargs[29], a31: sysargs[30], a32: sysargs[31],
35 f1: floats[0], f2: floats[1], f3: floats[2], f4: floats[3],
36 f5: floats[4], f6: floats[5], f7: floats[6], f8: floats[7],
37 arm64_r8: r8,
38 }
39 runtime_cgocall(syscallXABI0, unsafe.Pointer(s))
40 return s
41}
42
43// SyscallN takes fn, a C function pointer and a list of arguments as uintptr.
44// There is an internal maximum number of arguments that SyscallN can take. It panics
45// when the maximum is exceeded. It returns the result and the libc error code if there is one.
46//
47// In order to call this function properly make sure to follow all the rules specified in [unsafe.Pointer]
48// especially point 4.
49//
50// NOTE: SyscallN does not properly call functions that have both integer and float parameters.
51// See discussion comment https://github.com/ebiten/purego/pull/1#issuecomment-1128057607
52// for an explanation of why that is.
53//
54// On amd64, if there are more than 8 floats the 9th and so on will be placed incorrectly on the
55// stack.
56//
57// The pragma go:nosplit is not needed at this function declaration because it uses go:uintptrescapes
58// which forces all the objects that the uintptrs point to onto the heap where a stack split won't affect
59// their memory location.
60//
61//go:uintptrescapes
62func SyscallN(fn uintptr, args ...uintptr) (r1, r2, err uintptr) {
63 if fn == 0 {
64 panic("purego: fn is nil")
65 }
66 if len(args) > maxArgs {
67 panic("purego: too many arguments to SyscallN")
68 }
69
70 // Windows uses syscall.SyscallN in syscall_windows.go.
71 if runtime.GOOS == "windows" {
72 return syscall_syscallN(fn, args...)
73 }
74
75 // add padding so there is no out-of-bounds slicing
76 var tmp [maxArgs]uintptr
77 copy(tmp[:], args)
78 var floats [maxArgs]uintptr
79 copy(floats[:], tmp[:])
80 s := syscall_SyscallN(fn, tmp[:], floats[:], 0)
81 return s.a1, s.a2, s.a3
82}