Source file src/runtime/runtime2.go
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 package runtime 6 7 import ( 8 "internal/abi" 9 "internal/chacha8rand" 10 "internal/goarch" 11 "internal/runtime/atomic" 12 "internal/runtime/sys" 13 "unsafe" 14 ) 15 16 // defined constants 17 const ( 18 // G status 19 // 20 // Beyond indicating the general state of a G, the G status 21 // acts like a lock on the goroutine's stack (and hence its 22 // ability to execute user code). 23 // 24 // If you add to this list, add to the list 25 // of "okay during garbage collection" status 26 // in mgcmark.go too. 27 // 28 // TODO(austin): The _Gscan bit could be much lighter-weight. 29 // For example, we could choose not to run _Gscanrunnable 30 // goroutines found in the run queue, rather than CAS-looping 31 // until they become _Grunnable. And transitions like 32 // _Gscanwaiting -> _Gscanrunnable are actually okay because 33 // they don't affect stack ownership. 34 35 // _Gidle means this goroutine was just allocated and has not 36 // yet been initialized. 37 _Gidle = iota // 0 38 39 // _Grunnable means this goroutine is on a run queue. It is 40 // not currently executing user code. The stack is not owned. 41 _Grunnable // 1 42 43 // _Grunning means this goroutine may execute user code. The 44 // stack is owned by this goroutine. It is not on a run queue. 45 // It is assigned an M (g.m is valid) and it usually has a P 46 // (g.m.p is valid), but there are small windows of time where 47 // it might not, namely upon entering and exiting _Gsyscall. 48 _Grunning // 2 49 50 // _Gsyscall means this goroutine is executing a system call. 51 // It is not executing user code. The stack is owned by this 52 // goroutine. It is not on a run queue. It is assigned an M. 53 // It may have a P attached, but it does not own it. Code 54 // executing in this state must not touch g.m.p. 55 _Gsyscall // 3 56 57 // _Gwaiting means this goroutine is blocked in the runtime. 58 // It is not executing user code. It is not on a run queue, 59 // but should be recorded somewhere (e.g., a channel wait 60 // queue) so it can be ready()d when necessary. The stack is 61 // not owned *except* that a channel operation may read or 62 // write parts of the stack under the appropriate channel 63 // lock. Otherwise, it is not safe to access the stack after a 64 // goroutine enters _Gwaiting (e.g., it may get moved). 65 _Gwaiting // 4 66 67 // _Gmoribund_unused is currently unused, but hardcoded in gdb 68 // scripts. 69 _Gmoribund_unused // 5 70 71 // _Gdead means this goroutine is currently unused. It may be 72 // just exited, on a free list, or just being initialized. It 73 // is not executing user code. It may or may not have a stack 74 // allocated. The G and its stack (if any) are owned by the M 75 // that is exiting the G or that obtained the G from the free 76 // list. 77 _Gdead // 6 78 79 // _Genqueue_unused is currently unused. 80 _Genqueue_unused // 7 81 82 // _Gcopystack means this goroutine's stack is being moved. It 83 // is not executing user code and is not on a run queue. The 84 // stack is owned by the goroutine that put it in _Gcopystack. 85 _Gcopystack // 8 86 87 // _Gpreempted means this goroutine stopped itself for a 88 // suspendG preemption. It is like _Gwaiting, but nothing is 89 // yet responsible for ready()ing it. Some suspendG must CAS 90 // the status to _Gwaiting to take responsibility for 91 // ready()ing this G. 92 _Gpreempted // 9 93 94 // _Gleaked represents a leaked goroutine caught by the GC. 95 _Gleaked // 10 96 97 // _Gdeadextra is a _Gdead goroutine that's attached to an extra M 98 // used for cgo callbacks. 99 _Gdeadextra // 11 100 101 // _Gscan combined with one of the above states other than 102 // _Grunning indicates that GC is scanning the stack. The 103 // goroutine is not executing user code and the stack is owned 104 // by the goroutine that set the _Gscan bit. 105 // 106 // _Gscanrunning is different: it is used to briefly block 107 // state transitions while GC signals the G to scan its own 108 // stack. This is otherwise like _Grunning. 109 // 110 // atomicstatus&~Gscan gives the state the goroutine will 111 // return to when the scan completes. 112 _Gscan = 0x1000 113 _Gscanrunnable = _Gscan + _Grunnable // 0x1001 114 _Gscanrunning = _Gscan + _Grunning // 0x1002 115 _Gscansyscall = _Gscan + _Gsyscall // 0x1003 116 _Gscanwaiting = _Gscan + _Gwaiting // 0x1004 117 _Gscanpreempted = _Gscan + _Gpreempted // 0x1009 118 _Gscanleaked = _Gscan + _Gleaked // 0x100a 119 _Gscandeadextra = _Gscan + _Gdeadextra // 0x100b 120 ) 121 122 const ( 123 // P status 124 125 // _Pidle means a P is not being used to run user code or the 126 // scheduler. Typically, it's on the idle P list and available 127 // to the scheduler, but it may just be transitioning between 128 // other states. 129 // 130 // The P is owned by the idle list or by whatever is 131 // transitioning its state. Its run queue is empty. 132 _Pidle = iota 133 134 // _Prunning means a P is owned by an M and is being used to 135 // run user code or the scheduler. Only the M that owns this P 136 // is allowed to change the P's status from _Prunning. The M 137 // may transition the P to _Pidle (if it has no more work to 138 // do), or _Pgcstop (to halt for the GC). The M may also hand 139 // ownership of the P off directly to another M (for example, 140 // to schedule a locked G). 141 _Prunning 142 143 // _Psyscall_unused is a now-defunct state for a P. A P is 144 // identified as "in a system call" by looking at the goroutine's 145 // state. 146 _Psyscall_unused 147 148 // _Pgcstop means a P is halted for STW and owned by the M 149 // that stopped the world. The M that stopped the world 150 // continues to use its P, even in _Pgcstop. Transitioning 151 // from _Prunning to _Pgcstop causes an M to release its P and 152 // park. 153 // 154 // The P retains its run queue and startTheWorld will restart 155 // the scheduler on Ps with non-empty run queues. 156 _Pgcstop 157 158 // _Pdead means a P is no longer used (GOMAXPROCS shrank). We 159 // reuse Ps if GOMAXPROCS increases. A dead P is mostly 160 // stripped of its resources, though a few things remain 161 // (e.g., trace buffers). 162 _Pdead 163 ) 164 165 // Mutual exclusion locks. In the uncontended case, 166 // as fast as spin locks (just a few user-level instructions), 167 // but on the contention path they sleep in the kernel. 168 // A zeroed Mutex is unlocked (no need to initialize each lock). 169 // Initialization is helpful for static lock ranking, but not required. 170 type mutex struct { 171 // Empty struct if lock ranking is disabled, otherwise includes the lock rank 172 lockRankStruct 173 // Futex-based impl treats it as uint32 key, 174 // while sema-based impl as M* waitm. 175 // Used to be a union, but unions break precise GC. 176 key uintptr 177 } 178 179 type funcval struct { 180 fn uintptr 181 // variable-size, fn-specific data here 182 } 183 184 type iface struct { 185 tab *itab 186 data unsafe.Pointer 187 } 188 189 type eface struct { 190 _type *_type 191 data unsafe.Pointer 192 } 193 194 func efaceOf(ep *any) *eface { 195 return (*eface)(unsafe.Pointer(ep)) 196 } 197 198 // The guintptr, muintptr, and puintptr are all used to bypass write barriers. 199 // It is particularly important to avoid write barriers when the current P has 200 // been released, because the GC thinks the world is stopped, and an 201 // unexpected write barrier would not be synchronized with the GC, 202 // which can lead to a half-executed write barrier that has marked the object 203 // but not queued it. If the GC skips the object and completes before the 204 // queuing can occur, it will incorrectly free the object. 205 // 206 // We tried using special assignment functions invoked only when not 207 // holding a running P, but then some updates to a particular memory 208 // word went through write barriers and some did not. This breaks the 209 // write barrier shadow checking mode, and it is also scary: better to have 210 // a word that is completely ignored by the GC than to have one for which 211 // only a few updates are ignored. 212 // 213 // Gs and Ps are always reachable via true pointers in the 214 // allgs and allp lists or (during allocation before they reach those lists) 215 // from stack variables. 216 // 217 // Ms are always reachable via true pointers either from allm or 218 // freem. Unlike Gs and Ps we do free Ms, so it's important that 219 // nothing ever hold an muintptr across a safe point. 220 221 // A guintptr holds a goroutine pointer, but typed as a uintptr 222 // to bypass write barriers. It is used in the Gobuf goroutine state 223 // and in scheduling lists that are manipulated without a P. 224 // 225 // The Gobuf.g goroutine pointer is almost always updated by assembly code. 226 // In one of the few places it is updated by Go code - func save - it must be 227 // treated as a uintptr to avoid a write barrier being emitted at a bad time. 228 // Instead of figuring out how to emit the write barriers missing in the 229 // assembly manipulation, we change the type of the field to uintptr, 230 // so that it does not require write barriers at all. 231 // 232 // Goroutine structs are published in the allg list and never freed. 233 // That will keep the goroutine structs from being collected. 234 // There is never a time that Gobuf.g's contain the only references 235 // to a goroutine: the publishing of the goroutine in allg comes first. 236 // Goroutine pointers are also kept in non-GC-visible places like TLS, 237 // so I can't see them ever moving. If we did want to start moving data 238 // in the GC, we'd need to allocate the goroutine structs from an 239 // alternate arena. Using guintptr doesn't make that problem any worse. 240 // Note that pollDesc.rg, pollDesc.wg also store g in uintptr form, 241 // so they would need to be updated too if g's start moving. 242 type guintptr uintptr 243 244 //go:nosplit 245 func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) } 246 247 //go:nosplit 248 func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) } 249 250 //go:nosplit 251 func (gp *guintptr) cas(old, new guintptr) bool { 252 return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new)) 253 } 254 255 //go:nosplit 256 func (gp *g) guintptr() guintptr { 257 return guintptr(unsafe.Pointer(gp)) 258 } 259 260 // setGNoWB performs *gp = new without a write barrier. 261 // For times when it's impractical to use a guintptr. 262 // 263 //go:nosplit 264 //go:nowritebarrier 265 func setGNoWB(gp **g, new *g) { 266 (*guintptr)(unsafe.Pointer(gp)).set(new) 267 } 268 269 type puintptr uintptr 270 271 //go:nosplit 272 func (pp puintptr) ptr() *p { return (*p)(unsafe.Pointer(pp)) } 273 274 //go:nosplit 275 func (pp *puintptr) set(p *p) { *pp = puintptr(unsafe.Pointer(p)) } 276 277 // muintptr is a *m that is not tracked by the garbage collector. 278 // 279 // Because we do free Ms, there are some additional constrains on 280 // muintptrs: 281 // 282 // 1. Never hold an muintptr locally across a safe point. 283 // 284 // 2. Any muintptr in the heap must be owned by the M itself so it can 285 // ensure it is not in use when the last true *m is released. 286 type muintptr uintptr 287 288 //go:nosplit 289 func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) } 290 291 //go:nosplit 292 func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) } 293 294 // setMNoWB performs *mp = new without a write barrier. 295 // For times when it's impractical to use an muintptr. 296 // 297 //go:nosplit 298 //go:nowritebarrier 299 func setMNoWB(mp **m, new *m) { 300 (*muintptr)(unsafe.Pointer(mp)).set(new) 301 } 302 303 type gobuf struct { 304 // ctxt is unusual with respect to GC: it may be a 305 // heap-allocated funcval, so GC needs to track it, but it 306 // needs to be set and cleared from assembly, where it's 307 // difficult to have write barriers. However, ctxt is really a 308 // saved, live register, and we only ever exchange it between 309 // the real register and the gobuf. Hence, we treat it as a 310 // root during stack scanning, which means assembly that saves 311 // and restores it doesn't need write barriers. It's still 312 // typed as a pointer so that any other writes from Go get 313 // write barriers. 314 sp uintptr 315 pc uintptr 316 g guintptr 317 ctxt unsafe.Pointer 318 lr uintptr // link register; on wasm32, PC_F paired with pc 319 bp uintptr // for framepointer-enabled architectures 320 } 321 322 // maybeTraceablePtr is a special pointer that is conditionally trackable 323 // by the GC. It consists of an address as a uintptr (vu) and a pointer 324 // to a data element (vp). 325 // 326 // maybeTraceablePtr values can be in one of three states: 327 // 1. Unset: vu == 0 && vp == nil 328 // 2. Untracked: vu != 0 && vp == nil 329 // 3. Tracked: vu != 0 && vp != nil 330 // 331 // Do not set fields manually. Use methods instead. 332 // Extend this type with additional methods if needed. 333 type maybeTraceablePtr struct { 334 vp unsafe.Pointer // For liveness only. 335 vu uintptr // Source of truth. 336 } 337 338 // setUntraceable unsets the pointer but preserves the address. 339 // This is used to hide the pointer from the GC. 340 // 341 //go:nosplit 342 func (p *maybeTraceablePtr) setUntraceable() { 343 p.vp = nil 344 } 345 346 // setTraceable resets the pointer to the stored address. 347 // This is used to make the pointer visible to the GC. 348 // 349 //go:nosplit 350 func (p *maybeTraceablePtr) setTraceable() { 351 p.vp = unsafe.Pointer(p.vu) 352 } 353 354 // set sets the pointer to the data element and updates the address. 355 // 356 //go:nosplit 357 func (p *maybeTraceablePtr) set(v unsafe.Pointer) { 358 p.vp = v 359 p.vu = uintptr(v) 360 } 361 362 // get retrieves the pointer to the data element. 363 // 364 //go:nosplit 365 func (p *maybeTraceablePtr) get() unsafe.Pointer { 366 return unsafe.Pointer(p.vu) 367 } 368 369 // uintptr returns the uintptr address of the pointer. 370 // 371 //go:nosplit 372 func (p *maybeTraceablePtr) uintptr() uintptr { 373 return p.vu 374 } 375 376 // maybeTraceableChan extends conditionally trackable pointers (maybeTraceablePtr) 377 // to track hchan pointers. 378 // 379 // Do not set fields manually. Use methods instead. 380 type maybeTraceableChan struct { 381 maybeTraceablePtr 382 } 383 384 //go:nosplit 385 func (p *maybeTraceableChan) set(c *hchan) { 386 p.maybeTraceablePtr.set(unsafe.Pointer(c)) 387 } 388 389 //go:nosplit 390 func (p *maybeTraceableChan) get() *hchan { 391 return (*hchan)(p.maybeTraceablePtr.get()) 392 } 393 394 // waitingGType describes which type of synchronization primitive a waitingG is 395 // used with. 396 type waitingGType uint8 397 398 const ( 399 waitingGTypeNone waitingGType = iota 400 401 // Channel operations, including select. 402 waitingGTypeChan 403 404 // semaphores (semacquire, which transitively includes sync.Mutex/RWMutex). 405 waitingGTypeSema 406 407 // sync.Cond, via runtime.notifyList. 408 waitingGTypeCond 409 ) 410 411 // waitingG represents a g on a wait list of a synchronization primitive: 412 // either a channel, semaphore (including sync.Mutex) or notifyList 413 // (sync.Cond). 414 // 415 // waitingG is necessary because the g ↔ synchronization object relation 416 // is many-to-many. A g can be on many wait lists, so there may be 417 // many waitingGs for one g; and many gs may be waiting on the same 418 // synchronization object, so there may be many waitingGs for one object. 419 // 420 // waitingGs are allocated from a special pool. Use acquireWaitingG and 421 // releaseWaitingG to allocate and free them. 422 type waitingG struct { 423 // Which type of waitingG is this. Immutable after allocation. 424 typ waitingGType 425 426 // For channels, the following fields are protected by the 427 // hchan.lock of the channel this waitingG is blocking on. shrinkstack 428 // depends on this for waitingGs involved in channel ops. 429 // 430 // For semaphores, all fields are only accessed when holding a semaRoot 431 // lock. 432 433 g *g 434 435 next *waitingG 436 prev *waitingG 437 438 elem maybeTraceablePtr // data element (may point to stack) 439 440 // The following fields are never accessed concurrently. 441 // For channels, waitlink is only accessed by g. 442 443 acquiretime int64 444 releasetime int64 445 ticket uint32 446 447 // isSelect indicates g is participating in a select, so 448 // g.selectDone must be CAS'd to win the wake-up race. 449 isSelect bool 450 451 // success indicates whether communication over channel c 452 // succeeded. It is true if the goroutine was awoken because a 453 // value was delivered over channel c, and false if awoken 454 // because c was closed. 455 success bool 456 457 // waiters is a count of semaRoot waiting list other than head of list, 458 // clamped to a uint16 to fit in unused space. 459 // Only meaningful at the head of the list. 460 // (If we wanted to be overly clever, we could store a high 16 bits 461 // in the second entry in the list.) 462 waiters uint16 463 464 parent *waitingG // semaRoot binary tree 465 waitlink *waitingG // g.waiting list or semaRoot 466 waittail *waitingG // semaRoot 467 c maybeTraceableChan // channel 468 } 469 470 type libcall struct { 471 fn uintptr 472 n uintptr // number of parameters 473 args uintptr // parameters 474 r1 uintptr // return values 475 r2 uintptr 476 err uintptr // error number 477 } 478 479 // Stack describes a Go execution stack. 480 // The bounds of the stack are exactly [lo, hi), 481 // with no implicit data structures on either side. 482 type stack struct { 483 lo uintptr 484 hi uintptr 485 } 486 487 // A stackOff is a position on a goroutine's stack, recorded as its distance 488 // below the top of the stack (gp.stack.hi). 489 // 490 // Stack copying preserves the distance from every stack address to the top of 491 // the stack, so unlike an address, a stackOff remains valid across stack copies 492 // without any adjustment. And unlike a pointer, it does not keep stack objects 493 // alive during stack scanning. 494 type stackOff uintptr 495 496 // toStackOff returns the stackOff of addr, which must be on gp's stack 497 // (or exactly gp.stack.hi, which can be a frame boundary). It throws if addr 498 // isn't, which catches addresses taken on the wrong goroutine or left stale by 499 // a stack move. 500 // 501 // This must be nosplit: callers pass absolute stack addresses, which a stack 502 // move in this function's prologue would leave stale. 503 // 504 //go:nosplit 505 func (gp *g) toStackOff(addr uintptr) stackOff { 506 if addr < gp.stack.lo || addr > gp.stack.hi { 507 badStackOff(gp, addr) 508 } 509 return stackOff(gp.stack.hi - addr) 510 } 511 512 // badStackOff reports a toStackOff of an address that is not on gp's stack. 513 // It is out of line to keep toStackOff inlinable. 514 // 515 //go:noinline 516 func badStackOff(gp *g, addr uintptr) { 517 print("runtime: address ", hex(addr), " not on goroutine ", gp.goid, " stack [", hex(gp.stack.lo), ", ", hex(gp.stack.hi), "]\n") 518 throw("toStackOff: address not on stack") 519 } 520 521 // fromStackOff returns the current address of off on gp's stack. The result is 522 // only valid until gp's stack next moves. 523 // 524 // This must be nosplit for the same reason as toStackOff: callers may have 525 // loaded other absolute stack addresses to compare against the result. 526 // 527 //go:nosplit 528 func (gp *g) fromStackOff(off stackOff) uintptr { 529 return gp.stack.hi - uintptr(off) 530 } 531 532 // heldLockInfo gives info on a held lock and the rank of that lock 533 type heldLockInfo struct { 534 lockAddr uintptr 535 rank lockRank 536 } 537 538 type g struct { 539 // Stack parameters. 540 // stack describes the actual stack memory: [stack.lo, stack.hi). 541 // stackguard0 is the stack pointer compared in the Go stack growth prologue. 542 // It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption. 543 // stackguard1 is the stack pointer compared in the //go:systemstack stack growth prologue. 544 // It is stack.lo+StackGuard on g0 and gsignal stacks. 545 // It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash). 546 stack stack // offset known to runtime/cgo 547 stackguard0 uintptr // offset known to cmd/internal/obj/* 548 stackguard1 uintptr // offset known to cmd/internal/obj/* 549 550 _panic *_panic // innermost panic 551 _defer *_defer // innermost defer 552 m *m // current m 553 sched gobuf 554 syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc 555 syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc 556 syscallbp uintptr // if status==Gsyscall, syscallbp = sched.bp to use in fpTraceback 557 stktopsp uintptr // expected sp at top of stack, to check in traceback 558 // param is a generic pointer parameter field used to pass 559 // values in particular contexts where other storage for the 560 // parameter would be difficult to find. It is currently used 561 // in four ways: 562 // 1. When a channel operation wakes up a blocked goroutine, it sets param to 563 // point to the waitingG of the completed blocking operation. 564 // 2. By gcAssistAlloc1 to signal back to its caller that the goroutine completed 565 // the GC cycle. It is unsafe to do so in any other way, because the goroutine's 566 // stack may have moved in the meantime. 567 // 3. By debugCallWrap to pass parameters to a new goroutine because allocating a 568 // closure in the runtime is forbidden. 569 // 4. When a panic is recovered and control returns to the respective frame, 570 // param may point to a savedOpenDeferState. 571 param unsafe.Pointer 572 atomicstatus atomic.Uint32 573 trace gTraceState // per-G tracer state 574 wasmCont wasmContPerG // GOWASM=stackswitch only 575 goid uint64 576 schedlink guintptr 577 waitsince int64 // approx time when the g become blocked 578 waitreason waitReason // if status==Gwaiting 579 580 preempt bool // preemption signal, duplicates stackguard0 = stackpreempt 581 preemptStop bool // transition to _Gpreempted on preemption; otherwise, just deschedule 582 preemptShrink bool // shrink stack at synchronous safe point 583 584 // asyncSafePoint is set if g is stopped at an asynchronous 585 // safe point. This means there are frames on the stack 586 // without precise pointer information. 587 asyncSafePoint bool 588 589 paniconfault bool // panic (instead of crash) on unexpected fault address 590 gcscandone bool // g has scanned stack; protected by _Gscan bit in status 591 throwsplit bool // must not split stack 592 // activeStackChans indicates that there are unlocked channels 593 // pointing into this goroutine's stack. If true, stack 594 // copying needs to acquire channel locks to protect these 595 // areas of the stack. 596 activeStackChans bool 597 // parkingOnChan indicates that the goroutine is about to 598 // park on a chansend or chanrecv. Used to signal an unsafe point 599 // for stack shrinking. 600 parkingOnChan atomic.Bool 601 // inMarkAssist indicates whether the goroutine is in mark assist. 602 // Used by the execution tracer. 603 inMarkAssist bool 604 coroexit bool // argument to coroswitch_m 605 606 raceignore int8 // ignore race detection events 607 nocgocallback bool // whether disable callback from C 608 tracking bool // whether we're tracking this G for sched latency statistics 609 trackingSeq uint8 // used to decide whether to track this G 610 trackingStamp int64 // timestamp of when the G last started being tracked 611 runnableTime int64 // the amount of time spent runnable, cleared when running, only used when tracking 612 lockedm muintptr 613 fipsIndicator uint8 614 fipsOnlyBypass bool 615 ditWanted bool // set if g wants to be executed with DIT enabled 616 syncSafePoint bool // set if g is stopped at a synchronous safe point. 617 runningCleanups atomic.Bool 618 619 // waiting references the synchronization primitives that g is 620 // currently or soon waiting on. 621 // 622 // For waitingGTypeChan: waiting.elem refers to the src/dst location 623 // for the channel operation. waitingG.waitlink forms a linked list of 624 // channels the G is waiting on (that have a valid elem pointer), in 625 // lock order. 626 // 627 // For waitingGTypeSema: waiting.elem refers to the semaphore the G is 628 // waiting on. 629 // 630 // For waitingGTypeCond: waiting.elem refers to the notifyList the G is 631 // waiting on. 632 // 633 // Note that waiting need not be set atomically with parking. Thus the 634 // G may be preempted with waiting set. 635 waiting *waitingG 636 637 sig uint32 638 secret int32 // current nesting of runtime/secret.Do calls. 639 sigcode0 uintptr 640 sigcode1 uintptr 641 sigpc uintptr 642 parentGoid uint64 // goid of goroutine that created this goroutine 643 gopc uintptr // pc of go statement that created this goroutine 644 ancestors *[]ancestorInfo // ancestor information goroutine(s) that created this goroutine (only used if debug.tracebackancestors) 645 startpc uintptr // pc of goroutine function 646 racectx uintptr 647 cgoCtxt *[]uintptr // cgo traceback contexts, allocated on first use (see cgocallbackg1) 648 labels unsafe.Pointer // profiler labels 649 timer *timer // cached timer for time.Sleep 650 sleepWhen int64 // when to sleep until 651 selectDone atomic.Uint32 // are we participating in a select and did someone win the race? 652 653 // goroutineProfiled indicates the status of this goroutine's stack for the 654 // current in-progress goroutine profile 655 goroutineProfiled goroutineProfileStateHolder 656 657 coroarg *coro // argument during coroutine transfers 658 bubble *synctestBubble 659 660 // xRegs stores the extended register state if this G has been 661 // asynchronously preempted. 662 xRegs xRegPerG 663 664 // valgrind is the Valgrind state of this G's stack. It is empty 665 // unless the runtime is built with Valgrind support (see valgrind.go). 666 valgrind valgrindPerG 667 668 // Per-G GC state 669 670 // gcAssistBytes is this G's GC assist credit in terms of 671 // bytes allocated. If this is positive, then the G has credit 672 // to allocate gcAssistBytes bytes without assisting. If this 673 // is negative, then the G must correct this by performing 674 // scan work. We track this in bytes to make it fast to update 675 // and check for debt in the malloc hot path. The assist ratio 676 // determines how this corresponds to scan work debt. 677 gcAssistBytes int64 678 } 679 680 // gTrackingPeriod is the number of transitions out of _Grunning between 681 // latency tracking runs. 682 const gTrackingPeriod = 8 683 684 const ( 685 // tlsSlots is the number of pointer-sized slots reserved for TLS on some platforms, 686 // like Windows. 687 tlsSlots = 6 688 tlsSize = tlsSlots * goarch.PtrSize 689 ) 690 691 // Values for m.freeWait. 692 const ( 693 freeMStack = 0 // M done, free stack and reference. 694 freeMRef = 1 // M done, free reference. 695 freeMWait = 2 // M still in use. 696 ) 697 698 type m struct { 699 g0 *g // goroutine with scheduling stack 700 morebuf gobuf // gobuf arg to morestack 701 divmod uint32 // div/mod denominator for arm - known to liblink (cmd/internal/obj/arm/obj5.go) 702 703 // Fields whose offsets are not known to debuggers. 704 705 procid uint64 // for debuggers, but offset not hard-coded 706 gsignal *g // signal-handling g 707 goSigStack gsignalStack // Go-allocated signal handling stack 708 sigmask sigset // storage for saved signal mask 709 tls [tlsSlots]uintptr // thread-local storage (for x86 extern register) 710 mstartfn func() 711 curg *g // current running goroutine 712 caughtsig guintptr // goroutine running during fatal signal 713 714 // Indicates whether we've received a signal while 715 // running in secret mode. 716 signalSecret bool 717 718 // p is the currently attached P for executing Go code, nil if not executing user Go code. 719 // 720 // A non-nil p implies exclusive ownership of the P, unless curg is in _Gsyscall. 721 // In _Gsyscall the scheduler may mutate this instead. The point of synchronization 722 // is the _Gscan bit on curg's status. The scheduler must arrange to prevent curg 723 // from transitioning out of _Gsyscall if it intends to mutate p. 724 p puintptr 725 726 nextp puintptr // The next P to install before executing. Implies exclusive ownership of this P. 727 oldp puintptr // The P that was attached before executing a syscall. 728 id int64 729 mallocing int32 730 throwing throwType 731 preemptoff string // if != "", keep curg running on this m 732 locks int32 733 dying int32 734 profilehz int32 735 spinning bool // m is out of work and is actively looking for work 736 blocked bool // m is blocked on a note 737 newSigstack bool // minit on C thread called sigaltstack 738 printlock int8 739 incgo bool // m is executing a cgo call 740 isextra bool // m is an extra m 741 isExtraInC bool // m is an extra m that does not have any Go frames 742 isExtraInSig bool // m is an extra m in a signal handler 743 freeWait atomic.Uint32 // Whether it is safe to free g0 and delete m (one of freeMRef, freeMStack, freeMWait) 744 needextram bool 745 g0StackAccurate bool // whether the g0 stack has accurate bounds 746 traceback uint8 747 allpSnapshot []*p // Snapshot of allp for use after dropping P in findRunnable, nil otherwise. 748 ncgocall uint64 // number of cgo calls in total 749 ncgo int32 // number of cgo calls currently in progress 750 cgoCallersUse atomic.Uint32 // if non-zero, cgoCallers in use temporarily 751 cgoCallers *cgoCallers // cgo traceback if crashing in cgo call 752 park note 753 alllink *m // on allm 754 schedlink muintptr 755 idleNode listNodeManual 756 lockedg guintptr 757 createstack [32]uintptr // stack that created this thread, it's used for StackRecord.Stack0, so it must align with it. 758 lockedExt uint32 // tracking for external LockOSThread 759 lockedInt uint32 // tracking for internal lockOSThread 760 mWaitList mWaitList // list of runtime lock waiters 761 ditEnabled bool // set if DIT is currently enabled on this M 762 syncIOWorker bool // dedicated Windows synchronous pipe I/O worker; immutable after startup 763 764 mLockProfile mLockProfile // fields relating to runtime.lock contention 765 profStack []uintptr // used for memory/block/mutex stack traces 766 767 // wait* are used to carry arguments from gopark into park_m, because 768 // there's no stack to put them on. That is their sole purpose. 769 waitunlockf func(*g, unsafe.Pointer) bool 770 waitlock unsafe.Pointer 771 waitTraceSkip int 772 waitTraceBlockReason traceBlockReason 773 774 syscalltick uint32 775 freelink *m // on sched.freem 776 trace mTraceState 777 778 // These are here to avoid using the G stack so the stack can move during the call. 779 libcallpc uintptr // for cpu profiler 780 libcallsp uintptr 781 libcallg guintptr 782 winsyscall winlibcall // stores syscall parameters on windows 783 784 vdsoSP uintptr // SP for traceback while in VDSO call (0 if not in call) 785 vdsoPC uintptr // PC for traceback while in VDSO call 786 787 // preemptGen counts the number of completed preemption 788 // signals. This is used to detect when a preemption is 789 // requested, but fails. 790 preemptGen atomic.Uint32 791 792 // Whether this is a pending preemption signal on this M. 793 signalPending atomic.Uint32 794 795 // pcvalue lookup cache 796 pcvalueCache pcvalueCache 797 798 dlogPerM 799 800 mOS 801 802 chacha8 chacha8rand.State 803 cheaprand uint32 804 cheaprand64 uint64 805 806 // Up to 10 locks held by this m, maintained by the lock ranking code. 807 locksHeldLen int 808 locksHeld [10]heldLockInfo 809 810 // self points this M until mexit clears it to return nil. 811 self mWeakPointer 812 813 // While writebufg is set, its print output goes to writebuf (see 814 // gwrite). Output of any other goroutine on this M, for example a 815 // signal handler running on gsignal, still goes to standard error. 816 // writebufg must not move to another M while it is set. 817 writebuf []byte 818 writebufg guintptr 819 } 820 821 const mRedZoneSize = (16 << 3) * asanenabledBit // redZoneSize(2048) 822 823 type mPadded struct { 824 m 825 826 // Size the runtime.m structure so it fits in the 2048-byte size class, and 827 // not in the next-smallest (1792-byte) size class. That leaves the 11 low 828 // bits of muintptr values available for flags, as required by 829 // lock_spinbit.go. 830 _ [(1 - (goarch.IsWasm | goarch.IsWasm32)) * (2048 - mallocHeaderSize - mRedZoneSize - unsafe.Sizeof(m{}))]byte 831 } 832 833 // mWeakPointer is a "weak" pointer to an M. A weak pointer for each M is 834 // available as m.self. Users may copy mWeakPointer arbitrarily, and get will 835 // return the M if it is still live, or nil after mexit. 836 // 837 // The zero value is treated as a nil pointer. 838 // 839 // Note that get may race with M exit. A successful get will keep the m object 840 // alive, but the M itself may be exited and thus not actually usable. 841 type mWeakPointer struct { 842 m *atomic.Pointer[m] 843 } 844 845 func newMWeakPointer(mp *m) mWeakPointer { 846 w := mWeakPointer{m: new(atomic.Pointer[m])} 847 w.m.Store(mp) 848 return w 849 } 850 851 func (w mWeakPointer) get() *m { 852 if w.m == nil { 853 return nil 854 } 855 return w.m.Load() 856 } 857 858 // clear sets the weak pointer to nil. It cannot be used on zero value 859 // mWeakPointers. 860 func (w mWeakPointer) clear() { 861 w.m.Store(nil) 862 } 863 864 type p struct { 865 id int32 866 status uint32 // one of pidle/prunning/... 867 link puintptr 868 schedtick uint32 // incremented on every scheduler call 869 syscalltick uint32 // incremented on every system call 870 sysmontick sysmontick // last tick observed by sysmon 871 m muintptr // back-link to associated m (nil if idle) 872 mcache *mcache 873 pcache pageCache 874 raceprocctx uintptr 875 876 // oldm is the previous m this p ran on. 877 // 878 // We are not associated with this m, so we have no control over its 879 // lifecycle. This value is an m.self object which points to the m 880 // until the m exits. 881 // 882 // Note that this m may be idle, running, or exiting. It should only be 883 // used with mgetSpecific, which will take ownership of the m only if 884 // it is idle. 885 oldm mWeakPointer 886 887 deferpool []*_defer // pool of available defer structs (see panic.go) 888 deferpoolbuf [32]*_defer 889 890 // Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen. 891 goidcache uint64 892 goidcacheend uint64 893 894 // Queue of runnable goroutines. Accessed without lock. 895 runqhead uint32 896 runqtail uint32 897 runq [256]guintptr 898 // runnext, if non-nil, is a runnable G that was ready'd by 899 // the current G and should be run next instead of what's in 900 // runq if there's time remaining in the running G's time 901 // slice. It will inherit the time left in the current time 902 // slice. If a set of goroutines is locked in a 903 // communicate-and-wait pattern, this schedules that set as a 904 // unit and eliminates the (potentially large) scheduling 905 // latency that otherwise arises from adding the ready'd 906 // goroutines to the end of the run queue. 907 // 908 // Note that while other P's may atomically CAS this to zero, 909 // only the owner P can CAS it to a valid G. 910 runnext guintptr 911 912 // Available G's (status == Gdead) 913 gFree gList 914 915 waitingGCache []*waitingG 916 waitingGBuf [128]*waitingG 917 918 // Cache of mspan objects from the heap. 919 mspancache struct { 920 // We need an explicit length here because this field is used 921 // in allocation codepaths where write barriers are not allowed, 922 // and eliminating the write barrier/keeping it eliminated from 923 // slice updates is tricky, more so than just managing the length 924 // ourselves. 925 len int 926 buf [128]*mspan 927 } 928 929 // Cache of a single pinner object to reduce allocations from repeated 930 // pinner creation. 931 pinnerCache *pinner 932 933 // Cache of one unused pin counter. Accessed by this P with preemption 934 // disabled, or during STW. Returned to the allocator when the P is destroyed. 935 pinCounterCache *specialPinCounter 936 937 trace pTraceState 938 939 palloc persistentAlloc // per-P to avoid mutex 940 941 // Per-P GC state 942 gcAssistTime int64 // Nanoseconds in assistAlloc 943 gcFractionalMarkTime atomic.Int64 // Nanoseconds in fractional mark worker 944 945 // limiterEvent tracks events for the GC CPU limiter. 946 limiterEvent limiterEvent 947 948 // gcMarkWorkerMode is the mode for the next mark worker to run in. 949 // That is, this is used to communicate with the worker goroutine 950 // selected for immediate execution by 951 // gcController.findRunnableGCWorker. When scheduling other goroutines, 952 // this field must be set to gcMarkWorkerNotWorker. 953 gcMarkWorkerMode gcMarkWorkerMode 954 // gcMarkWorkerStartTime is the nanotime() at which the most recent 955 // mark worker started. 956 gcMarkWorkerStartTime int64 957 958 // nextGCMarkWorker is the next mark worker to run. This may be set 959 // during start-the-world to assign a worker to this P. The P runs this 960 // worker on the next call to gcController.findRunnableGCWorker. If the 961 // P runs something else or stops, it must release this worker via 962 // gcController.releaseNextGCMarkWorker. 963 // 964 // See comment in gcBgMarkWorker about the lifetime of 965 // gcBgMarkWorkerNode. 966 // 967 // Only accessed by this P or during STW. 968 nextGCMarkWorker *gcBgMarkWorkerNode 969 970 // gcw is this P's GC work buffer cache. The work buffer is 971 // filled by write barriers, drained by mutator assists, and 972 // disposed on certain GC state transitions. 973 gcw gcWork 974 975 // wbBuf is this P's GC write barrier buffer. 976 // 977 // TODO: Consider caching this in the running G. 978 wbBuf wbBuf 979 980 runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point 981 982 // statsSeq is a counter indicating whether this P is currently 983 // writing any stats. Its value is even when not, odd when it is. 984 statsSeq atomic.Uint32 985 986 // Timer heap. 987 timers timers 988 989 // Cleanups. 990 cleanups *cleanupBlock 991 cleanupsQueued uint64 // monotonic count of cleanups queued by this P 992 993 // maxStackScanDelta accumulates the amount of stack space held by 994 // live goroutines (i.e. those eligible for stack scanning). 995 // Flushed to gcController.maxStackScan once maxStackScanSlack 996 // or -maxStackScanSlack is reached. 997 maxStackScanDelta int64 998 999 // gc-time statistics about current goroutines 1000 // Note that this differs from maxStackScan in that this 1001 // accumulates the actual stack observed to be used at GC time (hi - sp), 1002 // not an instantaneous measure of the total stack size that might need 1003 // to be scanned (hi - lo). 1004 scannedStackSize uint64 // stack size of goroutines scanned by this P 1005 scannedStacks uint64 // number of goroutines scanned by this P 1006 1007 // preempt is set to indicate that this P should be enter the 1008 // scheduler ASAP (regardless of what G is running on it). 1009 preempt bool 1010 1011 // gcStopTime is the nanotime timestamp that this P last entered _Pgcstop. 1012 gcStopTime int64 1013 1014 // goroutinesCreated is the total count of goroutines created by this P. 1015 goroutinesCreated uint64 1016 1017 // xRegs is the per-P extended register state used by asynchronous 1018 // preemption. This is an empty struct on platforms that don't use extended 1019 // register state. 1020 xRegs xRegPerP 1021 1022 // Padding is no longer needed. False sharing is now not a worry because p is large enough 1023 // that its size class is an integer multiple of the cache line size (for any of our architectures). 1024 } 1025 1026 type schedt struct { 1027 goidgen atomic.Uint64 1028 lastpoll atomic.Int64 // time of last network poll, 0 if currently polling 1029 pollUntil atomic.Int64 // time to which current poll is sleeping 1030 pollingNet atomic.Int32 // 1 if some P doing non-blocking network poll 1031 1032 lock mutex 1033 1034 // When increasing nmidle, nmidlelocked, nmsys, or nmfreed, be 1035 // sure to call checkdead(). 1036 1037 midle listHeadManual // idle m's waiting for work 1038 nmidle int32 // number of idle m's waiting for work 1039 nmidlelocked int32 // number of locked m's waiting for work 1040 mnext int64 // number of m's that have been created and next M ID 1041 maxmcount int32 // maximum number of m's allowed (or die) 1042 nmsys int32 // number of system m's not counted for deadlock 1043 nmfreed int64 // cumulative number of freed m's 1044 1045 ngsys atomic.Int32 // number of system goroutines 1046 nGsyscallNoP atomic.Int32 // number of goroutines in syscalls without a P but whose M is not isExtraInC 1047 1048 pidle puintptr // idle p's 1049 npidle atomic.Int32 1050 nmspinning atomic.Int32 // See "Worker thread parking/unparking" comment in proc.go. 1051 needspinning atomic.Uint32 // See "Delicate dance" comment in proc.go. Boolean. Must hold sched.lock to set to 1. 1052 1053 // Global runnable queue. 1054 runq gQueue 1055 1056 // disable controls selective disabling of the scheduler. 1057 // 1058 // Use schedEnableUser to control this. 1059 // 1060 // disable is protected by sched.lock. 1061 disable struct { 1062 // user disables scheduling of user goroutines. 1063 user bool 1064 runnable gQueue // pending runnable Gs 1065 } 1066 1067 // Global cache of dead G's. 1068 gFree struct { 1069 lock mutex 1070 stack gList // Gs with stacks 1071 noStack gList // Gs without stacks 1072 } 1073 1074 // Central cache of waitingG structs. 1075 waitingGLock mutex 1076 waitingGCache *waitingG 1077 1078 // Central pool of available defer structs. 1079 deferlock mutex 1080 deferpool *_defer 1081 1082 // freem is the list of m's waiting to be freed when their 1083 // m.exited is set. Linked through m.freelink. 1084 freem *m 1085 1086 // gcwaiting indicates that the gc is waiting to run. When runqgrab delays 1087 // before stealing runnext, platforms with futex sleep on this address. 1088 gcwaiting gcWaitingWord 1089 stopwait int32 1090 stopnote note 1091 sysmonwait atomic.Bool 1092 sysmonnote note 1093 1094 // safePointFn should be called on each P at the next GC 1095 // safepoint if p.runSafePointFn is set. 1096 safePointFn func(*p) 1097 safePointWait int32 1098 safePointNote note 1099 1100 profilehz int32 // cpu profiling rate 1101 1102 procresizetime int64 // nanotime() of last change to gomaxprocs 1103 totaltime int64 // ∫gomaxprocs dt up to procresizetime 1104 1105 customGOMAXPROCS bool // GOMAXPROCS was manually set from the environment or runtime.GOMAXPROCS 1106 1107 // sysmonlock protects sysmon's actions on the runtime. 1108 // 1109 // Acquire and hold this mutex to block sysmon from interacting 1110 // with the rest of the runtime. 1111 sysmonlock mutex 1112 1113 // timeToRun is a distribution of scheduling latencies, defined 1114 // as the sum of time a G spends in the _Grunnable state before 1115 // it transitions to _Grunning. 1116 timeToRun timeHistogram 1117 1118 // idleTime is the total CPU time Ps have "spent" idle. 1119 // 1120 // Reset on each GC cycle. 1121 idleTime atomic.Int64 1122 1123 // totalMutexWaitTime is the sum of time goroutines have spent in _Gwaiting 1124 // with a waitreason of the form waitReasonSync{RW,}Mutex{R,}Lock. 1125 totalMutexWaitTime atomic.Int64 1126 1127 // stwStoppingTimeGC/Other are distributions of stop-the-world stopping 1128 // latencies, defined as the time taken by stopTheWorldWithSema to get 1129 // all Ps to stop. stwStoppingTimeGC covers all GC-related STWs, 1130 // stwStoppingTimeOther covers the others. 1131 stwStoppingTimeGC timeHistogram 1132 stwStoppingTimeOther timeHistogram 1133 1134 // stwTotalTimeGC/Other are distributions of stop-the-world total 1135 // latencies, defined as the total time from stopTheWorldWithSema to 1136 // startTheWorldWithSema. This is a superset of 1137 // stwStoppingTimeGC/Other. stwTotalTimeGC covers all GC-related STWs, 1138 // stwTotalTimeOther covers the others. 1139 stwTotalTimeGC timeHistogram 1140 stwTotalTimeOther timeHistogram 1141 1142 // totalRuntimeLockWaitTime (plus the value of lockWaitTime on each M in 1143 // allm) is the sum of time goroutines have spent in _Grunnable and with an 1144 // M, but waiting for locks within the runtime. This field stores the value 1145 // for Ms that have exited. 1146 totalRuntimeLockWaitTime atomic.Int64 1147 1148 // goroutinesCreated (plus the value of goroutinesCreated on each P in allp) 1149 // is the sum of all goroutines created by the program. 1150 goroutinesCreated atomic.Uint64 1151 } 1152 1153 // gcWaitingWord is an atomic boolean, whose address is also available as a 1154 // *uint32 for futexsleep. 1155 type gcWaitingWord struct { 1156 val uint32 1157 } 1158 1159 //go:nosplit 1160 func (p *gcWaitingWord) Store(v bool) { 1161 var val uint32 1162 if v { 1163 val = 1 1164 } 1165 atomic.Store(&p.val, val) 1166 } 1167 1168 //go:nosplit 1169 func (p *gcWaitingWord) Load() bool { 1170 return atomic.Load(&p.val) != 0 1171 } 1172 1173 //go:nosplit 1174 func (p *gcWaitingWord) Addr() *uint32 { 1175 return &p.val 1176 } 1177 1178 // Values for the flags field of a sigTabT. 1179 const ( 1180 _SigNotify = 1 << iota // let signal.Notify have signal, even if from kernel 1181 _SigKill // if signal.Notify doesn't take it, exit quietly 1182 _SigThrow // if signal.Notify doesn't take it, exit loudly 1183 _SigPanic // if the signal is from the kernel, panic 1184 _SigDefault // if the signal isn't explicitly requested, don't monitor it 1185 _SigGoExit // cause all runtime procs to exit (only used on Plan 9). 1186 _SigSetStack // Don't explicitly install handler, but add SA_ONSTACK to existing libc handler 1187 _SigUnblock // always unblock; see blockableSig 1188 _SigIgn // _SIG_DFL action is to ignore the signal 1189 ) 1190 1191 // Layout of in-memory per-function information prepared by linker 1192 // See https://golang.org/s/go12symtab. 1193 // Keep in sync with linker (../cmd/link/internal/ld/pcln.go:/pclntab) 1194 // and with package debug/gosym and with symtab.go in package runtime. 1195 type _func struct { 1196 sys.NotInHeap // Only in static data 1197 1198 entryOff uint32 // start pc, as offset from moduledata.text 1199 nameOff int32 // function name, as index into moduledata.funcnametab. 1200 1201 args int32 // in/out args size 1202 deferreturn uint32 // offset of start of a deferreturn call instruction from entry, if any. 1203 1204 pcsp uint32 1205 pcfile uint32 1206 pcln uint32 1207 npcdata uint32 1208 cuOffset uint32 // runtime.cutab offset of this function's CU 1209 startLine int32 // line number of start of function (func keyword/TEXT directive) 1210 funcID abi.FuncID // set for certain special runtime functions 1211 flag abi.FuncFlag 1212 _ [1]byte // pad 1213 nfuncdata uint8 // must be last, must end on a uint32-aligned boundary 1214 1215 // The end of the struct is followed immediately by two variable-length 1216 // arrays that reference the pcdata and funcdata locations for this 1217 // function. 1218 1219 // pcdata contains the offset into moduledata.pctab for the start of 1220 // that index's table. e.g., 1221 // &moduledata.pctab[_func.pcdata[_PCDATA_UnsafePoint]] is the start of 1222 // the unsafe point table. 1223 // 1224 // An offset of 0 indicates that there is no table. 1225 // 1226 // pcdata [npcdata]uint32 1227 1228 // funcdata contains the offset past moduledata.gofunc which contains a 1229 // pointer to that index's funcdata. e.g., 1230 // *(moduledata.gofunc + _func.funcdata[_FUNCDATA_ArgsPointerMaps]) is 1231 // the argument pointer map. 1232 // 1233 // An offset of ^uint32(0) indicates that there is no entry. 1234 // 1235 // funcdata [nfuncdata]uint32 1236 } 1237 1238 // Pseudo-Func that is returned for PCs that occur in inlined code. 1239 // A *Func can be either a *_func or a *funcinl, and they are distinguished 1240 // by the first uintptr. 1241 // 1242 // TODO(austin): Can we merge this with inlinedCall? 1243 type funcinl struct { 1244 ones uint32 // set to ^0 to distinguish from _func 1245 entry uintptr // entry of the real (the "outermost") frame 1246 name string 1247 file string 1248 line int32 1249 startLine int32 1250 } 1251 1252 type itab = abi.ITab 1253 1254 // Lock-free stack node. 1255 // Also known to export_test.go. 1256 type lfnode struct { 1257 next uint64 1258 pushcnt uintptr 1259 } 1260 1261 type forcegcstate struct { 1262 lock mutex 1263 g *g 1264 idle atomic.Bool 1265 } 1266 1267 // A _defer holds an entry on the list of deferred calls. 1268 // If you add a field here, add code to clear it in deferProcStack. 1269 // This struct must match the code in cmd/compile/internal/ssagen/ssa.go:deferstruct 1270 // and cmd/compile/internal/ssagen/ssa.go:(*state).call. 1271 // Some defers will be allocated on the stack and some on the heap. 1272 // All defers are logically part of the stack, so write barriers to 1273 // initialize them are not required. All defers must be manually scanned, 1274 // and for heap defers, marked. 1275 type _defer struct { 1276 heap bool 1277 rangefunc bool // true for rangefunc list 1278 sp uintptr // sp at time of defer 1279 pc uintptr // pc at time of defer 1280 fn func() // can be nil for open-coded defers 1281 link *_defer // next defer on G; can point to either heap or stack! 1282 1283 // If rangefunc is true, *head is the head of the atomic linked list 1284 // during a range-over-func execution. 1285 head *atomic.Pointer[_defer] 1286 } 1287 1288 // A _panic holds information about an active panic. 1289 // 1290 // A _panic value must only ever live on the stack. 1291 // 1292 // The link, deferBitsPtr, and slotsPtr fields are stack pointers, but 1293 // don't need special handling during stack growth: because they are 1294 // pointer-typed and _panic values only live on the stack, regular stack 1295 // pointer adjustment takes care of them. 1296 // 1297 // startSP, sp, and fp record frame boundaries. These are stackOffs rather 1298 // than pointers because a frame boundary is often also the address of a 1299 // stack object (for example, on amd64, a caller's SP is the address of the 1300 // stack arguments it passes to its callee), and a pointer would keep that 1301 // object alive after it is dead. See go.dev/issue/82394 and 1302 // go.dev/issue/82917. 1303 type _panic struct { 1304 arg any // argument to panic 1305 link *_panic // link to earlier panic 1306 1307 // startPC and startSP track where _panic.start was called. 1308 // (These are the SP and PC of the gopanic frame itself.) 1309 startPC uintptr 1310 startSP stackOff 1311 1312 // The current stack frame that we're running deferred calls for. 1313 pc uintptr 1314 sp stackOff 1315 fp stackOff 1316 1317 // retpc stores the PC where the panic should jump back to, if the 1318 // function last returned by _panic.nextDefer() recovers the panic. 1319 retpc uintptr 1320 1321 // Extra state for handling open-coded defers. 1322 deferBitsPtr *uint8 1323 slotsPtr unsafe.Pointer 1324 1325 recoverAtToken *recoverAtToken // identify this panic to gorecoverat 1326 1327 recovered bool // whether this panic has been recovered 1328 repanicked bool // whether this panic repanicked 1329 goexit bool 1330 deferreturn bool 1331 } 1332 1333 // savedOpenDeferState tracks the extra state from _panic that's 1334 // necessary for deferreturn to pick up where gopanic left off, 1335 // without needing to unwind the stack. 1336 type savedOpenDeferState struct { 1337 retpc uintptr 1338 deferBitsOffset uintptr 1339 slotsOffset uintptr 1340 } 1341 1342 // ancestorInfo records details of where a goroutine was started. 1343 type ancestorInfo struct { 1344 pcs []uintptr // pcs from the stack of this goroutine 1345 goid uint64 // goroutine id of this goroutine; original goroutine possibly dead 1346 gopc uintptr // pc of go statement that created this goroutine 1347 } 1348 1349 // A waitReason explains why a goroutine has been stopped. 1350 // See gopark. Do not re-use waitReasons, add new ones. 1351 type waitReason uint8 1352 1353 const ( 1354 waitReasonZero waitReason = iota // "" 1355 waitReasonGCAssistMarking // "GC assist marking" 1356 waitReasonIOWait // "IO wait" 1357 waitReasonDumpingHeap // "dumping heap" 1358 waitReasonGarbageCollection // "garbage collection" 1359 waitReasonGarbageCollectionScan // "garbage collection scan" 1360 waitReasonPanicWait // "panicwait" 1361 waitReasonGCAssistWait // "GC assist wait" 1362 waitReasonGCSweepWait // "GC sweep wait" 1363 waitReasonGCScavengeWait // "GC scavenge wait" 1364 waitReasonFinalizerWait // "finalizer wait" 1365 waitReasonForceGCIdle // "force gc (idle)" 1366 waitReasonUpdateGOMAXPROCSIdle // "GOMAXPROCS updater (idle)" 1367 waitReasonSemacquire // "semacquire" 1368 waitReasonSleep // "sleep" 1369 waitReasonChanReceiveNilChan // "chan receive (nil chan)" 1370 waitReasonChanSendNilChan // "chan send (nil chan)" 1371 waitReasonSelectNoCases // "select (no cases)" 1372 waitReasonSelect // "select" 1373 waitReasonChanReceive // "chan receive" 1374 waitReasonChanSend // "chan send" 1375 waitReasonSyncCondWait // "sync.Cond.Wait" 1376 waitReasonSyncMutexLock // "sync.Mutex.Lock" 1377 waitReasonSyncRWMutexRLock // "sync.RWMutex.RLock" 1378 waitReasonSyncRWMutexLock // "sync.RWMutex.Lock" 1379 waitReasonSyncWaitGroupWait // "sync.WaitGroup.Wait" 1380 waitReasonTraceReaderBlocked // "trace reader (blocked)" 1381 waitReasonWaitForGCCycle // "wait for GC cycle" 1382 waitReasonGCWorkerIdle // "GC worker (idle)" 1383 waitReasonGCWorkerActive // "GC worker (active)" 1384 waitReasonPreempted // "preempted" 1385 waitReasonDebugCall // "debug call" 1386 waitReasonGCMarkTermination // "GC mark termination" 1387 waitReasonStoppingTheWorld // "stopping the world" 1388 waitReasonFlushProcCaches // "flushing proc caches" 1389 waitReasonTraceGoroutineStatus // "trace goroutine status" 1390 waitReasonTraceProcStatus // "trace proc status" 1391 waitReasonPageTraceFlush // "page trace flush" 1392 waitReasonCoroutine // "coroutine" 1393 waitReasonGCWeakToStrongWait // "GC weak to strong wait" 1394 waitReasonSynctestRun // "synctest.Run" 1395 waitReasonSynctestWait // "synctest.Wait" 1396 waitReasonSynctestChanReceive // "chan receive (durable)" 1397 waitReasonSynctestChanSend // "chan send (durable)" 1398 waitReasonSynctestSelect // "select (durable)" 1399 waitReasonSynctestWaitGroupWait // "sync.WaitGroup.Wait (durable)" 1400 waitReasonCleanupWait // "cleanup wait" 1401 ) 1402 1403 var waitReasonStrings = [...]string{ 1404 waitReasonZero: "", 1405 waitReasonGCAssistMarking: "GC assist marking", 1406 waitReasonIOWait: "IO wait", 1407 waitReasonChanReceiveNilChan: "chan receive (nil chan)", 1408 waitReasonChanSendNilChan: "chan send (nil chan)", 1409 waitReasonDumpingHeap: "dumping heap", 1410 waitReasonGarbageCollection: "garbage collection", 1411 waitReasonGarbageCollectionScan: "garbage collection scan", 1412 waitReasonPanicWait: "panicwait", 1413 waitReasonSelect: "select", 1414 waitReasonSelectNoCases: "select (no cases)", 1415 waitReasonGCAssistWait: "GC assist wait", 1416 waitReasonGCSweepWait: "GC sweep wait", 1417 waitReasonGCScavengeWait: "GC scavenge wait", 1418 waitReasonChanReceive: "chan receive", 1419 waitReasonChanSend: "chan send", 1420 waitReasonFinalizerWait: "finalizer wait", 1421 waitReasonForceGCIdle: "force gc (idle)", 1422 waitReasonUpdateGOMAXPROCSIdle: "GOMAXPROCS updater (idle)", 1423 waitReasonSemacquire: "semacquire", 1424 waitReasonSleep: "sleep", 1425 waitReasonSyncCondWait: "sync.Cond.Wait", 1426 waitReasonSyncMutexLock: "sync.Mutex.Lock", 1427 waitReasonSyncRWMutexRLock: "sync.RWMutex.RLock", 1428 waitReasonSyncRWMutexLock: "sync.RWMutex.Lock", 1429 waitReasonSyncWaitGroupWait: "sync.WaitGroup.Wait", 1430 waitReasonTraceReaderBlocked: "trace reader (blocked)", 1431 waitReasonWaitForGCCycle: "wait for GC cycle", 1432 waitReasonGCWorkerIdle: "GC worker (idle)", 1433 waitReasonGCWorkerActive: "GC worker (active)", 1434 waitReasonPreempted: "preempted", 1435 waitReasonDebugCall: "debug call", 1436 waitReasonGCMarkTermination: "GC mark termination", 1437 waitReasonStoppingTheWorld: "stopping the world", 1438 waitReasonFlushProcCaches: "flushing proc caches", 1439 waitReasonTraceGoroutineStatus: "trace goroutine status", 1440 waitReasonTraceProcStatus: "trace proc status", 1441 waitReasonPageTraceFlush: "page trace flush", 1442 waitReasonCoroutine: "coroutine", 1443 waitReasonGCWeakToStrongWait: "GC weak to strong wait", 1444 waitReasonSynctestRun: "synctest.Run", 1445 waitReasonSynctestWait: "synctest.Wait", 1446 waitReasonSynctestChanReceive: "chan receive (durable)", 1447 waitReasonSynctestChanSend: "chan send (durable)", 1448 waitReasonSynctestSelect: "select (durable)", 1449 waitReasonSynctestWaitGroupWait: "sync.WaitGroup.Wait (durable)", 1450 waitReasonCleanupWait: "cleanup wait", 1451 } 1452 1453 func (w waitReason) String() string { 1454 if w < 0 || w >= waitReason(len(waitReasonStrings)) { 1455 return "unknown wait reason" 1456 } 1457 return waitReasonStrings[w] 1458 } 1459 1460 // isMutexWait returns true if the goroutine is blocked because of 1461 // sync.Mutex.Lock or sync.RWMutex.[R]Lock. 1462 // 1463 //go:nosplit 1464 func (w waitReason) isMutexWait() bool { 1465 return w == waitReasonSyncMutexLock || 1466 w == waitReasonSyncRWMutexRLock || 1467 w == waitReasonSyncRWMutexLock 1468 } 1469 1470 // isSyncWait returns true if the goroutine is blocked because of 1471 // sync library primitive operations. 1472 // 1473 //go:nosplit 1474 func (w waitReason) isSyncWait() bool { 1475 return waitReasonSyncCondWait <= w && w <= waitReasonSyncWaitGroupWait 1476 } 1477 1478 // isChanWait is true if the goroutine is blocked because of non-nil 1479 // channel operations or a select statement with at least one case. 1480 // 1481 //go:nosplit 1482 func (w waitReason) isChanWait() bool { 1483 return w == waitReasonSelect || 1484 w == waitReasonChanReceive || 1485 w == waitReasonChanSend 1486 } 1487 1488 func (w waitReason) isWaitingForSuspendG() bool { 1489 return isWaitingForSuspendG[w] 1490 } 1491 1492 // isWaitingForSuspendG indicates that a goroutine is only entering _Gwaiting and 1493 // setting a waitReason because it needs to be able to let the suspendG 1494 // (used by the GC and the execution tracer) take ownership of its stack. 1495 // The G is always actually executing on the system stack in these cases. 1496 // 1497 // TODO(mknyszek): Consider replacing this with a new dedicated G status. 1498 var isWaitingForSuspendG = [len(waitReasonStrings)]bool{ 1499 waitReasonStoppingTheWorld: true, 1500 waitReasonGCMarkTermination: true, 1501 waitReasonGarbageCollection: true, 1502 waitReasonGarbageCollectionScan: true, 1503 waitReasonTraceGoroutineStatus: true, 1504 waitReasonTraceProcStatus: true, 1505 waitReasonPageTraceFlush: true, 1506 waitReasonGCAssistMarking: true, 1507 waitReasonGCWorkerActive: true, 1508 waitReasonFlushProcCaches: true, 1509 } 1510 1511 func (w waitReason) isIdleInSynctest() bool { 1512 return isIdleInSynctest[w] 1513 } 1514 1515 // isIdleInSynctest indicates that a goroutine is considered idle by synctest.Wait. 1516 var isIdleInSynctest = [len(waitReasonStrings)]bool{ 1517 waitReasonChanReceiveNilChan: true, 1518 waitReasonChanSendNilChan: true, 1519 waitReasonSelectNoCases: true, 1520 waitReasonSleep: true, 1521 waitReasonSyncCondWait: true, 1522 waitReasonSynctestWaitGroupWait: true, 1523 waitReasonCoroutine: true, 1524 waitReasonSynctestRun: true, 1525 waitReasonSynctestWait: true, 1526 waitReasonSynctestChanReceive: true, 1527 waitReasonSynctestChanSend: true, 1528 waitReasonSynctestSelect: true, 1529 } 1530 1531 var ( 1532 // Linked-list of all Ms. Written under sched.lock, read atomically. 1533 allm *m 1534 1535 gomaxprocs int32 1536 numCPUStartup int32 1537 forcegc forcegcstate 1538 sched schedt 1539 newprocs int32 1540 ) 1541 1542 var ( 1543 // allpLock protects P-less reads and size changes of allp, idlepMask, 1544 // and timerpMask, and all writes to allp. 1545 allpLock mutex 1546 1547 // len(allp) == gomaxprocs; may change at safe points, otherwise 1548 // immutable. 1549 allp []*p 1550 1551 // Bitmask of Ps in _Pidle list, one bit per P. Reads and writes must 1552 // be atomic. Length may change at safe points. 1553 // 1554 // Each P must update only its own bit. In order to maintain 1555 // consistency, a P going idle must set the idle mask simultaneously with 1556 // updates to the idle P list under the sched.lock, otherwise a racing 1557 // pidleget may clear the mask before pidleput sets the mask, 1558 // corrupting the bitmap. 1559 // 1560 // N.B., procresize takes ownership of all Ps in stopTheWorldWithSema. 1561 idlepMask pMask 1562 1563 // Bitmask of Ps that may have a timer, one bit per P. Reads and writes 1564 // must be atomic. Length may change at safe points. 1565 // 1566 // Ideally, the timer mask would be kept immediately consistent on any timer 1567 // operations. Unfortunately, updating a shared global data structure in the 1568 // timer hot path adds too much overhead in applications frequently switching 1569 // between no timers and some timers. 1570 // 1571 // As a compromise, the timer mask is updated only on pidleget / pidleput. A 1572 // running P (returned by pidleget) may add a timer at any time, so its mask 1573 // must be set. An idle P (passed to pidleput) cannot add new timers while 1574 // idle, so if it has no timers at that time, its mask may be cleared. 1575 // 1576 // Thus, we get the following effects on timer-stealing in findRunnable: 1577 // 1578 // - Idle Ps with no timers when they go idle are never checked in findRunnable 1579 // (for work- or timer-stealing; this is the ideal case). 1580 // - Running Ps must always be checked. 1581 // - Idle Ps whose timers are stolen must continue to be checked until they run 1582 // again, even after timer expiration. 1583 // 1584 // When the P starts running again, the mask should be set, as a timer may be 1585 // added at any time. 1586 // 1587 // TODO(prattmic): Additional targeted updates may improve the above cases. 1588 // e.g., updating the mask when stealing a timer. 1589 timerpMask pMask 1590 ) 1591 1592 // goarmsoftfp is used by runtime/cgo assembly. 1593 // 1594 //go:linkname goarmsoftfp 1595 1596 var ( 1597 // Pool of GC parked background workers. Entries are type 1598 // *gcBgMarkWorkerNode. 1599 gcBgMarkWorkerPool lfstack 1600 1601 // Total number of gcBgMarkWorker goroutines. Protected by worldsema. 1602 gcBgMarkWorkerCount int32 1603 1604 // Information about what cpu features are available. 1605 // Packages outside the runtime should not use these 1606 // as they are not an external api. 1607 // Set on startup in asm_{386,amd64}.s 1608 processorVersionInfo uint32 1609 isIntel bool 1610 ) 1611 1612 // set by cmd/link on arm systems 1613 // accessed using linkname by internal/runtime/atomic. 1614 // 1615 // goarm should be an internal detail, 1616 // but widely used packages access it using linkname. 1617 // Notable members of the hall of shame include: 1618 // - github.com/creativeprojects/go-selfupdate 1619 // 1620 // Do not remove or change the type signature. 1621 // See go.dev/issue/67401. 1622 // 1623 //go:linkname goarm 1624 var ( 1625 goarm uint8 1626 goarmsoftfp uint8 1627 ) 1628 1629 // Set by the linker so the runtime can determine the buildmode. 1630 var ( 1631 islibrary bool // -buildmode=c-shared 1632 isarchive bool // -buildmode=c-archive 1633 ) 1634 1635 // Must agree with internal/buildcfg.FramePointerEnabled. 1636 const framepointer_enabled = GOARCH == "amd64" || GOARCH == "arm64" 1637 1638 // getcallerfp returns the frame pointer of the caller of the caller 1639 // of this function. 1640 // 1641 //go:nosplit 1642 //go:noinline 1643 func getcallerfp() uintptr { 1644 fp := getfp() // This frame's FP. 1645 if fp != 0 { 1646 fp = *(*uintptr)(unsafe.Pointer(fp)) // The caller's FP. 1647 fp = *(*uintptr)(unsafe.Pointer(fp)) // The caller's caller's FP. 1648 } 1649 return fp 1650 } 1651