(root)/
gcc-13.2.0/
libgo/
runtime/
runtime.h
       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.
       4  
       5  #include "config.h"
       6  
       7  #include "go-assert.h"
       8  #include <complex.h>
       9  #include <signal.h>
      10  #include <stdint.h>
      11  #include <stdio.h>
      12  #include <stdlib.h>
      13  #include <string.h>
      14  #include <sys/types.h>
      15  #include <sys/stat.h>
      16  #include <fcntl.h>
      17  #include <unistd.h>
      18  #include <pthread.h>
      19  #include <semaphore.h>
      20  #include <ucontext.h>
      21  
      22  #ifdef HAVE_SYS_MMAN_H
      23  #include <sys/mman.h>
      24  #endif
      25  
      26  #define _STRINGIFY2_(x) #x
      27  #define _STRINGIFY_(x) _STRINGIFY2_(x)
      28  #define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__)
      29  
      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
      32     of the code.  */
      33  
      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)));
      46  
      47  typedef intptr		intgo; // Go's int
      48  typedef uintptr		uintgo; // Go's uint
      49  
      50  typedef uintptr		uintreg;
      51  
      52  /* Defined types.  */
      53  
      54  typedef	_Bool			bool;
      55  typedef	uint8			byte;
      56  typedef	struct	g		G;
      57  typedef	struct	mutex		Lock;
      58  typedef	struct	m		M;
      59  typedef	struct	p		P;
      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;
      71  
      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;
      77  
      78  typedef struct  tracebackg	Traceback;
      79  
      80  typedef struct	location	Location;
      81  
      82  struct String
      83  {
      84  	const byte*	str;
      85  	intgo		len;
      86  };
      87  
      88  struct FuncVal
      89  {
      90  	uintptr_t fn;
      91  	// variable-size, fn-specific data here
      92  };
      93  
      94  // Type structs will be defined by runtime.inc.
      95  struct _type;
      96  struct functype;
      97  
      98  #include "array.h"
      99  
     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
     104  
     105  #include "runtime.inc"
     106  
     107  #undef timeval
     108  #undef timespec
     109  
     110  /*
     111   * Per-CPU declaration.
     112   */
     113  extern M*	runtime_m(void);
     114  extern G*	runtime_g(void)
     115    __asm__(GOSYM_PREFIX "runtime.getg");
     116  
     117  enum
     118  {
     119  	true	= 1,
     120  	false	= 0,
     121  };
     122  enum
     123  {
     124  	PtrSize = sizeof(void*),
     125  };
     126  enum
     127  {
     128  	// Per-M stack segment cache size.
     129  	StackCacheSize = 32,
     130  	// Global <-> per-M stack segment cache transfer batch size.
     131  	StackCacheBatch = 16,
     132  };
     133  
     134  struct	SigTab
     135  {
     136  	int32	sig;
     137  	int32	flags;
     138  	void*   fwdsig;
     139  };
     140  
     141  #ifdef GOOS_nacl
     142  enum {
     143     NaCl = 1,
     144  };
     145  #else
     146  enum {
     147     NaCl = 0,
     148  };
     149  #endif
     150  
     151  #ifdef GOOS_windows
     152  enum {
     153     Windows = 1
     154  };
     155  #else
     156  enum {
     157     Windows = 0
     158  };
     159  #endif
     160  #ifdef GOOS_solaris
     161  enum {
     162     Solaris = 1
     163  };
     164  #else
     165  enum {
     166     Solaris = 0
     167  };
     168  #endif
     169  
     170  extern bool runtime_copystack;
     171  
     172  /*
     173   * defined macros
     174   *    you need super-gopher-guru privilege
     175   *    to add this list.
     176   */
     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 */
     181  
     182  enum {
     183  	// hashinit wants this many random bytes
     184  	HashRandomBytes = 32
     185  };
     186  void	runtime_hashinit(void);
     187  
     188  /*
     189   * external data
     190   */
     191  extern	uintptr* runtime_getZerobase(void)
     192    __asm__(GOSYM_PREFIX "runtime.getZerobase");
     193  
     194  extern	bool	runtime_isstarted;
     195  extern	bool	runtime_isarchive;
     196  
     197  extern	void	panicmem(void) __asm__ (GOSYM_PREFIX "runtime.panicmem");
     198  
     199  /*
     200   * common functions and data
     201   */
     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");
     207  
     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  void runtime_memmove(void*, void*, uint64)
     225    __asm__ (GOSYM_PREFIX "runtime.memmove");
     226  String	runtime_gostringnocopy(const byte*)
     227    __asm__ (GOSYM_PREFIX "runtime.gostringnocopy");
     228  void	runtime_ginit(void)
     229    __asm__ (GOSYM_PREFIX "runtime.ginit");
     230  void	runtime_schedinit(void)
     231    __asm__ (GOSYM_PREFIX "runtime.schedinit");
     232  void	runtime_initsig(bool)
     233    __asm__ (GOSYM_PREFIX "runtime.initsig");
     234  #define runtime_open(p, f, m) open((p), (f), (m))
     235  #define runtime_read(d, v, n) read((d), (v), (n))
     236  #define runtime_write(d, v, n) write((d), (v), (n))
     237  #define runtime_close(d) close(d)
     238  void	runtime_ready(G*, intgo, bool)
     239    __asm__ (GOSYM_PREFIX "runtime.ready");
     240  String	runtime_getenv(const char*);
     241  int32	runtime_atoi(const byte*, intgo);
     242  void*	runtime_mstart(void*);
     243  G*	runtime_malg(bool, bool, byte**, uintptr*)
     244  	__asm__(GOSYM_PREFIX "runtime.malg");
     245  void	runtime_minit(void)
     246    __asm__ (GOSYM_PREFIX "runtime.minit");
     247  void	runtime_signalstack(byte*, uintptr)
     248    __asm__ (GOSYM_PREFIX "runtime.signalstack");
     249  void	runtime_mallocinit(void)
     250    __asm__ (GOSYM_PREFIX "runtime.mallocinit");
     251  void*	runtime_mallocgc(uintptr, const struct _type*, bool)
     252    __asm__ (GOSYM_PREFIX "runtime.mallocgc");
     253  void*	runtime_sysAlloc(uintptr, uint64*)
     254    __asm__ (GOSYM_PREFIX "runtime.sysAlloc");
     255  void	runtime_sysFree(void*, uintptr, uint64*)
     256    __asm__ (GOSYM_PREFIX "runtime.sysFree");
     257  void	runtime_mprofinit(void);
     258  #define runtime_getcallersp() __builtin_dwarf_cfa()
     259  void	runtime_mcall(FuncVal*)
     260    __asm__ (GOSYM_PREFIX "runtime.mcall");
     261  int32	runtime_timediv(int64, int32, int32*)
     262    __asm__ (GOSYM_PREFIX "runtime.timediv");
     263  int32	runtime_round2(int32 x); // round x up to a power of 2.
     264  
     265  // atomic operations
     266  #define runtime_xadd(p, v) __atomic_add_fetch (p, v, __ATOMIC_SEQ_CST)
     267  #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
     268  
     269  void runtime_setg(G*)
     270    __asm__ (GOSYM_PREFIX "runtime.setg");
     271  void runtime_newextram(void)
     272    __asm__ (GOSYM_PREFIX "runtime.newextram");
     273  #define runtime_exit(s) exit(s)
     274  void	runtime_gosched(void)
     275    __asm__ (GOSYM_PREFIX "runtime.Gosched");
     276  void	runtime_schedtrace(bool)
     277    __asm__ (GOSYM_PREFIX "runtime.schedtrace");
     278  void	runtime_goparkunlock(Lock*, String, byte, intgo)
     279    __asm__ (GOSYM_PREFIX "runtime.goparkunlock");
     280  void	runtime_tsleep(int64, const char*);
     281  void	runtime_entersyscall()
     282    __asm__ (GOSYM_PREFIX "runtime.entersyscall");
     283  void	runtime_entersyscallblock()
     284    __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
     285  G*	__go_go(uintptr, void*);
     286  int32	runtime_callers(int32, Location*, int32, bool keep_callers);
     287  struct callers_data;
     288  bool	runtime_skipInCallback(const char *, struct callers_data *)
     289    __asm__ (GOSYM_PREFIX "runtime.skipInCallback");
     290  int64	runtime_nanotime1(void)	// monotonic time
     291    __asm__(GOSYM_PREFIX "runtime.nanotime1");
     292  void	runtime_dopanic(int32) __attribute__ ((noreturn));
     293  void	runtime_startpanic(void)
     294    __asm__ (GOSYM_PREFIX "runtime.startpanic");
     295  void	runtime_unwindstack(G*, byte*);
     296  void	runtime_usleep(uint32)
     297       __asm__ (GOSYM_PREFIX "runtime.usleep");
     298  int64	runtime_cputicks(void)
     299       __asm__ (GOSYM_PREFIX "runtime.cputicks");
     300  int64	runtime_tickspersecond(void)
     301       __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
     302  void	runtime_blockevent(int64, int32);
     303  extern int64 runtime_blockprofilerate;
     304  G*	runtime_netpoll(bool)
     305    __asm__ (GOSYM_PREFIX "runtime.netpoll");
     306  void	runtime_parsedebugvars(void)
     307    __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
     308  void	_rt0_go(void);
     309  G*	runtime_timejump(void);
     310  
     311  /*
     312   * mutual exclusion locks.  in the uncontended case,
     313   * as fast as spin locks (just a few user-level instructions),
     314   * but on the contention path they sleep in the kernel.
     315   * a zeroed Lock is unlocked (no need to initialize each lock).
     316   */
     317  void	runtime_lock(Lock*)
     318    __asm__(GOSYM_PREFIX "runtime.lock");
     319  void	runtime_unlock(Lock*)
     320    __asm__(GOSYM_PREFIX "runtime.unlock");
     321  
     322  /*
     323   * sleep and wakeup on one-time events.
     324   * before any calls to notesleep or notewakeup,
     325   * must call noteclear to initialize the Note.
     326   * then, exactly one thread can call notesleep
     327   * and exactly one thread can call notewakeup (once).
     328   * once notewakeup has been called, the notesleep
     329   * will return.  future notesleep will return immediately.
     330   * subsequent noteclear must be called only after
     331   * previous notesleep has returned, e.g. it's disallowed
     332   * to call noteclear straight after notewakeup.
     333   *
     334   * notetsleep is like notesleep but wakes up after
     335   * a given number of nanoseconds even if the event
     336   * has not yet happened.  if a goroutine uses notetsleep to
     337   * wake up early, it must wait to call noteclear until it
     338   * can be sure that no other goroutine is calling
     339   * notewakeup.
     340   *
     341   * notesleep/notetsleep are generally called on g0,
     342   * notetsleepg is similar to notetsleep but is called on user g.
     343   */
     344  void	runtime_noteclear(Note*)
     345    __asm__ (GOSYM_PREFIX "runtime.noteclear");
     346  void	runtime_notesleep(Note*)
     347    __asm__ (GOSYM_PREFIX "runtime.notesleep");
     348  void	runtime_notewakeup(Note*)
     349    __asm__ (GOSYM_PREFIX "runtime.notewakeup");
     350  bool	runtime_notetsleep(Note*, int64)  // false - timeout
     351    __asm__ (GOSYM_PREFIX "runtime.notetsleep");
     352  bool	runtime_notetsleepg(Note*, int64)  // false - timeout
     353    __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
     354  
     355  /*
     356   * low level C-called
     357   */
     358  #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
     359  #define runtime_getcallerpc() __builtin_return_address(0)
     360  
     361  #ifdef __rtems__
     362  void __wrap_rtems_task_variable_add(void **);
     363  #endif
     364  
     365  /*
     366   * runtime go-called
     367   */
     368  void reflect_call(const struct functype *, FuncVal *, _Bool, _Bool,
     369  		  void **, void **)
     370    __asm__ (GOSYM_PREFIX "runtime.reflectcall");
     371  void runtime_panic(Eface)
     372    __asm__ (GOSYM_PREFIX "runtime.gopanic");
     373  void runtime_panic(Eface)
     374    __attribute__ ((noreturn));
     375  
     376  /*
     377   * runtime c-called (but written in Go)
     378   */
     379  void	runtime_newErrorCString(uintptr, Eface*)
     380       __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
     381  
     382  /*
     383   * wrapped for go users
     384   */
     385  void	runtime_procyield(uint32)
     386    __asm__(GOSYM_PREFIX "runtime.procyield");
     387  void	runtime_osyield(void)
     388    __asm__(GOSYM_PREFIX "runtime.osyield");
     389  
     390  uintptr	runtime_memlimit(void);
     391  
     392  #define ISNAN(f) __builtin_isnan(f)
     393  
     394  enum
     395  {
     396  	UseSpanType = 1,
     397  };
     398  
     399  #define runtime_setitimer setitimer
     400  
     401  void	runtime_check(void)
     402    __asm__ (GOSYM_PREFIX "runtime.check");
     403  
     404  // Size of stack space allocated using Go's allocator.
     405  // This will be 0 when using split stacks, as in that case
     406  // the stacks are allocated by the splitstack library.
     407  extern uintptr runtime_stacks_sys;
     408  
     409  /*
     410   * ia64's register file is spilled to a separate stack, the register backing
     411   * store, on window overflow, and must also be scanned. This occupies the other
     412   * end of the normal stack allocation, growing upwards.
     413   * We also need to ensure all register windows are flushed to the backing
     414   * store, as unlike SPARC, __builtin_unwind_init doesn't do this on ia64.
     415   */
     416  #ifdef __ia64__
     417  # define secondary_stack_pointer() __builtin_ia64_bsp()
     418  # define initial_secondary_stack_pointer(stack_alloc) (stack_alloc)
     419  # define flush_registers_to_secondary_stack() __builtin_ia64_flushrs()
     420  #else
     421  # define secondary_stack_pointer() nil
     422  # define initial_secondary_stack_pointer(stack_alloc) nil
     423  # define flush_registers_to_secondary_stack()
     424  #endif
     425  
     426  struct backtrace_state;
     427  extern struct backtrace_state *__go_get_backtrace_state(void);
     428  extern void __go_syminfo_fnname_callback(void*, uintptr_t, const char*,
     429  					 uintptr_t, uintptr_t);
     430  extern void runtime_main(void*)
     431    __asm__(GOSYM_PREFIX "runtime.main");
     432  
     433  #define PREFETCH(p) __builtin_prefetch(p)
     434  
     435  void	runtime_badsignal(int);
     436  Defer*	runtime_newdefer(void);
     437  void	runtime_freedefer(Defer*);
     438  
     439  extern void _cgo_wait_runtime_init_done (void);
     440  extern void _cgo_notify_runtime_init_done (void)
     441    __asm__ (GOSYM_PREFIX "runtime.__cgo__notify__runtime__init__done");
     442  extern _Bool runtime_iscgo;
     443  extern uintptr __go_end __attribute__ ((weak));
     444  extern void *getitab(const struct _type *, const struct _type *, _Bool)
     445    __asm__ (GOSYM_PREFIX "runtime.getitab");
     446  
     447  extern void runtime_cpuinit(void);
     448  extern void setRandomNumber(uint32)
     449    __asm__ (GOSYM_PREFIX "runtime.setRandomNumber");
     450  extern void setIsCgo(void)
     451    __asm__ (GOSYM_PREFIX "runtime.setIsCgo");
     452  extern void setSupportAES(bool)
     453    __asm__ (GOSYM_PREFIX "runtime.setSupportAES");
     454  extern void typedmemmove(const struct _type *, void *, const void *)
     455    __asm__ (GOSYM_PREFIX "runtime.typedmemmove");
     456  extern Sched* runtime_getsched(void)
     457    __asm__ (GOSYM_PREFIX "runtime.getsched");
     458  
     459  struct funcfileline_return
     460  {
     461    String retfn;
     462    String retfile;
     463    intgo retline;
     464    intgo retframes;
     465  };
     466  
     467  struct funcfileline_return
     468  runtime_funcfileline (uintptr targetpc, int32 index, bool more)
     469    __asm__ (GOSYM_PREFIX "runtime.funcfileline");
     470  
     471  /*
     472   * helpers for stack scan.
     473   */
     474  bool scanstackwithmap(void*)
     475    __asm__(GOSYM_PREFIX "runtime.scanstackwithmap");
     476  bool doscanstack(G*, void*)
     477    __asm__("runtime.doscanstack");
     478  
     479  extern bool runtime_usestackmaps;
     480  
     481  bool probestackmaps(void)
     482    __asm__("runtime.probestackmaps");
     483  
     484  // This is set to non-zero when calling backtrace_full.  This is used
     485  // to avoid getting hanging on a recursive lock in dl_iterate_phdr on
     486  // older versions of glibc when a SIGPROF signal arrives while
     487  // collecting a backtrace.
     488  extern uint32 __go_runtime_in_callers;
     489  
     490  // Cheaper context switch functions.  Currently only defined on
     491  // Linux/AMD64.
     492  #if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
     493  typedef struct {
     494  	uint64 regs[8];
     495  } __go_context_t;
     496  int __go_getcontext(__go_context_t*);
     497  int __go_setcontext(__go_context_t*);
     498  void __go_makecontext(__go_context_t*, void (*)(), void*, size_t);
     499  #else
     500  #define __go_context_t	ucontext_t
     501  #define __go_getcontext(c)	getcontext(c)
     502  #define __go_setcontext(c)	setcontext(c)
     503  #define __go_makecontext(c, fn, sp, size) \
     504  	((c)->uc_stack.ss_sp = sp, (c)->uc_stack.ss_size = size, makecontext(c, fn, 0))
     505  #endif
     506  
     507  // Symbols defined by the linker.
     508  extern const char _etext[] __attribute__ ((weak));
     509  extern const char _edata[] __attribute__ ((weak));
     510  #ifdef _AIX
     511  // Following symbols do not exist on AIX
     512  #define __etext nil
     513  #define __data_start nil
     514  #define __edata nil
     515  #define __bss_start nil
     516  #else
     517  extern const char __etext[] __attribute__ ((weak));
     518  extern const char __data_start[] __attribute__ ((weak));
     519  extern const char __edata[] __attribute__ ((weak));
     520  extern const char __bss_start[] __attribute__ ((weak));
     521  #endif