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