Skip to content

Commit 91e783e

Browse files
4a6f656cpull[bot]
authored andcommitted
runtime: add crash stack support for riscv64
Change-Id: Ib89a71e20f9c6b86c97814c75cb427e9bd7075e5 Reviewed-on: https://go-review.googlesource.com/c/go/+/538735 Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Run-TryBot: Joel Sing <[email protected]>
1 parent 0ff0626 commit 91e783e

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

src/runtime/asm.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ TEXT ·mapinitnoop<ABIInternal>(SB),NOSPLIT,$0-0
1515

1616
#ifndef GOARCH_amd64
1717
#ifndef GOARCH_arm64
18+
#ifndef GOARCH_riscv64
1819
// stub to appease shared build mode.
1920
TEXT ·switchToCrashStack0<ABIInternal>(SB),NOSPLIT,$0-0
2021
UNDEF
2122
#endif
2223
#endif
24+
#endif

src/runtime/asm_riscv64.s

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ TEXT runtime·getcallerpc(SB),NOSPLIT|NOFRAME,$0-8
153153
MOV T0, ret+0(FP)
154154
RET
155155

156+
// func switchToCrashStack0(fn func())
157+
TEXT runtime·switchToCrashStack0<ABIInternal>(SB), NOSPLIT, $0-8
158+
MOV X10, CTXT // context register
159+
MOV g_m(g), X11 // curm
160+
161+
// set g to gcrash
162+
MOV $runtime·gcrash(SB), g // g = &gcrash
163+
CALL runtime·save_g(SB) // clobbers X31
164+
MOV X11, g_m(g) // g.m = curm
165+
MOV g, m_g0(X11) // curm.g0 = g
166+
167+
// switch to crashstack
168+
MOV (g_stack+stack_hi)(g), X11
169+
ADD $(-4*8), X11
170+
MOV X11, X2
171+
172+
// call target function
173+
MOV 0(CTXT), X10
174+
JALR X1, X10
175+
176+
// should never return
177+
CALL runtime·abort(SB)
178+
UNDEF
179+
156180
/*
157181
* support for morestack
158182
*/
@@ -168,6 +192,13 @@ TEXT runtime·getcallerpc(SB),NOSPLIT|NOFRAME,$0-8
168192

169193
// func morestack()
170194
TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
195+
// Called from f.
196+
// Set g->sched to context in f.
197+
MOV X2, (g_sched+gobuf_sp)(g)
198+
MOV T0, (g_sched+gobuf_pc)(g)
199+
MOV RA, (g_sched+gobuf_lr)(g)
200+
MOV CTXT, (g_sched+gobuf_ctxt)(g)
201+
171202
// Cannot grow scheduler stack (m->g0).
172203
MOV g_m(g), A0
173204
MOV m_g0(A0), A1
@@ -181,13 +212,6 @@ TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
181212
CALL runtime·badmorestackgsignal(SB)
182213
CALL runtime·abort(SB)
183214

184-
// Called from f.
185-
// Set g->sched to context in f.
186-
MOV X2, (g_sched+gobuf_sp)(g)
187-
MOV T0, (g_sched+gobuf_pc)(g)
188-
MOV RA, (g_sched+gobuf_lr)(g)
189-
MOV CTXT, (g_sched+gobuf_ctxt)(g)
190-
191215
// Called from f.
192216
// Set m->morebuf to f's caller.
193217
MOV RA, (m_morebuf+gobuf_pc)(A0) // f's caller's PC

src/runtime/crash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ func TestG0StackOverflow(t *testing.T) {
804804
if n := strings.Count(string(out), "morestack on g0\n"); n != 1 {
805805
t.Fatalf("%s\n(exit status %v)", out, err)
806806
}
807-
if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" {
807+
if runtime.CrashStackImplemented {
808808
// check for a stack trace
809809
want := "runtime.stackOverflow"
810810
if n := strings.Count(string(out), want); n < 5 {

src/runtime/export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ var MemclrNoHeapPointers = memclrNoHeapPointers
5050

5151
var CgoCheckPointer = cgoCheckPointer
5252

53+
const CrashStackImplemented = crashStackImplemented
54+
5355
const TracebackInnerFrames = tracebackInnerFrames
5456
const TracebackOuterFrames = tracebackOuterFrames
5557

src/runtime/proc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ func switchToCrashStack(fn func()) {
574574
abort()
575575
}
576576

577-
const crashStackImplemented = GOARCH == "amd64" || GOARCH == "arm64"
577+
const crashStackImplemented = GOARCH == "amd64" || GOARCH == "arm64" || GOARCH == "riscv64"
578578

579579
//go:noescape
580580
func switchToCrashStack0(func()) // in assembly

0 commit comments

Comments
 (0)