]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/stubs.go
[dev.garbage] all: merge default (f38460037b72) into dev.garbage
[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 "unsafe"
8
9 // Declarations for runtime services implemented in C or assembly.
10
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
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 // n must be a power of 2
21 func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer {
22         delta := -uintptr(p) & (n - 1)
23         return unsafe.Pointer(uintptr(p) + delta)
24 }
25
26 // in runtime.c
27 func getg() *g
28 func acquirem() *m
29 func releasem(mp *m)
30 func gomcache() *mcache
31 func readgstatus(*g) uint32 // proc.c
32
33 // mcall switches from the g to the g0 stack and invokes fn(g),
34 // where g is the goroutine that made the call.
35 // mcall saves g's current PC/SP in g->sched so that it can be restored later.
36 // It is up to fn to arrange for that later execution, typically by recording
37 // g in a data structure, causing something to call ready(g) later.
38 // mcall returns to the original goroutine g later, when g has been rescheduled.
39 // fn must not return at all; typically it ends by calling schedule, to let the m
40 // run other goroutines.
41 //
42 // mcall can only be called from g stacks (not g0, not gsignal).
43 //go:noescape
44 func mcall(fn func(*g))
45
46 // onM switches from the g to the g0 stack and invokes fn().
47 // When fn returns, onM switches back to the g and returns,
48 // continuing execution on the g stack.
49 // If arguments must be passed to fn, they can be written to
50 // g->m->ptrarg (pointers) and g->m->scalararg (non-pointers)
51 // before the call and then consulted during fn.
52 // Similarly, fn can pass return values back in those locations.
53 // If fn is written in Go, it can be a closure, which avoids the need for
54 // ptrarg and scalararg entirely.
55 // After reading values out of ptrarg and scalararg it is conventional
56 // to zero them to avoid (memory or information) leaks.
57 //
58 // If onM is called from a g0 stack, it invokes fn and returns,
59 // without any stack switches.
60 //
61 // If onM is called from a gsignal stack, it crashes the program.
62 // The implication is that functions used in signal handlers must
63 // not use onM.
64 //
65 // NOTE(rsc): We could introduce a separate onMsignal that is
66 // like onM but if called from a gsignal stack would just run fn on
67 // that stack. The caller of onMsignal would be required to save the
68 // old values of ptrarg/scalararg and restore them when the call
69 // was finished, in case the signal interrupted an onM sequence
70 // in progress on the g or g0 stacks. Until there is a clear need for this,
71 // we just reject onM in signal handling contexts entirely.
72 //
73 //go:noescape
74 func onM(fn func())
75
76 // onMsignal is like onM but is allowed to be used in code that
77 // might run on the gsignal stack. Code running on a signal stack
78 // may be interrupting an onM sequence on the main stack, so
79 // if the onMsignal calling sequence writes to ptrarg/scalararg,
80 // it must first save the old values and then restore them when
81 // finished. As an exception to the rule, it is fine not to save and
82 // restore the values if the program is trying to crash rather than
83 // return from the signal handler.
84 // Once all the runtime is written in Go, there will be no ptrarg/scalararg
85 // and the distinction between onM and onMsignal (and perhaps mcall)
86 // can go away.
87 //
88 // If onMsignal is called from a gsignal stack, it invokes fn directly,
89 // without a stack switch. Otherwise onMsignal behaves like onM.
90 //
91 //go:noescape
92 func onM_signalok(fn func())
93
94 func badonm() {
95         gothrow("onM called from signal goroutine")
96 }
97
98 // C functions that run on the M stack.
99 // Call using mcall.
100 func gosched_m(*g)
101 func park_m(*g)
102 func recovery_m(*g)
103
104 // More C functions that run on the M stack.
105 // Call using onM.
106 func mcacheRefill_m()
107 func largeAlloc_m()
108 func gc_m()
109 func gcscan_m()
110 func gcmark_m()
111 func gccheckmark_m()
112 func gccheckmarkenable_m()
113 func gccheckmarkdisable_m()
114 func gcinstallmarkwb_m()
115 func gcinstalloffwb_m()
116 func gcmarknewobject_m()
117 func gcmarkwb_m()
118 func finishsweep_m()
119 func scavenge_m()
120 func setFinalizer_m()
121 func removeFinalizer_m()
122 func markallocated_m()
123 func unrollgcprog_m()
124 func unrollgcproginplace_m()
125 func setgcpercent_m()
126 func setmaxthreads_m()
127 func ready_m()
128 func deferproc_m()
129 func goexit_m()
130 func startpanic_m()
131 func dopanic_m()
132 func readmemstats_m()
133 func writeheapdump_m()
134
135 // memclr clears n bytes starting at ptr.
136 // in memclr_*.s
137 //go:noescape
138 func memclr(ptr unsafe.Pointer, n uintptr)
139
140 // memmove copies n bytes from "from" to "to".
141 // in memmove_*.s
142 //go:noescape
143 func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
144
145 func starttheworld()
146 func stoptheworld()
147 func newextram()
148 func lockOSThread()
149 func unlockOSThread()
150
151 // exported value for testing
152 var hashLoad = loadFactor
153
154 // in asm_*.s
155 func fastrand1() uint32
156
157 // in asm_*.s
158 //go:noescape
159 func memeq(a, b unsafe.Pointer, size uintptr) bool
160
161 // noescape hides a pointer from escape analysis.  noescape is
162 // the identity function but escape analysis doesn't think the
163 // output depends on the input.  noescape is inlined and currently
164 // compiles down to a single xor instruction.
165 // USE CAREFULLY!
166 //go:nosplit
167 func noescape(p unsafe.Pointer) unsafe.Pointer {
168         x := uintptr(p)
169         return unsafe.Pointer(x ^ 0)
170 }
171
172 func entersyscall()
173 func reentersyscall(pc uintptr, sp unsafe.Pointer)
174 func entersyscallblock()
175 func exitsyscall()
176
177 func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
178 func gogo(buf *gobuf)
179 func gosave(buf *gobuf)
180 func read(fd int32, p unsafe.Pointer, n int32) int32
181 func close(fd int32) int32
182 func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
183
184 //go:noescape
185 func jmpdefer(fv *funcval, argp uintptr)
186 func exit1(code int32)
187 func asminit()
188 func setg(gg *g)
189 func exit(code int32)
190 func breakpoint()
191 func nanotime() int64
192 func usleep(usec uint32)
193
194 // careful: cputicks is not guaranteed to be monotonic!  In particular, we have
195 // noticed drift between cpus on certain os/arch combinations.  See issue 8976.
196 func cputicks() int64
197
198 func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
199 func munmap(addr unsafe.Pointer, n uintptr)
200 func madvise(addr unsafe.Pointer, n uintptr, flags int32)
201 func reflectcall(fn, arg unsafe.Pointer, n uint32, retoffset uint32)
202 func osyield()
203 func procyield(cycles uint32)
204 func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
205 func readgogc() int32
206 func purgecachedstats(c *mcache)
207 func gostringnocopy(b *byte) string
208 func goexit()
209
210 //go:noescape
211 func write(fd uintptr, p unsafe.Pointer, n int32) int32
212
213 //go:noescape
214 func cas(ptr *uint32, old, new uint32) bool
215
216 //go:noescape
217 func casuintptr(ptr *uintptr, old, new uintptr) bool
218
219 //go:noescape
220 func atomicstoreuintptr(ptr *uintptr, new uintptr)
221
222 //go:noescape
223 func atomicloaduintptr(ptr *uintptr) uintptr
224
225 //go:noescape
226 func atomicloaduint(ptr *uint) uint
227
228 //go:noescape
229 func setcallerpc(argp unsafe.Pointer, pc uintptr)
230
231 // getcallerpc returns the program counter (PC) of its caller's caller.
232 // getcallersp returns the stack pointer (SP) of its caller's caller.
233 // For both, the argp must be a pointer to the caller's first function argument.
234 // The implementation may or may not use argp, depending on
235 // the architecture.
236 //
237 // For example:
238 //
239 //      func f(arg1, arg2, arg3 int) {
240 //              pc := getcallerpc(unsafe.Pointer(&arg1))
241 //              sp := getcallerpc(unsafe.Pointer(&arg2))
242 //      }
243 //
244 // These two lines find the PC and SP immediately following
245 // the call to f (where f will return).
246 //
247 // The call to getcallerpc and getcallersp must be done in the
248 // frame being asked about. It would not be correct for f to pass &arg1
249 // to another function g and let g call getcallerpc/getcallersp.
250 // The call inside g might return information about g's caller or
251 // information about f's caller or complete garbage.
252 //
253 // The result of getcallersp is correct at the time of the return,
254 // but it may be invalidated by any subsequent call to a function
255 // that might relocate the stack in order to grow or shrink it.
256 // A general rule is that the result of getcallersp should be used
257 // immediately and can only be passed to nosplit functions.
258
259 //go:noescape
260 func getcallerpc(argp unsafe.Pointer) uintptr
261
262 //go:noescape
263 func getcallersp(argp unsafe.Pointer) uintptr
264
265 //go:noescape
266 func asmcgocall(fn, arg unsafe.Pointer)
267
268 //go:noescape
269 func asmcgocall_errno(fn, arg unsafe.Pointer) int32
270
271 //go:noescape
272 func open(name *byte, mode, perm int32) int32
273
274 //go:noescape
275 func gotraceback(*bool) int32
276
277 const _NoArgs = ^uintptr(0)
278
279 func newstack()
280 func newproc()
281 func morestack()
282 func mstart()
283 func rt0_go()
284
285 // return0 is a stub used to return 0 from deferproc.
286 // It is called at the very end of deferproc to signal
287 // the calling Go function that it should not jump
288 // to deferreturn.
289 // in asm_*.s
290 func return0()
291
292 // thunk to call time.now.
293 func timenow() (sec int64, nsec int32)
294
295 // in asm_*.s
296 // not called directly; definitions here supply type information for traceback.
297 func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
298 func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
299 func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
300 func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
301 func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
302 func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
303 func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
304 func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
305 func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
306 func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
307 func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
308 func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
309 func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
310 func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
311 func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
312 func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
313 func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
314 func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
315 func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
316 func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
317 func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
318 func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
319 func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
320 func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
321 func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
322 func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
323 func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)