1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4//go:build darwin || freebsd || linux || netbsd
5
6#include "textflag.h"
7#include "abi_amd64.h"
8#include "go_asm.h"
9#include "funcdata.h"
10
11#define STACK_SIZE 224
12#define PTR_ADDRESS (STACK_SIZE - 8)
13
14// syscallX calls a function in libc on behalf of the syscall package.
15// syscallX takes a pointer to a struct like:
16// struct {
17// fn uintptr
18// a1 uintptr
19// a2 uintptr
20// a3 uintptr
21// a4 uintptr
22// a5 uintptr
23// a6 uintptr
24// a7 uintptr
25// a8 uintptr
26// a9 uintptr
27// a10 uintptr
28// a11 uintptr
29// a12 uintptr
30// a13 uintptr
31// a14 uintptr
32// a15 uintptr
33// r1 uintptr
34// r2 uintptr
35// err uintptr
36// }
37// syscallX must be called on the g0 stack with the
38// C calling convention (use libcCall).
39GLOBL ·syscallXABI0(SB), NOPTR|RODATA, $8
40DATA ·syscallXABI0(SB)/8, $syscallX(SB)
41TEXT syscallX(SB), NOSPLIT, $STACK_SIZE
42 MOVQ DI, PTR_ADDRESS(SP) // save the pointer
43 MOVQ DI, R11
44
45 MOVQ syscallArgs_f1(R11), X0 // f1
46 MOVQ syscallArgs_f2(R11), X1 // f2
47 MOVQ syscallArgs_f3(R11), X2 // f3
48 MOVQ syscallArgs_f4(R11), X3 // f4
49 MOVQ syscallArgs_f5(R11), X4 // f5
50 MOVQ syscallArgs_f6(R11), X5 // f6
51 MOVQ syscallArgs_f7(R11), X6 // f7
52 MOVQ syscallArgs_f8(R11), X7 // f8
53
54 MOVQ syscallArgs_a1(R11), DI // a1
55 MOVQ syscallArgs_a2(R11), SI // a2
56 MOVQ syscallArgs_a3(R11), DX // a3
57 MOVQ syscallArgs_a4(R11), CX // a4
58 MOVQ syscallArgs_a5(R11), R8 // a5
59 MOVQ syscallArgs_a6(R11), R9 // a6
60
61 // push the remaining parameters onto the stack
62 MOVQ syscallArgs_a7(R11), R12
63 MOVQ R12, 0(SP) // push a7
64 MOVQ syscallArgs_a8(R11), R12
65 MOVQ R12, 8(SP) // push a8
66 MOVQ syscallArgs_a9(R11), R12
67 MOVQ R12, 16(SP) // push a9
68 MOVQ syscallArgs_a10(R11), R12
69 MOVQ R12, 24(SP) // push a10
70 MOVQ syscallArgs_a11(R11), R12
71 MOVQ R12, 32(SP) // push a11
72 MOVQ syscallArgs_a12(R11), R12
73 MOVQ R12, 40(SP) // push a12
74 MOVQ syscallArgs_a13(R11), R12
75 MOVQ R12, 48(SP) // push a13
76 MOVQ syscallArgs_a14(R11), R12
77 MOVQ R12, 56(SP) // push a14
78 MOVQ syscallArgs_a15(R11), R12
79 MOVQ R12, 64(SP) // push a15
80 MOVQ syscallArgs_a16(R11), R12
81 MOVQ R12, 72(SP) // push a16
82 MOVQ syscallArgs_a17(R11), R12
83 MOVQ R12, 80(SP) // push a17
84 MOVQ syscallArgs_a18(R11), R12
85 MOVQ R12, 88(SP) // push a18
86 MOVQ syscallArgs_a19(R11), R12
87 MOVQ R12, 96(SP) // push a19
88 MOVQ syscallArgs_a20(R11), R12
89 MOVQ R12, 104(SP) // push a20
90 MOVQ syscallArgs_a21(R11), R12
91 MOVQ R12, 112(SP) // push a21
92 MOVQ syscallArgs_a22(R11), R12
93 MOVQ R12, 120(SP) // push a22
94 MOVQ syscallArgs_a23(R11), R12
95 MOVQ R12, 128(SP) // push a23
96 MOVQ syscallArgs_a24(R11), R12
97 MOVQ R12, 136(SP) // push a24
98 MOVQ syscallArgs_a25(R11), R12
99 MOVQ R12, 144(SP) // push a25
100 MOVQ syscallArgs_a26(R11), R12
101 MOVQ R12, 152(SP) // push a26
102 MOVQ syscallArgs_a27(R11), R12
103 MOVQ R12, 160(SP) // push a27
104 MOVQ syscallArgs_a28(R11), R12
105 MOVQ R12, 168(SP) // push a28
106 MOVQ syscallArgs_a29(R11), R12
107 MOVQ R12, 176(SP) // push a29
108 MOVQ syscallArgs_a30(R11), R12
109 MOVQ R12, 184(SP) // push a30
110 MOVQ syscallArgs_a31(R11), R12
111 MOVQ R12, 192(SP) // push a31
112 MOVQ syscallArgs_a32(R11), R12
113 MOVQ R12, 200(SP) // push a32
114 XORL AX, AX // vararg: say "no float args"
115
116 MOVQ syscallArgs_fn(R11), R10 // fn
117 CALL R10
118
119 MOVQ PTR_ADDRESS(SP), DI // get the pointer back
120 MOVQ AX, syscallArgs_a1(DI) // r1
121 MOVQ DX, syscallArgs_a2(DI) // r2
122 MOVQ X0, syscallArgs_f1(DI) // f1
123 MOVQ X1, syscallArgs_f2(DI) // f2
124
125#ifdef GOOS_darwin
126 CALL purego_error(SB)
127 MOVQ PTR_ADDRESS(SP), DI // reload (DI clobbered by call)
128 MOVQ (AX), AX
129 MOVQ AX, syscallArgs_a3(DI) // save errno
130#else
131 MOVQ $0, syscallArgs_a3(DI) // no errno here: clear the input argument
132#endif
133
134 XORL AX, AX // no error (it's ignored anyway)
135 RET
136
137TEXT callbackasm1(SB), NOSPLIT|NOFRAME, $0
138 MOVQ 0(SP), AX // save the return address to calculate the cb index
139 MOVQ 8(SP), R10 // get the return SP so that we can align register args with stack args
140 ADDQ $8, SP // remove return address from stack, we are not returning to callbackasm, but to its caller.
141
142 // make space for first six int and 8 float arguments below the frame
143 ADJSP $14*8, SP
144 MOVSD X0, (1*8)(SP)
145 MOVSD X1, (2*8)(SP)
146 MOVSD X2, (3*8)(SP)
147 MOVSD X3, (4*8)(SP)
148 MOVSD X4, (5*8)(SP)
149 MOVSD X5, (6*8)(SP)
150 MOVSD X6, (7*8)(SP)
151 MOVSD X7, (8*8)(SP)
152 MOVQ DI, (9*8)(SP)
153 MOVQ SI, (10*8)(SP)
154 MOVQ DX, (11*8)(SP)
155 MOVQ CX, (12*8)(SP)
156 MOVQ R8, (13*8)(SP)
157 MOVQ R9, (14*8)(SP)
158 LEAQ 8(SP), R8 // R8 = address of args vector
159
160 PUSHQ R10 // push the stack pointer below registers
161
162 // Switch from the host ABI to the Go ABI.
163 PUSH_REGS_HOST_TO_ABI0()
164
165 // determine index into runtime·cbs table
166 MOVQ $callbackasm(SB), DX
167 SUBQ DX, AX
168 MOVQ $0, DX
169 MOVQ $5, CX // divide by 5 because each call instruction in ·callbacks is 5 bytes long
170 DIVL CX
171 SUBQ $1, AX // subtract 1 because return PC is to the next slot
172
173 // Create a struct callbackArgs on our stack to be passed as
174 // the "frame" to cgocallback and on to callbackWrap.
175 // $24 to make enough room for the arguments to runtime.cgocallback
176 SUBQ $(24+callbackArgs__size), SP
177 MOVQ AX, (24+callbackArgs_index)(SP) // callback index
178 MOVQ R8, (24+callbackArgs_args)(SP) // address of args vector
179 MOVQ DI, (24+callbackArgs_result)(SP) // result
180 LEAQ 24(SP), AX // take the address of callbackArgs
181
182 // Call cgocallback, which will call callbackWrap(frame).
183 MOVQ ·callbackWrap_call(SB), DI // Get the ABIInternal function pointer
184 MOVQ (DI), DI // without <ABIInternal> by using a closure.
185 MOVQ AX, SI // frame (address of callbackArgs)
186 MOVQ $0, CX // context
187
188 CALL crosscall2(SB) // runtime.cgocallback(fn, frame, ctxt uintptr)
189
190 // Get callback result.
191 MOVQ (24+callbackArgs_result)(SP), AX
192 MOVQ (24+callbackArgs_result+8)(SP), DX
193 // Restore XMM0/XMM1 from result[2]/result[3] for struct eightbytes
194 // classified as SSE by the SysV ABI.
195 MOVQ (24+callbackArgs_result+16)(SP), X0
196 MOVQ (24+callbackArgs_result+24)(SP), X1
197 ADDQ $(24+callbackArgs__size), SP // remove callbackArgs struct
198
199 POP_REGS_HOST_TO_ABI0()
200
201 POPQ R10 // get the SP back
202 ADJSP $-14*8, SP // remove arguments
203
204 MOVQ R10, 0(SP)
205
206 RET