2015-05-29 François Dumont fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / go / runtime / stubs.go
blobfe8f9c9222aba574260cd9f132515aba18fee49e
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.
5 package runtime
7 import "unsafe"
9 // Declarations for runtime services implemented in C or assembly.
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
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)
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)
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
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.
42 // mcall can only be called from g stacks (not g0, not gsignal).
43 //go:noescape
44 func mcall(fn func(*g))
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.
58 // If onM is called from a g0 stack, it invokes fn and returns,
59 // without any stack switches.
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.
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.
73 //go:noescape
74 func onM(fn func())
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.
88 // If onMsignal is called from a gsignal stack, it invokes fn directly,
89 // without a stack switch. Otherwise onMsignal behaves like onM.
91 //go:noescape
92 func onM_signalok(fn func())
94 func badonm() {
95 gothrow("onM called from signal goroutine")
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)
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 scavenge_m()
110 func setFinalizer_m()
111 func removeFinalizer_m()
112 func markallocated_m()
113 func unrollgcprog_m()
114 func unrollgcproginplace_m()
115 func setgcpercent_m()
116 func setmaxthreads_m()
117 func ready_m()
118 func deferproc_m()
119 func goexit_m()
120 func startpanic_m()
121 func dopanic_m()
122 func readmemstats_m()
123 func writeheapdump_m()
125 // memclr clears n bytes starting at ptr.
126 // in memclr_*.s
127 //go:noescape
128 func memclr(ptr unsafe.Pointer, n uintptr)
130 // memmove copies n bytes from "from" to "to".
131 // in memmove_*.s
132 //go:noescape
133 func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
135 func starttheworld()
136 func stoptheworld()
137 func newextram()
138 func lockOSThread()
139 func unlockOSThread()
141 // exported value for testing
142 var hashLoad = loadFactor
144 // in asm_*.s
145 func fastrand1() uint32
147 // in asm_*.s
148 //go:noescape
149 func memeq(a, b unsafe.Pointer, size uintptr) bool
151 // noescape hides a pointer from escape analysis. noescape is
152 // the identity function but escape analysis doesn't think the
153 // output depends on the input. noescape is inlined and currently
154 // compiles down to a single xor instruction.
155 // USE CAREFULLY!
156 //go:nosplit
157 func noescape(p unsafe.Pointer) unsafe.Pointer {
158 x := uintptr(p)
159 return unsafe.Pointer(x ^ 0)
162 func entersyscall()
163 func reentersyscall(pc uintptr, sp unsafe.Pointer)
164 func entersyscallblock()
165 func exitsyscall()
167 func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
168 func gogo(buf *gobuf)
169 func gosave(buf *gobuf)
170 func read(fd int32, p unsafe.Pointer, n int32) int32
171 func close(fd int32) int32
172 func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
174 //go:noescape
175 func jmpdefer(fv *funcval, argp uintptr)
176 func exit1(code int32)
177 func asminit()
178 func setg(gg *g)
179 func exit(code int32)
180 func breakpoint()
181 func nanotime() int64
182 func usleep(usec uint32)
184 // careful: cputicks is not guaranteed to be monotonic! In particular, we have
185 // noticed drift between cpus on certain os/arch combinations. See issue 8976.
186 func cputicks() int64
188 func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
189 func munmap(addr unsafe.Pointer, n uintptr)
190 func madvise(addr unsafe.Pointer, n uintptr, flags int32)
191 func reflectcall(fn, arg unsafe.Pointer, n uint32, retoffset uint32)
192 func osyield()
193 func procyield(cycles uint32)
194 func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
195 func readgogc() int32
196 func purgecachedstats(c *mcache)
197 func gostringnocopy(b *byte) string
198 func goexit()
200 //go:noescape
201 func write(fd uintptr, p unsafe.Pointer, n int32) int32
203 //go:noescape
204 func cas(ptr *uint32, old, new uint32) bool
206 //go:noescape
207 func casp(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
209 //go:noescape
210 func casuintptr(ptr *uintptr, old, new uintptr) bool
212 //go:noescape
213 func atomicstoreuintptr(ptr *uintptr, new uintptr)
215 //go:noescape
216 func atomicloaduintptr(ptr *uintptr) uintptr
218 //go:noescape
219 func atomicloaduint(ptr *uint) uint
221 //go:noescape
222 func setcallerpc(argp unsafe.Pointer, pc uintptr)
224 // getcallerpc returns the program counter (PC) of its caller's caller.
225 // getcallersp returns the stack pointer (SP) of its caller's caller.
226 // For both, the argp must be a pointer to the caller's first function argument.
227 // The implementation may or may not use argp, depending on
228 // the architecture.
230 // For example:
232 // func f(arg1, arg2, arg3 int) {
233 // pc := getcallerpc(unsafe.Pointer(&arg1))
234 // sp := getcallerpc(unsafe.Pointer(&arg2))
235 // }
237 // These two lines find the PC and SP immediately following
238 // the call to f (where f will return).
240 // The call to getcallerpc and getcallersp must be done in the
241 // frame being asked about. It would not be correct for f to pass &arg1
242 // to another function g and let g call getcallerpc/getcallersp.
243 // The call inside g might return information about g's caller or
244 // information about f's caller or complete garbage.
246 // The result of getcallersp is correct at the time of the return,
247 // but it may be invalidated by any subsequent call to a function
248 // that might relocate the stack in order to grow or shrink it.
249 // A general rule is that the result of getcallersp should be used
250 // immediately and can only be passed to nosplit functions.
252 //go:noescape
253 func getcallerpc(argp unsafe.Pointer) uintptr
255 //go:noescape
256 func getcallersp(argp unsafe.Pointer) uintptr
258 //go:noescape
259 func asmcgocall(fn, arg unsafe.Pointer)
261 //go:noescape
262 func asmcgocall_errno(fn, arg unsafe.Pointer) int32
264 //go:noescape
265 func open(name *byte, mode, perm int32) int32
267 //go:noescape
268 func gotraceback(*bool) int32
270 const _NoArgs = ^uintptr(0)
272 func newstack()
273 func newproc()
274 func morestack()
275 func mstart()
276 func rt0_go()
278 // return0 is a stub used to return 0 from deferproc.
279 // It is called at the very end of deferproc to signal
280 // the calling Go function that it should not jump
281 // to deferreturn.
282 // in asm_*.s
283 func return0()
285 // thunk to call time.now.
286 func timenow() (sec int64, nsec int32)
288 // in asm_*.s
289 // not called directly; definitions here supply type information for traceback.
290 func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
291 func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
292 func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
293 func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
294 func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
295 func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
296 func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
297 func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
298 func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
299 func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
300 func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
301 func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
302 func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
303 func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
304 func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
305 func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
306 func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
307 func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
308 func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
309 func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
310 func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
311 func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
312 func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
313 func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
314 func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
315 func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
316 func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)