github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/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 // memclrNoHeapPointers clears n bytes starting at ptr. 61 // 62 // Usually you should use typedmemclr. memclrNoHeapPointers should be 63 // used only when the caller knows that *ptr contains no heap pointers 64 // because either: 65 // 66 // 1. *ptr is initialized memory and its type is pointer-free. 67 // 68 // 2. *ptr is uninitialized memory (e.g., memory that's being reused 69 // for a new allocation) and hence contains only "junk". 70 // 71 // in memclr_*.s 72 //go:noescape 73 func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) 74 75 //go:linkname reflect_memclrNoHeapPointers reflect.memclrNoHeapPointers 76 func reflect_memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) { 77 memclrNoHeapPointers(ptr, n) 78 } 79 80 // memmove copies n bytes from "from" to "to". 81 // in memmove_*.s 82 //go:noescape 83 func memmove(to, from unsafe.Pointer, n uintptr) 84 85 //go:linkname reflect_memmove reflect.memmove 86 func reflect_memmove(to, from unsafe.Pointer, n uintptr) { 87 memmove(to, from, n) 88 } 89 90 // exported value for testing 91 var hashLoad = float32(loadFactorNum) / float32(loadFactorDen) 92 93 //go:nosplit 94 func fastrand() uint32 { 95 mp := getg().m 96 // Implement xorshift64+: 2 32-bit xorshift sequences added together. 97 // Shift triplet [17,7,16] was calculated as indicated in Marsaglia's 98 // Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf 99 // This generator passes the SmallCrush suite, part of TestU01 framework: 100 // http://simul.iro.umontreal.ca/testu01/tu01.html 101 s1, s0 := mp.fastrand[0], mp.fastrand[1] 102 s1 ^= s1 << 17 103 s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16 104 mp.fastrand[0], mp.fastrand[1] = s0, s1 105 return s0 + s1 106 } 107 108 //go:nosplit 109 func fastrandn(n uint32) uint32 { 110 // This is similar to fastrand() % n, but faster. 111 // See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ 112 return uint32(uint64(fastrand()) * uint64(n) >> 32) 113 } 114 115 //go:linkname sync_fastrand sync.fastrand 116 func sync_fastrand() uint32 { return fastrand() } 117 118 // in asm_*.s 119 //go:noescape 120 func memequal(a, b unsafe.Pointer, size uintptr) bool 121 122 // noescape hides a pointer from escape analysis. noescape is 123 // the identity function but escape analysis doesn't think the 124 // output depends on the input. noescape is inlined and currently 125 // compiles down to zero instructions. 126 // USE CAREFULLY! 127 //go:nosplit 128 func noescape(p unsafe.Pointer) unsafe.Pointer { 129 x := uintptr(p) 130 return unsafe.Pointer(x ^ 0) 131 } 132 133 func cgocallback(fn, frame unsafe.Pointer, framesize, ctxt uintptr) 134 func gogo(buf *gobuf) 135 func gosave(buf *gobuf) 136 137 //go:noescape 138 func jmpdefer(fv *funcval, argp uintptr) 139 func asminit() 140 func setg(gg *g) 141 func breakpoint() 142 143 // reflectcall calls fn with a copy of the n argument bytes pointed at by arg. 144 // After fn returns, reflectcall copies n-retoffset result bytes 145 // back into arg+retoffset before returning. If copying result bytes back, 146 // the caller should pass the argument frame type as argtype, so that 147 // call can execute appropriate write barriers during the copy. 148 // Package reflect passes a frame type. In package runtime, there is only 149 // one call that copies results back, in cgocallbackg1, and it does NOT pass a 150 // frame type, meaning there are no write barriers invoked. See that call 151 // site for justification. 152 func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32) 153 154 func procyield(cycles uint32) 155 156 type neverCallThisFunction struct{} 157 158 // goexit is the return stub at the top of every goroutine call stack. 159 // Each goroutine stack is constructed as if goexit called the 160 // goroutine's entry point function, so that when the entry point 161 // function returns, it will return to goexit, which will call goexit1 162 // to perform the actual exit. 163 // 164 // This function must never be called directly. Call goexit1 instead. 165 // gentraceback assumes that goexit terminates the stack. A direct 166 // call on the stack will cause gentraceback to stop walking the stack 167 // prematurely and if there is leftover state it may panic. 168 func goexit(neverCallThisFunction) 169 170 // Not all cgocallback_gofunc frames are actually cgocallback_gofunc, 171 // so not all have these arguments. Mark them uintptr so that the GC 172 // does not misinterpret memory when the arguments are not present. 173 // cgocallback_gofunc is not called from go, only from cgocallback, 174 // so the arguments will be found via cgocallback's pointer-declared arguments. 175 // See the assembly implementations for more details. 176 func cgocallback_gofunc(fv uintptr, frame uintptr, framesize, ctxt uintptr) 177 178 // publicationBarrier performs a store/store barrier (a "publication" 179 // or "export" barrier). Some form of synchronization is required 180 // between initializing an object and making that object accessible to 181 // another processor. Without synchronization, the initialization 182 // writes and the "publication" write may be reordered, allowing the 183 // other processor to follow the pointer and observe an uninitialized 184 // object. In general, higher-level synchronization should be used, 185 // such as locking or an atomic pointer write. publicationBarrier is 186 // for when those aren't an option, such as in the implementation of 187 // the memory manager. 188 // 189 // There's no corresponding barrier for the read side because the read 190 // side naturally has a data dependency order. All architectures that 191 // Go supports or seems likely to ever support automatically enforce 192 // data dependency ordering. 193 func publicationBarrier() 194 195 // getcallerpc returns the program counter (PC) of its caller's caller. 196 // getcallersp returns the stack pointer (SP) of its caller's caller. 197 // argp must be a pointer to the caller's first function argument. 198 // The implementation may or may not use argp, depending on 199 // the architecture. The implementation may be a compiler 200 // intrinsic; there is not necessarily code implementing this 201 // on every platform. 202 // 203 // For example: 204 // 205 // func f(arg1, arg2, arg3 int) { 206 // pc := getcallerpc() 207 // sp := getcallersp(unsafe.Pointer(&arg1)) 208 // } 209 // 210 // These two lines find the PC and SP immediately following 211 // the call to f (where f will return). 212 // 213 // The call to getcallerpc and getcallersp must be done in the 214 // frame being asked about. It would not be correct for f to pass &arg1 215 // to another function g and let g call getcallerpc/getcallersp. 216 // The call inside g might return information about g's caller or 217 // information about f's caller or complete garbage. 218 // 219 // The result of getcallersp is correct at the time of the return, 220 // but it may be invalidated by any subsequent call to a function 221 // that might relocate the stack in order to grow or shrink it. 222 // A general rule is that the result of getcallersp should be used 223 // immediately and can only be passed to nosplit functions. 224 225 //go:noescape 226 func getcallerpc() uintptr 227 228 //go:noescape 229 func getcallersp(argp unsafe.Pointer) uintptr // implemented as an intrinsic on all platforms 230 231 // getclosureptr returns the pointer to the current closure. 232 // getclosureptr can only be used in an assignment statement 233 // at the entry of a function. Moreover, go:nosplit directive 234 // must be specified at the declaration of caller function, 235 // so that the function prolog does not clobber the closure register. 236 // for example: 237 // 238 // //go:nosplit 239 // func f(arg1, arg2, arg3 int) { 240 // dx := getclosureptr() 241 // } 242 // 243 // The compiler rewrites calls to this function into instructions that fetch the 244 // pointer from a well-known register (DX on x86 architecture, etc.) directly. 245 func getclosureptr() uintptr 246 247 //go:noescape 248 func asmcgocall(fn, arg unsafe.Pointer) int32 249 250 // argp used in Defer structs when there is no argp. 251 const _NoArgs = ^uintptr(0) 252 253 func morestack() 254 func morestack_noctxt() 255 func rt0_go() 256 257 // return0 is a stub used to return 0 from deferproc. 258 // It is called at the very end of deferproc to signal 259 // the calling Go function that it should not jump 260 // to deferreturn. 261 // in asm_*.s 262 func return0() 263 264 // in asm_*.s 265 // not called directly; definitions here supply type information for traceback. 266 func call32(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 267 func call64(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 268 func call128(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 269 func call256(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 270 func call512(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 271 func call1024(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 272 func call2048(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 273 func call4096(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 274 func call8192(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 275 func call16384(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 276 func call32768(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 277 func call65536(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 278 func call131072(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 279 func call262144(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 280 func call524288(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 281 func call1048576(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 282 func call2097152(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 283 func call4194304(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 284 func call8388608(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 285 func call16777216(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 286 func call33554432(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 287 func call67108864(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 288 func call134217728(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 289 func call268435456(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 290 func call536870912(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 291 func call1073741824(typ, fn, arg unsafe.Pointer, n, retoffset uint32) 292 293 func systemstack_switch() 294 295 // round n up to a multiple of a. a must be a power of 2. 296 func round(n, a uintptr) uintptr { 297 return (n + a - 1) &^ (a - 1) 298 } 299 300 // checkASM returns whether assembly runtime checks have passed. 301 func checkASM() bool 302 303 func memequal_varlen(a, b unsafe.Pointer) bool 304 305 // bool2int returns 0 if x is false or 1 if x is true. 306 func bool2int(x bool) int { 307 // Avoid branches. In the SSA compiler, this compiles to 308 // exactly what you would want it to. 309 return int(uint8(*(*uint8)(unsafe.Pointer(&x)))) 310 }