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
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
51 typedef uintptr uintreg
;
58 typedef struct mutex 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 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 sudog SudoG
;
76 typedef struct schedt Sched
;
78 typedef struct __go_open_array Slice
;
79 typedef struct iface Iface
;
80 typedef struct eface Eface
;
81 typedef struct __go_type_descriptor Type
;
82 typedef struct _defer Defer
;
83 typedef struct _panic 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 tracebackg Traceback
;
93 typedef struct location Location
;
104 // variable-size, fn-specific data here
109 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
110 // the name conflict.
111 #define timeval go_timeval
112 #define timespec go_timespec
114 #include "runtime.inc"
120 * Per-CPU declaration.
122 extern M
* runtime_m(void);
123 extern G
* runtime_g(void)
124 __asm__(GOSYM_PREFIX
"runtime.getg");
136 PtrSize
= sizeof(void*),
140 // Per-M stack segment cache size.
142 // Global <-> per-M stack segment cache transfer batch size.
143 StackCacheBatch
= 16,
182 // Parallel for descriptor.
185 const FuncVal
*body
; // executed for each element
186 uint32 done
; // number of idle threads
187 uint32 nthr
; // total number of threads
188 uint32 nthrmax
; // maximum number of threads
189 uint32 thrseq
; // thread id sequencer
190 uint32 cnt
; // iteration space [0, cnt)
191 bool wait
; // if true, wait while all threads finish processing,
192 // otherwise parfor may return while other threads are still working
193 ParForThread
*thr
; // array of thread descriptors
202 extern bool runtime_precisestack
;
203 extern bool runtime_copystack
;
207 * you need super-gopher-guru privilege
210 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
211 #define nil ((void*)0)
212 #define USED(v) ((void) v)
213 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
216 // hashinit wants this many random bytes
219 void runtime_hashinit(void);
221 void runtime_traceback(int32
)
222 __asm__ (GOSYM_PREFIX
"runtime.traceback");
223 void runtime_tracebackothers(G
*)
224 __asm__ (GOSYM_PREFIX
"runtime.tracebackothers");
227 // The maximum number of frames we print for a traceback
228 TracebackMaxFrames
= 100,
234 extern uintptr
* runtime_getZerobase(void)
235 __asm__(GOSYM_PREFIX
"runtime.getZerobase");
236 extern G
** runtime_allg
;
237 extern uintptr runtime_allglen
;
238 extern G
* runtime_lastg
;
239 extern M
* runtime_allm
;
240 extern P
** runtime_allp
;
241 extern Sched
* runtime_sched
;
242 extern int32 runtime_gomaxprocs
;
243 extern uint32
runtime_panicking(void)
244 __asm__ (GOSYM_PREFIX
"runtime.getPanicking");
245 extern int8
* runtime_goos
;
246 extern int32 runtime_ncpu
;
247 extern void (*runtime_sysargs
)(int32
, uint8
**);
248 extern struct debugVars runtime_debug
;
250 extern bool runtime_isstarted
;
251 extern bool runtime_isarchive
;
254 * common functions and data
256 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
257 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
258 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
259 intgo
runtime_findnull(const byte
*)
260 __asm__ (GOSYM_PREFIX
"runtime.findnull");
262 void runtime_gogo(G
*);
263 struct __go_func_type
;
264 void runtime_args(int32
, byte
**)
265 __asm__ (GOSYM_PREFIX
"runtime.args");
266 void runtime_osinit();
267 void runtime_alginit(void)
268 __asm__ (GOSYM_PREFIX
"runtime.alginit");
269 void runtime_goargs(void)
270 __asm__ (GOSYM_PREFIX
"runtime.goargs");
271 void runtime_goenvs(void);
272 void runtime_goenvs_unix(void)
273 __asm__ (GOSYM_PREFIX
"runtime.goenvs_unix");
274 void runtime_throw(const char*) __attribute__ ((noreturn
));
275 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
276 bool runtime_canpanic(G
*);
277 void runtime_printf(const char*, ...);
278 int32
runtime_snprintf(byte
*, int32
, const char*, ...);
279 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
280 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
281 void* runtime_mal(uintptr
);
282 String
runtime_gostringnocopy(const byte
*)
283 __asm__ (GOSYM_PREFIX
"runtime.gostringnocopy");
284 void runtime_schedinit(void);
285 void runtime_initsig(bool)
286 __asm__ (GOSYM_PREFIX
"runtime.initsig");
287 int32
runtime_gotraceback(bool *crash
);
288 void runtime_goroutineheader(G
*)
289 __asm__ (GOSYM_PREFIX
"runtime.goroutineheader");
290 void runtime_printtrace(Slice
, G
*)
291 __asm__ (GOSYM_PREFIX
"runtime.printtrace");
292 #define runtime_open(p, f, m) open((p), (f), (m))
293 #define runtime_read(d, v, n) read((d), (v), (n))
294 #define runtime_write(d, v, n) write((d), (v), (n))
295 #define runtime_close(d) close(d)
296 void runtime_ready(G
*);
297 String
runtime_getenv(const char*);
298 int32
runtime_atoi(const byte
*, intgo
);
299 void* runtime_mstart(void*);
300 G
* runtime_malg(bool, bool, byte
**, uintptr
*)
301 __asm__(GOSYM_PREFIX
"runtime.malg");
302 void runtime_mpreinit(M
*);
303 void runtime_minit(void)
304 __asm__ (GOSYM_PREFIX
"runtime.minit");
305 void runtime_signalstack(byte
*, uintptr
)
306 __asm__ (GOSYM_PREFIX
"runtime.signalstack");
307 MCache
* runtime_allocmcache(void)
308 __asm__ (GOSYM_PREFIX
"runtime.allocmcache");
309 void runtime_freemcache(MCache
*);
310 void runtime_mallocinit(void);
311 void runtime_mprofinit(void);
312 #define runtime_malloc(s) __go_alloc(s)
313 #define runtime_free(p) __go_free(p)
314 #define runtime_getcallersp(p) __builtin_frame_address(0)
315 int32
runtime_mcount(void)
316 __asm__ (GOSYM_PREFIX
"runtime.mcount");
317 int32
runtime_gcount(void)
318 __asm__ (GOSYM_PREFIX
"runtime.gcount");
319 void runtime_mcall(void(*)(G
*));
320 uint32
runtime_fastrand1(void) __asm__ (GOSYM_PREFIX
"runtime.fastrand1");
321 int32
runtime_timediv(int64
, int32
, int32
*)
322 __asm__ (GOSYM_PREFIX
"runtime.timediv");
323 int32
runtime_round2(int32 x
); // round x up to a power of 2.
326 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
327 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
328 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
329 // Don't confuse with XADD x86 instruction,
330 // this one is actually 'addx', that is, add-and-fetch.
331 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
332 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
333 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
334 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
335 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
336 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
337 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
338 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
339 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
340 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
341 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
343 void runtime_setg(G
*)
344 __asm__ (GOSYM_PREFIX
"runtime.setg");
345 void runtime_newextram(void)
346 __asm__ (GOSYM_PREFIX
"runtime.newextram");
347 #define runtime_exit(s) exit(s)
348 #define runtime_breakpoint() __builtin_trap()
349 void runtime_gosched(void);
350 void runtime_gosched0(G
*);
351 void runtime_schedtrace(bool)
352 __asm__ (GOSYM_PREFIX
"runtime.schedtrace");
353 void runtime_park(bool(*)(G
*, void*), void*, const char*);
354 void runtime_parkunlock(Lock
*, const char*);
355 void runtime_tsleep(int64
, const char*);
356 M
* runtime_newm(void);
357 void runtime_goexit1(void)
358 __asm__ (GOSYM_PREFIX
"runtime.goexit1");
359 void runtime_entersyscall(int32
)
360 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
361 void runtime_entersyscallblock(int32
)
362 __asm__ (GOSYM_PREFIX
"runtime.entersyscallblock");
363 void runtime_exitsyscall(int32
)
364 __asm__ (GOSYM_PREFIX
"runtime.exitsyscall");
365 G
* __go_go(void (*pfn
)(void*), void*);
366 int32
runtime_callers(int32
, Location
*, int32
, bool keep_callers
);
367 int64
runtime_nanotime(void) // monotonic time
368 __asm__(GOSYM_PREFIX
"runtime.nanotime");
369 int64
runtime_unixnanotime(void) // real time, can skip
370 __asm__ (GOSYM_PREFIX
"runtime.unixnanotime");
371 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
372 void runtime_startpanic(void)
373 __asm__ (GOSYM_PREFIX
"runtime.startpanic");
374 void runtime_freezetheworld(void)
375 __asm__ (GOSYM_PREFIX
"runtime.freezetheworld");
376 void runtime_unwindstack(G
*, byte
*);
377 void runtime_sigprof()
378 __asm__ (GOSYM_PREFIX
"runtime.sigprof");
379 void runtime_resetcpuprofiler(int32
)
380 __asm__ (GOSYM_PREFIX
"runtime.resetcpuprofiler");
381 void runtime_setcpuprofilerate_m(int32
)
382 __asm__ (GOSYM_PREFIX
"runtime.setcpuprofilerate_m");
383 void runtime_cpuprofAdd(Slice
)
384 __asm__ (GOSYM_PREFIX
"runtime.cpuprofAdd");
385 void runtime_usleep(uint32
)
386 __asm__ (GOSYM_PREFIX
"runtime.usleep");
387 int64
runtime_cputicks(void)
388 __asm__ (GOSYM_PREFIX
"runtime.cputicks");
389 int64
runtime_tickspersecond(void)
390 __asm__ (GOSYM_PREFIX
"runtime.tickspersecond");
391 void runtime_blockevent(int64
, int32
);
392 extern int64 runtime_blockprofilerate
;
393 G
* runtime_netpoll(bool)
394 __asm__ (GOSYM_PREFIX
"runtime.netpoll");
395 void runtime_crash(void)
396 __asm__ (GOSYM_PREFIX
"runtime.crash");
397 void runtime_parsedebugvars(void)
398 __asm__(GOSYM_PREFIX
"runtime.parsedebugvars");
400 intgo
runtime_setmaxthreads(intgo
)
401 __asm__ (GOSYM_PREFIX
"runtime.setmaxthreads");
402 G
* runtime_timejump(void);
403 void runtime_iterate_finq(void (*callback
)(FuncVal
*, void*, const FuncType
*, const PtrType
*));
405 void runtime_stopTheWorldWithSema(void)
406 __asm__(GOSYM_PREFIX
"runtime.stopTheWorldWithSema");
407 void runtime_startTheWorldWithSema(void)
408 __asm__(GOSYM_PREFIX
"runtime.startTheWorldWithSema");
409 void runtime_acquireWorldsema(void)
410 __asm__(GOSYM_PREFIX
"runtime.acquireWorldsema");
411 void runtime_releaseWorldsema(void)
412 __asm__(GOSYM_PREFIX
"runtime.releaseWorldsema");
415 * mutual exclusion locks. in the uncontended case,
416 * as fast as spin locks (just a few user-level instructions),
417 * but on the contention path they sleep in the kernel.
418 * a zeroed Lock is unlocked (no need to initialize each lock).
420 void runtime_lock(Lock
*)
421 __asm__(GOSYM_PREFIX
"runtime.lock");
422 void runtime_unlock(Lock
*)
423 __asm__(GOSYM_PREFIX
"runtime.unlock");
426 * sleep and wakeup on one-time events.
427 * before any calls to notesleep or notewakeup,
428 * must call noteclear to initialize the Note.
429 * then, exactly one thread can call notesleep
430 * and exactly one thread can call notewakeup (once).
431 * once notewakeup has been called, the notesleep
432 * will return. future notesleep will return immediately.
433 * subsequent noteclear must be called only after
434 * previous notesleep has returned, e.g. it's disallowed
435 * to call noteclear straight after notewakeup.
437 * notetsleep is like notesleep but wakes up after
438 * a given number of nanoseconds even if the event
439 * has not yet happened. if a goroutine uses notetsleep to
440 * wake up early, it must wait to call noteclear until it
441 * can be sure that no other goroutine is calling
444 * notesleep/notetsleep are generally called on g0,
445 * notetsleepg is similar to notetsleep but is called on user g.
447 void runtime_noteclear(Note
*)
448 __asm__ (GOSYM_PREFIX
"runtime.noteclear");
449 void runtime_notesleep(Note
*)
450 __asm__ (GOSYM_PREFIX
"runtime.notesleep");
451 void runtime_notewakeup(Note
*)
452 __asm__ (GOSYM_PREFIX
"runtime.notewakeup");
453 bool runtime_notetsleep(Note
*, int64
) // false - timeout
454 __asm__ (GOSYM_PREFIX
"runtime.notetsleep");
455 bool runtime_notetsleepg(Note
*, int64
) // false - timeout
456 __asm__ (GOSYM_PREFIX
"runtime.notetsleepg");
460 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
461 * The stack does not keep pointers to nodes,
462 * so they can be garbage collected if there are no other pointers to nodes.
464 void runtime_lfstackpush(uint64
*head
, LFNode
*node
)
465 __asm__ (GOSYM_PREFIX
"runtime.lfstackpush");
466 void* runtime_lfstackpop(uint64
*head
)
467 __asm__ (GOSYM_PREFIX
"runtime.lfstackpop");
470 * Parallel for over [0, n).
471 * body() is executed for each iteration.
472 * nthr - total number of worker threads.
473 * if wait=true, threads return from parfor() when all work is done;
474 * otherwise, threads can return while other threads are still finishing processing.
476 ParFor
* runtime_parforalloc(uint32 nthrmax
);
477 void runtime_parforsetup(ParFor
*desc
, uint32 nthr
, uint32 n
, bool wait
, const FuncVal
*body
);
478 void runtime_parfordo(ParFor
*desc
);
479 void runtime_parforiters(ParFor
*, uintptr
, uintptr
*, uintptr
*);
484 #define runtime_mmap mmap
485 #define runtime_munmap munmap
486 #define runtime_madvise madvise
487 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
488 #define runtime_getcallerpc(p) __builtin_return_address(0)
491 void __wrap_rtems_task_variable_add(void **);
497 void reflect_call(const struct __go_func_type
*, FuncVal
*, _Bool
, _Bool
,
499 __asm__ (GOSYM_PREFIX
"reflect.call");
500 void runtime_panic(Eface
)
501 __asm__ (GOSYM_PREFIX
"runtime.gopanic");
502 void runtime_panic(Eface
)
503 __attribute__ ((noreturn
));
506 * runtime c-called (but written in Go)
508 void runtime_newTypeAssertionError(const String
*, const String
*, const String
*, const String
*, Eface
*)
509 __asm__ (GOSYM_PREFIX
"runtime.NewTypeAssertionError");
510 void runtime_newErrorCString(const char*, Eface
*)
511 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
514 * wrapped for go users
516 void runtime_semacquire(uint32
volatile *, bool)
517 __asm__ (GOSYM_PREFIX
"runtime.semacquire");
518 void runtime_semrelease(uint32
volatile *)
519 __asm__ (GOSYM_PREFIX
"runtime.semrelease");
520 void runtime_procyield(uint32
)
521 __asm__(GOSYM_PREFIX
"runtime.procyield");
522 void runtime_osyield(void)
523 __asm__(GOSYM_PREFIX
"runtime.osyield");
524 void runtime_lockOSThread(void)
525 __asm__(GOSYM_PREFIX
"runtime.lockOSThread");
526 void runtime_unlockOSThread(void)
527 __asm__(GOSYM_PREFIX
"runtime.unlockOSThread");
528 bool runtime_lockedOSThread(void)
529 __asm__(GOSYM_PREFIX
"runtime.lockedOSThread");
531 void runtime_printcreatedby(G
*)
532 __asm__(GOSYM_PREFIX
"runtime.printcreatedby");
534 uintptr
runtime_memlimit(void);
536 #define ISNAN(f) __builtin_isnan(f)
543 #define runtime_setitimer setitimer
545 void runtime_check(void)
546 __asm__ (GOSYM_PREFIX
"runtime.check");
548 // A list of global variables that the garbage collector must scan.
550 struct root_list
*next
;
557 void __go_register_gc_roots(struct root_list
*);
559 // Size of stack space allocated using Go's allocator.
560 // This will be 0 when using split stacks, as in that case
561 // the stacks are allocated by the splitstack library.
562 extern uintptr runtime_stacks_sys
;
564 struct backtrace_state
;
565 extern struct backtrace_state
*__go_get_backtrace_state(void);
566 extern _Bool
__go_file_line(uintptr
, int, String
*, String
*, intgo
*);
567 extern void runtime_main(void*);
568 extern uint32 runtime_in_callers
;
570 int32
getproccount(void);
572 #define PREFETCH(p) __builtin_prefetch(p)
574 bool runtime_gcwaiting(void);
575 void runtime_badsignal(int);
576 Defer
* runtime_newdefer(void);
577 void runtime_freedefer(Defer
*);
585 struct time_now_ret
now() __asm__ (GOSYM_PREFIX
"time.now")
586 __attribute__ ((no_split_stack
));
588 extern void _cgo_wait_runtime_init_done (void);
589 extern void _cgo_notify_runtime_init_done (void);
590 extern _Bool runtime_iscgo
;
591 extern uintptr __go_end
__attribute__ ((weak
));
592 extern void *getitab(const struct __go_type_descriptor
*,
593 const struct __go_type_descriptor
*,
595 __asm__ (GOSYM_PREFIX
"runtime.getitab");
597 extern void runtime_cpuinit(void);
598 extern void setIsCgo(void)
599 __asm__ (GOSYM_PREFIX
"runtime.setIsCgo");
600 extern void setCpuidECX(uint32
)
601 __asm__ (GOSYM_PREFIX
"runtime.setCpuidECX");
602 extern void makeMainInitDone(void)
603 __asm__ (GOSYM_PREFIX
"runtime.makeMainInitDone");
604 extern void closeMainInitDone(void)
605 __asm__ (GOSYM_PREFIX
"runtime.closeMainInitDone");