github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/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 or from the dedicated register).
    18  func getg() *g
    19  
    20  // mcall switches from the g to the g0 stack and invokes fn(g),
    21  // where g is the goroutine that made the call.
    22  // mcall saves g's current PC/SP in g->sched so that it can be restored later.
    23  // It is up to fn to arrange for that later execution, typically by recording
    24  // g in a data structure, causing something to call ready(g) later.
    25  // mcall returns to the original goroutine g later, when g has been rescheduled.
    26  // fn must not return at all; typically it ends by calling schedule, to let the m
    27  // run other goroutines.
    28  //
    29  // mcall can only be called from g stacks (not g0, not gsignal).
    30  //
    31  // This must NOT be go:noescape: if fn is a stack-allocated closure,
    32  // fn puts g on a run queue, and g executes before fn returns, the
    33  // closure will be invalidated while it is still executing.
    34  func mcall(fn func(*g))
    35  
    36  // systemstack runs fn on a system stack.
    37  // If systemstack is called from the per-OS-thread (g0) stack, or
    38  // if systemstack is called from the signal handling (gsignal) stack,
    39  // systemstack calls fn directly and returns.
    40  // Otherwise, systemstack is being called from the limited stack
    41  // of an ordinary goroutine. In this case, systemstack switches
    42  // to the per-OS-thread stack, calls fn, and switches back.
    43  // It is common to use a func literal as the argument, in order
    44  // to share inputs and outputs with the code around the call
    45  // to system stack:
    46  //
    47  //	... set up y ...
    48  //	systemstack(func() {
    49  //		x = bigcall(y)
    50  //	})
    51  //	... use x ...
    52  //
    53  //go:noescape
    54  func systemstack(fn func())
    55  
    56  var badsystemstackMsg = "fatal: systemstack called from unexpected goroutine"
    57  
    58  //go:nosplit
    59  //go:nowritebarrierrec
    60  func badsystemstack() {
    61  	sp := stringStructOf(&badsystemstackMsg)
    62  	write(2, sp.str, int32(sp.len))
    63  }
    64  
    65  // memclrNoHeapPointers clears n bytes starting at ptr.
    66  //
    67  // Usually you should use typedmemclr. memclrNoHeapPointers should be
    68  // used only when the caller knows that *ptr contains no heap pointers
    69  // because either:
    70  //
    71  // *ptr is initialized memory and its type is pointer-free, or
    72  //
    73  // *ptr is uninitialized memory (e.g., memory that's being reused
    74  // for a new allocation) and hence contains only "junk".
    75  //
    76  // The (CPU-specific) implementations of this function are in memclr_*.s.
    77  //go:noescape
    78  func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)
    79  
    80  //go:linkname reflect_memclrNoHeapPointers reflect.memclrNoHeapPointers
    81  func reflect_memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) {
    82  	memclrNoHeapPointers(ptr, n)
    83  }
    84  
    85  // memmove copies n bytes from "from" to "to".
    86  //
    87  // memmove ensures that any pointer in "from" is written to "to" with
    88  // an indivisible write, so that racy reads cannot observe a
    89  // half-written pointer. This is necessary to prevent the garbage
    90  // collector from observing invalid pointers, and differs from memmove
    91  // in unmanaged languages. However, memmove is only required to do
    92  // this if "from" and "to" may contain pointers, which can only be the
    93  // case if "from", "to", and "n" are all be word-aligned.
    94  //
    95  // Implementations are in memmove_*.s.
    96  //
    97  //go:noescape
    98  func memmove(to, from unsafe.Pointer, n uintptr)
    99  
   100  //go:linkname reflect_memmove reflect.memmove
   101  func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
   102  	memmove(to, from, n)
   103  }
   104  
   105  // exported value for testing
   106  var hashLoad = float32(loadFactorNum) / float32(loadFactorDen)
   107  
   108  //go:nosplit
   109  func fastrand() uint32 {
   110  	mp := getg().m
   111  	// Implement xorshift64+: 2 32-bit xorshift sequences added together.
   112  	// Shift triplet [17,7,16] was calculated as indicated in Marsaglia's
   113  	// Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
   114  	// This generator passes the SmallCrush suite, part of TestU01 framework:
   115  	// http://simul.iro.umontreal.ca/testu01/tu01.html
   116  	s1, s0 := mp.fastrand[0], mp.fastrand[1]
   117  	s1 ^= s1 << 17
   118  	s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16
   119  	mp.fastrand[0], mp.fastrand[1] = s0, s1
   120  	return s0 + s1
   121  }
   122  
   123  //go:nosplit
   124  func fastrandn(n uint32) uint32 {
   125  	// This is similar to fastrand() % n, but faster.
   126  	// See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
   127  	return uint32(uint64(fastrand()) * uint64(n) >> 32)
   128  }
   129  
   130  //go:linkname sync_fastrand sync.fastrand
   131  func sync_fastrand() uint32 { return fastrand() }
   132  
   133  //go:linkname net_fastrand net.fastrand
   134  func net_fastrand() uint32 { return fastrand() }
   135  
   136  //go:linkname os_fastrand os.fastrand
   137  func os_fastrand() uint32 { return fastrand() }
   138  
   139  // in internal/bytealg/equal_*.s
   140  //go:noescape
   141  func memequal(a, b unsafe.Pointer, size uintptr) bool
   142  
   143  // noescape hides a pointer from escape analysis.  noescape is
   144  // the identity function but escape analysis doesn't think the
   145  // output depends on the input.  noescape is inlined and currently
   146  // compiles down to zero instructions.
   147  // USE CAREFULLY!
   148  //go:nosplit
   149  func noescape(p unsafe.Pointer) unsafe.Pointer {
   150  	x := uintptr(p)
   151  	return unsafe.Pointer(x ^ 0)
   152  }
   153  
   154  // Not all cgocallback frames are actually cgocallback,
   155  // so not all have these arguments. Mark them uintptr so that the GC
   156  // does not misinterpret memory when the arguments are not present.
   157  // cgocallback is not called from Go, only from crosscall2.
   158  // This in turn calls cgocallbackg, which is where we'll find
   159  // pointer-declared arguments.
   160  func cgocallback(fn, frame, ctxt uintptr)
   161  func gogo(buf *gobuf)
   162  func gosave(buf *gobuf)
   163  
   164  //go:noescape
   165  func jmpdefer(fv *funcval, argp uintptr)
   166  func asminit()
   167  func setg(gg *g)
   168  func breakpoint()
   169  
   170  // reflectcall calls fn with a copy of the n argument bytes pointed at by arg.
   171  // After fn returns, reflectcall copies n-retoffset result bytes
   172  // back into arg+retoffset before returning. If copying result bytes back,
   173  // the caller should pass the argument frame type as argtype, so that
   174  // call can execute appropriate write barriers during the copy.
   175  //
   176  // Package reflect always passes a frame type. In package runtime,
   177  // Windows callbacks are the only use of this that copies results
   178  // back, and those cannot have pointers in their results, so runtime
   179  // passes nil for the frame type.
   180  //
   181  // Package reflect accesses this symbol through a linkname.
   182  func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32)
   183  
   184  func procyield(cycles uint32)
   185  
   186  type neverCallThisFunction struct{}
   187  
   188  // goexit is the return stub at the top of every goroutine call stack.
   189  // Each goroutine stack is constructed as if goexit called the
   190  // goroutine's entry point function, so that when the entry point
   191  // function returns, it will return to goexit, which will call goexit1
   192  // to perform the actual exit.
   193  //
   194  // This function must never be called directly. Call goexit1 instead.
   195  // gentraceback assumes that goexit terminates the stack. A direct
   196  // call on the stack will cause gentraceback to stop walking the stack
   197  // prematurely and if there is leftover state it may panic.
   198  func goexit(neverCallThisFunction)
   199  
   200  // publicationBarrier performs a store/store barrier (a "publication"
   201  // or "export" barrier). Some form of synchronization is required
   202  // between initializing an object and making that object accessible to
   203  // another processor. Without synchronization, the initialization
   204  // writes and the "publication" write may be reordered, allowing the
   205  // other processor to follow the pointer and observe an uninitialized
   206  // object. In general, higher-level synchronization should be used,
   207  // such as locking or an atomic pointer write. publicationBarrier is
   208  // for when those aren't an option, such as in the implementation of
   209  // the memory manager.
   210  //
   211  // There's no corresponding barrier for the read side because the read
   212  // side naturally has a data dependency order. All architectures that
   213  // Go supports or seems likely to ever support automatically enforce
   214  // data dependency ordering.
   215  func publicationBarrier()
   216  
   217  // getcallerpc returns the program counter (PC) of its caller's caller.
   218  // getcallersp returns the stack pointer (SP) of its caller's caller.
   219  // The implementation may be a compiler intrinsic; there is not
   220  // necessarily code implementing this on every platform.
   221  //
   222  // For example:
   223  //
   224  //	func f(arg1, arg2, arg3 int) {
   225  //		pc := getcallerpc()
   226  //		sp := getcallersp()
   227  //	}
   228  //
   229  // These two lines find the PC and SP immediately following
   230  // the call to f (where f will return).
   231  //
   232  // The call to getcallerpc and getcallersp must be done in the
   233  // frame being asked about.
   234  //
   235  // The result of getcallersp is correct at the time of the return,
   236  // but it may be invalidated by any subsequent call to a function
   237  // that might relocate the stack in order to grow or shrink it.
   238  // A general rule is that the result of getcallersp should be used
   239  // immediately and can only be passed to nosplit functions.
   240  
   241  //go:noescape
   242  func getcallerpc() uintptr
   243  
   244  //go:noescape
   245  func getcallersp() uintptr // implemented as an intrinsic on all platforms
   246  
   247  // getclosureptr returns the pointer to the current closure.
   248  // getclosureptr can only be used in an assignment statement
   249  // at the entry of a function. Moreover, go:nosplit directive
   250  // must be specified at the declaration of caller function,
   251  // so that the function prolog does not clobber the closure register.
   252  // for example:
   253  //
   254  //	//go:nosplit
   255  //	func f(arg1, arg2, arg3 int) {
   256  //		dx := getclosureptr()
   257  //	}
   258  //
   259  // The compiler rewrites calls to this function into instructions that fetch the
   260  // pointer from a well-known register (DX on x86 architecture, etc.) directly.
   261  func getclosureptr() uintptr
   262  
   263  //go:noescape
   264  func asmcgocall(fn, arg unsafe.Pointer) int32
   265  
   266  func morestack()
   267  func morestack_noctxt()
   268  func rt0_go()
   269  
   270  // return0 is a stub used to return 0 from deferproc.
   271  // It is called at the very end of deferproc to signal
   272  // the calling Go function that it should not jump
   273  // to deferreturn.
   274  // in asm_*.s
   275  func return0()
   276  
   277  // in asm_*.s
   278  // not called directly; definitions here supply type information for traceback.
   279  func call16(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   280  func call32(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   281  func call64(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   282  func call128(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   283  func call256(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   284  func call512(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   285  func call1024(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   286  func call2048(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   287  func call4096(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   288  func call8192(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   289  func call16384(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   290  func call32768(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   291  func call65536(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   292  func call131072(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   293  func call262144(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   294  func call524288(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   295  func call1048576(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   296  func call2097152(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   297  func call4194304(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   298  func call8388608(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   299  func call16777216(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   300  func call33554432(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   301  func call67108864(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   302  func call134217728(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   303  func call268435456(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   304  func call536870912(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   305  func call1073741824(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
   306  
   307  func systemstack_switch()
   308  
   309  // alignUp rounds n up to a multiple of a. a must be a power of 2.
   310  func alignUp(n, a uintptr) uintptr {
   311  	return (n + a - 1) &^ (a - 1)
   312  }
   313  
   314  // alignDown rounds n down to a multiple of a. a must be a power of 2.
   315  func alignDown(n, a uintptr) uintptr {
   316  	return n &^ (a - 1)
   317  }
   318  
   319  // divRoundUp returns ceil(n / a).
   320  func divRoundUp(n, a uintptr) uintptr {
   321  	// a is generally a power of two. This will get inlined and
   322  	// the compiler will optimize the division.
   323  	return (n + a - 1) / a
   324  }
   325  
   326  // checkASM reports whether assembly runtime checks have passed.
   327  func checkASM() bool
   328  
   329  func memequal_varlen(a, b unsafe.Pointer) bool
   330  
   331  // bool2int returns 0 if x is false or 1 if x is true.
   332  func bool2int(x bool) int {
   333  	// Avoid branches. In the SSA compiler, this compiles to
   334  	// exactly what you would want it to.
   335  	return int(uint8(*(*uint8)(unsafe.Pointer(&x))))
   336  }
   337  
   338  // abort crashes the runtime in situations where even throw might not
   339  // work. In general it should do something a debugger will recognize
   340  // (e.g., an INT3 on x86). A crash in abort is recognized by the
   341  // signal handler, which will attempt to tear down the runtime
   342  // immediately.
   343  func abort()
   344  
   345  // Called from compiled code; declared for vet; do NOT call from Go.
   346  func gcWriteBarrier()
   347  func duffzero()
   348  func duffcopy()
   349  
   350  // Called from linker-generated .initarray; declared for go vet; do NOT call from Go.
   351  func addmoduledata()