From aa0388f2ed937669e9f938da8a65c75ea144ebfd Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 15 Feb 2021 09:25:55 -0500 Subject: runtime: remove unnecessary writes to gp.sched.g A g's sched.g is set in newproc1: newg.sched.g = guintptr(unsafe.Pointer(newg)) After that, it never changes. Yet lots of assembly code does "g.sched.g = g" unnecessarily. Remove all those lines to avoid confusion about whether it ever changes. Also, split gogo into two functions, one that does the nil g check and a second that does the actual switch. This way, if the nil g check fails, we get a stack trace showing the call stack that led to the failure. (The SP write would otherwise cause the stack trace to abort.) Also restore the proper nil g check in a handful of assembly functions. (There is little point in checking for nil g *after* installing it as the real g.) Change-Id: I22866b093f901f765de1d074e36eeec10366abfb Reviewed-on: https://go-review.googlesource.com/c/go/+/292109 Trust: Russ Cox Reviewed-by: Cherry Zhang --- src/runtime/asm_amd64.s | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/runtime/asm_amd64.s') diff --git a/src/runtime/asm_amd64.s b/src/runtime/asm_amd64.s index cd5ce3effb..8ee2ac2123 100644 --- a/src/runtime/asm_amd64.s +++ b/src/runtime/asm_amd64.s @@ -256,10 +256,13 @@ TEXT runtime·asminit(SB),NOSPLIT,$0-0 // func gogo(buf *gobuf) // restore state from Gobuf; longjmp -TEXT runtime·gogo(SB), NOSPLIT, $16-8 +TEXT runtime·gogo(SB), NOSPLIT, $0-8 MOVQ buf+0(FP), BX // gobuf MOVQ gobuf_g(BX), DX MOVQ 0(DX), CX // make sure g != nil + JMP gogo<>(SB) + +TEXT gogo<>(SB), NOSPLIT, $0 get_tls(CX) MOVQ DX, g(CX) MOVQ DX, R14 // set the g register @@ -287,7 +290,6 @@ TEXT runtime·mcall(SB), NOSPLIT, $0-8 MOVQ BX, (g_sched+gobuf_pc)(AX) LEAQ fn+0(FP), BX // caller's SP MOVQ BX, (g_sched+gobuf_sp)(AX) - MOVQ AX, (g_sched+gobuf_g)(AX) MOVQ BP, (g_sched+gobuf_bp)(AX) // switch to m->g0 & its stack, call fn @@ -418,7 +420,6 @@ TEXT runtime·morestack(SB),NOSPLIT,$0-0 // Set g->sched to context in f. MOVQ 0(SP), AX // f's PC MOVQ AX, (g_sched+gobuf_pc)(SI) - MOVQ SI, (g_sched+gobuf_g)(SI) LEAQ 8(SP), AX // f's SP MOVQ AX, (g_sched+gobuf_sp)(SI) MOVQ BP, (g_sched+gobuf_bp)(SI) -- cgit v1.3-6-g1900