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 _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");
124 PtrSize
= sizeof(void*),
128 // Per-M stack segment cache size.
130 // Global <-> per-M stack segment cache transfer batch size.
131 StackCacheBatch
= 16,
170 extern bool runtime_copystack
;
174 * you need super-gopher-guru privilege
177 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
178 #define nil ((void*)0)
179 #define USED(v) ((void) v)
180 #define ROUND(x, n) (((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
183 // hashinit wants this many random bytes
186 void runtime_hashinit(void);
191 extern uintptr
* runtime_getZerobase(void)
192 __asm__(GOSYM_PREFIX
"runtime.getZerobase");
194 extern bool runtime_isstarted
;
195 extern bool runtime_isarchive
;
197 extern void panicmem(void) __asm__ (GOSYM_PREFIX
"runtime.panicmem");
200 * common functions and data
202 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
203 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
204 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
205 intgo
runtime_findnull(const byte
*)
206 __asm__ (GOSYM_PREFIX
"runtime.findnull");
208 void runtime_gogo(G
*)
209 __asm__ (GOSYM_PREFIX
"runtime.gogo");
210 void runtime_args(int32
, byte
**)
211 __asm__ (GOSYM_PREFIX
"runtime.args");
212 void runtime_osinit(void)
213 __asm__ (GOSYM_PREFIX
"runtime.osinit");
214 void runtime_alginit(void)
215 __asm__ (GOSYM_PREFIX
"runtime.alginit");
216 void runtime_goargs(void)
217 __asm__ (GOSYM_PREFIX
"runtime.goargs");
218 void runtime_throw(const char*) __attribute__ ((noreturn
));
219 void runtime_panicstring(const char*) __attribute__ ((noreturn
));
220 bool runtime_canpanic(G
*);
221 void runtime_printf(const char*, ...);
222 int32
runtime_snprintf(byte
*, int32
, const char*, ...);
223 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
224 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
225 String
runtime_gostringnocopy(const byte
*)
226 __asm__ (GOSYM_PREFIX
"runtime.gostringnocopy");
227 void runtime_ginit(void)
228 __asm__ (GOSYM_PREFIX
"runtime.ginit");
229 void runtime_schedinit(void)
230 __asm__ (GOSYM_PREFIX
"runtime.schedinit");
231 void runtime_initsig(bool)
232 __asm__ (GOSYM_PREFIX
"runtime.initsig");
233 #define runtime_open(p, f, m) open((p), (f), (m))
234 #define runtime_read(d, v, n) read((d), (v), (n))
235 #define runtime_write(d, v, n) write((d), (v), (n))
236 #define runtime_close(d) close(d)
237 void runtime_ready(G
*, intgo
, bool)
238 __asm__ (GOSYM_PREFIX
"runtime.ready");
239 String
runtime_getenv(const char*);
240 int32
runtime_atoi(const byte
*, intgo
);
241 void* runtime_mstart(void*);
242 G
* runtime_malg(bool, bool, byte
**, uintptr
*)
243 __asm__(GOSYM_PREFIX
"runtime.malg");
244 void runtime_minit(void)
245 __asm__ (GOSYM_PREFIX
"runtime.minit");
246 void runtime_signalstack(byte
*, uintptr
)
247 __asm__ (GOSYM_PREFIX
"runtime.signalstack");
248 void runtime_mallocinit(void)
249 __asm__ (GOSYM_PREFIX
"runtime.mallocinit");
250 void* runtime_mallocgc(uintptr
, const struct _type
*, bool)
251 __asm__ (GOSYM_PREFIX
"runtime.mallocgc");
252 void* runtime_sysAlloc(uintptr
, uint64
*)
253 __asm__ (GOSYM_PREFIX
"runtime.sysAlloc");
254 void runtime_sysFree(void*, uintptr
, uint64
*)
255 __asm__ (GOSYM_PREFIX
"runtime.sysFree");
256 void runtime_mprofinit(void);
257 #define runtime_getcallersp() __builtin_dwarf_cfa()
258 void runtime_mcall(FuncVal
*)
259 __asm__ (GOSYM_PREFIX
"runtime.mcall");
260 int32
runtime_timediv(int64
, int32
, int32
*)
261 __asm__ (GOSYM_PREFIX
"runtime.timediv");
262 int32
runtime_round2(int32 x
); // round x up to a power of 2.
265 #define runtime_xadd(p, v) __atomic_add_fetch (p, v, __ATOMIC_SEQ_CST)
266 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
268 void runtime_setg(G
*)
269 __asm__ (GOSYM_PREFIX
"runtime.setg");
270 void runtime_newextram(void)
271 __asm__ (GOSYM_PREFIX
"runtime.newextram");
272 #define runtime_exit(s) exit(s)
273 void runtime_gosched(void)
274 __asm__ (GOSYM_PREFIX
"runtime.Gosched");
275 void runtime_schedtrace(bool)
276 __asm__ (GOSYM_PREFIX
"runtime.schedtrace");
277 void runtime_goparkunlock(Lock
*, String
, byte
, intgo
)
278 __asm__ (GOSYM_PREFIX
"runtime.goparkunlock");
279 void runtime_tsleep(int64
, const char*);
280 void runtime_entersyscall()
281 __asm__ (GOSYM_PREFIX
"runtime.entersyscall");
282 void runtime_entersyscallblock()
283 __asm__ (GOSYM_PREFIX
"runtime.entersyscallblock");
284 G
* __go_go(uintptr
, void*);
285 int32
runtime_callers(int32
, Location
*, int32
, bool keep_callers
);
287 bool runtime_skipInCallback(const char *, struct callers_data
*)
288 __asm__ (GOSYM_PREFIX
"runtime.skipInCallback");
289 int64
runtime_nanotime1(void) // monotonic time
290 __asm__(GOSYM_PREFIX
"runtime.nanotime1");
291 void runtime_dopanic(int32
) __attribute__ ((noreturn
));
292 void runtime_startpanic(void)
293 __asm__ (GOSYM_PREFIX
"runtime.startpanic");
294 void runtime_unwindstack(G
*, byte
*);
295 void runtime_usleep(uint32
)
296 __asm__ (GOSYM_PREFIX
"runtime.usleep");
297 int64
runtime_cputicks(void)
298 __asm__ (GOSYM_PREFIX
"runtime.cputicks");
299 int64
runtime_tickspersecond(void)
300 __asm__ (GOSYM_PREFIX
"runtime.tickspersecond");
301 void runtime_blockevent(int64
, int32
);
302 extern int64 runtime_blockprofilerate
;
303 G
* runtime_netpoll(bool)
304 __asm__ (GOSYM_PREFIX
"runtime.netpoll");
305 void runtime_parsedebugvars(void)
306 __asm__(GOSYM_PREFIX
"runtime.parsedebugvars");
308 G
* runtime_timejump(void);
311 * mutual exclusion locks. in the uncontended case,
312 * as fast as spin locks (just a few user-level instructions),
313 * but on the contention path they sleep in the kernel.
314 * a zeroed Lock is unlocked (no need to initialize each lock).
316 void runtime_lock(Lock
*)
317 __asm__(GOSYM_PREFIX
"runtime.lock");
318 void runtime_unlock(Lock
*)
319 __asm__(GOSYM_PREFIX
"runtime.unlock");
322 * sleep and wakeup on one-time events.
323 * before any calls to notesleep or notewakeup,
324 * must call noteclear to initialize the Note.
325 * then, exactly one thread can call notesleep
326 * and exactly one thread can call notewakeup (once).
327 * once notewakeup has been called, the notesleep
328 * will return. future notesleep will return immediately.
329 * subsequent noteclear must be called only after
330 * previous notesleep has returned, e.g. it's disallowed
331 * to call noteclear straight after notewakeup.
333 * notetsleep is like notesleep but wakes up after
334 * a given number of nanoseconds even if the event
335 * has not yet happened. if a goroutine uses notetsleep to
336 * wake up early, it must wait to call noteclear until it
337 * can be sure that no other goroutine is calling
340 * notesleep/notetsleep are generally called on g0,
341 * notetsleepg is similar to notetsleep but is called on user g.
343 void runtime_noteclear(Note
*)
344 __asm__ (GOSYM_PREFIX
"runtime.noteclear");
345 void runtime_notesleep(Note
*)
346 __asm__ (GOSYM_PREFIX
"runtime.notesleep");
347 void runtime_notewakeup(Note
*)
348 __asm__ (GOSYM_PREFIX
"runtime.notewakeup");
349 bool runtime_notetsleep(Note
*, int64
) // false - timeout
350 __asm__ (GOSYM_PREFIX
"runtime.notetsleep");
351 bool runtime_notetsleepg(Note
*, int64
) // false - timeout
352 __asm__ (GOSYM_PREFIX
"runtime.notetsleepg");
357 #define runtime_mmap mmap
358 #define runtime_munmap munmap
359 #define runtime_madvise madvise
360 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
361 #define runtime_getcallerpc() __builtin_return_address(0)
364 void __wrap_rtems_task_variable_add(void **);
370 void reflect_call(const struct functype
*, FuncVal
*, _Bool
, _Bool
,
372 __asm__ (GOSYM_PREFIX
"runtime.reflectcall");
373 void runtime_panic(Eface
)
374 __asm__ (GOSYM_PREFIX
"runtime.gopanic");
375 void runtime_panic(Eface
)
376 __attribute__ ((noreturn
));
379 * runtime c-called (but written in Go)
381 void runtime_newErrorCString(uintptr
, Eface
*)
382 __asm__ (GOSYM_PREFIX
"runtime.NewErrorCString");
385 * wrapped for go users
387 void runtime_procyield(uint32
)
388 __asm__(GOSYM_PREFIX
"runtime.procyield");
389 void runtime_osyield(void)
390 __asm__(GOSYM_PREFIX
"runtime.osyield");
392 uintptr
runtime_memlimit(void);
394 #define ISNAN(f) __builtin_isnan(f)
401 #define runtime_setitimer setitimer
403 void runtime_check(void)
404 __asm__ (GOSYM_PREFIX
"runtime.check");
406 // Size of stack space allocated using Go's allocator.
407 // This will be 0 when using split stacks, as in that case
408 // the stacks are allocated by the splitstack library.
409 extern uintptr runtime_stacks_sys
;
412 * ia64's register file is spilled to a separate stack, the register backing
413 * store, on window overflow, and must also be scanned. This occupies the other
414 * end of the normal stack allocation, growing upwards.
415 * We also need to ensure all register windows are flushed to the backing
416 * store, as unlike SPARC, __builtin_unwind_init doesn't do this on ia64.
419 # define secondary_stack_pointer() __builtin_ia64_bsp()
420 # define initial_secondary_stack_pointer(stack_alloc) (stack_alloc)
421 # define flush_registers_to_secondary_stack() __builtin_ia64_flushrs()
423 # define secondary_stack_pointer() nil
424 # define initial_secondary_stack_pointer(stack_alloc) nil
425 # define flush_registers_to_secondary_stack()
428 struct backtrace_state
;
429 extern struct backtrace_state
*__go_get_backtrace_state(void);
430 extern void __go_syminfo_fnname_callback(void*, uintptr_t, const char*,
431 uintptr_t, uintptr_t);
432 extern void runtime_main(void*)
433 __asm__(GOSYM_PREFIX
"runtime.main");
435 #define PREFETCH(p) __builtin_prefetch(p)
437 void runtime_badsignal(int);
438 Defer
* runtime_newdefer(void);
439 void runtime_freedefer(Defer
*);
441 extern void _cgo_wait_runtime_init_done (void);
442 extern void _cgo_notify_runtime_init_done (void)
443 __asm__ (GOSYM_PREFIX
"runtime._cgo_notify_runtime_init_done");
444 extern _Bool runtime_iscgo
;
445 extern uintptr __go_end
__attribute__ ((weak
));
446 extern void *getitab(const struct _type
*, const struct _type
*, _Bool
)
447 __asm__ (GOSYM_PREFIX
"runtime.getitab");
449 extern void runtime_cpuinit(void);
450 extern void setRandomNumber(uint32
)
451 __asm__ (GOSYM_PREFIX
"runtime.setRandomNumber");
452 extern void setIsCgo(void)
453 __asm__ (GOSYM_PREFIX
"runtime.setIsCgo");
454 extern void setSupportAES(bool)
455 __asm__ (GOSYM_PREFIX
"runtime.setSupportAES");
456 extern void typedmemmove(const struct _type
*, void *, const void *)
457 __asm__ (GOSYM_PREFIX
"runtime.typedmemmove");
458 extern Sched
* runtime_getsched(void)
459 __asm__ (GOSYM_PREFIX
"runtime.getsched");
461 struct funcfileline_return
469 struct funcfileline_return
470 runtime_funcfileline (uintptr targetpc
, int32 index
, bool more
)
471 __asm__ (GOSYM_PREFIX
"runtime.funcfileline");
474 * helpers for stack scan.
476 bool scanstackwithmap(void*)
477 __asm__(GOSYM_PREFIX
"runtime.scanstackwithmap");
478 bool doscanstack(G
*, void*)
479 __asm__("runtime.doscanstack");
481 extern bool runtime_usestackmaps
;
483 bool probestackmaps(void)
484 __asm__("runtime.probestackmaps");
486 // This is set to non-zero when calling backtrace_full. This is used
487 // to avoid getting hanging on a recursive lock in dl_iterate_phdr on
488 // older versions of glibc when a SIGPROF signal arrives while
489 // collecting a backtrace.
490 extern uint32 __go_runtime_in_callers
;
492 // Cheaper context switch functions. Currently only defined on
494 #if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
498 int __go_getcontext(__go_context_t
*);
499 int __go_setcontext(__go_context_t
*);
500 void __go_makecontext(__go_context_t
*, void (*)(), void*, size_t);
502 #define __go_context_t ucontext_t
503 #define __go_getcontext(c) getcontext(c)
504 #define __go_setcontext(c) setcontext(c)
505 #define __go_makecontext(c, fn, sp, size) \
506 ((c)->uc_stack.ss_sp = sp, (c)->uc_stack.ss_size = size, makecontext(c, fn, 0))
509 // Symbols defined by the linker.
510 extern const char _etext
[] __attribute__ ((weak
));
511 extern const char _edata
[] __attribute__ ((weak
));
513 // Following symbols do not exist on AIX
515 #define __data_start nil
517 #define __bss_start nil
519 extern const char __etext
[] __attribute__ ((weak
));
520 extern const char __data_start
[] __attribute__ ((weak
));
521 extern const char __edata
[] __attribute__ ((weak
));
522 extern const char __bss_start
[] __attribute__ ((weak
));