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 (aix || linux || openbsd) && (ppc64 || ppc64le)
6
7package runtime
8
9import (
10	"internal/abi"
11	"runtime/internal/sys"
12	"unsafe"
13)
14
15func dumpregs(c *sigctxt) {
16	print("r0   ", hex(c.r0()), "\t")
17	print("r1   ", hex(c.r1()), "\n")
18	print("r2   ", hex(c.r2()), "\t")
19	print("r3   ", hex(c.r3()), "\n")
20	print("r4   ", hex(c.r4()), "\t")
21	print("r5   ", hex(c.r5()), "\n")
22	print("r6   ", hex(c.r6()), "\t")
23	print("r7   ", hex(c.r7()), "\n")
24	print("r8   ", hex(c.r8()), "\t")
25	print("r9   ", hex(c.r9()), "\n")
26	print("r10  ", hex(c.r10()), "\t")
27	print("r11  ", hex(c.r11()), "\n")
28	print("r12  ", hex(c.r12()), "\t")
29	print("r13  ", hex(c.r13()), "\n")
30	print("r14  ", hex(c.r14()), "\t")
31	print("r15  ", hex(c.r15()), "\n")
32	print("r16  ", hex(c.r16()), "\t")
33	print("r17  ", hex(c.r17()), "\n")
34	print("r18  ", hex(c.r18()), "\t")
35	print("r19  ", hex(c.r19()), "\n")
36	print("r20  ", hex(c.r20()), "\t")
37	print("r21  ", hex(c.r21()), "\n")
38	print("r22  ", hex(c.r22()), "\t")
39	print("r23  ", hex(c.r23()), "\n")
40	print("r24  ", hex(c.r24()), "\t")
41	print("r25  ", hex(c.r25()), "\n")
42	print("r26  ", hex(c.r26()), "\t")
43	print("r27  ", hex(c.r27()), "\n")
44	print("r28  ", hex(c.r28()), "\t")
45	print("r29  ", hex(c.r29()), "\n")
46	print("r30  ", hex(c.r30()), "\t")
47	print("r31  ", hex(c.r31()), "\n")
48	print("pc   ", hex(c.pc()), "\t")
49	print("ctr  ", hex(c.ctr()), "\n")
50	print("link ", hex(c.link()), "\t")
51	print("xer  ", hex(c.xer()), "\n")
52	print("ccr  ", hex(c.ccr()), "\t")
53	print("trap ", hex(c.trap()), "\n")
54}
55
56//go:nosplit
57//go:nowritebarrierrec
58func (c *sigctxt) sigpc() uintptr    { return uintptr(c.pc()) }
59func (c *sigctxt) setsigpc(x uint64) { c.set_pc(x) }
60
61func (c *sigctxt) sigsp() uintptr { return uintptr(c.sp()) }
62func (c *sigctxt) siglr() uintptr { return uintptr(c.link()) }
63
64// preparePanic sets up the stack to look like a call to sigpanic.
65func (c *sigctxt) preparePanic(sig uint32, gp *g) {
66	// We arrange link, and pc to pretend the panicking
67	// function calls sigpanic directly.
68	// Always save LINK to stack so that panics in leaf
69	// functions are correctly handled. This smashes
70	// the stack frame but we're not going back there
71	// anyway.
72	sp := c.sp() - sys.MinFrameSize
73	c.set_sp(sp)
74	*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.link()
75
76	pc := gp.sigpc
77
78	if shouldPushSigpanic(gp, pc, uintptr(c.link())) {
79		// Make it look the like faulting PC called sigpanic.
80		c.set_link(uint64(pc))
81	}
82
83	// In case we are panicking from external C code
84	c.set_r0(0)
85	c.set_r30(uint64(uintptr(unsafe.Pointer(gp))))
86	c.set_r12(uint64(abi.FuncPCABIInternal(sigpanic)))
87	c.set_pc(uint64(abi.FuncPCABIInternal(sigpanic)))
88}
89
90func (c *sigctxt) pushCall(targetPC, resumePC uintptr) {
91	// Push the LR to stack, as we'll clobber it in order to
92	// push the call. The function being pushed is responsible
93	// for restoring the LR and setting the SP back.
94	// This extra space is known to gentraceback.
95	sp := c.sp() - sys.MinFrameSize
96	c.set_sp(sp)
97	*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.link()
98	// In PIC mode, we'll set up (i.e. clobber) R2 on function
99	// entry. Save it ahead of time.
100	// In PIC mode it requires R12 points to the function entry,
101	// so we'll set it up when pushing the call. Save it ahead
102	// of time as well.
103	// 8(SP) and 16(SP) are unused space in the reserved
104	// MinFrameSize (32) bytes.
105	*(*uint64)(unsafe.Pointer(uintptr(sp) + 8)) = c.r2()
106	*(*uint64)(unsafe.Pointer(uintptr(sp) + 16)) = c.r12()
107	// Set up PC and LR to pretend the function being signaled
108	// calls targetPC at resumePC.
109	c.set_link(uint64(resumePC))
110	c.set_r12(uint64(targetPC))
111	c.set_pc(uint64(targetPC))
112}
113