]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/stubs.go
[dev.typeparams] all: merge master (06b86e9) into dev.typeparams
[gostls13.git] / src / runtime / stubs.go
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         "internal/abi"
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 var badsystemstackMsg = "fatal: systemstack called from unexpected goroutine"
60
61 //go:nosplit
62 //go:nowritebarrierrec
63 func badsystemstack() {
64         sp := stringStructOf(&badsystemstackMsg)
65         write(2, sp.str, int32(sp.len))
66 }
67
68 // memclrNoHeapPointers clears n bytes starting at ptr.
69 //
70 // Usually you should use typedmemclr. memclrNoHeapPointers should be
71 // used only when the caller knows that *ptr contains no heap pointers
72 // because either:
73 //
74 // *ptr is initialized memory and its type is pointer-free, or
75 //
76 // *ptr is uninitialized memory (e.g., memory that's being reused
77 // for a new allocation) and hence contains only "junk".
78 //
79 // memclrNoHeapPointers ensures that if ptr is pointer-aligned, and n
80 // is a multiple of the pointer size, then any pointer-aligned,
81 // pointer-sized portion is cleared atomically. Despite the function
82 // name, this is necessary because this function is the underlying
83 // implementation of typedmemclr and memclrHasPointers. See the doc of
84 // memmove for more details.
85 //
86 // The (CPU-specific) implementations of this function are in memclr_*.s.
87 //
88 //go:noescape
89 func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr)
90
91 //go:linkname reflect_memclrNoHeapPointers reflect.memclrNoHeapPointers
92 func reflect_memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) {
93         memclrNoHeapPointers(ptr, n)
94 }
95
96 // memmove copies n bytes from "from" to "to".
97 //
98 // memmove ensures that any pointer in "from" is written to "to" with
99 // an indivisible write, so that racy reads cannot observe a
100 // half-written pointer. This is necessary to prevent the garbage
101 // collector from observing invalid pointers, and differs from memmove
102 // in unmanaged languages. However, memmove is only required to do
103 // this if "from" and "to" may contain pointers, which can only be the
104 // case if "from", "to", and "n" are all be word-aligned.
105 //
106 // Implementations are in memmove_*.s.
107 //
108 //go:noescape
109 func memmove(to, from unsafe.Pointer, n uintptr)
110
111 //go:linkname reflect_memmove reflect.memmove
112 func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
113         memmove(to, from, n)
114 }
115
116 // exported value for testing
117 var hashLoad = float32(loadFactorNum) / float32(loadFactorDen)
118
119 //go:nosplit
120 func fastrand() uint32 {
121         mp := getg().m
122         // Implement xorshift64+: 2 32-bit xorshift sequences added together.
123         // Shift triplet [17,7,16] was calculated as indicated in Marsaglia's
124         // Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
125         // This generator passes the SmallCrush suite, part of TestU01 framework:
126         // http://simul.iro.umontreal.ca/testu01/tu01.html
127         s1, s0 := mp.fastrand[0], mp.fastrand[1]
128         s1 ^= s1 << 17
129         s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16
130         mp.fastrand[0], mp.fastrand[1] = s0, s1
131         return s0 + s1
132 }
133
134 //go:nosplit
135 func fastrandn(n uint32) uint32 {
136         // This is similar to fastrand() % n, but faster.
137         // See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
138         return uint32(uint64(fastrand()) * uint64(n) >> 32)
139 }
140
141 //go:linkname sync_fastrand sync.fastrand
142 func sync_fastrand() uint32 { return fastrand() }
143
144 //go:linkname net_fastrand net.fastrand
145 func net_fastrand() uint32 { return fastrand() }
146
147 //go:linkname os_fastrand os.fastrand
148 func os_fastrand() uint32 { return fastrand() }
149
150 // in internal/bytealg/equal_*.s
151 //go:noescape
152 func memequal(a, b unsafe.Pointer, size uintptr) bool
153
154 // noescape hides a pointer from escape analysis.  noescape is
155 // the identity function but escape analysis doesn't think the
156 // output depends on the input.  noescape is inlined and currently
157 // compiles down to zero instructions.
158 // USE CAREFULLY!
159 //go:nosplit
160 func noescape(p unsafe.Pointer) unsafe.Pointer {
161         x := uintptr(p)
162         return unsafe.Pointer(x ^ 0)
163 }
164
165 // Not all cgocallback frames are actually cgocallback,
166 // so not all have these arguments. Mark them uintptr so that the GC
167 // does not misinterpret memory when the arguments are not present.
168 // cgocallback is not called from Go, only from crosscall2.
169 // This in turn calls cgocallbackg, which is where we'll find
170 // pointer-declared arguments.
171 func cgocallback(fn, frame, ctxt uintptr)
172
173 func gogo(buf *gobuf)
174
175 //go:noescape
176 func jmpdefer(fv *funcval, argp uintptr)
177 func asminit()
178 func setg(gg *g)
179 func breakpoint()
180
181 // reflectcall calls fn with arguments described by stackArgs, stackArgsSize,
182 // frameSize, and regArgs.
183 //
184 // Arguments passed on the stack and space for return values passed on the stack
185 // must be laid out at the space pointed to by stackArgs (with total length
186 // stackArgsSize) according to the ABI.
187 //
188 // stackRetOffset must be some value <= stackArgsSize that indicates the
189 // offset within stackArgs where the return value space begins.
190 //
191 // frameSize is the total size of the argument frame at stackArgs and must
192 // therefore be >= stackArgsSize. It must include additional space for spilling
193 // register arguments for stack growth and preemption.
194 //
195 // TODO(mknyszek): Once we don't need the additional spill space, remove frameSize,
196 // since frameSize will be redundant with stackArgsSize.
197 //
198 // Arguments passed in registers must be laid out in regArgs according to the ABI.
199 // regArgs will hold any return values passed in registers after the call.
200 //
201 // reflectcall copies stack arguments from stackArgs to the goroutine stack, and
202 // then copies back stackArgsSize-stackRetOffset bytes back to the return space
203 // in stackArgs once fn has completed. It also "unspills" argument registers from
204 // regArgs before calling fn, and spills them back into regArgs immediately
205 // following the call to fn. If there are results being returned on the stack,
206 // the caller should pass the argument frame type as stackArgsType so that
207 // reflectcall can execute appropriate write barriers during the copy.
208 //
209 // reflectcall expects regArgs.ReturnIsPtr to be populated indicating which
210 // registers on the return path will contain Go pointers. It will then store
211 // these pointers in regArgs.Ptrs such that they are visible to the GC.
212 //
213 // Package reflect passes a frame type. In package runtime, there is only
214 // one call that copies results back, in callbackWrap in syscall_windows.go, and it
215 // does NOT pass a frame type, meaning there are no write barriers invoked. See that
216 // call site for justification.
217 //
218 // Package reflect accesses this symbol through a linkname.
219 //
220 // Arguments passed through to reflectcall do not escape. The type is used
221 // only in a very limited callee of reflectcall, the stackArgs are copied, and
222 // regArgs is only used in the reflectcall frame.
223 //go:noescape
224 func reflectcall(stackArgsType *_type, fn, stackArgs unsafe.Pointer, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs)
225
226 func procyield(cycles uint32)
227
228 type neverCallThisFunction struct{}
229
230 // goexit is the return stub at the top of every goroutine call stack.
231 // Each goroutine stack is constructed as if goexit called the
232 // goroutine's entry point function, so that when the entry point
233 // function returns, it will return to goexit, which will call goexit1
234 // to perform the actual exit.
235 //
236 // This function must never be called directly. Call goexit1 instead.
237 // gentraceback assumes that goexit terminates the stack. A direct
238 // call on the stack will cause gentraceback to stop walking the stack
239 // prematurely and if there is leftover state it may panic.
240 func goexit(neverCallThisFunction)
241
242 // publicationBarrier performs a store/store barrier (a "publication"
243 // or "export" barrier). Some form of synchronization is required
244 // between initializing an object and making that object accessible to
245 // another processor. Without synchronization, the initialization
246 // writes and the "publication" write may be reordered, allowing the
247 // other processor to follow the pointer and observe an uninitialized
248 // object. In general, higher-level synchronization should be used,
249 // such as locking or an atomic pointer write. publicationBarrier is
250 // for when those aren't an option, such as in the implementation of
251 // the memory manager.
252 //
253 // There's no corresponding barrier for the read side because the read
254 // side naturally has a data dependency order. All architectures that
255 // Go supports or seems likely to ever support automatically enforce
256 // data dependency ordering.
257 func publicationBarrier()
258
259 // getcallerpc returns the program counter (PC) of its caller's caller.
260 // getcallersp returns the stack pointer (SP) of its caller's caller.
261 // The implementation may be a compiler intrinsic; there is not
262 // necessarily code implementing this on every platform.
263 //
264 // For example:
265 //
266 //      func f(arg1, arg2, arg3 int) {
267 //              pc := getcallerpc()
268 //              sp := getcallersp()
269 //      }
270 //
271 // These two lines find the PC and SP immediately following
272 // the call to f (where f will return).
273 //
274 // The call to getcallerpc and getcallersp must be done in the
275 // frame being asked about.
276 //
277 // The result of getcallersp is correct at the time of the return,
278 // but it may be invalidated by any subsequent call to a function
279 // that might relocate the stack in order to grow or shrink it.
280 // A general rule is that the result of getcallersp should be used
281 // immediately and can only be passed to nosplit functions.
282
283 //go:noescape
284 func getcallerpc() uintptr
285
286 //go:noescape
287 func getcallersp() uintptr // implemented as an intrinsic on all platforms
288
289 // getclosureptr returns the pointer to the current closure.
290 // getclosureptr can only be used in an assignment statement
291 // at the entry of a function. Moreover, go:nosplit directive
292 // must be specified at the declaration of caller function,
293 // so that the function prolog does not clobber the closure register.
294 // for example:
295 //
296 //      //go:nosplit
297 //      func f(arg1, arg2, arg3 int) {
298 //              dx := getclosureptr()
299 //      }
300 //
301 // The compiler rewrites calls to this function into instructions that fetch the
302 // pointer from a well-known register (DX on x86 architecture, etc.) directly.
303 func getclosureptr() uintptr
304
305 //go:noescape
306 func asmcgocall(fn, arg unsafe.Pointer) int32
307
308 func morestack()
309 func morestack_noctxt()
310 func rt0_go()
311
312 // return0 is a stub used to return 0 from deferproc.
313 // It is called at the very end of deferproc to signal
314 // the calling Go function that it should not jump
315 // to deferreturn.
316 // in asm_*.s
317 func return0()
318
319 // in asm_*.s
320 // not called directly; definitions here supply type information for traceback.
321 func call16(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
322 func call32(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
323 func call64(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
324 func call128(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
325 func call256(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
326 func call512(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
327 func call1024(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
328 func call2048(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
329 func call4096(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
330 func call8192(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
331 func call16384(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
332 func call32768(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
333 func call65536(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
334 func call131072(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
335 func call262144(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
336 func call524288(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
337 func call1048576(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
338 func call2097152(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
339 func call4194304(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
340 func call8388608(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
341 func call16777216(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
342 func call33554432(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
343 func call67108864(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
344 func call134217728(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
345 func call268435456(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
346 func call536870912(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
347 func call1073741824(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
348
349 func systemstack_switch()
350
351 // alignUp rounds n up to a multiple of a. a must be a power of 2.
352 func alignUp(n, a uintptr) uintptr {
353         return (n + a - 1) &^ (a - 1)
354 }
355
356 // alignDown rounds n down to a multiple of a. a must be a power of 2.
357 func alignDown(n, a uintptr) uintptr {
358         return n &^ (a - 1)
359 }
360
361 // divRoundUp returns ceil(n / a).
362 func divRoundUp(n, a uintptr) uintptr {
363         // a is generally a power of two. This will get inlined and
364         // the compiler will optimize the division.
365         return (n + a - 1) / a
366 }
367
368 // checkASM reports whether assembly runtime checks have passed.
369 func checkASM() bool
370
371 func memequal_varlen(a, b unsafe.Pointer) bool
372
373 // bool2int returns 0 if x is false or 1 if x is true.
374 func bool2int(x bool) int {
375         // Avoid branches. In the SSA compiler, this compiles to
376         // exactly what you would want it to.
377         return int(uint8(*(*uint8)(unsafe.Pointer(&x))))
378 }
379
380 // abort crashes the runtime in situations where even throw might not
381 // work. In general it should do something a debugger will recognize
382 // (e.g., an INT3 on x86). A crash in abort is recognized by the
383 // signal handler, which will attempt to tear down the runtime
384 // immediately.
385 func abort()
386
387 // Called from compiled code; declared for vet; do NOT call from Go.
388 func gcWriteBarrier()
389 func duffzero()
390 func duffcopy()
391
392 // Called from linker-generated .initarray; declared for go vet; do NOT call from Go.
393 func addmoduledata()
394
395 // Injected by the signal handler for panicking signals. On many platforms it just
396 // jumps to sigpanic.
397 func sigpanic0()