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