9392df162bffb5ab368b64ce42ccd36f664e6fe2
[platform/upstream/gcc48.git] / libgo / runtime / runtime.h
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.
4
5 #include "config.h"
6
7 #include "go-assert.h"
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <pthread.h>
17 #include <semaphore.h>
18 #include <ucontext.h>
19
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23
24 #include "interface.h"
25 #include "go-alloc.h"
26
27 #define _STRINGIFY2_(x) #x
28 #define _STRINGIFY_(x) _STRINGIFY2_(x)
29 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
30
31 /* This file supports C files copied from the 6g runtime library.
32    This is a version of the 6g runtime.h rewritten for gccgo's version
33    of the code.  */
34
35 typedef signed int   int8    __attribute__ ((mode (QI)));
36 typedef unsigned int uint8   __attribute__ ((mode (QI)));
37 typedef signed int   int16   __attribute__ ((mode (HI)));
38 typedef unsigned int uint16  __attribute__ ((mode (HI)));
39 typedef signed int   int32   __attribute__ ((mode (SI)));
40 typedef unsigned int uint32  __attribute__ ((mode (SI)));
41 typedef signed int   int64   __attribute__ ((mode (DI)));
42 typedef unsigned int uint64  __attribute__ ((mode (DI)));
43 typedef float        float32 __attribute__ ((mode (SF)));
44 typedef double       float64 __attribute__ ((mode (DF)));
45 typedef signed int   intptr __attribute__ ((mode (pointer)));
46 typedef unsigned int uintptr __attribute__ ((mode (pointer)));
47
48 typedef intptr          intgo; // Go's int
49 typedef uintptr         uintgo; // Go's uint
50
51 /* Defined types.  */
52
53 typedef uint8                   bool;
54 typedef uint8                   byte;
55 typedef struct  Func            Func;
56 typedef struct  G               G;
57 typedef union   Lock            Lock;
58 typedef struct  M               M;
59 typedef union   Note            Note;
60 typedef struct  SigTab          SigTab;
61 typedef struct  MCache          MCache;
62 typedef struct  FixAlloc        FixAlloc;
63 typedef struct  Hchan           Hchan;
64 typedef struct  Timers          Timers;
65 typedef struct  Timer           Timer;
66 typedef struct  GCStats         GCStats;
67 typedef struct  LFNode          LFNode;
68 typedef struct  ParFor          ParFor;
69 typedef struct  ParForThread    ParForThread;
70 typedef struct  CgoMal          CgoMal;
71
72 typedef struct  __go_open_array         Slice;
73 typedef struct  String                  String;
74 typedef struct  __go_interface          Iface;
75 typedef struct  __go_empty_interface    Eface;
76 typedef struct  __go_type_descriptor    Type;
77 typedef struct  __go_defer_stack        Defer;
78 typedef struct  __go_panic_stack        Panic;
79
80 typedef struct  __go_ptr_type           PtrType;
81 typedef struct  __go_func_type          FuncType;
82 typedef struct  __go_map_type           MapType;
83
84 typedef struct  Traceback       Traceback;
85
86 typedef struct  Location        Location;
87
88 /*
89  * Per-CPU declaration.
90  */
91 extern M*       runtime_m(void);
92 extern G*       runtime_g(void);
93
94 extern M        runtime_m0;
95 extern G        runtime_g0;
96
97 /*
98  * defined constants
99  */
100 enum
101 {
102         // G status
103         //
104         // If you add to this list, add to the list
105         // of "okay during garbage collection" status
106         // in mgc0.c too.
107         Gidle,
108         Grunnable,
109         Grunning,
110         Gsyscall,
111         Gwaiting,
112         Gmoribund,
113         Gdead,
114 };
115 enum
116 {
117         true    = 1,
118         false   = 0,
119 };
120 enum
121 {
122         PtrSize = sizeof(void*),
123 };
124 enum
125 {
126         // Per-M stack segment cache size.
127         StackCacheSize = 32,
128         // Global <-> per-M stack segment cache transfer batch size.
129         StackCacheBatch = 16,
130 };
131
132 /*
133  * structures
134  */
135 union   Lock
136 {
137         uint32  key;    // futex-based impl
138         M*      waitm;  // linked list of waiting M's (sema-based impl)
139 };
140 union   Note
141 {
142         uint32  key;    // futex-based impl
143         M*      waitm;  // waiting M (sema-based impl)
144 };
145 struct String
146 {
147         const byte*     str;
148         intgo           len;
149 };
150 struct  GCStats
151 {
152         // the struct must consist of only uint64's,
153         // because it is casted to uint64[].
154         uint64  nhandoff;
155         uint64  nhandoffcnt;
156         uint64  nprocyield;
157         uint64  nosyield;
158         uint64  nsleep;
159 };
160
161 // A location in the program, used for backtraces.
162 struct  Location
163 {
164         uintptr pc;
165         String  filename;
166         String  function;
167         intgo   lineno;
168 };
169
170 struct  G
171 {
172         Defer*  defer;
173         Panic*  panic;
174         void*   exception;      // current exception being thrown
175         bool    is_foreign;     // whether current exception from other language
176         void    *gcstack;       // if status==Gsyscall, gcstack = stackbase to use during gc
177         uintptr gcstack_size;
178         void*   gcnext_segment;
179         void*   gcnext_sp;
180         void*   gcinitial_sp;
181         ucontext_t gcregs;
182         byte*   entry;          // initial function
183         G*      alllink;        // on allg
184         void*   param;          // passed parameter on wakeup
185         bool    fromgogo;       // reached from gogo
186         int16   status;
187         int64   goid;
188         uint32  selgen;         // valid sudog pointer
189         const char*     waitreason;     // if status==Gwaiting
190         G*      schedlink;
191         bool    readyonstop;
192         bool    ispanic;
193         bool    issystem;
194         int8    raceignore; // ignore race detection events
195         M*      m;              // for debuggers, but offset not hard-coded
196         M*      lockedm;
197         M*      idlem;
198         int32   sig;
199         int32   writenbuf;
200         byte*   writebuf;
201         // DeferChunk   *dchunk;
202         // DeferChunk   *dchunknext;
203         uintptr sigcode0;
204         uintptr sigcode1;
205         // uintptr      sigpc;
206         uintptr gopc;   // pc of go statement that created this goroutine
207
208         int32   ncgo;
209         CgoMal* cgomal;
210
211         Traceback* traceback;
212
213         ucontext_t      context;
214         void*           stack_context[10];
215 };
216
217 struct  M
218 {
219         G*      g0;             // goroutine with scheduling stack
220         G*      gsignal;        // signal-handling G
221         G*      curg;           // current running goroutine
222         int32   id;
223         int32   mallocing;
224         int32   throwing;
225         int32   gcing;
226         int32   locks;
227         int32   nomemprof;
228         int32   waitnextg;
229         int32   dying;
230         int32   profilehz;
231         int32   helpgc;
232         uint32  fastrand;
233         uint64  ncgocall;       // number of cgo calls in total
234         Note    havenextg;
235         G*      nextg;
236         M*      alllink;        // on allm
237         M*      schedlink;
238         MCache  *mcache;
239         G*      lockedg;
240         G*      idleg;
241         Location createstack[32];       // Stack that created this thread.
242         M*      nextwaitm;      // next M waiting for lock
243         uintptr waitsema;       // semaphore for parking on locks
244         uint32  waitsemacount;
245         uint32  waitsemalock;
246         GCStats gcstats;
247         bool    racecall;
248         void*   racepc;
249
250         uintptr settype_buf[1024];
251         uintptr settype_bufsize;
252
253         uintptr end[];
254 };
255
256 struct  SigTab
257 {
258         int32   sig;
259         int32   flags;
260 };
261 enum
262 {
263         SigNotify = 1<<0,       // let signal.Notify have signal, even if from kernel
264         SigKill = 1<<1,         // if signal.Notify doesn't take it, exit quietly
265         SigThrow = 1<<2,        // if signal.Notify doesn't take it, exit loudly
266         SigPanic = 1<<3,        // if the signal is from the kernel, panic
267         SigDefault = 1<<4,      // if the signal isn't explicitly requested, don't monitor it
268 };
269
270 #ifndef NSIG
271 #define NSIG 32
272 #endif
273
274 // NOTE(rsc): keep in sync with extern.go:/type.Func.
275 // Eventually, the loaded symbol table should be closer to this form.
276 struct  Func
277 {
278         String  name;
279         uintptr entry;  // entry pc
280 };
281
282
283 #ifdef GOOS_windows
284 enum {
285    Windows = 1
286 };
287 #else
288 enum {
289    Windows = 0
290 };
291 #endif
292
293 struct  Timers
294 {
295         Lock;
296         G       *timerproc;
297         bool            sleeping;
298         bool            rescheduling;
299         Note    waitnote;
300         Timer   **t;
301         int32   len;
302         int32   cap;
303 };
304
305 // Package time knows the layout of this structure.
306 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
307 struct  Timer
308 {
309         int32   i;              // heap index
310
311         // Timer wakes up at when, and then at when+period, ... (period > 0 only)
312         // each time calling f(now, arg) in the timer goroutine, so f must be
313         // a well-behaved function and not block.
314         int64   when;
315         int64   period;
316         void    (*f)(int64, Eface);
317         Eface   arg;
318 };
319
320 // Lock-free stack node.
321 struct LFNode
322 {
323         LFNode  *next;
324         uintptr pushcnt;
325 };
326
327 // Parallel for descriptor.
328 struct ParFor
329 {
330         void (*body)(ParFor*, uint32);  // executed for each element
331         uint32 done;                    // number of idle threads
332         uint32 nthr;                    // total number of threads
333         uint32 nthrmax;                 // maximum number of threads
334         uint32 thrseq;                  // thread id sequencer
335         uint32 cnt;                     // iteration space [0, cnt)
336         void *ctx;                      // arbitrary user context
337         bool wait;                      // if true, wait while all threads finish processing,
338                                         // otherwise parfor may return while other threads are still working
339         ParForThread *thr;              // array of thread descriptors
340         // stats
341         uint64 nsteal;
342         uint64 nstealcnt;
343         uint64 nprocyield;
344         uint64 nosyield;
345         uint64 nsleep;
346 };
347
348 // Track memory allocated by code not written in Go during a cgo call,
349 // so that the garbage collector can see them.
350 struct CgoMal
351 {
352         CgoMal  *next;
353         byte    *alloc;
354 };
355
356 /*
357  * defined macros
358  *    you need super-gopher-guru privilege
359  *    to add this list.
360  */
361 #define nelem(x)        (sizeof(x)/sizeof((x)[0]))
362 #define nil             ((void*)0)
363 #define USED(v)         ((void) v)
364 #define ROUND(x, n)     (((x)+(n)-1)&~((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
365
366 /*
367  * external data
368  */
369 extern  uintptr runtime_zerobase;
370 extern  G*      runtime_allg;
371 extern  G*      runtime_lastg;
372 extern  M*      runtime_allm;
373 extern  int32   runtime_gomaxprocs;
374 extern  bool    runtime_singleproc;
375 extern  uint32  runtime_panicking;
376 extern  int32   runtime_gcwaiting;              // gc is waiting to run
377 extern  int32   runtime_ncpu;
378
379 /*
380  * common functions and data
381  */
382 intgo   runtime_findnull(const byte*);
383 void    runtime_dump(byte*, int32);
384
385 /*
386  * very low level c-called
387  */
388 void    runtime_args(int32, byte**);
389 void    runtime_osinit();
390 void    runtime_goargs(void);
391 void    runtime_goenvs(void);
392 void    runtime_goenvs_unix(void);
393 void    runtime_throw(const char*) __attribute__ ((noreturn));
394 void    runtime_panicstring(const char*) __attribute__ ((noreturn));
395 void    runtime_prints(const char*);
396 void    runtime_printf(const char*, ...);
397 void*   runtime_mal(uintptr);
398 void    runtime_schedinit(void);
399 void    runtime_initsig(void);
400 void    runtime_sigenable(uint32 sig);
401 int32   runtime_gotraceback(void);
402 void    runtime_goroutineheader(G*);
403 void    runtime_goroutinetrailer(G*);
404 void    runtime_traceback();
405 void    runtime_tracebackothers(G*);
406 void    runtime_printtrace(Location*, int32, bool);
407 String  runtime_gostring(const byte*);
408 String  runtime_gostringnocopy(const byte*);
409 void*   runtime_mstart(void*);
410 G*      runtime_malg(int32, byte**, size_t*);
411 void    runtime_minit(void);
412 void    runtime_mallocinit(void);
413 void    runtime_gosched(void);
414 void    runtime_park(void(*)(Lock*), Lock*, const char*);
415 void    runtime_tsleep(int64, const char*);
416 M*      runtime_newm(void);
417 void    runtime_goexit(void);
418 void    runtime_entersyscall(void) __asm__ (GOSYM_PREFIX "syscall.Entersyscall");
419 void    runtime_exitsyscall(void) __asm__ (GOSYM_PREFIX "syscall.Exitsyscall");
420 void    siginit(void);
421 bool    __go_sigsend(int32 sig);
422 int32   runtime_callers(int32, Location*, int32);
423 int64   runtime_nanotime(void);
424 int64   runtime_cputicks(void);
425 int64   runtime_tickspersecond(void);
426 void    runtime_blockevent(int64, int32);
427 extern int64 runtime_blockprofilerate;
428
429 void    runtime_stoptheworld(void);
430 void    runtime_starttheworld(void);
431 extern uint32 runtime_worldsema;
432 G*      __go_go(void (*pfn)(void*), void*);
433
434 /*
435  * mutual exclusion locks.  in the uncontended case,
436  * as fast as spin locks (just a few user-level instructions),
437  * but on the contention path they sleep in the kernel.
438  * a zeroed Lock is unlocked (no need to initialize each lock).
439  */
440 void    runtime_lock(Lock*);
441 void    runtime_unlock(Lock*);
442
443 /*
444  * sleep and wakeup on one-time events.
445  * before any calls to notesleep or notewakeup,
446  * must call noteclear to initialize the Note.
447  * then, exactly one thread can call notesleep
448  * and exactly one thread can call notewakeup (once).
449  * once notewakeup has been called, the notesleep
450  * will return.  future notesleep will return immediately.
451  * subsequent noteclear must be called only after
452  * previous notesleep has returned, e.g. it's disallowed
453  * to call noteclear straight after notewakeup.
454  *
455  * notetsleep is like notesleep but wakes up after
456  * a given number of nanoseconds even if the event
457  * has not yet happened.  if a goroutine uses notetsleep to
458  * wake up early, it must wait to call noteclear until it
459  * can be sure that no other goroutine is calling
460  * notewakeup.
461  */
462 void    runtime_noteclear(Note*);
463 void    runtime_notesleep(Note*);
464 void    runtime_notewakeup(Note*);
465 void    runtime_notetsleep(Note*, int64);
466
467 /*
468  * low-level synchronization for implementing the above
469  */
470 uintptr runtime_semacreate(void);
471 int32   runtime_semasleep(int64);
472 void    runtime_semawakeup(M*);
473 // or
474 void    runtime_futexsleep(uint32*, uint32, int64);
475 void    runtime_futexwakeup(uint32*, uint32);
476
477 /*
478  * Lock-free stack.
479  * Initialize uint64 head to 0, compare with 0 to test for emptiness.
480  * The stack does not keep pointers to nodes,
481  * so they can be garbage collected if there are no other pointers to nodes.
482  */
483 void    runtime_lfstackpush(uint64 *head, LFNode *node)
484   __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
485 LFNode* runtime_lfstackpop(uint64 *head);
486
487 /*
488  * Parallel for over [0, n).
489  * body() is executed for each iteration.
490  * nthr - total number of worker threads.
491  * ctx - arbitrary user context.
492  * if wait=true, threads return from parfor() when all work is done;
493  * otherwise, threads can return while other threads are still finishing processing.
494  */
495 ParFor* runtime_parforalloc(uint32 nthrmax);
496 void    runtime_parforsetup(ParFor *desc, uint32 nthr, uint32 n, void *ctx, bool wait, void (*body)(ParFor*, uint32));
497 void    runtime_parfordo(ParFor *desc) __asm__ (GOSYM_PREFIX "runtime.parfordo");
498
499 /*
500  * low level C-called
501  */
502 #define runtime_mmap mmap
503 #define runtime_munmap munmap
504 #define runtime_madvise madvise
505 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
506 #define runtime_getcallerpc(p) __builtin_return_address(0)
507
508 #ifdef __rtems__
509 void __wrap_rtems_task_variable_add(void **);
510 #endif
511
512 /*
513  * Names generated by gccgo.
514  */
515 #define runtime_printbool       __go_print_bool
516 #define runtime_printfloat      __go_print_double
517 #define runtime_printint        __go_print_int64
518 #define runtime_printiface      __go_print_interface
519 #define runtime_printeface      __go_print_empty_interface
520 #define runtime_printstring     __go_print_string
521 #define runtime_printpointer    __go_print_pointer
522 #define runtime_printuint       __go_print_uint64
523 #define runtime_printslice      __go_print_slice
524 #define runtime_printcomplex    __go_print_complex
525
526 /*
527  * runtime go-called
528  */
529 void    runtime_printbool(_Bool);
530 void    runtime_printfloat(double);
531 void    runtime_printint(int64);
532 void    runtime_printiface(Iface);
533 void    runtime_printeface(Eface);
534 void    runtime_printstring(String);
535 void    runtime_printpc(void*);
536 void    runtime_printpointer(void*);
537 void    runtime_printuint(uint64);
538 void    runtime_printhex(uint64);
539 void    runtime_printslice(Slice);
540 void    runtime_printcomplex(__complex double);
541
542 struct __go_func_type;
543 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
544                   void **, void **)
545   __asm__ (GOSYM_PREFIX "reflect.call");
546
547 /* Functions.  */
548 #define runtime_panic __go_panic
549 #define runtime_write(d, v, n) write((d), (v), (n))
550 #define runtime_malloc(s) __go_alloc(s)
551 #define runtime_free(p) __go_free(p)
552 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
553 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
554 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
555 #define runtime_exit(s) exit(s)
556 MCache* runtime_allocmcache(void);
557 void    free(void *v);
558 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
559 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
560 #define runtime_cas64(pval, pold, new) __atomic_compare_exchange_n (pval, pold, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
561 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
562 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
563 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
564 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
565 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
566 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
567 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
568 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
569 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
570 #define PREFETCH(p) __builtin_prefetch(p)
571
572 struct __go_func_type;
573 bool    runtime_addfinalizer(void*, void(*fn)(void*), const struct __go_func_type *);
574 #define runtime_getcallersp(p) __builtin_frame_address(1)
575 int32   runtime_mcount(void);
576 int32   runtime_gcount(void);
577 void    runtime_dopanic(int32) __attribute__ ((noreturn));
578 void    runtime_startpanic(void);
579 void    runtime_ready(G*);
580 const byte*     runtime_getenv(const char*);
581 int32   runtime_atoi(const byte*);
582 uint32  runtime_fastrand1(void);
583
584 void    runtime_sigprof();
585 void    runtime_resetcpuprofiler(int32);
586 void    runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
587 void    runtime_usleep(uint32);
588
589 /*
590  * runtime c-called (but written in Go)
591  */
592 void    runtime_printany(Eface)
593      __asm__ (GOSYM_PREFIX "runtime.Printany");
594 void    runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
595      __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
596 void    runtime_newErrorString(String, Eface*)
597      __asm__ (GOSYM_PREFIX "runtime.NewErrorString");
598
599 /*
600  * wrapped for go users
601  */
602 #define ISNAN(f) __builtin_isnan(f)
603 void    runtime_semacquire(uint32 volatile *);
604 void    runtime_semrelease(uint32 volatile *);
605 int32   runtime_gomaxprocsfunc(int32 n);
606 void    runtime_procyield(uint32);
607 void    runtime_osyield(void);
608 void    runtime_LockOSThread(void) __asm__ (GOSYM_PREFIX "runtime.LockOSThread");
609 void    runtime_UnlockOSThread(void) __asm__ (GOSYM_PREFIX "runtime.UnlockOSThread");
610
611 bool    runtime_showframe(String, bool);
612
613 uintptr runtime_memlimit(void);
614
615 // If appropriate, ask the operating system to control whether this
616 // thread should receive profiling signals.  This is only necessary on OS X.
617 // An operating system should not deliver a profiling signal to a
618 // thread that is not actually executing (what good is that?), but that's
619 // what OS X prefers to do.  When profiling is turned on, we mask
620 // away the profiling signal when threads go to sleep, so that OS X
621 // is forced to deliver the signal to a thread that's actually running.
622 // This is a no-op on other systems.
623 void    runtime_setprof(bool);
624
625 enum
626 {
627         UseSpanType = 1,
628 };
629
630 void    runtime_setsig(int32, bool, bool);
631 #define runtime_setitimer setitimer
632
633 void    runtime_check(void);
634
635 // A list of global variables that the garbage collector must scan.
636 struct root_list {
637         struct root_list *next;
638         struct root {
639                 void *decl;
640                 size_t size;
641         } roots[];
642 };
643
644 void    __go_register_gc_roots(struct root_list*);
645
646 // Size of stack space allocated using Go's allocator.
647 // This will be 0 when using split stacks, as in that case
648 // the stacks are allocated by the splitstack library.
649 extern uintptr runtime_stacks_sys;
650
651 struct backtrace_state;
652 extern struct backtrace_state *__go_get_backtrace_state(void);
653 extern _Bool __go_file_line(uintptr, String*, String*, intgo *);
654 extern byte* runtime_progname();
655
656 int32 getproccount(void);