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.
13 #include <sys/types.h>
18 #include <semaphore.h>
21 #ifdef HAVE_SYS_MMAN_H
25 #include "interface.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
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
56 typedef struct Func Func
;
58 typedef struct Lock Lock
;
61 typedef struct Note Note
;
62 typedef struct String String
;
63 typedef struct FuncVal FuncVal
;
64 typedef struct SigTab SigTab
;
65 typedef struct MCache MCache
;
66 typedef struct FixAlloc FixAlloc
;
67 typedef struct Hchan Hchan
;
68 typedef struct Timers Timers
;
69 typedef struct Timer Timer
;
70 typedef struct GCStats GCStats
;
71 typedef struct LFNode LFNode
;
72 typedef struct ParFor ParFor
;
73 typedef struct ParForThread ParForThread
;
74 typedef struct CgoMal CgoMal
;
75 typedef struct PollDesc PollDesc
;
76 typedef struct DebugVars DebugVars
;
78 typedef struct __go_open_array Slice
;
79 typedef struct __go_interface Iface
;
80 typedef struct __go_empty_interface Eface
;
81 typedef struct __go_type_descriptor Type
;
82 typedef struct __go_defer_stack Defer
;
83 typedef struct __go_panic_stack Panic
;
85 typedef struct __go_ptr_type PtrType
;
86 typedef struct __go_func_type FuncType
;
87 typedef struct __go_interface_type InterfaceType
;
88 typedef struct __go_map_type MapType
;
89 typedef struct __go_channel_type ChanType
;
91 typedef struct Traceback Traceback
;
93 typedef struct Location Location
;
96 * Per-CPU declaration.
98 extern M
* runtime_m(void);
99 extern G
* runtime_g(void);
111 // If you add to this list, add to the list
112 // of "okay during garbage collection" status
119 Gmoribund_unused
, // currently unused, but hardcoded in gdb scripts
138 PtrSize
= sizeof(void*),
142 // Per-M stack segment cache size.
144 // Global <-> per-M stack segment cache transfer batch size.
145 StackCacheBatch
= 16,
152 // Futex-based impl treats it as uint32 key,
153 // while sema-based impl as M* waitm.
154 // Used to be a union, but unions break precise GC.
159 // Futex-based impl treats it as uint32 key,
160 // while sema-based impl as M* waitm.
161 // Used to be a union, but unions break precise GC.
172 // variable-size, fn-specific data here
176 // the struct must consist of only uint64's,
177 // because it is casted to uint64[].
185 // A location in the program, used for backtraces.
196 void* closure
; // Closure value.
199 void* exception
; // current exception being thrown
200 bool is_foreign
; // whether current exception from other language
201 void *gcstack
; // if status==Gsyscall, gcstack = stackbase to use during gc
202 uintptr gcstack_size
;
203 void* gcnext_segment
;
207 byte
* entry
; // initial function
208 void* param
; // passed parameter on wakeup
209 bool fromgogo
; // reached from gogo
211 uint32 selgen
; // valid sudog pointer
213 int64 waitsince
; // approx time when the G become blocked
214 const char* waitreason
; // if status==Gwaiting
217 bool issystem
; // do not output in stack dump
218 bool isbackground
; // ignore in deadlock detector
219 M
* m
; // for debuggers, but offset not hard-coded
227 uintptr gopc
; // pc of go statement that created this goroutine
232 Traceback
* traceback
;
235 void* stack_context
[10];
240 G
* g0
; // goroutine with scheduling stack
241 G
* gsignal
; // signal-handling G
243 size_t gsignalstacksize
;
244 void (*mstartfn
)(void);
245 G
* curg
; // current running goroutine
246 G
* caughtsig
; // goroutine running during fatal signal
247 P
* p
; // attached P for executing Go code (nil if not executing Go code)
257 bool spinning
; // M is out of work and is actively looking for work
258 bool blocked
; // M is blocked on a Note
260 uint64 ncgocall
; // number of cgo calls in total
261 int32 ncgo
; // number of cgo calls currently in progress
264 M
* alllink
; // on allm
268 Location createstack
[32]; // Stack that created this thread.
269 uint32 locked
; // tracking for LockOSThread
270 M
* nextwaitm
; // next M waiting for lock
271 uintptr waitsema
; // semaphore for parking on locks
272 uint32 waitsemacount
;
277 bool dropextram
; // for gccgo: drop after call is done.
278 bool (*waitunlockf
)(G
*, void*);
281 uintptr settype_buf
[1024];
282 uintptr settype_bufsize
;
292 uint32 status
; // one of Pidle/Prunning/...
294 uint32 schedtick
; // incremented on every scheduler call
295 uint32 syscalltick
; // incremented on every system call
296 M
* m
; // back-link to associated M (nil if idle)
298 Defer
* deferpool
; // pool of available Defer structs (see panic.c)
300 // Cache of goroutine ids, amortizes accesses to runtime_sched.goidgen.
304 // Queue of runnable goroutines.
309 // Available G's (status == Gdead)
316 // The m->locked word holds two pieces of state counting active calls to LockOSThread/lockOSThread.
317 // The low bit (LockExternal) is a boolean reporting whether any LockOSThread call is active.
318 // External locks are not recursive; a second lock is silently ignored.
319 // The upper bits of m->lockedcount record the nesting depth of calls to lockOSThread
320 // (counting up by LockInternal), popped by unlockOSThread (counting down by LockInternal).
321 // Internal locks can be recursive. For instance, a lock for cgo can occur while the main
322 // goroutine is holding the lock during the initialization phase.
336 SigNotify
= 1<<0, // let signal.Notify have signal, even if from kernel
337 SigKill
= 1<<1, // if signal.Notify doesn't take it, exit quietly
338 SigThrow
= 1<<2, // if signal.Notify doesn't take it, exit loudly
339 SigPanic
= 1<<3, // if the signal is from the kernel, panic
340 SigDefault
= 1<<4, // if the signal isn't explicitly requested, don't monitor it
341 SigHandling
= 1<<5, // our signal handler is registered
342 SigIgnored
= 1<<6, // the signal was ignored before we registered for it
345 // Layout of in-memory per-function information prepared by linker
346 // See http://golang.org/s/go12symtab.
347 // Keep in sync with linker and with ../../libmach/sym.c
348 // and with package debug/gosym.
352 uintptr entry
; // entry pc
386 // Package time knows the layout of this structure.
387 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
390 int32 i
; // heap index
392 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
393 // each time calling f(now, arg) in the timer goroutine, so f must be
394 // a well-behaved function and not block.
401 // Lock-free stack node.
408 // Parallel for descriptor.
411 void (*body
)(ParFor
*, uint32
); // executed for each element
412 uint32 done
; // number of idle threads
413 uint32 nthr
; // total number of threads
414 uint32 nthrmax
; // maximum number of threads
415 uint32 thrseq
; // thread id sequencer
416 uint32 cnt
; // iteration space [0, cnt)
417 void *ctx
; // arbitrary user context
418 bool wait
; // if true, wait while all threads finish processing,
419 // otherwise parfor may return while other threads are still working
420 ParForThread
*thr
; // array of thread descriptors
421 uint32 pad
; // to align ParForThread.pos for 64-bit atomic operations
430 // Track memory allocated by code not written in Go during a cgo call,
431 // so that the garbage collector can see them.
438 // Holds variables parsed from GODEBUG env var.
441 int32 allocfreetrace
;
448 extern bool runtime_precisestack
;
452 * you need super-gopher-guru privilege
455 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
456 #define nil ((void*)0)
457 #define USED(v) ((void) v)
458 #define ROUND(x, n) (((x)+(n)-1)&~((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
460 byte
* runtime_startup_random_data
;
461 uint32 runtime_startup_random_data_len
;
462 void runtime_get_random_data(byte
**, int32
*);
465 // hashinit wants this many random bytes
468 void runtime_hashinit(void);
470 void runtime_traceback(void);
471 void runtime_tracebackothers(G
*);
474 // The maximum number of frames we print for a traceback
475 TracebackMaxFrames
= 100,
481 extern uintptr runtime_zerobase
;
482 extern G
** runtime_allg
;
483 extern uintptr runtime_allglen
;
484 extern G
* runtime_lastg
;
485 extern M
* runtime_allm
;
486 extern P
** runtime_allp
;
487 extern int32 runtime_gomaxprocs
;
488 extern uint32 runtime_needextram
;
489 extern uint32 runtime_panicking
;
490 extern int8
* runtime_goos
;
491 extern int32 runtime_ncpu
;
492 extern void (*runtime_sysargs
)(int32
, uint8
**);
493 extern DebugVars runtime_debug
;
496 * common functions and data
498 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
499 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
500 intgo
runtime_findnull(const byte
*);
501 intgo
runtime_findnullw(const uint16
*);
502 void runtime_dump(byte
*, int32
);
505 * very low level c-called
507 void runtime_gogo(G
*);
508 struct __go_func_type
;
509 void runtime_args(int32
, byte
**);
510 void runtime_osinit();
511 void runtime_goargs(void);
512 void runtime_goenvs(void);
513 void runtime_goenvs_unix(void);
514 void runtime_throw(const char*) __attribute__ ((noreturn
));
515 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
516 void runtime_prints(const char*);
517 void runtime_printf(const char*, ...);
518 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
519 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
520 void* runtime_mal(uintptr
);
521 String
runtime_gostring(const byte
*);
522 String
runtime_gostringnocopy(const byte
*);
523 void runtime_schedinit(void);
524 void runtime_initsig(void);
525 void runtime_sigenable(uint32 sig
);
526 void runtime_sigdisable(uint32 sig
);
527 int32
runtime_gotraceback(bool *crash
);
528 void runtime_goroutineheader(G
*);
529 void runtime_printtrace(Location
*, int32
, bool);
530 #define runtime_open(p, f, m) open((p), (f), (m))
531 #define runtime_read(d, v, n) read((d), (v), (n))
532 #define runtime_write(d, v, n) write((d), (v), (n))
533 #define runtime_close(d) close(d)
534 void runtime_ready(G
*);
535 const byte
* runtime_getenv(const char*);
536 int32
runtime_atoi(const byte
*);
537 void* runtime_mstart(void*);
538 G
* runtime_malg(int32
, byte
**, size_t*);
539 void runtime_mpreinit(M
*);
540 void runtime_minit(void);
541 void runtime_unminit(void);
542 void runtime_needm(void);
543 void runtime_dropm(void);
544 void runtime_signalstack(byte
*, int32
);
545 MCache
* runtime_allocmcache(void);
546 void runtime_freemcache(MCache
*);
547 void runtime_mallocinit(void);
548 void runtime_mprofinit(void);
549 #define runtime_malloc(s) __go_alloc(s)
550 #define runtime_free(p) __go_free(p)
551 #define runtime_getcallersp(p) __builtin_frame_address(1)
552 int32
runtime_mcount(void);
553 int32
runtime_gcount(void);
554 void runtime_mcall(void(*)(G
*));
555 uint32
runtime_fastrand1(void);
556 int32
runtime_timediv(int64
, int32
, int32
*);
559 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
560 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
561 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
562 // Don't confuse with XADD x86 instruction,
563 // this one is actually 'addx', that is, add-and-fetch.
564 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
565 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
566 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
567 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
568 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
569 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
570 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
571 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
572 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
573 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
574 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
576 void runtime_setmg(M
*, G
*);
577 void runtime_newextram(void);
578 #define runtime_exit(s) exit(s)
579 #define runtime_breakpoint() __builtin_trap()
580 void runtime_gosched(void);
581 void runtime_gosched0(G
*);
582 void runtime_schedtrace(bool);
583 void runtime_park(bool(*)(G
*, void*), void*, const char*);
584 void runtime_parkunlock(Lock
*, const char*);
585 void runtime_tsleep(int64
, const char*);
586 M
* runtime_newm(void);
587 void runtime_goexit(void);
588 void runtime_entersyscall(void) __asm__ (GOSYM_PREFIX
"syscall.Entersyscall");
589 void runtime_entersyscallblock(void);
590 void runtime_exitsyscall(void) __asm__ (GOSYM_PREFIX
"syscall.Exitsyscall");
591 G
* __go_go(void (*pfn
)(void*), void*);
593 bool __go_sigsend(int32 sig
);
594 int32
runtime_callers(int32
, Location
*, int32
);
595 int64
runtime_nanotime(void);
596 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
597 void runtime_startpanic(void);
598 void runtime_freezetheworld(void);
599 void runtime_unwindstack(G
*, byte
*);
600 void runtime_sigprof();
601 void runtime_resetcpuprofiler(int32
);
602 void runtime_setcpuprofilerate(void(*)(uintptr
*, int32
), int32
);
603 void runtime_usleep(uint32
);
604 int64
runtime_cputicks(void);
605 int64
runtime_tickspersecond(void);
606 void runtime_blockevent(int64
, int32
);
607 extern int64 runtime_blockprofilerate
;
608 void runtime_addtimer(Timer
*);
609 bool runtime_deltimer(Timer
*);
610 G
* runtime_netpoll(bool);
611 void runtime_netpollinit(void);
612 int32
runtime_netpollopen(uintptr
, PollDesc
*);
613 int32
runtime_netpollclose(uintptr
);
614 void runtime_netpollready(G
**, PollDesc
*, int32
);
615 uintptr
runtime_netpollfd(PollDesc
*);
616 void runtime_netpollarm(uintptr
, int32
);
617 void runtime_crash(void);
618 void runtime_parsedebugvars(void);
620 void* runtime_funcdata(Func
*, int32
);
622 void runtime_stoptheworld(void);
623 void runtime_starttheworld(void);
624 extern uint32 runtime_worldsema
;
627 * mutual exclusion locks. in the uncontended case,
628 * as fast as spin locks (just a few user-level instructions),
629 * but on the contention path they sleep in the kernel.
630 * a zeroed Lock is unlocked (no need to initialize each lock).
632 void runtime_lock(Lock
*);
633 void runtime_unlock(Lock
*);
636 * sleep and wakeup on one-time events.
637 * before any calls to notesleep or notewakeup,
638 * must call noteclear to initialize the Note.
639 * then, exactly one thread can call notesleep
640 * and exactly one thread can call notewakeup (once).
641 * once notewakeup has been called, the notesleep
642 * will return. future notesleep will return immediately.
643 * subsequent noteclear must be called only after
644 * previous notesleep has returned, e.g. it's disallowed
645 * to call noteclear straight after notewakeup.
647 * notetsleep is like notesleep but wakes up after
648 * a given number of nanoseconds even if the event
649 * has not yet happened. if a goroutine uses notetsleep to
650 * wake up early, it must wait to call noteclear until it
651 * can be sure that no other goroutine is calling
654 * notesleep/notetsleep are generally called on g0,
655 * notetsleepg is similar to notetsleep but is called on user g.
657 void runtime_noteclear(Note
*);
658 void runtime_notesleep(Note
*);
659 void runtime_notewakeup(Note
*);
660 bool runtime_notetsleep(Note
*, int64
); // false - timeout
661 bool runtime_notetsleepg(Note
*, int64
); // false - timeout
664 * low-level synchronization for implementing the above
666 uintptr
runtime_semacreate(void);
667 int32
runtime_semasleep(int64
);
668 void runtime_semawakeup(M
*);
670 void runtime_futexsleep(uint32
*, uint32
, int64
);
671 void runtime_futexwakeup(uint32
*, uint32
);
675 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
676 * The stack does not keep pointers to nodes,
677 * so they can be garbage collected if there are no other pointers to nodes.
679 void runtime_lfstackpush(uint64
*head
, LFNode
*node
)
680 __asm__ (GOSYM_PREFIX
"runtime.lfstackpush");
681 LFNode
* runtime_lfstackpop(uint64
*head
);
684 * Parallel for over [0, n).
685 * body() is executed for each iteration.
686 * nthr - total number of worker threads.
687 * ctx - arbitrary user context.
688 * if wait=true, threads return from parfor() when all work is done;
689 * otherwise, threads can return while other threads are still finishing processing.
691 ParFor
* runtime_parforalloc(uint32 nthrmax
);
692 void runtime_parforsetup(ParFor
*desc
, uint32 nthr
, uint32 n
, void *ctx
, bool wait
, void (*body
)(ParFor
*, uint32
));
693 void runtime_parfordo(ParFor
*desc
) __asm__ (GOSYM_PREFIX
"runtime.parfordo");
698 #define runtime_mmap mmap
699 #define runtime_munmap munmap
700 #define runtime_madvise madvise
701 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
702 #define runtime_getcallerpc(p) __builtin_return_address(0)
705 void __wrap_rtems_task_variable_add(void **);
709 * Names generated by gccgo.
711 #define runtime_printbool __go_print_bool
712 #define runtime_printfloat __go_print_double
713 #define runtime_printint __go_print_int64
714 #define runtime_printiface __go_print_interface
715 #define runtime_printeface __go_print_empty_interface
716 #define runtime_printstring __go_print_string
717 #define runtime_printpointer __go_print_pointer
718 #define runtime_printuint __go_print_uint64
719 #define runtime_printslice __go_print_slice
720 #define runtime_printcomplex __go_print_complex
725 void runtime_printbool(_Bool
);
726 void runtime_printbyte(int8
);
727 void runtime_printfloat(double);
728 void runtime_printint(int64
);
729 void runtime_printiface(Iface
);
730 void runtime_printeface(Eface
);
731 void runtime_printstring(String
);
732 void runtime_printpc(void*);
733 void runtime_printpointer(void*);
734 void runtime_printuint(uint64
);
735 void runtime_printhex(uint64
);
736 void runtime_printslice(Slice
);
737 void runtime_printcomplex(complex double);
738 void reflect_call(const struct __go_func_type
*, FuncVal
*, _Bool
, _Bool
,
740 __asm__ (GOSYM_PREFIX
"reflect.call");
741 #define runtime_panic __go_panic
744 * runtime c-called (but written in Go)
746 void runtime_printany(Eface
)
747 __asm__ (GOSYM_PREFIX
"runtime.Printany");
748 void runtime_newTypeAssertionError(const String
*, const String
*, const String
*, const String
*, Eface
*)
749 __asm__ (GOSYM_PREFIX
"runtime.NewTypeAssertionError");
750 void runtime_newErrorString(String
, Eface
*)
751 __asm__ (GOSYM_PREFIX
"runtime.NewErrorString");
752 void runtime_newErrorCString(const char*, Eface
*)
753 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
756 * wrapped for go users
758 void runtime_semacquire(uint32
volatile *, bool);
759 void runtime_semrelease(uint32
volatile *);
760 int32
runtime_gomaxprocsfunc(int32 n
);
761 void runtime_procyield(uint32
);
762 void runtime_osyield(void);
763 void runtime_lockOSThread(void);
764 void runtime_unlockOSThread(void);
766 bool runtime_showframe(String
, bool);
767 void runtime_printcreatedby(G
*);
769 uintptr
runtime_memlimit(void);
771 #define ISNAN(f) __builtin_isnan(f)
778 #define runtime_setitimer setitimer
780 void runtime_check(void);
782 // A list of global variables that the garbage collector must scan.
784 struct root_list
*next
;
791 void __go_register_gc_roots(struct root_list
*);
793 // Size of stack space allocated using Go's allocator.
794 // This will be 0 when using split stacks, as in that case
795 // the stacks are allocated by the splitstack library.
796 extern uintptr runtime_stacks_sys
;
798 struct backtrace_state
;
799 extern struct backtrace_state
*__go_get_backtrace_state(void);
800 extern _Bool
__go_file_line(uintptr
, String
*, String
*, intgo
*);
801 extern byte
* runtime_progname();
802 extern void runtime_main(void*);
803 extern uint32 runtime_in_callers
;
805 int32
getproccount(void);
807 #define PREFETCH(p) __builtin_prefetch(p)
809 void __go_set_closure(void*);
810 void* __go_get_closure(void);
812 bool runtime_gcwaiting(void);
813 void runtime_badsignal(int);
814 Defer
* runtime_newdefer(void);
815 void runtime_freedefer(Defer
*);