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 || ppc64le || riscv64 || (s390x && (cgo || go1.27)))) || netbsd
5
6package purego
7
8import (
9 "math"
10 "reflect"
11 "runtime"
12 "sync"
13 "unsafe"
14)
15
16var syscallXABI0 uintptr
17
18func syscall_syscallN(fn uintptr, args ...uintptr) (r1, r2, err uintptr) {
19 panic("purego: syscall_syscallN is only supported on windows")
20}
21
22// NewCallback converts a Go function to a function pointer conforming to the C calling convention.
23// This is useful when interoperating with C code requiring callbacks. The argument is expected to be a
24// function with zero or one uintptr-sized result. The function must not have arguments with size larger than the size
25// of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated
26// for these callbacks is never released. At least 2000 callbacks can always be created. Although this function
27// provides similar functionality to windows.NewCallback it is distinct.
28func NewCallback(fn any) uintptr {
29 ty := reflect.TypeOf(fn)
30 for i := 0; i < ty.NumIn(); i++ {
31 in := ty.In(i)
32 if !in.AssignableTo(reflect.TypeFor[CDecl]()) {
33 continue
34 }
35 if i != 0 {
36 panic("purego: CDecl must be the first argument")
37 }
38 }
39 return compileCallback(fn)
40}
41
42// maxCb is the maximum number of callbacks
43// only increase this if you have added more to the callbackasm function
44const maxCB = 2000
45
46var cbs struct {
47 lock sync.Mutex
48 numFn int // the number of functions currently in cbs.funcs
49 funcs [maxCB]reflect.Value // the saved callbacks
50}
51
52func compileCallback(fn any) uintptr {
53 val := reflect.ValueOf(fn)
54 if val.Kind() != reflect.Func {
55 panic("purego: the type must be a function but was not")
56 }
57 if val.IsNil() {
58 panic("purego: function must not be nil")
59 }
60 ty := val.Type()
61 for i := 0; i < ty.NumIn(); i++ {
62 in := ty.In(i)
63 switch in.Kind() {
64 case reflect.Struct:
65 if i == 0 && in.AssignableTo(reflect.TypeFor[CDecl]()) {
66 continue
67 }
68 ensureCallbackStructSupported()
69 checkStructFieldsSupported(in)
70 continue
71 case reflect.Interface, reflect.Func, reflect.Slice,
72 reflect.Chan, reflect.Complex64, reflect.Complex128,
73 reflect.String, reflect.Map, reflect.Invalid:
74 panic("purego: unsupported argument type: " + in.Kind().String())
75 }
76 }
77output:
78 switch {
79 case ty.NumOut() == 1:
80 switch ty.Out(0).Kind() {
81 case reflect.Struct:
82 ensureCallbackStructSupported()
83 checkStructFieldsSupported(ty.Out(0))
84 break output
85 case reflect.Pointer, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
86 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
87 reflect.Bool, reflect.UnsafePointer:
88 break output
89 }
90 panic("purego: unsupported return type: " + ty.String())
91 case ty.NumOut() > 1:
92 panic("purego: callbacks can only have one return")
93 }
94 cbs.lock.Lock()
95 defer cbs.lock.Unlock()
96 if cbs.numFn >= maxCB {
97 panic("purego: the maximum number of callbacks has been reached")
98 }
99 cbs.funcs[cbs.numFn] = val
100 cbs.numFn++
101 return callbackasmAddr(cbs.numFn - 1)
102}
103
104const ptrSize = unsafe.Sizeof((*int)(nil))
105
106const callbackMaxFrame = 64 * ptrSize
107
108// callbackasm is implemented in zcallback_GOOS_GOARCH.s
109//
110//go:linkname __callbackasm callbackasm
111var __callbackasm byte
112var callbackasmABI0 = uintptr(unsafe.Pointer(&__callbackasm))
113
114// callbackWrap_call allows the calling of the ABIInternal wrapper
115// which is required for runtime.cgocallback without the
116// <ABIInternal> tag which is only allowed in the runtime.
117// This closure is used inside sys_darwin_GOARCH.s
118var callbackWrap_call = callbackWrap
119
120// callbackWrap is called by assembly code which determines which Go function to call.
121// This function takes the arguments and passes them to the Go function and returns the result.
122func callbackWrap(a *callbackArgs) {
123 cbs.lock.Lock()
124 fn := cbs.funcs[a.index]
125 cbs.lock.Unlock()
126 fnType := fn.Type()
127 args := make([]reflect.Value, fnType.NumIn())
128 frame := (*[callbackMaxFrame]uintptr)(a.args)
129 // stackFrame points to stack-passed arguments. On most architectures this is
130 // contiguous with frame (after register args), but on ppc64le it's separate.
131 var stackFrame *[callbackMaxFrame]uintptr
132 var intFrame *[callbackMaxFrame]uintptr
133 if sf := a.stackFrame(); sf != nil {
134 // Only ppc64le uses separate stackArgs pointer due to NOSPLIT constraints
135 stackFrame = (*[callbackMaxFrame]uintptr)(sf)
136 }
137 if intf := a.intFrame(); intf != nil {
138 intFrame = (*[callbackMaxFrame]uintptr)(intf)
139 }
140 // floatsN and intsN track the number of register slots used, not argument count.
141 // This distinction matters on ARM32 where float64 uses 2 slots (32-bit registers).
142 var floatsN int
143 var intsN int
144 // On amd64/loong64/ppc64le/riscv64/s390x, when returning a struct larger than
145 // maxRegAllocStructSize, the caller passes a hidden pointer in the first integer
146 // register. Skip it to avoid misreading it as the first function argument.
147 if (runtime.GOARCH == "amd64" || runtime.GOARCH == "loong64" || runtime.GOARCH == "riscv64" || runtime.GOARCH == "s390x") &&
148 fnType.NumOut() == 1 && fnType.Out(0).Kind() == reflect.Struct &&
149 fnType.Out(0).Size() > maxRegAllocStructSize {
150 intsN = 1
151 }
152 // stackSlot points to the index into frame (or stackFrame) of the current stack element.
153 // When stackFrame is nil, stack begins after float and integer registers in frame.
154 // When stackFrame is not nil (ppc64le), stackSlot indexes into stackFrame starting at 0.
155 stackSlot := numOfIntegerRegisters() + numOfFloatRegisters()
156 if stackFrame != nil {
157 // ppc64le: stackArgs is a separate pointer, indices start at 0
158 stackSlot = 0
159 }
160 // stackByteOffset tracks the byte offset within the stack area for Darwin ARM64
161 // tight packing. On Darwin ARM64, C passes small types packed on the stack.
162 stackByteOffset := uintptr(0)
163 for i := range args {
164 // slots is the number of pointer-sized slots the argument takes
165 var slots int
166 inType := fnType.In(i)
167 switch inType.Kind() {
168 case reflect.Float32, reflect.Float64:
169 slots = int((fnType.In(i).Size() + ptrSize - 1) / ptrSize)
170 if floatsN+slots > numOfFloatRegisters() {
171 if isDarwin && runtime.GOARCH == "arm64" {
172 // Darwin ARM64: read from packed stack with proper alignment
173 args[i] = callbackArgFromStack(a.args, stackSlot, &stackByteOffset, inType)
174 } else if stackFrame != nil {
175 // ppc64le/s390x: stack args are in separate stackFrame
176 switch runtime.GOARCH {
177 case "ppc64le":
178 args[i] = callbackFloatFromDoubleSlot(unsafe.Pointer(&stackFrame[stackSlot]), inType)
179 case "s390x":
180 // s390x big-endian: sub-8-byte values are right-justified
181 args[i] = callbackArgFromSlotBigEndian(unsafe.Pointer(&stackFrame[stackSlot]), inType)
182 default:
183 args[i] = reflect.NewAt(inType, unsafe.Pointer(&stackFrame[stackSlot])).Elem()
184 }
185 stackSlot += slots
186 } else {
187 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[stackSlot])).Elem()
188 stackSlot += slots
189 }
190 } else {
191 switch runtime.GOARCH {
192 case "ppc64le":
193 args[i] = callbackFloatFromDoubleSlot(unsafe.Pointer(&frame[floatsN]), inType)
194 case "s390x":
195 // s390x big-endian: float32 is right-justified in 8-byte FPR slot
196 args[i] = callbackArgFromSlotBigEndian(unsafe.Pointer(&frame[floatsN]), inType)
197 default:
198 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[floatsN])).Elem()
199 }
200 }
201 floatsN += slots
202 case reflect.Struct:
203 if i == 0 && inType.AssignableTo(reflect.TypeFor[CDecl]()) {
204 args[i] = reflect.Zero(inType)
205 continue
206 }
207 if inType.Size() == 0 {
208 args[i] = reflect.New(inType).Elem()
209 continue
210 }
211 args[i] = getCallbackStruct(inType, a.args, &floatsN, &intsN, &stackSlot, &stackByteOffset)
212 continue
213 default:
214 slots = int((inType.Size() + ptrSize - 1) / ptrSize)
215 if intsN+slots > numOfIntegerRegisters() {
216 if isDarwin && runtime.GOARCH == "arm64" {
217 // Darwin ARM64: read from packed stack with proper alignment
218 args[i] = callbackArgFromStack(a.args, stackSlot, &stackByteOffset, inType)
219 } else if stackFrame != nil {
220 // ppc64le/s390x: stack args are in separate stackFrame
221 if runtime.GOARCH == "s390x" {
222 // s390x big-endian: sub-8-byte values are right-justified
223 args[i] = callbackArgFromSlotBigEndian(unsafe.Pointer(&stackFrame[stackSlot]), inType)
224 } else {
225 args[i] = reflect.NewAt(inType, unsafe.Pointer(&stackFrame[stackSlot])).Elem()
226 }
227 stackSlot += slots
228 } else {
229 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[stackSlot])).Elem()
230 stackSlot += slots
231 }
232 } else {
233 if intFrame != nil {
234 args[i] = reflect.NewAt(inType, unsafe.Pointer(&intFrame[intsN])).Elem()
235 } else {
236 // the integers begin after the floats in frame
237 pos := intsN + numOfFloatRegisters()
238 if runtime.GOARCH == "s390x" {
239 // s390x big-endian: sub-8-byte values are right-justified in GPR slot
240 args[i] = callbackArgFromSlotBigEndian(unsafe.Pointer(&frame[pos]), inType)
241 } else {
242 args[i] = reflect.NewAt(inType, unsafe.Pointer(&frame[pos])).Elem()
243 }
244 }
245 }
246 intsN += slots
247 }
248 }
249 ret := fn.Call(args)
250 if len(ret) > 0 {
251 switch k := ret[0].Kind(); k {
252 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
253 a.result[0] = uintptr(ret[0].Uint())
254 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
255 a.result[0] = uintptr(ret[0].Int())
256 case reflect.Bool:
257 if ret[0].Bool() {
258 a.result[0] = 1
259 } else {
260 a.result[0] = 0
261 }
262 case reflect.Pointer:
263 a.result[0] = ret[0].Pointer()
264 case reflect.UnsafePointer:
265 a.result[0] = ret[0].Pointer()
266 case reflect.Struct:
267 setStruct(a, ret[0])
268 default:
269 panic("purego: unsupported kind: " + k.String())
270 }
271 }
272}
273
274// callbackArgFromStack reads an argument from the tightly-packed stack area on Darwin ARM64.
275// The C ABI on Darwin ARM64 packs small types on the stack without padding to 8 bytes.
276// This function handles proper alignment and advances stackByteOffset accordingly.
277func callbackArgFromStack(argsBase unsafe.Pointer, stackSlot int, stackByteOffset *uintptr, inType reflect.Type) reflect.Value {
278 // Calculate base address of stack area (after float and int registers)
279 stackBase := unsafe.Add(argsBase, stackSlot*int(ptrSize))
280
281 // Get type's natural alignment
282 align := uintptr(inType.Align())
283 size := inType.Size()
284
285 // Align the offset
286 if *stackByteOffset%align != 0 {
287 *stackByteOffset = (*stackByteOffset + align - 1) &^ (align - 1)
288 }
289
290 // Read value at aligned offset
291 ptr := unsafe.Add(stackBase, *stackByteOffset)
292 *stackByteOffset += size
293
294 return reflect.NewAt(inType, ptr).Elem()
295}
296
297// callbackFloatFromDoubleSlot reads a floating-point callback argument from an
298// 8-byte register or stack slot on ppc64le, where a single-precision value is
299// held in double-precision format.
300func callbackFloatFromDoubleSlot(slotPtr unsafe.Pointer, inType reflect.Type) reflect.Value {
301 if inType.Kind() != reflect.Float32 {
302 return reflect.NewAt(inType, slotPtr).Elem()
303 }
304 v := reflect.New(inType).Elem()
305 v.SetFloat(math.Float64frombits(*(*uint64)(slotPtr)))
306 return v
307}
308
309// callbackArgFromSlotBigEndian reads an argument from an 8-byte slot on big-endian architectures.
310// On s390x:
311// - Integer types are right-justified in GPRs: sub-8-byte values are at offset (8 - size)
312// - Float32 in FPRs is left-justified: stored in upper 32 bits, so at offset 0
313// - Float64 occupies the full 8-byte slot
314func callbackArgFromSlotBigEndian(slotPtr unsafe.Pointer, inType reflect.Type) reflect.Value {
315 size := inType.Size()
316 if size >= 8 {
317 // 8-byte values occupy the entire slot
318 return reflect.NewAt(inType, slotPtr).Elem()
319 }
320 // Float32 is left-justified in FPRs (upper 32 bits), so offset is 0
321 if inType.Kind() == reflect.Float32 {
322 return reflect.NewAt(inType, slotPtr).Elem()
323 }
324 // Integer types are right-justified: offset = 8 - size
325 offset := 8 - size
326 ptr := unsafe.Add(slotPtr, offset)
327 return reflect.NewAt(inType, ptr).Elem()
328}
329
330// callbackasmAddr returns address of runtime.callbackasm
331// function adjusted by i.
332// On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
333// and we want callback to arrive at
334// correspondent call instruction instead of start of
335// runtime.callbackasm.
336// On ARM, runtime.callbackasm is a series of mov and branch instructions.
337// R12 is loaded with the callback index. Each entry is two instructions,
338// hence 8 bytes.
339func callbackasmAddr(i int) uintptr {
340 var entrySize int
341 switch runtime.GOARCH {
342 default:
343 panic("purego: unsupported architecture")
344 case "amd64":
345 // On amd64, each callback entry is just a CALL instruction (5 bytes)
346 entrySize = 5
347 case "386":
348 // On 386, each callback entry is MOVL $imm, CX (5 bytes) + JMP (5 bytes)
349 entrySize = 10
350 case "arm", "arm64", "loong64", "ppc64le", "riscv64":
351 // On ARM, ARM64, Loong64, PPC64LE and RISCV64, each entry is a MOV instruction
352 // followed by a branch instruction
353 entrySize = 8
354 case "s390x":
355 // On S390X, each entry is LGHI (4 bytes) + JG (6 bytes)
356 entrySize = 10
357 }
358 return callbackasmABI0 + uintptr(i*entrySize)
359}