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