github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/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  func badsystemstack() {
    57  	throw("systemstack called from unexpected goroutine")
    58  }
    59  
    60  // memclr clears n bytes starting at ptr.
    61  // in memclr_*.s
    62  //go:noescape
    63  func memclr(ptr unsafe.Pointer, n uintptr)
    64  
    65  //go:linkname reflect_memclr reflect.memclr
    66  func reflect_memclr(ptr unsafe.Pointer, n uintptr) {
    67  	memclr(ptr, n)
    68  }
    69  
    70  // memmove copies n bytes from "from" to "to".
    71  // in memmove_*.s
    72  //go:noescape
    73  func memmove(to, from unsafe.Pointer, n uintptr)
    74  
    75  //go:linkname reflect_memmove reflect.memmove
    76  func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
    77  	memmove(to, from, n)
    78  }
    79  
    80  // exported value for testing
    81  var hashLoad = loadFactor
    82  
    83  // in asm_*.s
    84  func fastrand1() uint32
    85  
    86  // in asm_*.s
    87  //go:noescape
    88  func memeq(a, b unsafe.Pointer, size uintptr) bool
    89  
    90  // noescape hides a pointer from escape analysis.  noescape is
    91  // the identity function but escape analysis doesn't think the
    92  // output depends on the input.  noescape is inlined and currently
    93  // compiles down to a single xor instruction.
    94  // USE CAREFULLY!
    95  //go:nosplit
    96  func noescape(p unsafe.Pointer) unsafe.Pointer {
    97  	x := uintptr(p)
    98  	return unsafe.Pointer(x ^ 0)
    99  }
   100  
   101  func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
   102  func gogo(buf *gobuf)
   103  func gosave(buf *gobuf)
   104  func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
   105  
   106  //go:noescape
   107  func jmpdefer(fv *funcval, argp uintptr)
   108  func exit1(code int32)
   109  func asminit()
   110  func setg(gg *g)
   111  func breakpoint()
   112  
   113  // reflectcall calls fn with a copy of the n argument bytes pointed at by arg.
   114  // After fn returns, reflectcall copies n-retoffset result bytes
   115  // back into arg+retoffset before returning. If copying result bytes back,
   116  // the caller should pass the argument frame type as argtype, so that
   117  // call can execute appropriate write barriers during the copy.
   118  // Package reflect passes a frame type. In package runtime, there is only
   119  // one call that copies results back, in cgocallbackg1, and it does NOT pass a
   120  // frame type, meaning there are no write barriers invoked. See that call
   121  // site for justification.
   122  func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32)
   123  
   124  func procyield(cycles uint32)
   125  
   126  type neverCallThisFunction struct{}
   127  
   128  // goexit is the return stub at the top of every goroutine call stack.
   129  // Each goroutine stack is constructed as if goexit called the
   130  // goroutine's entry point function, so that when the entry point
   131  // function returns, it will return to goexit, which will call goexit1
   132  // to perform the actual exit.
   133  //
   134  // This function must never be called directly. Call goexit1 instead.
   135  // gentraceback assumes that goexit terminates the stack. A direct
   136  // call on the stack will cause gentraceback to stop walking the stack
   137  // prematurely and if there are leftover stack barriers it may panic.
   138  func goexit(neverCallThisFunction)
   139  
   140  // Not all cgocallback_gofunc frames are actually cgocallback_gofunc,
   141  // so not all have these arguments. Mark them uintptr so that the GC
   142  // does not misinterpret memory when the arguments are not present.
   143  // cgocallback_gofunc is not called from go, only from cgocallback,
   144  // so the arguments will be found via cgocallback's pointer-declared arguments.
   145  // See the assembly implementations for more details.
   146  func cgocallback_gofunc(fv uintptr, frame uintptr, framesize uintptr)
   147  
   148  // publicationBarrier performs a store/store barrier (a "publication"
   149  // or "export" barrier). Some form of synchronization is required
   150  // between initializing an object and making that object accessible to
   151  // another processor. Without synchronization, the initialization
   152  // writes and the "publication" write may be reordered, allowing the
   153  // other processor to follow the pointer and observe an uninitialized
   154  // object. In general, higher-level synchronization should be used,
   155  // such as locking or an atomic pointer write. publicationBarrier is
   156  // for when those aren't an option, such as in the implementation of
   157  // the memory manager.
   158  //
   159  // There's no corresponding barrier for the read side because the read
   160  // side naturally has a data dependency order. All architectures that
   161  // Go supports or seems likely to ever support automatically enforce
   162  // data dependency ordering.
   163  func publicationBarrier()
   164  
   165  //go:noescape
   166  func setcallerpc(argp unsafe.Pointer, pc uintptr)
   167  
   168  // getcallerpc returns the program counter (PC) of its caller's caller.
   169  // getcallersp returns the stack pointer (SP) of its caller's caller.
   170  // For both, the argp must be a pointer to the caller's first function argument.
   171  // The implementation may or may not use argp, depending on
   172  // the architecture.
   173  //
   174  // For example:
   175  //
   176  //	func f(arg1, arg2, arg3 int) {
   177  //		pc := getcallerpc(unsafe.Pointer(&arg1))
   178  //		sp := getcallersp(unsafe.Pointer(&arg1))
   179  //	}
   180  //
   181  // These two lines find the PC and SP immediately following
   182  // the call to f (where f will return).
   183  //
   184  // The call to getcallerpc and getcallersp must be done in the
   185  // frame being asked about. It would not be correct for f to pass &arg1
   186  // to another function g and let g call getcallerpc/getcallersp.
   187  // The call inside g might return information about g's caller or
   188  // information about f's caller or complete garbage.
   189  //
   190  // The result of getcallersp is correct at the time of the return,
   191  // but it may be invalidated by any subsequent call to a function
   192  // that might relocate the stack in order to grow or shrink it.
   193  // A general rule is that the result of getcallersp should be used
   194  // immediately and can only be passed to nosplit functions.
   195  
   196  //go:noescape
   197  func getcallerpc(argp unsafe.Pointer) uintptr
   198  
   199  //go:noescape
   200  func getcallersp(argp unsafe.Pointer) uintptr
   201  
   202  //go:noescape
   203  func asmcgocall(fn, arg unsafe.Pointer) int32
   204  
   205  // argp used in Defer structs when there is no argp.
   206  const _NoArgs = ^uintptr(0)
   207  
   208  func morestack()
   209  func rt0_go()
   210  
   211  // stackBarrier records that the stack has been unwound past a certain
   212  // point. It is installed over a return PC on the stack. It must
   213  // retrieve the original return PC from g.stkbuf, increment
   214  // g.stkbufPos to record that the barrier was hit, and jump to the
   215  // original return PC.
   216  func stackBarrier()
   217  
   218  // return0 is a stub used to return 0 from deferproc.
   219  // It is called at the very end of deferproc to signal
   220  // the calling Go function that it should not jump
   221  // to deferreturn.
   222  // in asm_*.s
   223  func return0()
   224  
   225  //go:linkname time_now time.now
   226  func time_now() (sec int64, nsec int32)
   227  
   228  // in asm_*.s
   229  // not called directly; definitions here supply type information for traceback.
   230  func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
   231  func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
   232  func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
   233  func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
   234  func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
   235  func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
   236  func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
   237  func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
   238  func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
   239  func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
   240  func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
   241  func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
   242  func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
   243  func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
   244  func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
   245  func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
   246  func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
   247  func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
   248  func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
   249  func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
   250  func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
   251  func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
   252  func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
   253  func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
   254  func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
   255  func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)
   256  
   257  func systemstack_switch()
   258  
   259  func prefetcht0(addr uintptr)
   260  func prefetcht1(addr uintptr)
   261  func prefetcht2(addr uintptr)
   262  func prefetchnta(addr uintptr)
   263  
   264  func unixnanotime() int64 {
   265  	sec, nsec := time_now()
   266  	return sec*1e9 + int64(nsec)
   267  }
   268  
   269  // round n up to a multiple of a.  a must be a power of 2.
   270  func round(n, a uintptr) uintptr {
   271  	return (n + a - 1) &^ (a - 1)
   272  }
   273  
   274  // checkASM returns whether assembly runtime checks have passed.
   275  func checkASM() bool