github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/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 // Declarations for runtime services implemented in C or assembly. 10 11 const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const 12 const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const 13 14 // Should be a built-in for unsafe.Pointer? 15 //go:nosplit 16 func add(p unsafe.Pointer, x uintptr) unsafe.Pointer { 17 return unsafe.Pointer(uintptr(p) + x) 18 } 19 20 // n must be a power of 2 21 func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer { 22 delta := -uintptr(p) & (n - 1) 23 return unsafe.Pointer(uintptr(p) + delta) 24 } 25 26 func getg() *g 27 28 // mcall switches from the g to the g0 stack and invokes fn(g), 29 // where g is the goroutine that made the call. 30 // mcall saves g's current PC/SP in g->sched so that it can be restored later. 31 // It is up to fn to arrange for that later execution, typically by recording 32 // g in a data structure, causing something to call ready(g) later. 33 // mcall returns to the original goroutine g later, when g has been rescheduled. 34 // fn must not return at all; typically it ends by calling schedule, to let the m 35 // run other goroutines. 36 // 37 // mcall can only be called from g stacks (not g0, not gsignal). 38 //go:noescape 39 func mcall(fn func(*g)) 40 41 // systemstack runs fn on a system stack. 42 // If systemstack is called from the per-OS-thread (g0) stack, or 43 // if systemstack is called from the signal handling (gsignal) stack, 44 // systemstack calls fn directly and returns. 45 // Otherwise, systemstack is being called from the limited stack 46 // of an ordinary goroutine. In this case, systemstack switches 47 // to the per-OS-thread stack, calls fn, and switches back. 48 // It is common to use a func literal as the argument, in order 49 // to share inputs and outputs with the code around the call 50 // to system stack: 51 // 52 // ... set up y ... 53 // systemstack(func() { 54 // x = bigcall(y) 55 // }) 56 // ... use x ... 57 // 58 //go:noescape 59 func systemstack(fn func()) 60 61 func badsystemstack() { 62 throw("systemstack called from unexpected goroutine") 63 } 64 65 // memclr clears n bytes starting at ptr. 66 // in memclr_*.s 67 //go:noescape 68 func memclr(ptr unsafe.Pointer, n uintptr) 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 func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr) 126 func goexit() 127 128 //go:noescape 129 func cas(ptr *uint32, old, new uint32) bool 130 131 // NO go:noescape annotation; see atomic_pointer.go. 132 func casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool 133 134 func nop() // call to prevent inlining of function body 135 136 //go:noescape 137 func casuintptr(ptr *uintptr, old, new uintptr) bool 138 139 //go:noescape 140 func atomicstoreuintptr(ptr *uintptr, new uintptr) 141 142 //go:noescape 143 func atomicloaduintptr(ptr *uintptr) uintptr 144 145 //go:noescape 146 func atomicloaduint(ptr *uint) uint 147 148 //go:noescape 149 func setcallerpc(argp unsafe.Pointer, pc uintptr) 150 151 // getcallerpc returns the program counter (PC) of its caller's caller. 152 // getcallersp returns the stack pointer (SP) of its caller's caller. 153 // For both, the argp must be a pointer to the caller's first function argument. 154 // The implementation may or may not use argp, depending on 155 // the architecture. 156 // 157 // For example: 158 // 159 // func f(arg1, arg2, arg3 int) { 160 // pc := getcallerpc(unsafe.Pointer(&arg1)) 161 // sp := getcallersp(unsafe.Pointer(&arg1)) 162 // } 163 // 164 // These two lines find the PC and SP immediately following 165 // the call to f (where f will return). 166 // 167 // The call to getcallerpc and getcallersp must be done in the 168 // frame being asked about. It would not be correct for f to pass &arg1 169 // to another function g and let g call getcallerpc/getcallersp. 170 // The call inside g might return information about g's caller or 171 // information about f's caller or complete garbage. 172 // 173 // The result of getcallersp is correct at the time of the return, 174 // but it may be invalidated by any subsequent call to a function 175 // that might relocate the stack in order to grow or shrink it. 176 // A general rule is that the result of getcallersp should be used 177 // immediately and can only be passed to nosplit functions. 178 179 //go:noescape 180 func getcallerpc(argp unsafe.Pointer) uintptr 181 182 //go:noescape 183 func getcallersp(argp unsafe.Pointer) uintptr 184 185 //go:noescape 186 func asmcgocall(fn, arg unsafe.Pointer) 187 188 //go:noescape 189 func asmcgocall_errno(fn, arg unsafe.Pointer) int32 190 191 // argp used in Defer structs when there is no argp. 192 const _NoArgs = ^uintptr(0) 193 194 func morestack() 195 func rt0_go() 196 197 // return0 is a stub used to return 0 from deferproc. 198 // It is called at the very end of deferproc to signal 199 // the calling Go function that it should not jump 200 // to deferreturn. 201 // in asm_*.s 202 func return0() 203 204 //go:linkname time_now time.now 205 func time_now() (sec int64, nsec int32) 206 207 // in asm_*.s 208 // not called directly; definitions here supply type information for traceback. 209 func call16(fn, arg unsafe.Pointer, n, retoffset uint32) 210 func call32(fn, arg unsafe.Pointer, n, retoffset uint32) 211 func call64(fn, arg unsafe.Pointer, n, retoffset uint32) 212 func call128(fn, arg unsafe.Pointer, n, retoffset uint32) 213 func call256(fn, arg unsafe.Pointer, n, retoffset uint32) 214 func call512(fn, arg unsafe.Pointer, n, retoffset uint32) 215 func call1024(fn, arg unsafe.Pointer, n, retoffset uint32) 216 func call2048(fn, arg unsafe.Pointer, n, retoffset uint32) 217 func call4096(fn, arg unsafe.Pointer, n, retoffset uint32) 218 func call8192(fn, arg unsafe.Pointer, n, retoffset uint32) 219 func call16384(fn, arg unsafe.Pointer, n, retoffset uint32) 220 func call32768(fn, arg unsafe.Pointer, n, retoffset uint32) 221 func call65536(fn, arg unsafe.Pointer, n, retoffset uint32) 222 func call131072(fn, arg unsafe.Pointer, n, retoffset uint32) 223 func call262144(fn, arg unsafe.Pointer, n, retoffset uint32) 224 func call524288(fn, arg unsafe.Pointer, n, retoffset uint32) 225 func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32) 226 func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32) 227 func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32) 228 func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32) 229 func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32) 230 func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32) 231 func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32) 232 func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32) 233 func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32) 234 func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32) 235 func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32) 236 237 func systemstack_switch() 238 239 func prefetcht0(addr uintptr) 240 func prefetcht1(addr uintptr) 241 func prefetcht2(addr uintptr) 242 func prefetchnta(addr uintptr)