rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/runtime/runtime2.go (about) 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 "unsafe" 8 9 /* 10 * defined constants 11 */ 12 const ( 13 // G status 14 // 15 // If you add to this list, add to the list 16 // of "okay during garbage collection" status 17 // in mgcmark.go too. 18 _Gidle = iota // 0 19 _Grunnable // 1 runnable and on a run queue 20 _Grunning // 2 21 _Gsyscall // 3 22 _Gwaiting // 4 23 _Gmoribund_unused // 5 currently unused, but hardcoded in gdb scripts 24 _Gdead // 6 25 _Genqueue // 7 Only the Gscanenqueue is used. 26 _Gcopystack // 8 in this state when newstack is moving the stack 27 // the following encode that the GC is scanning the stack and what to do when it is done 28 _Gscan = 0x1000 // atomicstatus&~Gscan = the non-scan state, 29 // _Gscanidle = _Gscan + _Gidle, // Not used. Gidle only used with newly malloced gs 30 _Gscanrunnable = _Gscan + _Grunnable // 0x1001 When scanning complets make Grunnable (it is already on run queue) 31 _Gscanrunning = _Gscan + _Grunning // 0x1002 Used to tell preemption newstack routine to scan preempted stack. 32 _Gscansyscall = _Gscan + _Gsyscall // 0x1003 When scanning completes make is Gsyscall 33 _Gscanwaiting = _Gscan + _Gwaiting // 0x1004 When scanning completes make it Gwaiting 34 // _Gscanmoribund_unused, // not possible 35 // _Gscandead, // not possible 36 _Gscanenqueue = _Gscan + _Genqueue // When scanning completes make it Grunnable and put on runqueue 37 ) 38 39 const ( 40 // P status 41 _Pidle = iota 42 _Prunning 43 _Psyscall 44 _Pgcstop 45 _Pdead 46 ) 47 48 // The next line makes 'go generate' write the zgen_*.go files with 49 // per-OS and per-arch information, including constants 50 // named goos_$GOOS and goarch_$GOARCH for every 51 // known GOOS and GOARCH. The constant is 1 on the 52 // current system, 0 otherwise; multiplying by them is 53 // useful for defining GOOS- or GOARCH-specific constants. 54 //go:generate go run gengoos.go 55 56 type mutex struct { 57 // Futex-based impl treats it as uint32 key, 58 // while sema-based impl as M* waitm. 59 // Used to be a union, but unions break precise GC. 60 key uintptr 61 } 62 63 type note struct { 64 // Futex-based impl treats it as uint32 key, 65 // while sema-based impl as M* waitm. 66 // Used to be a union, but unions break precise GC. 67 key uintptr 68 } 69 70 type _string struct { 71 str *byte 72 len int 73 } 74 75 type funcval struct { 76 fn uintptr 77 // variable-size, fn-specific data here 78 } 79 80 type iface struct { 81 tab *itab 82 data unsafe.Pointer 83 } 84 85 type eface struct { 86 _type *_type 87 data unsafe.Pointer 88 } 89 90 // A guintptr holds a goroutine pointer, but typed as a uintptr 91 // to bypass write barriers. It is used in the Gobuf goroutine state. 92 // 93 // The Gobuf.g goroutine pointer is almost always updated by assembly code. 94 // In one of the few places it is updated by Go code - func save - it must be 95 // treated as a uintptr to avoid a write barrier being emitted at a bad time. 96 // Instead of figuring out how to emit the write barriers missing in the 97 // assembly manipulation, we change the type of the field to uintptr, 98 // so that it does not require write barriers at all. 99 // 100 // Goroutine structs are published in the allg list and never freed. 101 // That will keep the goroutine structs from being collected. 102 // There is never a time that Gobuf.g's contain the only references 103 // to a goroutine: the publishing of the goroutine in allg comes first. 104 // Goroutine pointers are also kept in non-GC-visible places like TLS, 105 // so I can't see them ever moving. If we did want to start moving data 106 // in the GC, we'd need to allocate the goroutine structs from an 107 // alternate arena. Using guintptr doesn't make that problem any worse. 108 type guintptr uintptr 109 110 func (gp guintptr) ptr() *g { 111 return (*g)(unsafe.Pointer(gp)) 112 } 113 114 // ps, ms, gs, and mcache are structures that must be manipulated at a level 115 // lower than that of the normal Go language. For example the routine that 116 // stops the world removes the p from the m structure informing the GC that 117 // this P is stopped and then it moves the g to the global runnable queue. 118 // If write barriers were allowed to happen at this point not only does 119 // the GC think the thread is stopped but the underlying structures 120 // like a p or m are not in a state that is not coherent enough to 121 // support the write barrier actions. 122 // This is particularly painful since a partially executed write barrier 123 // may mark the object but be delinquent in informing the GC that the 124 // object needs to be scanned. 125 126 // setGNoWriteBarriers does *gdst = gval without a write barrier. 127 func setGNoWriteBarrier(gdst **g, gval *g) { 128 *(*uintptr)(unsafe.Pointer(gdst)) = uintptr(unsafe.Pointer(gval)) 129 } 130 131 // setMNoWriteBarriers does *mdst = mval without a write barrier. 132 func setMNoWriteBarrier(mdst **m, mval *m) { 133 *(*uintptr)(unsafe.Pointer(mdst)) = uintptr(unsafe.Pointer(mval)) 134 } 135 136 // setPNoWriteBarriers does *pdst = pval without a write barrier. 137 func setPNoWriteBarrier(pdst **p, pval *p) { 138 *(*uintptr)(unsafe.Pointer(pdst)) = uintptr(unsafe.Pointer(pval)) 139 } 140 141 // setMcacheNoWriteBarriers does *mcachedst = mcacheval without a write barrier. 142 func setMcacheNoWriteBarrier(mcachedst **mcache, mcacheval *mcache) { 143 *(*uintptr)(unsafe.Pointer(mcachedst)) = uintptr(unsafe.Pointer(mcacheval)) 144 } 145 146 type gobuf struct { 147 // The offsets of sp, pc, and g are known to (hard-coded in) libmach. 148 sp uintptr 149 pc uintptr 150 g guintptr 151 ctxt unsafe.Pointer // this has to be a pointer so that gc scans it 152 ret uintreg 153 lr uintptr 154 bp uintptr // for GOEXPERIMENT=framepointer 155 } 156 157 // Known to compiler. 158 // Changes here must also be made in src/cmd/internal/gc/select.go's selecttype. 159 type sudog struct { 160 g *g 161 selectdone *uint32 162 next *sudog 163 prev *sudog 164 elem unsafe.Pointer // data element 165 releasetime int64 166 nrelease int32 // -1 for acquire 167 waitlink *sudog // g.waiting list 168 } 169 170 type gcstats struct { 171 // the struct must consist of only uint64's, 172 // because it is casted to uint64[]. 173 nhandoff uint64 174 nhandoffcnt uint64 175 nprocyield uint64 176 nosyield uint64 177 nsleep uint64 178 } 179 180 type libcall struct { 181 fn uintptr 182 n uintptr // number of parameters 183 args uintptr // parameters 184 r1 uintptr // return values 185 r2 uintptr 186 err uintptr // error number 187 } 188 189 // describes how to handle callback 190 type wincallbackcontext struct { 191 gobody unsafe.Pointer // go function to call 192 argsize uintptr // callback arguments size (in bytes) 193 restorestack uintptr // adjust stack on return by (in bytes) (386 only) 194 cleanstack bool 195 } 196 197 // Stack describes a Go execution stack. 198 // The bounds of the stack are exactly [lo, hi), 199 // with no implicit data structures on either side. 200 type stack struct { 201 lo uintptr 202 hi uintptr 203 } 204 205 type g struct { 206 // Stack parameters. 207 // stack describes the actual stack memory: [stack.lo, stack.hi). 208 // stackguard0 is the stack pointer compared in the Go stack growth prologue. 209 // It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption. 210 // stackguard1 is the stack pointer compared in the C stack growth prologue. 211 // It is stack.lo+StackGuard on g0 and gsignal stacks. 212 // It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash). 213 stack stack // offset known to runtime/cgo 214 stackguard0 uintptr // offset known to liblink 215 stackguard1 uintptr // offset known to liblink 216 217 _panic *_panic // innermost panic - offset known to liblink 218 _defer *_defer // innermost defer 219 sched gobuf 220 syscallsp uintptr // if status==gsyscall, syscallsp = sched.sp to use during gc 221 syscallpc uintptr // if status==gsyscall, syscallpc = sched.pc to use during gc 222 param unsafe.Pointer // passed parameter on wakeup 223 atomicstatus uint32 224 goid int64 225 waitsince int64 // approx time when the g become blocked 226 waitreason string // if status==gwaiting 227 schedlink *g 228 preempt bool // preemption signal, duplicates stackguard0 = stackpreempt 229 paniconfault bool // panic (instead of crash) on unexpected fault address 230 preemptscan bool // preempted g does scan for gc 231 gcworkdone bool // debug: cleared at begining of gc work phase cycle, set by gcphasework, tested at end of cycle 232 gcscanvalid bool // false at start of gc cycle, true if G has not run since last scan 233 throwsplit bool // must not split stack 234 raceignore int8 // ignore race detection events 235 m *m // for debuggers, but offset not hard-coded 236 lockedm *m 237 sig uint32 238 writebuf []byte 239 sigcode0 uintptr 240 sigcode1 uintptr 241 sigpc uintptr 242 gopc uintptr // pc of go statement that created this goroutine 243 startpc uintptr // pc of goroutine function 244 racectx uintptr 245 waiting *sudog // sudog structures this g is waiting on (that have a valid elem ptr) 246 } 247 248 type mts struct { 249 tv_sec int64 250 tv_nsec int64 251 } 252 253 type mscratch struct { 254 v [6]uintptr 255 } 256 257 type m struct { 258 g0 *g // goroutine with scheduling stack 259 morebuf gobuf // gobuf arg to morestack 260 261 // Fields not known to debuggers. 262 procid uint64 // for debuggers, but offset not hard-coded 263 gsignal *g // signal-handling g 264 tls [4]uintptr // thread-local storage (for x86 extern register) 265 mstartfn uintptr // TODO: type as func(); note: this is a non-heap allocated func() 266 curg *g // current running goroutine 267 caughtsig *g // goroutine running during fatal signal 268 p *p // attached p for executing go code (nil if not executing go code) 269 nextp *p 270 id int32 271 mallocing int32 272 throwing int32 273 preemptoff string // if != "", keep curg running on this m 274 locks int32 275 softfloat int32 276 dying int32 277 profilehz int32 278 helpgc int32 279 spinning bool // m is out of work and is actively looking for work 280 blocked bool // m is blocked on a note 281 inwb bool // m is executing a write barrier 282 printlock int8 283 fastrand uint32 284 ncgocall uint64 // number of cgo calls in total 285 ncgo int32 // number of cgo calls currently in progress 286 cgomal *cgomal 287 park note 288 alllink *m // on allm 289 schedlink *m 290 machport uint32 // return address for mach ipc (os x) 291 mcache *mcache 292 lockedg *g 293 createstack [32]uintptr // stack that created this thread. 294 freglo [16]uint32 // d[i] lsb and f[i] 295 freghi [16]uint32 // d[i] msb and f[i+16] 296 fflag uint32 // floating point compare flags 297 locked uint32 // tracking for lockosthread 298 nextwaitm uintptr // next m waiting for lock 299 waitsema uintptr // semaphore for parking on locks 300 waitsemacount uint32 301 waitsemalock uint32 302 gcstats gcstats 303 currentwbuf uintptr // use locks or atomic operations such as xchguinptr to access. 304 needextram bool 305 traceback uint8 306 waitunlockf unsafe.Pointer // todo go func(*g, unsafe.pointer) bool 307 waitlock unsafe.Pointer 308 waittraceev byte 309 waittraceskip int 310 syscalltick uint32 311 //#ifdef GOOS_windows 312 thread uintptr // thread handle 313 // these are here because they are too large to be on the stack 314 // of low-level NOSPLIT functions. 315 libcall libcall 316 libcallpc uintptr // for cpu profiler 317 libcallsp uintptr 318 libcallg *g 319 //#endif 320 //#ifdef GOOS_solaris 321 perrno *int32 // pointer to tls errno 322 // these are here because they are too large to be on the stack 323 // of low-level NOSPLIT functions. 324 //LibCall libcall; 325 ts mts 326 scratch mscratch 327 //#endif 328 //#ifdef GOOS_plan9 329 notesig *int8 330 errstr *byte 331 //#endif 332 } 333 334 type p struct { 335 lock mutex 336 337 id int32 338 status uint32 // one of pidle/prunning/... 339 link *p 340 schedtick uint32 // incremented on every scheduler call 341 syscalltick uint32 // incremented on every system call 342 m *m // back-link to associated m (nil if idle) 343 mcache *mcache 344 345 deferpool [5][]*_defer // pool of available defer structs of different sizes (see panic.go) 346 deferpoolbuf [5][32]*_defer 347 348 // Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen. 349 goidcache uint64 350 goidcacheend uint64 351 352 // Queue of runnable goroutines. 353 runqhead uint32 354 runqtail uint32 355 runq [256]*g 356 357 // Available G's (status == Gdead) 358 gfree *g 359 gfreecnt int32 360 361 sudogcache []*sudog 362 sudogbuf [128]*sudog 363 364 tracebuf *traceBuf 365 366 palloc persistentAlloc // per-P to avoid mutex 367 368 pad [64]byte 369 } 370 371 const ( 372 // The max value of GOMAXPROCS. 373 // There are no fundamental restrictions on the value. 374 _MaxGomaxprocs = 1 << 8 375 ) 376 377 type schedt struct { 378 lock mutex 379 380 goidgen uint64 381 382 midle *m // idle m's waiting for work 383 nmidle int32 // number of idle m's waiting for work 384 nmidlelocked int32 // number of locked m's waiting for work 385 mcount int32 // number of m's that have been created 386 maxmcount int32 // maximum number of m's allowed (or die) 387 388 pidle *p // idle p's 389 npidle uint32 390 nmspinning uint32 391 392 // Global runnable queue. 393 runqhead *g 394 runqtail *g 395 runqsize int32 396 397 // Global cache of dead G's. 398 gflock mutex 399 gfree *g 400 ngfree int32 401 402 // Central cache of sudog structs. 403 sudoglock mutex 404 sudogcache *sudog 405 406 // Central pool of available defer structs of different sizes. 407 deferlock mutex 408 deferpool [5]*_defer 409 410 gcwaiting uint32 // gc is waiting to run 411 stopwait int32 412 stopnote note 413 sysmonwait uint32 414 sysmonnote note 415 lastpoll uint64 416 417 profilehz int32 // cpu profiling rate 418 419 procresizetime int64 // nanotime() of last change to gomaxprocs 420 totaltime int64 // ∫gomaxprocs dt up to procresizetime 421 } 422 423 // The m->locked word holds two pieces of state counting active calls to LockOSThread/lockOSThread. 424 // The low bit (LockExternal) is a boolean reporting whether any LockOSThread call is active. 425 // External locks are not recursive; a second lock is silently ignored. 426 // The upper bits of m->lockedcount record the nesting depth of calls to lockOSThread 427 // (counting up by LockInternal), popped by unlockOSThread (counting down by LockInternal). 428 // Internal locks can be recursive. For instance, a lock for cgo can occur while the main 429 // goroutine is holding the lock during the initialization phase. 430 const ( 431 _LockExternal = 1 432 _LockInternal = 2 433 ) 434 435 type sigtabtt struct { 436 flags int32 437 name *int8 438 } 439 440 const ( 441 _SigNotify = 1 << 0 // let signal.Notify have signal, even if from kernel 442 _SigKill = 1 << 1 // if signal.Notify doesn't take it, exit quietly 443 _SigThrow = 1 << 2 // if signal.Notify doesn't take it, exit loudly 444 _SigPanic = 1 << 3 // if the signal is from the kernel, panic 445 _SigDefault = 1 << 4 // if the signal isn't explicitly requested, don't monitor it 446 _SigHandling = 1 << 5 // our signal handler is registered 447 _SigIgnored = 1 << 6 // the signal was ignored before we registered for it 448 _SigGoExit = 1 << 7 // cause all runtime procs to exit (only used on Plan 9). 449 _SigSetStack = 1 << 8 // add SA_ONSTACK to libc handler 450 ) 451 452 // Layout of in-memory per-function information prepared by linker 453 // See http://golang.org/s/go12symtab. 454 // Keep in sync with linker 455 // and with package debug/gosym and with symtab.go in package runtime. 456 type _func struct { 457 entry uintptr // start pc 458 nameoff int32 // function name 459 460 args int32 // in/out args size 461 frame int32 // legacy frame size; use pcsp if possible 462 463 pcsp int32 464 pcfile int32 465 pcln int32 466 npcdata int32 467 nfuncdata int32 468 } 469 470 // layout of Itab known to compilers 471 // allocated in non-garbage-collected memory 472 type itab struct { 473 inter *interfacetype 474 _type *_type 475 link *itab 476 bad int32 477 unused int32 478 fun [1]uintptr // variable sized 479 } 480 481 // Lock-free stack node. 482 // // Also known to export_test.go. 483 type lfnode struct { 484 next uint64 485 pushcnt uintptr 486 } 487 488 // Track memory allocated by code not written in Go during a cgo call, 489 // so that the garbage collector can see them. 490 type cgomal struct { 491 next *cgomal 492 alloc unsafe.Pointer 493 } 494 495 // Indicates to write barrier and sychronization task to preform. 496 const ( 497 _GCoff = iota // GC not running, write barrier disabled 498 _GCquiesce // unused state 499 _GCstw // unused state 500 _GCscan // GC collecting roots into workbufs, write barrier disabled 501 _GCmark // GC marking from workbufs, write barrier ENABLED 502 _GCmarktermination // GC mark termination: allocate black, P's help GC, write barrier ENABLED 503 _GCsweep // GC mark completed; sweeping in background, write barrier disabled 504 ) 505 506 type forcegcstate struct { 507 lock mutex 508 g *g 509 idle uint32 510 } 511 512 var gcphase uint32 513 514 /* 515 * known to compiler 516 */ 517 const ( 518 _Structrnd = regSize 519 ) 520 521 // startup_random_data holds random bytes initialized at startup. These come from 522 // the ELF AT_RANDOM auxiliary vector (vdso_linux_amd64.go or os_linux_386.go). 523 var startupRandomData []byte 524 525 // extendRandom extends the random numbers in r[:n] to the whole slice r. 526 // Treats n<0 as n==0. 527 func extendRandom(r []byte, n int) { 528 if n < 0 { 529 n = 0 530 } 531 for n < len(r) { 532 // Extend random bits using hash function & time seed 533 w := n 534 if w > 16 { 535 w = 16 536 } 537 h := memhash(unsafe.Pointer(&r[n-w]), uintptr(nanotime()), uintptr(w)) 538 for i := 0; i < ptrSize && n < len(r); i++ { 539 r[n] = byte(h) 540 n++ 541 h >>= 8 542 } 543 } 544 } 545 546 /* 547 * deferred subroutine calls 548 */ 549 type _defer struct { 550 siz int32 551 started bool 552 sp uintptr // sp at time of defer 553 pc uintptr 554 fn *funcval 555 _panic *_panic // panic that is running defer 556 link *_defer 557 } 558 559 /* 560 * panics 561 */ 562 type _panic struct { 563 argp unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink 564 arg interface{} // argument to panic 565 link *_panic // link to earlier panic 566 recovered bool // whether this panic is over 567 aborted bool // the panic was aborted 568 } 569 570 /* 571 * stack traces 572 */ 573 574 type stkframe struct { 575 fn *_func // function being run 576 pc uintptr // program counter within fn 577 continpc uintptr // program counter where execution can continue, or 0 if not 578 lr uintptr // program counter at caller aka link register 579 sp uintptr // stack pointer at pc 580 fp uintptr // stack pointer at caller aka frame pointer 581 varp uintptr // top of local variables 582 argp uintptr // pointer to function arguments 583 arglen uintptr // number of bytes at argp 584 argmap *bitvector // force use of this argmap 585 } 586 587 const ( 588 _TraceRuntimeFrames = 1 << 0 // include frames for internal runtime functions. 589 _TraceTrap = 1 << 1 // the initial PC, SP are from a trap, not a return PC from a call 590 ) 591 592 const ( 593 // The maximum number of frames we print for a traceback 594 _TracebackMaxFrames = 100 595 ) 596 597 var ( 598 emptystring string 599 allg **g 600 allglen uintptr 601 lastg *g 602 allm *m 603 allp [_MaxGomaxprocs + 1]*p 604 gomaxprocs int32 605 panicking uint32 606 goos *int8 607 ncpu int32 608 signote note 609 forcegc forcegcstate 610 sched schedt 611 newprocs int32 612 613 // Information about what cpu features are available. 614 // Set on startup in asm_{x86,amd64}.s. 615 cpuid_ecx uint32 616 cpuid_edx uint32 617 lfenceBeforeRdtsc bool 618 ) 619 620 // Set by the linker so the runtime can determine the buildmode. 621 var ( 622 islibrary bool // -buildmode=c-shared 623 isarchive bool // -buildmode=c-archive 624 ) 625 626 /* 627 * mutual exclusion locks. in the uncontended case, 628 * as fast as spin locks (just a few user-level instructions), 629 * but on the contention path they sleep in the kernel. 630 * a zeroed Mutex is unlocked (no need to initialize each lock). 631 */ 632 633 /* 634 * sleep and wakeup on one-time events. 635 * before any calls to notesleep or notewakeup, 636 * must call noteclear to initialize the Note. 637 * then, exactly one thread can call notesleep 638 * and exactly one thread can call notewakeup (once). 639 * once notewakeup has been called, the notesleep 640 * will return. future notesleep will return immediately. 641 * subsequent noteclear must be called only after 642 * previous notesleep has returned, e.g. it's disallowed 643 * to call noteclear straight after notewakeup. 644 * 645 * notetsleep is like notesleep but wakes up after 646 * a given number of nanoseconds even if the event 647 * has not yet happened. if a goroutine uses notetsleep to 648 * wake up early, it must wait to call noteclear until it 649 * can be sure that no other goroutine is calling 650 * notewakeup. 651 * 652 * notesleep/notetsleep are generally called on g0, 653 * notetsleepg is similar to notetsleep but is called on user g. 654 */ 655 // bool runtime·notetsleep(Note*, int64); // false - timeout 656 // bool runtime·notetsleepg(Note*, int64); // false - timeout 657 658 /* 659 * Lock-free stack. 660 * Initialize uint64 head to 0, compare with 0 to test for emptiness. 661 * The stack does not keep pointers to nodes, 662 * so they can be garbage collected if there are no other pointers to nodes. 663 */ 664 665 // for mmap, we only pass the lower 32 bits of file offset to the 666 // assembly routine; the higher bits (if required), should be provided 667 // by the assembly routine as 0.