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.
8 "runtime/internal/atomic"
13 // Should be a built-in for unsafe.Pointer?
15 func add(p unsafe
.Pointer
, x
uintptr) unsafe
.Pointer
{
16 return unsafe
.Pointer(uintptr(p
) + x
)
19 // getg returns the pointer to the current g.
20 // The compiler rewrites calls to this function into instructions
21 // that fetch the g directly (from TLS or from the dedicated register).
24 // mcall switches from the g to the g0 stack and invokes fn(g),
25 // where g is the goroutine that made the call.
26 // mcall saves g's current PC/SP in g->sched so that it can be restored later.
27 // It is up to fn to arrange for that later execution, typically by recording
28 // g in a data structure, causing something to call ready(g) later.
29 // mcall returns to the original goroutine g later, when g has been rescheduled.
30 // fn must not return at all; typically it ends by calling schedule, to let the m
31 // run other goroutines.
33 // mcall can only be called from g stacks (not g0, not gsignal).
35 // This must NOT be go:noescape: if fn is a stack-allocated closure,
36 // fn puts g on a run queue, and g executes before fn returns, the
37 // closure will be invalidated while it is still executing.
38 func mcall(fn
func(*g
))
40 // systemstack runs fn on a system stack.
42 // It is common to use a func literal as the argument, in order
43 // to share inputs and outputs with the code around the call
47 // systemstack(func() {
52 // For the gc toolchain this permits running a function that requires
53 // additional stack space in a context where the stack can not be
54 // split. We don't really need additional stack space in gccgo, since
55 // stack splitting is handled separately. But to keep things looking
56 // the same, we do switch to the g0 stack here if necessary.
57 func systemstack(fn
func()) {
60 if gp
== mp
.g0 || gp
== mp
.gsignal
{
62 } else if gp
== mp
.curg
{
63 mcall(func(origg
*g
) {
72 func badsystemstack() {
73 throw("systemstack called from unexpected goroutine")
76 // memclrNoHeapPointers clears n bytes starting at ptr.
78 // Usually you should use typedmemclr. memclrNoHeapPointers should be
79 // used only when the caller knows that *ptr contains no heap pointers
82 // 1. *ptr is initialized memory and its type is pointer-free.
84 // 2. *ptr is uninitialized memory (e.g., memory that's being reused
85 // for a new allocation) and hence contains only "junk".
89 func memclrNoHeapPointers(ptr unsafe
.Pointer
, n
uintptr)
91 //go:linkname reflect_memclrNoHeapPointers reflect.memclrNoHeapPointers
92 func reflect_memclrNoHeapPointers(ptr unsafe
.Pointer
, n
uintptr) {
93 memclrNoHeapPointers(ptr
, n
)
96 // memmove copies n bytes from "from" to "to".
98 func memmove(to
, from unsafe
.Pointer
, n
uintptr)
100 //go:linkname reflect_memmove reflect.memmove
101 func reflect_memmove(to
, from unsafe
.Pointer
, n
uintptr) {
106 //extern __builtin_memcmp
107 func memcmp(a
, b unsafe
.Pointer
, size
uintptr) int32
109 // exported value for testing
110 var hashLoad
= loadFactor
113 func fastrand() uint32 {
116 mx
:= uint32(int32(fr
)>>31) & 0xa8888eef
123 func fastrandn(n
uint32) uint32 {
124 // This is similar to fastrand() % n, but faster.
125 // See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
126 return uint32(uint64(fastrand()) * uint64(n
) >> 32)
129 //go:linkname sync_fastrand sync.fastrand
130 func sync_fastrand() uint32 { return fastrand() }
134 func memequal(a
, b unsafe
.Pointer
, size
uintptr) bool
136 // noescape hides a pointer from escape analysis. noescape is
137 // the identity function but escape analysis doesn't think the
138 // output depends on the input. noescape is inlined and currently
139 // compiles down to zero instructions.
142 func noescape(p unsafe
.Pointer
) unsafe
.Pointer
{
144 return unsafe
.Pointer(x
^ 0)
148 func jmpdefer(fv
*funcval
, argp
uintptr)
149 func exit1(code
int32)
152 //extern __builtin_trap
157 //go:linkname reflectcall reflect.call
158 func reflectcall(fntype
*functype
, fn
*funcval
, isInterface
, isMethod
bool, params
, results
*unsafe
.Pointer
)
160 func procyield(cycles
uint32)
162 type neverCallThisFunction
struct{}
164 // goexit is the return stub at the top of every goroutine call stack.
165 // Each goroutine stack is constructed as if goexit called the
166 // goroutine's entry point function, so that when the entry point
167 // function returns, it will return to goexit, which will call goexit1
168 // to perform the actual exit.
170 // This function must never be called directly. Call goexit1 instead.
171 // gentraceback assumes that goexit terminates the stack. A direct
172 // call on the stack will cause gentraceback to stop walking the stack
173 // prematurely and if there is leftover state it may panic.
174 func goexit(neverCallThisFunction
)
176 // publicationBarrier performs a store/store barrier (a "publication"
177 // or "export" barrier). Some form of synchronization is required
178 // between initializing an object and making that object accessible to
179 // another processor. Without synchronization, the initialization
180 // writes and the "publication" write may be reordered, allowing the
181 // other processor to follow the pointer and observe an uninitialized
182 // object. In general, higher-level synchronization should be used,
183 // such as locking or an atomic pointer write. publicationBarrier is
184 // for when those aren't an option, such as in the implementation of
185 // the memory manager.
187 // There's no corresponding barrier for the read side because the read
188 // side naturally has a data dependency order. All architectures that
189 // Go supports or seems likely to ever support automatically enforce
190 // data dependency ordering.
191 func publicationBarrier()
193 // getcallerpc returns the program counter (PC) of its caller's caller.
194 // getcallersp returns the stack pointer (SP) of its caller's caller.
195 // For both, the argp must be a pointer to the caller's first function argument.
196 // The implementation may or may not use argp, depending on
201 // func f(arg1, arg2, arg3 int) {
202 // pc := getcallerpc(unsafe.Pointer(&arg1))
203 // sp := getcallersp(unsafe.Pointer(&arg1))
206 // These two lines find the PC and SP immediately following
207 // the call to f (where f will return).
209 // The call to getcallerpc and getcallersp must be done in the
210 // frame being asked about. It would not be correct for f to pass &arg1
211 // to another function g and let g call getcallerpc/getcallersp.
212 // The call inside g might return information about g's caller or
213 // information about f's caller or complete garbage.
215 // The result of getcallersp is correct at the time of the return,
216 // but it may be invalidated by any subsequent call to a function
217 // that might relocate the stack in order to grow or shrink it.
218 // A general rule is that the result of getcallersp should be used
219 // immediately and can only be passed to nosplit functions.
222 func getcallerpc(argp unsafe
.Pointer
) uintptr
225 func getcallersp(argp unsafe
.Pointer
) uintptr
227 func asmcgocall(fn
, arg unsafe
.Pointer
) int32 {
232 // argp used in Defer structs when there is no argp.
233 const _NoArgs
= ^uintptr(0)
235 //extern __builtin_prefetch
236 func prefetch(addr unsafe
.Pointer
, rw
int32, locality
int32)
238 func prefetcht0(addr
uintptr) {
239 prefetch(unsafe
.Pointer(addr
), 0, 3)
242 func prefetcht1(addr
uintptr) {
243 prefetch(unsafe
.Pointer(addr
), 0, 2)
246 func prefetcht2(addr
uintptr) {
247 prefetch(unsafe
.Pointer(addr
), 0, 1)
250 func prefetchnta(addr
uintptr) {
251 prefetch(unsafe
.Pointer(addr
), 0, 0)
254 // round n up to a multiple of a. a must be a power of 2.
255 func round(n
, a
uintptr) uintptr {
256 return (n
+ a
- 1) &^ (a
- 1)
259 // checkASM returns whether assembly runtime checks have passed.
260 func checkASM() bool {
264 func eqstring(x
, y
string) bool {
265 a
:= stringStructOf(&x
)
266 b
:= stringStructOf(&y
)
273 return memequal(a
.str
, b
.str
, uintptr(a
.len))
276 // For gccgo this is in the C code.
279 // For gccgo this can be called directly.
281 func syscall(trap
uintptr, a1
, a2
, a3
, a4
, a5
, a6
uintptr) uintptr
283 // For gccgo, to communicate from the C code to the Go code.
284 //go:linkname setIsCgo runtime.setIsCgo
289 // For gccgo, to communicate from the C code to the Go code.
290 //go:linkname setCpuidECX runtime.setCpuidECX
291 func setCpuidECX(v
uint32) {
295 // For gccgo, to communicate from the C code to the Go code.
296 //go:linkname setSupportAES runtime.setSupportAES
297 func setSupportAES(v
bool) {
301 // Here for gccgo until we port atomic_pointer.go and mgc.go.
303 func casp(ptr
*unsafe
.Pointer
, old
, new unsafe
.Pointer
) bool {
304 if !atomic
.Casp1((*unsafe
.Pointer
)(noescape(unsafe
.Pointer(ptr
))), noescape(old
), new) {
310 // Here for gccgo until we port lock_*.go.
312 func unlock(l
*mutex
)
317 // Temporary for gccgo until we port proc.go.
318 func entersyscall(int32)
319 func entersyscallblock(int32)
321 // Here for gccgo until we port mgc.go.
324 // For gccgo to call from C code, so that the C code and the Go code
325 // can share the memstats variable for now.
326 //go:linkname getMstats runtime.getMstats
327 func getMstats() *mstats
{
331 // Temporary for gccgo until we port mem_GOOS.go.
332 func sysAlloc(n
uintptr, sysStat
*uint64) unsafe
.Pointer
333 func sysFree(v unsafe
.Pointer
, n
uintptr, sysStat
*uint64)
335 // Temporary for gccgo until we port malloc.go
336 func persistentalloc(size
, align
uintptr, sysStat
*uint64) unsafe
.Pointer
338 // Temporary for gccgo until we port mheap.go
339 func setprofilebucket(p unsafe
.Pointer
, b
*bucket
)
341 // Temporary for gccgo until we port atomic_pointer.go.
343 func atomicstorep(ptr unsafe
.Pointer
, new unsafe
.Pointer
) {
344 atomic
.StorepNoWB(noescape(ptr
), new)
347 // Get signal trampoline, written in C.
348 func getSigtramp() uintptr
350 // The sa_handler field is generally hidden in a union, so use C accessors.
351 func getSigactionHandler(*_sigaction
) uintptr
352 func setSigactionHandler(*_sigaction
, uintptr)
354 // Retrieve fields from the siginfo_t and ucontext_t pointers passed
355 // to a signal handler using C, as they are often hidden in a union.
356 // Returns and, if available, PC where signal occurred.
357 func getSiginfo(*_siginfo_t
, unsafe
.Pointer
) (sigaddr
uintptr, sigpc
uintptr)
359 // Implemented in C for gccgo.
360 func dumpregs(*_siginfo_t
, unsafe
.Pointer
)
362 // Temporary for gccgo until we port proc.go.
363 //go:linkname getsched runtime.getsched
364 func getsched() *schedt
{
368 // Temporary for gccgo until we port proc.go.
369 //go:linkname getCgoHasExtraM runtime.getCgoHasExtraM
370 func getCgoHasExtraM() *bool {
374 // Temporary for gccgo until we port proc.go.
375 //go:linkname getAllP runtime.getAllP
380 // Temporary for gccgo until we port proc.go.
381 //go:linkname allocg runtime.allocg
386 // Temporary for gccgo until we port the garbage collector.
387 //go:linkname getallglen runtime.getallglen
388 func getallglen() uintptr {
392 // Temporary for gccgo until we port the garbage collector.
393 //go:linkname getallg runtime.getallg
394 func getallg(i
int) *g
{
398 // Temporary for gccgo until we port the garbage collector.
399 //go:linkname getallm runtime.getallm
404 // Throw and rethrow an exception.
405 func throwException()
406 func rethrowException()
408 // Fetch the size and required alignment of the _Unwind_Exception type
409 // used by the stack unwinder.
410 func unwindExceptionSize() uintptr
412 // Temporary for gccgo until C code no longer needs it.
414 //go:linkname getPanicking runtime.getPanicking
415 func getPanicking() uint32 {
419 // Called by C code to set the number of CPUs.
420 //go:linkname setncpu runtime.setncpu
421 func setncpu(n
int32) {
425 // Called by C code to set the page size.
426 //go:linkname setpagesize runtime.setpagesize
427 func setpagesize(s
uintptr) {
428 if physPageSize
== 0 {
433 // Temporary for gccgo until we port mgc.go.
434 //go:linkname runtime_m0 runtime.runtime_m0
435 func runtime_m0() *m
{
439 // Temporary for gccgo until we port mgc.go.
440 //go:linkname runtime_g0 runtime.runtime_g0
441 func runtime_g0() *g
{
445 const uintptrMask
= 1<<(8*sys
.PtrSize
) - 1
447 type bitvector
struct {
452 // bool2int returns 0 if x is false or 1 if x is true.
453 func bool2int(x
bool) int {