1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4//go:build darwin || freebsd || (linux && (amd64 || arm64))
5
6package purego
7
8import (
9 "reflect"
10 "runtime"
11 "sync"
12 "unsafe"
13)
14
15var syscall15XABI0 uintptr
16
17//go:nosplit
18func syscall_syscall15X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2, err uintptr) {
19 args := syscall15Args{
20 fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
21 a1, a2, a3, a4, a5, a6, a7, a8,
22 0,
23 }
24 runtime_cgocall(syscall15XABI0, unsafe.Pointer(&args))
25 return args.a1, args.a2, 0
26}
27
28// NewCallback converts a Go function to a function pointer conforming to the C calling convention.
29// This is useful when interoperating with C code requiring callbacks. The argument is expected to be a
30// function with zero or one uintptr-sized result. The function must not have arguments with size larger than the size
31// of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory allocated
32// for these callbacks is never released. At least 2000 callbacks can always be created. Although this function
33// provides similar functionality to windows.NewCallback it is distinct.
34func NewCallback(fn interface{}) uintptr {
35 return compileCallback(fn)
36}
37
38// maxCb is the maximum number of callbacks
39// only increase this if you have added more to the callbackasm function
40const maxCB = 2000
41
42var cbs struct {
43 lock sync.Mutex
44 numFn int // the number of functions currently in cbs.funcs
45 funcs [maxCB]reflect.Value // the saved callbacks
46}
47
48type callbackArgs struct {
49 index uintptr
50 // args points to the argument block.
51 //
52 // The structure of the arguments goes
53 // float registers followed by the
54 // integer registers followed by the stack.
55 //
56 // This variable is treated as a continuous
57 // block of memory containing all of the arguments
58 // for this callback.
59 args unsafe.Pointer
60 // Below are out-args from callbackWrap
61 result uintptr
62}
63
64func compileCallback(fn interface{}) uintptr {
65 val := reflect.ValueOf(fn)
66 if val.Kind() != reflect.Func {
67 panic("purego: the type must be a function but was not")
68 }
69 if val.IsNil() {
70 panic("purego: function must not be nil")
71 }
72 ty := val.Type()
73 for i := 0; i < ty.NumIn(); i++ {
74 in := ty.In(i)
75 switch in.Kind() {
76 case reflect.Struct, reflect.Interface, reflect.Func, reflect.Slice,
77 reflect.Chan, reflect.Complex64, reflect.Complex128,
78 reflect.String, reflect.Map, reflect.Invalid:
79 panic("purego: unsupported argument type: " + in.Kind().String())
80 }
81 }
82output:
83 switch {
84 case ty.NumOut() == 1:
85 switch ty.Out(0).Kind() {
86 case reflect.Pointer, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
87 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
88 reflect.Bool, reflect.UnsafePointer:
89 break output
90 }
91 panic("purego: unsupported return type: " + ty.String())
92 case ty.NumOut() > 1:
93 panic("purego: callbacks can only have one return")
94 }
95 cbs.lock.Lock()
96 defer cbs.lock.Unlock()
97 if cbs.numFn >= maxCB {
98 panic("purego: the maximum number of callbacks has been reached")
99 }
100 cbs.funcs[cbs.numFn] = val
101 cbs.numFn++
102 return callbackasmAddr(cbs.numFn - 1)
103}
104
105const ptrSize = unsafe.Sizeof((*int)(nil))
106
107const callbackMaxFrame = 64 * ptrSize
108
109// callbackasm is implemented in zcallback_GOOS_GOARCH.s
110//
111//go:linkname __callbackasm callbackasm
112var __callbackasm byte
113var callbackasmABI0 = uintptr(unsafe.Pointer(&__callbackasm))
114
115// callbackWrap_call allows the calling of the ABIInternal wrapper
116// which is required for runtime.cgocallback without the
117// <ABIInternal> tag which is only allowed in the runtime.
118// This closure is used inside sys_darwin_GOARCH.s
119var callbackWrap_call = callbackWrap
120
121// callbackWrap is called by assembly code which determines which Go function to call.
122// This function takes the arguments and passes them to the Go function and returns the result.
123func callbackWrap(a *callbackArgs) {
124 cbs.lock.Lock()
125 fn := cbs.funcs[a.index]
126 cbs.lock.Unlock()
127 fnType := fn.Type()
128 args := make([]reflect.Value, fnType.NumIn())
129 frame := (*[callbackMaxFrame]uintptr)(a.args)
130 var floatsN int // floatsN represents the number of float arguments processed
131 var intsN int // intsN represents the number of integer arguments processed
132 // stack points to the index into frame of the current stack element.
133 // The stack begins after the float and integer registers.
134 stack := numOfIntegerRegisters() + numOfFloats
135 for i := range args {
136 var pos int
137 switch fnType.In(i).Kind() {
138 case reflect.Float32, reflect.Float64:
139 if floatsN >= numOfFloats {
140 pos = stack
141 stack++
142 } else {
143 pos = floatsN
144 }
145 floatsN++
146 default:
147 if intsN >= numOfIntegerRegisters() {
148 pos = stack
149 stack++
150 } else {
151 // the integers begin after the floats in frame
152 pos = intsN + numOfFloats
153 }
154 intsN++
155 }
156 args[i] = reflect.NewAt(fnType.In(i), unsafe.Pointer(&frame[pos])).Elem()
157 }
158 ret := fn.Call(args)
159 if len(ret) > 0 {
160 switch k := ret[0].Kind(); k {
161 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
162 a.result = uintptr(ret[0].Uint())
163 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
164 a.result = uintptr(ret[0].Int())
165 case reflect.Bool:
166 if ret[0].Bool() {
167 a.result = 1
168 } else {
169 a.result = 0
170 }
171 case reflect.Pointer:
172 a.result = ret[0].Pointer()
173 case reflect.UnsafePointer:
174 a.result = ret[0].Pointer()
175 default:
176 panic("purego: unsupported kind: " + k.String())
177 }
178 }
179}
180
181// callbackasmAddr returns address of runtime.callbackasm
182// function adjusted by i.
183// On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
184// and we want callback to arrive at
185// correspondent call instruction instead of start of
186// runtime.callbackasm.
187// On ARM, runtime.callbackasm is a series of mov and branch instructions.
188// R12 is loaded with the callback index. Each entry is two instructions,
189// hence 8 bytes.
190func callbackasmAddr(i int) uintptr {
191 var entrySize int
192 switch runtime.GOARCH {
193 default:
194 panic("purego: unsupported architecture")
195 case "386", "amd64":
196 entrySize = 5
197 case "arm", "arm64":
198 // On ARM and ARM64, each entry is a MOV instruction
199 // followed by a branch instruction
200 entrySize = 8
201 }
202 return callbackasmABI0 + uintptr(i*entrySize)
203}