blob: 793cc3acb77ef0df281c47b0d48175c17f704085 [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.
10// C implementations of these functions are in stubs.goc.
11// Assembly implementations are in various files, see comments with
12// each function.
13
Keith Randall0c6b55e2014-07-16 14:16:19 -070014const (
15 ptrSize = unsafe.Sizeof((*byte)(nil))
16)
17
Keith Randall61dca942014-06-16 23:03:03 -070018//go:noescape
Keith Randall0c6b55e2014-07-16 14:16:19 -070019func racereadpc(addr unsafe.Pointer, callpc, pc uintptr)
20
21//go:noescape
22func racewritepc(addr unsafe.Pointer, callpc, pc uintptr)
23
24//go:noescape
Keith Randall61dca942014-06-16 23:03:03 -070025func racereadrangepc(addr unsafe.Pointer, len int, callpc, pc uintptr)
Keith Randall0c6b55e2014-07-16 14:16:19 -070026
Keith Randallcc9ec522014-07-31 12:43:40 -070027//go:noescape
28func racewriterangepc(addr unsafe.Pointer, len int, callpc, pc uintptr)
29
Keith Randall9a1e1422014-08-24 12:31:03 +040030//go:noescape
31func raceacquire(addr unsafe.Pointer)
32
33//go:noescape
34func racerelease(addr unsafe.Pointer)
35
36//go:noescape
37func raceacquireg(gp *g, addr unsafe.Pointer)
38
39//go:noescape
40func racereleaseg(gp *g, addr unsafe.Pointer)
41
Keith Randall0c6b55e2014-07-16 14:16:19 -070042// Should be a built-in for unsafe.Pointer?
43func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
44 return unsafe.Pointer(uintptr(p) + x)
45}
46
Keith Randall4aa50432014-07-30 09:01:52 -070047// n must be a power of 2
48func roundup(p unsafe.Pointer, n uintptr) unsafe.Pointer {
Russ Cox6c67dd92014-08-24 20:28:29 -040049 delta := -uintptr(p) & (n - 1)
50 return unsafe.Pointer(uintptr(p) + delta)
Keith Randall4aa50432014-07-30 09:01:52 -070051}
52
Keith Randall0c6b55e2014-07-16 14:16:19 -070053// in stubs.goc
Dmitriy Vyukov684de042014-08-21 20:41:09 +040054func getg() *g
Keith Randall4aa50432014-07-30 09:01:52 -070055func acquirem() *m
56func releasem(mp *m)
Dmitriy Vyukov30940cf2014-08-18 16:33:39 +040057func gomcache() *mcache
Keith Randall4aa50432014-07-30 09:01:52 -070058
Keith Randalle359bea2014-08-06 14:33:57 -070059// An mFunction represents a C function that runs on the M stack. It
60// can be called from Go using mcall or onM. Through the magic of
61// linking, an mFunction variable and the corresponding C code entry
62// point live at the same address.
63type mFunction byte
Keith Randall4aa50432014-07-30 09:01:52 -070064
Keith Randalle359bea2014-08-06 14:33:57 -070065// in asm_*.s
66func mcall(fn *mFunction)
67func onM(fn *mFunction)
68
69// C functions that run on the M stack. Call these like
70// mcall(&mcacheRefill_m)
Keith Randall4aa50432014-07-30 09:01:52 -070071// Arguments should be passed in m->scalararg[x] and
72// m->ptrarg[x]. Return values can be passed in those
73// same slots.
Keith Randalle359bea2014-08-06 14:33:57 -070074var (
75 mcacheRefill_m,
76 largeAlloc_m,
77 mprofMalloc_m,
78 gc_m,
79 setFinalizer_m,
Dmitriy Vyukovaac7f1a2014-08-07 13:34:30 +040080 markallocated_m,
81 unrollgcprog_m,
Dmitriy Vyukov5d407422014-08-19 11:49:59 +040082 unrollgcproginplace_m,
Dmitriy Vyukov684de042014-08-21 20:41:09 +040083 gosched_m,
Sanjay Menakuruef504622014-08-24 20:27:00 -070084 setgcpercent_m,
85 setmaxthreads_m,
Dmitriy Vyukov684de042014-08-21 20:41:09 +040086 ready_m,
87 park_m,
Dmitriy Vyukovafb22602014-08-22 22:13:01 +040088 notewakeup_m,
89 notetsleepg_m mFunction
Keith Randalle359bea2014-08-06 14:33:57 -070090)
Keith Randall0c6b55e2014-07-16 14:16:19 -070091
Russ Coxd21638b2014-08-27 21:59:49 -040092func blockevent(int64, int32)
93
Keith Randall0c6b55e2014-07-16 14:16:19 -070094// memclr clears n bytes starting at ptr.
95// in memclr_*.s
Keith Randall483cb612014-08-07 13:58:42 -070096//go:noescape
Keith Randall0c6b55e2014-07-16 14:16:19 -070097func memclr(ptr unsafe.Pointer, n uintptr)
98
Keith Randall4aa50432014-07-30 09:01:52 -070099func racemalloc(p unsafe.Pointer, size uintptr)
100func tracealloc(p unsafe.Pointer, size uintptr, typ *_type)
101
Keith Randall0c6b55e2014-07-16 14:16:19 -0700102// memmove copies n bytes from "from" to "to".
103// in memmove_*.s
Keith Randall483cb612014-08-07 13:58:42 -0700104//go:noescape
Keith Randall0c6b55e2014-07-16 14:16:19 -0700105func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr)
106
107// in asm_*.s
108func fastrand2() uint32
109
Keith Randall4aa50432014-07-30 09:01:52 -0700110const (
111 gcpercentUnknown = -2
112 concurrentSweep = true
113)
114
Keith Randall4aa50432014-07-30 09:01:52 -0700115func gosched()
116func starttheworld()
117func stoptheworld()
118func clearpools()
119
Keith Randall0c6b55e2014-07-16 14:16:19 -0700120// exported value for testing
121var hashLoad = loadFactor
122
123// in asm_*.s
124//go:noescape
Keith Randall7aa4e5a2014-08-07 14:52:55 -0700125func memeq(a, b unsafe.Pointer, size uintptr) bool
Keith Randall0c6b55e2014-07-16 14:16:19 -0700126
Keith Randall483cb612014-08-07 13:58:42 -0700127// Code pointers for the nohash/noequal algorithms. Used for producing better error messages.
Keith Randall0c6b55e2014-07-16 14:16:19 -0700128var nohashcode uintptr
Keith Randall483cb612014-08-07 13:58:42 -0700129var noequalcode uintptr
Dave Cheneyec5d7ba2014-07-18 16:30:38 +1000130
131// Go version of runtime.throw.
132// in panic.c
Dave Cheney355c38d2014-07-23 07:08:52 +1000133func gothrow(s string)
Keith Randall4aa50432014-07-30 09:01:52 -0700134
Keith Randalla2a97682014-07-31 15:07:05 -0700135// Return the Go equivalent of the C Alg structure.
136// TODO: at some point Go will hold the truth for the layout
137// of runtime structures and C will be derived from it (if
138// needed at all). At that point this function can go away.
139type goalgtype struct {
140 // function for hashing objects of this type
141 // (ptr to object, size, seed) -> hash
142 hash func(unsafe.Pointer, uintptr, uintptr) uintptr
Keith Randall7aa4e5a2014-08-07 14:52:55 -0700143 // function for comparing objects of this type
144 // (ptr to object A, ptr to object B, size) -> ==?
145 equal func(unsafe.Pointer, unsafe.Pointer, uintptr) bool
Keith Randalla2a97682014-07-31 15:07:05 -0700146}
147
148func goalg(a *alg) *goalgtype {
149 return (*goalgtype)(unsafe.Pointer(a))
150}
151
152// noescape hides a pointer from escape analysis. noescape is
153// the identity function but escape analysis doesn't think the
154// output depends on the input. noescape is inlined and currently
155// compiles down to a single xor instruction.
156// USE CAREFULLY!
157func noescape(p unsafe.Pointer) unsafe.Pointer {
158 x := uintptr(p)
159 return unsafe.Pointer(x ^ 0)
160}
Keith Randall483cb612014-08-07 13:58:42 -0700161
Dmitriy Vyukovafb22602014-08-22 22:13:01 +0400162func exitsyscall()
Rémy Oudompheng39ffa8b2014-08-26 08:34:46 +0200163
Brad Fitzpatrick9a5654a2014-08-27 09:31:32 -0700164func goroutineheader(gp *g)
Rémy Oudompheng39ffa8b2014-08-26 08:34:46 +0200165func traceback(pc, sp, lr uintptr, gp *g)
166func tracebackothers(gp *g)
Russ Cox25f6b022014-08-27 11:32:17 -0400167
168func cgocallback(fn, frame unsafe.Pointer, framesize uintptr)
169func gogo(buf *gobuf)
170func gosave(buf *gobuf)
171func open(name *byte, mode, perm int32) int32
172func read(fd int32, p unsafe.Pointer, n int32) int32
173func write(fd uintptr, p unsafe.Pointer, n int32) int32
174func close(fd int32) int32
175func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
176func jmpdefer(fv *funcval, argp unsafe.Pointer)
177func exit1(code int32)
178func asminit()
Russ Cox25f6b022014-08-27 11:32:17 -0400179func setg(gg *g)
180func exit(code int32)
181func breakpoint()
182func asmcgocall(fn, arg unsafe.Pointer)
183func nanotime() int64
184func usleep(usec uint32)
185func cputicks() int64
186func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer
187func munmap(addr unsafe.Pointer, n uintptr)
188func madvise(addr unsafe.Pointer, n uintptr, flags int32)
Russ Cox25f6b022014-08-27 11:32:17 -0400189func newstackcall(fv *funcval, addr unsafe.Pointer, size uint32)
190func procyield(cycles uint32)
191func osyield()
192func cgocallback_gofunc(fv *funcval, frame unsafe.Pointer, framesize uintptr)
193func cmpstring(s1, s2 string) int
Russ Coxd21638b2014-08-27 21:59:49 -0400194func persistentalloc(size, align uintptr, stat *uint64) unsafe.Pointer
195func readgogc() int32
196func notetsleepg(n *note, ns int64)
197func notetsleep(n *note, ns int64)
198func notewakeup(n *note)
199func notesleep(n *note)
200func noteclear(n *note)
Russ Cox8ecb9a72014-08-27 23:32:49 -0400201func lock(lk *mutex)
202func unlock(lk *mutex)
Russ Coxd21638b2014-08-27 21:59:49 -0400203
204//go:noescape
205func cas(ptr *uint32, old, new uint32) bool
206
207//go:noescape
208func cas64(ptr *uint64, old, new uint64) bool
209
210//go:noescape
211func casp(ptr *unsafe.Pointer, old, new unsafe.Pointer) bool
212
213//go:noescape
214func casuintptr(ptr *uintptr, old, new uintptr) bool
215
216//go:noescape
217func xadd(ptr *uint32, delta int32) uint32
218
219//go:noescape
220func xadd64(ptr *uint64, delta int64) uint64
221
222//go:noescape
223func xchg(ptr *uint32, new uint32) uint32
224
225//go:noescape
226func xchg64(ptr *uint64, new uint64) uint64
227
228//go:noescape
229func xchgp(ptr unsafe.Pointer, new unsafe.Pointer) unsafe.Pointer
230
231//go:noescape
232func atomicstore(ptr *uint32, val uint32)
233
234//go:noescape
235func atomicstore64(ptr *uint64, val uint64)
236
237//go:noescape
238func atomicstorep(ptr unsafe.Pointer, val unsafe.Pointer)
239
240//go:noescape
241func atomicload(ptr *uint32) uint32
242
243//go:noescape
244func atomicload64(ptr *uint64) uint64
245
246//go:noescape
247func atomicloadp(ptr unsafe.Pointer) unsafe.Pointer
248
249//go:noescape
250func atomicor8(ptr *uint8, val uint8)
251
252//go:noescape
253func setcallerpc(argp unsafe.Pointer, pc uintptr)
254
255//go:noescape
256func getcallerpc(argp unsafe.Pointer) uintptr
257
258//go:noescape
259func getcallersp(argp unsafe.Pointer) uintptr