github.com/madokast/nopreempt@v1.0.3-0.20240125081507-3fa05aef38ed/g.go (about)

     1  //go:build go1.19
     2  
     3  package nopreempt
     4  
     5  import "unsafe"
     6  
     7  func getg() *g
     8  
     9  // Stack describes a Go execution stack.
    10  // The bounds of the stack are exactly [lo, hi),
    11  // with no implicit data structures on either side.
    12  type stack struct {
    13  	lo uintptr
    14  	hi uintptr
    15  }
    16  
    17  type guintptr uintptr
    18  type puintptr uintptr
    19  
    20  type gobuf struct {
    21  	// The offsets of sp, pc, and g are known to (hard-coded in) libmach.
    22  	//
    23  	// ctxt is unusual with respect to GC: it may be a
    24  	// heap-allocated funcval, so GC needs to track it, but it
    25  	// needs to be set and cleared from assembly, where it's
    26  	// difficult to have write barriers. However, ctxt is really a
    27  	// saved, live register, and we only ever exchange it between
    28  	// the real register and the gobuf. Hence, we treat it as a
    29  	// root during stack scanning, which means assembly that saves
    30  	// and restores it doesn't need write barriers. It's still
    31  	// typed as a pointer so that any other writes from Go get
    32  	// write barriers.
    33  	sp   uintptr
    34  	pc   uintptr
    35  	g    guintptr
    36  	ctxt uintptr // unsafe.Pointer
    37  	ret  uintptr
    38  	lr   uintptr
    39  	bp   uintptr // for framepointer-enabled architectures
    40  }
    41  
    42  type g struct {
    43  	// Stack parameters.
    44  	// stack describes the actual stack memory: [stack.lo, stack.hi).
    45  	// stackguard0 is the stack pointer compared in the Go stack growth prologue.
    46  	// It is stack.lo+StackGuard normally, but can be StackPreempt to trigger a preemption.
    47  	// stackguard1 is the stack pointer compared in the C stack growth prologue.
    48  	// It is stack.lo+StackGuard on g0 and gsignal stacks.
    49  	// It is ~0 on other goroutine stacks, to trigger a call to morestackc (and crash).
    50  	stack       stack   // offset known to runtime/cgo
    51  	stackguard0 uintptr // offset known to liblink
    52  	stackguard1 uintptr // offset known to liblink
    53  
    54  	_panic    uintptr //*_panic // innermost panic - offset known to liblink
    55  	_defer    uintptr // *_defer // innermost defer
    56  	m         *m      // current m; offset known to arm liblink
    57  	sched     gobuf
    58  	syscallsp uintptr // if status==Gsyscall, syscallsp = sched.sp to use during gc
    59  	syscallpc uintptr // if status==Gsyscall, syscallpc = sched.pc to use during gc
    60  	stktopsp  uintptr // expected sp at top of stack, to check in traceback
    61  	// param is a generic pointer parameter field used to pass
    62  	// values in particular contexts where other storage for the
    63  	// parameter would be difficult to find. It is currently used
    64  	// in three ways:
    65  	// 1. When a channel operation wakes up a blocked goroutine, it sets param to
    66  	//    point to the sudog of the completed blocking operation.
    67  	// 2. By gcAssistAlloc1 to signal back to its caller that the goroutine completed
    68  	//    the GC cycle. It is unsafe to do so in any other way, because the goroutine's
    69  	//    stack may have moved in the meantime.
    70  	// 3. By debugCallWrap to pass parameters to a new goroutine because allocating a
    71  	//    closure in the runtime is forbidden.
    72  	param        unsafe.Pointer
    73  	atomicstatus uint32
    74  	stackLock    uint32 // sigprof/scang lock; TODO: fold in to atomicstatus
    75  	goid         int64
    76  }
    77  
    78  const (
    79  	// tlsSlots is the number of pointer-sized slots reserved for TLS on some platforms,
    80  	// like Windows.
    81  	tlsSlots = 6
    82  )
    83  
    84  type sigset struct{}
    85  type throwType uint32
    86  
    87  type m struct {
    88  	g0      uintptr //*g     // goroutine with scheduling stack
    89  	morebuf gobuf   // gobuf arg to morestack
    90  	divmod  uint32  // div/mod denominator for arm - known to liblink
    91  	_       uint32  // align next field to 8 bytes
    92  
    93  	// Fields not known to debuggers.
    94  	procid     uint64            // for debuggers, but offset not hard-coded
    95  	gsignal    uintptr           // *g                // signal-handling g
    96  	goSigStack gsignalStack      // Go-allocated signal handling stack
    97  	sigmask    sigset            // storage for saved signal mask
    98  	tls        [tlsSlots]uintptr // thread-local storage (for x86 extern register)
    99  	mstartfn   func()
   100  	curg       uintptr  //*g       // current running goroutine
   101  	caughtsig  guintptr // goroutine running during fatal signal
   102  	p          puintptr // attached p for executing go code (nil if not executing go code)
   103  	nextp      puintptr
   104  	oldp       puintptr // the p that was attached before executing a syscall
   105  	id         int64
   106  	mallocing  int32
   107  	throwing   throwType
   108  	preemptoff string // if != "", keep curg running on this m
   109  	locks      int32
   110  	dying      int32
   111  	profilehz  int32
   112  }