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()
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()
122 func readmemstats_m()
123 func writeheapdump_m()
125 // memclr clears n bytes starting at ptr.
128 func memclr(ptr unsafe
.Pointer
, n
uintptr)
130 // memmove copies n bytes from "from" to "to".
133 func memmove(to unsafe
.Pointer
, from unsafe
.Pointer
, n
uintptr)
139 func unlockOSThread()
141 // exported value for testing
142 var hashLoad
= loadFactor
145 func fastrand1() uint32
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.
157 func noescape(p unsafe
.Pointer
) unsafe
.Pointer
{
159 return unsafe
.Pointer(x
^ 0)
163 func reentersyscall(pc
uintptr, sp unsafe
.Pointer
)
164 func entersyscallblock()
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
175 func jmpdefer(fv
*funcval
, argp
uintptr)
176 func exit1(code
int32)
179 func exit(code
int32)
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)
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
201 func write(fd
uintptr, p unsafe
.Pointer
, n
int32) int32
204 func cas(ptr
*uint32, old
, new uint32) bool
207 func casp(ptr
*unsafe
.Pointer
, old
, new unsafe
.Pointer
) bool
210 func casuintptr(ptr
*uintptr, old
, new uintptr) bool
213 func atomicstoreuintptr(ptr
*uintptr, new uintptr)
216 func atomicloaduintptr(ptr
*uintptr) uintptr
219 func atomicloaduint(ptr
*uint) uint
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
232 // func f(arg1, arg2, arg3 int) {
233 // pc := getcallerpc(unsafe.Pointer(&arg1))
234 // sp := getcallerpc(unsafe.Pointer(&arg2))
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.
253 func getcallerpc(argp unsafe
.Pointer
) uintptr
256 func getcallersp(argp unsafe
.Pointer
) uintptr
259 func asmcgocall(fn
, arg unsafe
.Pointer
)
262 func asmcgocall_errno(fn
, arg unsafe
.Pointer
) int32
265 func open(name
*byte, mode
, perm
int32) int32
268 func gotraceback(*bool) int32
270 const _NoArgs
= ^uintptr(0)
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
285 // thunk to call time.now.
286 func timenow() (sec
int64, nsec
int32)
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)