2014-11-11 15:08:54 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
//extern SigTabTT runtime·sigtab[];
|
|
|
|
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
type sigset uint32
|
|
|
|
|
|
|
|
var sigset_all = ^sigset(0)
|
2014-11-11 15:08:54 -07:00
|
|
|
|
|
|
|
func unimplemented(name string) {
|
|
|
|
println(name, "not implemented")
|
|
|
|
*(*int)(unsafe.Pointer(uintptr(1231))) = 1231
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func semawakeup(mp *m) {
|
2015-10-21 19:36:05 -06:00
|
|
|
mach_semrelease(mp.waitsema)
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
2015-10-21 19:36:05 -06:00
|
|
|
func semacreate(mp *m) {
|
|
|
|
if mp.waitsema != 0 {
|
|
|
|
return
|
|
|
|
}
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(func() {
|
2015-10-21 19:36:05 -06:00
|
|
|
mp.waitsema = mach_semcreate()
|
2014-11-11 15:08:54 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// BSD interface for threading.
|
|
|
|
func osinit() {
|
|
|
|
// bsdthread_register delayed until end of goenvs so that we
|
|
|
|
// can look at the environment first.
|
|
|
|
|
2015-08-07 09:48:52 -06:00
|
|
|
ncpu = getncpu()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getncpu() int32 {
|
2014-11-11 15:08:54 -07:00
|
|
|
// Use sysctl to fetch hw.ncpu.
|
|
|
|
mib := [2]uint32{6, 3}
|
|
|
|
out := uint32(0)
|
|
|
|
nout := unsafe.Sizeof(out)
|
|
|
|
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
|
2015-08-07 09:48:52 -06:00
|
|
|
if ret >= 0 && int32(out) > 0 {
|
|
|
|
return int32(out)
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
2015-08-07 09:48:52 -06:00
|
|
|
return 1
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
2015-01-08 16:30:22 -07:00
|
|
|
var urandom_dev = []byte("/dev/urandom\x00")
|
2014-11-11 15:08:54 -07:00
|
|
|
|
|
|
|
//go:nosplit
|
2014-12-09 15:40:40 -07:00
|
|
|
func getRandomData(r []byte) {
|
2014-11-11 15:08:54 -07:00
|
|
|
fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
|
2014-12-09 15:40:40 -07:00
|
|
|
n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
|
2015-04-13 17:37:04 -06:00
|
|
|
closefd(fd)
|
2014-12-09 15:40:40 -07:00
|
|
|
extendRandom(r, int(n))
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func goenvs() {
|
|
|
|
goenvs_unix()
|
|
|
|
|
|
|
|
// Register our thread-creation callback (see sys_darwin_{amd64,386}.s)
|
|
|
|
// but only if we're not using cgo. If we are using cgo we need
|
|
|
|
// to let the C pthread library install its own thread-creation callback.
|
|
|
|
if !iscgo {
|
|
|
|
if bsdthread_register() != 0 {
|
|
|
|
if gogetenv("DYLD_INSERT_LIBRARIES") != "" {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: bsdthread_register error (unset DYLD_INSERT_LIBRARIES)")
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: bsdthread_register error")
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 08:20:54 -06:00
|
|
|
// May run with m.p==nil, so write barriers are not allowed.
|
runtime: disallow write barriers in handoffp and callees
handoffp by definition runs without a P, so it's not allowed to have
write barriers. It doesn't have any right now, but mark it
nowritebarrier to disallow any creeping in in the future. handoffp in
turns calls startm, newm, and newosproc, all of which are "below Go"
and make sense to run without a P, so disallow write barriers in these
as well.
For most functions, we've done this because they may race with
stoptheworld() and hence must not have write barriers. For these
functions, it's a little different: the world can't stop while we're
in handoffp, so this race isn't present. But we implement this
restriction with a somewhat broader rule that you can't have a write
barrier without a P. We like this rule because it's simple and means
that our write barriers can depend on there being a P, even though
this rule is actually a little broader than necessary. Hence, even
though there's no danger of the race in these functions, we want to
adhere to the broader rule.
Change-Id: Ie22319c30eea37d703eb52f5c7ca5da872030b88
Reviewed-on: https://go-review.googlesource.com/8130
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2015-03-26 13:50:22 -06:00
|
|
|
//go:nowritebarrier
|
2014-11-11 15:08:54 -07:00
|
|
|
func newosproc(mp *m, stk unsafe.Pointer) {
|
|
|
|
if false {
|
2015-11-12 15:26:19 -07:00
|
|
|
print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
var oset sigset
|
2014-11-11 15:08:54 -07:00
|
|
|
sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
|
2015-04-08 12:16:26 -06:00
|
|
|
errno := bsdthread_create(stk, unsafe.Pointer(mp), funcPC(mstart))
|
2014-11-11 15:08:54 -07:00
|
|
|
sigprocmask(_SIG_SETMASK, &oset, nil)
|
|
|
|
|
|
|
|
if errno < 0 {
|
|
|
|
print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -errno, ")\n")
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime.newosproc")
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:16:26 -06:00
|
|
|
// newosproc0 is a version of newosproc that can be called before the runtime
|
|
|
|
// is initialized.
|
|
|
|
//
|
|
|
|
// As Go uses bsdthread_register when running without cgo, this function is
|
|
|
|
// not safe to use after initialization as it does not pass an M as fnarg.
|
|
|
|
//
|
|
|
|
//go:nosplit
|
|
|
|
func newosproc0(stacksize uintptr, fn unsafe.Pointer, fnarg uintptr) {
|
2015-04-16 15:32:18 -06:00
|
|
|
stack := sysAlloc(stacksize, &memstats.stacks_sys)
|
2015-04-08 12:16:26 -06:00
|
|
|
if stack == nil {
|
|
|
|
write(2, unsafe.Pointer(&failallocatestack[0]), int32(len(failallocatestack)))
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
stk := unsafe.Pointer(uintptr(stack) + stacksize)
|
|
|
|
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
var oset sigset
|
2015-04-08 12:16:26 -06:00
|
|
|
sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
|
|
|
|
errno := bsdthread_create(stk, fn, fnarg)
|
|
|
|
sigprocmask(_SIG_SETMASK, &oset, nil)
|
|
|
|
|
|
|
|
if errno < 0 {
|
|
|
|
write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate)))
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n")
|
|
|
|
var failthreadcreate = []byte("runtime: failed to create new OS thread\n")
|
|
|
|
|
2014-11-11 15:08:54 -07:00
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
|
|
|
// Called on the parent thread (main thread in case of bootstrap), can allocate memory.
|
|
|
|
func mpreinit(mp *m) {
|
|
|
|
mp.gsignal = malg(32 * 1024) // OS X wants >= 8K
|
|
|
|
mp.gsignal.m = mp
|
|
|
|
}
|
|
|
|
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
runtime: don't always unblock all signals
Ian proposed an improved way of handling signals masks in Go, motivated
by a problem where the Android java runtime expects certain signals to
be blocked for all JVM threads. Discussion here
https://groups.google.com/forum/#!topic/golang-dev/_TSCkQHJt6g
Ian's text is used in the following:
A Go program always needs to have the synchronous signals enabled.
These are the signals for which _SigPanic is set in sigtable, namely
SIGSEGV, SIGBUS, SIGFPE.
A Go program that uses the os/signal package, and calls signal.Notify,
needs to have at least one thread which is not blocking that signal,
but it doesn't matter much which one.
Unix programs do not change signal mask across execve. They inherit
signal masks across fork. The shell uses this fact to some extent;
for example, the job control signals (SIGTTIN, SIGTTOU, SIGTSTP) are
blocked for commands run due to backquote quoting or $().
Our current position on signal masks was not thought out. We wandered
into step by step, e.g., http://golang.org/cl/7323067 .
This CL does the following:
Introduce a new platform hook, msigsave, that saves the signal mask of
the current thread to m.sigsave.
Call msigsave from needm and newm.
In minit grab set up the signal mask from m.sigsave and unblock the
essential synchronous signals, and SIGILL, SIGTRAP, SIGPROF, SIGSTKFLT
(for systems that have it).
In unminit, restore the signal mask from m.sigsave.
The first time that os/signal.Notify is called, start a new thread whose
only purpose is to update its signal mask to make sure signals for
signal.Notify are unblocked on at least one thread.
The effect on Go programs will be that if they are invoked with some
non-synchronous signals blocked, those signals will normally be
ignored. Previously, those signals would mostly be ignored. A change
in behaviour will occur for programs started with any of these signals
blocked, if they receive the signal: SIGHUP, SIGINT, SIGQUIT, SIGABRT,
SIGTERM. Previously those signals would always cause a crash (unless
using the os/signal package); with this change, they will be ignored
if the program is started with the signal blocked (and does not use
the os/signal package).
./all.bash completes successfully on linux/amd64.
OpenBSD is missing the implementation.
Change-Id: I188098ba7eb85eae4c14861269cc466f2aa40e8c
Reviewed-on: https://go-review.googlesource.com/10173
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-18 03:00:24 -06:00
|
|
|
func msigsave(mp *m) {
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
sigprocmask(_SIG_SETMASK, nil, &mp.sigmask)
|
runtime: don't always unblock all signals
Ian proposed an improved way of handling signals masks in Go, motivated
by a problem where the Android java runtime expects certain signals to
be blocked for all JVM threads. Discussion here
https://groups.google.com/forum/#!topic/golang-dev/_TSCkQHJt6g
Ian's text is used in the following:
A Go program always needs to have the synchronous signals enabled.
These are the signals for which _SigPanic is set in sigtable, namely
SIGSEGV, SIGBUS, SIGFPE.
A Go program that uses the os/signal package, and calls signal.Notify,
needs to have at least one thread which is not blocking that signal,
but it doesn't matter much which one.
Unix programs do not change signal mask across execve. They inherit
signal masks across fork. The shell uses this fact to some extent;
for example, the job control signals (SIGTTIN, SIGTTOU, SIGTSTP) are
blocked for commands run due to backquote quoting or $().
Our current position on signal masks was not thought out. We wandered
into step by step, e.g., http://golang.org/cl/7323067 .
This CL does the following:
Introduce a new platform hook, msigsave, that saves the signal mask of
the current thread to m.sigsave.
Call msigsave from needm and newm.
In minit grab set up the signal mask from m.sigsave and unblock the
essential synchronous signals, and SIGILL, SIGTRAP, SIGPROF, SIGSTKFLT
(for systems that have it).
In unminit, restore the signal mask from m.sigsave.
The first time that os/signal.Notify is called, start a new thread whose
only purpose is to update its signal mask to make sure signals for
signal.Notify are unblocked on at least one thread.
The effect on Go programs will be that if they are invoked with some
non-synchronous signals blocked, those signals will normally be
ignored. Previously, those signals would mostly be ignored. A change
in behaviour will occur for programs started with any of these signals
blocked, if they receive the signal: SIGHUP, SIGINT, SIGQUIT, SIGABRT,
SIGTERM. Previously those signals would always cause a crash (unless
using the os/signal package); with this change, they will be ignored
if the program is started with the signal blocked (and does not use
the os/signal package).
./all.bash completes successfully on linux/amd64.
OpenBSD is missing the implementation.
Change-Id: I188098ba7eb85eae4c14861269cc466f2aa40e8c
Reviewed-on: https://go-review.googlesource.com/10173
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-18 03:00:24 -06:00
|
|
|
}
|
|
|
|
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
|
|
|
func msigrestore(mp *m) {
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
sigprocmask(_SIG_SETMASK, &mp.sigmask, nil)
|
2015-11-13 14:21:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func sigblock() {
|
|
|
|
sigprocmask(_SIG_SETMASK, &sigset_all, nil)
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:08:54 -07:00
|
|
|
// Called to initialize a new m (including the bootstrap m).
|
|
|
|
// Called on the new thread, can not allocate memory.
|
|
|
|
func minit() {
|
|
|
|
// Initialize signal handling.
|
|
|
|
_g_ := getg()
|
2015-12-23 19:38:18 -07:00
|
|
|
|
|
|
|
var st stackt
|
|
|
|
sigaltstack(nil, &st)
|
|
|
|
if st.ss_flags&_SS_DISABLE != 0 {
|
|
|
|
signalstack(&_g_.m.gsignal.stack)
|
|
|
|
_g_.m.newSigstack = true
|
|
|
|
} else {
|
|
|
|
// Use existing signal stack.
|
|
|
|
stsp := uintptr(unsafe.Pointer(st.ss_sp))
|
|
|
|
_g_.m.gsignal.stack.lo = stsp
|
|
|
|
_g_.m.gsignal.stack.hi = stsp + st.ss_size
|
|
|
|
_g_.m.gsignal.stackguard0 = stsp + _StackGuard
|
|
|
|
_g_.m.gsignal.stackguard1 = stsp + _StackGuard
|
|
|
|
_g_.m.gsignal.stackAlloc = st.ss_size
|
|
|
|
_g_.m.newSigstack = false
|
|
|
|
}
|
runtime: don't always unblock all signals
Ian proposed an improved way of handling signals masks in Go, motivated
by a problem where the Android java runtime expects certain signals to
be blocked for all JVM threads. Discussion here
https://groups.google.com/forum/#!topic/golang-dev/_TSCkQHJt6g
Ian's text is used in the following:
A Go program always needs to have the synchronous signals enabled.
These are the signals for which _SigPanic is set in sigtable, namely
SIGSEGV, SIGBUS, SIGFPE.
A Go program that uses the os/signal package, and calls signal.Notify,
needs to have at least one thread which is not blocking that signal,
but it doesn't matter much which one.
Unix programs do not change signal mask across execve. They inherit
signal masks across fork. The shell uses this fact to some extent;
for example, the job control signals (SIGTTIN, SIGTTOU, SIGTSTP) are
blocked for commands run due to backquote quoting or $().
Our current position on signal masks was not thought out. We wandered
into step by step, e.g., http://golang.org/cl/7323067 .
This CL does the following:
Introduce a new platform hook, msigsave, that saves the signal mask of
the current thread to m.sigsave.
Call msigsave from needm and newm.
In minit grab set up the signal mask from m.sigsave and unblock the
essential synchronous signals, and SIGILL, SIGTRAP, SIGPROF, SIGSTKFLT
(for systems that have it).
In unminit, restore the signal mask from m.sigsave.
The first time that os/signal.Notify is called, start a new thread whose
only purpose is to update its signal mask to make sure signals for
signal.Notify are unblocked on at least one thread.
The effect on Go programs will be that if they are invoked with some
non-synchronous signals blocked, those signals will normally be
ignored. Previously, those signals would mostly be ignored. A change
in behaviour will occur for programs started with any of these signals
blocked, if they receive the signal: SIGHUP, SIGINT, SIGQUIT, SIGABRT,
SIGTERM. Previously those signals would always cause a crash (unless
using the os/signal package); with this change, they will be ignored
if the program is started with the signal blocked (and does not use
the os/signal package).
./all.bash completes successfully on linux/amd64.
OpenBSD is missing the implementation.
Change-Id: I188098ba7eb85eae4c14861269cc466f2aa40e8c
Reviewed-on: https://go-review.googlesource.com/10173
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-18 03:00:24 -06:00
|
|
|
|
|
|
|
// restore signal mask from m.sigmask and unblock essential signals
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
nmask := _g_.m.sigmask
|
runtime: don't always unblock all signals
Ian proposed an improved way of handling signals masks in Go, motivated
by a problem where the Android java runtime expects certain signals to
be blocked for all JVM threads. Discussion here
https://groups.google.com/forum/#!topic/golang-dev/_TSCkQHJt6g
Ian's text is used in the following:
A Go program always needs to have the synchronous signals enabled.
These are the signals for which _SigPanic is set in sigtable, namely
SIGSEGV, SIGBUS, SIGFPE.
A Go program that uses the os/signal package, and calls signal.Notify,
needs to have at least one thread which is not blocking that signal,
but it doesn't matter much which one.
Unix programs do not change signal mask across execve. They inherit
signal masks across fork. The shell uses this fact to some extent;
for example, the job control signals (SIGTTIN, SIGTTOU, SIGTSTP) are
blocked for commands run due to backquote quoting or $().
Our current position on signal masks was not thought out. We wandered
into step by step, e.g., http://golang.org/cl/7323067 .
This CL does the following:
Introduce a new platform hook, msigsave, that saves the signal mask of
the current thread to m.sigsave.
Call msigsave from needm and newm.
In minit grab set up the signal mask from m.sigsave and unblock the
essential synchronous signals, and SIGILL, SIGTRAP, SIGPROF, SIGSTKFLT
(for systems that have it).
In unminit, restore the signal mask from m.sigsave.
The first time that os/signal.Notify is called, start a new thread whose
only purpose is to update its signal mask to make sure signals for
signal.Notify are unblocked on at least one thread.
The effect on Go programs will be that if they are invoked with some
non-synchronous signals blocked, those signals will normally be
ignored. Previously, those signals would mostly be ignored. A change
in behaviour will occur for programs started with any of these signals
blocked, if they receive the signal: SIGHUP, SIGINT, SIGQUIT, SIGABRT,
SIGTERM. Previously those signals would always cause a crash (unless
using the os/signal package); with this change, they will be ignored
if the program is started with the signal blocked (and does not use
the os/signal package).
./all.bash completes successfully on linux/amd64.
OpenBSD is missing the implementation.
Change-Id: I188098ba7eb85eae4c14861269cc466f2aa40e8c
Reviewed-on: https://go-review.googlesource.com/10173
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-18 03:00:24 -06:00
|
|
|
for i := range sigtable {
|
|
|
|
if sigtable[i].flags&_SigUnblock != 0 {
|
|
|
|
nmask &^= 1 << (uint32(i) - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sigprocmask(_SIG_SETMASK, &nmask, nil)
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from dropm to undo the effect of an minit.
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
2014-11-11 15:08:54 -07:00
|
|
|
func unminit() {
|
2015-12-23 19:38:18 -07:00
|
|
|
if getg().m.newSigstack {
|
|
|
|
signalstack(nil)
|
|
|
|
}
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mach IPC, to get at semaphores
|
|
|
|
// Definitions are in /usr/include/mach on a Mac.
|
|
|
|
|
|
|
|
func macherror(r int32, fn string) {
|
|
|
|
print("mach error ", fn, ": ", r, "\n")
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("mach error")
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const _DebugMach = false
|
|
|
|
|
|
|
|
var zerondr machndr
|
|
|
|
|
|
|
|
func mach_msgh_bits(a, b uint32) uint32 {
|
|
|
|
return a | b<<8
|
|
|
|
}
|
|
|
|
|
|
|
|
func mach_msg(h *machheader, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32 {
|
|
|
|
// TODO: Loop on interrupt.
|
|
|
|
return mach_msg_trap(unsafe.Pointer(h), op, send_size, rcv_size, rcv_name, timeout, notify)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mach RPC (MIG)
|
|
|
|
const (
|
|
|
|
_MinMachMsg = 48
|
|
|
|
_MachReply = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
type codemsg struct {
|
|
|
|
h machheader
|
|
|
|
ndr machndr
|
|
|
|
code int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func machcall(h *machheader, maxsize int32, rxsize int32) int32 {
|
|
|
|
_g_ := getg()
|
|
|
|
port := _g_.m.machport
|
|
|
|
if port == 0 {
|
|
|
|
port = mach_reply_port()
|
|
|
|
_g_.m.machport = port
|
|
|
|
}
|
|
|
|
|
|
|
|
h.msgh_bits |= mach_msgh_bits(_MACH_MSG_TYPE_COPY_SEND, _MACH_MSG_TYPE_MAKE_SEND_ONCE)
|
|
|
|
h.msgh_local_port = port
|
|
|
|
h.msgh_reserved = 0
|
|
|
|
id := h.msgh_id
|
|
|
|
|
|
|
|
if _DebugMach {
|
|
|
|
p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h))
|
|
|
|
print("send:\t")
|
|
|
|
var i uint32
|
|
|
|
for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ {
|
|
|
|
print(" ", p[i])
|
|
|
|
if i%8 == 7 {
|
|
|
|
print("\n\t")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i%8 != 0 {
|
|
|
|
print("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret := mach_msg(h, _MACH_SEND_MSG|_MACH_RCV_MSG, h.msgh_size, uint32(maxsize), port, 0, 0)
|
|
|
|
if ret != 0 {
|
|
|
|
if _DebugMach {
|
|
|
|
print("mach_msg error ", ret, "\n")
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
if _DebugMach {
|
|
|
|
p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h))
|
|
|
|
var i uint32
|
|
|
|
for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ {
|
|
|
|
print(" ", p[i])
|
|
|
|
if i%8 == 7 {
|
|
|
|
print("\n\t")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i%8 != 0 {
|
|
|
|
print("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if h.msgh_id != id+_MachReply {
|
|
|
|
if _DebugMach {
|
|
|
|
print("mach_msg _MachReply id mismatch ", h.msgh_id, " != ", id+_MachReply, "\n")
|
|
|
|
}
|
|
|
|
return -303 // MIG_REPLY_MISMATCH
|
|
|
|
}
|
|
|
|
// Look for a response giving the return value.
|
|
|
|
// Any call can send this back with an error,
|
|
|
|
// and some calls only have return values so they
|
|
|
|
// send it back on success too. I don't quite see how
|
|
|
|
// you know it's one of these and not the full response
|
|
|
|
// format, so just look if the message is right.
|
|
|
|
c := (*codemsg)(unsafe.Pointer(h))
|
|
|
|
if uintptr(h.msgh_size) == unsafe.Sizeof(*c) && h.msgh_bits&_MACH_MSGH_BITS_COMPLEX == 0 {
|
|
|
|
if _DebugMach {
|
|
|
|
print("mig result ", c.code, "\n")
|
|
|
|
}
|
|
|
|
return c.code
|
|
|
|
}
|
|
|
|
if h.msgh_size != uint32(rxsize) {
|
|
|
|
if _DebugMach {
|
|
|
|
print("mach_msg _MachReply size mismatch ", h.msgh_size, " != ", rxsize, "\n")
|
|
|
|
}
|
|
|
|
return -307 // MIG_ARRAY_TOO_LARGE
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Semaphores!
|
|
|
|
|
|
|
|
const (
|
|
|
|
tmach_semcreate = 3418
|
|
|
|
rmach_semcreate = tmach_semcreate + _MachReply
|
|
|
|
|
|
|
|
tmach_semdestroy = 3419
|
|
|
|
rmach_semdestroy = tmach_semdestroy + _MachReply
|
|
|
|
|
|
|
|
_KERN_ABORTED = 14
|
|
|
|
_KERN_OPERATION_TIMED_OUT = 49
|
|
|
|
)
|
|
|
|
|
|
|
|
type tmach_semcreatemsg struct {
|
|
|
|
h machheader
|
|
|
|
ndr machndr
|
|
|
|
policy int32
|
|
|
|
value int32
|
|
|
|
}
|
|
|
|
|
|
|
|
type rmach_semcreatemsg struct {
|
|
|
|
h machheader
|
|
|
|
body machbody
|
|
|
|
semaphore machport
|
|
|
|
}
|
|
|
|
|
|
|
|
type tmach_semdestroymsg struct {
|
|
|
|
h machheader
|
|
|
|
body machbody
|
|
|
|
semaphore machport
|
|
|
|
}
|
|
|
|
|
|
|
|
func mach_semcreate() uint32 {
|
|
|
|
var m [256]uint8
|
|
|
|
tx := (*tmach_semcreatemsg)(unsafe.Pointer(&m))
|
|
|
|
rx := (*rmach_semcreatemsg)(unsafe.Pointer(&m))
|
|
|
|
|
|
|
|
tx.h.msgh_bits = 0
|
|
|
|
tx.h.msgh_size = uint32(unsafe.Sizeof(*tx))
|
|
|
|
tx.h.msgh_remote_port = mach_task_self()
|
|
|
|
tx.h.msgh_id = tmach_semcreate
|
|
|
|
tx.ndr = zerondr
|
|
|
|
|
|
|
|
tx.policy = 0 // 0 = SYNC_POLICY_FIFO
|
|
|
|
tx.value = 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
r := machcall(&tx.h, int32(unsafe.Sizeof(m)), int32(unsafe.Sizeof(*rx)))
|
|
|
|
if r == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if r == _KERN_ABORTED { // interrupted
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
macherror(r, "semaphore_create")
|
|
|
|
}
|
|
|
|
if rx.body.msgh_descriptor_count != 1 {
|
|
|
|
unimplemented("mach_semcreate desc count")
|
|
|
|
}
|
|
|
|
return rx.semaphore.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func mach_semdestroy(sem uint32) {
|
|
|
|
var m [256]uint8
|
|
|
|
tx := (*tmach_semdestroymsg)(unsafe.Pointer(&m))
|
|
|
|
|
|
|
|
tx.h.msgh_bits = _MACH_MSGH_BITS_COMPLEX
|
|
|
|
tx.h.msgh_size = uint32(unsafe.Sizeof(*tx))
|
|
|
|
tx.h.msgh_remote_port = mach_task_self()
|
|
|
|
tx.h.msgh_id = tmach_semdestroy
|
|
|
|
tx.body.msgh_descriptor_count = 1
|
|
|
|
tx.semaphore.name = sem
|
|
|
|
tx.semaphore.disposition = _MACH_MSG_TYPE_MOVE_SEND
|
|
|
|
tx.semaphore._type = 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
r := machcall(&tx.h, int32(unsafe.Sizeof(m)), 0)
|
|
|
|
if r == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if r == _KERN_ABORTED { // interrupted
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
macherror(r, "semaphore_destroy")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The other calls have simple system call traps in sys_darwin_{amd64,386}.s
|
|
|
|
|
|
|
|
func mach_semaphore_wait(sema uint32) int32
|
|
|
|
func mach_semaphore_timedwait(sema, sec, nsec uint32) int32
|
|
|
|
func mach_semaphore_signal(sema uint32) int32
|
|
|
|
func mach_semaphore_signal_all(sema uint32) int32
|
|
|
|
|
|
|
|
func semasleep1(ns int64) int32 {
|
|
|
|
_g_ := getg()
|
|
|
|
|
|
|
|
if ns >= 0 {
|
|
|
|
var nsecs int32
|
|
|
|
secs := timediv(ns, 1000000000, &nsecs)
|
2015-10-21 19:36:05 -06:00
|
|
|
r := mach_semaphore_timedwait(_g_.m.waitsema, uint32(secs), uint32(nsecs))
|
2014-11-11 15:08:54 -07:00
|
|
|
if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if r != 0 {
|
|
|
|
macherror(r, "semaphore_wait")
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2015-10-21 19:36:05 -06:00
|
|
|
r := mach_semaphore_wait(_g_.m.waitsema)
|
2014-11-11 15:08:54 -07:00
|
|
|
if r == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if r == _KERN_ABORTED { // interrupted
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
macherror(r, "semaphore_wait")
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func semasleep(ns int64) int32 {
|
|
|
|
var r int32
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(func() {
|
2014-11-11 15:08:54 -07:00
|
|
|
r = semasleep1(ns)
|
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func mach_semrelease(sem uint32) {
|
|
|
|
for {
|
|
|
|
r := mach_semaphore_signal(sem)
|
|
|
|
if r == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if r == _KERN_ABORTED { // interrupted
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// mach_semrelease must be completely nosplit,
|
|
|
|
// because it is called from Go code.
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
// If we're going to die, start that process on the system stack
|
2014-11-11 15:08:54 -07:00
|
|
|
// to avoid a Go stack split.
|
[dev.cc] runtime: delete scalararg, ptrarg; rename onM to systemstack
Scalararg and ptrarg are not "signal safe".
Go code filling them out can be interrupted by a signal,
and then the signal handler runs, and if it also ends up
in Go code that uses scalararg or ptrarg, now the old
values have been smashed.
For the pieces of code that do need to run in a signal handler,
we introduced onM_signalok, which is really just onM
except that the _signalok is meant to convey that the caller
asserts that scalarg and ptrarg will be restored to their old
values after the call (instead of the usual behavior, zeroing them).
Scalararg and ptrarg are also untyped and therefore error-prone.
Go code can always pass a closure instead of using scalararg
and ptrarg; they were only really necessary for C code.
And there's no more C code.
For all these reasons, delete scalararg and ptrarg, converting
the few remaining references to use closures.
Once those are gone, there is no need for a distinction between
onM and onM_signalok, so replace both with a single function
equivalent to the current onM_signalok (that is, it can be called
on any of the curg, g0, and gsignal stacks).
The name onM and the phrase 'm stack' are misnomers,
because on most system an M has two system stacks:
the main thread stack and the signal handling stack.
Correct the misnomer by naming the replacement function systemstack.
Fix a few references to "M stack" in code.
The main motivation for this change is to eliminate scalararg/ptrarg.
Rick and I have already seen them cause problems because
the calling sequence m.ptrarg[0] = p is a heap pointer assignment,
so it gets a write barrier. The write barrier also uses onM, so it has
all the same problems as if it were being invoked by a signal handler.
We worked around this by saving and restoring the old values
and by calling onM_signalok, but there's no point in keeping this nice
home for bugs around any longer.
This CL also changes funcline to return the file name as a result
instead of filling in a passed-in *string. (The *string signature is
left over from when the code was written in and called from C.)
That's arguably an unrelated change, except that once I had done
the ptrarg/scalararg/onM cleanup I started getting false positives
about the *string argument escaping (not allowed in package runtime).
The compiler is wrong, but the easiest fix is to write the code like
Go code instead of like C code. I am a bit worried that the compiler
is wrong because of some use of uninitialized memory in the escape
analysis. If that's the reason, it will go away when we convert the
compiler to Go. (And if not, we'll debug it the next time.)
LGTM=khr
R=r, khr
CC=austin, golang-codereviews, iant, rlh
https://golang.org/cl/174950043
2014-11-12 12:54:31 -07:00
|
|
|
systemstack(func() { macherror(r, "semaphore_signal") })
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
func osyield() {
|
|
|
|
usleep(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func memlimit() uintptr {
|
|
|
|
// NOTE(rsc): Could use getrlimit here,
|
|
|
|
// like on FreeBSD or Linux, but Darwin doesn't enforce
|
|
|
|
// ulimit -v, so it's unclear why we'd try to stay within
|
|
|
|
// the limit.
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func setsig(i int32, fn uintptr, restart bool) {
|
|
|
|
var sa sigactiont
|
|
|
|
sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK
|
|
|
|
if restart {
|
|
|
|
sa.sa_flags |= _SA_RESTART
|
|
|
|
}
|
|
|
|
sa.sa_mask = ^uint32(0)
|
|
|
|
sa.sa_tramp = unsafe.Pointer(funcPC(sigtramp)) // runtime·sigtramp's job is to call into real handler
|
|
|
|
*(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = fn
|
|
|
|
sigaction(uint32(i), &sa, nil)
|
|
|
|
}
|
|
|
|
|
2014-12-19 14:16:17 -07:00
|
|
|
func setsigstack(i int32) {
|
2015-12-17 15:49:34 -07:00
|
|
|
var osa usigactiont
|
|
|
|
sigaction(uint32(i), nil, &osa)
|
|
|
|
handler := *(*uintptr)(unsafe.Pointer(&osa.__sigaction_u))
|
|
|
|
if handler == 0 || handler == _SIG_DFL || handler == _SIG_IGN || osa.sa_flags&_SA_ONSTACK != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var sa sigactiont
|
|
|
|
*(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = handler
|
|
|
|
sa.sa_tramp = unsafe.Pointer(funcPC(sigtramp))
|
|
|
|
sa.sa_mask = osa.sa_mask
|
|
|
|
sa.sa_flags = osa.sa_flags | _SA_ONSTACK
|
|
|
|
sigaction(uint32(i), &sa, nil)
|
2014-12-19 14:16:17 -07:00
|
|
|
}
|
|
|
|
|
2014-11-11 15:08:54 -07:00
|
|
|
func getsig(i int32) uintptr {
|
2015-12-17 15:49:34 -07:00
|
|
|
var sa usigactiont
|
2014-11-11 15:08:54 -07:00
|
|
|
sigaction(uint32(i), nil, &sa)
|
|
|
|
return *(*uintptr)(unsafe.Pointer(&sa.__sigaction_u))
|
|
|
|
}
|
|
|
|
|
2015-11-13 14:21:01 -07:00
|
|
|
//go:nosplit
|
2015-05-21 12:12:29 -06:00
|
|
|
func signalstack(s *stack) {
|
2014-11-11 15:08:54 -07:00
|
|
|
var st stackt
|
2015-05-21 12:12:29 -06:00
|
|
|
if s == nil {
|
2014-11-11 15:08:54 -07:00
|
|
|
st.ss_flags = _SS_DISABLE
|
2015-05-21 12:12:29 -06:00
|
|
|
} else {
|
|
|
|
st.ss_sp = (*byte)(unsafe.Pointer(s.lo))
|
|
|
|
st.ss_size = s.hi - s.lo
|
|
|
|
st.ss_flags = 0
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
|
|
|
sigaltstack(&st, nil)
|
|
|
|
}
|
|
|
|
|
runtime: don't always unblock all signals
Ian proposed an improved way of handling signals masks in Go, motivated
by a problem where the Android java runtime expects certain signals to
be blocked for all JVM threads. Discussion here
https://groups.google.com/forum/#!topic/golang-dev/_TSCkQHJt6g
Ian's text is used in the following:
A Go program always needs to have the synchronous signals enabled.
These are the signals for which _SigPanic is set in sigtable, namely
SIGSEGV, SIGBUS, SIGFPE.
A Go program that uses the os/signal package, and calls signal.Notify,
needs to have at least one thread which is not blocking that signal,
but it doesn't matter much which one.
Unix programs do not change signal mask across execve. They inherit
signal masks across fork. The shell uses this fact to some extent;
for example, the job control signals (SIGTTIN, SIGTTOU, SIGTSTP) are
blocked for commands run due to backquote quoting or $().
Our current position on signal masks was not thought out. We wandered
into step by step, e.g., http://golang.org/cl/7323067 .
This CL does the following:
Introduce a new platform hook, msigsave, that saves the signal mask of
the current thread to m.sigsave.
Call msigsave from needm and newm.
In minit grab set up the signal mask from m.sigsave and unblock the
essential synchronous signals, and SIGILL, SIGTRAP, SIGPROF, SIGSTKFLT
(for systems that have it).
In unminit, restore the signal mask from m.sigsave.
The first time that os/signal.Notify is called, start a new thread whose
only purpose is to update its signal mask to make sure signals for
signal.Notify are unblocked on at least one thread.
The effect on Go programs will be that if they are invoked with some
non-synchronous signals blocked, those signals will normally be
ignored. Previously, those signals would mostly be ignored. A change
in behaviour will occur for programs started with any of these signals
blocked, if they receive the signal: SIGHUP, SIGINT, SIGQUIT, SIGABRT,
SIGTERM. Previously those signals would always cause a crash (unless
using the os/signal package); with this change, they will be ignored
if the program is started with the signal blocked (and does not use
the os/signal package).
./all.bash completes successfully on linux/amd64.
OpenBSD is missing the implementation.
Change-Id: I188098ba7eb85eae4c14861269cc466f2aa40e8c
Reviewed-on: https://go-review.googlesource.com/10173
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-05-18 03:00:24 -06:00
|
|
|
func updatesigmask(m sigmask) {
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
s := sigset(m[0])
|
|
|
|
sigprocmask(_SIG_SETMASK, &s, nil)
|
2014-11-11 15:08:54 -07:00
|
|
|
}
|
2015-07-21 23:34:48 -06:00
|
|
|
|
|
|
|
func unblocksig(sig int32) {
|
runtime: use a proper type, sigset, for m.sigmask
Replace the cross platform but unsafe [4]uintptr type with a OS
specific type, sigset. Most OSes already define sigset, and this
change defines a suitable sigset for the OSes that don't (darwin,
openbsd). The OSes that don't use m.sigmask (windows, plan9, nacl)
now defines sigset as the empty type, struct{}.
The gain is strongly typed access to m.sigmask, saving a dynamic
size sanity check and unsafe.Pointer casting. Also, some storage is
saved for each M, since [4]uinptr was conservative for most OSes.
The cost is that OSes that don't need m.sigmask has to define sigset.
completes ./all.bash with GOOS linux, on amd64
completes ./make.bash with GOOSes openbsd, android, plan9, windows,
darwin, solaris, netbsd, freebsd, dragonfly, all amd64.
With GOOS=nacl ./make.bash failed with a seemingly unrelated error.
[Replay of CL 16942 by Elias Naur.]
Change-Id: I98f144d626033ae5318576115ed635415ac71b2c
Reviewed-on: https://go-review.googlesource.com/17033
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
2015-11-17 03:41:06 -07:00
|
|
|
mask := sigset(1) << (uint32(sig) - 1)
|
2015-07-21 23:34:48 -06:00
|
|
|
sigprocmask(_SIG_UNBLOCK, &mask, nil)
|
|
|
|
}
|