]> Cypherpunks.ru repositories - gostls13.git/commit
runtime: clean up system calls during cgo callback init
authorRuss Cox <rsc@golang.org>
Sat, 30 Jan 2021 12:07:42 +0000 (07:07 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 19 Feb 2021 00:01:25 +0000 (00:01 +0000)
commit0d94f989d12a52ddc3869dbaa02255873f7a8196
tree8482a8b11c2c556380e6fd4b5d04906506ff528b
parente7ee3c1fa87556245e38b662ea5b3002bbeb32b9
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>
30 files changed:
src/runtime/asm_386.s
src/runtime/asm_amd64.s
src/runtime/asm_arm.s
src/runtime/asm_arm64.s
src/runtime/asm_mips64x.s
src/runtime/os2_aix.go
src/runtime/os3_solaris.go
src/runtime/os_darwin.go
src/runtime/os_dragonfly.go
src/runtime/os_freebsd.go
src/runtime/os_js.go
src/runtime/os_linux.go
src/runtime/os_netbsd.go
src/runtime/os_openbsd_syscall1.go
src/runtime/os_openbsd_syscall2.go
src/runtime/os_plan9.go
src/runtime/os_windows.go
src/runtime/proc.go
src/runtime/stubs2.go
src/runtime/stubs_386.go
src/runtime/stubs_amd64.go
src/runtime/stubs_arm.go
src/runtime/stubs_arm64.go
src/runtime/stubs_mips64x.go
src/runtime/sys_darwin.go
src/runtime/sys_openbsd1.go
src/runtime/sys_openbsd2.go
src/runtime/sys_windows_386.s
src/runtime/sys_windows_amd64.s
src/runtime/sys_windows_arm.s