github.com/ronhuafeng/gofrontend@v0.0.0-20220715151246-ff23266b8bc5/libgo/runtime/runtime.h (about)

     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_mmap mmap
   359  #define runtime_munmap munmap
   360  #define runtime_madvise madvise
   361  #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
   362  #define runtime_getcallerpc() __builtin_return_address(0)
   363  
   364  #ifdef __rtems__
   365  void __wrap_rtems_task_variable_add(void **);
   366  #endif
   367  
   368  /*
   369   * runtime go-called
   370   */
   371  void reflect_call(const struct functype *, FuncVal *, _Bool, _Bool,
   372  		  void **, void **)
   373    __asm__ (GOSYM_PREFIX "runtime.reflectcall");
   374  void runtime_panic(Eface)
   375    __asm__ (GOSYM_PREFIX "runtime.gopanic");
   376  void runtime_panic(Eface)
   377    __attribute__ ((noreturn));
   378  
   379  /*
   380   * runtime c-called (but written in Go)
   381   */
   382  void	runtime_newErrorCString(uintptr, Eface*)
   383       __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
   384  
   385  /*
   386   * wrapped for go users
   387   */
   388  void	runtime_procyield(uint32)
   389    __asm__(GOSYM_PREFIX "runtime.procyield");
   390  void	runtime_osyield(void)
   391    __asm__(GOSYM_PREFIX "runtime.osyield");
   392  
   393  uintptr	runtime_memlimit(void);
   394  
   395  #define ISNAN(f) __builtin_isnan(f)
   396  
   397  enum
   398  {
   399  	UseSpanType = 1,
   400  };
   401  
   402  #define runtime_setitimer setitimer
   403  
   404  void	runtime_check(void)
   405    __asm__ (GOSYM_PREFIX "runtime.check");
   406  
   407  // Size of stack space allocated using Go's allocator.
   408  // This will be 0 when using split stacks, as in that case
   409  // the stacks are allocated by the splitstack library.
   410  extern uintptr runtime_stacks_sys;
   411  
   412  /*
   413   * ia64's register file is spilled to a separate stack, the register backing
   414   * store, on window overflow, and must also be scanned. This occupies the other
   415   * end of the normal stack allocation, growing upwards.
   416   * We also need to ensure all register windows are flushed to the backing
   417   * store, as unlike SPARC, __builtin_unwind_init doesn't do this on ia64.
   418   */
   419  #ifdef __ia64__
   420  # define secondary_stack_pointer() __builtin_ia64_bsp()
   421  # define initial_secondary_stack_pointer(stack_alloc) (stack_alloc)
   422  # define flush_registers_to_secondary_stack() __builtin_ia64_flushrs()
   423  #else
   424  # define secondary_stack_pointer() nil
   425  # define initial_secondary_stack_pointer(stack_alloc) nil
   426  # define flush_registers_to_secondary_stack()
   427  #endif
   428  
   429  struct backtrace_state;
   430  extern struct backtrace_state *__go_get_backtrace_state(void);
   431  extern void __go_syminfo_fnname_callback(void*, uintptr_t, const char*,
   432  					 uintptr_t, uintptr_t);
   433  extern void runtime_main(void*)
   434    __asm__(GOSYM_PREFIX "runtime.main");
   435  
   436  #define PREFETCH(p) __builtin_prefetch(p)
   437  
   438  void	runtime_badsignal(int);
   439  Defer*	runtime_newdefer(void);
   440  void	runtime_freedefer(Defer*);
   441  
   442  extern void _cgo_wait_runtime_init_done (void);
   443  extern void _cgo_notify_runtime_init_done (void)
   444    __asm__ (GOSYM_PREFIX "runtime.__cgo__notify__runtime__init__done");
   445  extern _Bool runtime_iscgo;
   446  extern uintptr __go_end __attribute__ ((weak));
   447  extern void *getitab(const struct _type *, const struct _type *, _Bool)
   448    __asm__ (GOSYM_PREFIX "runtime.getitab");
   449  
   450  extern void runtime_cpuinit(void);
   451  extern void setRandomNumber(uint32)
   452    __asm__ (GOSYM_PREFIX "runtime.setRandomNumber");
   453  extern void setIsCgo(void)
   454    __asm__ (GOSYM_PREFIX "runtime.setIsCgo");
   455  extern void setSupportAES(bool)
   456    __asm__ (GOSYM_PREFIX "runtime.setSupportAES");
   457  extern void typedmemmove(const struct _type *, void *, const void *)
   458    __asm__ (GOSYM_PREFIX "runtime.typedmemmove");
   459  extern Sched* runtime_getsched(void)
   460    __asm__ (GOSYM_PREFIX "runtime.getsched");
   461  
   462  struct funcfileline_return
   463  {
   464    String retfn;
   465    String retfile;
   466    intgo retline;
   467    intgo retframes;
   468  };
   469  
   470  struct funcfileline_return
   471  runtime_funcfileline (uintptr targetpc, int32 index, bool more)
   472    __asm__ (GOSYM_PREFIX "runtime.funcfileline");
   473  
   474  /*
   475   * helpers for stack scan.
   476   */
   477  bool scanstackwithmap(void*)
   478    __asm__(GOSYM_PREFIX "runtime.scanstackwithmap");
   479  bool doscanstack(G*, void*)
   480    __asm__("runtime.doscanstack");
   481  
   482  extern bool runtime_usestackmaps;
   483  
   484  bool probestackmaps(void)
   485    __asm__("runtime.probestackmaps");
   486  
   487  // This is set to non-zero when calling backtrace_full.  This is used
   488  // to avoid getting hanging on a recursive lock in dl_iterate_phdr on
   489  // older versions of glibc when a SIGPROF signal arrives while
   490  // collecting a backtrace.
   491  extern uint32 __go_runtime_in_callers;
   492  
   493  // Cheaper context switch functions.  Currently only defined on
   494  // Linux/AMD64.
   495  #if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
   496  typedef struct {
   497  	uint64 regs[8];
   498  } __go_context_t;
   499  int __go_getcontext(__go_context_t*);
   500  int __go_setcontext(__go_context_t*);
   501  void __go_makecontext(__go_context_t*, void (*)(), void*, size_t);
   502  #else
   503  #define __go_context_t	ucontext_t
   504  #define __go_getcontext(c)	getcontext(c)
   505  #define __go_setcontext(c)	setcontext(c)
   506  #define __go_makecontext(c, fn, sp, size) \
   507  	((c)->uc_stack.ss_sp = sp, (c)->uc_stack.ss_size = size, makecontext(c, fn, 0))
   508  #endif
   509  
   510  // Symbols defined by the linker.
   511  extern const char _etext[] __attribute__ ((weak));
   512  extern const char _edata[] __attribute__ ((weak));
   513  #ifdef _AIX
   514  // Following symbols do not exist on AIX
   515  #define __etext nil
   516  #define __data_start nil
   517  #define __edata nil
   518  #define __bss_start nil
   519  #else
   520  extern const char __etext[] __attribute__ ((weak));
   521  extern const char __data_start[] __attribute__ ((weak));
   522  extern const char __edata[] __attribute__ ((weak));
   523  extern const char __bss_start[] __attribute__ ((weak));
   524  #endif