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.
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?
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)
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).
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
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.
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)
88 // If onMsignal is called from a gsignal stack, it invokes fn directly,
89 // without a stack switch. Otherwise onMsignal behaves like onM.
92 func onM_signalok(fn func())
95 gothrow("onM called from signal goroutine")
98 // C functions that run on the M stack.
104 // More C functions that run on the M stack.
106 func mcacheRefill_m()
112 func gccheckmarkenable_m()
113 func gccheckmarkdisable_m()
114 func gcinstallmarkwb_m()
115 func gcinstalloffwb_m()
116 func gcmarknewobject_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()
132 func readmemstats_m()
133 func writeheapdump_m()
135 // memclr clears n bytes starting at ptr.
138 func memclr(ptr unsafe.Pointer, n uintptr)
140 // memmove copies n bytes from "from" to "to".
143 func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
149 func unlockOSThread()
151 // exported value for testing
152 var hashLoad = loadFactor
155 func fastrand1() uint32
159 func memeq(a, b unsafe.Pointer, size uintptr) bool
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.
167 func noescape(p unsafe.Pointer) unsafe.Pointer {
169 return unsafe.Pointer(x ^ 0)
173 func reentersyscall(pc uintptr, sp unsafe.Pointer)
174 func entersyscallblock()
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
185 func jmpdefer(fv *funcval, argp uintptr)
186 func exit1(code int32)
189 func exit(code int32)
191 func nanotime() int64
192 func usleep(usec uint32)
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
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)
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
211 func write(fd uintptr, p unsafe.Pointer, n int32) int32
214 func cas(ptr *uint32, old, new uint32) bool
217 func casuintptr(ptr *uintptr, old, new uintptr) bool
220 func atomicstoreuintptr(ptr *uintptr, new uintptr)
223 func atomicloaduintptr(ptr *uintptr) uintptr
226 func atomicloaduint(ptr *uint) uint
229 func setcallerpc(argp unsafe.Pointer, pc uintptr)
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
239 // func f(arg1, arg2, arg3 int) {
240 // pc := getcallerpc(unsafe.Pointer(&arg1))
241 // sp := getcallerpc(unsafe.Pointer(&arg2))
244 // These two lines find the PC and SP immediately following
245 // the call to f (where f will return).
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.
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.
260 func getcallerpc(argp unsafe.Pointer) uintptr
263 func getcallersp(argp unsafe.Pointer) uintptr
266 func asmcgocall(fn, arg unsafe.Pointer)
269 func asmcgocall_errno(fn, arg unsafe.Pointer) int32
272 func open(name *byte, mode, perm int32) int32
275 func gotraceback(*bool) int32
277 const _NoArgs = ^uintptr(0)
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
292 // thunk to call time.now.
293 func timenow() (sec int64, nsec int32)
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)