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 linux && (ppc64 || ppc64le)
6
7.file "gcc_linux_ppc64x.S"
8
9// Define a frame which has no argument space, but is compatible with
10// a call into a Go ABI. We allocate 32B to match FIXED_FRAME with
11// similar semantics, except we store the backchain pointer, not the
12// LR at offset 0. R2 is stored in the Go TOC save slot (offset 24).
13.set GPR_OFFSET, 32
14.set FPR_OFFSET, GPR_OFFSET + 18*8
15.set VR_OFFSET, FPR_OFFSET + 18*8
16.set FRAME_SIZE, VR_OFFSET + 12*16
17
18.macro FOR_EACH_GPR opcode r=14
19.ifge 31 - \r
20	\opcode \r, GPR_OFFSET + 8*(\r-14)(1)
21	FOR_EACH_GPR \opcode "(\r+1)"
22.endif
23.endm
24
25.macro FOR_EACH_FPR opcode fr=14
26.ifge 31 - \fr
27	\opcode \fr, FPR_OFFSET + 8*(\fr-14)(1)
28	FOR_EACH_FPR \opcode "(\fr+1)"
29.endif
30.endm
31
32.macro FOR_EACH_VR opcode vr=20
33.ifge 31 - \vr
34	li 0, VR_OFFSET + 16*(\vr-20)
35	\opcode \vr, 1, 0
36	FOR_EACH_VR \opcode "(\vr+1)"
37.endif
38.endm
39
40/*
41 * void crosscall_ppc64(void (*fn)(void), void *g)
42 *
43 * Calling into the gc tool chain, where all registers are caller save.
44 * Called from standard ppc64 C ABI, where r2, r14-r31, f14-f31 are
45 * callee-save, so they must be saved explicitly.
46 */
47.globl crosscall_ppc64
48crosscall_ppc64:
49	// Start with standard C stack frame layout and linkage
50	mflr	%r0
51	std	%r0, 16(%r1)	// Save LR in caller's frame
52	mfcr	%r0
53	std	%r0, 8(%r1)	// Save CR in caller's frame
54	stdu	%r1, -FRAME_SIZE(%r1)
55	std	%r2, 24(%r1)
56
57	FOR_EACH_GPR std
58	FOR_EACH_FPR stfd
59	FOR_EACH_VR stvx
60
61	// Set up Go ABI constant registers
62	li	%r0, 0
63
64	// Restore g pointer (r30 in Go ABI, which may have been clobbered by C)
65	mr	%r30, %r4
66
67	// Call fn
68	mr	%r12, %r3
69	mtctr	%r3
70	bctrl
71
72	FOR_EACH_GPR ld
73	FOR_EACH_FPR lfd
74	FOR_EACH_VR lvx
75
76	ld	%r2, 24(%r1)
77	addi	%r1, %r1, FRAME_SIZE
78	ld	%r0, 16(%r1)
79	mtlr	%r0
80	ld	%r0, 8(%r1)
81	mtcr	%r0
82	blr
83
84#ifdef __ELF__
85.section .note.GNU-stack,"",%progbits
86#endif
87