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_needextram
;
244 extern uint32
runtime_panicking(void)
245 __asm__ (GOSYM_PREFIX
"runtime.getPanicking");
246 extern int8
* runtime_goos
;
247 extern int32 runtime_ncpu
;
248 extern void (*runtime_sysargs
)(int32
, uint8
**);
249 extern struct debugVars runtime_debug
;
251 extern bool runtime_isstarted
;
252 extern bool runtime_isarchive
;
255 * common functions and data
257 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
258 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
259 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
260 intgo
runtime_findnull(const byte
*)
261 __asm__ (GOSYM_PREFIX
"runtime.findnull");
263 void runtime_gogo(G
*);
264 struct __go_func_type
;
265 void runtime_args(int32
, byte
**)
266 __asm__ (GOSYM_PREFIX
"runtime.args");
267 void runtime_osinit();
268 void runtime_alginit(void)
269 __asm__ (GOSYM_PREFIX
"runtime.alginit");
270 void runtime_goargs(void)
271 __asm__ (GOSYM_PREFIX
"runtime.goargs");
272 void runtime_goenvs(void);
273 void runtime_goenvs_unix(void)
274 __asm__ (GOSYM_PREFIX
"runtime.goenvs_unix");
275 void runtime_throw(const char*) __attribute__ ((noreturn
));
276 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
277 bool runtime_canpanic(G
*);
278 void runtime_printf(const char*, ...);
279 int32
runtime_snprintf(byte
*, int32
, const char*, ...);
280 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
281 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
282 void* runtime_mal(uintptr
);
283 String
runtime_gostringnocopy(const byte
*)
284 __asm__ (GOSYM_PREFIX
"runtime.gostringnocopy");
285 void runtime_schedinit(void);
286 void runtime_initsig(bool)
287 __asm__ (GOSYM_PREFIX
"runtime.initsig");
288 int32
runtime_gotraceback(bool *crash
);
289 void runtime_goroutineheader(G
*)
290 __asm__ (GOSYM_PREFIX
"runtime.goroutineheader");
291 void runtime_printtrace(Slice
, G
*)
292 __asm__ (GOSYM_PREFIX
"runtime.printtrace");
293 #define runtime_open(p, f, m) open((p), (f), (m))
294 #define runtime_read(d, v, n) read((d), (v), (n))
295 #define runtime_write(d, v, n) write((d), (v), (n))
296 #define runtime_close(d) close(d)
297 void runtime_ready(G
*);
298 String
runtime_getenv(const char*);
299 int32
runtime_atoi(const byte
*, intgo
);
300 void* runtime_mstart(void*);
301 G
* runtime_malg(int32
, byte
**, uintptr
*);
302 void runtime_mpreinit(M
*);
303 void runtime_minit(void);
304 void runtime_unminit(void);
305 void runtime_needm(void)
306 __asm__ (GOSYM_PREFIX
"runtime.needm");
307 void runtime_dropm(void)
308 __asm__ (GOSYM_PREFIX
"runtime.dropm");
309 void runtime_signalstack(byte
*, int32
);
310 MCache
* runtime_allocmcache(void)
311 __asm__ (GOSYM_PREFIX
"runtime.allocmcache");
312 void runtime_freemcache(MCache
*);
313 void runtime_mallocinit(void);
314 void runtime_mprofinit(void);
315 #define runtime_malloc(s) __go_alloc(s)
316 #define runtime_free(p) __go_free(p)
317 #define runtime_getcallersp(p) __builtin_frame_address(0)
318 int32
runtime_mcount(void)
319 __asm__ (GOSYM_PREFIX
"runtime.mcount");
320 int32
runtime_gcount(void)
321 __asm__ (GOSYM_PREFIX
"runtime.gcount");
322 void runtime_mcall(void(*)(G
*));
323 uint32
runtime_fastrand1(void) __asm__ (GOSYM_PREFIX
"runtime.fastrand1");
324 int32
runtime_timediv(int64
, int32
, int32
*)
325 __asm__ (GOSYM_PREFIX
"runtime.timediv");
326 int32
runtime_round2(int32 x
); // round x up to a power of 2.
329 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
330 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
331 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
332 // Don't confuse with XADD x86 instruction,
333 // this one is actually 'addx', that is, add-and-fetch.
334 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
335 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
336 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
337 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
338 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
339 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
340 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
341 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
342 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
343 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
344 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
346 void runtime_setg(G
*)
347 __asm__ (GOSYM_PREFIX
"runtime.setg");
348 void runtime_newextram(void);
349 #define runtime_exit(s) exit(s)
350 #define runtime_breakpoint() __builtin_trap()
351 void runtime_gosched(void);
352 void runtime_gosched0(G
*);
353 void runtime_schedtrace(bool)
354 __asm__ (GOSYM_PREFIX
"runtime.schedtrace");
355 void runtime_park(bool(*)(G
*, void*), void*, const char*);
356 void runtime_parkunlock(Lock
*, const char*);
357 void runtime_tsleep(int64
, const char*);
358 M
* runtime_newm(void);
359 void runtime_goexit1(void)
360 __asm__ (GOSYM_PREFIX
"runtime.goexit1");
361 void runtime_entersyscall(int32
)
362 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
363 void runtime_entersyscallblock(int32
)
364 __asm__ (GOSYM_PREFIX
"runtime.entersyscallblock");
365 void runtime_exitsyscall(int32
)
366 __asm__ (GOSYM_PREFIX
"runtime.exitsyscall");
367 G
* __go_go(void (*pfn
)(void*), void*);
368 int32
runtime_callers(int32
, Location
*, int32
, bool keep_callers
);
369 int64
runtime_nanotime(void) // monotonic time
370 __asm__(GOSYM_PREFIX
"runtime.nanotime");
371 int64
runtime_unixnanotime(void) // real time, can skip
372 __asm__ (GOSYM_PREFIX
"runtime.unixnanotime");
373 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
374 void runtime_startpanic(void)
375 __asm__ (GOSYM_PREFIX
"runtime.startpanic");
376 void runtime_freezetheworld(void)
377 __asm__ (GOSYM_PREFIX
"runtime.freezetheworld");
378 void runtime_unwindstack(G
*, byte
*);
379 void runtime_sigprof()
380 __asm__ (GOSYM_PREFIX
"runtime.sigprof");
381 void runtime_resetcpuprofiler(int32
)
382 __asm__ (GOSYM_PREFIX
"runtime.resetcpuprofiler");
383 void runtime_setcpuprofilerate_m(int32
)
384 __asm__ (GOSYM_PREFIX
"runtime.setcpuprofilerate_m");
385 void runtime_cpuprofAdd(Slice
)
386 __asm__ (GOSYM_PREFIX
"runtime.cpuprofAdd");
387 void runtime_usleep(uint32
)
388 __asm__ (GOSYM_PREFIX
"runtime.usleep");
389 int64
runtime_cputicks(void)
390 __asm__ (GOSYM_PREFIX
"runtime.cputicks");
391 int64
runtime_tickspersecond(void)
392 __asm__ (GOSYM_PREFIX
"runtime.tickspersecond");
393 void runtime_blockevent(int64
, int32
);
394 extern int64 runtime_blockprofilerate
;
395 G
* runtime_netpoll(bool)
396 __asm__ (GOSYM_PREFIX
"runtime.netpoll");
397 void runtime_crash(void)
398 __asm__ (GOSYM_PREFIX
"runtime.crash");
399 void runtime_parsedebugvars(void)
400 __asm__(GOSYM_PREFIX
"runtime.parsedebugvars");
402 intgo
runtime_setmaxthreads(intgo
)
403 __asm__ (GOSYM_PREFIX
"runtime.setmaxthreads");
404 G
* runtime_timejump(void);
405 void runtime_iterate_finq(void (*callback
)(FuncVal
*, void*, const FuncType
*, const PtrType
*));
407 void runtime_stopTheWorldWithSema(void)
408 __asm__(GOSYM_PREFIX
"runtime.stopTheWorldWithSema");
409 void runtime_startTheWorldWithSema(void)
410 __asm__(GOSYM_PREFIX
"runtime.startTheWorldWithSema");
411 void runtime_acquireWorldsema(void)
412 __asm__(GOSYM_PREFIX
"runtime.acquireWorldsema");
413 void runtime_releaseWorldsema(void)
414 __asm__(GOSYM_PREFIX
"runtime.releaseWorldsema");
417 * mutual exclusion locks. in the uncontended case,
418 * as fast as spin locks (just a few user-level instructions),
419 * but on the contention path they sleep in the kernel.
420 * a zeroed Lock is unlocked (no need to initialize each lock).
422 void runtime_lock(Lock
*)
423 __asm__(GOSYM_PREFIX
"runtime.lock");
424 void runtime_unlock(Lock
*)
425 __asm__(GOSYM_PREFIX
"runtime.unlock");
428 * sleep and wakeup on one-time events.
429 * before any calls to notesleep or notewakeup,
430 * must call noteclear to initialize the Note.
431 * then, exactly one thread can call notesleep
432 * and exactly one thread can call notewakeup (once).
433 * once notewakeup has been called, the notesleep
434 * will return. future notesleep will return immediately.
435 * subsequent noteclear must be called only after
436 * previous notesleep has returned, e.g. it's disallowed
437 * to call noteclear straight after notewakeup.
439 * notetsleep is like notesleep but wakes up after
440 * a given number of nanoseconds even if the event
441 * has not yet happened. if a goroutine uses notetsleep to
442 * wake up early, it must wait to call noteclear until it
443 * can be sure that no other goroutine is calling
446 * notesleep/notetsleep are generally called on g0,
447 * notetsleepg is similar to notetsleep but is called on user g.
449 void runtime_noteclear(Note
*)
450 __asm__ (GOSYM_PREFIX
"runtime.noteclear");
451 void runtime_notesleep(Note
*)
452 __asm__ (GOSYM_PREFIX
"runtime.notesleep");
453 void runtime_notewakeup(Note
*)
454 __asm__ (GOSYM_PREFIX
"runtime.notewakeup");
455 bool runtime_notetsleep(Note
*, int64
) // false - timeout
456 __asm__ (GOSYM_PREFIX
"runtime.notetsleep");
457 bool runtime_notetsleepg(Note
*, int64
) // false - timeout
458 __asm__ (GOSYM_PREFIX
"runtime.notetsleepg");
462 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
463 * The stack does not keep pointers to nodes,
464 * so they can be garbage collected if there are no other pointers to nodes.
466 void runtime_lfstackpush(uint64
*head
, LFNode
*node
)
467 __asm__ (GOSYM_PREFIX
"runtime.lfstackpush");
468 void* runtime_lfstackpop(uint64
*head
)
469 __asm__ (GOSYM_PREFIX
"runtime.lfstackpop");
472 * Parallel for over [0, n).
473 * body() is executed for each iteration.
474 * nthr - total number of worker threads.
475 * if wait=true, threads return from parfor() when all work is done;
476 * otherwise, threads can return while other threads are still finishing processing.
478 ParFor
* runtime_parforalloc(uint32 nthrmax
);
479 void runtime_parforsetup(ParFor
*desc
, uint32 nthr
, uint32 n
, bool wait
, const FuncVal
*body
);
480 void runtime_parfordo(ParFor
*desc
);
481 void runtime_parforiters(ParFor
*, uintptr
, uintptr
*, uintptr
*);
486 #define runtime_mmap mmap
487 #define runtime_munmap munmap
488 #define runtime_madvise madvise
489 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
490 #define runtime_getcallerpc(p) __builtin_return_address(0)
493 void __wrap_rtems_task_variable_add(void **);
499 void reflect_call(const struct __go_func_type
*, FuncVal
*, _Bool
, _Bool
,
501 __asm__ (GOSYM_PREFIX
"reflect.call");
502 void runtime_panic(Eface
)
503 __asm__ (GOSYM_PREFIX
"runtime.gopanic");
504 void runtime_panic(Eface
)
505 __attribute__ ((noreturn
));
508 * runtime c-called (but written in Go)
510 void runtime_newTypeAssertionError(const String
*, const String
*, const String
*, const String
*, Eface
*)
511 __asm__ (GOSYM_PREFIX
"runtime.NewTypeAssertionError");
512 void runtime_newErrorCString(const char*, Eface
*)
513 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
516 * wrapped for go users
518 void runtime_semacquire(uint32
volatile *, bool)
519 __asm__ (GOSYM_PREFIX
"runtime.semacquire");
520 void runtime_semrelease(uint32
volatile *)
521 __asm__ (GOSYM_PREFIX
"runtime.semrelease");
522 void runtime_procyield(uint32
)
523 __asm__(GOSYM_PREFIX
"runtime.procyield");
524 void runtime_osyield(void)
525 __asm__(GOSYM_PREFIX
"runtime.osyield");
526 void runtime_lockOSThread(void);
527 void runtime_unlockOSThread(void);
528 bool runtime_lockedOSThread(void);
530 void runtime_printcreatedby(G
*)
531 __asm__(GOSYM_PREFIX
"runtime.printcreatedby");
533 uintptr
runtime_memlimit(void);
535 #define ISNAN(f) __builtin_isnan(f)
542 #define runtime_setitimer setitimer
544 void runtime_check(void)
545 __asm__ (GOSYM_PREFIX
"runtime.check");
547 // A list of global variables that the garbage collector must scan.
549 struct root_list
*next
;
556 void __go_register_gc_roots(struct root_list
*);
558 // Size of stack space allocated using Go's allocator.
559 // This will be 0 when using split stacks, as in that case
560 // the stacks are allocated by the splitstack library.
561 extern uintptr runtime_stacks_sys
;
563 struct backtrace_state
;
564 extern struct backtrace_state
*__go_get_backtrace_state(void);
565 extern _Bool
__go_file_line(uintptr
, int, String
*, String
*, intgo
*);
566 extern void runtime_main(void*);
567 extern uint32 runtime_in_callers
;
569 int32
getproccount(void);
571 #define PREFETCH(p) __builtin_prefetch(p)
573 bool runtime_gcwaiting(void);
574 void runtime_badsignal(int);
575 Defer
* runtime_newdefer(void);
576 void runtime_freedefer(Defer
*);
584 struct time_now_ret
now() __asm__ (GOSYM_PREFIX
"time.now")
585 __attribute__ ((no_split_stack
));
587 extern void _cgo_wait_runtime_init_done (void);
588 extern void _cgo_notify_runtime_init_done (void);
589 extern _Bool runtime_iscgo
;
590 extern _Bool runtime_cgoHasExtraM
;
591 extern Hchan
*runtime_main_init_done
;
592 extern uintptr __go_end
__attribute__ ((weak
));
593 extern void *getitab(const struct __go_type_descriptor
*,
594 const struct __go_type_descriptor
*,
596 __asm__ (GOSYM_PREFIX
"runtime.getitab");
598 extern void runtime_cpuinit(void);
599 extern void setCpuidECX(uint32
)
600 __asm__ (GOSYM_PREFIX
"runtime.setCpuidECX");