blob: 99d8dd45e211c1150abc3d7bed8cf40092f25a4c [file] [log] [blame]
Keith Randall61dca942014-06-16 23:03:03 -07001// 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
5package runtime
6
7import "unsafe"
8
9// Declarations for runtime services implemented in C or assembly.
Keith Randall61dca942014-06-16 23:03:03 -070010
Aram Hăvărneanu846ee042015-03-08 14:20:20 +010011const ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
12const regSize = 4 << (^uintreg(0) >> 63) // unsafe.Sizeof(uintreg(0)) but an ideal const
13const spAlign = 1*(1-goarch_arm64) + 16*goarch_arm64 // SP alignment: 1 normally, 16 for ARM64
Keith Randall0c6b55e2014-07-16 14:16:19 -070014
Keith Randall0c6b55e2014-07-16 14:16:19 -070015// Should be a built-in for unsafe.Pointer?
Keith Randallf4407372014-09-03 08:49:43 -070016//go:nosplit
Keith Randall0c6b55e2014-07-16 14:16:19 -070017func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
18 return unsafe.Pointer(uintptr(p) + x)
19}
20
Dmitriy Vyukov684de042014-08-21 20:41:09 +040021func getg() *g
Keith Randall4aa50432014-07-30 09:01:52 -070022
Russ Cox32ecf572014-09-04 00:10:10 -040023// mcall switches from the g to the g0 stack and invokes fn(g),
24// where g is the goroutine that made the call.
25// mcall saves g's current PC/SP in g->sched so that it can be restored later.
26// It is up to fn to arrange for that later execution, typically by recording
27// g in a data structure, causing something to call ready(g) later.
28// mcall returns to the original goroutine g later, when g has been rescheduled.
29// fn must not return at all; typically it ends by calling schedule, to let the m
30// run other goroutines.
31//
32// mcall can only be called from g stacks (not g0, not gsignal).
33//go:noescape
34func mcall(fn func(*g))
35
Russ Cox656be312014-11-12 14:54:31 -050036// systemstack runs fn on a system stack.
37// If systemstack is called from the per-OS-thread (g0) stack, or
38// if systemstack is called from the signal handling (gsignal) stack,
39// systemstack calls fn directly and returns.
40// Otherwise, systemstack is being called from the limited stack
41// of an ordinary goroutine. In this case, systemstack switches
42// to the per-OS-thread stack, calls fn, and switches back.
43// It is common to use a func literal as the argument, in order
44// to share inputs and outputs with the code around the call
45// to system stack:
Russ Cox32ecf572014-09-04 00:10:10 -040046//
Russ Cox656be312014-11-12 14:54:31 -050047// ... set up y ...
48// systemstack(func() {
49// x = bigcall(y)
50// })
51// ... use x ...
Russ Cox32ecf572014-09-04 00:10:10 -040052//
53//go:noescape
Russ Cox656be312014-11-12 14:54:31 -050054func systemstack(fn func())
Keith Randalle359bea2014-08-06 14:33:57 -070055
Russ Cox656be312014-11-12 14:54:31 -050056func badsystemstack() {
Keith Randallb2a950b2014-12-27 20:58:00 -080057 throw("systemstack called from unexpected goroutine")
Russ Cox32ecf572014-09-04 00:10:10 -040058}
59
Keith Randall0c6b55e2014-07-16 14:16:19 -070060// memclr clears n bytes starting at ptr.
61// in memclr_*.s
Keith Randall483cb612014-08-07 13:58:42 -070062//go:noescape
Keith Randall0c6b55e2014-07-16 14:16:19 -070063func memclr(ptr unsafe.Pointer, n uintptr)
64
Dmitry Vyukov67f8a812014-12-22 22:31:55 +030065//go:linkname reflect_memclr reflect.memclr
66func reflect_memclr(ptr unsafe.Pointer, n uintptr) {
67 memclr(ptr, n)
68}
69
Keith Randall0c6b55e2014-07-16 14:16:19 -070070// memmove copies n bytes from "from" to "to".
71// in memmove_*.s
Keith Randall483cb612014-08-07 13:58:42 -070072//go:noescape
Russ Cox7a524a12014-12-22 13:27:53 -050073func memmove(to, from unsafe.Pointer, n uintptr)
74
75//go:linkname reflect_memmove reflect.memmove
76func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
77 memmove(to, from, n)
78}
Keith Randall0c6b55e2014-07-16 14:16:19 -070079
Keith Randall0c6b55e2014-07-16 14:16:19 -070080// exported value for testing
81var hashLoad = loadFactor
82
83// in asm_*.s
Keith Randall1a5e3942014-09-09 14:32:53 -070084func fastrand1() uint32
85
86// in asm_*.s
Keith Randall0c6b55e2014-07-16 14:16:19 -070087//go:noescape
Keith Randall7aa4e5a2014-08-07 14:52:55 -070088func memeq(a, b unsafe.Pointer, size uintptr) bool
Keith Randall0c6b55e2014-07-16 14:16:19 -070089
Keith Randalla2a97682014-07-31 15:07:05 -070090// noescape hides a pointer from escape analysis. noescape is
91// the identity function but escape analysis doesn't think the
92// output depends on the input. noescape is inlined and currently
93// compiles down to a single xor instruction.
94// USE CAREFULLY!
Keith Randallf4407372014-09-03 08:49:43 -070095//go:nosplit
Keith Randalla2a97682014-07-31 15:07:05 -070096func noescape(p unsafe.Pointer) unsafe.Pointer {
97 x := uintptr(p)
98 return unsafe.Pointer(x ^ 0)
99}
Keith Randall483cb612014-08-07 13:58:42 -0700100
Russ Cox25f6b022014-08-27 11:32:17 -0400101func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
102func gogo(buf *gobuf)
103func gosave(buf *gobuf)
Russ Cox25f6b022014-08-27 11:32:17 -0400104func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
Keith Randallf4407372014-09-03 08:49:43 -0700105
106//go:noescape
107func jmpdefer(fv *funcval, argp uintptr)
Russ Cox25f6b022014-08-27 11:32:17 -0400108func exit1(code int32)
109func asminit()
Russ Cox25f6b022014-08-27 11:32:17 -0400110func setg(gg *g)
Russ Cox25f6b022014-08-27 11:32:17 -0400111func breakpoint()
Keith Randallb60d5e12014-10-21 14:46:07 -0700112
Russ Coxdf027ac2014-12-30 13:59:55 -0500113// reflectcall calls fn with a copy of the n argument bytes pointed at by arg.
114// After fn returns, reflectcall copies n-retoffset result bytes
115// back into arg+retoffset before returning. If copying result bytes back,
116// the caller should pass the argument frame type as argtype, so that
117// call can execute appropriate write barriers during the copy.
118// Package reflect passes a frame type. In package runtime, there is only
119// one call that copies results back, in cgocallbackg1, and it does NOT pass a
120// frame type, meaning there are no write barriers invoked. See that call
121// site for justification.
122func reflectcall(argtype *_type, fn, arg unsafe.Pointer, argsize uint32, retoffset uint32)
123
Russ Coxdb58ab92014-09-04 21:12:31 -0400124func procyield(cycles uint32)
Russ Cox25f6b022014-08-27 11:32:17 -0400125func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
Keith Randallf4407372014-09-03 08:49:43 -0700126func goexit()
Russ Coxd21638b2014-08-27 21:59:49 -0400127
128//go:noescape
129func cas(ptr *uint32, old, new uint32) bool
130
Russ Cox7b4df8f2014-12-22 22:50:42 -0500131// NO go:noescape annotation; see atomic_pointer.go.
Russ Cox92048212014-11-11 17:09:09 -0500132func casp1(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
133
134func nop() // call to prevent inlining of function body
Russ Coxd21638b2014-08-27 21:59:49 -0400135
136//go:noescape
137func casuintptr(ptr *uintptr, old, new uintptr) bool
138
139//go:noescape
Russ Cox3a7f6642014-08-29 16:20:48 -0400140func atomicstoreuintptr(ptr *uintptr, new uintptr)
141
142//go:noescape
Russ Cox3a7f6642014-08-29 16:20:48 -0400143func atomicloaduintptr(ptr *uintptr) uintptr
144
145//go:noescape
Keith Randall47d6af22014-08-30 11:03:28 -0700146func atomicloaduint(ptr *uint) uint
147
148//go:noescape
Russ Coxd21638b2014-08-27 21:59:49 -0400149func setcallerpc(argp unsafe.Pointer, pc uintptr)
150
Russ Cox39bcbb32014-11-05 23:01:48 -0500151// getcallerpc returns the program counter (PC) of its caller's caller.
152// getcallersp returns the stack pointer (SP) of its caller's caller.
153// For both, the argp must be a pointer to the caller's first function argument.
154// The implementation may or may not use argp, depending on
155// the architecture.
156//
157// For example:
158//
159// func f(arg1, arg2, arg3 int) {
160// pc := getcallerpc(unsafe.Pointer(&arg1))
Alex Brainman55f19ed2014-11-18 09:55:15 +1100161// sp := getcallersp(unsafe.Pointer(&arg1))
Russ Cox39bcbb32014-11-05 23:01:48 -0500162// }
163//
164// These two lines find the PC and SP immediately following
165// the call to f (where f will return).
166//
167// The call to getcallerpc and getcallersp must be done in the
168// frame being asked about. It would not be correct for f to pass &arg1
169// to another function g and let g call getcallerpc/getcallersp.
170// The call inside g might return information about g's caller or
171// information about f's caller or complete garbage.
172//
173// The result of getcallersp is correct at the time of the return,
174// but it may be invalidated by any subsequent call to a function
175// that might relocate the stack in order to grow or shrink it.
176// A general rule is that the result of getcallersp should be used
177// immediately and can only be passed to nosplit functions.
178
Russ Coxd21638b2014-08-27 21:59:49 -0400179//go:noescape
180func getcallerpc(argp unsafe.Pointer) uintptr
181
182//go:noescape
183func getcallersp(argp unsafe.Pointer) uintptr
Russ Cox50199d72014-08-30 14:53:47 -0400184
185//go:noescape
186func asmcgocall(fn, arg unsafe.Pointer)
187
188//go:noescape
Dmitriy Vyukovc08d8832014-09-04 14:40:40 -0400189func asmcgocall_errno(fn, arg unsafe.Pointer) int32
190
Russ Cox92048212014-11-11 17:09:09 -0500191// argp used in Defer structs when there is no argp.
Russ Coxfa2af442014-09-02 15:12:53 -0400192const _NoArgs = ^uintptr(0)
193
Russ Cox97f83862014-09-03 13:02:48 -0400194func morestack()
Russ Cox97f83862014-09-03 13:02:48 -0400195func rt0_go()
Keith Randallf4407372014-09-03 08:49:43 -0700196
197// return0 is a stub used to return 0 from deferproc.
198// It is called at the very end of deferproc to signal
199// the calling Go function that it should not jump
200// to deferreturn.
201// in asm_*.s
202func return0()
Keith Randall1a5e3942014-09-09 14:32:53 -0700203
Russ Cox7a524a12014-12-22 13:27:53 -0500204//go:linkname time_now time.now
205func time_now() (sec int64, nsec int32)
Russ Coxcb6f5ac2014-10-15 13:12:16 -0400206
207// in asm_*.s
208// not called directly; definitions here supply type information for traceback.
209func call16(fn, arg unsafe.Pointer, n, retoffset uint32)
210func call32(fn, arg unsafe.Pointer, n, retoffset uint32)
211func call64(fn, arg unsafe.Pointer, n, retoffset uint32)
212func call128(fn, arg unsafe.Pointer, n, retoffset uint32)
213func call256(fn, arg unsafe.Pointer, n, retoffset uint32)
214func call512(fn, arg unsafe.Pointer, n, retoffset uint32)
215func call1024(fn, arg unsafe.Pointer, n, retoffset uint32)
216func call2048(fn, arg unsafe.Pointer, n, retoffset uint32)
217func call4096(fn, arg unsafe.Pointer, n, retoffset uint32)
218func call8192(fn, arg unsafe.Pointer, n, retoffset uint32)
219func call16384(fn, arg unsafe.Pointer, n, retoffset uint32)
220func call32768(fn, arg unsafe.Pointer, n, retoffset uint32)
221func call65536(fn, arg unsafe.Pointer, n, retoffset uint32)
222func call131072(fn, arg unsafe.Pointer, n, retoffset uint32)
223func call262144(fn, arg unsafe.Pointer, n, retoffset uint32)
224func call524288(fn, arg unsafe.Pointer, n, retoffset uint32)
225func call1048576(fn, arg unsafe.Pointer, n, retoffset uint32)
226func call2097152(fn, arg unsafe.Pointer, n, retoffset uint32)
227func call4194304(fn, arg unsafe.Pointer, n, retoffset uint32)
228func call8388608(fn, arg unsafe.Pointer, n, retoffset uint32)
229func call16777216(fn, arg unsafe.Pointer, n, retoffset uint32)
230func call33554432(fn, arg unsafe.Pointer, n, retoffset uint32)
231func call67108864(fn, arg unsafe.Pointer, n, retoffset uint32)
232func call134217728(fn, arg unsafe.Pointer, n, retoffset uint32)
233func call268435456(fn, arg unsafe.Pointer, n, retoffset uint32)
234func call536870912(fn, arg unsafe.Pointer, n, retoffset uint32)
235func call1073741824(fn, arg unsafe.Pointer, n, retoffset uint32)
Russ Cox92048212014-11-11 17:09:09 -0500236
Russ Cox656be312014-11-12 14:54:31 -0500237func systemstack_switch()
Russ Cox8c3f6402014-11-21 15:57:10 -0500238
239func prefetcht0(addr uintptr)
240func prefetcht1(addr uintptr)
241func prefetcht2(addr uintptr)
242func prefetchnta(addr uintptr)
Russ Cox484f8012015-02-19 13:38:46 -0500243
244func unixnanotime() int64 {
245 sec, nsec := time_now()
246 return sec*1e9 + int64(nsec)
247}
248
249// round n up to a multiple of a. a must be a power of 2.
250func round(n, a uintptr) uintptr {
251 return (n + a - 1) &^ (a - 1)
252}