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