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 struct Lock Lock
;
60 typedef struct Note Note
;
61 typedef struct String String
;
62 typedef struct FuncVal FuncVal
;
63 typedef struct SigTab SigTab
;
64 typedef struct MCache MCache
;
65 typedef struct FixAlloc FixAlloc
;
66 typedef struct Hchan Hchan
;
67 typedef struct Timers Timers
;
68 typedef struct Timer Timer
;
69 typedef struct GCStats GCStats
;
70 typedef struct LFNode LFNode
;
71 typedef struct ParFor ParFor
;
72 typedef struct ParForThread ParForThread
;
73 typedef struct CgoMal CgoMal
;
74 typedef struct PollDesc PollDesc
;
75 typedef struct DebugVars DebugVars
;
77 typedef struct __go_open_array Slice
;
78 typedef struct __go_interface Iface
;
79 typedef struct __go_empty_interface Eface
;
80 typedef struct __go_type_descriptor Type
;
81 typedef struct __go_defer_stack Defer
;
82 typedef struct __go_panic_stack Panic
;
84 typedef struct __go_ptr_type PtrType
;
85 typedef struct __go_func_type FuncType
;
86 typedef struct __go_interface_type InterfaceType
;
87 typedef struct __go_map_type MapType
;
88 typedef struct __go_channel_type ChanType
;
90 typedef struct Traceback Traceback
;
92 typedef struct Location Location
;
95 * Per-CPU declaration.
97 extern M
* runtime_m(void);
98 extern G
* runtime_g(void);
110 // If you add to this list, add to the list
111 // of "okay during garbage collection" status
118 Gmoribund_unused
, // currently unused, but hardcoded in gdb scripts
137 PtrSize
= sizeof(void*),
141 // Per-M stack segment cache size.
143 // Global <-> per-M stack segment cache transfer batch size.
144 StackCacheBatch
= 16,
151 // Futex-based impl treats it as uint32 key,
152 // while sema-based impl as M* waitm.
153 // Used to be a union, but unions break precise GC.
158 // Futex-based impl treats it as uint32 key,
159 // while sema-based impl as M* waitm.
160 // Used to be a union, but unions break precise GC.
171 // variable-size, fn-specific data here
175 // the struct must consist of only uint64's,
176 // because it is casted to uint64[].
184 // A location in the program, used for backtraces.
195 void* closure
; // Closure value.
198 void* exception
; // current exception being thrown
199 bool is_foreign
; // whether current exception from other language
200 void *gcstack
; // if status==Gsyscall, gcstack = stackbase to use during gc
201 uintptr gcstack_size
;
202 void* gcnext_segment
;
206 byte
* entry
; // initial function
207 G
* alllink
; // on allg
208 void* param
; // passed parameter on wakeup
209 bool fromgogo
; // reached from gogo
211 uint32 selgen
; // valid sudog pointer
213 const char* waitreason
; // if status==Gwaiting
216 bool issystem
; // do not output in stack dump
217 bool isbackground
; // ignore in deadlock detector
218 M
* m
; // for debuggers, but offset not hard-coded
223 // DeferChunk* dchunk;
224 // DeferChunk* dchunknext;
228 uintptr gopc
; // pc of go statement that created this goroutine
233 Traceback
* traceback
;
236 void* stack_context
[10];
241 G
* g0
; // goroutine with scheduling stack
242 G
* gsignal
; // signal-handling G
244 size_t gsignalstacksize
;
245 void (*mstartfn
)(void);
246 G
* curg
; // current running goroutine
247 G
* caughtsig
; // goroutine running during fatal signal
248 P
* p
; // attached P for executing Go code (nil if not executing Go code)
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.
279 void (*waitunlockf
)(Lock
*);
282 uintptr settype_buf
[1024];
283 uintptr settype_bufsize
;
293 uint32 status
; // one of Pidle/Prunning/...
295 uint32 schedtick
; // incremented on every scheduler call
296 uint32 syscalltick
; // incremented on every system call
297 M
* m
; // back-link to associated M (nil if idle)
300 // Queue of runnable goroutines.
306 // Available G's (status == Gdead)
313 // The m->locked word holds two pieces of state counting active calls to LockOSThread/lockOSThread.
314 // The low bit (LockExternal) is a boolean reporting whether any LockOSThread call is active.
315 // External locks are not recursive; a second lock is silently ignored.
316 // The upper bits of m->lockedcount record the nesting depth of calls to lockOSThread
317 // (counting up by LockInternal), popped by unlockOSThread (counting down by LockInternal).
318 // Internal locks can be recursive. For instance, a lock for cgo can occur while the main
319 // goroutine is holding the lock during the initialization phase.
333 SigNotify
= 1<<0, // let signal.Notify have signal, even if from kernel
334 SigKill
= 1<<1, // if signal.Notify doesn't take it, exit quietly
335 SigThrow
= 1<<2, // if signal.Notify doesn't take it, exit loudly
336 SigPanic
= 1<<3, // if the signal is from the kernel, panic
337 SigDefault
= 1<<4, // if the signal isn't explicitly requested, don't monitor it
338 SigHandling
= 1<<5, // our signal handler is registered
339 SigIgnored
= 1<<6, // the signal was ignored before we registered for it
342 // Layout of in-memory per-function information prepared by linker
343 // See http://golang.org/s/go12symtab.
344 // Keep in sync with linker and with ../../libmach/sym.c
345 // and with package debug/gosym.
349 uintptr entry
; // entry pc
374 // Package time knows the layout of this structure.
375 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
378 int32 i
; // heap index
380 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
381 // each time calling f(now, arg) in the timer goroutine, so f must be
382 // a well-behaved function and not block.
389 // Lock-free stack node.
396 // Parallel for descriptor.
399 void (*body
)(ParFor
*, uint32
); // executed for each element
400 uint32 done
; // number of idle threads
401 uint32 nthr
; // total number of threads
402 uint32 nthrmax
; // maximum number of threads
403 uint32 thrseq
; // thread id sequencer
404 uint32 cnt
; // iteration space [0, cnt)
405 void *ctx
; // arbitrary user context
406 bool wait
; // if true, wait while all threads finish processing,
407 // otherwise parfor may return while other threads are still working
408 ParForThread
*thr
; // array of thread descriptors
409 uint32 pad
; // to align ParForThread.pos for 64-bit atomic operations
418 // Track memory allocated by code not written in Go during a cgo call,
419 // so that the garbage collector can see them.
426 // Holds variables parsed from GODEBUG env var.
434 extern bool runtime_precisestack
;
438 * you need super-gopher-guru privilege
441 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
442 #define nil ((void*)0)
443 #define USED(v) ((void) v)
444 #define ROUND(x, n) (((x)+(n)-1)&~((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
446 byte
* runtime_startup_random_data
;
447 uint32 runtime_startup_random_data_len
;
448 void runtime_get_random_data(byte
**, int32
*);
451 // hashinit wants this many random bytes
454 void runtime_hashinit(void);
456 void runtime_traceback(void);
457 void runtime_tracebackothers(G
*);
462 extern uintptr runtime_zerobase
;
463 extern G
* runtime_allg
;
464 extern G
* runtime_lastg
;
465 extern M
* runtime_allm
;
466 extern P
** runtime_allp
;
467 extern int32 runtime_gomaxprocs
;
468 extern uint32 runtime_needextram
;
469 extern uint32 runtime_panicking
;
470 extern int8
* runtime_goos
;
471 extern int32 runtime_ncpu
;
472 extern void (*runtime_sysargs
)(int32
, uint8
**);
473 extern DebugVars runtime_debug
;
476 * common functions and data
478 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
479 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
480 intgo
runtime_findnull(const byte
*);
481 intgo
runtime_findnullw(const uint16
*);
482 void runtime_dump(byte
*, int32
);
485 * very low level c-called
487 void runtime_gogo(G
*);
488 struct __go_func_type
;
489 void runtime_args(int32
, byte
**);
490 void runtime_osinit();
491 void runtime_goargs(void);
492 void runtime_goenvs(void);
493 void runtime_goenvs_unix(void);
494 void runtime_throw(const char*) __attribute__ ((noreturn
));
495 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
496 void runtime_prints(const char*);
497 void runtime_printf(const char*, ...);
498 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
499 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
500 void* runtime_mal(uintptr
);
501 String
runtime_gostring(const byte
*);
502 String
runtime_gostringnocopy(const byte
*);
503 void runtime_schedinit(void);
504 void runtime_initsig(void);
505 void runtime_sigenable(uint32 sig
);
506 void runtime_sigdisable(uint32 sig
);
507 int32
runtime_gotraceback(bool *crash
);
508 void runtime_goroutineheader(G
*);
509 void runtime_printtrace(Location
*, int32
, bool);
510 #define runtime_open(p, f, m) open((p), (f), (m))
511 #define runtime_read(d, v, n) read((d), (v), (n))
512 #define runtime_write(d, v, n) write((d), (v), (n))
513 #define runtime_close(d) close(d)
514 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
515 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
516 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
517 // Don't confuse with XADD x86 instruction,
518 // this one is actually 'addx', that is, add-and-fetch.
519 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
520 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
521 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
522 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
523 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
524 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
525 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
526 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
527 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
528 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
529 void runtime_ready(G
*);
530 const byte
* runtime_getenv(const char*);
531 int32
runtime_atoi(const byte
*);
532 void* runtime_mstart(void*);
533 G
* runtime_malg(int32
, byte
**, size_t*);
534 void runtime_mpreinit(M
*);
535 void runtime_minit(void);
536 void runtime_unminit(void);
537 void runtime_needm(void);
538 void runtime_dropm(void);
539 void runtime_signalstack(byte
*, int32
);
540 MCache
* runtime_allocmcache(void);
541 void runtime_freemcache(MCache
*);
542 void runtime_mallocinit(void);
543 void runtime_mprofinit(void);
544 #define runtime_malloc(s) __go_alloc(s)
545 #define runtime_free(p) __go_free(p)
546 bool runtime_addfinalizer(void*, FuncVal
*fn
, const struct __go_func_type
*, const struct __go_ptr_type
*);
547 #define runtime_getcallersp(p) __builtin_frame_address(1)
548 int32
runtime_mcount(void);
549 int32
runtime_gcount(void);
550 void runtime_mcall(void(*)(G
*));
551 uint32
runtime_fastrand1(void);
552 int32
runtime_timediv(int64
, int32
, int32
*);
554 void runtime_setmg(M
*, G
*);
555 void runtime_newextram(void);
556 #define runtime_exit(s) exit(s)
557 #define runtime_breakpoint() __builtin_trap()
558 void runtime_gosched(void);
559 void runtime_gosched0(G
*);
560 void runtime_schedtrace(bool);
561 void runtime_park(void(*)(Lock
*), Lock
*, const char*);
562 void runtime_tsleep(int64
, const char*);
563 M
* runtime_newm(void);
564 void runtime_goexit(void);
565 void runtime_entersyscall(void) __asm__ (GOSYM_PREFIX
"syscall.Entersyscall");
566 void runtime_entersyscallblock(void);
567 void runtime_exitsyscall(void) __asm__ (GOSYM_PREFIX
"syscall.Exitsyscall");
568 G
* __go_go(void (*pfn
)(void*), void*);
570 bool __go_sigsend(int32 sig
);
571 int32
runtime_callers(int32
, Location
*, int32
);
572 int64
runtime_nanotime(void);
573 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
574 void runtime_startpanic(void);
575 void runtime_freezetheworld(void);
576 void runtime_unwindstack(G
*, byte
*);
577 void runtime_sigprof();
578 void runtime_resetcpuprofiler(int32
);
579 void runtime_setcpuprofilerate(void(*)(uintptr
*, int32
), int32
);
580 void runtime_usleep(uint32
);
581 int64
runtime_cputicks(void);
582 int64
runtime_tickspersecond(void);
583 void runtime_blockevent(int64
, int32
);
584 extern int64 runtime_blockprofilerate
;
585 void runtime_addtimer(Timer
*);
586 bool runtime_deltimer(Timer
*);
587 G
* runtime_netpoll(bool);
588 void runtime_netpollinit(void);
589 int32
runtime_netpollopen(uintptr
, PollDesc
*);
590 int32
runtime_netpollclose(uintptr
);
591 void runtime_netpollready(G
**, PollDesc
*, int32
);
592 uintptr
runtime_netpollfd(PollDesc
*);
593 void runtime_crash(void);
594 void runtime_parsedebugvars(void);
596 void* runtime_funcdata(Func
*, int32
);
598 void runtime_stoptheworld(void);
599 void runtime_starttheworld(void);
600 extern uint32 runtime_worldsema
;
603 * mutual exclusion locks. in the uncontended case,
604 * as fast as spin locks (just a few user-level instructions),
605 * but on the contention path they sleep in the kernel.
606 * a zeroed Lock is unlocked (no need to initialize each lock).
608 void runtime_lock(Lock
*);
609 void runtime_unlock(Lock
*);
612 * sleep and wakeup on one-time events.
613 * before any calls to notesleep or notewakeup,
614 * must call noteclear to initialize the Note.
615 * then, exactly one thread can call notesleep
616 * and exactly one thread can call notewakeup (once).
617 * once notewakeup has been called, the notesleep
618 * will return. future notesleep will return immediately.
619 * subsequent noteclear must be called only after
620 * previous notesleep has returned, e.g. it's disallowed
621 * to call noteclear straight after notewakeup.
623 * notetsleep is like notesleep but wakes up after
624 * a given number of nanoseconds even if the event
625 * has not yet happened. if a goroutine uses notetsleep to
626 * wake up early, it must wait to call noteclear until it
627 * can be sure that no other goroutine is calling
630 * notesleep/notetsleep are generally called on g0,
631 * notetsleepg is similar to notetsleep but is called on user g.
633 void runtime_noteclear(Note
*);
634 void runtime_notesleep(Note
*);
635 void runtime_notewakeup(Note
*);
636 bool runtime_notetsleep(Note
*, int64
); // false - timeout
637 bool runtime_notetsleepg(Note
*, int64
); // false - timeout
640 * low-level synchronization for implementing the above
642 uintptr
runtime_semacreate(void);
643 int32
runtime_semasleep(int64
);
644 void runtime_semawakeup(M
*);
646 void runtime_futexsleep(uint32
*, uint32
, int64
);
647 void runtime_futexwakeup(uint32
*, uint32
);
651 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
652 * The stack does not keep pointers to nodes,
653 * so they can be garbage collected if there are no other pointers to nodes.
655 void runtime_lfstackpush(uint64
*head
, LFNode
*node
)
656 __asm__ (GOSYM_PREFIX
"runtime.lfstackpush");
657 LFNode
* runtime_lfstackpop(uint64
*head
);
660 * Parallel for over [0, n).
661 * body() is executed for each iteration.
662 * nthr - total number of worker threads.
663 * ctx - arbitrary user context.
664 * if wait=true, threads return from parfor() when all work is done;
665 * otherwise, threads can return while other threads are still finishing processing.
667 ParFor
* runtime_parforalloc(uint32 nthrmax
);
668 void runtime_parforsetup(ParFor
*desc
, uint32 nthr
, uint32 n
, void *ctx
, bool wait
, void (*body
)(ParFor
*, uint32
));
669 void runtime_parfordo(ParFor
*desc
) __asm__ (GOSYM_PREFIX
"runtime.parfordo");
674 #define runtime_mmap mmap
675 #define runtime_munmap munmap
676 #define runtime_madvise madvise
677 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
678 #define runtime_getcallerpc(p) __builtin_return_address(0)
681 void __wrap_rtems_task_variable_add(void **);
685 * Names generated by gccgo.
687 #define runtime_printbool __go_print_bool
688 #define runtime_printfloat __go_print_double
689 #define runtime_printint __go_print_int64
690 #define runtime_printiface __go_print_interface
691 #define runtime_printeface __go_print_empty_interface
692 #define runtime_printstring __go_print_string
693 #define runtime_printpointer __go_print_pointer
694 #define runtime_printuint __go_print_uint64
695 #define runtime_printslice __go_print_slice
696 #define runtime_printcomplex __go_print_complex
701 void runtime_printbool(_Bool
);
702 void runtime_printbyte(int8
);
703 void runtime_printfloat(double);
704 void runtime_printint(int64
);
705 void runtime_printiface(Iface
);
706 void runtime_printeface(Eface
);
707 void runtime_printstring(String
);
708 void runtime_printpc(void*);
709 void runtime_printpointer(void*);
710 void runtime_printuint(uint64
);
711 void runtime_printhex(uint64
);
712 void runtime_printslice(Slice
);
713 void runtime_printcomplex(__complex
double);
714 void reflect_call(const struct __go_func_type
*, FuncVal
*, _Bool
, _Bool
,
716 __asm__ (GOSYM_PREFIX
"reflect.call");
717 #define runtime_panic __go_panic
720 * runtime c-called (but written in Go)
722 void runtime_printany(Eface
)
723 __asm__ (GOSYM_PREFIX
"runtime.Printany");
724 void runtime_newTypeAssertionError(const String
*, const String
*, const String
*, const String
*, Eface
*)
725 __asm__ (GOSYM_PREFIX
"runtime.NewTypeAssertionError");
726 void runtime_newErrorString(String
, Eface
*)
727 __asm__ (GOSYM_PREFIX
"runtime.NewErrorString");
728 void runtime_newErrorCString(const char*, Eface
*)
729 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
732 * wrapped for go users
734 void runtime_semacquire(uint32
volatile *, bool);
735 void runtime_semrelease(uint32
volatile *);
736 int32
runtime_gomaxprocsfunc(int32 n
);
737 void runtime_procyield(uint32
);
738 void runtime_osyield(void);
739 void runtime_lockOSThread(void);
740 void runtime_unlockOSThread(void);
742 bool runtime_showframe(String
, bool);
743 void runtime_printcreatedby(G
*);
745 uintptr
runtime_memlimit(void);
747 #define ISNAN(f) __builtin_isnan(f)
754 #define runtime_setitimer setitimer
756 void runtime_check(void);
758 // A list of global variables that the garbage collector must scan.
760 struct root_list
*next
;
767 void __go_register_gc_roots(struct root_list
*);
769 // Size of stack space allocated using Go's allocator.
770 // This will be 0 when using split stacks, as in that case
771 // the stacks are allocated by the splitstack library.
772 extern uintptr runtime_stacks_sys
;
774 struct backtrace_state
;
775 extern struct backtrace_state
*__go_get_backtrace_state(void);
776 extern _Bool
__go_file_line(uintptr
, String
*, String
*, intgo
*);
777 extern byte
* runtime_progname();
778 extern void runtime_main(void*);
779 extern uint32 runtime_in_callers
;
781 int32
getproccount(void);
783 #define PREFETCH(p) __builtin_prefetch(p)
785 void __go_set_closure(void*);
786 void* __go_get_closure(void);
788 bool runtime_gcwaiting(void);
789 void runtime_badsignal(int);