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	struct	Lock		Lock;
58 typedef	struct	M		M;
59 typedef	struct	P		P;
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;
76 
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;
83 
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;
89 
90 typedef struct  Traceback	Traceback;
91 
92 typedef struct	Location	Location;
93 
94 /*
95  * Per-CPU declaration.
96  */
97 extern M*	runtime_m(void);
98 extern G*	runtime_g(void);
99 
100 extern M	runtime_m0;
101 extern G	runtime_g0;
102 
103 /*
104  * defined constants
105  */
106 enum
107 {
108 	// G status
109 	//
110 	// If you add to this list, add to the list
111 	// of "okay during garbage collection" status
112 	// in mgc0.c too.
113 	Gidle,
114 	Grunnable,
115 	Grunning,
116 	Gsyscall,
117 	Gwaiting,
118 	Gmoribund_unused,  // currently unused, but hardcoded in gdb scripts
119 	Gdead,
120 };
121 enum
122 {
123 	// P status
124 	Pidle,
125 	Prunning,
126 	Psyscall,
127 	Pgcstop,
128 	Pdead,
129 };
130 enum
131 {
132 	true	= 1,
133 	false	= 0,
134 };
135 enum
136 {
137 	PtrSize = sizeof(void*),
138 };
139 enum
140 {
141 	// Per-M stack segment cache size.
142 	StackCacheSize = 32,
143 	// Global <-> per-M stack segment cache transfer batch size.
144 	StackCacheBatch = 16,
145 };
146 /*
147  * structures
148  */
149 struct	Lock
150 {
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.
154 	uintptr	key;
155 };
156 struct	Note
157 {
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.
161 	uintptr	key;
162 };
163 struct String
164 {
165 	const byte*	str;
166 	intgo		len;
167 };
168 struct FuncVal
169 {
170 	void	(*fn)(void);
171 	// variable-size, fn-specific data here
172 };
173 struct	GCStats
174 {
175 	// the struct must consist of only uint64's,
176 	// because it is casted to uint64[].
177 	uint64	nhandoff;
178 	uint64	nhandoffcnt;
179 	uint64	nprocyield;
180 	uint64	nosyield;
181 	uint64	nsleep;
182 };
183 
184 // A location in the program, used for backtraces.
185 struct	Location
186 {
187 	uintptr	pc;
188 	String	filename;
189 	String	function;
190 	intgo	lineno;
191 };
192 
193 struct	G
194 {
195 	void*	closure;	// Closure value.
196 	Defer*	defer;
197 	Panic*	panic;
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;
203 	void*	gcnext_sp;
204 	void*	gcinitial_sp;
205 	ucontext_t gcregs;
206 	byte*	entry;		// initial function
207 	G*	alllink;	// on allg
208 	void*	param;		// passed parameter on wakeup
209 	bool	fromgogo;	// reached from gogo
210 	int16	status;
211 	uint32	selgen;		// valid sudog pointer
212 	int64	goid;
213 	const char*	waitreason;	// if status==Gwaiting
214 	G*	schedlink;
215 	bool	ispanic;
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
219 	M*	lockedm;
220 	int32	sig;
221 	int32	writenbuf;
222 	byte*	writebuf;
223 	// DeferChunk*	dchunk;
224 	// DeferChunk*	dchunknext;
225 	uintptr	sigcode0;
226 	uintptr	sigcode1;
227 	// uintptr	sigpc;
228 	uintptr	gopc;	// pc of go statement that created this goroutine
229 
230 	int32	ncgo;
231 	CgoMal*	cgomal;
232 
233 	Traceback* traceback;
234 
235 	ucontext_t	context;
236 	void*		stack_context[10];
237 };
238 
239 struct	M
240 {
241 	G*	g0;		// goroutine with scheduling stack
242 	G*	gsignal;	// signal-handling G
243 	byte*	gsignalstack;
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)
249 	P*	nextp;
250 	int32	id;
251 	int32	mallocing;
252 	int32	throwing;
253 	int32	gcing;
254 	int32	locks;
255 	int32	dying;
256 	int32	profilehz;
257 	int32	helpgc;
258 	bool	spinning;
259 	uint32	fastrand;
260 	uint64	ncgocall;	// number of cgo calls in total
261 	int32	ncgo;		// number of cgo calls currently in progress
262 	CgoMal*	cgomal;
263 	Note	park;
264 	M*	alllink;	// on allm
265 	M*	schedlink;
266 	MCache	*mcache;
267 	G*	lockedg;
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;
273 	uint32	waitsemalock;
274 	GCStats	gcstats;
275 	bool	racecall;
276 	bool	needextram;
277 	bool	dropextram;	// for gccgo: drop after call is done.
278 	void*	racepc;
279 	void	(*waitunlockf)(Lock*);
280 	void*	waitlock;
281 
282 	uintptr	settype_buf[1024];
283 	uintptr	settype_bufsize;
284 
285 	uintptr	end[];
286 };
287 
288 struct P
289 {
290 	Lock;
291 
292 	int32	id;
293 	uint32	status;		// one of Pidle/Prunning/...
294 	P*	link;
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)
298 	MCache*	mcache;
299 
300 	// Queue of runnable goroutines.
301 	G**	runq;
302 	int32	runqhead;
303 	int32	runqtail;
304 	int32	runqsize;
305 
306 	// Available G's (status == Gdead)
307 	G*	gfree;
308 	int32	gfreecnt;
309 
310 	byte	pad[64];
311 };
312 
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.
320 enum
321 {
322 	LockExternal = 1,
323 	LockInternal = 2,
324 };
325 
326 struct	SigTab
327 {
328 	int32	sig;
329 	int32	flags;
330 };
331 enum
332 {
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
340 };
341 
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.
346 struct	Func
347 {
348 	String	name;
349 	uintptr	entry;	// entry pc
350 };
351 
352 #ifdef GOOS_windows
353 enum {
354    Windows = 1
355 };
356 #else
357 enum {
358    Windows = 0
359 };
360 #endif
361 
362 struct	Timers
363 {
364 	Lock;
365 	G	*timerproc;
366 	bool		sleeping;
367 	bool		rescheduling;
368 	Note	waitnote;
369 	Timer	**t;
370 	int32	len;
371 	int32	cap;
372 };
373 
374 // Package time knows the layout of this structure.
375 // If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
376 struct	Timer
377 {
378 	int32	i;	// heap index
379 
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.
383 	int64	when;
384 	int64	period;
385 	FuncVal	*fv;
386 	Eface	arg;
387 };
388 
389 // Lock-free stack node.
390 struct LFNode
391 {
392 	LFNode	*next;
393 	uintptr	pushcnt;
394 };
395 
396 // Parallel for descriptor.
397 struct ParFor
398 {
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
410 	// stats
411 	uint64 nsteal;
412 	uint64 nstealcnt;
413 	uint64 nprocyield;
414 	uint64 nosyield;
415 	uint64 nsleep;
416 };
417 
418 // Track memory allocated by code not written in Go during a cgo call,
419 // so that the garbage collector can see them.
420 struct CgoMal
421 {
422 	CgoMal	*next;
423 	void	*alloc;
424 };
425 
426 // Holds variables parsed from GODEBUG env var.
427 struct DebugVars
428 {
429 	int32	gctrace;
430 	int32	schedtrace;
431 	int32	scheddetail;
432 };
433 
434 extern bool runtime_precisestack;
435 
436 /*
437  * defined macros
438  *    you need super-gopher-guru privilege
439  *    to add this list.
440  */
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 */
445 
446 byte*	runtime_startup_random_data;
447 uint32	runtime_startup_random_data_len;
448 void	runtime_get_random_data(byte**, int32*);
449 
450 enum {
451 	// hashinit wants this many random bytes
452 	HashRandomBytes = 32
453 };
454 void	runtime_hashinit(void);
455 
456 void	runtime_traceback(void);
457 void	runtime_tracebackothers(G*);
458 
459 /*
460  * external data
461  */
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;
474 
475 /*
476  * common functions and data
477  */
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);
483 
484 /*
485  * very low level c-called
486  */
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*);
553 
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*);
569 void	siginit(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);
595 void	_rt0_go(void);
596 void*	runtime_funcdata(Func*, int32);
597 
598 void	runtime_stoptheworld(void);
599 void	runtime_starttheworld(void);
600 extern uint32 runtime_worldsema;
601 
602 /*
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).
607  */
608 void	runtime_lock(Lock*);
609 void	runtime_unlock(Lock*);
610 
611 /*
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.
622  *
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
628  * notewakeup.
629  *
630  * notesleep/notetsleep are generally called on g0,
631  * notetsleepg is similar to notetsleep but is called on user g.
632  */
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
638 
639 /*
640  * low-level synchronization for implementing the above
641  */
642 uintptr	runtime_semacreate(void);
643 int32	runtime_semasleep(int64);
644 void	runtime_semawakeup(M*);
645 // or
646 void	runtime_futexsleep(uint32*, uint32, int64);
647 void	runtime_futexwakeup(uint32*, uint32);
648 
649 /*
650  * Lock-free stack.
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.
654  */
655 void	runtime_lfstackpush(uint64 *head, LFNode *node)
656   __asm__ (GOSYM_PREFIX "runtime.lfstackpush");
657 LFNode*	runtime_lfstackpop(uint64 *head);
658 
659 /*
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.
666  */
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");
670 
671 /*
672  * low level C-called
673  */
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)
679 
680 #ifdef __rtems__
681 void __wrap_rtems_task_variable_add(void **);
682 #endif
683 
684 /*
685  * Names generated by gccgo.
686  */
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
697 
698 /*
699  * runtime go-called
700  */
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,
715 		  void **, void **)
716   __asm__ (GOSYM_PREFIX "reflect.call");
717 #define runtime_panic __go_panic
718 
719 /*
720  * runtime c-called (but written in Go)
721  */
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");
730 
731 /*
732  * wrapped for go users
733  */
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);
741 
742 bool	runtime_showframe(String, bool);
743 void	runtime_printcreatedby(G*);
744 
745 uintptr	runtime_memlimit(void);
746 
747 #define ISNAN(f) __builtin_isnan(f)
748 
749 enum
750 {
751 	UseSpanType = 0,
752 };
753 
754 #define runtime_setitimer setitimer
755 
756 void	runtime_check(void);
757 
758 // A list of global variables that the garbage collector must scan.
759 struct root_list {
760 	struct root_list *next;
761 	struct root {
762 		void *decl;
763 		size_t size;
764 	} roots[];
765 };
766 
767 void	__go_register_gc_roots(struct root_list*);
768 
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;
773 
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;
780 
781 int32 getproccount(void);
782 
783 #define PREFETCH(p) __builtin_prefetch(p)
784 
785 void	__go_set_closure(void*);
786 void*	__go_get_closure(void);
787 
788 bool	runtime_gcwaiting(void);
789 void	runtime_badsignal(int);
790