github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/runtime/stubs.go (about)

     1  // Copyright 2014 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  // Should be a built-in for unsafe.Pointer?
    10  //go:nosplit
    11  func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
    12  	return unsafe.Pointer(uintptr(p) + x)
    13  }
    14  
    15  // getg returns the pointer to the current g.
    16  // The compiler rewrites calls to this function into instructions
    17  // that fetch the g directly (from TLS(thread local storage) or from the dedicated register).
    18  // 获取指向当前g的指针, 指令运行时由编译器生成
    19  func getg() *g
    20  
    21  // mcall switches from the g to the g0 stack and invokes fn(g),
    22  // 从g栈切换到g0栈来执行fn(g)
    23  // where g is the goroutine that made the call.
    24  // mcall saves g's current PC/SP in g->sched so that it can be restored later.
    25  // It is up to fn to arrange for that later execution, typically by recording
    26  // g in a data structure, causing something to call ready(g) later.
    27  // mcall returns to the original goroutine g later, when g has been rescheduled.
    28  // fn must not return at all; typically it ends by calling schedule, to let the m
    29  // run other goroutines.
    30  //
    31  // mcall can only be called from g stacks (not g0, not gsignal).
    32  //
    33  // This must NOT be go:noescape: if fn is a stack-allocated closure,
    34  // fn puts g on a run queue, and g executes before fn returns, the
    35  // closure will be invalidated while it is still executing.
    36  func mcall(fn func(*g))
    37  
    38  // systemstack runs fn on a system stack.
    39  // 在系统栈(os thread stack)上执行fn
    40  // If systemstack is called from the per-OS-thread (g0) stack, or
    41  // if systemstack is called from the signal handling (gsignal) stack,
    42  // systemstack calls fn directly and returns.
    43  // Otherwise, systemstack is being called from the limited stack
    44  // of an ordinary goroutine. In this case, systemstack switches
    45  // to the per-OS-thread stack, calls fn, and switches back.
    46  // It is common to use a func literal as the argument, in order
    47  // to share inputs and outputs with the code around the call
    48  // to system stack:
    49  //
    50  //	... set up y ...
    51  //	systemstack(func() {
    52  //		x = bigcall(y)
    53  //	})
    54  //	... use x ...
    55  //
    56  //go:noescape
    57  func systemstack(fn func())
    58  
    59  func badsystemstack() {
    60  	throw("systemstack called from unexpected goroutine")
    61  }
    62  
    63  // memclrNoHeapPointers clears n bytes starting at ptr.
    64  //
    65  // Usually you should use typedmemclr. memclrNoHeapPointers should be
    66  // used only when the caller knows that *ptr contains no heap pointers
    67  // because either:
    68  //
    69  // 1. *ptr is initialized memory and its type is pointer-free.
    70  //
    71  // 2. *ptr is uninitialized memory (e.g., memory that's being reused
    72  //    for a new allocation) and hence contains only "junk".
    73  //
    74  // in memclr_*.s
    75  //go:noescape
    76  func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)
    77  
    78  //go:linkname reflect_memclrNoHeapPointers reflect.memclrNoHeapPointers
    79  func reflect_memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) {
    80  	memclrNoHeapPointers(ptr, n)
    81  }
    82  
    83  // memmove copies n bytes from "from" to "to".
    84  // in memmove_*.s
    85  //go:noescape
    86  func memmove(to, from unsafe.Pointer, n uintptr)
    87  
    88  //go:linkname reflect_memmove reflect.memmove
    89  func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
    90  	memmove(to, from, n)
    91  }
    92  
    93  // exported value for testing
    94  var hashLoad = float32(loadFactorNum) / float32(loadFactorDen)
    95  
    96  //go:nosplit
    97  func fastrand() uint32 {
    98  	mp := getg().m
    99  	// Implement xorshift64+: 2 32-bit xorshift sequences added together.
   100  	// Shift triplet [17,7,16] was calculated as indicated in Marsaglia's
   101  	// Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
   102  	// This generator passes the SmallCrush suite, part of TestU01 framework:
   103  	// http://simul.iro.umontreal.ca/testu01/tu01.html
   104  	s1, s0 := mp.fastrand[0], mp.fastrand[1]
   105  	s1 ^= s1 << 17
   106  	s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16
   107  	mp.fastrand[0], mp.fastrand[1] = s0, s1
   108  	return s0 + s1
   109  }
   110  
   111  //go:nosplit
   112  func fastrandn(n uint32) uint32 {
   113  	// This is similar to fastrand() % n, but faster.
   114  	// See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
   115  	return uint32(uint64(fastrand()) * uint64(n) >> 32)
   116  }
   117  
   118  //go:linkname sync_fastrand sync.fastrand
   119  func sync_fastrand() uint32 { return fastrand() }
   120  
   121  // in asm_*.s
   122  //go:noescape
   123  func memequal(a, b unsafe.Pointer, size uintptr) bool
   124  
   125  // noescape hides a pointer from escape analysis.  noescape is
   126  // the identity function but escape analysis doesn't think the
   127  // output depends on the input.  noescape is inlined and currently
   128  // compiles down to zero instructions.
   129  // USE CAREFULLY!
   130  //go:nosplit
   131  func noescape(p unsafe.Pointer) unsafe.Pointer {
   132  	x := uintptr(p)
   133  	return unsafe.Pointer(x ^ 0)
   134  }
   135  
   136  func cgocallback(fn, frame unsafe.Pointer, framesize, ctxt uintptr)
   137  
   138  // 根据gobuf恢复寄存器的值并跳转到g的继续执行地址
   139  func gogo(buf *gobuf)
   140  func gosave(buf *gobuf)
   141  
   142  //go:noescape
   143  func jmpdefer(fv *funcval, argp uintptr)
   144  func asminit()
   145  func setg(gg *g)
   146  func breakpoint()
   147  
   148  // reflectcall calls fn with a copy of the n argument bytes pointed at by arg.
   149  // After fn returns, reflectcall copies n-retoffset result bytes
   150  // back into arg+retoffset before returning. If copying result bytes back,
   151  // the caller should pass the argument frame type as argtype, so that
   152  // call can execute appropriate write barriers during the copy.
   153  // Package reflect passes a frame type. In package runtime, there is only
   154  // one call that copies results back, in cgocallbackg1, and it does NOT pass a
   155  // frame type, meaning there are no write barriers invoked. See that call
   156  // site for justification.
   157  func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32)
   158  
   159  // 循环一定次数, 每次循环都执行pause指令
   160  // https://www.felixcloutier.com/x86/pause
   161  func procyield(cycles uint32)
   162  
   163  type neverCallThisFunction struct{}
   164  
   165  // goexit is the return stub at the top of every goroutine call stack.
   166  // goexit是每个goroutine调用栈顶的桩
   167  // Each goroutine stack is constructed as if goexit called the
   168  // goroutine's entry point function, so that when the entry point
   169  // function returns, it will return to goexit, which will call goexit1
   170  // to perform the actual exit.
   171  // 每个goroutine的栈都被组织成好像goexit调用goroutine的入口函数, 当
   172  // 入口函数返回, 会回到goexit
   173  //
   174  // This function must never be called directly. Call goexit1 instead.
   175  // gentraceback assumes that goexit terminates the stack. A direct
   176  // call on the stack will cause gentraceback to stop walking the stack
   177  // prematurely and if there is leftover state it may panic.
   178  func goexit(neverCallThisFunction)
   179  
   180  // Not all cgocallback_gofunc frames are actually cgocallback_gofunc,
   181  // so not all have these arguments. Mark them uintptr so that the GC
   182  // does not misinterpret memory when the arguments are not present.
   183  // cgocallback_gofunc is not called from go, only from cgocallback,
   184  // so the arguments will be found via cgocallback's pointer-declared arguments.
   185  // See the assembly implementations for more details.
   186  func cgocallback_gofunc(fv uintptr, frame uintptr, framesize, ctxt uintptr)
   187  
   188  // publicationBarrier performs a store/store barrier (a "publication"
   189  // or "export" barrier). Some form of synchronization is required
   190  // between initializing an object and making that object accessible to
   191  // another processor. Without synchronization, the initialization
   192  // writes and the "publication" write may be reordered, allowing the
   193  // other processor to follow the pointer and observe an uninitialized
   194  // object. In general, higher-level synchronization should be used,
   195  // such as locking or an atomic pointer write. publicationBarrier is
   196  // for when those aren't an option, such as in the implementation of
   197  // the memory manager.
   198  //
   199  // There's no corresponding barrier for the read side because the read
   200  // side naturally has a data dependency order. All architectures that
   201  // Go supports or seems likely to ever support automatically enforce
   202  // data dependency ordering.
   203  func publicationBarrier()
   204  
   205  // getcallerpc returns the program counter (PC) of its caller's caller.
   206  // getcallersp returns the stack pointer (SP) of its caller's caller.
   207  // argp must be a pointer to the caller's first function argument.
   208  // The implementation may or may not use argp, depending on
   209  // the architecture. The implementation may be a compiler
   210  // intrinsic; there is not necessarily code implementing this
   211  // on every platform.
   212  //
   213  // For example:
   214  //
   215  //	func f(arg1, arg2, arg3 int) {
   216  //		pc := getcallerpc()
   217  //		sp := getcallersp(unsafe.Pointer(&arg1))
   218  //	}
   219  //
   220  // These two lines find the PC and SP immediately following
   221  // the call to f (where f will return).
   222  //
   223  // The call to getcallerpc and getcallersp must be done in the
   224  // frame being asked about. It would not be correct for f to pass &arg1
   225  // to another function g and let g call getcallerpc/getcallersp.
   226  // The call inside g might return information about g's caller or
   227  // information about f's caller or complete garbage.
   228  //
   229  // The result of getcallersp is correct at the time of the return,
   230  // but it may be invalidated by any subsequent call to a function
   231  // that might relocate the stack in order to grow or shrink it.
   232  // A general rule is that the result of getcallersp should be used
   233  // immediately and can only be passed to nosplit functions.
   234  
   235  //go:noescape
   236  func getcallerpc() uintptr
   237  
   238  //go:noescape
   239  func getcallersp(argp unsafe.Pointer) uintptr // implemented as an intrinsic on all platforms
   240  
   241  // getclosureptr returns the pointer to the current closure.
   242  // getclosureptr can only be used in an assignment statement
   243  // at the entry of a function. Moreover, go:nosplit directive
   244  // must be specified at the declaration of caller function,
   245  // so that the function prolog does not clobber the closure register.
   246  // for example:
   247  //
   248  //	//go:nosplit
   249  //	func f(arg1, arg2, arg3 int) {
   250  //		dx := getclosureptr()
   251  //	}
   252  //
   253  // The compiler rewrites calls to this function into instructions that fetch the
   254  // pointer from a well-known register (DX on x86 architecture, etc.) directly.
   255  func getclosureptr() uintptr
   256  
   257  //go:noescape
   258  func asmcgocall(fn, arg unsafe.Pointer) int32
   259  
   260  // argp used in Defer structs when there is no argp.
   261  const _NoArgs = ^uintptr(0)
   262  
   263  func morestack()
   264  func morestack_noctxt()
   265  func rt0_go()
   266  
   267  // return0 is a stub used to return 0 from deferproc.
   268  // It is called at the very end of deferproc to signal
   269  // the calling Go function that it should not jump
   270  // to deferreturn.
   271  // in asm_*.s
   272  func return0()
   273  
   274  // in asm_*.s
   275  // not called directly; definitions here supply type information for traceback.
   276  func call32(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   277  func call64(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   278  func call128(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   279  func call256(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   280  func call512(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   281  func call1024(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   282  func call2048(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   283  func call4096(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   284  func call8192(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   285  func call16384(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   286  func call32768(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   287  func call65536(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   288  func call131072(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   289  func call262144(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   290  func call524288(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   291  func call1048576(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   292  func call2097152(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   293  func call4194304(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   294  func call8388608(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   295  func call16777216(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   296  func call33554432(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   297  func call67108864(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   298  func call134217728(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   299  func call268435456(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   300  func call536870912(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   301  func call1073741824(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   302  
   303  func systemstack_switch()
   304  
   305  // round n up to a multiple of a.  a must be a power of 2.
   306  func round(n, a uintptr) uintptr {
   307  	return (n + a - 1) &^ (a - 1)
   308  }
   309  
   310  // checkASM returns whether assembly runtime checks have passed.
   311  func checkASM() bool
   312  
   313  func memequal_varlen(a, b unsafe.Pointer) bool
   314  
   315  // bool2int returns 0 if x is false or 1 if x is true.
   316  func bool2int(x bool) int {
   317  	// Avoid branches. In the SSA compiler, this compiles to
   318  	// exactly what you would want it to.
   319  	return int(uint8(*(*uint8)(unsafe.Pointer(&x))))
   320  }