2014-08-24 11:50:37 +04: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.
|
|
|
|
|
|
|
|
|
|
// This file implements runtime support for signal handling.
|
2014-09-04 13:51:12 -04:00
|
|
|
//
|
|
|
|
|
// Most synchronization primitives are not available from
|
|
|
|
|
// the signal handler (it cannot block, allocate memory, or use locks)
|
|
|
|
|
// so the handler communicates with a processing goroutine
|
|
|
|
|
// via struct sig, below.
|
|
|
|
|
//
|
2014-09-05 14:38:29 -04:00
|
|
|
// sigsend is called by the signal handler to queue a new signal.
|
|
|
|
|
// signal_recv is called by the Go program to receive a newly queued signal.
|
|
|
|
|
// Synchronization between sigsend and signal_recv is based on the sig.state
|
2016-03-01 23:21:55 +00:00
|
|
|
// variable. It can be in 3 states: sigIdle, sigReceiving and sigSending.
|
2014-09-05 14:38:29 -04:00
|
|
|
// sigReceiving means that signal_recv is blocked on sig.Note and there are no
|
2014-09-04 13:51:12 -04:00
|
|
|
// new pending signals.
|
2014-09-05 14:38:29 -04:00
|
|
|
// sigSending means that sig.mask *may* contain new pending signals,
|
|
|
|
|
// signal_recv can't be blocked in this state.
|
|
|
|
|
// sigIdle means that there are no new pending signals and signal_recv is not blocked.
|
2014-09-04 13:51:12 -04:00
|
|
|
// Transitions between states are done atomically with CAS.
|
2014-09-05 14:38:29 -04:00
|
|
|
// When signal_recv is unblocked, it resets sig.Note and rechecks sig.mask.
|
|
|
|
|
// If several sigsends and signal_recv execute concurrently, it can lead to
|
|
|
|
|
// unnecessary rechecks of sig.mask, but it cannot lead to missed signals
|
2014-09-04 13:51:12 -04:00
|
|
|
// nor deadlocks.
|
2014-08-24 11:50:37 +04:00
|
|
|
|
2015-01-22 23:38:29 +01:00
|
|
|
// +build !plan9
|
|
|
|
|
|
2014-08-24 11:50:37 +04:00
|
|
|
package runtime
|
|
|
|
|
|
2015-11-02 14:09:24 -05:00
|
|
|
import (
|
|
|
|
|
"runtime/internal/atomic"
|
2016-03-22 22:11:42 -05:00
|
|
|
_ "unsafe" // for go:linkname
|
2015-11-02 14:09:24 -05:00
|
|
|
)
|
2014-09-04 13:51:12 -04:00
|
|
|
|
|
|
|
|
var sig struct {
|
2016-01-07 10:19:55 -05:00
|
|
|
note note
|
|
|
|
|
mask [(_NSIG + 31) / 32]uint32
|
|
|
|
|
wanted [(_NSIG + 31) / 32]uint32
|
|
|
|
|
ignored [(_NSIG + 31) / 32]uint32
|
|
|
|
|
recv [(_NSIG + 31) / 32]uint32
|
|
|
|
|
state uint32
|
|
|
|
|
inuse bool
|
2014-09-04 13:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
2014-09-05 14:38:29 -04:00
|
|
|
sigIdle = iota
|
|
|
|
|
sigReceiving
|
|
|
|
|
sigSending
|
2014-09-04 13:51:12 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Called from sighandler to send a signal back out of the signal handling thread.
|
2014-09-05 14:38:29 -04:00
|
|
|
// Reports whether the signal was sent. If not, the caller typically crashes the program.
|
2014-11-11 17:05:55 -05:00
|
|
|
func sigsend(s uint32) bool {
|
2014-09-04 13:51:12 -04:00
|
|
|
bit := uint32(1) << uint(s&31)
|
2015-08-25 18:25:42 -05:00
|
|
|
if !sig.inuse || s >= uint32(32*len(sig.wanted)) || sig.wanted[s/32]&bit == 0 {
|
2014-09-04 13:51:12 -04:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-05 14:38:29 -04:00
|
|
|
// Add signal to outgoing queue.
|
2014-09-04 13:51:12 -04:00
|
|
|
for {
|
|
|
|
|
mask := sig.mask[s/32]
|
|
|
|
|
if mask&bit != 0 {
|
2014-09-05 14:38:29 -04:00
|
|
|
return true // signal already in queue
|
2014-09-04 13:51:12 -04:00
|
|
|
}
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Cas(&sig.mask[s/32], mask, mask|bit) {
|
2014-09-05 14:38:29 -04:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-04 13:51:12 -04:00
|
|
|
|
2014-09-05 14:38:29 -04:00
|
|
|
// Notify receiver that queue has new bit.
|
|
|
|
|
Send:
|
|
|
|
|
for {
|
2015-11-02 14:09:24 -05:00
|
|
|
switch atomic.Load(&sig.state) {
|
2014-09-05 14:38:29 -04:00
|
|
|
default:
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("sigsend: inconsistent state")
|
2014-09-05 14:38:29 -04:00
|
|
|
case sigIdle:
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Cas(&sig.state, sigIdle, sigSending) {
|
2014-09-05 14:38:29 -04:00
|
|
|
break Send
|
|
|
|
|
}
|
|
|
|
|
case sigSending:
|
|
|
|
|
// notification already pending
|
|
|
|
|
break Send
|
|
|
|
|
case sigReceiving:
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Cas(&sig.state, sigReceiving, sigIdle) {
|
2014-09-05 14:38:29 -04:00
|
|
|
notewakeup(&sig.note)
|
|
|
|
|
break Send
|
2014-09-04 13:51:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-05 14:38:29 -04:00
|
|
|
|
2014-09-04 13:51:12 -04:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called to receive the next queued signal.
|
|
|
|
|
// Must only be called from a single goroutine at a time.
|
2015-10-16 09:56:07 +13:00
|
|
|
//go:linkname signal_recv os/signal.signal_recv
|
2014-09-04 13:51:12 -04:00
|
|
|
func signal_recv() uint32 {
|
2014-08-24 11:50:37 +04:00
|
|
|
for {
|
2014-09-05 14:38:29 -04:00
|
|
|
// Serve any signals from local copy.
|
2014-09-04 13:51:12 -04:00
|
|
|
for i := uint32(0); i < _NSIG; i++ {
|
|
|
|
|
if sig.recv[i/32]&(1<<(i&31)) != 0 {
|
|
|
|
|
sig.recv[i/32] &^= 1 << (i & 31)
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-05 14:38:29 -04:00
|
|
|
// Wait for updates to be available from signal sender.
|
|
|
|
|
Receive:
|
2014-09-04 13:51:12 -04:00
|
|
|
for {
|
2015-11-02 14:09:24 -05:00
|
|
|
switch atomic.Load(&sig.state) {
|
2014-09-05 14:38:29 -04:00
|
|
|
default:
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("signal_recv: inconsistent state")
|
2014-09-05 14:38:29 -04:00
|
|
|
case sigIdle:
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Cas(&sig.state, sigIdle, sigReceiving) {
|
2014-09-04 13:51:12 -04:00
|
|
|
notetsleepg(&sig.note, -1)
|
|
|
|
|
noteclear(&sig.note)
|
2014-09-05 14:38:29 -04:00
|
|
|
break Receive
|
|
|
|
|
}
|
|
|
|
|
case sigSending:
|
2015-11-02 14:09:24 -05:00
|
|
|
if atomic.Cas(&sig.state, sigSending, sigIdle) {
|
2014-09-05 14:38:29 -04:00
|
|
|
break Receive
|
2014-09-04 13:51:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-05 14:38:29 -04:00
|
|
|
// Incorporate updates from sender into local copy.
|
2014-09-04 13:51:12 -04:00
|
|
|
for i := range sig.mask {
|
2015-11-02 14:09:24 -05:00
|
|
|
sig.recv[i] = atomic.Xchg(&sig.mask[i], 0)
|
2014-08-24 11:50:37 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 13:51:12 -04:00
|
|
|
// Must only be called from a single goroutine at a time.
|
2015-10-16 09:56:07 +13:00
|
|
|
//go:linkname signal_enable os/signal.signal_enable
|
2014-08-24 11:50:37 +04:00
|
|
|
func signal_enable(s uint32) {
|
2014-09-04 13:51:12 -04:00
|
|
|
if !sig.inuse {
|
|
|
|
|
// The first call to signal_enable is for us
|
2016-03-01 23:21:55 +00:00
|
|
|
// to use for initialization. It does not pass
|
2014-09-04 13:51:12 -04:00
|
|
|
// signal information in m.
|
|
|
|
|
sig.inuse = true // enable reception of signals; cannot disable
|
|
|
|
|
noteclear(&sig.note)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-25 18:25:42 -05:00
|
|
|
if s >= uint32(len(sig.wanted)*32) {
|
2014-09-04 13:51:12 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sig.wanted[s/32] |= 1 << (s & 31)
|
2016-01-07 10:19:55 -05:00
|
|
|
sig.ignored[s/32] &^= 1 << (s & 31)
|
[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 14:54:31 -05:00
|
|
|
sigenable(s)
|
2014-08-24 11:50:37 +04:00
|
|
|
}
|
|
|
|
|
|
2014-09-04 13:51:12 -04:00
|
|
|
// Must only be called from a single goroutine at a time.
|
2015-10-16 09:56:07 +13:00
|
|
|
//go:linkname signal_disable os/signal.signal_disable
|
2014-08-24 11:50:37 +04:00
|
|
|
func signal_disable(s uint32) {
|
2015-08-25 18:25:42 -05:00
|
|
|
if s >= uint32(len(sig.wanted)*32) {
|
2014-09-04 13:51:12 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sig.wanted[s/32] &^= 1 << (s & 31)
|
[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 14:54:31 -05:00
|
|
|
sigdisable(s)
|
2014-09-04 13:51:12 -04:00
|
|
|
}
|
|
|
|
|
|
2015-01-29 22:37:41 -05:00
|
|
|
// Must only be called from a single goroutine at a time.
|
2015-10-16 09:56:07 +13:00
|
|
|
//go:linkname signal_ignore os/signal.signal_ignore
|
2015-01-29 22:37:41 -05:00
|
|
|
func signal_ignore(s uint32) {
|
2015-08-25 18:25:42 -05:00
|
|
|
if s >= uint32(len(sig.wanted)*32) {
|
2015-01-29 22:37:41 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sig.wanted[s/32] &^= 1 << (s & 31)
|
2016-01-07 10:19:55 -05:00
|
|
|
sig.ignored[s/32] |= 1 << (s & 31)
|
2015-01-29 22:37:41 -05:00
|
|
|
sigignore(s)
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 10:19:55 -05:00
|
|
|
// Checked by signal handlers.
|
|
|
|
|
func signal_ignored(s uint32) bool {
|
|
|
|
|
return sig.ignored[s/32]&(1<<(s&31)) != 0
|
|
|
|
|
}
|