Add support for ARMv8-R architecture
[official-gcc.git] / libgo / runtime / runtime.h
blob9f84f523f5ee54a3716ce264604b5d6c39d1a2f5
1 // Copyright 2009 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.
5 #include "config.h"
7 #include "go-assert.h"
8 #include <complex.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <ucontext.h>
22 #ifdef HAVE_SYS_MMAN_H
23 #include <sys/mman.h>
24 #endif
26 #define _STRINGIFY2_(x) #x
27 #define _STRINGIFY_(x) _STRINGIFY2_(x)
28 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
30 /* This file supports C files copied from the 6g runtime library.
31 This is a version of the 6g runtime.h rewritten for gccgo's version
32 of the code. */
34 typedef signed int int8 __attribute__ ((mode (QI)));
35 typedef unsigned int uint8 __attribute__ ((mode (QI)));
36 typedef signed int int16 __attribute__ ((mode (HI)));
37 typedef unsigned int uint16 __attribute__ ((mode (HI)));
38 typedef signed int int32 __attribute__ ((mode (SI)));
39 typedef unsigned int uint32 __attribute__ ((mode (SI)));
40 typedef signed int int64 __attribute__ ((mode (DI)));
41 typedef unsigned int uint64 __attribute__ ((mode (DI)));
42 typedef float float32 __attribute__ ((mode (SF)));
43 typedef double float64 __attribute__ ((mode (DF)));
44 typedef signed int intptr __attribute__ ((mode (pointer)));
45 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
47 typedef intptr intgo; // Go's int
48 typedef uintptr uintgo; // Go's uint
50 typedef uintptr uintreg;
52 /* Defined types. */
54 typedef _Bool bool;
55 typedef uint8 byte;
56 typedef struct g G;
57 typedef struct mutex Lock;
58 typedef struct m M;
59 typedef struct p P;
60 typedef struct note Note;
61 typedef struct String String;
62 typedef struct FuncVal FuncVal;
63 typedef struct SigTab SigTab;
64 typedef struct hchan Hchan;
65 typedef struct timer Timer;
66 typedef struct lfnode LFNode;
67 typedef struct cgoMal CgoMal;
68 typedef struct PollDesc PollDesc;
69 typedef struct sudog SudoG;
70 typedef struct schedt Sched;
72 typedef struct __go_open_array Slice;
73 typedef struct iface Iface;
74 typedef struct eface Eface;
75 typedef struct __go_type_descriptor Type;
76 typedef struct _defer Defer;
77 typedef struct _panic Panic;
79 typedef struct __go_ptr_type PtrType;
80 typedef struct __go_func_type FuncType;
81 typedef struct __go_interface_type InterfaceType;
82 typedef struct __go_map_type MapType;
83 typedef struct __go_channel_type ChanType;
85 typedef struct tracebackg Traceback;
87 typedef struct location Location;
89 struct String
91 const byte* str;
92 intgo len;
95 struct FuncVal
97 void (*fn)(void);
98 // variable-size, fn-specific data here
101 #include "array.h"
103 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
104 // the name conflict.
105 #define timeval go_timeval
106 #define timespec go_timespec
108 #include "runtime.inc"
110 #undef timeval
111 #undef timespec
114 * Per-CPU declaration.
116 extern M* runtime_m(void);
117 extern G* runtime_g(void)
118 __asm__(GOSYM_PREFIX "runtime.getg");
120 extern M* runtime_m0(void)
121 __asm__(GOSYM_PREFIX "runtime.runtime_m0");
122 extern G* runtime_g0(void)
123 __asm__(GOSYM_PREFIX "runtime.runtime_g0");
125 enum
127 true = 1,
128 false = 0,
130 enum
132 PtrSize = sizeof(void*),
134 enum
136 // Per-M stack segment cache size.
137 StackCacheSize = 32,
138 // Global <-> per-M stack segment cache transfer batch size.
139 StackCacheBatch = 16,
142 struct SigTab
144 int32 sig;
145 int32 flags;
146 void* fwdsig;
149 #ifdef GOOS_nacl
150 enum {
151 NaCl = 1,
153 #else
154 enum {
155 NaCl = 0,
157 #endif
159 #ifdef GOOS_windows
160 enum {
161 Windows = 1
163 #else
164 enum {
165 Windows = 0
167 #endif
168 #ifdef GOOS_solaris
169 enum {
170 Solaris = 1
172 #else
173 enum {
174 Solaris = 0
176 #endif
178 extern bool runtime_copystack;
181 * defined macros
182 * you need super-gopher-guru privilege
183 * to add this list.
185 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
186 #define nil ((void*)0)
187 #define USED(v) ((void) v)
188 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
190 enum {
191 // hashinit wants this many random bytes
192 HashRandomBytes = 32
194 void runtime_hashinit(void);
196 void runtime_traceback(int32)
197 __asm__ (GOSYM_PREFIX "runtime.traceback");
198 void runtime_tracebackothers(G*)
199 __asm__ (GOSYM_PREFIX "runtime.tracebackothers");
200 enum
202 // The maximum number of frames we print for a traceback
203 TracebackMaxFrames = 100,
207 * external data
209 extern uintptr* runtime_getZerobase(void)
210 __asm__(GOSYM_PREFIX "runtime.getZerobase");
211 extern G* runtime_getallg(intgo)
212 __asm__(GOSYM_PREFIX "runtime.getallg");
213 extern uintptr runtime_getallglen(void)
214 __asm__(GOSYM_PREFIX "runtime.getallglen");
215 extern M* runtime_getallm(void)
216 __asm__(GOSYM_PREFIX "runtime.getallm");
217 extern Sched* runtime_sched;
218 extern uint32 runtime_panicking(void)
219 __asm__ (GOSYM_PREFIX "runtime.getPanicking");
220 extern int32 runtime_ncpu;
221 extern struct debugVars runtime_debug;
223 extern bool runtime_isstarted;
224 extern bool runtime_isarchive;
227 * common functions and data
229 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
230 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
231 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
232 intgo runtime_findnull(const byte*)
233 __asm__ (GOSYM_PREFIX "runtime.findnull");
235 void runtime_gogo(G*)
236 __asm__ (GOSYM_PREFIX "runtime.gogo");
237 struct __go_func_type;
238 void runtime_args(int32, byte**)
239 __asm__ (GOSYM_PREFIX "runtime.args");
240 void runtime_osinit();
241 void runtime_alginit(void)
242 __asm__ (GOSYM_PREFIX "runtime.alginit");
243 void runtime_goargs(void)
244 __asm__ (GOSYM_PREFIX "runtime.goargs");
245 void runtime_throw(const char*) __attribute__ ((noreturn));
246 void runtime_panicstring(const char*) __attribute__ ((noreturn));
247 bool runtime_canpanic(G*);
248 void runtime_printf(const char*, ...);
249 int32 runtime_snprintf(byte*, int32, const char*, ...);
250 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
251 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
252 String runtime_gostringnocopy(const byte*)
253 __asm__ (GOSYM_PREFIX "runtime.gostringnocopy");
254 void runtime_schedinit(void)
255 __asm__ (GOSYM_PREFIX "runtime.schedinit");
256 void runtime_initsig(bool)
257 __asm__ (GOSYM_PREFIX "runtime.initsig");
258 void runtime_goroutineheader(G*)
259 __asm__ (GOSYM_PREFIX "runtime.goroutineheader");
260 void runtime_printtrace(Slice, G*)
261 __asm__ (GOSYM_PREFIX "runtime.printtrace");
262 #define runtime_open(p, f, m) open((p), (f), (m))
263 #define runtime_read(d, v, n) read((d), (v), (n))
264 #define runtime_write(d, v, n) write((d), (v), (n))
265 #define runtime_close(d) close(d)
266 void runtime_ready(G*, intgo, bool)
267 __asm__ (GOSYM_PREFIX "runtime.ready");
268 String runtime_getenv(const char*);
269 int32 runtime_atoi(const byte*, intgo);
270 void* runtime_mstart(void*);
271 G* runtime_malg(bool, bool, byte**, uintptr*)
272 __asm__(GOSYM_PREFIX "runtime.malg");
273 void runtime_minit(void)
274 __asm__ (GOSYM_PREFIX "runtime.minit");
275 void runtime_signalstack(byte*, uintptr)
276 __asm__ (GOSYM_PREFIX "runtime.signalstack");
277 void runtime_mallocinit(void)
278 __asm__ (GOSYM_PREFIX "runtime.mallocinit");
279 void* runtime_mallocgc(uintptr, const Type*, bool)
280 __asm__ (GOSYM_PREFIX "runtime.mallocgc");
281 void* runtime_sysAlloc(uintptr, uint64*)
282 __asm__ (GOSYM_PREFIX "runtime.sysAlloc");
283 void runtime_mprofinit(void);
284 #define runtime_getcallersp(p) __builtin_frame_address(0)
285 void runtime_mcall(FuncVal*)
286 __asm__ (GOSYM_PREFIX "runtime.mcall");
287 uint32 runtime_fastrand(void) __asm__ (GOSYM_PREFIX "runtime.fastrand");
288 int32 runtime_timediv(int64, int32, int32*)
289 __asm__ (GOSYM_PREFIX "runtime.timediv");
290 int32 runtime_round2(int32 x); // round x up to a power of 2.
292 // atomic operations
293 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
294 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
295 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
296 // Don't confuse with XADD x86 instruction,
297 // this one is actually 'addx', that is, add-and-fetch.
298 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
299 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
300 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
301 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
302 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
303 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
304 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
305 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
306 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
307 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
308 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
310 void runtime_setg(G*)
311 __asm__ (GOSYM_PREFIX "runtime.setg");
312 void runtime_newextram(void)
313 __asm__ (GOSYM_PREFIX "runtime.newextram");
314 #define runtime_exit(s) exit(s)
315 void runtime_gosched(void)
316 __asm__ (GOSYM_PREFIX "runtime.Gosched");
317 void runtime_schedtrace(bool)
318 __asm__ (GOSYM_PREFIX "runtime.schedtrace");
319 void runtime_goparkunlock(Lock*, String, byte, intgo)
320 __asm__ (GOSYM_PREFIX "runtime.goparkunlock");
321 void runtime_tsleep(int64, const char*);
322 void runtime_entersyscall(int32)
323 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
324 void runtime_entersyscallblock(int32)
325 __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
326 void runtime_exitsyscall(int32)
327 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
328 G* __go_go(void (*pfn)(void*), void*);
329 int32 runtime_callers(int32, Location*, int32, bool keep_callers);
330 int64 runtime_nanotime(void) // monotonic time
331 __asm__(GOSYM_PREFIX "runtime.nanotime");
332 int64 runtime_unixnanotime(void) // real time, can skip
333 __asm__ (GOSYM_PREFIX "runtime.unixnanotime");
334 void runtime_dopanic(int32) __attribute__ ((noreturn));
335 void runtime_startpanic(void)
336 __asm__ (GOSYM_PREFIX "runtime.startpanic");
337 void runtime_unwindstack(G*, byte*);
338 void runtime_usleep(uint32)
339 __asm__ (GOSYM_PREFIX "runtime.usleep");
340 int64 runtime_cputicks(void)
341 __asm__ (GOSYM_PREFIX "runtime.cputicks");
342 int64 runtime_tickspersecond(void)
343 __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
344 void runtime_blockevent(int64, int32);
345 extern int64 runtime_blockprofilerate;
346 G* runtime_netpoll(bool)
347 __asm__ (GOSYM_PREFIX "runtime.netpoll");
348 void runtime_crash(void)
349 __asm__ (GOSYM_PREFIX "runtime.crash");
350 void runtime_parsedebugvars(void)
351 __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
352 void _rt0_go(void);
353 G* runtime_timejump(void);
355 void runtime_callStopTheWorldWithSema(void)
356 __asm__(GOSYM_PREFIX "runtime.callStopTheWorldWithSema");
357 void runtime_callStartTheWorldWithSema(void)
358 __asm__(GOSYM_PREFIX "runtime.callStartTheWorldWithSema");
359 void runtime_acquireWorldsema(void)
360 __asm__(GOSYM_PREFIX "runtime.acquireWorldsema");
361 void runtime_releaseWorldsema(void)
362 __asm__(GOSYM_PREFIX "runtime.releaseWorldsema");
365 * mutual exclusion locks. in the uncontended case,
366 * as fast as spin locks (just a few user-level instructions),
367 * but on the contention path they sleep in the kernel.
368 * a zeroed Lock is unlocked (no need to initialize each lock).
370 void runtime_lock(Lock*)
371 __asm__(GOSYM_PREFIX "runtime.lock");
372 void runtime_unlock(Lock*)
373 __asm__(GOSYM_PREFIX "runtime.unlock");
376 * sleep and wakeup on one-time events.
377 * before any calls to notesleep or notewakeup,
378 * must call noteclear to initialize the Note.
379 * then, exactly one thread can call notesleep
380 * and exactly one thread can call notewakeup (once).
381 * once notewakeup has been called, the notesleep
382 * will return. future notesleep will return immediately.
383 * subsequent noteclear must be called only after
384 * previous notesleep has returned, e.g. it's disallowed
385 * to call noteclear straight after notewakeup.
387 * notetsleep is like notesleep but wakes up after
388 * a given number of nanoseconds even if the event
389 * has not yet happened. if a goroutine uses notetsleep to
390 * wake up early, it must wait to call noteclear until it
391 * can be sure that no other goroutine is calling
392 * notewakeup.
394 * notesleep/notetsleep are generally called on g0,
395 * notetsleepg is similar to notetsleep but is called on user g.
397 void runtime_noteclear(Note*)
398 __asm__ (GOSYM_PREFIX "runtime.noteclear");
399 void runtime_notesleep(Note*)
400 __asm__ (GOSYM_PREFIX "runtime.notesleep");
401 void runtime_notewakeup(Note*)
402 __asm__ (GOSYM_PREFIX "runtime.notewakeup");
403 bool runtime_notetsleep(Note*, int64) // false - timeout
404 __asm__ (GOSYM_PREFIX "runtime.notetsleep");
405 bool runtime_notetsleepg(Note*, int64) // false - timeout
406 __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
409 * Lock-free stack.
410 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
411 * The stack does not keep pointers to nodes,
412 * so they can be garbage collected if there are no other pointers to nodes.
414 void runtime_lfstackpush(uint64 *head, LFNode *node)
415 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
416 void* runtime_lfstackpop(uint64 *head)
417 __asm__ (GOSYM_PREFIX "runtime.lfstackpop");
420 * low level C-called
422 #define runtime_mmap mmap
423 #define runtime_munmap munmap
424 #define runtime_madvise madvise
425 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
426 #define runtime_getcallerpc(p) __builtin_return_address(0)
428 #ifdef __rtems__
429 void __wrap_rtems_task_variable_add(void **);
430 #endif
433 * runtime go-called
435 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
436 void **, void **)
437 __asm__ (GOSYM_PREFIX "reflect.call");
438 void runtime_panic(Eface)
439 __asm__ (GOSYM_PREFIX "runtime.gopanic");
440 void runtime_panic(Eface)
441 __attribute__ ((noreturn));
444 * runtime c-called (but written in Go)
446 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
447 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
448 void runtime_newErrorCString(const char*, Eface*)
449 __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
452 * wrapped for go users
454 void runtime_procyield(uint32)
455 __asm__(GOSYM_PREFIX "runtime.procyield");
456 void runtime_osyield(void)
457 __asm__(GOSYM_PREFIX "runtime.osyield");
459 void runtime_printcreatedby(G*)
460 __asm__(GOSYM_PREFIX "runtime.printcreatedby");
462 uintptr runtime_memlimit(void);
464 #define ISNAN(f) __builtin_isnan(f)
466 enum
468 UseSpanType = 1,
471 #define runtime_setitimer setitimer
473 void runtime_check(void)
474 __asm__ (GOSYM_PREFIX "runtime.check");
476 // Size of stack space allocated using Go's allocator.
477 // This will be 0 when using split stacks, as in that case
478 // the stacks are allocated by the splitstack library.
479 extern uintptr runtime_stacks_sys;
481 struct backtrace_state;
482 extern struct backtrace_state *__go_get_backtrace_state(void);
483 extern _Bool __go_file_line(uintptr, int, String*, String*, intgo *);
484 extern void runtime_main(void*)
485 __asm__(GOSYM_PREFIX "runtime.main");
487 int32 getproccount(void);
489 #define PREFETCH(p) __builtin_prefetch(p)
491 bool runtime_gcwaiting(void);
492 void runtime_badsignal(int);
493 Defer* runtime_newdefer(void);
494 void runtime_freedefer(Defer*);
496 struct time_now_ret
498 int64_t sec;
499 int32_t nsec;
502 struct time_now_ret now() __asm__ (GOSYM_PREFIX "time.now")
503 __attribute__ ((no_split_stack));
505 extern void _cgo_wait_runtime_init_done (void);
506 extern void _cgo_notify_runtime_init_done (void)
507 __asm__ (GOSYM_PREFIX "runtime._cgo_notify_runtime_init_done");
508 extern _Bool runtime_iscgo;
509 extern uintptr __go_end __attribute__ ((weak));
510 extern void *getitab(const struct __go_type_descriptor *,
511 const struct __go_type_descriptor *,
512 _Bool)
513 __asm__ (GOSYM_PREFIX "runtime.getitab");
515 extern void runtime_cpuinit(void);
516 extern void setIsCgo(void)
517 __asm__ (GOSYM_PREFIX "runtime.setIsCgo");
518 extern void setCpuidECX(uint32)
519 __asm__ (GOSYM_PREFIX "runtime.setCpuidECX");
520 extern void setSupportAES(bool)
521 __asm__ (GOSYM_PREFIX "runtime.setSupportAES");
522 extern void typedmemmove(const Type *, void *, const void *)
523 __asm__ (GOSYM_PREFIX "runtime.typedmemmove");
524 extern void setncpu(int32)
525 __asm__(GOSYM_PREFIX "runtime.setncpu");
526 extern P** runtime_getAllP()
527 __asm__ (GOSYM_PREFIX "runtime.getAllP");
528 extern Sched* runtime_getsched()
529 __asm__ (GOSYM_PREFIX "runtime.getsched");
530 extern void setpagesize(uintptr_t)
531 __asm__(GOSYM_PREFIX "runtime.setpagesize");