Home | History | Annotate | Download | only in runtime
      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 (
      8 	"runtime/internal/sys"
      9 	"unsafe"
     10 )
     11 
     12 // Should be a built-in for unsafe.Pointer?
     13 //go:nosplit
     14 func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
     15 	return unsafe.Pointer(uintptr(p) + x)
     16 }
     17 
     18 // getg returns the pointer to the current g.
     19 // The compiler rewrites calls to this function into instructions
     20 // that fetch the g directly (from TLS or from the dedicated register).
     21 func getg() *g
     22 
     23 // mcall switches from the g to the g0 stack and invokes fn(g),
     24 // where g is the goroutine that made the call.
     25 // mcall saves g's current PC/SP in g->sched so that it can be restored later.
     26 // It is up to fn to arrange for that later execution, typically by recording
     27 // g in a data structure, causing something to call ready(g) later.
     28 // mcall returns to the original goroutine g later, when g has been rescheduled.
     29 // fn must not return at all; typically it ends by calling schedule, to let the m
     30 // run other goroutines.
     31 //
     32 // mcall can only be called from g stacks (not g0, not gsignal).
     33 //
     34 // This must NOT be go:noescape: if fn is a stack-allocated closure,
     35 // fn puts g on a run queue, and g executes before fn returns, the
     36 // closure will be invalidated while it is still executing.
     37 func mcall(fn func(*g))
     38 
     39 // systemstack runs fn on a system stack.
     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 = loadFactor
     95 
     96 // in asm_*.s
     97 func fastrand() uint32
     98 
     99 //go:linkname sync_fastrand sync.fastrand
    100 func sync_fastrand() uint32 { return fastrand() }
    101 
    102 // in asm_*.s
    103 //go:noescape
    104 func memequal(a, b unsafe.Pointer, size uintptr) bool
    105 
    106 // noescape hides a pointer from escape analysis.  noescape is
    107 // the identity function but escape analysis doesn't think the
    108 // output depends on the input.  noescape is inlined and currently
    109 // compiles down to zero instructions.
    110 // USE CAREFULLY!
    111 //go:nosplit
    112 func noescape(p unsafe.Pointer) unsafe.Pointer {
    113 	x := uintptr(p)
    114 	return unsafe.Pointer(x ^ 0)
    115 }
    116 
    117 func cgocallback(fn, frame unsafe.Pointer, framesize, ctxt uintptr)
    118 func gogo(buf *gobuf)
    119 func gosave(buf *gobuf)
    120 func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
    121 
    122 //go:noescape
    123 func jmpdefer(fv *funcval, argp uintptr)
    124 func exit1(code int32)
    125 func asminit()
    126 func setg(gg *g)
    127 func breakpoint()
    128 
    129 // reflectcall calls fn with a copy of the n argument bytes pointed at by arg.
    130 // After fn returns, reflectcall copies n-retoffset result bytes
    131 // back into arg+retoffset before returning. If copying result bytes back,
    132 // the caller should pass the argument frame type as argtype, so that
    133 // call can execute appropriate write barriers during the copy.
    134 // Package reflect passes a frame type. In package runtime, there is only
    135 // one call that copies results back, in cgocallbackg1, and it does NOT pass a
    136 // frame type, meaning there are no write barriers invoked. See that call
    137 // site for justification.
    138 func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32)
    139 
    140 func procyield(cycles uint32)
    141 
    142 type neverCallThisFunction struct{}
    143 
    144 // goexit is the return stub at the top of every goroutine call stack.
    145 // Each goroutine stack is constructed as if goexit called the
    146 // goroutine's entry point function, so that when the entry point
    147 // function returns, it will return to goexit, which will call goexit1
    148 // to perform the actual exit.
    149 //
    150 // This function must never be called directly. Call goexit1 instead.
    151 // gentraceback assumes that goexit terminates the stack. A direct
    152 // call on the stack will cause gentraceback to stop walking the stack
    153 // prematurely and if there are leftover stack barriers it may panic.
    154 func goexit(neverCallThisFunction)
    155 
    156 // Not all cgocallback_gofunc frames are actually cgocallback_gofunc,
    157 // so not all have these arguments. Mark them uintptr so that the GC
    158 // does not misinterpret memory when the arguments are not present.
    159 // cgocallback_gofunc is not called from go, only from cgocallback,
    160 // so the arguments will be found via cgocallback's pointer-declared arguments.
    161 // See the assembly implementations for more details.
    162 func cgocallback_gofunc(fv uintptr, frame uintptr, framesize, ctxt uintptr)
    163 
    164 // publicationBarrier performs a store/store barrier (a "publication"
    165 // or "export" barrier). Some form of synchronization is required
    166 // between initializing an object and making that object accessible to
    167 // another processor. Without synchronization, the initialization
    168 // writes and the "publication" write may be reordered, allowing the
    169 // other processor to follow the pointer and observe an uninitialized
    170 // object. In general, higher-level synchronization should be used,
    171 // such as locking or an atomic pointer write. publicationBarrier is
    172 // for when those aren't an option, such as in the implementation of
    173 // the memory manager.
    174 //
    175 // There's no corresponding barrier for the read side because the read
    176 // side naturally has a data dependency order. All architectures that
    177 // Go supports or seems likely to ever support automatically enforce
    178 // data dependency ordering.
    179 func publicationBarrier()
    180 
    181 //go:noescape
    182 func setcallerpc(argp unsafe.Pointer, pc uintptr)
    183 
    184 // getcallerpc returns the program counter (PC) of its caller's caller.
    185 // getcallersp returns the stack pointer (SP) of its caller's caller.
    186 // For both, the argp must be a pointer to the caller's first function argument.
    187 // The implementation may or may not use argp, depending on
    188 // the architecture.
    189 //
    190 // For example:
    191 //
    192 //	func f(arg1, arg2, arg3 int) {
    193 //		pc := getcallerpc(unsafe.Pointer(&arg1))
    194 //		sp := getcallersp(unsafe.Pointer(&arg1))
    195 //	}
    196 //
    197 // These two lines find the PC and SP immediately following
    198 // the call to f (where f will return).
    199 //
    200 // The call to getcallerpc and getcallersp must be done in the
    201 // frame being asked about. It would not be correct for f to pass &arg1
    202 // to another function g and let g call getcallerpc/getcallersp.
    203 // The call inside g might return information about g's caller or
    204 // information about f's caller or complete garbage.
    205 //
    206 // The result of getcallersp is correct at the time of the return,
    207 // but it may be invalidated by any subsequent call to a function
    208 // that might relocate the stack in order to grow or shrink it.
    209 // A general rule is that the result of getcallersp should be used
    210 // immediately and can only be passed to nosplit functions.
    211 
    212 //go:noescape
    213 func getcallerpc(argp unsafe.Pointer) uintptr
    214 
    215 //go:nosplit
    216 func getcallersp(argp unsafe.Pointer) uintptr {
    217 	return uintptr(argp) - sys.MinFrameSize
    218 }
    219 
    220 //go:noescape
    221 func asmcgocall(fn, arg unsafe.Pointer) int32
    222 
    223 // argp used in Defer structs when there is no argp.
    224 const _NoArgs = ^uintptr(0)
    225 
    226 func morestack()
    227 func morestack_noctxt()
    228 func rt0_go()
    229 
    230 // stackBarrier records that the stack has been unwound past a certain
    231 // point. It is installed over a return PC on the stack. It must
    232 // retrieve the original return PC from g.stkbuf, increment
    233 // g.stkbufPos to record that the barrier was hit, and jump to the
    234 // original return PC.
    235 func stackBarrier()
    236 
    237 // return0 is a stub used to return 0 from deferproc.
    238 // It is called at the very end of deferproc to signal
    239 // the calling Go function that it should not jump
    240 // to deferreturn.
    241 // in asm_*.s
    242 func return0()
    243 
    244 //go:linkname time_now time.now
    245 func time_now() (sec int64, nsec int32)
    246 
    247 // in asm_*.s
    248 // not called directly; definitions here supply type information for traceback.
    249 func call32(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    250 func call64(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    251 func call128(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    252 func call256(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    253 func call512(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    254 func call1024(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    255 func call2048(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    256 func call4096(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    257 func call8192(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    258 func call16384(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    259 func call32768(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    260 func call65536(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    261 func call131072(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    262 func call262144(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    263 func call524288(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    264 func call1048576(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    265 func call2097152(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    266 func call4194304(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    267 func call8388608(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    268 func call16777216(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    269 func call33554432(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    270 func call67108864(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    271 func call134217728(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    272 func call268435456(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    273 func call536870912(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    274 func call1073741824(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
    275 
    276 func systemstack_switch()
    277 
    278 func prefetcht0(addr uintptr)
    279 func prefetcht1(addr uintptr)
    280 func prefetcht2(addr uintptr)
    281 func prefetchnta(addr uintptr)
    282 
    283 func unixnanotime() int64 {
    284 	sec, nsec := time_now()
    285 	return sec*1e9 + int64(nsec)
    286 }
    287 
    288 // round n up to a multiple of a.  a must be a power of 2.
    289 func round(n, a uintptr) uintptr {
    290 	return (n + a - 1) &^ (a - 1)
    291 }
    292 
    293 // checkASM returns whether assembly runtime checks have passed.
    294 func checkASM() bool
    295 
    296 func memequal_varlen(a, b unsafe.Pointer) bool
    297 func eqstring(s1, s2 string) bool
    298