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.
12 #include <sys/types.h>
17 #include <semaphore.h>
20 #ifdef HAVE_SYS_MMAN_H
24 #include "interface.h"
27 #define _STRINGIFY2_(x) #x
28 #define _STRINGIFY_(x) _STRINGIFY2_(x)
29 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
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
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)));
48 typedef intptr intgo; // Go's int
49 typedef uintptr uintgo; // Go's uint
55 typedef struct Func Func;
57 typedef union Lock Lock;
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;
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;
80 typedef struct __go_ptr_type PtrType;
81 typedef struct __go_func_type FuncType;
82 typedef struct __go_map_type MapType;
84 typedef struct Traceback Traceback;
86 typedef struct Location Location;
89 * Per-CPU declaration.
91 extern M* runtime_m(void);
92 extern G* runtime_g(void);
104 // If you add to this list, add to the list
105 // of "okay during garbage collection" status
122 PtrSize = sizeof(void*),
126 // Per-M stack segment cache size.
128 // Global <-> per-M stack segment cache transfer batch size.
129 StackCacheBatch = 16,
137 uint32 key; // futex-based impl
138 M* waitm; // linked list of waiting M's (sema-based impl)
142 uint32 key; // futex-based impl
143 M* waitm; // waiting M (sema-based impl)
152 // the struct must consist of only uint64's,
153 // because it is casted to uint64[].
161 // A location in the program, used for backtraces.
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;
182 byte* entry; // initial function
183 G* alllink; // on allg
184 void* param; // passed parameter on wakeup
185 bool fromgogo; // reached from gogo
188 uint32 selgen; // valid sudog pointer
189 const char* waitreason; // if status==Gwaiting
194 int8 raceignore; // ignore race detection events
195 M* m; // for debuggers, but offset not hard-coded
201 // DeferChunk *dchunk;
202 // DeferChunk *dchunknext;
206 uintptr gopc; // pc of go statement that created this goroutine
211 Traceback* traceback;
214 void* stack_context[10];
219 G* g0; // goroutine with scheduling stack
220 G* gsignal; // signal-handling G
221 G* curg; // current running goroutine
233 uint64 ncgocall; // number of cgo calls in total
236 M* alllink; // on allm
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;
250 uintptr settype_buf[1024];
251 uintptr settype_bufsize;
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
274 // NOTE(rsc): keep in sync with extern.go:/type.Func.
275 // Eventually, the loaded symbol table should be closer to this form.
279 uintptr entry; // entry pc
305 // Package time knows the layout of this structure.
306 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
309 int32 i; // heap index
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.
316 void (*f)(int64, Eface);
320 // Lock-free stack node.
327 // Parallel for descriptor.
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
348 // Track memory allocated by code not written in Go during a cgo call,
349 // so that the garbage collector can see them.
358 * you need super-gopher-guru privilege
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 */
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;
380 * common functions and data
382 intgo runtime_findnull(const byte*);
383 void runtime_dump(byte*, int32);
386 * very low level c-called
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");
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;
429 void runtime_stoptheworld(void);
430 void runtime_starttheworld(void);
431 extern uint32 runtime_worldsema;
432 G* __go_go(void (*pfn)(void*), void*);
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).
440 void runtime_lock(Lock*);
441 void runtime_unlock(Lock*);
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.
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
462 void runtime_noteclear(Note*);
463 void runtime_notesleep(Note*);
464 void runtime_notewakeup(Note*);
465 void runtime_notetsleep(Note*, int64);
468 * low-level synchronization for implementing the above
470 uintptr runtime_semacreate(void);
471 int32 runtime_semasleep(int64);
472 void runtime_semawakeup(M*);
474 void runtime_futexsleep(uint32*, uint32, int64);
475 void runtime_futexwakeup(uint32*, uint32);
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.
483 void runtime_lfstackpush(uint64 *head, LFNode *node)
484 __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
485 LFNode* runtime_lfstackpop(uint64 *head);
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.
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");
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)
509 void __wrap_rtems_task_variable_add(void **);
513 * Names generated by gccgo.
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
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);
542 struct __go_func_type;
543 void reflect_call(const struct __go_func_type *, const void *, _Bool, _Bool,
545 __asm__ (GOSYM_PREFIX "reflect.call");
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);
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)
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);
584 void runtime_sigprof();
585 void runtime_resetcpuprofiler(int32);
586 void runtime_setcpuprofilerate(void(*)(uintptr*, int32), int32);
587 void runtime_usleep(uint32);
590 * runtime c-called (but written in Go)
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");
600 * wrapped for go users
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");
611 bool runtime_showframe(String, bool);
613 uintptr runtime_memlimit(void);
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);
630 void runtime_setsig(int32, bool, bool);
631 #define runtime_setitimer setitimer
633 void runtime_check(void);
635 // A list of global variables that the garbage collector must scan.
637 struct root_list *next;
644 void __go_register_gc_roots(struct root_list*);
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;
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();
656 int32 getproccount(void);