1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4//go:build darwin || freebsd || (linux && (386 || amd64 || arm || arm64 || loong64 || riscv64)) || netbsd
5
6package purego
7
8import (
9 "reflect"
10 "runtime"
11 "sync"
12 "unsafe"
13)
14
15var syscall15XABI0 uintptr
16
17func syscall_syscall15X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2, err uintptr) {
18 args := thePool.Get().(*syscall15Args)
19 defer thePool.Put(args)
20
21 *args = syscall15Args{
22 fn: fn,
23 a1: a1, a2: a2, a3: a3, a4: a4, a5: a5, a6: a6, a7: a7, a8: a8,
24 a9: a9, a10: a10, a11: a11, a12: a12, a13: a13, a14: a14, a15: a15,
25 f1: a1, f2: a2, f3: a3, f4: a4, f5: a5, f6: a6, f7: a7, f8: a8,
26 }
27
28 runtime_cgocall(syscall15XABI0, unsafe.Pointer(args))
29 return args.a1, args.a2, 0
30}
31
32// NewCallback converts a Go function to a function pointer conforming to the C calling convention.
33// This is useful when interoperating with C code requiring callbacks. The argument is expected to be a
34// function with zero or one uintptr-sized result. The function must not have arguments with size larger than the size
35// of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated
36// for these callbacks is never released. At least 2000 callbacks can always be created. Although this function
37// provides similar functionality to windows.NewCallback it is distinct.
38func NewCallback(fn any) uintptr {
39 ty := reflect.TypeOf(fn)
40 for i := 0; i < ty.NumIn(); i++ {
41 in := ty.In(i)
42 if !in.AssignableTo(reflect.TypeOf(CDecl{})) {
43 continue
44 }
45 if i != 0 {
46 panic("purego: CDecl must be the first argument")
47 }
48 }
49 return compileCallback(fn)
50}
51
52// maxCb is the maximum number of callbacks
53// only increase this if you have added more to the callbackasm function
54const maxCB = 2000
55
56var cbs struct {
57 lock sync.Mutex
58 numFn int // the number of functions currently in cbs.funcs
59 funcs [maxCB]reflect.Value // the saved callbacks
60}
61
62type callbackArgs struct {
63 index uintptr
64 // args points to the argument block.
65 //
66 // The structure of the arguments goes
67 // float registers followed by the
68 // integer registers followed by the stack.
69 //
70 // This variable is treated as a continuous
71 // block of memory containing all of the arguments
72 // for this callback.
73 args unsafe.Pointer
74 // Below are out-args from callbackWrap
75 result uintptr
76}
77
78func compileCallback(fn any) uintptr {
79 val := reflect.ValueOf(fn)
80 if val.Kind() != reflect.Func {
81 panic("purego: the type must be a function but was not")
82 }
83 if val.IsNil() {
84 panic("purego: function must not be nil")
85 }
86 ty := val.Type()
87 for i := 0; i < ty.NumIn(); i++ {
88 in := ty.In(i)
89 switch in.Kind() {
90 case reflect.Struct:
91 if i == 0 && in.AssignableTo(reflect.TypeOf(CDecl{})) {
92 continue
93 }
94 fallthrough
95 case reflect.Interface, reflect.Func, reflect.Slice,
96 reflect.Chan, reflect.Complex64, reflect.Complex128,
97 reflect.String, reflect.Map, reflect.Invalid:
98 panic("purego: unsupported argument type: " + in.Kind().String())
99 }
100 }
101output:
102 switch {
103 case ty.NumOut() == 1:
104 switch ty.Out(0).Kind() {
105 case reflect.Pointer, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
106 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
107 reflect.Bool, reflect.UnsafePointer:
108 break output
109 }
110 panic("purego: unsupported return type: " + ty.String())
111 case ty.NumOut() > 1:
112 panic("purego: callbacks can only have one return")
113 }
114 cbs.lock.Lock()
115 defer cbs.lock.Unlock()
116 if cbs.numFn >= maxCB {
117 panic("purego: the maximum number of callbacks has been reached")
118 }
119 cbs.funcs[cbs.numFn] = val
120 cbs.numFn++
121 return callbackasmAddr(cbs.numFn - 1)
122}
123
124const ptrSize = unsafe.Sizeof((*int)(nil))
125
126const callbackMaxFrame = 64 * ptrSize
127
128// callbackasm is implemented in zcallback_GOOS_GOARCH.s
129//
130//go:linkname __callbackasm callbackasm
131var __callbackasm byte
132var callbackasmABI0 = uintptr(unsafe.Pointer(&__callbackasm))
133
134// callbackWrap_call allows the calling of the ABIInternal wrapper
135// which is required for runtime.cgocallback without the
136// <ABIInternal> tag which is only allowed in the runtime.
137// This closure is used inside sys_darwin_GOARCH.s
138var callbackWrap_call = callbackWrap
139
140// callbackWrap is called by assembly code which determines which Go function to call.
141// This function takes the arguments and passes them to the Go function and returns the result.
142func callbackWrap(a *callbackArgs) {
143 cbs.lock.Lock()
144 fn := cbs.funcs[a.index]
145 cbs.lock.Unlock()
146 fnType := fn.Type()
147 args := make([]reflect.Value, fnType.NumIn())
148 frame := (*[callbackMaxFrame]uintptr)(a.args)
149 // floatsN and intsN track the number of register slots used, not argument count.
150 // This distinction matters on ARM32 where float64 uses 2 slots (32-bit registers).
151 var floatsN int
152 var intsN int
153 // stackSlot points to the index into frame of the current stack element.
154 // The stack begins after the float and integer registers.
155 stackSlot := numOfIntegerRegisters() + numOfFloatRegisters()
156 // stackByteOffset tracks the byte offset within the stack area for Darwin ARM64
157 // tight packing. On Darwin ARM64, C passes small types packed on the stack.
158 stackByteOffset := uintptr(0)
159 for i := range args {
160 // slots is the number of pointer-sized slots the argument takes
161 var slots int
162 inType := fnType.In(i)
163 switch inType.Kind() {
164 case reflect.Float32, reflect.Float64:
165 slots = int((fnType.In(i).Size() + ptrSize - 1) / ptrSize)
166 if floatsN+slots > numOfFloatRegisters() {
167 if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
168 // Darwin ARM64: read from packed stack with proper alignment
169 args[i] = callbackArgFromStack(a.args, stackSlot, &stackByteOffset, inType)
170 } else {
171 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[stackSlot])).Elem()
172 stackSlot += slots
173 }
174 } else {
175 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[floatsN])).Elem()
176 }
177 floatsN += slots
178 case reflect.Struct:
179 // This is the CDecl field
180 args[i] = reflect.Zero(inType)
181 default:
182 slots = int((inType.Size() + ptrSize - 1) / ptrSize)
183 if intsN+slots > numOfIntegerRegisters() {
184 if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
185 // Darwin ARM64: read from packed stack with proper alignment
186 args[i] = callbackArgFromStack(a.args, stackSlot, &stackByteOffset, inType)
187 } else {
188 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[stackSlot])).Elem()
189 stackSlot += slots
190 }
191 } else {
192 // the integers begin after the floats in frame
193 pos := intsN + numOfFloatRegisters()
194 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[pos])).Elem()
195 }
196 intsN += slots
197 }
198 }
199 ret := fn.Call(args)
200 if len(ret) > 0 {
201 switch k := ret[0].Kind(); k {
202 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
203 a.result = uintptr(ret[0].Uint())
204 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
205 a.result = uintptr(ret[0].Int())
206 case reflect.Bool:
207 if ret[0].Bool() {
208 a.result = 1
209 } else {
210 a.result = 0
211 }
212 case reflect.Pointer:
213 a.result = ret[0].Pointer()
214 case reflect.UnsafePointer:
215 a.result = ret[0].Pointer()
216 default:
217 panic("purego: unsupported kind: " + k.String())
218 }
219 }
220}
221
222// callbackArgFromStack reads an argument from the tightly-packed stack area on Darwin ARM64.
223// The C ABI on Darwin ARM64 packs small types on the stack without padding to 8 bytes.
224// This function handles proper alignment and advances stackByteOffset accordingly.
225func callbackArgFromStack(argsBase unsafe.Pointer, stackSlot int, stackByteOffset *uintptr, inType reflect.Type) reflect.Value {
226 // Calculate base address of stack area (after float and int registers)
227 stackBase := unsafe.Add(argsBase, stackSlot*int(ptrSize))
228
229 // Get type's natural alignment
230 align := uintptr(inType.Align())
231 size := inType.Size()
232
233 // Align the offset
234 if *stackByteOffset%align != 0 {
235 *stackByteOffset = (*stackByteOffset + align - 1) &^ (align - 1)
236 }
237
238 // Read value at aligned offset
239 ptr := unsafe.Add(stackBase, *stackByteOffset)
240 *stackByteOffset += size
241
242 return reflect.NewAt(inType, ptr).Elem()
243}
244
245// callbackasmAddr returns address of runtime.callbackasm
246// function adjusted by i.
247// On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
248// and we want callback to arrive at
249// correspondent call instruction instead of start of
250// runtime.callbackasm.
251// On ARM, runtime.callbackasm is a series of mov and branch instructions.
252// R12 is loaded with the callback index. Each entry is two instructions,
253// hence 8 bytes.
254func callbackasmAddr(i int) uintptr {
255 var entrySize int
256 switch runtime.GOARCH {
257 default:
258 panic("purego: unsupported architecture")
259 case "amd64":
260 // On amd64, each callback entry is just a CALL instruction (5 bytes)
261 entrySize = 5
262 case "386":
263 // On 386, each callback entry is MOVL $imm, CX (5 bytes) + JMP (5 bytes)
264 entrySize = 10
265 case "arm", "arm64", "loong64", "riscv64":
266 // On ARM, ARM64, Loong64, and RISCV64, each entry is a MOV instruction
267 // followed by a branch instruction
268 entrySize = 8
269 }
270 return callbackasmABI0 + uintptr(i*entrySize)
271}