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