1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // This file implements runtime support for signal handling. 6 // 7 // Most synchronization primitives are not available from 8 // the signal handler (it cannot block, allocate memory, or use locks) 9 // so the handler communicates with a processing goroutine 10 // via struct sig, below. 11 // 12 // sigsend is called by the signal handler to queue a new signal. 13 // signal_recv is called by the Go program to receive a newly queued signal. 14 // Synchronization between sigsend and signal_recv is based on the sig.state 15 // variable. It can be in 4 states: sigIdle, sigReceiving, sigSending and sigFixup. 16 // sigReceiving means that signal_recv is blocked on sig.Note and there are no 17 // new pending signals. 18 // sigSending means that sig.mask *may* contain new pending signals, 19 // signal_recv can't be blocked in this state. 20 // sigIdle means that there are no new pending signals and signal_recv is not blocked. 21 // sigFixup is a transient state that can only exist as a short 22 // transition from sigReceiving and then on to sigIdle: it is 23 // used to ensure the AllThreadsSyscall()'s mDoFixup() operation 24 // occurs on the sleeping m, waiting to receive a signal. 25 // Transitions between states are done atomically with CAS. 26 // When signal_recv is unblocked, it resets sig.Note and rechecks sig.mask. 27 // If several sigsends and signal_recv execute concurrently, it can lead to 28 // unnecessary rechecks of sig.mask, but it cannot lead to missed signals 29 // nor deadlocks. 30 31 //go:build !plan9 32 // +build !plan9 33 34 package runtime 35 36 import ( 37 "runtime/internal/atomic" 38 _ "unsafe" // for go:linkname 39 ) 40 41 // sig handles communication between the signal handler and os/signal. 42 // Other than the inuse and recv fields, the fields are accessed atomically. 43 // 44 // The wanted and ignored fields are only written by one goroutine at 45 // a time; access is controlled by the handlers Mutex in os/signal. 46 // The fields are only read by that one goroutine and by the signal handler. 47 // We access them atomically to minimize the race between setting them 48 // in the goroutine calling os/signal and the signal handler, 49 // which may be running in a different thread. That race is unavoidable, 50 // as there is no connection between handling a signal and receiving one, 51 // but atomic instructions should minimize it. 52 var sig struct { 53 note note 54 mask [(_NSIG + 31) / 32]uint32 55 wanted [(_NSIG + 31) / 32]uint32 56 ignored [(_NSIG + 31) / 32]uint32 57 recv [(_NSIG + 31) / 32]uint32 58 state uint32 59 delivering uint32 60 inuse bool 61 } 62 63 const ( 64 sigIdle = iota 65 sigReceiving 66 sigSending 67 sigFixup 68 ) 69 70 // sigsend delivers a signal from sighandler to the internal signal delivery queue. 71 // It reports whether the signal was sent. If not, the caller typically crashes the program. 72 // It runs from the signal handler, so it's limited in what it can do. 73 func sigsend(s uint32) bool { 74 bit := uint32(1) << uint(s&31) 75 if s >= uint32(32*len(sig.wanted)) { 76 return false 77 } 78 79 atomic.Xadd(&sig.delivering, 1) 80 // We are running in the signal handler; defer is not available. 81 82 if w := atomic.Load(&sig.wanted[s/32]); w&bit == 0 { 83 atomic.Xadd(&sig.delivering, -1) 84 return false 85 } 86 87 // Add signal to outgoing queue. 88 for { 89 mask := sig.mask[s/32] 90 if mask&bit != 0 { 91 atomic.Xadd(&sig.delivering, -1) 92 return true // signal already in queue 93 } 94 if atomic.Cas(&sig.mask[s/32], mask, mask|bit) { 95 break 96 } 97 } 98 99 // Notify receiver that queue has new bit. 100 Send: 101 for { 102 switch atomic.Load(&sig.state) { 103 default: 104 throw("sigsend: inconsistent state") 105 case sigIdle: 106 if atomic.Cas(&sig.state, sigIdle, sigSending) { 107 break Send 108 } 109 case sigSending: 110 // notification already pending 111 break Send 112 case sigReceiving: 113 if atomic.Cas(&sig.state, sigReceiving, sigIdle) { 114 if GOOS == "darwin" || GOOS == "ios" { 115 sigNoteWakeup(&sig.note) 116 break Send 117 } 118 notewakeup(&sig.note) 119 break Send 120 } 121 case sigFixup: 122 // nothing to do - we need to wait for sigIdle. 123 mDoFixupAndOSYield() 124 } 125 } 126 127 atomic.Xadd(&sig.delivering, -1) 128 return true 129 } 130 131 // sigRecvPrepareForFixup is used to temporarily wake up the 132 // signal_recv() running thread while it is blocked waiting for the 133 // arrival of a signal. If it causes the thread to wake up, the 134 // sig.state travels through this sequence: sigReceiving -> sigFixup 135 // -> sigIdle -> sigReceiving and resumes. (This is only called while 136 // GC is disabled.) 137 //go:nosplit 138 func sigRecvPrepareForFixup() { 139 if atomic.Cas(&sig.state, sigReceiving, sigFixup) { 140 notewakeup(&sig.note) 141 } 142 } 143 144 // Called to receive the next queued signal. 145 // Must only be called from a single goroutine at a time. 146 //go:linkname signal_recv os/signal.signal_recv 147 func signal_recv() uint32 { 148 for { 149 // Serve any signals from local copy. 150 for i := uint32(0); i < _NSIG; i++ { 151 if sig.recv[i/32]&(1<<(i&31)) != 0 { 152 sig.recv[i/32] &^= 1 << (i & 31) 153 return i 154 } 155 } 156 157 // Wait for updates to be available from signal sender. 158 Receive: 159 for { 160 switch atomic.Load(&sig.state) { 161 default: 162 throw("signal_recv: inconsistent state") 163 case sigIdle: 164 if atomic.Cas(&sig.state, sigIdle, sigReceiving) { 165 if GOOS == "darwin" || GOOS == "ios" { 166 sigNoteSleep(&sig.note) 167 break Receive 168 } 169 notetsleepg(&sig.note, -1) 170 noteclear(&sig.note) 171 if !atomic.Cas(&sig.state, sigFixup, sigIdle) { 172 break Receive 173 } 174 // Getting here, the code will 175 // loop around again to sleep 176 // in state sigReceiving. This 177 // path is taken when 178 // sigRecvPrepareForFixup() 179 // has been called by another 180 // thread. 181 } 182 case sigSending: 183 if atomic.Cas(&sig.state, sigSending, sigIdle) { 184 break Receive 185 } 186 } 187 } 188 189 // Incorporate updates from sender into local copy. 190 for i := range sig.mask { 191 sig.recv[i] = atomic.Xchg(&sig.mask[i], 0) 192 } 193 } 194 } 195 196 // signalWaitUntilIdle waits until the signal delivery mechanism is idle. 197 // This is used to ensure that we do not drop a signal notification due 198 // to a race between disabling a signal and receiving a signal. 199 // This assumes that signal delivery has already been disabled for 200 // the signal(s) in question, and here we are just waiting to make sure 201 // that all the signals have been delivered to the user channels 202 // by the os/signal package. 203 //go:linkname signalWaitUntilIdle os/signal.signalWaitUntilIdle 204 func signalWaitUntilIdle() { 205 // Although the signals we care about have been removed from 206 // sig.wanted, it is possible that another thread has received 207 // a signal, has read from sig.wanted, is now updating sig.mask, 208 // and has not yet woken up the processor thread. We need to wait 209 // until all current signal deliveries have completed. 210 for atomic.Load(&sig.delivering) != 0 { 211 Gosched() 212 } 213 214 // Although WaitUntilIdle seems like the right name for this 215 // function, the state we are looking for is sigReceiving, not 216 // sigIdle. The sigIdle state is really more like sigProcessing. 217 for atomic.Load(&sig.state) != sigReceiving { 218 Gosched() 219 } 220 } 221 222 // Must only be called from a single goroutine at a time. 223 //go:linkname signal_enable os/signal.signal_enable 224 func signal_enable(s uint32) { 225 if !sig.inuse { 226 // This is the first call to signal_enable. Initialize. 227 sig.inuse = true // enable reception of signals; cannot disable 228 if GOOS == "darwin" || GOOS == "ios" { 229 sigNoteSetup(&sig.note) 230 } else { 231 noteclear(&sig.note) 232 } 233 } 234 235 if s >= uint32(len(sig.wanted)*32) { 236 return 237 } 238 239 w := sig.wanted[s/32] 240 w |= 1 << (s & 31) 241 atomic.Store(&sig.wanted[s/32], w) 242 243 i := sig.ignored[s/32] 244 i &^= 1 << (s & 31) 245 atomic.Store(&sig.ignored[s/32], i) 246 247 sigenable(s) 248 } 249 250 // Must only be called from a single goroutine at a time. 251 //go:linkname signal_disable os/signal.signal_disable 252 func signal_disable(s uint32) { 253 if s >= uint32(len(sig.wanted)*32) { 254 return 255 } 256 sigdisable(s) 257 258 w := sig.wanted[s/32] 259 w &^= 1 << (s & 31) 260 atomic.Store(&sig.wanted[s/32], w) 261 } 262 263 // Must only be called from a single goroutine at a time. 264 //go:linkname signal_ignore os/signal.signal_ignore 265 func signal_ignore(s uint32) { 266 if s >= uint32(len(sig.wanted)*32) { 267 return 268 } 269 sigignore(s) 270 271 w := sig.wanted[s/32] 272 w &^= 1 << (s & 31) 273 atomic.Store(&sig.wanted[s/32], w) 274 275 i := sig.ignored[s/32] 276 i |= 1 << (s & 31) 277 atomic.Store(&sig.ignored[s/32], i) 278 } 279 280 // sigInitIgnored marks the signal as already ignored. This is called at 281 // program start by initsig. In a shared library initsig is called by 282 // libpreinit, so the runtime may not be initialized yet. 283 //go:nosplit 284 func sigInitIgnored(s uint32) { 285 i := sig.ignored[s/32] 286 i |= 1 << (s & 31) 287 atomic.Store(&sig.ignored[s/32], i) 288 } 289 290 // Checked by signal handlers. 291 //go:linkname signal_ignored os/signal.signal_ignored 292 func signal_ignored(s uint32) bool { 293 i := atomic.Load(&sig.ignored[s/32]) 294 return i&(1<<(s&31)) != 0 295 } 296