]> Cypherpunks.ru repositories - gostls13.git/commitdiff
runtime: fix uses of ABIInternal PCs in assembly
authorAustin Clements <austin@google.com>
Mon, 29 Mar 2021 21:38:20 +0000 (17:38 -0400)
committerAustin Clements <austin@google.com>
Thu, 1 Apr 2021 00:51:26 +0000 (00:51 +0000)
The covers three kinds of uses:

1. Calls of closures from assembly. These are always ABIInternal calls
without wrappers. I went through every indirect call in the runtime
and I think mcall is the only case of assembly calling a Go closure in
a way that's affected by ABIInternal. systemstack also calls a
closure, but it takes no arguments.

2. Calls of Go functions that expect raw ABIInternal pointers. I also
only found one of these: callbackasm1 -> cgocallback on Windows. These
are trickier to find, though.

3. Finally, I found one case on NetBSD where new OS threads were
directly calling the Go runtime entry-point from assembly via a PC,
rather than going through a wrapper. This meant new threads may not
have special registers set up. In this case, a change on all other
OSes had already forced new thread entry to go through an ABI wrapper,
so I just caught NetBSD up with that change.

With this change, I'm able to run a "hello world" with
GOEXPERIMENT=regabi,regabiargs.

For #40724.

Change-Id: I2a6d0e530c4fd4edf13484d923891c6160d683aa
Reviewed-on: https://go-review.googlesource.com/c/go/+/305669
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/internal/objabi/util.go
src/runtime/asm_amd64.s
src/runtime/cgocall.go
src/runtime/os_netbsd.go
src/runtime/proc.go
src/runtime/sys_linux_amd64.s
src/runtime/sys_netbsd_386.s
src/runtime/sys_netbsd_amd64.s
src/runtime/sys_netbsd_arm.s
src/runtime/sys_netbsd_arm64.s
src/runtime/sys_windows_amd64.s

index e066311cd1a06ff8eae4a4c5c26aa3e1ad9daa8b..ae03aac31ac4539311a32b09fc2300c729b0a603 100644 (file)
@@ -166,8 +166,8 @@ func init() {
        if Experiment.RegabiG && !Experiment.RegabiWrappers {
                panic("GOEXPERIMENT regabig requires regabiwrappers")
        }
-       if Experiment.RegabiArgs && !(Experiment.RegabiWrappers && Experiment.RegabiReflect && Experiment.RegabiDefer) {
-               panic("GOEXPERIMENT regabiargs requires regabiwrappers,regabireflect,regabidefer")
+       if Experiment.RegabiArgs && !(Experiment.RegabiWrappers && Experiment.RegabiG && Experiment.RegabiReflect && Experiment.RegabiDefer) {
+               panic("GOEXPERIMENT regabiargs requires regabiwrappers,regabig,regabireflect,regabidefer")
        }
 
        // Set GOEXPERIMENT to the parsed and canonicalized set of experiments.
@@ -242,7 +242,11 @@ type ExpFlags struct {
        // RegabiArgs enables register arguments/results in all
        // compiled Go functions.
        //
-       // Requires wrappers, reflect, defer.
+       // Requires wrappers (to do ABI translation), g (because
+       // runtime assembly that's been ported to ABIInternal uses the
+       // G register), reflect (so reflection calls use registers),
+       // and defer (because the runtime doesn't support passing
+       // register arguments to defer/go).
        RegabiArgs bool
 }
 
index b9efad06813d03be96e6c57158d5ef689d5713b6..193d8f00bb2aa44f2c7ce17ac277cc5eed29dcfc 100644 (file)
@@ -285,6 +285,34 @@ TEXT gogo<>(SB), NOSPLIT, $0
 // Switch to m->g0's stack, call fn(g).
 // Fn must never return. It should gogo(&g->sched)
 // to keep running g.
+#ifdef GOEXPERIMENT_REGABI_ARGS
+TEXT runtime·mcall<ABIInternal>(SB), NOSPLIT, $0-8
+       MOVQ    AX, DX  // DX = fn
+
+       // save state in g->sched
+       MOVQ    0(SP), BX       // caller's PC
+       MOVQ    BX, (g_sched+gobuf_pc)(R14)
+       LEAQ    fn+0(FP), BX    // caller's SP
+       MOVQ    BX, (g_sched+gobuf_sp)(R14)
+       MOVQ    BP, (g_sched+gobuf_bp)(R14)
+
+       // switch to m->g0 & its stack, call fn
+       MOVQ    g_m(R14), BX
+       MOVQ    m_g0(BX), SI    // SI = g.m.g0
+       CMPQ    SI, R14 // if g == m->g0 call badmcall
+       JNE     goodm
+       JMP     runtime·badmcall(SB)
+goodm:
+       MOVQ    R14, AX         // AX (and arg 0) = g
+       MOVQ    SI, R14         // g = g.m.g0
+       get_tls(CX)             // Set G in TLS
+       MOVQ    R14, g(CX)
+       MOVQ    (g_sched+gobuf_sp)(R14), SP     // sp = g0.sched.sp
+       MOVQ    0(DX), R12
+       CALL    R12             // fn(g)
+       JMP     runtime·badmcall2(SB)
+       RET
+#else
 TEXT runtime·mcall(SB), NOSPLIT, $0-8
        MOVQ    fn+0(FP), DI
 
@@ -315,6 +343,7 @@ TEXT runtime·mcall(SB), NOSPLIT, $0-8
        MOVQ    $runtime·badmcall2(SB), AX
        JMP     AX
        RET
+#endif
 
 // systemstack_switch is a dummy routine that systemstack leaves at the bottom
 // of the G stack. We need to distinguish the routine that
index 534a2c42951949e003aa6370acfe38ececc03d48..0e287d0b8ede48f92fcaf919c3998f86db15bbae 100644 (file)
@@ -195,7 +195,7 @@ func cgocall(fn, arg unsafe.Pointer) int32 {
        return errno
 }
 
-// Call from C back to Go.
+// Call from C back to Go. fn must point to an ABIInternal Go entry-point.
 //go:nosplit
 func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
        gp := getg()
index 0328fa57ae67082b05e054b45a93877a924c4898..6fbb3aa6947bad99357103f6adb00364ed5b9a02 100644 (file)
@@ -228,7 +228,11 @@ func newosproc(mp *m) {
        }
 }
 
-// netbsdMStart is the function call that starts executing a newly
+// mstart is the entry-point for new Ms.
+// It is written in assembly, uses ABI0, is marked TOPFRAME, and calls netbsdMstart0.
+func netbsdMstart()
+
+// netbsdMStart0 is the function call that starts executing a newly
 // created thread. On NetBSD, a new thread inherits the signal stack
 // of the creating thread. That confuses minit, so we remove that
 // signal stack here before calling the regular mstart. It's a bit
@@ -236,10 +240,10 @@ func newosproc(mp *m) {
 // it's a simple change that keeps NetBSD working like other OS's.
 // At this point all signals are blocked, so there is no race.
 //go:nosplit
-func netbsdMstart() {
+func netbsdMstart0() {
        st := stackt{ss_flags: _SS_DISABLE}
        sigaltstack(&st, nil)
-       mstart()
+       mstart0()
 }
 
 func osinit() {
index 2a7a766b25b053aef3102623009d44169c1c3b22..a256b6e04afa6ac49c9e21438c368fcdab659566 100644 (file)
@@ -1258,7 +1258,7 @@ func mStackIsSystemAllocated() bool {
 }
 
 // mstart is the entry-point for new Ms.
-// It is written in assembly, marked TOPFRAME, and calls mstart0.
+// It is written in assembly, uses ABI0, is marked TOPFRAME, and calls mstart0.
 func mstart()
 
 // mstart0 is the Go entry-point for new Ms.
index 584f2c5b1e66b13521c3401349470aa1f5244bde..7b538c3e2f2ddc9c9ccda752c47630835ff02cae 100644 (file)
@@ -652,7 +652,7 @@ nog1:
        CALL    runtime·stackcheck(SB)
 
 nog2:
-       // Call fn
+       // Call fn. This is the PC of an ABI0 function.
        CALL    R12
 
        // It shouldn't return. If it does, exit that thread.
index d0c470c4575cc8fe9a540467775aa7357d7caa1b..d3f22454a40d746669181d79278230d6bf786022 100644 (file)
@@ -376,6 +376,10 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
        MOVL    $0x1234, 0x1005
        RET
 
+TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
+       CALL    ·netbsdMstart0(SB)
+       RET // not reached
+
 TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
        MOVL    $SYS___sigaltstack14, AX
        MOVL    new+0(FP), BX
index dc9bd127d2349d0dc0f5d79a99e2e90946569aca..addd98cd2761adc0661a3b88eec75d1c9615c4cd 100644 (file)
@@ -70,7 +70,7 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
        MOVQ    R9, g(CX)
        CALL    runtime·stackcheck(SB)
 
-       // Call fn
+       // Call fn. This is an ABI0 PC.
        CALL    R12
 
        // It shouldn't return. If it does, exit.
@@ -78,6 +78,10 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
        SYSCALL
        JMP     -3(PC)                  // keep exiting
 
+TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
+       CALL    ·netbsdMstart0(SB)
+       RET // not reached
+
 TEXT runtime·osyield(SB),NOSPLIT,$0
        MOVL    $SYS_sched_yield, AX
        SYSCALL
index 678dea57c6803f79b74f76ae2da4285ab3bc3f26..82f9d2161e4639fec71fd1ded5acc4cc29067365 100644 (file)
@@ -177,6 +177,10 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
        MOVW R8, (R8)
        RET
 
+TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
+       BL ·netbsdMstart0(SB)
+       RET // not reached
+
 TEXT runtime·usleep(SB),NOSPLIT,$16
        MOVW usec+0(FP), R0
        CALL runtime·usplitR0(SB)
index 4d9b05478fabdc0e7914fbd13e95892baebfd6c0..1446a27f4c8d77c8db9b66cb741315d980808d81 100644 (file)
@@ -76,6 +76,10 @@ nog:
        MOVD    $0, R0  // crash (not reached)
        MOVD    R0, (R8)
 
+TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
+       CALL    ·netbsdMstart0(SB)
+       RET // not reached
+
 TEXT runtime·osyield(SB),NOSPLIT,$0
        SVC     $SYS_sched_yield
        RET
index 8a9174161914cb762595ca4a1eaf4eb0282b549f..099894efe7dbad57a022b933d4f6da7274639ce4 100644 (file)
@@ -320,7 +320,7 @@ TEXT runtime·callbackasm1(SB),NOSPLIT,$0
        // Call cgocallback, which will call callbackWrap(frame).
        MOVQ    $0, 16(SP)      // context
        MOVQ    AX, 8(SP)       // frame (address of callbackArgs)
-       LEAQ    ·callbackWrap(SB), BX
+       LEAQ    ·callbackWrap<ABIInternal>(SB), BX     // cgocallback takes an ABIInternal entry-point
        MOVQ    BX, 0(SP)       // PC of function value to call (callbackWrap)
        CALL    ·cgocallback(SB)
        // Get callback result.