Keith Randall | 61dca94 | 2014-06-16 23:03:03 -0700 | [diff] [blame] | 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. |
Keith Randall | 61dca94 | 2014-06-16 23:03:03 -0700 | [diff] [blame] | 10 | |
Aram Hăvărneanu | 846ee04 | 2015-03-08 14:20:20 +0100 | [diff] [blame] | 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 |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 14 | |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 15 | // Should be a built-in for unsafe.Pointer? |
Keith Randall | f440737 | 2014-09-03 08:49:43 -0700 | [diff] [blame] | 16 | //go:nosplit |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 17 | func add(p unsafe.Pointer, x uintptr) unsafe.Pointer { |
| 18 | return unsafe.Pointer(uintptr(p) + x) |
| 19 | } |
| 20 | |
Dmitriy Vyukov | 684de04 | 2014-08-21 20:41:09 +0400 | [diff] [blame] | 21 | func getg() *g |
Keith Randall | 4aa5043 | 2014-07-30 09:01:52 -0700 | [diff] [blame] | 22 | |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 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 | //go:noescape |
| 34 | func mcall(fn func(*g)) |
| 35 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 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: |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 46 | // |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 47 | // ... set up y ... |
| 48 | // systemstack(func() { |
| 49 | // x = bigcall(y) |
| 50 | // }) |
| 51 | // ... use x ... |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 52 | // |
| 53 | //go:noescape |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 54 | func systemstack(fn func()) |
Keith Randall | e359bea | 2014-08-06 14:33:57 -0700 | [diff] [blame] | 55 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 56 | func badsystemstack() { |
Keith Randall | b2a950b | 2014-12-27 20:58:00 -0800 | [diff] [blame] | 57 | throw("systemstack called from unexpected goroutine") |
Russ Cox | 32ecf57 | 2014-09-04 00:10:10 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 60 | // memclr clears n bytes starting at ptr. |
| 61 | // in memclr_*.s |
Keith Randall | 483cb61 | 2014-08-07 13:58:42 -0700 | [diff] [blame] | 62 | //go:noescape |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 63 | func memclr(ptr unsafe.Pointer, n uintptr) |
| 64 | |
Dmitry Vyukov | 67f8a81 | 2014-12-22 22:31:55 +0300 | [diff] [blame] | 65 | //go:linkname reflect_memclr reflect.memclr |
| 66 | func reflect_memclr(ptr unsafe.Pointer, n uintptr) { |
| 67 | memclr(ptr, n) |
| 68 | } |
| 69 | |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 70 | // memmove copies n bytes from "from" to "to". |
| 71 | // in memmove_*.s |
Keith Randall | 483cb61 | 2014-08-07 13:58:42 -0700 | [diff] [blame] | 72 | //go:noescape |
Russ Cox | 7a524a1 | 2014-12-22 13:27:53 -0500 | [diff] [blame] | 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 | } |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 79 | |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 80 | // exported value for testing |
| 81 | var hashLoad = loadFactor |
| 82 | |
| 83 | // in asm_*.s |
Keith Randall | 1a5e394 | 2014-09-09 14:32:53 -0700 | [diff] [blame] | 84 | func fastrand1() uint32 |
| 85 | |
| 86 | // in asm_*.s |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 87 | //go:noescape |
Keith Randall | 7aa4e5a | 2014-08-07 14:52:55 -0700 | [diff] [blame] | 88 | func memeq(a, b unsafe.Pointer, size uintptr) bool |
Keith Randall | 0c6b55e | 2014-07-16 14:16:19 -0700 | [diff] [blame] | 89 | |
Keith Randall | a2a9768 | 2014-07-31 15:07:05 -0700 | [diff] [blame] | 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! |
Keith Randall | f440737 | 2014-09-03 08:49:43 -0700 | [diff] [blame] | 95 | //go:nosplit |
Keith Randall | a2a9768 | 2014-07-31 15:07:05 -0700 | [diff] [blame] | 96 | func noescape(p unsafe.Pointer) unsafe.Pointer { |
| 97 | x := uintptr(p) |
| 98 | return unsafe.Pointer(x ^ 0) |
| 99 | } |
Keith Randall | 483cb61 | 2014-08-07 13:58:42 -0700 | [diff] [blame] | 100 | |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 101 | func cgocallback(fn, frame unsafe.Pointer, framesize uintptr) |
| 102 | func gogo(buf *gobuf) |
| 103 | func gosave(buf *gobuf) |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 104 | func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32 |
Keith Randall | f440737 | 2014-09-03 08:49:43 -0700 | [diff] [blame] | 105 | |
| 106 | //go:noescape |
| 107 | func jmpdefer(fv *funcval, argp uintptr) |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 108 | func exit1(code int32) |
| 109 | func asminit() |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 110 | func setg(gg *g) |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 111 | func breakpoint() |
Keith Randall | b60d5e1 | 2014-10-21 14:46:07 -0700 | [diff] [blame] | 112 | |
Russ Cox | df027ac | 2014-12-30 13:59:55 -0500 | [diff] [blame] | 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 | |
Russ Cox | db58ab9 | 2014-09-04 21:12:31 -0400 | [diff] [blame] | 124 | func procyield(cycles uint32) |
Russ Cox | 25f6b02 | 2014-08-27 11:32:17 -0400 | [diff] [blame] | 125 | func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr) |
Keith Randall | f440737 | 2014-09-03 08:49:43 -0700 | [diff] [blame] | 126 | func goexit() |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 127 | |
| 128 | //go:noescape |
| 129 | func cas(ptr *uint32, old, new uint32) bool |
| 130 | |
Russ Cox | 7b4df8f | 2014-12-22 22:50:42 -0500 | [diff] [blame] | 131 | // NO go:noescape annotation; see atomic_pointer.go. |
Russ Cox | 9204821 | 2014-11-11 17:09:09 -0500 | [diff] [blame] | 132 | func casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool |
| 133 | |
| 134 | func nop() // call to prevent inlining of function body |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 135 | |
| 136 | //go:noescape |
| 137 | func casuintptr(ptr *uintptr, old, new uintptr) bool |
| 138 | |
| 139 | //go:noescape |
Russ Cox | 3a7f664 | 2014-08-29 16:20:48 -0400 | [diff] [blame] | 140 | func atomicstoreuintptr(ptr *uintptr, new uintptr) |
| 141 | |
| 142 | //go:noescape |
Russ Cox | 3a7f664 | 2014-08-29 16:20:48 -0400 | [diff] [blame] | 143 | func atomicloaduintptr(ptr *uintptr) uintptr |
| 144 | |
| 145 | //go:noescape |
Keith Randall | 47d6af2 | 2014-08-30 11:03:28 -0700 | [diff] [blame] | 146 | func atomicloaduint(ptr *uint) uint |
| 147 | |
| 148 | //go:noescape |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 149 | func setcallerpc(argp unsafe.Pointer, pc uintptr) |
| 150 | |
Russ Cox | 39bcbb3 | 2014-11-05 23:01:48 -0500 | [diff] [blame] | 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)) |
Alex Brainman | 55f19ed | 2014-11-18 09:55:15 +1100 | [diff] [blame] | 161 | // sp := getcallersp(unsafe.Pointer(&arg1)) |
Russ Cox | 39bcbb3 | 2014-11-05 23:01:48 -0500 | [diff] [blame] | 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 | |
Russ Cox | d21638b | 2014-08-27 21:59:49 -0400 | [diff] [blame] | 179 | //go:noescape |
| 180 | func getcallerpc(argp unsafe.Pointer) uintptr |
| 181 | |
| 182 | //go:noescape |
| 183 | func getcallersp(argp unsafe.Pointer) uintptr |
Russ Cox | 50199d7 | 2014-08-30 14:53:47 -0400 | [diff] [blame] | 184 | |
| 185 | //go:noescape |
| 186 | func asmcgocall(fn, arg unsafe.Pointer) |
| 187 | |
| 188 | //go:noescape |
Dmitriy Vyukov | c08d883 | 2014-09-04 14:40:40 -0400 | [diff] [blame] | 189 | func asmcgocall_errno(fn, arg unsafe.Pointer) int32 |
| 190 | |
Russ Cox | 9204821 | 2014-11-11 17:09:09 -0500 | [diff] [blame] | 191 | // argp used in Defer structs when there is no argp. |
Russ Cox | fa2af44 | 2014-09-02 15:12:53 -0400 | [diff] [blame] | 192 | const _NoArgs = ^uintptr(0) |
| 193 | |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 194 | func morestack() |
Russ Cox | 97f8386 | 2014-09-03 13:02:48 -0400 | [diff] [blame] | 195 | func rt0_go() |
Keith Randall | f440737 | 2014-09-03 08:49:43 -0700 | [diff] [blame] | 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() |
Keith Randall | 1a5e394 | 2014-09-09 14:32:53 -0700 | [diff] [blame] | 203 | |
Russ Cox | 7a524a1 | 2014-12-22 13:27:53 -0500 | [diff] [blame] | 204 | //go:linkname time_now time.now |
| 205 | func time_now() (sec int64, nsec int32) |
Russ Cox | cb6f5ac | 2014-10-15 13:12:16 -0400 | [diff] [blame] | 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) |
Russ Cox | 9204821 | 2014-11-11 17:09:09 -0500 | [diff] [blame] | 236 | |
Russ Cox | 656be31 | 2014-11-12 14:54:31 -0500 | [diff] [blame] | 237 | func systemstack_switch() |
Russ Cox | 8c3f640 | 2014-11-21 15:57:10 -0500 | [diff] [blame] | 238 | |
| 239 | func prefetcht0(addr uintptr) |
| 240 | func prefetcht1(addr uintptr) |
| 241 | func prefetcht2(addr uintptr) |
| 242 | func prefetchnta(addr uintptr) |
Russ Cox | 484f801 | 2015-02-19 13:38:46 -0500 | [diff] [blame] | 243 | |
| 244 | func unixnanotime() int64 { |
| 245 | sec, nsec := time_now() |
| 246 | return sec*1e9 + int64(nsec) |
| 247 | } |
| 248 | |
| 249 | // round n up to a multiple of a. a must be a power of 2. |
| 250 | func round(n, a uintptr) uintptr { |
| 251 | return (n + a - 1) &^ (a - 1) |
| 252 | } |