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