1// Copyright 2022 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5#include "textflag.h"
6#include "abi_loong64.h"
7
8// Set the x_crosscall2_ptr C function pointer variable point to crosscall2.
9// It's such a pointer chain: _crosscall2_ptr -> x_crosscall2_ptr -> crosscall2
10// Use a local trampoline, to avoid taking the address of a dynamically exported
11// function.
12TEXT ·set_crosscall2(SB),NOSPLIT,$0-0
13	MOVV	_crosscall2_ptr(SB), R5
14	MOVV	$crosscall2_trampoline<>(SB), R6
15	MOVV	R6, (R5)
16	RET
17
18TEXT crosscall2_trampoline<>(SB),NOSPLIT,$0-0
19	JMP	crosscall2(SB)
20
21// Called by C code generated by cmd/cgo.
22// func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr)
23// Saves C callee-saved registers and calls cgocallback with three arguments.
24// fn is the PC of a func(a unsafe.Pointer) function.
25TEXT crosscall2(SB),NOSPLIT|NOFRAME,$0
26	/*
27	 * We still need to save all callee save register as before, and then
28	 * push 3 args for fn (R4, R5, R7), skipping R6.
29	 * Also note that at procedure entry in gc world, 8(R29) will be the
30	 *  first arg.
31	 */
32
33	ADDV	$(-23*8), R3
34	MOVV	R4, (1*8)(R3) // fn unsafe.Pointer
35	MOVV	R5, (2*8)(R3) // a unsafe.Pointer
36	MOVV	R7, (3*8)(R3) // ctxt uintptr
37
38	SAVE_R22_TO_R31((4*8))
39	SAVE_F24_TO_F31((14*8))
40	MOVV	R1, (22*8)(R3)
41
42	// Initialize Go ABI environment
43	JAL	runtime·load_g(SB)
44
45	JAL	runtime·cgocallback(SB)
46
47	RESTORE_R22_TO_R31((4*8))
48	RESTORE_F24_TO_F31((14*8))
49	MOVV	(22*8)(R3), R1
50
51	ADDV	$(23*8), R3
52
53	RET
54