]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/stubs.go
[dev.regabi] reflect: support for register ABI on amd64 for reflect.(Value).Call
[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 func gogo(buf *gobuf)
173
174 //go:noescape
175 func jmpdefer(fv *funcval, argp uintptr)
176 func asminit()
177 func setg(gg *g)
178 func breakpoint()
179
180 // reflectcall calls fn with arguments described by stackArgs, stackArgsSize,
181 // frameSize, and regArgs.
182 //
183 // Arguments passed on the stack and space for return values passed on the stack
184 // must be laid out at the space pointed to by stackArgs (with total length
185 // stackArgsSize) according to the ABI.
186 //
187 // stackRetOffset must be some value <= stackArgsSize that indicates the
188 // offset within stackArgs where the return value space begins.
189 //
190 // frameSize is the total size of the argument frame at stackArgs and must
191 // therefore be >= stackArgsSize. It must include additional space for spilling
192 // register arguments for stack growth and preemption.
193 //
194 // TODO(mknyszek): Once we don't need the additional spill space, remove frameSize,
195 // since frameSize will be redundant with stackArgsSize.
196 //
197 // Arguments passed in registers must be laid out in regArgs according to the ABI.
198 // regArgs will hold any return values passed in registers after the call.
199 //
200 // reflectcall copies stack arguments from stackArgs to the goroutine stack, and
201 // then copies back stackArgsSize-stackRetOffset bytes back to the return space
202 // in stackArgs once fn has completed. It also "unspills" argument registers from
203 // regArgs before calling fn, and spills them back into regArgs immediately
204 // following the call to fn. If there are results being returned on the stack,
205 // the caller should pass the argument frame type as stackArgsType so that
206 // reflectcall can execute appropriate write barriers during the copy.
207 //
208 // reflectcall expects regArgs.ReturnIsPtr to be populated indicating which
209 // registers on the return path will contain Go pointers. It will then store
210 // these pointers in regArgs.Ptrs such that they are visible to the GC.
211 //
212 // Package reflect passes a frame type. In package runtime, there is only
213 // one call that copies results back, in callbackWrap in syscall_windows.go, and it
214 // does NOT pass a frame type, meaning there are no write barriers invoked. See that
215 // call site for justification.
216 //
217 // Package reflect accesses this symbol through a linkname.
218 //
219 // Arguments passed through to reflectcall do not escape. The type is used
220 // only in a very limited callee of reflectcall, the stackArgs are copied, and
221 // regArgs is only used in the reflectcall frame.
222 //go:noescape
223 func reflectcall(stackArgsType *_type, fn, stackArgs unsafe.Pointer, stackArgsSize, stackRetOffset, frameSize uint32, regArgs *abi.RegArgs)
224
225 func procyield(cycles uint32)
226
227 type neverCallThisFunction struct{}
228
229 // goexit is the return stub at the top of every goroutine call stack.
230 // Each goroutine stack is constructed as if goexit called the
231 // goroutine's entry point function, so that when the entry point
232 // function returns, it will return to goexit, which will call goexit1
233 // to perform the actual exit.
234 //
235 // This function must never be called directly. Call goexit1 instead.
236 // gentraceback assumes that goexit terminates the stack. A direct
237 // call on the stack will cause gentraceback to stop walking the stack
238 // prematurely and if there is leftover state it may panic.
239 func goexit(neverCallThisFunction)
240
241 // publicationBarrier performs a store/store barrier (a "publication"
242 // or "export" barrier). Some form of synchronization is required
243 // between initializing an object and making that object accessible to
244 // another processor. Without synchronization, the initialization
245 // writes and the "publication" write may be reordered, allowing the
246 // other processor to follow the pointer and observe an uninitialized
247 // object. In general, higher-level synchronization should be used,
248 // such as locking or an atomic pointer write. publicationBarrier is
249 // for when those aren't an option, such as in the implementation of
250 // the memory manager.
251 //
252 // There's no corresponding barrier for the read side because the read
253 // side naturally has a data dependency order. All architectures that
254 // Go supports or seems likely to ever support automatically enforce
255 // data dependency ordering.
256 func publicationBarrier()
257
258 // getcallerpc returns the program counter (PC) of its caller's caller.
259 // getcallersp returns the stack pointer (SP) of its caller's caller.
260 // The implementation may be a compiler intrinsic; there is not
261 // necessarily code implementing this on every platform.
262 //
263 // For example:
264 //
265 //      func f(arg1, arg2, arg3 int) {
266 //              pc := getcallerpc()
267 //              sp := getcallersp()
268 //      }
269 //
270 // These two lines find the PC and SP immediately following
271 // the call to f (where f will return).
272 //
273 // The call to getcallerpc and getcallersp must be done in the
274 // frame being asked about.
275 //
276 // The result of getcallersp is correct at the time of the return,
277 // but it may be invalidated by any subsequent call to a function
278 // that might relocate the stack in order to grow or shrink it.
279 // A general rule is that the result of getcallersp should be used
280 // immediately and can only be passed to nosplit functions.
281
282 //go:noescape
283 func getcallerpc() uintptr
284
285 //go:noescape
286 func getcallersp() uintptr // implemented as an intrinsic on all platforms
287
288 // getclosureptr returns the pointer to the current closure.
289 // getclosureptr can only be used in an assignment statement
290 // at the entry of a function. Moreover, go:nosplit directive
291 // must be specified at the declaration of caller function,
292 // so that the function prolog does not clobber the closure register.
293 // for example:
294 //
295 //      //go:nosplit
296 //      func f(arg1, arg2, arg3 int) {
297 //              dx := getclosureptr()
298 //      }
299 //
300 // The compiler rewrites calls to this function into instructions that fetch the
301 // pointer from a well-known register (DX on x86 architecture, etc.) directly.
302 func getclosureptr() uintptr
303
304 //go:noescape
305 func asmcgocall(fn, arg unsafe.Pointer) int32
306
307 func morestack()
308 func morestack_noctxt()
309 func rt0_go()
310
311 // return0 is a stub used to return 0 from deferproc.
312 // It is called at the very end of deferproc to signal
313 // the calling Go function that it should not jump
314 // to deferreturn.
315 // in asm_*.s
316 func return0()
317
318 // in asm_*.s
319 // not called directly; definitions here supply type information for traceback.
320 func call16(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
321 func call32(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
322 func call64(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
323 func call128(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
324 func call256(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
325 func call512(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
326 func call1024(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
327 func call2048(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
328 func call4096(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
329 func call8192(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
330 func call16384(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
331 func call32768(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
332 func call65536(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
333 func call131072(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
334 func call262144(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
335 func call524288(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
336 func call1048576(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
337 func call2097152(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
338 func call4194304(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
339 func call8388608(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
340 func call16777216(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
341 func call33554432(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
342 func call67108864(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
343 func call134217728(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
344 func call268435456(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
345 func call536870912(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
346 func call1073741824(typ, fn, arg unsafe.Pointer, n, retoffset uint32)
347
348 func systemstack_switch()
349
350 // alignUp rounds n up to a multiple of a. a must be a power of 2.
351 func alignUp(n, a uintptr) uintptr {
352         return (n + a - 1) &^ (a - 1)
353 }
354
355 // alignDown rounds n down to a multiple of a. a must be a power of 2.
356 func alignDown(n, a uintptr) uintptr {
357         return n &^ (a - 1)
358 }
359
360 // divRoundUp returns ceil(n / a).
361 func divRoundUp(n, a uintptr) uintptr {
362         // a is generally a power of two. This will get inlined and
363         // the compiler will optimize the division.
364         return (n + a - 1) / a
365 }
366
367 // checkASM reports whether assembly runtime checks have passed.
368 func checkASM() bool
369
370 func memequal_varlen(a, b unsafe.Pointer) bool
371
372 // bool2int returns 0 if x is false or 1 if x is true.
373 func bool2int(x bool) int {
374         // Avoid branches. In the SSA compiler, this compiles to
375         // exactly what you would want it to.
376         return int(uint8(*(*uint8)(unsafe.Pointer(&x))))
377 }
378
379 // abort crashes the runtime in situations where even throw might not
380 // work. In general it should do something a debugger will recognize
381 // (e.g., an INT3 on x86). A crash in abort is recognized by the
382 // signal handler, which will attempt to tear down the runtime
383 // immediately.
384 func abort()
385
386 // Called from compiled code; declared for vet; do NOT call from Go.
387 func gcWriteBarrier()
388 func duffzero()
389 func duffcopy()
390
391 // Called from linker-generated .initarray; declared for go vet; do NOT call from Go.
392 func addmoduledata()
393
394 // Injected by the signal handler for panicking signals. On many platforms it just
395 // jumps to sigpanic.
396 func sigpanic0()