mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: fix uses of ABIInternal PCs in assembly
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>
This commit is contained in:
parent
1f29e69bad
commit
ec721d92bf
11 changed files with 64 additions and 11 deletions
|
|
@ -166,8 +166,8 @@ func init() {
|
||||||
if Experiment.RegabiG && !Experiment.RegabiWrappers {
|
if Experiment.RegabiG && !Experiment.RegabiWrappers {
|
||||||
panic("GOEXPERIMENT regabig requires regabiwrappers")
|
panic("GOEXPERIMENT regabig requires regabiwrappers")
|
||||||
}
|
}
|
||||||
if Experiment.RegabiArgs && !(Experiment.RegabiWrappers && Experiment.RegabiReflect && Experiment.RegabiDefer) {
|
if Experiment.RegabiArgs && !(Experiment.RegabiWrappers && Experiment.RegabiG && Experiment.RegabiReflect && Experiment.RegabiDefer) {
|
||||||
panic("GOEXPERIMENT regabiargs requires regabiwrappers,regabireflect,regabidefer")
|
panic("GOEXPERIMENT regabiargs requires regabiwrappers,regabig,regabireflect,regabidefer")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set GOEXPERIMENT to the parsed and canonicalized set of experiments.
|
// Set GOEXPERIMENT to the parsed and canonicalized set of experiments.
|
||||||
|
|
@ -242,7 +242,11 @@ type ExpFlags struct {
|
||||||
// RegabiArgs enables register arguments/results in all
|
// RegabiArgs enables register arguments/results in all
|
||||||
// compiled Go functions.
|
// 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
|
RegabiArgs bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -285,6 +285,34 @@ TEXT gogo<>(SB), NOSPLIT, $0
|
||||||
// Switch to m->g0's stack, call fn(g).
|
// Switch to m->g0's stack, call fn(g).
|
||||||
// Fn must never return. It should gogo(&g->sched)
|
// Fn must never return. It should gogo(&g->sched)
|
||||||
// to keep running g.
|
// 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
|
TEXT runtime·mcall(SB), NOSPLIT, $0-8
|
||||||
MOVQ fn+0(FP), DI
|
MOVQ fn+0(FP), DI
|
||||||
|
|
||||||
|
|
@ -315,6 +343,7 @@ TEXT runtime·mcall(SB), NOSPLIT, $0-8
|
||||||
MOVQ $runtime·badmcall2(SB), AX
|
MOVQ $runtime·badmcall2(SB), AX
|
||||||
JMP AX
|
JMP AX
|
||||||
RET
|
RET
|
||||||
|
#endif
|
||||||
|
|
||||||
// systemstack_switch is a dummy routine that systemstack leaves at the bottom
|
// systemstack_switch is a dummy routine that systemstack leaves at the bottom
|
||||||
// of the G stack. We need to distinguish the routine that
|
// of the G stack. We need to distinguish the routine that
|
||||||
|
|
|
||||||
|
|
@ -195,7 +195,7 @@ func cgocall(fn, arg unsafe.Pointer) int32 {
|
||||||
return errno
|
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
|
//go:nosplit
|
||||||
func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
|
func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
|
||||||
gp := getg()
|
gp := getg()
|
||||||
|
|
|
||||||
|
|
@ -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
|
// created thread. On NetBSD, a new thread inherits the signal stack
|
||||||
// of the creating thread. That confuses minit, so we remove that
|
// of the creating thread. That confuses minit, so we remove that
|
||||||
// signal stack here before calling the regular mstart. It's a bit
|
// 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.
|
// 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.
|
// At this point all signals are blocked, so there is no race.
|
||||||
//go:nosplit
|
//go:nosplit
|
||||||
func netbsdMstart() {
|
func netbsdMstart0() {
|
||||||
st := stackt{ss_flags: _SS_DISABLE}
|
st := stackt{ss_flags: _SS_DISABLE}
|
||||||
sigaltstack(&st, nil)
|
sigaltstack(&st, nil)
|
||||||
mstart()
|
mstart0()
|
||||||
}
|
}
|
||||||
|
|
||||||
func osinit() {
|
func osinit() {
|
||||||
|
|
|
||||||
|
|
@ -1258,7 +1258,7 @@ func mStackIsSystemAllocated() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// mstart is the entry-point for new Ms.
|
// 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()
|
func mstart()
|
||||||
|
|
||||||
// mstart0 is the Go entry-point for new Ms.
|
// mstart0 is the Go entry-point for new Ms.
|
||||||
|
|
|
||||||
|
|
@ -652,7 +652,7 @@ nog1:
|
||||||
CALL runtime·stackcheck(SB)
|
CALL runtime·stackcheck(SB)
|
||||||
|
|
||||||
nog2:
|
nog2:
|
||||||
// Call fn
|
// Call fn. This is the PC of an ABI0 function.
|
||||||
CALL R12
|
CALL R12
|
||||||
|
|
||||||
// It shouldn't return. If it does, exit that thread.
|
// It shouldn't return. If it does, exit that thread.
|
||||||
|
|
|
||||||
|
|
@ -376,6 +376,10 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
|
||||||
MOVL $0x1234, 0x1005
|
MOVL $0x1234, 0x1005
|
||||||
RET
|
RET
|
||||||
|
|
||||||
|
TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
|
||||||
|
CALL ·netbsdMstart0(SB)
|
||||||
|
RET // not reached
|
||||||
|
|
||||||
TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
|
TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
|
||||||
MOVL $SYS___sigaltstack14, AX
|
MOVL $SYS___sigaltstack14, AX
|
||||||
MOVL new+0(FP), BX
|
MOVL new+0(FP), BX
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
|
||||||
MOVQ R9, g(CX)
|
MOVQ R9, g(CX)
|
||||||
CALL runtime·stackcheck(SB)
|
CALL runtime·stackcheck(SB)
|
||||||
|
|
||||||
// Call fn
|
// Call fn. This is an ABI0 PC.
|
||||||
CALL R12
|
CALL R12
|
||||||
|
|
||||||
// It shouldn't return. If it does, exit.
|
// It shouldn't return. If it does, exit.
|
||||||
|
|
@ -78,6 +78,10 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
|
||||||
SYSCALL
|
SYSCALL
|
||||||
JMP -3(PC) // keep exiting
|
JMP -3(PC) // keep exiting
|
||||||
|
|
||||||
|
TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
|
||||||
|
CALL ·netbsdMstart0(SB)
|
||||||
|
RET // not reached
|
||||||
|
|
||||||
TEXT runtime·osyield(SB),NOSPLIT,$0
|
TEXT runtime·osyield(SB),NOSPLIT,$0
|
||||||
MOVL $SYS_sched_yield, AX
|
MOVL $SYS_sched_yield, AX
|
||||||
SYSCALL
|
SYSCALL
|
||||||
|
|
|
||||||
|
|
@ -177,6 +177,10 @@ TEXT runtime·lwp_tramp(SB),NOSPLIT,$0
|
||||||
MOVW R8, (R8)
|
MOVW R8, (R8)
|
||||||
RET
|
RET
|
||||||
|
|
||||||
|
TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
|
||||||
|
BL ·netbsdMstart0(SB)
|
||||||
|
RET // not reached
|
||||||
|
|
||||||
TEXT runtime·usleep(SB),NOSPLIT,$16
|
TEXT runtime·usleep(SB),NOSPLIT,$16
|
||||||
MOVW usec+0(FP), R0
|
MOVW usec+0(FP), R0
|
||||||
CALL runtime·usplitR0(SB)
|
CALL runtime·usplitR0(SB)
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,10 @@ nog:
|
||||||
MOVD $0, R0 // crash (not reached)
|
MOVD $0, R0 // crash (not reached)
|
||||||
MOVD R0, (R8)
|
MOVD R0, (R8)
|
||||||
|
|
||||||
|
TEXT ·netbsdMstart(SB),NOSPLIT|TOPFRAME,$0
|
||||||
|
CALL ·netbsdMstart0(SB)
|
||||||
|
RET // not reached
|
||||||
|
|
||||||
TEXT runtime·osyield(SB),NOSPLIT,$0
|
TEXT runtime·osyield(SB),NOSPLIT,$0
|
||||||
SVC $SYS_sched_yield
|
SVC $SYS_sched_yield
|
||||||
RET
|
RET
|
||||||
|
|
|
||||||
|
|
@ -320,7 +320,7 @@ TEXT runtime·callbackasm1(SB),NOSPLIT,$0
|
||||||
// Call cgocallback, which will call callbackWrap(frame).
|
// Call cgocallback, which will call callbackWrap(frame).
|
||||||
MOVQ $0, 16(SP) // context
|
MOVQ $0, 16(SP) // context
|
||||||
MOVQ AX, 8(SP) // frame (address of callbackArgs)
|
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)
|
MOVQ BX, 0(SP) // PC of function value to call (callbackWrap)
|
||||||
CALL ·cgocallback(SB)
|
CALL ·cgocallback(SB)
|
||||||
// Get callback result.
|
// Get callback result.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue