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 darwin || freebsd || linux || netbsd || openbsd
6
7package runtime
8
9import (
10	"internal/abi"
11	"internal/goarch"
12	"runtime/internal/sys"
13	"unsafe"
14)
15
16func dumpregs(c *sigctxt) {
17	print("r0      ", hex(c.r0()), "\n")
18	print("r1      ", hex(c.r1()), "\n")
19	print("r2      ", hex(c.r2()), "\n")
20	print("r3      ", hex(c.r3()), "\n")
21	print("r4      ", hex(c.r4()), "\n")
22	print("r5      ", hex(c.r5()), "\n")
23	print("r6      ", hex(c.r6()), "\n")
24	print("r7      ", hex(c.r7()), "\n")
25	print("r8      ", hex(c.r8()), "\n")
26	print("r9      ", hex(c.r9()), "\n")
27	print("r10     ", hex(c.r10()), "\n")
28	print("r11     ", hex(c.r11()), "\n")
29	print("r12     ", hex(c.r12()), "\n")
30	print("r13     ", hex(c.r13()), "\n")
31	print("r14     ", hex(c.r14()), "\n")
32	print("r15     ", hex(c.r15()), "\n")
33	print("r16     ", hex(c.r16()), "\n")
34	print("r17     ", hex(c.r17()), "\n")
35	print("r18     ", hex(c.r18()), "\n")
36	print("r19     ", hex(c.r19()), "\n")
37	print("r20     ", hex(c.r20()), "\n")
38	print("r21     ", hex(c.r21()), "\n")
39	print("r22     ", hex(c.r22()), "\n")
40	print("r23     ", hex(c.r23()), "\n")
41	print("r24     ", hex(c.r24()), "\n")
42	print("r25     ", hex(c.r25()), "\n")
43	print("r26     ", hex(c.r26()), "\n")
44	print("r27     ", hex(c.r27()), "\n")
45	print("r28     ", hex(c.r28()), "\n")
46	print("r29     ", hex(c.r29()), "\n")
47	print("lr      ", hex(c.lr()), "\n")
48	print("sp      ", hex(c.sp()), "\n")
49	print("pc      ", hex(c.pc()), "\n")
50	print("fault   ", hex(c.fault()), "\n")
51}
52
53//go:nosplit
54//go:nowritebarrierrec
55func (c *sigctxt) sigpc() uintptr { return uintptr(c.pc()) }
56
57func (c *sigctxt) setsigpc(x uint64) { c.set_pc(x) }
58func (c *sigctxt) sigsp() uintptr    { return uintptr(c.sp()) }
59func (c *sigctxt) siglr() uintptr    { return uintptr(c.lr()) }
60
61// preparePanic sets up the stack to look like a call to sigpanic.
62func (c *sigctxt) preparePanic(sig uint32, gp *g) {
63	// We arrange lr, and pc to pretend the panicking
64	// function calls sigpanic directly.
65	// Always save LR to stack so that panics in leaf
66	// functions are correctly handled. This smashes
67	// the stack frame but we're not going back there
68	// anyway.
69	sp := c.sp() - sys.StackAlign // needs only sizeof uint64, but must align the stack
70	c.set_sp(sp)
71	*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.lr()
72	// Make sure a valid frame pointer is saved on the stack so that the
73	// frame pointer checks in adjustframe are happy, if they're enabled.
74	// Frame pointer unwinding won't visit the sigpanic frame, since
75	// sigpanic will save the same frame pointer before calling into a panic
76	// function.
77	*(*uint64)(unsafe.Pointer(uintptr(sp - goarch.PtrSize))) = c.r29()
78
79	pc := gp.sigpc
80
81	if shouldPushSigpanic(gp, pc, uintptr(c.lr())) {
82		// Make it look the like faulting PC called sigpanic.
83		c.set_lr(uint64(pc))
84	}
85
86	// In case we are panicking from external C code
87	c.set_r28(uint64(uintptr(unsafe.Pointer(gp))))
88	c.set_pc(uint64(abi.FuncPCABIInternal(sigpanic)))
89}
90
91func (c *sigctxt) pushCall(targetPC, resumePC uintptr) {
92	// Push the LR to stack, as we'll clobber it in order to
93	// push the call. The function being pushed is responsible
94	// for restoring the LR and setting the SP back.
95	// This extra space is known to gentraceback.
96	sp := c.sp() - 16 // SP needs 16-byte alignment
97	c.set_sp(sp)
98	*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.lr()
99	// Make sure a valid frame pointer is saved on the stack so that the
100	// frame pointer checks in adjustframe are happy, if they're enabled.
101	// This is not actually used for unwinding.
102	*(*uint64)(unsafe.Pointer(uintptr(sp - goarch.PtrSize))) = c.r29()
103	// Set up PC and LR to pretend the function being signaled
104	// calls targetPC at resumePC.
105	c.set_lr(uint64(resumePC))
106	c.set_pc(uint64(targetPC))
107}
108