github.com/c0deoo1/golang1.5@v0.0.0-20220525150107-c87c805d4593/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 const spAlign = 1*(1-goarch_arm64) + 16*goarch_arm64 // SP alignment: 1 normally, 16 for ARM64 14 15 // Should be a built-in for unsafe.Pointer? 16 //go:nosplit 17 func add(p unsafe.Pointer, x uintptr) unsafe.Pointer { 18 return unsafe.Pointer(uintptr(p) + x) 19 } 20 21 // getg returns the pointer to the current g. 22 // The compiler rewrites calls to this function into instructions 23 // that fetch the g directly (from TLS or from the dedicated register). 24 func getg() *g 25 26 // mcall switches from the g to the g0 stack and invokes fn(g), 27 // where g is the goroutine that made the call. 28 // mcall saves g's current PC/SP in g->sched so that it can be restored later. 29 // It is up to fn to arrange for that later execution, typically by recording 30 // g in a data structure, causing something to call ready(g) later. 31 // mcall returns to the original goroutine g later, when g has been rescheduled. 32 // fn must not return at all; typically it ends by calling schedule, to let the m 33 // run other goroutines. 34 // 35 // mcall can only be called from g stacks (not g0, not gsignal). 36 // 37 // This must NOT be go:noescape: if fn is a stack-allocated closure, 38 // fn puts g on a run queue, and g executes before fn returns, the 39 // closure will be invalidated while it is still executing. 40 func mcall(fn func(*g)) 41 42 // systemstack runs fn on a system stack. 43 // If systemstack is called from the per-OS-thread (g0) stack, or 44 // if systemstack is called from the signal handling (gsignal) stack, 45 // systemstack calls fn directly and returns. 46 // Otherwise, systemstack is being called from the limited stack 47 // of an ordinary goroutine. In this case, systemstack switches 48 // to the per-OS-thread stack, calls fn, and switches back. 49 // It is common to use a func literal as the argument, in order 50 // to share inputs and outputs with the code around the call 51 // to system stack: 52 // 53 // ... set up y ... 54 // systemstack(func() { 55 // x = bigcall(y) 56 // }) 57 // ... use x ... 58 // 59 //go:noescape 60 func systemstack(fn func()) 61 62 func badsystemstack() { 63 throw("systemstack called from unexpected goroutine") 64 } 65 66 // memclr clears n bytes starting at ptr. 67 // in memclr_*.s 68 //go:noescape 69 func memclr(ptr unsafe.Pointer, n uintptr) 70 71 //go:linkname reflect_memclr reflect.memclr 72 func reflect_memclr(ptr unsafe.Pointer, n uintptr) { 73 memclr(ptr, n) 74 } 75 76 // memmove copies n bytes from "from" to "to". 77 // in memmove_*.s 78 //go:noescape 79 func memmove(to, from unsafe.Pointer, n uintptr) 80 81 //go:linkname reflect_memmove reflect.memmove 82 func reflect_memmove(to, from unsafe.Pointer, n uintptr) { 83 memmove(to, from, n) 84 } 85 86 // exported value for testing 87 var hashLoad = loadFactor 88 89 // in asm_*.s 90 func fastrand1() uint32 91 92 // in asm_*.s 93 //go:noescape 94 func memeq(a, b unsafe.Pointer, size uintptr) bool 95 96 // noescape hides a pointer from escape analysis. noescape is 97 // the identity function but escape analysis doesn't think the 98 // output depends on the input. noescape is inlined and currently 99 // compiles down to a single xor instruction. 100 // USE CAREFULLY! 101 //go:nosplit 102 func noescape(p unsafe.Pointer) unsafe.Pointer { 103 x := uintptr(p) 104 return unsafe.Pointer(x ^ 0) 105 } 106 107 func cgocallback(fn, frame unsafe.Pointer, framesize uintptr) 108 func gogo(buf *gobuf) 109 func gosave(buf *gobuf) 110 func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32 111 112 //go:noescape 113 func jmpdefer(fv *funcval, argp uintptr) 114 func exit1(code int32) 115 func asminit() 116 func setg(gg *g) 117 func breakpoint() 118 119 // reflectcall calls fn with a copy of the n argument bytes pointed at by arg. 120 // After fn returns, reflectcall copies n-retoffset result bytes 121 // back into arg+retoffset before returning. If copying result bytes back, 122 // the caller should pass the argument frame type as argtype, so that 123 // call can execute appropriate write barriers during the copy. 124 // Package reflect passes a frame type. In package runtime, there is only 125 // one call that copies results back, in cgocallbackg1, and it does NOT pass a 126 // frame type, meaning there are no write barriers invoked. See that call 127 // site for justification. 128 func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32) 129 130 func procyield(cycles uint32) 131 func goexit() 132 133 // Not all cgocallback_gofunc frames are actually cgocallback_gofunc, 134 // so not all have these arguments. Mark them uintptr so that the GC 135 // does not misinterpret memory when the arguments are not present. 136 // cgocallback_gofunc is not called from go, only from cgocallback, 137 // so the arguments will be found via cgocallback's pointer-declared arguments. 138 // See the assembly implementations for more details. 139 func cgocallback_gofunc(fv uintptr, frame uintptr, framesize uintptr) 140 141 //go:noescape 142 func cas(ptr *uint32, old, new uint32) bool 143 144 // NO go:noescape annotation; see atomic_pointer.go. 145 func casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool 146 147 func nop() // call to prevent inlining of function body 148 149 //go:noescape 150 func casuintptr(ptr *uintptr, old, new uintptr) bool 151 152 //go:noescape 153 func atomicstoreuintptr(ptr *uintptr, new uintptr) 154 155 //go:noescape 156 func atomicloaduintptr(ptr *uintptr) uintptr 157 158 //go:noescape 159 func atomicloaduint(ptr *uint) uint 160 161 // TODO: Write native implementations of int64 atomic ops (or improve 162 // inliner). These portable ones can't be inlined right now, so we're 163 // taking an extra function call hit. 164 165 func atomicstoreint64(ptr *int64, new int64) { 166 atomicstore64((*uint64)(unsafe.Pointer(ptr)), uint64(new)) 167 } 168 169 func atomicloadint64(ptr *int64) int64 { 170 return int64(atomicload64((*uint64)(unsafe.Pointer(ptr)))) 171 } 172 173 func xaddint64(ptr *int64, delta int64) int64 { 174 return int64(xadd64((*uint64)(unsafe.Pointer(ptr)), delta)) 175 } 176 177 // publicationBarrier performs a store/store barrier (a "publication" 178 // or "export" barrier). Some form of synchronization is required 179 // between initializing an object and making that object accessible to 180 // another processor. Without synchronization, the initialization 181 // writes and the "publication" write may be reordered, allowing the 182 // other processor to follow the pointer and observe an uninitialized 183 // object. In general, higher-level synchronization should be used, 184 // such as locking or an atomic pointer write. publicationBarrier is 185 // for when those aren't an option, such as in the implementation of 186 // the memory manager. 187 // 188 // There's no corresponding barrier for the read side because the read 189 // side naturally has a data dependency order. All architectures that 190 // Go supports or seems likely to ever support automatically enforce 191 // data dependency ordering. 192 func publicationBarrier() 193 194 //go:noescape 195 func setcallerpc(argp unsafe.Pointer, pc uintptr) 196 197 // getcallerpc returns the program counter (PC) of its caller's caller. 198 // getcallersp returns the stack pointer (SP) of its caller's caller. 199 // For both, the argp must be a pointer to the caller's first function argument. 200 // The implementation may or may not use argp, depending on 201 // the architecture. 202 // 203 // For example: 204 // 205 // func f(arg1, arg2, arg3 int) { 206 // pc := getcallerpc(unsafe.Pointer(&arg1)) 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(argp unsafe.Pointer) uintptr 227 228 //go:noescape 229 func getcallersp(argp unsafe.Pointer) uintptr 230 231 //go:noescape 232 func asmcgocall(fn, arg unsafe.Pointer) int32 233 234 // argp used in Defer structs when there is no argp. 235 const _NoArgs = ^uintptr(0) 236 237 func morestack() 238 func rt0_go() 239 240 // stackBarrier records that the stack has been unwound past a certain 241 // point. It is installed over a return PC on the stack. It must 242 // retrieve the original return PC from g.stkbuf, increment 243 // g.stkbufPos to record that the barrier was hit, and jump to the 244 // original return PC. 245 func stackBarrier() 246 247 // return0 is a stub used to return 0 from deferproc. 248 // It is called at the very end of deferproc to signal 249 // the calling Go function that it should not jump 250 // to deferreturn. 251 // in asm_*.s 252 func return0() 253 254 //go:linkname time_now time.now 255 func time_now() (sec int64, nsec int32) 256 257 // in asm_*.s 258 // not called directly; definitions here supply type information for traceback. 259 func call32(fn, arg unsafe.Pointer, n, retoffset uint32) 260 func call64(fn, arg unsafe.Pointer, n, retoffset uint32) 261 func call128(fn, arg unsafe.Pointer, n, retoffset uint32) 262 func call256(fn, arg unsafe.Pointer, n, retoffset uint32) 263 func call512(fn, arg unsafe.Pointer, n, retoffset uint32) 264 func call1024(fn, arg unsafe.Pointer, n, retoffset uint32) 265 func call2048(fn, arg unsafe.Pointer, n, retoffset uint32) 266 func call4096(fn, arg unsafe.Pointer, n, retoffset uint32) 267 func call8192(fn, arg unsafe.Pointer, n, retoffset uint32) 268 func call16384(fn, arg unsafe.Pointer, n, retoffset uint32) 269 func call32768(fn, arg unsafe.Pointer, n, retoffset uint32) 270 func call65536(fn, arg unsafe.Pointer, n, retoffset uint32) 271 func call131072(fn, arg unsafe.Pointer, n, retoffset uint32) 272 func call262144(fn, arg unsafe.Pointer, n, retoffset uint32) 273 func call524288(fn, arg unsafe.Pointer, n, retoffset uint32) 274 func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32) 275 func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32) 276 func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32) 277 func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32) 278 func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32) 279 func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32) 280 func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32) 281 func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32) 282 func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32) 283 func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32) 284 func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32) 285 286 func systemstack_switch() 287 288 func prefetcht0(addr uintptr) 289 func prefetcht1(addr uintptr) 290 func prefetcht2(addr uintptr) 291 func prefetchnta(addr uintptr) 292 293 func unixnanotime() int64 { 294 sec, nsec := time_now() 295 return sec*1e9 + int64(nsec) 296 } 297 298 // round n up to a multiple of a. a must be a power of 2. 299 func round(n, a uintptr) uintptr { 300 return (n + a - 1) &^ (a - 1) 301 }