2016-10-07 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / runtime.h
blobd1aad1e2d73010e3e1d5937c3631c8b60b727687
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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <pthread.h>
18 #include <semaphore.h>
19 #include <ucontext.h>
21 #ifdef HAVE_SYS_MMAN_H
22 #include <sys/mman.h>
23 #endif
25 #include "interface.h"
26 #include "go-alloc.h"
28 #define _STRINGIFY2_(x) #x
29 #define _STRINGIFY_(x) _STRINGIFY2_(x)
30 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
32 /* This file supports C files copied from the 6g runtime library.
33 This is a version of the 6g runtime.h rewritten for gccgo's version
34 of the code. */
36 typedef signed int int8 __attribute__ ((mode (QI)));
37 typedef unsigned int uint8 __attribute__ ((mode (QI)));
38 typedef signed int int16 __attribute__ ((mode (HI)));
39 typedef unsigned int uint16 __attribute__ ((mode (HI)));
40 typedef signed int int32 __attribute__ ((mode (SI)));
41 typedef unsigned int uint32 __attribute__ ((mode (SI)));
42 typedef signed int int64 __attribute__ ((mode (DI)));
43 typedef unsigned int uint64 __attribute__ ((mode (DI)));
44 typedef float float32 __attribute__ ((mode (SF)));
45 typedef double float64 __attribute__ ((mode (DF)));
46 typedef signed int intptr __attribute__ ((mode (pointer)));
47 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
49 typedef intptr intgo; // Go's int
50 typedef uintptr uintgo; // Go's uint
52 typedef uintptr uintreg;
54 /* Defined types. */
56 typedef uint8 bool;
57 typedef uint8 byte;
58 typedef struct Func Func;
59 typedef struct g G;
60 typedef struct mutex Lock;
61 typedef struct m M;
62 typedef struct p P;
63 typedef struct note Note;
64 typedef struct String String;
65 typedef struct FuncVal FuncVal;
66 typedef struct SigTab SigTab;
67 typedef struct mcache MCache;
68 typedef struct FixAlloc FixAlloc;
69 typedef struct Hchan Hchan;
70 typedef struct Timers Timers;
71 typedef struct Timer Timer;
72 typedef struct gcstats GCStats;
73 typedef struct LFNode LFNode;
74 typedef struct ParFor ParFor;
75 typedef struct ParForThread ParForThread;
76 typedef struct cgoMal CgoMal;
77 typedef struct PollDesc PollDesc;
79 typedef struct __go_open_array Slice;
80 typedef struct __go_interface Iface;
81 typedef struct __go_empty_interface Eface;
82 typedef struct __go_type_descriptor Type;
83 typedef struct _defer Defer;
84 typedef struct _panic Panic;
86 typedef struct __go_ptr_type PtrType;
87 typedef struct __go_func_type FuncType;
88 typedef struct __go_interface_type InterfaceType;
89 typedef struct __go_map_type MapType;
90 typedef struct __go_channel_type ChanType;
92 typedef struct traceback Traceback;
94 typedef struct location Location;
96 struct String
98 const byte* str;
99 intgo len;
102 struct FuncVal
104 void (*fn)(void);
105 // variable-size, fn-specific data here
108 #include "array.h"
109 #include "interface.h"
111 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
112 // the name conflict.
113 #define timeval go_timeval
114 #define timespec go_timespec
116 #include "runtime.inc"
118 #undef timeval
119 #undef timespec
122 * Per-CPU declaration.
124 extern M* runtime_m(void);
125 extern G* runtime_g(void)
126 __asm__(GOSYM_PREFIX "runtime.getg");
128 extern M runtime_m0;
129 extern G runtime_g0;
131 enum
133 true = 1,
134 false = 0,
136 enum
138 PtrSize = sizeof(void*),
140 enum
142 // Per-M stack segment cache size.
143 StackCacheSize = 32,
144 // Global <-> per-M stack segment cache transfer batch size.
145 StackCacheBatch = 16,
148 struct SigTab
150 int32 sig;
151 int32 flags;
152 void* fwdsig;
155 // Layout of in-memory per-function information prepared by linker
156 // See http://golang.org/s/go12symtab.
157 // Keep in sync with linker and with ../../libmach/sym.c
158 // and with package debug/gosym.
159 struct Func
161 String name;
162 uintptr entry; // entry pc
165 #ifdef GOOS_nacl
166 enum {
167 NaCl = 1,
169 #else
170 enum {
171 NaCl = 0,
173 #endif
175 #ifdef GOOS_windows
176 enum {
177 Windows = 1
179 #else
180 enum {
181 Windows = 0
183 #endif
184 #ifdef GOOS_solaris
185 enum {
186 Solaris = 1
188 #else
189 enum {
190 Solaris = 0
192 #endif
194 struct Timers
196 Lock;
197 G *timerproc;
198 bool sleeping;
199 bool rescheduling;
200 Note waitnote;
201 Timer **t;
202 int32 len;
203 int32 cap;
206 // Package time knows the layout of this structure.
207 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
208 // For GOOS=nacl, package syscall knows the layout of this structure.
209 // If this struct changes, adjust ../syscall/net_nacl.go:/runtimeTimer.
210 struct Timer
212 intgo i; // heap index
214 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
215 // each time calling f(now, arg) in the timer goroutine, so f must be
216 // a well-behaved function and not block.
217 int64 when;
218 int64 period;
219 FuncVal *fv;
220 Eface arg;
221 uintptr seq;
224 // Lock-free stack node.
225 struct LFNode
227 LFNode *next;
228 uintptr pushcnt;
231 // Parallel for descriptor.
232 struct ParFor
234 const FuncVal *body; // executed for each element
235 uint32 done; // number of idle threads
236 uint32 nthr; // total number of threads
237 uint32 nthrmax; // maximum number of threads
238 uint32 thrseq; // thread id sequencer
239 uint32 cnt; // iteration space [0, cnt)
240 bool wait; // if true, wait while all threads finish processing,
241 // otherwise parfor may return while other threads are still working
242 ParForThread *thr; // array of thread descriptors
243 // stats
244 uint64 nsteal;
245 uint64 nstealcnt;
246 uint64 nprocyield;
247 uint64 nosyield;
248 uint64 nsleep;
251 extern bool runtime_precisestack;
252 extern bool runtime_copystack;
255 * defined macros
256 * you need super-gopher-guru privilege
257 * to add this list.
259 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
260 #define nil ((void*)0)
261 #define USED(v) ((void) v)
262 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
264 byte* runtime_startup_random_data;
265 uint32 runtime_startup_random_data_len;
266 void runtime_get_random_data(byte**, int32*);
268 enum {
269 // hashinit wants this many random bytes
270 HashRandomBytes = 32
272 void runtime_hashinit(void);
274 void runtime_traceback(void);
275 void runtime_tracebackothers(G*);
276 enum
278 // The maximum number of frames we print for a traceback
279 TracebackMaxFrames = 100,
283 * external data
285 extern uintptr runtime_zerobase;
286 extern G** runtime_allg;
287 extern uintptr runtime_allglen;
288 extern G* runtime_lastg;
289 extern M* runtime_allm;
290 extern P** runtime_allp;
291 extern int32 runtime_gomaxprocs;
292 extern uint32 runtime_needextram;
293 extern uint32 runtime_panicking;
294 extern int8* runtime_goos;
295 extern int32 runtime_ncpu;
296 extern void (*runtime_sysargs)(int32, uint8**);
297 extern uint32 runtime_Hchansize;
298 extern struct debugVars runtime_debug;
299 extern uintptr runtime_maxstacksize;
301 extern bool runtime_isstarted;
302 extern bool runtime_isarchive;
305 * common functions and data
307 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
308 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
309 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
310 intgo runtime_findnull(const byte*);
311 intgo runtime_findnullw(const uint16*);
312 void runtime_dump(byte*, int32);
314 void runtime_gogo(G*);
315 struct __go_func_type;
316 void runtime_args(int32, byte**)
317 __asm__ (GOSYM_PREFIX "runtime.args");
318 void runtime_osinit();
319 void runtime_goargs(void)
320 __asm__ (GOSYM_PREFIX "runtime.goargs");
321 void runtime_goenvs(void);
322 void runtime_goenvs_unix(void)
323 __asm__ (GOSYM_PREFIX "runtime.goenvs_unix");
324 void runtime_throw(const char*) __attribute__ ((noreturn));
325 void runtime_panicstring(const char*) __attribute__ ((noreturn));
326 bool runtime_canpanic(G*);
327 void runtime_prints(const char*);
328 void runtime_printf(const char*, ...);
329 int32 runtime_snprintf(byte*, int32, const char*, ...);
330 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
331 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
332 void* runtime_mal(uintptr);
333 String runtime_gostring(const byte*);
334 String runtime_gostringnocopy(const byte*);
335 void runtime_schedinit(void);
336 void runtime_initsig(bool);
337 void runtime_sigenable(uint32 sig);
338 void runtime_sigdisable(uint32 sig);
339 void runtime_sigignore(uint32 sig);
340 int32 runtime_gotraceback(bool *crash);
341 void runtime_goroutineheader(G*);
342 void runtime_printtrace(Location*, int32, bool);
343 #define runtime_open(p, f, m) open((p), (f), (m))
344 #define runtime_read(d, v, n) read((d), (v), (n))
345 #define runtime_write(d, v, n) write((d), (v), (n))
346 #define runtime_close(d) close(d)
347 void runtime_ready(G*);
348 String runtime_getenv(const char*);
349 int32 runtime_atoi(const byte*, intgo);
350 void* runtime_mstart(void*);
351 G* runtime_malg(int32, byte**, uintptr*);
352 void runtime_mpreinit(M*);
353 void runtime_minit(void);
354 void runtime_unminit(void);
355 void runtime_needm(void);
356 void runtime_dropm(void);
357 void runtime_signalstack(byte*, int32);
358 MCache* runtime_allocmcache(void);
359 void runtime_freemcache(MCache*);
360 void runtime_mallocinit(void);
361 void runtime_mprofinit(void);
362 #define runtime_malloc(s) __go_alloc(s)
363 #define runtime_free(p) __go_free(p)
364 #define runtime_getcallersp(p) __builtin_frame_address(1)
365 int32 runtime_mcount(void);
366 int32 runtime_gcount(void);
367 void runtime_mcall(void(*)(G*));
368 uint32 runtime_fastrand1(void) __asm__ (GOSYM_PREFIX "runtime.fastrand1");
369 int32 runtime_timediv(int64, int32, int32*)
370 __asm__ (GOSYM_PREFIX "runtime.timediv");
371 int32 runtime_round2(int32 x); // round x up to a power of 2.
373 // atomic operations
374 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
375 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
376 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
377 // Don't confuse with XADD x86 instruction,
378 // this one is actually 'addx', that is, add-and-fetch.
379 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
380 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
381 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
382 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
383 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
384 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
385 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
386 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
387 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
388 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
389 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
391 void runtime_setg(G*);
392 void runtime_newextram(void);
393 #define runtime_exit(s) exit(s)
394 #define runtime_breakpoint() __builtin_trap()
395 void runtime_gosched(void);
396 void runtime_gosched0(G*);
397 void runtime_schedtrace(bool);
398 void runtime_park(bool(*)(G*, void*), void*, const char*);
399 void runtime_parkunlock(Lock*, const char*);
400 void runtime_tsleep(int64, const char*);
401 M* runtime_newm(void);
402 void runtime_goexit(void);
403 void runtime_entersyscall(int32)
404 __asm__ (GOSYM_PREFIX "runtime.entersyscall");
405 void runtime_entersyscallblock(int32)
406 __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
407 void runtime_exitsyscall(int32)
408 __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
409 G* __go_go(void (*pfn)(void*), void*);
410 void siginit(void);
411 bool __go_sigsend(int32 sig);
412 int32 runtime_callers(int32, Location*, int32, bool keep_callers);
413 int64 runtime_nanotime(void) // monotonic time
414 __asm__(GOSYM_PREFIX "runtime.nanotime");
415 int64 runtime_unixnanotime(void); // real time, can skip
416 void runtime_dopanic(int32) __attribute__ ((noreturn));
417 void runtime_startpanic(void);
418 void runtime_freezetheworld(void);
419 void runtime_unwindstack(G*, byte*);
420 void runtime_sigprof();
421 void runtime_resetcpuprofiler(int32);
422 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
423 void runtime_usleep(uint32)
424 __asm__ (GOSYM_PREFIX "runtime.usleep");
425 int64 runtime_cputicks(void)
426 __asm__ (GOSYM_PREFIX "runtime.cputicks");
427 int64 runtime_tickspersecond(void)
428 __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
429 void runtime_blockevent(int64, int32);
430 extern int64 runtime_blockprofilerate;
431 void runtime_addtimer(Timer*);
432 bool runtime_deltimer(Timer*);
433 G* runtime_netpoll(bool);
434 void runtime_netpollinit(void);
435 int32 runtime_netpollopen(uintptr, PollDesc*);
436 int32 runtime_netpollclose(uintptr);
437 void runtime_netpollready(G**, PollDesc*, int32);
438 uintptr runtime_netpollfd(PollDesc*);
439 void runtime_netpollarm(PollDesc*, int32);
440 void** runtime_netpolluser(PollDesc*);
441 bool runtime_netpollclosing(PollDesc*);
442 void runtime_netpolllock(PollDesc*);
443 void runtime_netpollunlock(PollDesc*);
444 void runtime_crash(void);
445 void runtime_parsedebugvars(void)
446 __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
447 void _rt0_go(void);
448 void* runtime_funcdata(Func*, int32);
449 int32 runtime_setmaxthreads(int32);
450 G* runtime_timejump(void);
451 void runtime_iterate_finq(void (*callback)(FuncVal*, void*, const FuncType*, const PtrType*));
453 void runtime_stoptheworld(void);
454 void runtime_starttheworld(void);
455 extern uint32 runtime_worldsema;
458 * mutual exclusion locks. in the uncontended case,
459 * as fast as spin locks (just a few user-level instructions),
460 * but on the contention path they sleep in the kernel.
461 * a zeroed Lock is unlocked (no need to initialize each lock).
463 void runtime_lock(Lock*)
464 __asm__(GOSYM_PREFIX "runtime.lock");
465 void runtime_unlock(Lock*)
466 __asm__(GOSYM_PREFIX "runtime.unlock");
469 * sleep and wakeup on one-time events.
470 * before any calls to notesleep or notewakeup,
471 * must call noteclear to initialize the Note.
472 * then, exactly one thread can call notesleep
473 * and exactly one thread can call notewakeup (once).
474 * once notewakeup has been called, the notesleep
475 * will return. future notesleep will return immediately.
476 * subsequent noteclear must be called only after
477 * previous notesleep has returned, e.g. it's disallowed
478 * to call noteclear straight after notewakeup.
480 * notetsleep is like notesleep but wakes up after
481 * a given number of nanoseconds even if the event
482 * has not yet happened. if a goroutine uses notetsleep to
483 * wake up early, it must wait to call noteclear until it
484 * can be sure that no other goroutine is calling
485 * notewakeup.
487 * notesleep/notetsleep are generally called on g0,
488 * notetsleepg is similar to notetsleep but is called on user g.
490 void runtime_noteclear(Note*)
491 __asm__ (GOSYM_PREFIX "runtime.noteclear");
492 void runtime_notesleep(Note*)
493 __asm__ (GOSYM_PREFIX "runtime.notesleep");
494 void runtime_notewakeup(Note*)
495 __asm__ (GOSYM_PREFIX "runtime.notewakeup");
496 bool runtime_notetsleep(Note*, int64) // false - timeout
497 __asm__ (GOSYM_PREFIX "runtime.notetsleep");
498 bool runtime_notetsleepg(Note*, int64) // false - timeout
499 __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
502 * Lock-free stack.
503 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
504 * The stack does not keep pointers to nodes,
505 * so they can be garbage collected if there are no other pointers to nodes.
507 void runtime_lfstackpush(uint64 *head, LFNode *node)
508 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
509 LFNode* runtime_lfstackpop(uint64 *head);
512 * Parallel for over [0, n).
513 * body() is executed for each iteration.
514 * nthr - total number of worker threads.
515 * if wait=true, threads return from parfor() when all work is done;
516 * otherwise, threads can return while other threads are still finishing processing.
518 ParFor* runtime_parforalloc(uint32 nthrmax);
519 void runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, bool wait, const FuncVal *body);
520 void runtime_parfordo(ParFor *desc);
521 void runtime_parforiters(ParFor*, uintptr, uintptr*, uintptr*);
524 * low level C-called
526 #define runtime_mmap mmap
527 #define runtime_munmap munmap
528 #define runtime_madvise madvise
529 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
530 #define runtime_getcallerpc(p) __builtin_return_address(0)
532 #ifdef __rtems__
533 void __wrap_rtems_task_variable_add(void **);
534 #endif
537 * Names generated by gccgo.
539 #define runtime_printbool __go_print_bool
540 #define runtime_printfloat __go_print_double
541 #define runtime_printint __go_print_int64
542 #define runtime_printiface __go_print_interface
543 #define runtime_printeface __go_print_empty_interface
544 #define runtime_printstring __go_print_string
545 #define runtime_printpointer __go_print_pointer
546 #define runtime_printuint __go_print_uint64
547 #define runtime_printslice __go_print_slice
548 #define runtime_printcomplex __go_print_complex
551 * runtime go-called
553 void runtime_printbool(_Bool);
554 void runtime_printbyte(int8);
555 void runtime_printfloat(double);
556 void runtime_printint(int64);
557 void runtime_printiface(Iface);
558 void runtime_printeface(Eface);
559 void runtime_printstring(String);
560 void runtime_printpc(void*);
561 void runtime_printpointer(void*);
562 void runtime_printuint(uint64);
563 void runtime_printhex(uint64);
564 void runtime_printslice(Slice);
565 void runtime_printcomplex(complex double);
566 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
567 void **, void **)
568 __asm__ (GOSYM_PREFIX "reflect.call");
569 #define runtime_panic __go_panic
572 * runtime c-called (but written in Go)
574 void runtime_printany(Eface)
575 __asm__ (GOSYM_PREFIX "runtime.Printany");
576 void runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
577 __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
578 void runtime_newErrorCString(const char*, Eface*)
579 __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
582 * wrapped for go users
584 void runtime_semacquire(uint32 volatile *, bool);
585 void runtime_semrelease(uint32 volatile *);
586 int32 runtime_gomaxprocsfunc(int32 n);
587 void runtime_procyield(uint32)
588 __asm__(GOSYM_PREFIX "runtime.procyield");
589 void runtime_osyield(void)
590 __asm__(GOSYM_PREFIX "runtime.osyield");
591 void runtime_lockOSThread(void);
592 void runtime_unlockOSThread(void);
593 bool runtime_lockedOSThread(void);
595 bool runtime_showframe(String, bool);
596 void runtime_printcreatedby(G*);
598 uintptr runtime_memlimit(void);
600 #define ISNAN(f) __builtin_isnan(f)
602 enum
604 UseSpanType = 1,
607 #define runtime_setitimer setitimer
609 void runtime_check(void)
610 __asm__ (GOSYM_PREFIX "runtime.check");
612 // A list of global variables that the garbage collector must scan.
613 struct root_list {
614 struct root_list *next;
615 struct root {
616 void *decl;
617 size_t size;
618 } roots[];
621 void __go_register_gc_roots(struct root_list*);
623 // Size of stack space allocated using Go's allocator.
624 // This will be 0 when using split stacks, as in that case
625 // the stacks are allocated by the splitstack library.
626 extern uintptr runtime_stacks_sys;
628 struct backtrace_state;
629 extern struct backtrace_state *__go_get_backtrace_state(void);
630 extern _Bool __go_file_line(uintptr, int, String*, String*, intgo *);
631 extern void runtime_main(void*);
632 extern uint32 runtime_in_callers;
634 int32 getproccount(void);
636 #define PREFETCH(p) __builtin_prefetch(p)
638 bool runtime_gcwaiting(void);
639 void runtime_badsignal(int);
640 Defer* runtime_newdefer(void);
641 void runtime_freedefer(Defer*);
643 struct time_now_ret
645 int64_t sec;
646 int32_t nsec;
649 struct time_now_ret now() __asm__ (GOSYM_PREFIX "time.now")
650 __attribute__ ((no_split_stack));
652 extern void _cgo_wait_runtime_init_done (void);
653 extern void _cgo_notify_runtime_init_done (void);
654 extern _Bool runtime_iscgo;
655 extern _Bool runtime_cgoHasExtraM;
656 extern Hchan *runtime_main_init_done;
657 extern uintptr __go_end __attribute__ ((weak));