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 "math"
10 "reflect"
11 "runtime"
12 "sync"
13 "unsafe"
14)
15
16var syscall9XABI0 uintptr
17
18type syscall9Args struct {
19 fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr
20 f1, f2, f3, f4, f5, f6, f7, f8 float64
21 r1, r2, err uintptr
22}
23
24//go:nosplit
25func syscall_syscall9X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2, err uintptr) {
26 args := syscall9Args{fn, a1, a2, a3, a4, a5, a6, a7, a8, a9,
27 math.Float64frombits(uint64(a1)), math.Float64frombits(uint64(a2)), math.Float64frombits(uint64(a3)),
28 math.Float64frombits(uint64(a4)), math.Float64frombits(uint64(a5)), math.Float64frombits(uint64(a6)),
29 math.Float64frombits(uint64(a7)), math.Float64frombits(uint64(a8)),
30 r1, r2, err}
31 runtime_cgocall(syscall9XABI0, 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.
41//
42// NOTE: Linux is currently not supported and will panic if called.
43func NewCallback(fn interface{}) uintptr {
44 if runtime.GOOS == "linux" {
45 panic("purego: NewCallback not supported")
46 }
47 return compileCallback(fn)
48}
49
50// maxCb is the maximum number of callbacks
51// only increase this if you have added more to the callbackasm function
52const maxCB = 2000
53
54var cbs struct {
55 lock sync.Mutex
56 numFn int // the number of functions currently in cbs.funcs
57 funcs [maxCB]reflect.Value // the saved callbacks
58}
59
60type callbackArgs struct {
61 index uintptr
62 // args points to the argument block.
63 //
64 // For cdecl and stdcall, all arguments are on the stack.
65 //
66 // For fastcall, the trampoline spills register arguments to
67 // the reserved spill slots below the stack arguments,
68 // resulting in a layout equivalent to stdcall.
69 //
70 // For arm, the trampoline stores the register arguments just
71 // below the stack arguments, so again we can treat it as one
72 // big stack arguments frame.
73 args unsafe.Pointer
74 // Below are out-args from callbackWrap
75 result uintptr
76}
77
78func compileCallback(fn interface{}) uintptr {
79 val := reflect.ValueOf(fn)
80 if val.Kind() != reflect.Func {
81 panic("purego: type is not a function")
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.Float32, reflect.Float64,
88 reflect.Interface, reflect.Func, reflect.Slice,
89 reflect.Chan, reflect.Complex64, reflect.Complex128,
90 reflect.String, reflect.Map, reflect.Invalid:
91 panic("purego: unsupported argument type: " + in.Kind().String())
92 }
93 }
94output:
95 switch {
96 case ty.NumOut() == 1:
97 switch ty.Out(0).Kind() {
98 case reflect.Pointer, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
99 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
100 reflect.Bool, reflect.UnsafePointer:
101 break output
102 }
103 panic("purego: unsupported return type: " + ty.String())
104 case ty.NumOut() > 1:
105 panic("purego: callbacks can only have one return")
106 }
107 cbs.lock.Lock()
108 defer cbs.lock.Unlock()
109 if cbs.numFn >= maxCB {
110 panic("purego: the maximum number of callbacks has been reached")
111 }
112 cbs.funcs[cbs.numFn] = val
113 cbs.numFn++
114 return callbackasmAddr(cbs.numFn - 1)
115}
116
117const ptrSize = unsafe.Sizeof((*int)(nil))
118
119const callbackMaxFrame = 64 * ptrSize
120
121// callbackasmABI0 is implemented in zcallback_GOOS_GOARCH.s
122var callbackasmABI0 uintptr
123
124// callbackWrap_call allows the calling of the ABIInternal wrapper
125// which is required for runtime.cgocallback without the
126// <ABIInternal> tag which is only allowed in the runtime.
127// This closure is used inside sys_darwin_GOARCH.s
128var callbackWrap_call = callbackWrap
129
130// callbackWrap is called by assembly code which determines which Go function to call.
131// This function takes the arguments and passes them to the Go function and returns the result.
132func callbackWrap(a *callbackArgs) {
133 cbs.lock.Lock()
134 fn := cbs.funcs[a.index]
135 cbs.lock.Unlock()
136 fnType := fn.Type()
137 args := make([]reflect.Value, fnType.NumIn())
138 frame := (*[callbackMaxFrame]uintptr)(a.args)
139 for i := range args {
140 //TODO: support float32 and float64
141 args[i] = reflect.NewAt(fnType.In(i), unsafe.Pointer(&frame[i])).Elem()
142 }
143 ret := fn.Call(args)
144 if len(ret) > 0 {
145 switch k := ret[0].Kind(); k {
146 case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uintptr:
147 a.result = uintptr(ret[0].Uint())
148 case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
149 a.result = uintptr(ret[0].Int())
150 case reflect.Bool:
151 if ret[0].Bool() {
152 a.result = 1
153 } else {
154 a.result = 0
155 }
156 case reflect.Pointer:
157 a.result = ret[0].Pointer()
158 case reflect.UnsafePointer:
159 a.result = ret[0].Pointer()
160 default:
161 panic("purego: unsupported kind: " + k.String())
162 }
163 }
164}
165
166// callbackasmAddr returns address of runtime.callbackasm
167// function adjusted by i.
168// On x86 and amd64, runtime.callbackasm is a series of CALL instructions,
169// and we want callback to arrive at
170// correspondent call instruction instead of start of
171// runtime.callbackasm.
172// On ARM, runtime.callbackasm is a series of mov and branch instructions.
173// R12 is loaded with the callback index. Each entry is two instructions,
174// hence 8 bytes.
175func callbackasmAddr(i int) uintptr {
176 var entrySize int
177 switch runtime.GOARCH {
178 default:
179 panic("purego: unsupported architecture")
180 case "386", "amd64":
181 entrySize = 5
182 case "arm", "arm64":
183 // On ARM and ARM64, each entry is a MOV instruction
184 // followed by a branch instruction
185 entrySize = 8
186 }
187 return callbackasmABI0 + uintptr(i*entrySize)
188}