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