1// Copyright 2016 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 || freebsd || openbsd) && riscv64
6
7package runtime
8
9import (
10	"internal/abi"
11	"internal/goarch"
12	"unsafe"
13)
14
15func dumpregs(c *sigctxt) {
16	print("ra  ", hex(c.ra()), "\t")
17	print("sp  ", hex(c.sp()), "\n")
18	print("gp  ", hex(c.gp()), "\t")
19	print("tp  ", hex(c.tp()), "\n")
20	print("t0  ", hex(c.t0()), "\t")
21	print("t1  ", hex(c.t1()), "\n")
22	print("t2  ", hex(c.t2()), "\t")
23	print("s0  ", hex(c.s0()), "\n")
24	print("s1  ", hex(c.s1()), "\t")
25	print("a0  ", hex(c.a0()), "\n")
26	print("a1  ", hex(c.a1()), "\t")
27	print("a2  ", hex(c.a2()), "\n")
28	print("a3  ", hex(c.a3()), "\t")
29	print("a4  ", hex(c.a4()), "\n")
30	print("a5  ", hex(c.a5()), "\t")
31	print("a6  ", hex(c.a6()), "\n")
32	print("a7  ", hex(c.a7()), "\t")
33	print("s2  ", hex(c.s2()), "\n")
34	print("s3  ", hex(c.s3()), "\t")
35	print("s4  ", hex(c.s4()), "\n")
36	print("s5  ", hex(c.s5()), "\t")
37	print("s6  ", hex(c.s6()), "\n")
38	print("s7  ", hex(c.s7()), "\t")
39	print("s8  ", hex(c.s8()), "\n")
40	print("s9  ", hex(c.s9()), "\t")
41	print("s10 ", hex(c.s10()), "\n")
42	print("s11 ", hex(c.s11()), "\t")
43	print("t3  ", hex(c.t3()), "\n")
44	print("t4  ", hex(c.t4()), "\t")
45	print("t5  ", hex(c.t5()), "\n")
46	print("t6  ", hex(c.t6()), "\t")
47	print("pc  ", hex(c.pc()), "\n")
48}
49
50//go:nosplit
51//go:nowritebarrierrec
52func (c *sigctxt) sigpc() uintptr { return uintptr(c.pc()) }
53
54func (c *sigctxt) sigsp() uintptr { return uintptr(c.sp()) }
55func (c *sigctxt) siglr() uintptr { return uintptr(c.ra()) }
56func (c *sigctxt) fault() uintptr { return uintptr(c.sigaddr()) }
57
58// preparePanic sets up the stack to look like a call to sigpanic.
59func (c *sigctxt) preparePanic(sig uint32, gp *g) {
60	// We arrange RA, and pc to pretend the panicking
61	// function calls sigpanic directly.
62	// Always save RA to stack so that panics in leaf
63	// functions are correctly handled. This smashes
64	// the stack frame but we're not going back there
65	// anyway.
66	sp := c.sp() - goarch.PtrSize
67	c.set_sp(sp)
68	*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.ra()
69
70	pc := gp.sigpc
71
72	if shouldPushSigpanic(gp, pc, uintptr(c.ra())) {
73		// Make it look the like faulting PC called sigpanic.
74		c.set_ra(uint64(pc))
75	}
76
77	// In case we are panicking from external C code
78	c.set_gp(uint64(uintptr(unsafe.Pointer(gp))))
79	c.set_pc(uint64(abi.FuncPCABIInternal(sigpanic)))
80}
81
82func (c *sigctxt) pushCall(targetPC, resumePC uintptr) {
83	// Push the LR to stack, as we'll clobber it in order to
84	// push the call. The function being pushed is responsible
85	// for restoring the LR and setting the SP back.
86	// This extra slot is known to gentraceback.
87	sp := c.sp() - goarch.PtrSize
88	c.set_sp(sp)
89	*(*uint64)(unsafe.Pointer(uintptr(sp))) = c.ra()
90	// Set up PC and LR to pretend the function being signaled
91	// calls targetPC at resumePC.
92	c.set_ra(uint64(resumePC))
93	c.set_pc(uint64(targetPC))
94}
95