1// Copyright 2009 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 dragonfly || freebsd || linux || netbsd || openbsd
6
7package runtime
8
9import (
10	"internal/abi"
11	"unsafe"
12)
13
14func dumpregs(c *sigctxt) {
15	print("trap    ", hex(c.trap()), "\n")
16	print("error   ", hex(c.error()), "\n")
17	print("oldmask ", hex(c.oldmask()), "\n")
18	print("r0      ", hex(c.r0()), "\n")
19	print("r1      ", hex(c.r1()), "\n")
20	print("r2      ", hex(c.r2()), "\n")
21	print("r3      ", hex(c.r3()), "\n")
22	print("r4      ", hex(c.r4()), "\n")
23	print("r5      ", hex(c.r5()), "\n")
24	print("r6      ", hex(c.r6()), "\n")
25	print("r7      ", hex(c.r7()), "\n")
26	print("r8      ", hex(c.r8()), "\n")
27	print("r9      ", hex(c.r9()), "\n")
28	print("r10     ", hex(c.r10()), "\n")
29	print("fp      ", hex(c.fp()), "\n")
30	print("ip      ", hex(c.ip()), "\n")
31	print("sp      ", hex(c.sp()), "\n")
32	print("lr      ", hex(c.lr()), "\n")
33	print("pc      ", hex(c.pc()), "\n")
34	print("cpsr    ", hex(c.cpsr()), "\n")
35	print("fault   ", hex(c.fault()), "\n")
36}
37
38//go:nosplit
39//go:nowritebarrierrec
40func (c *sigctxt) sigpc() uintptr { return uintptr(c.pc()) }
41
42func (c *sigctxt) sigsp() uintptr { return uintptr(c.sp()) }
43func (c *sigctxt) siglr() uintptr { return uintptr(c.lr()) }
44
45// preparePanic sets up the stack to look like a call to sigpanic.
46func (c *sigctxt) preparePanic(sig uint32, gp *g) {
47	// We arrange lr, and pc to pretend the panicking
48	// function calls sigpanic directly.
49	// Always save LR to stack so that panics in leaf
50	// functions are correctly handled. This smashes
51	// the stack frame but we're not going back there
52	// anyway.
53	sp := c.sp() - 4
54	c.set_sp(sp)
55	*(*uint32)(unsafe.Pointer(uintptr(sp))) = c.lr()
56
57	pc := gp.sigpc
58
59	if shouldPushSigpanic(gp, pc, uintptr(c.lr())) {
60		// Make it look the like faulting PC called sigpanic.
61		c.set_lr(uint32(pc))
62	}
63
64	// In case we are panicking from external C code
65	c.set_r10(uint32(uintptr(unsafe.Pointer(gp))))
66	c.set_pc(uint32(abi.FuncPCABIInternal(sigpanic)))
67}
68
69func (c *sigctxt) pushCall(targetPC, resumePC uintptr) {
70	// Push the LR to stack, as we'll clobber it in order to
71	// push the call. The function being pushed is responsible
72	// for restoring the LR and setting the SP back.
73	// This extra slot is known to gentraceback.
74	sp := c.sp() - 4
75	c.set_sp(sp)
76	*(*uint32)(unsafe.Pointer(uintptr(sp))) = c.lr()
77	// Set up PC and LR to pretend the function being signaled
78	// calls targetPC at resumePC.
79	c.set_lr(uint32(resumePC))
80	c.set_pc(uint32(targetPC))
81}
82