1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4//go:build darwin || (!cgo && linux)
5
6#include "textflag.h"
7#include "internal/abi/abi_amd64.h"
8#include "go_asm.h"
9#include "funcdata.h"
10
11// syscall9X calls a function in libc on behalf of the syscall package.
12// syscall9X takes a pointer to a struct like:
13// struct {
14// fn uintptr
15// a1 uintptr
16// a2 uintptr
17// a3 uintptr
18// a4 uintptr
19// a5 uintptr
20// a6 uintptr
21// a7 uintptr
22// a8 uintptr
23// a9 uintptr
24// r1 uintptr
25// r2 uintptr
26// err uintptr
27// }
28// syscall9X must be called on the g0 stack with the
29// C calling convention (use libcCall).
30GLOBL ·syscall9XABI0(SB), NOPTR|RODATA, $8
31DATA ·syscall9XABI0(SB)/8, $syscall9X(SB)
32TEXT syscall9X(SB), NOSPLIT, $0
33 PUSHQ BP
34 MOVQ SP, BP
35 SUBQ $32, SP
36 MOVQ DI, 24(BP) // save the pointer
37
38 MOVQ syscall9Args_f1(DI), X0 // f1
39 MOVQ syscall9Args_f2(DI), X1 // f2
40 MOVQ syscall9Args_f3(DI), X2 // f3
41 MOVQ syscall9Args_f4(DI), X3 // f4
42 MOVQ syscall9Args_f5(DI), X4 // f5
43 MOVQ syscall9Args_f6(DI), X5 // f6
44 MOVQ syscall9Args_f7(DI), X6 // f7
45 MOVQ syscall9Args_f8(DI), X7 // f8
46
47 MOVQ syscall9Args_fn(DI), R10 // fn
48 MOVQ syscall9Args_a2(DI), SI // a2
49 MOVQ syscall9Args_a3(DI), DX // a3
50 MOVQ syscall9Args_a4(DI), CX // a4
51 MOVQ syscall9Args_a5(DI), R8 // a5
52 MOVQ syscall9Args_a6(DI), R9 // a6
53 MOVQ syscall9Args_a7(DI), R11 // a7
54 MOVQ syscall9Args_a8(DI), R12 // a8
55 MOVQ syscall9Args_a9(DI), R13 // a9
56 MOVQ syscall9Args_a1(DI), DI // a1
57
58 // push the remaining paramters onto the stack
59 MOVQ R11, 0(SP) // push a7
60 MOVQ R12, 8(SP) // push a8
61 MOVQ R13, 16(SP) // push a9
62 XORL AX, AX // vararg: say "no float args"
63
64 CALL R10
65
66 MOVQ 24(BP), DI // get the pointer back
67 MOVQ AX, syscall9Args_r1(DI) // r1
68 MOVQ X0, syscall9Args_r2(DI) // r2
69
70 XORL AX, AX // no error (it's ignored anyway)
71 ADDQ $32, SP
72 MOVQ BP, SP
73 POPQ BP
74 RET
75
76TEXT callbackasm1(SB), NOSPLIT, $0
77 // remove return address from stack, we are not returning to callbackasm, but to its caller.
78 MOVQ 0(SP), AX
79 ADDQ $8, SP
80
81 MOVQ 0(SP), R10 // get the return SP so that we can align register args with stack args
82
83 // make space for first six int and 8 float arguments below the frame
84 ADJSP $14*8, SP
85 MOVSD X0, (1*8)(SP)
86 MOVSD X1, (2*8)(SP)
87 MOVSD X2, (3*8)(SP)
88 MOVSD X3, (4*8)(SP)
89 MOVSD X4, (5*8)(SP)
90 MOVSD X5, (6*8)(SP)
91 MOVSD X6, (7*8)(SP)
92 MOVSD X7, (8*8)(SP)
93 MOVQ DI, (9*8)(SP)
94 MOVQ SI, (10*8)(SP)
95 MOVQ DX, (11*8)(SP)
96 MOVQ CX, (12*8)(SP)
97 MOVQ R8, (13*8)(SP)
98 MOVQ R9, (14*8)(SP)
99 LEAQ 8(SP), R8 // R8 = address of args vector
100
101 MOVQ R10, 0(SP) // push the stack pointer below registers
102
103 // determine index into runtime·cbs table
104 MOVQ $callbackasm(SB), DX
105 SUBQ DX, AX
106 MOVQ $0, DX
107 MOVQ $5, CX // divide by 5 because each call instruction in ·callbacks is 5 bytes long
108 DIVL CX
109 SUBQ $1, AX // subtract 1 because return PC is to the next slot
110
111 // Switch from the host ABI to the Go ABI.
112 PUSH_REGS_HOST_TO_ABI0()
113
114 // Create a struct callbackArgs on our stack to be passed as
115 // the "frame" to cgocallback and on to callbackWrap.
116 // $24 to make enough room for the arguments to runtime.cgocallback
117 SUBQ $(24+callbackArgs__size), SP
118 MOVQ AX, (24+callbackArgs_index)(SP) // callback index
119 MOVQ R8, (24+callbackArgs_args)(SP) // address of args vector
120 MOVQ $0, (24+callbackArgs_result)(SP) // result
121 LEAQ 24(SP), AX // take the address of callbackArgs
122
123 // Call cgocallback, which will call callbackWrap(frame).
124 MOVQ ·callbackWrap_call(SB), DI // Get the ABIInternal function pointer
125 MOVQ (DI), DI // without <ABIInternal> by using a closure.
126 MOVQ AX, SI // frame (address of callbackArgs)
127 MOVQ $0, CX // context
128
129 CALL crosscall2(SB) // runtime.cgocallback(fn, frame, ctxt uintptr)
130
131 // Get callback result.
132 MOVQ (24+callbackArgs_result)(SP), AX
133 ADDQ $(24+callbackArgs__size), SP // remove callbackArgs struct
134
135 POP_REGS_HOST_TO_ABI0()
136
137 MOVQ 0(SP), R10 // get the SP back
138
139 ADJSP $-14*8, SP // remove arguments
140
141 MOVQ R10, 0(SP)
142
143 RET