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 arguments below the frame
84 ADJSP $6*8, SP
85 MOVQ DI, 8(SP)
86 MOVQ SI, 16(SP)
87 MOVQ DX, 24(SP)
88 MOVQ CX, 32(SP)
89 MOVQ R8, 40(SP)
90 MOVQ R9, 48(SP)
91 LEAQ 8(SP), R8 // R8 = address of args vector
92
93 MOVQ R10, 0(SP) // push the stack pointer below registers
94
95 // determine index into runtime·cbs table
96 MOVQ $callbackasm(SB), DX
97 SUBQ DX, AX
98 MOVQ $0, DX
99 MOVQ $5, CX // divide by 5 because each call instruction in ·callbacks is 5 bytes long
100 DIVL CX
101 SUBQ $1, AX // subtract 1 because return PC is to the next slot
102
103 // Switch from the host ABI to the Go ABI.
104 PUSH_REGS_HOST_TO_ABI0()
105
106 // Create a struct callbackArgs on our stack to be passed as
107 // the "frame" to cgocallback and on to callbackWrap.
108 // $24 to make enough room for the arguments to runtime.cgocallback
109 SUBQ $(24+callbackArgs__size), SP
110 MOVQ AX, (24+callbackArgs_index)(SP) // callback index
111 MOVQ R8, (24+callbackArgs_args)(SP) // address of args vector
112 MOVQ $0, (24+callbackArgs_result)(SP) // result
113 LEAQ 24(SP), AX // take the address of callbackArgs
114
115 // Call cgocallback, which will call callbackWrap(frame).
116 MOVQ ·callbackWrap_call(SB), DI // Get the ABIInternal function pointer
117 MOVQ (DI), DI // without <ABIInternal> by using a closure.
118 MOVQ AX, SI // frame (address of callbackArgs)
119 MOVQ $0, CX // context
120
121 CALL crosscall2(SB) // runtime.cgocallback(fn, frame, ctxt uintptr)
122
123 // Get callback result.
124 MOVQ (24+callbackArgs_result)(SP), AX
125 ADDQ $(24+callbackArgs__size), SP // remove callbackArgs struct
126
127 POP_REGS_HOST_TO_ABI0()
128
129 MOVQ 0(SP), R10 // get the SP back
130
131 ADJSP $-6*8, SP // remove arguments
132
133 MOVQ R10, 0(SP)
134
135 RET