github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/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  #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.
   263  
   264  // atomic operations
   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)
   267  
   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);
   286  struct callers_data;
   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");
   307  void	_rt0_go(void);
   308  G*	runtime_timejump(void);
   309  
   310  /*
   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).
   315   */
   316  void	runtime_lock(Lock*)
   317    __asm__(GOSYM_PREFIX "runtime.lock");
   318  void	runtime_unlock(Lock*)
   319    __asm__(GOSYM_PREFIX "runtime.unlock");
   320  
   321  /*
   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.
   332   *
   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
   338   * notewakeup.
   339   *
   340   * notesleep/notetsleep are generally called on g0,
   341   * notetsleepg is similar to notetsleep but is called on user g.
   342   */
   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");
   353  
   354  /*
   355   * low level C-called
   356   */
   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)
   362  
   363  #ifdef __rtems__
   364  void __wrap_rtems_task_variable_add(void **);
   365  #endif
   366  
   367  /*
   368   * runtime go-called
   369   */
   370  void reflect_call(const struct functype *, FuncVal *, _Bool, _Bool,
   371  		  void **, void **)
   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));
   377  
   378  /*
   379   * runtime c-called (but written in Go)
   380   */
   381  void	runtime_newErrorCString(uintptr, Eface*)
   382       __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
   383  
   384  /*
   385   * wrapped for go users
   386   */
   387  void	runtime_procyield(uint32)
   388    __asm__(GOSYM_PREFIX "runtime.procyield");
   389  void	runtime_osyield(void)
   390    __asm__(GOSYM_PREFIX "runtime.osyield");
   391  
   392  uintptr	runtime_memlimit(void);
   393  
   394  #define ISNAN(f) __builtin_isnan(f)
   395  
   396  enum
   397  {
   398  	UseSpanType = 1,
   399  };
   400  
   401  #define runtime_setitimer setitimer
   402  
   403  void	runtime_check(void)
   404    __asm__ (GOSYM_PREFIX "runtime.check");
   405  
   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;
   410  
   411  /*
   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.
   417   */
   418  #ifdef __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()
   422  #else
   423  # define secondary_stack_pointer() nil
   424  # define initial_secondary_stack_pointer(stack_alloc) nil
   425  # define flush_registers_to_secondary_stack()
   426  #endif
   427  
   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");
   434  
   435  #define PREFETCH(p) __builtin_prefetch(p)
   436  
   437  void	runtime_badsignal(int);
   438  Defer*	runtime_newdefer(void);
   439  void	runtime_freedefer(Defer*);
   440  
   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");
   448  
   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");
   460  
   461  struct funcfileline_return
   462  {
   463    String retfn;
   464    String retfile;
   465    intgo retline;
   466    intgo retframes;
   467  };
   468  
   469  struct funcfileline_return
   470  runtime_funcfileline (uintptr targetpc, int32 index, bool more)
   471    __asm__ (GOSYM_PREFIX "runtime.funcfileline");
   472  
   473  /*
   474   * helpers for stack scan.
   475   */
   476  bool scanstackwithmap(void*)
   477    __asm__(GOSYM_PREFIX "runtime.scanstackwithmap");
   478  bool doscanstack(G*, void*)
   479    __asm__("runtime.doscanstack");
   480  
   481  extern bool runtime_usestackmaps;
   482  
   483  bool probestackmaps(void)
   484    __asm__("runtime.probestackmaps");
   485  
   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;
   491  
   492  // Cheaper context switch functions.  Currently only defined on
   493  // Linux/AMD64.
   494  #if defined(__x86_64__) && defined(__linux__) && !defined(__CET__)
   495  typedef struct {
   496  	uint64 regs[8];
   497  } __go_context_t;
   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);
   501  #else
   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))
   507  #endif
   508  
   509  // Symbols defined by the linker.
   510  extern const char _etext[] __attribute__ ((weak));
   511  extern const char _edata[] __attribute__ ((weak));
   512  #ifdef _AIX
   513  // Following symbols do not exist on AIX
   514  #define __etext nil
   515  #define __data_start nil
   516  #define __edata nil
   517  #define __bss_start nil
   518  #else
   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));
   523  #endif