]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: add crash stack support for riscv64
authorJoel Sing <joel@sing.id.au>
Tue, 31 Oct 2023 14:34:33 +0000 (01:34 +1100)
committerJoel Sing <joel@sing.id.au>
Thu, 2 Nov 2023 14:10:56 +0000 (14:10 +0000)
Change-Id: Ib89a71e20f9c6b86c97814c75cb427e9bd7075e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/538735
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Joel Sing <joel@sing.id.au>

src/runtime/asm.s
src/runtime/asm_riscv64.s
src/runtime/crash_test.go
src/runtime/export_test.go
src/runtime/proc.go

index 84e561fb43dab4e436ca546ecb9bc486cd6a540b..81d3bfbb8a0a6a71516d7d8c9a8b69bba975c15c 100644 (file)
@@ -15,8 +15,10 @@ TEXT ·mapinitnoop<ABIInternal>(SB),NOSPLIT,$0-0
 
 #ifndef GOARCH_amd64
 #ifndef GOARCH_arm64
+#ifndef GOARCH_riscv64
 // stub to appease shared build mode.
 TEXT ·switchToCrashStack0<ABIInternal>(SB),NOSPLIT,$0-0
        UNDEF
 #endif
 #endif
+#endif
index c2142f1dbb9aa8df2707ac442bb62c045acb45c4..8ded78437bbf3919ac01a8abc205a4a8a512e29e 100644 (file)
@@ -153,6 +153,30 @@ TEXT runtime·getcallerpc(SB),NOSPLIT|NOFRAME,$0-8
        MOV     T0, ret+0(FP)
        RET
 
+// func switchToCrashStack0(fn func())
+TEXT runtime·switchToCrashStack0<ABIInternal>(SB), NOSPLIT, $0-8
+       MOV     X10, CTXT                       // context register
+       MOV     g_m(g), X11                     // curm
+
+       // set g to gcrash
+       MOV     $runtime·gcrash(SB), g // g = &gcrash
+       CALL    runtime·save_g(SB)     // clobbers X31
+       MOV     X11, g_m(g)                     // g.m = curm
+       MOV     g, m_g0(X11)                    // curm.g0 = g
+
+       // switch to crashstack
+       MOV     (g_stack+stack_hi)(g), X11
+       ADD     $(-4*8), X11
+       MOV     X11, X2
+
+       // call target function
+       MOV     0(CTXT), X10
+       JALR    X1, X10
+
+       // should never return
+       CALL    runtime·abort(SB)
+       UNDEF
+
 /*
  * support for morestack
  */
@@ -168,6 +192,13 @@ TEXT runtime·getcallerpc(SB),NOSPLIT|NOFRAME,$0-8
 
 // func morestack()
 TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
+       // Called from f.
+       // Set g->sched to context in f.
+       MOV     X2, (g_sched+gobuf_sp)(g)
+       MOV     T0, (g_sched+gobuf_pc)(g)
+       MOV     RA, (g_sched+gobuf_lr)(g)
+       MOV     CTXT, (g_sched+gobuf_ctxt)(g)
+
        // Cannot grow scheduler stack (m->g0).
        MOV     g_m(g), A0
        MOV     m_g0(A0), A1
@@ -181,13 +212,6 @@ TEXT runtime·morestack(SB),NOSPLIT|NOFRAME,$0-0
        CALL    runtime·badmorestackgsignal(SB)
        CALL    runtime·abort(SB)
 
-       // Called from f.
-       // Set g->sched to context in f.
-       MOV     X2, (g_sched+gobuf_sp)(g)
-       MOV     T0, (g_sched+gobuf_pc)(g)
-       MOV     RA, (g_sched+gobuf_lr)(g)
-       MOV     CTXT, (g_sched+gobuf_ctxt)(g)
-
        // Called from f.
        // Set m->morebuf to f's caller.
        MOV     RA, (m_morebuf+gobuf_pc)(A0)    // f's caller's PC
index 8aa01f724d7b517639b0afa9b709dd90c76f1f51..892a56355ace7c43e541df4e7c817e013ac11de4 100644 (file)
@@ -804,7 +804,7 @@ func TestG0StackOverflow(t *testing.T) {
                if n := strings.Count(string(out), "morestack on g0\n"); n != 1 {
                        t.Fatalf("%s\n(exit status %v)", out, err)
                }
-               if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" {
+               if runtime.CrashStackImplemented {
                        // check for a stack trace
                        want := "runtime.stackOverflow"
                        if n := strings.Count(string(out), want); n < 5 {
index 922794edd6d3dced6d71f4f2ed6429496d779d85..1d4a9748717d36c142dc4990084eb96b38429a4f 100644 (file)
@@ -50,6 +50,8 @@ var MemclrNoHeapPointers = memclrNoHeapPointers
 
 var CgoCheckPointer = cgoCheckPointer
 
+const CrashStackImplemented = crashStackImplemented
+
 const TracebackInnerFrames = tracebackInnerFrames
 const TracebackOuterFrames = tracebackOuterFrames
 
index 7189a0650ada59dcaa132e05ee9049c8785ab62e..9b5f2e9a6d91c368220130a4cd63cc3859095163 100644 (file)
@@ -574,7 +574,7 @@ func switchToCrashStack(fn func()) {
        abort()
 }
 
-const crashStackImplemented = GOARCH == "amd64" || GOARCH == "arm64"
+const crashStackImplemented = GOARCH == "amd64" || GOARCH == "arm64" || GOARCH == "riscv64"
 
 //go:noescape
 func switchToCrashStack0(func()) // in assembly