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.
14 #include <sys/types.h>
19 #include <semaphore.h>
22 #ifdef HAVE_SYS_MMAN_H
26 #define _STRINGIFY2_(x) #x
27 #define _STRINGIFY_(x) _STRINGIFY2_(x)
28 #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
30 /* This file supports C files copied from the 6g runtime library.
31 This is a version of the 6g runtime.h rewritten for gccgo's version
34 typedef signed int int8
__attribute__ ((mode (QI
)));
35 typedef unsigned int uint8
__attribute__ ((mode (QI
)));
36 typedef signed int int16
__attribute__ ((mode (HI
)));
37 typedef unsigned int uint16
__attribute__ ((mode (HI
)));
38 typedef signed int int32
__attribute__ ((mode (SI
)));
39 typedef unsigned int uint32
__attribute__ ((mode (SI
)));
40 typedef signed int int64
__attribute__ ((mode (DI
)));
41 typedef unsigned int uint64
__attribute__ ((mode (DI
)));
42 typedef float float32
__attribute__ ((mode (SF
)));
43 typedef double float64
__attribute__ ((mode (DF
)));
44 typedef signed int intptr
__attribute__ ((mode (pointer
)));
45 typedef unsigned int uintptr
__attribute__ ((mode (pointer
)));
47 typedef intptr intgo
; // Go's int
48 typedef uintptr uintgo
; // Go's uint
50 typedef uintptr uintreg
;
57 typedef struct mutex Lock
;
60 typedef struct note Note
;
61 typedef struct String String
;
62 typedef struct FuncVal FuncVal
;
63 typedef struct SigTab SigTab
;
64 typedef struct hchan Hchan
;
65 typedef struct timer Timer
;
66 typedef struct lfnode LFNode
;
67 typedef struct cgoMal CgoMal
;
68 typedef struct PollDesc PollDesc
;
69 typedef struct sudog SudoG
;
70 typedef struct schedt Sched
;
72 typedef struct __go_open_array Slice
;
73 typedef struct iface Iface
;
74 typedef struct eface Eface
;
75 typedef struct __go_type_descriptor Type
;
76 typedef struct _defer Defer
;
77 typedef struct _panic Panic
;
79 typedef struct __go_ptr_type PtrType
;
80 typedef struct __go_func_type FuncType
;
81 typedef struct __go_map_type MapType
;
83 typedef struct tracebackg Traceback
;
85 typedef struct location Location
;
96 // variable-size, fn-specific data here
101 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
102 // the name conflict.
103 #define timeval go_timeval
104 #define timespec go_timespec
106 #include "runtime.inc"
112 * Per-CPU declaration.
114 extern M
* runtime_m(void);
115 extern G
* runtime_g(void)
116 __asm__(GOSYM_PREFIX
"runtime.getg");
118 extern M
* runtime_m0(void)
119 __asm__(GOSYM_PREFIX
"runtime.runtime_m0");
120 extern G
* runtime_g0(void)
121 __asm__(GOSYM_PREFIX
"runtime.runtime_g0");
130 PtrSize
= sizeof(void*),
134 // Per-M stack segment cache size.
136 // Global <-> per-M stack segment cache transfer batch size.
137 StackCacheBatch
= 16,
176 extern bool runtime_copystack
;
180 * you need super-gopher-guru privilege
183 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
184 #define nil ((void*)0)
185 #define USED(v) ((void) v)
186 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
189 // hashinit wants this many random bytes
192 void runtime_hashinit(void);
197 extern uintptr
* runtime_getZerobase(void)
198 __asm__(GOSYM_PREFIX
"runtime.getZerobase");
199 extern G
* runtime_getallg(intgo
)
200 __asm__(GOSYM_PREFIX
"runtime.getallg");
201 extern uintptr
runtime_getallglen(void)
202 __asm__(GOSYM_PREFIX
"runtime.getallglen");
203 extern M
* runtime_getallm(void)
204 __asm__(GOSYM_PREFIX
"runtime.getallm");
205 extern Sched
* runtime_sched
;
206 extern uint32
runtime_panicking(void)
207 __asm__ (GOSYM_PREFIX
"runtime.getPanicking");
209 extern bool runtime_isstarted
;
210 extern bool runtime_isarchive
;
212 extern void panicmem(void) __asm__ (GOSYM_PREFIX
"runtime.panicmem");
215 * common functions and data
217 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
218 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
219 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
220 intgo
runtime_findnull(const byte
*)
221 __asm__ (GOSYM_PREFIX
"runtime.findnull");
223 void runtime_gogo(G
*)
224 __asm__ (GOSYM_PREFIX
"runtime.gogo");
225 struct __go_func_type
;
226 void runtime_args(int32
, byte
**)
227 __asm__ (GOSYM_PREFIX
"runtime.args");
228 void runtime_alginit(void)
229 __asm__ (GOSYM_PREFIX
"runtime.alginit");
230 void runtime_goargs(void)
231 __asm__ (GOSYM_PREFIX
"runtime.goargs");
232 void runtime_throw(const char*) __attribute__ ((noreturn
));
233 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
234 bool runtime_canpanic(G
*);
235 void runtime_printf(const char*, ...);
236 int32
runtime_snprintf(byte
*, int32
, const char*, ...);
237 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
238 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
239 String
runtime_gostringnocopy(const byte
*)
240 __asm__ (GOSYM_PREFIX
"runtime.gostringnocopy");
241 void runtime_ginit(void)
242 __asm__ (GOSYM_PREFIX
"runtime.ginit");
243 void runtime_schedinit(void)
244 __asm__ (GOSYM_PREFIX
"runtime.schedinit");
245 void runtime_initsig(bool)
246 __asm__ (GOSYM_PREFIX
"runtime.initsig");
247 #define runtime_open(p, f, m) open((p), (f), (m))
248 #define runtime_read(d, v, n) read((d), (v), (n))
249 #define runtime_write(d, v, n) write((d), (v), (n))
250 #define runtime_close(d) close(d)
251 void runtime_ready(G
*, intgo
, bool)
252 __asm__ (GOSYM_PREFIX
"runtime.ready");
253 String
runtime_getenv(const char*);
254 int32
runtime_atoi(const byte
*, intgo
);
255 void* runtime_mstart(void*);
256 G
* runtime_malg(bool, bool, byte
**, uintptr
*)
257 __asm__(GOSYM_PREFIX
"runtime.malg");
258 void runtime_minit(void)
259 __asm__ (GOSYM_PREFIX
"runtime.minit");
260 void runtime_signalstack(byte
*, uintptr
)
261 __asm__ (GOSYM_PREFIX
"runtime.signalstack");
262 void runtime_mallocinit(void)
263 __asm__ (GOSYM_PREFIX
"runtime.mallocinit");
264 void* runtime_mallocgc(uintptr
, const Type
*, bool)
265 __asm__ (GOSYM_PREFIX
"runtime.mallocgc");
266 void* runtime_sysAlloc(uintptr
, uint64
*)
267 __asm__ (GOSYM_PREFIX
"runtime.sysAlloc");
268 void runtime_sysFree(void*, uintptr
, uint64
*)
269 __asm__ (GOSYM_PREFIX
"runtime.sysFree");
270 void runtime_mprofinit(void);
271 #define runtime_getcallersp() __builtin_dwarf_cfa()
272 void runtime_mcall(FuncVal
*)
273 __asm__ (GOSYM_PREFIX
"runtime.mcall");
274 int32
runtime_timediv(int64
, int32
, int32
*)
275 __asm__ (GOSYM_PREFIX
"runtime.timediv");
276 int32
runtime_round2(int32 x
); // round x up to a power of 2.
279 #define runtime_xadd(p, v) __atomic_add_fetch (p, v, __ATOMIC_SEQ_CST)
280 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
282 void runtime_setg(G
*)
283 __asm__ (GOSYM_PREFIX
"runtime.setg");
284 void runtime_newextram(void)
285 __asm__ (GOSYM_PREFIX
"runtime.newextram");
286 #define runtime_exit(s) exit(s)
287 void runtime_gosched(void)
288 __asm__ (GOSYM_PREFIX
"runtime.Gosched");
289 void runtime_schedtrace(bool)
290 __asm__ (GOSYM_PREFIX
"runtime.schedtrace");
291 void runtime_goparkunlock(Lock
*, String
, byte
, intgo
)
292 __asm__ (GOSYM_PREFIX
"runtime.goparkunlock");
293 void runtime_tsleep(int64
, const char*);
294 void runtime_entersyscall()
295 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
296 void runtime_entersyscallblock()
297 __asm__ (GOSYM_PREFIX
"runtime.entersyscallblock");
298 G
* __go_go(uintptr
, void*);
299 int32
runtime_callers(int32
, Location
*, int32
, bool keep_callers
);
300 int64
runtime_nanotime(void) // monotonic time
301 __asm__(GOSYM_PREFIX
"runtime.nanotime");
302 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
303 void runtime_startpanic(void)
304 __asm__ (GOSYM_PREFIX
"runtime.startpanic");
305 void runtime_unwindstack(G
*, byte
*);
306 void runtime_usleep(uint32
)
307 __asm__ (GOSYM_PREFIX
"runtime.usleep");
308 int64
runtime_cputicks(void)
309 __asm__ (GOSYM_PREFIX
"runtime.cputicks");
310 int64
runtime_tickspersecond(void)
311 __asm__ (GOSYM_PREFIX
"runtime.tickspersecond");
312 void runtime_blockevent(int64
, int32
);
313 extern int64 runtime_blockprofilerate
;
314 G
* runtime_netpoll(bool)
315 __asm__ (GOSYM_PREFIX
"runtime.netpoll");
316 void runtime_parsedebugvars(void)
317 __asm__(GOSYM_PREFIX
"runtime.parsedebugvars");
319 G
* runtime_timejump(void);
322 * mutual exclusion locks. in the uncontended case,
323 * as fast as spin locks (just a few user-level instructions),
324 * but on the contention path they sleep in the kernel.
325 * a zeroed Lock is unlocked (no need to initialize each lock).
327 void runtime_lock(Lock
*)
328 __asm__(GOSYM_PREFIX
"runtime.lock");
329 void runtime_unlock(Lock
*)
330 __asm__(GOSYM_PREFIX
"runtime.unlock");
333 * sleep and wakeup on one-time events.
334 * before any calls to notesleep or notewakeup,
335 * must call noteclear to initialize the Note.
336 * then, exactly one thread can call notesleep
337 * and exactly one thread can call notewakeup (once).
338 * once notewakeup has been called, the notesleep
339 * will return. future notesleep will return immediately.
340 * subsequent noteclear must be called only after
341 * previous notesleep has returned, e.g. it's disallowed
342 * to call noteclear straight after notewakeup.
344 * notetsleep is like notesleep but wakes up after
345 * a given number of nanoseconds even if the event
346 * has not yet happened. if a goroutine uses notetsleep to
347 * wake up early, it must wait to call noteclear until it
348 * can be sure that no other goroutine is calling
351 * notesleep/notetsleep are generally called on g0,
352 * notetsleepg is similar to notetsleep but is called on user g.
354 void runtime_noteclear(Note
*)
355 __asm__ (GOSYM_PREFIX
"runtime.noteclear");
356 void runtime_notesleep(Note
*)
357 __asm__ (GOSYM_PREFIX
"runtime.notesleep");
358 void runtime_notewakeup(Note
*)
359 __asm__ (GOSYM_PREFIX
"runtime.notewakeup");
360 bool runtime_notetsleep(Note
*, int64
) // false - timeout
361 __asm__ (GOSYM_PREFIX
"runtime.notetsleep");
362 bool runtime_notetsleepg(Note
*, int64
) // false - timeout
363 __asm__ (GOSYM_PREFIX
"runtime.notetsleepg");
368 #define runtime_mmap mmap
369 #define runtime_munmap munmap
370 #define runtime_madvise madvise
371 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
372 #define runtime_getcallerpc() __builtin_return_address(0)
375 void __wrap_rtems_task_variable_add(void **);
381 void reflect_call(const struct __go_func_type
*, FuncVal
*, _Bool
, _Bool
,
383 __asm__ (GOSYM_PREFIX
"runtime.reflectcall");
384 void runtime_panic(Eface
)
385 __asm__ (GOSYM_PREFIX
"runtime.gopanic");
386 void runtime_panic(Eface
)
387 __attribute__ ((noreturn
));
390 * runtime c-called (but written in Go)
392 void runtime_newErrorCString(uintptr
, Eface
*)
393 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
396 * wrapped for go users
398 void runtime_procyield(uint32
)
399 __asm__(GOSYM_PREFIX
"runtime.procyield");
400 void runtime_osyield(void)
401 __asm__(GOSYM_PREFIX
"runtime.osyield");
403 uintptr
runtime_memlimit(void);
405 #define ISNAN(f) __builtin_isnan(f)
412 #define runtime_setitimer setitimer
414 void runtime_check(void)
415 __asm__ (GOSYM_PREFIX
"runtime.check");
417 // Size of stack space allocated using Go's allocator.
418 // This will be 0 when using split stacks, as in that case
419 // the stacks are allocated by the splitstack library.
420 extern uintptr runtime_stacks_sys
;
423 * ia64's register file is spilled to a separate stack, the register backing
424 * store, on window overflow, and must also be scanned. This occupies the other
425 * end of the normal stack allocation, growing upwards.
426 * We also need to ensure all register windows are flushed to the backing
427 * store, as unlike SPARC, __builtin_unwind_init doesn't do this on ia64.
430 # define secondary_stack_pointer() __builtin_ia64_bsp()
431 # define initial_secondary_stack_pointer(stack_alloc) (stack_alloc)
432 # define flush_registers_to_secondary_stack() __builtin_ia64_flushrs()
434 # define secondary_stack_pointer() nil
435 # define initial_secondary_stack_pointer(stack_alloc) nil
436 # define flush_registers_to_secondary_stack()
439 struct backtrace_state
;
440 extern struct backtrace_state
*__go_get_backtrace_state(void);
441 extern void __go_syminfo_fnname_callback(void*, uintptr_t, const char*,
442 uintptr_t, uintptr_t);
443 extern void runtime_main(void*)
444 __asm__(GOSYM_PREFIX
"runtime.main");
446 int32
getproccount(void);
448 #define PREFETCH(p) __builtin_prefetch(p)
450 bool runtime_gcwaiting(void);
451 void runtime_badsignal(int);
452 Defer
* runtime_newdefer(void);
453 void runtime_freedefer(Defer
*);
455 extern void _cgo_wait_runtime_init_done (void);
456 extern void _cgo_notify_runtime_init_done (void)
457 __asm__ (GOSYM_PREFIX
"runtime._cgo_notify_runtime_init_done");
458 extern _Bool runtime_iscgo
;
459 extern uintptr __go_end
__attribute__ ((weak
));
460 extern void *getitab(const struct __go_type_descriptor
*,
461 const struct __go_type_descriptor
*,
463 __asm__ (GOSYM_PREFIX
"runtime.getitab");
465 extern void runtime_cpuinit(void);
466 extern void setRandomNumber(uint32
)
467 __asm__ (GOSYM_PREFIX
"runtime.setRandomNumber");
468 extern void setIsCgo(void)
469 __asm__ (GOSYM_PREFIX
"runtime.setIsCgo");
470 extern void setSupportAES(bool)
471 __asm__ (GOSYM_PREFIX
"runtime.setSupportAES");
472 extern void typedmemmove(const Type
*, void *, const void *)
473 __asm__ (GOSYM_PREFIX
"runtime.typedmemmove");
474 extern void setncpu(int32
)
475 __asm__(GOSYM_PREFIX
"runtime.setncpu");
476 extern Sched
* runtime_getsched(void)
477 __asm__ (GOSYM_PREFIX
"runtime.getsched");
478 extern void setpagesize(uintptr_t)
479 __asm__(GOSYM_PREFIX
"runtime.setpagesize");
481 struct funcfileline_return
489 struct funcfileline_return
490 runtime_funcfileline (uintptr targetpc
, int32 index
)
491 __asm__ (GOSYM_PREFIX
"runtime.funcfileline");
494 * helpers for stack scan.
496 bool scanstackwithmap(void*)
497 __asm__(GOSYM_PREFIX
"runtime.scanstackwithmap");
498 bool doscanstack(G
*, void*)
499 __asm__("runtime.doscanstack");
501 bool runtime_usestackmaps
;
503 bool probestackmaps(void)
504 __asm__("runtime.probestackmaps");
506 // This is set to non-zero when calling backtrace_full. This is used
507 // to avoid getting hanging on a recursive lock in dl_iterate_phdr on
508 // older versions of glibc when a SIGPROF signal arrives while
509 // collecting a backtrace.
510 extern uint32 __go_runtime_in_callers
;
512 // Cheaper context switch functions. Currently only defined on
514 #if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
518 int __go_getcontext(__go_context_t
*);
519 int __go_setcontext(__go_context_t
*);
520 void __go_makecontext(__go_context_t
*, void (*)(), void*, size_t);
522 #define __go_context_t ucontext_t
523 #define __go_getcontext(c) getcontext(c)
524 #define __go_setcontext(c) setcontext(c)
525 #define __go_makecontext(c, fn, sp, size) \
526 ((c)->uc_stack.ss_sp = sp, (c)->uc_stack.ss_size = size, makecontext(c, fn, 0))