github.com/MangoDowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/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 (
     8  	"runtime/internal/atomic"
     9  	"runtime/internal/sys"
    10  	"unsafe"
    11  )
    12  
    13  // defined constants
    14  const (
    15  	// G status
    16  	//
    17  	// Beyond indicating the general state of a G, the G status
    18  	// acts like a lock on the goroutine's stack (and hence its
    19  	// ability to execute user code).
    20  	//
    21  	// If you add to this list, add to the list
    22  	// of "okay during garbage collection" status
    23  	// in mgcmark.go too.
    24  
    25  	// _Gidle means this goroutine was just allocated and has not
    26  	// yet been initialized.
    27  	_Gidle = iota // 0
    28  
    29  	// _Grunnable means this goroutine is on a run queue. It is
    30  	// not currently executing user code. The stack is not owned.
    31  	_Grunnable // 1
    32  
    33  	// _Grunning means this goroutine may execute user code. The
    34  	// stack is owned by this goroutine. It is not on a run queue.
    35  	// It is assigned an M and a P.
    36  	_Grunning // 2
    37  
    38  	// _Gsyscall means this goroutine is executing a system call.
    39  	// It is not executing user code. The stack is owned by this
    40  	// goroutine. It is not on a run queue. It is assigned an M.
    41  	_Gsyscall // 3
    42  
    43  	// _Gwaiting means this goroutine is blocked in the runtime.
    44  	// It is not executing user code. It is not on a run queue,
    45  	// but should be recorded somewhere (e.g., a channel wait
    46  	// queue) so it can be ready()d when necessary. The stack is
    47  	// not owned *except* that a channel operation may read or
    48  	// write parts of the stack under the appropriate channel
    49  	// lock. Otherwise, it is not safe to access the stack after a
    50  	// goroutine enters _Gwaiting (e.g., it may get moved).
    51  	_Gwaiting // 4
    52  
    53  	// _Gmoribund_unused is currently unused, but hardcoded in gdb
    54  	// scripts.
    55  	_Gmoribund_unused // 5
    56  
    57  	// _Gdead means this goroutine is currently unused. It may be
    58  	// just exited, on a free list, or just being initialized. It
    59  	// is not executing user code. It may or may not have a stack
    60  	// allocated. The G and its stack (if any) are owned by the M
    61  	// that is exiting the G or that obtained the G from the free
    62  	// list.
    63  	_Gdead // 6
    64  
    65  	// _Genqueue_unused is currently unused.
    66  	_Genqueue_unused // 7
    67  
    68  	// _Gcopystack means this goroutine's stack is being moved. It
    69  	// is not executing user code and is not on a run queue. The
    70  	// stack is owned by the goroutine that put it in _Gcopystack.
    71  	_Gcopystack // 8
    72  
    73  	// _Gscan combined with one of the above states other than
    74  	// _Grunning indicates that GC is scanning the stack. The
    75  	// goroutine is not executing user code and the stack is owned
    76  	// by the goroutine that set the _Gscan bit.
    77  	//
    78  	// _Gscanrunning is different: it is used to briefly block
    79  	// state transitions while GC signals the G to scan its own
    80  	// stack. This is otherwise like _Grunning.
    81  	//
    82  	// atomicstatus&~Gscan gives the state the goroutine will
    83  	// return to when the scan completes.
    84  	_Gscan         = 0x1000
    85  	_Gscanrunnable = _Gscan + _Grunnable // 0x1001
    86  	_Gscanrunning  = _Gscan + _Grunning  // 0x1002
    87  	_Gscansyscall  = _Gscan + _Gsyscall  // 0x1003
    88  	_Gscanwaiting  = _Gscan + _Gwaiting  // 0x1004
    89  )
    90  
    91  const (
    92  	// P status
    93  	_Pidle    = iota
    94  	_Prunning // Only this P is allowed to change from _Prunning.
    95  	_Psyscall
    96  	_Pgcstop
    97  	_Pdead
    98  )
    99  
   100  // Mutual exclusion locks.  In the uncontended case,
   101  // as fast as spin locks (just a few user-level instructions),
   102  // but on the contention path they sleep in the kernel.
   103  // A zeroed Mutex is unlocked (no need to initialize each lock).
   104  type mutex struct {
   105  	// Futex-based impl treats it as uint32 key,
   106  	// while sema-based impl as M* waitm.
   107  	// Used to be a union, but unions break precise GC.
   108  	key uintptr
   109  }
   110  
   111  // sleep and wakeup on one-time events.
   112  // before any calls to notesleep or notewakeup,
   113  // must call noteclear to initialize the Note.
   114  // then, exactly one thread can call notesleep
   115  // and exactly one thread can call notewakeup (once).
   116  // once notewakeup has been called, the notesleep
   117  // will return.  future notesleep will return immediately.
   118  // subsequent noteclear must be called only after
   119  // previous notesleep has returned, e.g. it's disallowed
   120  // to call noteclear straight after notewakeup.
   121  //
   122  // notetsleep is like notesleep but wakes up after
   123  // a given number of nanoseconds even if the event
   124  // has not yet happened.  if a goroutine uses notetsleep to
   125  // wake up early, it must wait to call noteclear until it
   126  // can be sure that no other goroutine is calling
   127  // notewakeup.
   128  //
   129  // notesleep/notetsleep are generally called on g0,
   130  // notetsleepg is similar to notetsleep but is called on user g.
   131  type note struct {
   132  	// Futex-based impl treats it as uint32 key,
   133  	// while sema-based impl as M* waitm.
   134  	// Used to be a union, but unions break precise GC.
   135  	key uintptr
   136  }
   137  
   138  type funcval struct {
   139  	fn uintptr
   140  	// variable-size, fn-specific data here
   141  }
   142  
   143  type iface struct {
   144  	tab  *itab
   145  	data unsafe.Pointer
   146  }
   147  
   148  type eface struct {
   149  	_type *_type
   150  	data  unsafe.Pointer
   151  }
   152  
   153  func efaceOf(ep *interface{}) *eface {
   154  	return (*eface)(unsafe.Pointer(ep))
   155  }
   156  
   157  // The guintptr, muintptr, and puintptr are all used to bypass write barriers.
   158  // It is particularly important to avoid write barriers when the current P has
   159  // been released, because the GC thinks the world is stopped, and an
   160  // unexpected write barrier would not be synchronized with the GC,
   161  // which can lead to a half-executed write barrier that has marked the object
   162  // but not queued it. If the GC skips the object and completes before the
   163  // queuing can occur, it will incorrectly free the object.
   164  //
   165  // We tried using special assignment functions invoked only when not
   166  // holding a running P, but then some updates to a particular memory
   167  // word went through write barriers and some did not. This breaks the
   168  // write barrier shadow checking mode, and it is also scary: better to have
   169  // a word that is completely ignored by the GC than to have one for which
   170  // only a few updates are ignored.
   171  //
   172  // Gs, Ms, and Ps are always reachable via true pointers in the
   173  // allgs, allm, and allp lists or (during allocation before they reach those lists)
   174  // from stack variables.
   175  
   176  // A guintptr holds a goroutine pointer, but typed as a uintptr
   177  // to bypass write barriers. It is used in the Gobuf goroutine state
   178  // and in scheduling lists that are manipulated without a P.
   179  //
   180  // The Gobuf.g goroutine pointer is almost always updated by assembly code.
   181  // In one of the few places it is updated by Go code - func save - it must be
   182  // treated as a uintptr to avoid a write barrier being emitted at a bad time.
   183  // Instead of figuring out how to emit the write barriers missing in the
   184  // assembly manipulation, we change the type of the field to uintptr,
   185  // so that it does not require write barriers at all.
   186  //
   187  // Goroutine structs are published in the allg list and never freed.
   188  // That will keep the goroutine structs from being collected.
   189  // There is never a time that Gobuf.g's contain the only references
   190  // to a goroutine: the publishing of the goroutine in allg comes first.
   191  // Goroutine pointers are also kept in non-GC-visible places like TLS,
   192  // so I can't see them ever moving. If we did want to start moving data
   193  // in the GC, we'd need to allocate the goroutine structs from an
   194  // alternate arena. Using guintptr doesn't make that problem any worse.
   195  type guintptr uintptr
   196  
   197  //go:nosplit
   198  func (gp guintptr) ptr() *g { return (*g)(unsafe.Pointer(gp)) }
   199  
   200  //go:nosplit
   201  func (gp *guintptr) set(g *g) { *gp = guintptr(unsafe.Pointer(g)) }
   202  
   203  //go:nosplit
   204  func (gp *guintptr) cas(old, new guintptr) bool {
   205  	return atomic.Casuintptr((*uintptr)(unsafe.Pointer(gp)), uintptr(old), uintptr(new))
   206  }
   207  
   208  // setGNoWB performs *gp = new without a write barrier.
   209  // For times when it's impractical to use a guintptr.
   210  //go:nosplit
   211  //go:nowritebarrier
   212  func setGNoWB(gp **g, new *g) {
   213  	(*guintptr)(unsafe.Pointer(gp)).set(new)
   214  }
   215  
   216  type puintptr uintptr
   217  
   218  //go:nosplit
   219  func (pp puintptr) ptr() *p { return (*p)(unsafe.Pointer(pp)) }
   220  
   221  //go:nosplit
   222  func (pp *puintptr) set(p *p) { *pp = puintptr(unsafe.Pointer(p)) }
   223  
   224  type muintptr uintptr
   225  
   226  //go:nosplit
   227  func (mp muintptr) ptr() *m { return (*m)(unsafe.Pointer(mp)) }
   228  
   229  //go:nosplit
   230  func (mp *muintptr) set(m *m) { *mp = muintptr(unsafe.Pointer(m)) }
   231  
   232  // setMNoWB performs *mp = new without a write barrier.
   233  // For times when it's impractical to use an muintptr.
   234  //go:nosplit
   235  //go:nowritebarrier
   236  func setMNoWB(mp **m, new *m) {
   237  	(*muintptr)(unsafe.Pointer(mp)).set(new)
   238  }
   239  
   240  type gobuf struct {
   241  	// The offsets of sp, pc, and g are known to (hard-coded in) libmach.
   242  	//
   243  	// ctxt is unusual with respect to GC: it may be a
   244  	// heap-allocated funcval so write require a write barrier,
   245  	// but gobuf needs to be cleared from assembly. We take
   246  	// advantage of the fact that the only path that uses a
   247  	// non-nil ctxt is morestack. As a result, gogo is the only
   248  	// place where it may not already be nil, so gogo uses an
   249  	// explicit write barrier. Everywhere else that resets the
   250  	// gobuf asserts that ctxt is already nil.
   251  	sp   uintptr
   252  	pc   uintptr
   253  	g    guintptr
   254  	ctxt unsafe.Pointer // this has to be a pointer so that gc scans it
   255  	ret  sys.Uintreg
   256  	lr   uintptr
   257  	bp   uintptr // for GOEXPERIMENT=framepointer
   258  }
   259  
   260  // sudog represents a g in a wait list, such as for sending/receiving
   261  // on a channel.
   262  //
   263  // sudog is necessary because the g ↔ synchronization object relation
   264  // is many-to-many. A g can be on many wait lists, so there may be
   265  // many sudogs for one g; and many gs may be waiting on the same
   266  // synchronization object, so there may be many sudogs for one object.
   267  //
   268  // sudogs are allocated from a special pool. Use acquireSudog and
   269  // releaseSudog to allocate and free them.
   270  type sudog struct {
   271  	// The following fields are protected by the hchan.lock of the
   272  	// channel this sudog is blocking on. shrinkstack depends on
   273  	// this for sudogs involved in channel ops.
   274  
   275  	g          *g
   276  	selectdone *uint32 // CAS to 1 to win select race (may point to stack)
   277  	next       *sudog
   278  	prev       *sudog
   279  	elem       unsafe.Pointer // data element (may point to stack)
   280  
   281  	// The following fields are never accessed concurrently.
   282  	// For channels, waitlink is only accessed by g.
   283  	// For semaphores, all fields (including the ones above)
   284  	// are only accessed when holding a semaRoot lock.
   285  
   286  	acquiretime int64
   287  	releasetime int64
   288  	ticket      uint32
   289  	parent      *sudog // semaRoot binary tree
   290  	waitlink    *sudog // g.waiting list or semaRoot
   291  	waittail    *sudog // semaRoot
   292  	c           *hchan // channel
   293  }
   294  
   295  type libcall struct {
   296  	fn   uintptr
   297  	n    uintptr // number of parameters
   298  	args uintptr // parameters
   299  	r1   uintptr // return values
   300  	r2   uintptr
   301  	err  uintptr // error number
   302  }
   303  
   304  // describes how to handle callback
   305  type wincallbackcontext struct {
   306  	gobody       unsafe.Pointer // go function to call
   307  	argsize      uintptr        // callback arguments size (in bytes)
   308  	restorestack uintptr        // adjust stack on return by (in bytes) (386 only)
   309  	cleanstack   bool
   310  }
   311  
   312  // Stack describes a Go execution stack.
   313  // The bounds of the stack are exactly [lo, hi),
   314  // with no implicit data structures on either side.
   315  type stack struct {
   316  	lo uintptr
   317  	hi uintptr
   318  }
   319  
   320  type g struct {
   321  	// Stack parameters.
   322  	// stack describes the actual stack memory: [stack.lo, stack.hi).
   323  	// stackguard0 is the stack pointer compared in the Go stack growth prologue.
   324  	// It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
   325  	// stackguard1 is the stack pointer compared in the C stack growth prologue.
   326  	// It is stack.lo+StackGuard on g0 and gsignal stacks.
   327  	// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
   328  	stack       stack   // offset known to runtime/cgo
   329  	stackguard0 uintptr // offset known to liblink
   330  	stackguard1 uintptr // offset known to liblink
   331  
   332  	_panic         *_panic // innermost panic - offset known to liblink
   333  	_defer         *_defer // innermost defer
   334  	m              *m      // current m; offset known to arm liblink
   335  	sched          gobuf
   336  	syscallsp      uintptr        // if status==Gsyscall, syscallsp = sched.sp to use during gc
   337  	syscallpc      uintptr        // if status==Gsyscall, syscallpc = sched.pc to use during gc
   338  	stktopsp       uintptr        // expected sp at top of stack, to check in traceback
   339  	param          unsafe.Pointer // passed parameter on wakeup
   340  	atomicstatus   uint32
   341  	stackLock      uint32 // sigprof/scang lock; TODO: fold in to atomicstatus
   342  	goid           int64
   343  	waitsince      int64  // approx time when the g become blocked
   344  	waitreason     string // if status==Gwaiting
   345  	schedlink      guintptr
   346  	preempt        bool     // preemption signal, duplicates stackguard0 = stackpreempt
   347  	paniconfault   bool     // panic (instead of crash) on unexpected fault address
   348  	preemptscan    bool     // preempted g does scan for gc
   349  	gcscandone     bool     // g has scanned stack; protected by _Gscan bit in status
   350  	gcscanvalid    bool     // false at start of gc cycle, true if G has not run since last scan; TODO: remove?
   351  	throwsplit     bool     // must not split stack
   352  	raceignore     int8     // ignore race detection events
   353  	sysblocktraced bool     // StartTrace has emitted EvGoInSyscall about this goroutine
   354  	sysexitticks   int64    // cputicks when syscall has returned (for tracing)
   355  	traceseq       uint64   // trace event sequencer
   356  	tracelastp     puintptr // last P emitted an event for this goroutine
   357  	lockedm        *m
   358  	sig            uint32
   359  	writebuf       []byte
   360  	sigcode0       uintptr
   361  	sigcode1       uintptr
   362  	sigpc          uintptr
   363  	gopc           uintptr // pc of go statement that created this goroutine
   364  	startpc        uintptr // pc of goroutine function
   365  	racectx        uintptr
   366  	waiting        *sudog         // sudog structures this g is waiting on (that have a valid elem ptr); in lock order
   367  	cgoCtxt        []uintptr      // cgo traceback context
   368  	labels         unsafe.Pointer // profiler labels
   369  	timer          *timer         // cached timer for time.Sleep
   370  
   371  	// Per-G GC state
   372  
   373  	// gcAssistBytes is this G's GC assist credit in terms of
   374  	// bytes allocated. If this is positive, then the G has credit
   375  	// to allocate gcAssistBytes bytes without assisting. If this
   376  	// is negative, then the G must correct this by performing
   377  	// scan work. We track this in bytes to make it fast to update
   378  	// and check for debt in the malloc hot path. The assist ratio
   379  	// determines how this corresponds to scan work debt.
   380  	gcAssistBytes int64
   381  }
   382  
   383  type m struct {
   384  	g0      *g     // goroutine with scheduling stack
   385  	morebuf gobuf  // gobuf arg to morestack
   386  	divmod  uint32 // div/mod denominator for arm - known to liblink
   387  
   388  	// Fields not known to debuggers.
   389  	procid        uint64       // for debuggers, but offset not hard-coded
   390  	gsignal       *g           // signal-handling g
   391  	goSigStack    gsignalStack // Go-allocated signal handling stack
   392  	sigmask       sigset       // storage for saved signal mask
   393  	tls           [6]uintptr   // thread-local storage (for x86 extern register)
   394  	mstartfn      func()
   395  	curg          *g       // current running goroutine
   396  	caughtsig     guintptr // goroutine running during fatal signal
   397  	p             puintptr // attached p for executing go code (nil if not executing go code)
   398  	nextp         puintptr
   399  	id            int32
   400  	mallocing     int32
   401  	throwing      int32
   402  	preemptoff    string // if != "", keep curg running on this m
   403  	locks         int32
   404  	softfloat     int32
   405  	dying         int32
   406  	profilehz     int32
   407  	helpgc        int32
   408  	spinning      bool // m is out of work and is actively looking for work
   409  	blocked       bool // m is blocked on a note
   410  	inwb          bool // m is executing a write barrier
   411  	newSigstack   bool // minit on C thread called sigaltstack
   412  	printlock     int8
   413  	incgo         bool // m is executing a cgo call
   414  	fastrand      uint32
   415  	ncgocall      uint64      // number of cgo calls in total
   416  	ncgo          int32       // number of cgo calls currently in progress
   417  	cgoCallersUse uint32      // if non-zero, cgoCallers in use temporarily
   418  	cgoCallers    *cgoCallers // cgo traceback if crashing in cgo call
   419  	park          note
   420  	alllink       *m // on allm
   421  	schedlink     muintptr
   422  	mcache        *mcache
   423  	lockedg       *g
   424  	createstack   [32]uintptr // stack that created this thread.
   425  	freglo        [16]uint32  // d[i] lsb and f[i]
   426  	freghi        [16]uint32  // d[i] msb and f[i+16]
   427  	fflag         uint32      // floating point compare flags
   428  	locked        uint32      // tracking for lockosthread
   429  	nextwaitm     uintptr     // next m waiting for lock
   430  	needextram    bool
   431  	traceback     uint8
   432  	waitunlockf   unsafe.Pointer // todo go func(*g, unsafe.pointer) bool
   433  	waitlock      unsafe.Pointer
   434  	waittraceev   byte
   435  	waittraceskip int
   436  	startingtrace bool
   437  	syscalltick   uint32
   438  	thread        uintptr // thread handle
   439  
   440  	// these are here because they are too large to be on the stack
   441  	// of low-level NOSPLIT functions.
   442  	libcall   libcall
   443  	libcallpc uintptr // for cpu profiler
   444  	libcallsp uintptr
   445  	libcallg  guintptr
   446  	syscall   libcall // stores syscall parameters on windows
   447  
   448  	mOS
   449  }
   450  
   451  type p struct {
   452  	lock mutex
   453  
   454  	id          int32
   455  	status      uint32 // one of pidle/prunning/...
   456  	link        puintptr
   457  	schedtick   uint32     // incremented on every scheduler call
   458  	syscalltick uint32     // incremented on every system call
   459  	sysmontick  sysmontick // last tick observed by sysmon
   460  	m           muintptr   // back-link to associated m (nil if idle)
   461  	mcache      *mcache
   462  	racectx     uintptr
   463  
   464  	deferpool    [5][]*_defer // pool of available defer structs of different sizes (see panic.go)
   465  	deferpoolbuf [5][32]*_defer
   466  
   467  	// Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen.
   468  	goidcache    uint64
   469  	goidcacheend uint64
   470  
   471  	// Queue of runnable goroutines. Accessed without lock.
   472  	runqhead uint32
   473  	runqtail uint32
   474  	runq     [256]guintptr
   475  	// runnext, if non-nil, is a runnable G that was ready'd by
   476  	// the current G and should be run next instead of what's in
   477  	// runq if there's time remaining in the running G's time
   478  	// slice. It will inherit the time left in the current time
   479  	// slice. If a set of goroutines is locked in a
   480  	// communicate-and-wait pattern, this schedules that set as a
   481  	// unit and eliminates the (potentially large) scheduling
   482  	// latency that otherwise arises from adding the ready'd
   483  	// goroutines to the end of the run queue.
   484  	runnext guintptr
   485  
   486  	// Available G's (status == Gdead)
   487  	gfree    *g
   488  	gfreecnt int32
   489  
   490  	sudogcache []*sudog
   491  	sudogbuf   [128]*sudog
   492  
   493  	tracebuf traceBufPtr
   494  
   495  	// traceSweep indicates the sweep events should be traced.
   496  	// This is used to defer the sweep start event until a span
   497  	// has actually been swept.
   498  	traceSweep bool
   499  	// traceSwept and traceReclaimed track the number of bytes
   500  	// swept and reclaimed by sweeping in the current sweep loop.
   501  	traceSwept, traceReclaimed uintptr
   502  
   503  	palloc persistentAlloc // per-P to avoid mutex
   504  
   505  	// Per-P GC state
   506  	gcAssistTime     int64 // Nanoseconds in assistAlloc
   507  	gcBgMarkWorker   guintptr
   508  	gcMarkWorkerMode gcMarkWorkerMode
   509  
   510  	// gcw is this P's GC work buffer cache. The work buffer is
   511  	// filled by write barriers, drained by mutator assists, and
   512  	// disposed on certain GC state transitions.
   513  	gcw gcWork
   514  
   515  	runSafePointFn uint32 // if 1, run sched.safePointFn at next safe point
   516  
   517  	pad [sys.CacheLineSize]byte
   518  }
   519  
   520  const (
   521  	// The max value of GOMAXPROCS.
   522  	// There are no fundamental restrictions on the value.
   523  	_MaxGomaxprocs = 1 << 10
   524  )
   525  
   526  type schedt struct {
   527  	// accessed atomically. keep at top to ensure alignment on 32-bit systems.
   528  	goidgen  uint64
   529  	lastpoll uint64
   530  
   531  	lock mutex
   532  
   533  	midle        muintptr // idle m's waiting for work
   534  	nmidle       int32    // number of idle m's waiting for work
   535  	nmidlelocked int32    // number of locked m's waiting for work
   536  	mcount       int32    // number of m's that have been created
   537  	maxmcount    int32    // maximum number of m's allowed (or die)
   538  
   539  	ngsys uint32 // number of system goroutines; updated atomically
   540  
   541  	pidle      puintptr // idle p's
   542  	npidle     uint32
   543  	nmspinning uint32 // See "Worker thread parking/unparking" comment in proc.go.
   544  
   545  	// Global runnable queue.
   546  	runqhead guintptr
   547  	runqtail guintptr
   548  	runqsize int32
   549  
   550  	// Global cache of dead G's.
   551  	gflock       mutex
   552  	gfreeStack   *g
   553  	gfreeNoStack *g
   554  	ngfree       int32
   555  
   556  	// Central cache of sudog structs.
   557  	sudoglock  mutex
   558  	sudogcache *sudog
   559  
   560  	// Central pool of available defer structs of different sizes.
   561  	deferlock mutex
   562  	deferpool [5]*_defer
   563  
   564  	gcwaiting  uint32 // gc is waiting to run
   565  	stopwait   int32
   566  	stopnote   note
   567  	sysmonwait uint32
   568  	sysmonnote note
   569  
   570  	// safepointFn should be called on each P at the next GC
   571  	// safepoint if p.runSafePointFn is set.
   572  	safePointFn   func(*p)
   573  	safePointWait int32
   574  	safePointNote note
   575  
   576  	profilehz int32 // cpu profiling rate
   577  
   578  	procresizetime int64 // nanotime() of last change to gomaxprocs
   579  	totaltime      int64 // ∫gomaxprocs dt up to procresizetime
   580  }
   581  
   582  // The m.locked word holds two pieces of state counting active calls to LockOSThread/lockOSThread.
   583  // The low bit (LockExternal) is a boolean reporting whether any LockOSThread call is active.
   584  // External locks are not recursive; a second lock is silently ignored.
   585  // The upper bits of m.locked record the nesting depth of calls to lockOSThread
   586  // (counting up by LockInternal), popped by unlockOSThread (counting down by LockInternal).
   587  // Internal locks can be recursive. For instance, a lock for cgo can occur while the main
   588  // goroutine is holding the lock during the initialization phase.
   589  const (
   590  	_LockExternal = 1
   591  	_LockInternal = 2
   592  )
   593  
   594  const (
   595  	_SigNotify   = 1 << iota // let signal.Notify have signal, even if from kernel
   596  	_SigKill                 // if signal.Notify doesn't take it, exit quietly
   597  	_SigThrow                // if signal.Notify doesn't take it, exit loudly
   598  	_SigPanic                // if the signal is from the kernel, panic
   599  	_SigDefault              // if the signal isn't explicitly requested, don't monitor it
   600  	_SigGoExit               // cause all runtime procs to exit (only used on Plan 9).
   601  	_SigSetStack             // add SA_ONSTACK to libc handler
   602  	_SigUnblock              // unblocked in minit
   603  )
   604  
   605  // Layout of in-memory per-function information prepared by linker
   606  // See https://golang.org/s/go12symtab.
   607  // Keep in sync with linker (../cmd/link/internal/ld/pcln.go:/pclntab)
   608  // and with package debug/gosym and with symtab.go in package runtime.
   609  type _func struct {
   610  	entry   uintptr // start pc
   611  	nameoff int32   // function name
   612  
   613  	args int32 // in/out args size
   614  	_    int32 // previously legacy frame size; kept for layout compatibility
   615  
   616  	pcsp      int32
   617  	pcfile    int32
   618  	pcln      int32
   619  	npcdata   int32
   620  	nfuncdata int32
   621  }
   622  
   623  // layout of Itab known to compilers
   624  // allocated in non-garbage-collected memory
   625  // Needs to be in sync with
   626  // ../cmd/compile/internal/gc/reflect.go:/^func.dumptypestructs.
   627  type itab struct {
   628  	inter  *interfacetype
   629  	_type  *_type
   630  	link   *itab
   631  	hash   uint32 // copy of _type.hash. Used for type switches.
   632  	bad    bool   // type does not implement interface
   633  	inhash bool   // has this itab been added to hash?
   634  	unused [2]byte
   635  	fun    [1]uintptr // variable sized
   636  }
   637  
   638  // Lock-free stack node.
   639  // // Also known to export_test.go.
   640  type lfnode struct {
   641  	next    uint64
   642  	pushcnt uintptr
   643  }
   644  
   645  type forcegcstate struct {
   646  	lock mutex
   647  	g    *g
   648  	idle uint32
   649  }
   650  
   651  // startup_random_data holds random bytes initialized at startup. These come from
   652  // the ELF AT_RANDOM auxiliary vector (vdso_linux_amd64.go or os_linux_386.go).
   653  var startupRandomData []byte
   654  
   655  // extendRandom extends the random numbers in r[:n] to the whole slice r.
   656  // Treats n<0 as n==0.
   657  func extendRandom(r []byte, n int) {
   658  	if n < 0 {
   659  		n = 0
   660  	}
   661  	for n < len(r) {
   662  		// Extend random bits using hash function & time seed
   663  		w := n
   664  		if w > 16 {
   665  			w = 16
   666  		}
   667  		h := memhash(unsafe.Pointer(&r[n-w]), uintptr(nanotime()), uintptr(w))
   668  		for i := 0; i < sys.PtrSize && n < len(r); i++ {
   669  			r[n] = byte(h)
   670  			n++
   671  			h >>= 8
   672  		}
   673  	}
   674  }
   675  
   676  // deferred subroutine calls
   677  type _defer struct {
   678  	siz     int32
   679  	started bool
   680  	sp      uintptr // sp at time of defer
   681  	pc      uintptr
   682  	fn      *funcval
   683  	_panic  *_panic // panic that is running defer
   684  	link    *_defer
   685  }
   686  
   687  // panics
   688  type _panic struct {
   689  	argp      unsafe.Pointer // pointer to arguments of deferred call run during panic; cannot move - known to liblink
   690  	arg       interface{}    // argument to panic
   691  	link      *_panic        // link to earlier panic
   692  	recovered bool           // whether this panic is over
   693  	aborted   bool           // the panic was aborted
   694  }
   695  
   696  // stack traces
   697  type stkframe struct {
   698  	fn       funcInfo   // function being run
   699  	pc       uintptr    // program counter within fn
   700  	continpc uintptr    // program counter where execution can continue, or 0 if not
   701  	lr       uintptr    // program counter at caller aka link register
   702  	sp       uintptr    // stack pointer at pc
   703  	fp       uintptr    // stack pointer at caller aka frame pointer
   704  	varp     uintptr    // top of local variables
   705  	argp     uintptr    // pointer to function arguments
   706  	arglen   uintptr    // number of bytes at argp
   707  	argmap   *bitvector // force use of this argmap
   708  }
   709  
   710  const (
   711  	_TraceRuntimeFrames = 1 << iota // include frames for internal runtime functions.
   712  	_TraceTrap                      // the initial PC, SP are from a trap, not a return PC from a call
   713  	_TraceJumpStack                 // if traceback is on a systemstack, resume trace at g that called into it
   714  )
   715  
   716  // The maximum number of frames we print for a traceback
   717  const _TracebackMaxFrames = 100
   718  
   719  var (
   720  	emptystring string
   721  	allglen     uintptr
   722  	allm        *m
   723  	allp        [_MaxGomaxprocs + 1]*p
   724  	gomaxprocs  int32
   725  	ncpu        int32
   726  	forcegc     forcegcstate
   727  	sched       schedt
   728  	newprocs    int32
   729  
   730  	// Information about what cpu features are available.
   731  	// Set on startup in asm_{386,amd64,amd64p32}.s.
   732  	// Packages outside the runtime should not use these
   733  	// as they are not an external api.
   734  	processorVersionInfo uint32
   735  	isIntel              bool
   736  	lfenceBeforeRdtsc    bool
   737  	support_aes          bool
   738  	support_avx          bool
   739  	support_avx2         bool
   740  	support_bmi1         bool
   741  	support_bmi2         bool
   742  	support_erms         bool
   743  	support_osxsave      bool
   744  	support_popcnt       bool
   745  	support_sse2         bool
   746  	support_sse41        bool
   747  	support_sse42        bool
   748  	support_ssse3        bool
   749  
   750  	goarm                uint8 // set by cmd/link on arm systems
   751  	framepointer_enabled bool  // set by cmd/link
   752  )
   753  
   754  // Set by the linker so the runtime can determine the buildmode.
   755  var (
   756  	islibrary bool // -buildmode=c-shared
   757  	isarchive bool // -buildmode=c-archive
   758  )