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.
15 #include <sys/types.h>
20 #include <semaphore.h>
23 #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
;
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 _defer Defer
;
76 typedef struct _panic Panic
;
78 typedef struct tracebackg Traceback
;
80 typedef struct location Location
;
91 // variable-size, fn-specific data here
94 // Type structs will be defined by runtime.inc.
100 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
101 // the name conflict.
102 #define timeval go_timeval
103 #define timespec go_timespec
105 #include "runtime.inc"
111 * Per-CPU declaration.
113 extern M
* runtime_m(void);
114 extern G
* runtime_g(void)
115 __asm__(GOSYM_PREFIX
"runtime.getg");
119 PtrSize
= sizeof(void*),
123 // Per-M stack segment cache size.
125 // Global <-> per-M stack segment cache transfer batch size.
126 StackCacheBatch
= 16,
165 extern bool runtime_copystack
;
169 * you need super-gopher-guru privilege
172 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
173 #define nil ((void*)0)
174 #define USED(v) ((void) v)
175 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
178 // hashinit wants this many random bytes
181 void runtime_hashinit(void);
186 extern uintptr
* runtime_getZerobase(void)
187 __asm__(GOSYM_PREFIX
"runtime.getZerobase");
189 extern bool runtime_isstarted
;
190 extern bool runtime_isarchive
;
192 extern void panicmem(void) __asm__ (GOSYM_PREFIX
"runtime.panicmem");
195 * common functions and data
197 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
198 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
199 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
200 intgo
runtime_findnull(const byte
*)
201 __asm__ (GOSYM_PREFIX
"runtime.findnull");
203 void runtime_gogo(G
*)
204 __asm__ (GOSYM_PREFIX
"runtime.gogo");
205 void runtime_args(int32
, byte
**)
206 __asm__ (GOSYM_PREFIX
"runtime.args");
207 void runtime_osinit(void)
208 __asm__ (GOSYM_PREFIX
"runtime.osinit");
209 void runtime_alginit(void)
210 __asm__ (GOSYM_PREFIX
"runtime.alginit");
211 void runtime_goargs(void)
212 __asm__ (GOSYM_PREFIX
"runtime.goargs");
213 void runtime_throw(const char*) __attribute__ ((noreturn
));
214 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
215 bool runtime_canpanic(G
*);
216 void runtime_printf(const char*, ...);
217 int32
runtime_snprintf(byte
*, int32
, const char*, ...);
218 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
219 void runtime_memmove(void*, void*, uint64
)
220 __asm__ (GOSYM_PREFIX
"runtime.memmove");
221 String
runtime_gostringnocopy(const byte
*)
222 __asm__ (GOSYM_PREFIX
"runtime.gostringnocopy");
223 void runtime_ginit(void)
224 __asm__ (GOSYM_PREFIX
"runtime.ginit");
225 void runtime_schedinit(void)
226 __asm__ (GOSYM_PREFIX
"runtime.schedinit");
227 void runtime_initsig(bool)
228 __asm__ (GOSYM_PREFIX
"runtime.initsig");
229 #define runtime_open(p, f, m) open((p), (f), (m))
230 #define runtime_read(d, v, n) read((d), (v), (n))
231 #define runtime_write(d, v, n) write((d), (v), (n))
232 #define runtime_close(d) close(d)
233 void runtime_ready(G
*, intgo
, bool)
234 __asm__ (GOSYM_PREFIX
"runtime.ready");
235 String
runtime_getenv(const char*);
236 int32
runtime_atoi(const byte
*, intgo
);
237 void* runtime_mstart(void*);
238 G
* runtime_malg(bool, bool, byte
**, uintptr
*)
239 __asm__(GOSYM_PREFIX
"runtime.malg");
240 void runtime_minit(void)
241 __asm__ (GOSYM_PREFIX
"runtime.minit");
242 void runtime_signalstack(byte
*, uintptr
)
243 __asm__ (GOSYM_PREFIX
"runtime.signalstack");
244 void runtime_mallocinit(void)
245 __asm__ (GOSYM_PREFIX
"runtime.mallocinit");
246 void* runtime_mallocgc(uintptr
, const struct _type
*, bool)
247 __asm__ (GOSYM_PREFIX
"runtime.mallocgc");
248 void* runtime_sysAlloc(uintptr
, uint64
*)
249 __asm__ (GOSYM_PREFIX
"runtime.sysAlloc");
250 void runtime_sysFree(void*, uintptr
, uint64
*)
251 __asm__ (GOSYM_PREFIX
"runtime.sysFree");
252 void runtime_mprofinit(void);
253 #define runtime_getcallersp() __builtin_dwarf_cfa()
254 void runtime_mcall(FuncVal
*)
255 __asm__ (GOSYM_PREFIX
"runtime.mcall");
256 int32
runtime_timediv(int64
, int32
, int32
*)
257 __asm__ (GOSYM_PREFIX
"runtime.timediv");
258 int32
runtime_round2(int32 x
); // round x up to a power of 2.
261 #define runtime_xadd(p, v) __atomic_add_fetch (p, v, __ATOMIC_SEQ_CST)
262 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
264 void runtime_setg(G
*)
265 __asm__ (GOSYM_PREFIX
"runtime.setg");
266 void runtime_newextram(void)
267 __asm__ (GOSYM_PREFIX
"runtime.newextram");
268 #define runtime_exit(s) exit(s)
269 void runtime_gosched(void)
270 __asm__ (GOSYM_PREFIX
"runtime.Gosched");
271 void runtime_schedtrace(bool)
272 __asm__ (GOSYM_PREFIX
"runtime.schedtrace");
273 void runtime_goparkunlock(Lock
*, String
, byte
, intgo
)
274 __asm__ (GOSYM_PREFIX
"runtime.goparkunlock");
275 void runtime_tsleep(int64
, const char*);
276 void runtime_entersyscall()
277 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
278 void runtime_entersyscallblock()
279 __asm__ (GOSYM_PREFIX
"runtime.entersyscallblock");
280 G
* __go_go(uintptr
, void*);
281 int32
runtime_callers(int32
, Location
*, int32
, bool keep_callers
);
283 bool runtime_skipInCallback(const char *, struct callers_data
*)
284 __asm__ (GOSYM_PREFIX
"runtime.skipInCallback");
285 int64
runtime_nanotime1(void) // monotonic time
286 __asm__(GOSYM_PREFIX
"runtime.nanotime1");
287 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
288 void runtime_startpanic(void)
289 __asm__ (GOSYM_PREFIX
"runtime.startpanic");
290 void runtime_unwindstack(G
*, byte
*);
291 void runtime_usleep(uint32
)
292 __asm__ (GOSYM_PREFIX
"runtime.usleep");
293 int64
runtime_cputicks(void)
294 __asm__ (GOSYM_PREFIX
"runtime.cputicks");
295 int64
runtime_tickspersecond(void)
296 __asm__ (GOSYM_PREFIX
"runtime.tickspersecond");
297 void runtime_blockevent(int64
, int32
);
298 extern int64 runtime_blockprofilerate
;
299 G
* runtime_netpoll(bool)
300 __asm__ (GOSYM_PREFIX
"runtime.netpoll");
301 void runtime_parsedebugvars(void)
302 __asm__(GOSYM_PREFIX
"runtime.parsedebugvars");
304 G
* runtime_timejump(void);
307 * mutual exclusion locks. in the uncontended case,
308 * as fast as spin locks (just a few user-level instructions),
309 * but on the contention path they sleep in the kernel.
310 * a zeroed Lock is unlocked (no need to initialize each lock).
312 void runtime_lock(Lock
*)
313 __asm__(GOSYM_PREFIX
"runtime.lock");
314 void runtime_unlock(Lock
*)
315 __asm__(GOSYM_PREFIX
"runtime.unlock");
318 * sleep and wakeup on one-time events.
319 * before any calls to notesleep or notewakeup,
320 * must call noteclear to initialize the Note.
321 * then, exactly one thread can call notesleep
322 * and exactly one thread can call notewakeup (once).
323 * once notewakeup has been called, the notesleep
324 * will return. future notesleep will return immediately.
325 * subsequent noteclear must be called only after
326 * previous notesleep has returned, e.g. it's disallowed
327 * to call noteclear straight after notewakeup.
329 * notetsleep is like notesleep but wakes up after
330 * a given number of nanoseconds even if the event
331 * has not yet happened. if a goroutine uses notetsleep to
332 * wake up early, it must wait to call noteclear until it
333 * can be sure that no other goroutine is calling
336 * notesleep/notetsleep are generally called on g0,
337 * notetsleepg is similar to notetsleep but is called on user g.
339 void runtime_noteclear(Note
*)
340 __asm__ (GOSYM_PREFIX
"runtime.noteclear");
341 void runtime_notesleep(Note
*)
342 __asm__ (GOSYM_PREFIX
"runtime.notesleep");
343 void runtime_notewakeup(Note
*)
344 __asm__ (GOSYM_PREFIX
"runtime.notewakeup");
345 bool runtime_notetsleep(Note
*, int64
) // false - timeout
346 __asm__ (GOSYM_PREFIX
"runtime.notetsleep");
347 bool runtime_notetsleepg(Note
*, int64
) // false - timeout
348 __asm__ (GOSYM_PREFIX
"runtime.notetsleepg");
353 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
354 #define runtime_getcallerpc() __builtin_return_address(0)
357 void __wrap_rtems_task_variable_add(void **);
363 void reflect_call(const struct functype
*, FuncVal
*, _Bool
, _Bool
,
365 __asm__ (GOSYM_PREFIX
"runtime.reflectcall");
366 void runtime_panic(Eface
)
367 __asm__ (GOSYM_PREFIX
"runtime.gopanic");
368 void runtime_panic(Eface
)
369 __attribute__ ((noreturn
));
372 * runtime c-called (but written in Go)
374 void runtime_newErrorCString(uintptr
, Eface
*)
375 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
378 * wrapped for go users
380 void runtime_procyield(uint32
)
381 __asm__(GOSYM_PREFIX
"runtime.procyield");
382 void runtime_osyield(void)
383 __asm__(GOSYM_PREFIX
"runtime.osyield");
385 uintptr
runtime_memlimit(void);
387 #define ISNAN(f) __builtin_isnan(f)
394 #define runtime_setitimer setitimer
396 void runtime_check(void)
397 __asm__ (GOSYM_PREFIX
"runtime.check");
399 // Size of stack space allocated using Go's allocator.
400 // This will be 0 when using split stacks, as in that case
401 // the stacks are allocated by the splitstack library.
402 extern uintptr runtime_stacks_sys
;
405 * ia64's register file is spilled to a separate stack, the register backing
406 * store, on window overflow, and must also be scanned. This occupies the other
407 * end of the normal stack allocation, growing upwards.
408 * We also need to ensure all register windows are flushed to the backing
409 * store, as unlike SPARC, __builtin_unwind_init doesn't do this on ia64.
412 # define secondary_stack_pointer() __builtin_ia64_bsp()
413 # define initial_secondary_stack_pointer(stack_alloc) (stack_alloc)
414 # define flush_registers_to_secondary_stack() __builtin_ia64_flushrs()
416 # define secondary_stack_pointer() nil
417 # define initial_secondary_stack_pointer(stack_alloc) nil
418 # define flush_registers_to_secondary_stack()
421 struct backtrace_state
;
422 extern struct backtrace_state
*__go_get_backtrace_state(void);
423 extern void __go_syminfo_fnname_callback(void*, uintptr_t, const char*,
424 uintptr_t, uintptr_t);
425 extern void runtime_main(void*)
426 __asm__(GOSYM_PREFIX
"runtime.main");
428 #define PREFETCH(p) __builtin_prefetch(p)
430 void runtime_badsignal(int);
431 Defer
* runtime_newdefer(void);
432 void runtime_freedefer(Defer
*);
434 extern void _cgo_wait_runtime_init_done (void);
435 extern void _cgo_notify_runtime_init_done (void)
436 __asm__ (GOSYM_PREFIX
"runtime.__cgo__notify__runtime__init__done");
437 extern _Bool runtime_iscgo
;
438 extern uintptr __go_end
__attribute__ ((weak
));
439 extern void *getitab(const struct _type
*, const struct _type
*, _Bool
)
440 __asm__ (GOSYM_PREFIX
"runtime.getitab");
442 extern void runtime_cpuinit(void);
443 extern void setRandomNumber(uint32
)
444 __asm__ (GOSYM_PREFIX
"runtime.setRandomNumber");
445 extern void setIsCgo(void)
446 __asm__ (GOSYM_PREFIX
"runtime.setIsCgo");
447 extern void setSupportAES(bool)
448 __asm__ (GOSYM_PREFIX
"runtime.setSupportAES");
449 extern void typedmemmove(const struct _type
*, void *, const void *)
450 __asm__ (GOSYM_PREFIX
"runtime.typedmemmove");
451 extern Sched
* runtime_getsched(void)
452 __asm__ (GOSYM_PREFIX
"runtime.getsched");
454 struct funcfileline_return
462 struct funcfileline_return
463 runtime_funcfileline (uintptr targetpc
, int32 index
, bool more
)
464 __asm__ (GOSYM_PREFIX
"runtime.funcfileline");
467 * helpers for stack scan.
469 bool scanstackwithmap(void*)
470 __asm__(GOSYM_PREFIX
"runtime.scanstackwithmap");
471 bool doscanstack(G
*, void*)
472 __asm__("runtime.doscanstack");
474 extern bool runtime_usestackmaps
;
476 bool probestackmaps(void)
477 __asm__("runtime.probestackmaps");
479 // This is set to non-zero when calling backtrace_full. This is used
480 // to avoid getting hanging on a recursive lock in dl_iterate_phdr on
481 // older versions of glibc when a SIGPROF signal arrives while
482 // collecting a backtrace.
483 extern uint32 __go_runtime_in_callers
;
485 // Cheaper context switch functions. Currently only defined on
487 #if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
491 int __go_getcontext(__go_context_t
*);
492 int __go_setcontext(__go_context_t
*);
493 void __go_makecontext(__go_context_t
*, void (*)(), void*, size_t);
495 #define __go_context_t ucontext_t
496 #define __go_getcontext(c) getcontext(c)
497 #define __go_setcontext(c) setcontext(c)
498 #define __go_makecontext(c, fn, sp, size) \
499 ((c)->uc_stack.ss_sp = sp, (c)->uc_stack.ss_size = size, makecontext(c, fn, 0))
502 // Symbols defined by the linker.
503 extern const char _etext
[] __attribute__ ((weak
));
504 extern const char _edata
[] __attribute__ ((weak
));
506 // Following symbols do not exist on AIX
508 #define __data_start nil
510 #define __bss_start nil
512 extern const char __etext
[] __attribute__ ((weak
));
513 extern const char __data_start
[] __attribute__ ((weak
));
514 extern const char __edata
[] __attribute__ ((weak
));
515 extern const char __bss_start
[] __attribute__ ((weak
));