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