diff options
author | Russ Cox <rsc@golang.org> | 2021-01-30 07:07:42 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2021-02-19 00:01:25 +0000 |
commit | 0d94f989d12a52ddc3869dbaa02255873f7a8196 (patch) | |
tree | 8482a8b11c2c556380e6fd4b5d04906506ff528b /src/runtime/sys_windows_arm.s | |
parent | e7ee3c1fa87556245e38b662ea5b3002bbeb32b9 (diff) | |
download | go-0d94f989d12a52ddc3869dbaa02255873f7a8196.tar.gz go-0d94f989d12a52ddc3869dbaa02255873f7a8196.zip |
runtime: clean up system calls during cgo callback init
During a cgocallback, the runtime calls needm to get an m.
The calls made during needm cannot themselves assume that
there is an m or a g (which is attached to the m).
In the old days of making direct system calls, the only thing
you had to do for such functions was mark them //go:nosplit,
to avoid the use of g in the stack split prologue.
But now, on operating systems that make system calls through
shared libraries and use code that saves state in the g or m
before doing so, it's not safe to assume g exists. In fact, it is
not even safe to call getg(), because it might fault deferencing
the TLS storage to find the g pointer (that storage may not be
initialized yet, at least on Windows, and perhaps on other systems
in the future).
The specific routines that are problematic are usleep and osyield,
which are called during lock contention in lockextra, called
from needm.
All this is rather subtle and hidden, so in addition to fixing the
problem on Windows, this CL makes the fact of not running on
a g much clearer by introducing variants usleep_no_g and
osyield_no_g whose names should make clear that there is no g.
And then we can remove the various sketchy getg() == nil checks
in the existing routines.
As part of this cleanup, this CL also deletes onosstack on Windows.
onosstack is from back when the runtime was implemented in C.
It predates systemstack but does essentially the same thing.
Instead of having two different copies of this code, we can use
systemstack consistently. This way we need not port onosstack
to each architecture.
This CL is part of a stack adding windows/arm64
support (#36439), intended to land in the Go 1.17 cycle.
This CL is, however, not windows/arm64-specific.
It is cleanup meant to make the port (and future ports) easier.
Change-Id: I3352de1fd0a3c26267c6e209063e6e86abd26187
Reviewed-on: https://go-review.googlesource.com/c/go/+/288793
Trust: Russ Cox <rsc@golang.org>
Trust: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'src/runtime/sys_windows_arm.s')
-rw-r--r-- | src/runtime/sys_windows_arm.s | 84 |
1 files changed, 9 insertions, 75 deletions
diff --git a/src/runtime/sys_windows_arm.s b/src/runtime/sys_windows_arm.s index fe267080cc..1d928a4f7d 100644 --- a/src/runtime/sys_windows_arm.s +++ b/src/runtime/sys_windows_arm.s @@ -377,79 +377,11 @@ TEXT runtime·tstart_stdcall(SB),NOSPLIT|NOFRAME,$0 MOVW $0, R0 MOVM.IA.W (R13), [R4-R11, R15] // pop {r4-r11, pc} -// onosstack calls fn on OS stack. -// adapted from asm_arm.s : systemstack -// func onosstack(fn unsafe.Pointer, arg uint32) -TEXT runtime·onosstack(SB),NOSPLIT,$0 - MOVW fn+0(FP), R5 // R5 = fn - MOVW arg+4(FP), R6 // R6 = arg - - // This function can be called when there is no g, - // for example, when we are handling a callback on a non-go thread. - // In this case we're already on the system stack. - CMP $0, g - BEQ noswitch - - MOVW g_m(g), R1 // R1 = m - - MOVW m_gsignal(R1), R2 // R2 = gsignal - CMP g, R2 - B.EQ noswitch - - MOVW m_g0(R1), R2 // R2 = g0 - CMP g, R2 - B.EQ noswitch - - MOVW m_curg(R1), R3 - CMP g, R3 - B.EQ switch - - // Bad: g is not gsignal, not g0, not curg. What is it? - // Hide call from linker nosplit analysis. - MOVW $runtime·badsystemstack(SB), R0 - BL (R0) - B runtime·abort(SB) - -switch: - // save our state in g->sched. Pretend to - // be systemstack_switch if the G stack is scanned. - MOVW $runtime·systemstack_switch(SB), R3 - ADD $4, R3, R3 // get past push {lr} - MOVW R3, (g_sched+gobuf_pc)(g) - MOVW R13, (g_sched+gobuf_sp)(g) - MOVW LR, (g_sched+gobuf_lr)(g) - MOVW g, (g_sched+gobuf_g)(g) - - // switch to g0 - MOVW R2, g - MOVW (g_sched+gobuf_sp)(R2), R3 - // make it look like mstart called systemstack on g0, to stop traceback - SUB $4, R3, R3 - MOVW $runtime·mstart(SB), R4 - MOVW R4, 0(R3) - MOVW R3, R13 - - // call target function - MOVW R6, R0 // arg - BL (R5) - - // switch back to g - MOVW g_m(g), R1 - MOVW m_curg(R1), g - MOVW (g_sched+gobuf_sp)(g), R13 - MOVW $0, R3 - MOVW R3, (g_sched+gobuf_sp)(g) - RET - -noswitch: - // Using a tail call here cleans up tracebacks since we won't stop - // at an intermediate systemstack. - MOVW.P 4(R13), R14 // restore LR - MOVW R6, R0 // arg - B (R5) - -// Runs on OS stack. Duration (in 100ns units) is in R0. -TEXT runtime·usleep2(SB),NOSPLIT|NOFRAME,$0 +// Runs on OS stack. +// duration (in -100ns units) is in dt+0(FP). +// g may be nil. +TEXT runtime·usleep2(SB),NOSPLIT|NOFRAME,$0-4 + MOVW dt+0(FP), R0 MOVM.DB.W [R4, R14], (R13) // push {r4, lr} MOVW R13, R4 // Save SP SUB $8, R13 // R13 = R13 - 8 @@ -465,9 +397,11 @@ TEXT runtime·usleep2(SB),NOSPLIT|NOFRAME,$0 MOVW R4, R13 // Restore SP MOVM.IA.W (R13), [R4, R15] // pop {R4, pc} -// Runs on OS stack. Duration (in 100ns units) is in R0. +// Runs on OS stack. +// duration (in -100ns units) is in dt+0(FP). +// g is valid. // TODO: neeeds to be implemented properly. -TEXT runtime·usleep2HighRes(SB),NOSPLIT|NOFRAME,$0 +TEXT runtime·usleep2HighRes(SB),NOSPLIT|NOFRAME,$0-4 B runtime·abort(SB) // Runs on OS stack. |