1// Copyright 2014 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//go:build ppc64 || ppc64le
6
7#include "textflag.h"
8#include "asm_ppc64x.h"
9#include "abi_ppc64x.h"
10
11#ifdef GO_PPC64X_HAS_FUNCDESC
12// crosscall2 is marked with go:cgo_export_static. On AIX, this creates and exports
13// the symbol name and descriptor as the AIX linker expects, but does not work if
14// referenced from within Go. Create and use an aliased descriptor of crosscall2
15// to workaround this.
16DEFINE_PPC64X_FUNCDESC(_crosscall2<>, crosscall2)
17#define CROSSCALL2_FPTR $_crosscall2<>(SB)
18#else
19// Use a local trampoline, to avoid taking the address of a dynamically exported
20// function.
21#define CROSSCALL2_FPTR $crosscall2_trampoline<>(SB)
22#endif
23
24// Set the x_crosscall2_ptr C function pointer variable point to crosscall2.
25// It's such a pointer chain: _crosscall2_ptr -> x_crosscall2_ptr -> crosscall2
26TEXT ·set_crosscall2(SB),NOSPLIT,$0-0
27	MOVD	_crosscall2_ptr(SB), R5
28	MOVD	CROSSCALL2_FPTR, R6
29	MOVD	R6, (R5)
30	RET
31
32TEXT crosscall2_trampoline<>(SB),NOSPLIT,$0-0
33	JMP	crosscall2(SB)
34
35// Called by C code generated by cmd/cgo.
36// func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr)
37// Saves C callee-saved registers and calls cgocallback with three arguments.
38// fn is the PC of a func(a unsafe.Pointer) function.
39// The value of R2 is saved on the new stack frame, and not
40// the caller's frame due to issue #43228.
41TEXT crosscall2(SB),NOSPLIT|NOFRAME,$0
42	// Start with standard C stack frame layout and linkage, allocate
43	// 32 bytes of argument space, save callee-save regs, and set R0 to $0.
44	STACK_AND_SAVE_HOST_TO_GO_ABI(32)
45	// The above will not preserve R2 (TOC). Save it in case Go is
46	// compiled without a TOC pointer (e.g -buildmode=default).
47	MOVD	R2, 24(R1)
48
49	// Load the current g.
50	BL	runtime·load_g(SB)
51
52#ifdef GO_PPC64X_HAS_FUNCDESC
53	// Load the real entry address from the first slot of the function descriptor.
54	// The first argument fn might be null, that means dropm in pthread key destructor.
55	CMP	R3, $0
56	BEQ	nil_fn
57	MOVD	8(R3), R2
58	MOVD	(R3), R3
59nil_fn:
60#endif
61	MOVD	R3, FIXED_FRAME+0(R1)	// fn unsafe.Pointer
62	MOVD	R4, FIXED_FRAME+8(R1)	// a unsafe.Pointer
63	// Skip R5 = n uint32
64	MOVD	R6, FIXED_FRAME+16(R1)	// ctxt uintptr
65	BL	runtime·cgocallback(SB)
66
67	// Restore the old frame, and R2.
68	MOVD	24(R1), R2
69	UNSTACK_AND_RESTORE_GO_TO_HOST_ABI(32)
70	RET
71