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	__go_type_descriptor	Type;
76 typedef	struct	_defer			Defer;
77 typedef	struct	_panic			Panic;
78 
79 typedef struct	__go_ptr_type		PtrType;
80 typedef struct	__go_func_type		FuncType;
81 typedef struct	__go_interface_type	InterfaceType;
82 typedef struct	__go_map_type		MapType;
83 typedef struct	__go_channel_type	ChanType;
84 
85 typedef struct  tracebackg	Traceback;
86 
87 typedef struct	location	Location;
88 
89 struct String
90 {
91 	const byte*	str;
92 	intgo		len;
93 };
94 
95 struct FuncVal
96 {
97 	void	(*fn)(void);
98 	// variable-size, fn-specific data here
99 };
100 
101 #include "array.h"
102 
103 // Rename Go types generated by mkrsysinfo.sh from C types, to avoid
104 // the name conflict.
105 #define timeval go_timeval
106 #define timespec go_timespec
107 
108 #include "runtime.inc"
109 
110 #undef timeval
111 #undef timespec
112 
113 /*
114  * Per-CPU declaration.
115  */
116 extern M*	runtime_m(void);
117 extern G*	runtime_g(void)
118   __asm__(GOSYM_PREFIX "runtime.getg");
119 
120 extern M*	runtime_m0(void)
121   __asm__(GOSYM_PREFIX "runtime.runtime_m0");
122 extern G*	runtime_g0(void)
123   __asm__(GOSYM_PREFIX "runtime.runtime_g0");
124 
125 enum
126 {
127 	true	= 1,
128 	false	= 0,
129 };
130 enum
131 {
132 	PtrSize = sizeof(void*),
133 };
134 enum
135 {
136 	// Per-M stack segment cache size.
137 	StackCacheSize = 32,
138 	// Global <-> per-M stack segment cache transfer batch size.
139 	StackCacheBatch = 16,
140 };
141 
142 struct	SigTab
143 {
144 	int32	sig;
145 	int32	flags;
146 	void*   fwdsig;
147 };
148 
149 #ifdef GOOS_nacl
150 enum {
151    NaCl = 1,
152 };
153 #else
154 enum {
155    NaCl = 0,
156 };
157 #endif
158 
159 #ifdef GOOS_windows
160 enum {
161    Windows = 1
162 };
163 #else
164 enum {
165    Windows = 0
166 };
167 #endif
168 #ifdef GOOS_solaris
169 enum {
170    Solaris = 1
171 };
172 #else
173 enum {
174    Solaris = 0
175 };
176 #endif
177 
178 extern bool runtime_copystack;
179 
180 /*
181  * defined macros
182  *    you need super-gopher-guru privilege
183  *    to add this list.
184  */
185 #define	nelem(x)	(sizeof(x)/sizeof((x)[0]))
186 #define	nil		((void*)0)
187 #define USED(v)		((void) v)
188 #define	ROUND(x, n)	(((x)+(n)-1)&~(uintptr)((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
189 
190 enum {
191 	// hashinit wants this many random bytes
192 	HashRandomBytes = 32
193 };
194 void	runtime_hashinit(void);
195 
196 /*
197  * external data
198  */
199 extern	uintptr* runtime_getZerobase(void)
200   __asm__(GOSYM_PREFIX "runtime.getZerobase");
201 extern G* runtime_getallg(intgo)
202   __asm__(GOSYM_PREFIX "runtime.getallg");
203 extern uintptr runtime_getallglen(void)
204   __asm__(GOSYM_PREFIX "runtime.getallglen");
205 extern	M*	runtime_getallm(void)
206   __asm__(GOSYM_PREFIX "runtime.getallm");
207 extern	Sched*  runtime_sched;
208 extern	uint32	runtime_panicking(void)
209   __asm__ (GOSYM_PREFIX "runtime.getPanicking");
210 
211 extern	bool	runtime_isstarted;
212 extern	bool	runtime_isarchive;
213 
214 extern	void	panicmem(void) __asm__ (GOSYM_PREFIX "runtime.panicmem");
215 
216 /*
217  * common functions and data
218  */
219 #define runtime_strcmp(s1, s2) __builtin_strcmp((s1), (s2))
220 #define runtime_strncmp(s1, s2, n) __builtin_strncmp((s1), (s2), (n))
221 #define runtime_strstr(s1, s2) __builtin_strstr((s1), (s2))
222 intgo	runtime_findnull(const byte*)
223   __asm__ (GOSYM_PREFIX "runtime.findnull");
224 
225 void	runtime_gogo(G*)
226   __asm__ (GOSYM_PREFIX "runtime.gogo");
227 struct __go_func_type;
228 void	runtime_args(int32, byte**)
229   __asm__ (GOSYM_PREFIX "runtime.args");
230 void	runtime_alginit(void)
231   __asm__ (GOSYM_PREFIX "runtime.alginit");
232 void	runtime_goargs(void)
233   __asm__ (GOSYM_PREFIX "runtime.goargs");
234 void	runtime_throw(const char*) __attribute__ ((noreturn));
235 void	runtime_panicstring(const char*) __attribute__ ((noreturn));
236 bool	runtime_canpanic(G*);
237 void	runtime_printf(const char*, ...);
238 int32	runtime_snprintf(byte*, int32, const char*, ...);
239 #define runtime_mcmp(a, b, s) __builtin_memcmp((a), (b), (s))
240 #define runtime_memmove(a, b, s) __builtin_memmove((a), (b), (s))
241 String	runtime_gostringnocopy(const byte*)
242   __asm__ (GOSYM_PREFIX "runtime.gostringnocopy");
243 void	runtime_schedinit(void)
244   __asm__ (GOSYM_PREFIX "runtime.schedinit");
245 void	runtime_initsig(bool)
246   __asm__ (GOSYM_PREFIX "runtime.initsig");
247 #define runtime_open(p, f, m) open((p), (f), (m))
248 #define runtime_read(d, v, n) read((d), (v), (n))
249 #define runtime_write(d, v, n) write((d), (v), (n))
250 #define runtime_close(d) close(d)
251 void	runtime_ready(G*, intgo, bool)
252   __asm__ (GOSYM_PREFIX "runtime.ready");
253 String	runtime_getenv(const char*);
254 int32	runtime_atoi(const byte*, intgo);
255 void*	runtime_mstart(void*);
256 G*	runtime_malg(bool, bool, byte**, uintptr*)
257 	__asm__(GOSYM_PREFIX "runtime.malg");
258 void	runtime_minit(void)
259   __asm__ (GOSYM_PREFIX "runtime.minit");
260 void	runtime_signalstack(byte*, uintptr)
261   __asm__ (GOSYM_PREFIX "runtime.signalstack");
262 void	runtime_mallocinit(void)
263   __asm__ (GOSYM_PREFIX "runtime.mallocinit");
264 void*	runtime_mallocgc(uintptr, const Type*, bool)
265   __asm__ (GOSYM_PREFIX "runtime.mallocgc");
266 void*	runtime_sysAlloc(uintptr, uint64*)
267   __asm__ (GOSYM_PREFIX "runtime.sysAlloc");
268 void	runtime_sysFree(void*, uintptr, uint64*)
269   __asm__ (GOSYM_PREFIX "runtime.sysFree");
270 void	runtime_mprofinit(void);
271 #define runtime_getcallersp(p) __builtin_frame_address(0)
272 void	runtime_mcall(FuncVal*)
273   __asm__ (GOSYM_PREFIX "runtime.mcall");
274 int32	runtime_timediv(int64, int32, int32*)
275   __asm__ (GOSYM_PREFIX "runtime.timediv");
276 int32	runtime_round2(int32 x); // round x up to a power of 2.
277 
278 // atomic operations
279 #define runtime_cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
280 #define runtime_cas64(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
281 #define runtime_casp(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)
282 // Don't confuse with XADD x86 instruction,
283 // this one is actually 'addx', that is, add-and-fetch.
284 #define runtime_xadd(p, v) __sync_add_and_fetch (p, v)
285 #define runtime_xadd64(p, v) __sync_add_and_fetch (p, v)
286 #define runtime_xchg(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
287 #define runtime_xchg64(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
288 #define runtime_xchgp(p, v) __atomic_exchange_n (p, v, __ATOMIC_SEQ_CST)
289 #define runtime_atomicload(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
290 #define runtime_atomicstore(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
291 #define runtime_atomicstore64(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
292 #define runtime_atomicload64(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
293 #define runtime_atomicloadp(p) __atomic_load_n (p, __ATOMIC_SEQ_CST)
294 #define runtime_atomicstorep(p, v) __atomic_store_n (p, v, __ATOMIC_SEQ_CST)
295 
296 void runtime_setg(G*)
297   __asm__ (GOSYM_PREFIX "runtime.setg");
298 void runtime_newextram(void)
299   __asm__ (GOSYM_PREFIX "runtime.newextram");
300 #define runtime_exit(s) exit(s)
301 void	runtime_gosched(void)
302   __asm__ (GOSYM_PREFIX "runtime.Gosched");
303 void	runtime_schedtrace(bool)
304   __asm__ (GOSYM_PREFIX "runtime.schedtrace");
305 void	runtime_goparkunlock(Lock*, String, byte, intgo)
306   __asm__ (GOSYM_PREFIX "runtime.goparkunlock");
307 void	runtime_tsleep(int64, const char*);
308 void	runtime_entersyscall(int32)
309   __asm__ (GOSYM_PREFIX "runtime.entersyscall");
310 void	runtime_entersyscallblock(int32)
311   __asm__ (GOSYM_PREFIX "runtime.entersyscallblock");
312 void	runtime_exitsyscall(int32)
313   __asm__ (GOSYM_PREFIX "runtime.exitsyscall");
314 G*	__go_go(void (*pfn)(void*), void*);
315 int32	runtime_callers(int32, Location*, int32, bool keep_callers);
316 int64	runtime_nanotime(void)	// monotonic time
317   __asm__(GOSYM_PREFIX "runtime.nanotime");
318 void	runtime_dopanic(int32) __attribute__ ((noreturn));
319 void	runtime_startpanic(void)
320   __asm__ (GOSYM_PREFIX "runtime.startpanic");
321 void	runtime_unwindstack(G*, byte*);
322 void	runtime_usleep(uint32)
323      __asm__ (GOSYM_PREFIX "runtime.usleep");
324 int64	runtime_cputicks(void)
325      __asm__ (GOSYM_PREFIX "runtime.cputicks");
326 int64	runtime_tickspersecond(void)
327      __asm__ (GOSYM_PREFIX "runtime.tickspersecond");
328 void	runtime_blockevent(int64, int32);
329 extern int64 runtime_blockprofilerate;
330 G*	runtime_netpoll(bool)
331   __asm__ (GOSYM_PREFIX "runtime.netpoll");
332 void	runtime_parsedebugvars(void)
333   __asm__(GOSYM_PREFIX "runtime.parsedebugvars");
334 void	_rt0_go(void);
335 G*	runtime_timejump(void);
336 
337 /*
338  * mutual exclusion locks.  in the uncontended case,
339  * as fast as spin locks (just a few user-level instructions),
340  * but on the contention path they sleep in the kernel.
341  * a zeroed Lock is unlocked (no need to initialize each lock).
342  */
343 void	runtime_lock(Lock*)
344   __asm__(GOSYM_PREFIX "runtime.lock");
345 void	runtime_unlock(Lock*)
346   __asm__(GOSYM_PREFIX "runtime.unlock");
347 
348 /*
349  * sleep and wakeup on one-time events.
350  * before any calls to notesleep or notewakeup,
351  * must call noteclear to initialize the Note.
352  * then, exactly one thread can call notesleep
353  * and exactly one thread can call notewakeup (once).
354  * once notewakeup has been called, the notesleep
355  * will return.  future notesleep will return immediately.
356  * subsequent noteclear must be called only after
357  * previous notesleep has returned, e.g. it's disallowed
358  * to call noteclear straight after notewakeup.
359  *
360  * notetsleep is like notesleep but wakes up after
361  * a given number of nanoseconds even if the event
362  * has not yet happened.  if a goroutine uses notetsleep to
363  * wake up early, it must wait to call noteclear until it
364  * can be sure that no other goroutine is calling
365  * notewakeup.
366  *
367  * notesleep/notetsleep are generally called on g0,
368  * notetsleepg is similar to notetsleep but is called on user g.
369  */
370 void	runtime_noteclear(Note*)
371   __asm__ (GOSYM_PREFIX "runtime.noteclear");
372 void	runtime_notesleep(Note*)
373   __asm__ (GOSYM_PREFIX "runtime.notesleep");
374 void	runtime_notewakeup(Note*)
375   __asm__ (GOSYM_PREFIX "runtime.notewakeup");
376 bool	runtime_notetsleep(Note*, int64)  // false - timeout
377   __asm__ (GOSYM_PREFIX "runtime.notetsleep");
378 bool	runtime_notetsleepg(Note*, int64)  // false - timeout
379   __asm__ (GOSYM_PREFIX "runtime.notetsleepg");
380 
381 /*
382  * low level C-called
383  */
384 #define runtime_mmap mmap
385 #define runtime_munmap munmap
386 #define runtime_madvise madvise
387 #define runtime_memclr(buf, size) __builtin_memset((buf), 0, (size))
388 #define runtime_getcallerpc(p) __builtin_return_address(0)
389 
390 #ifdef __rtems__
391 void __wrap_rtems_task_variable_add(void **);
392 #endif
393 
394 /*
395  * runtime go-called
396  */
397 void reflect_call(const struct __go_func_type *, FuncVal *, _Bool, _Bool,
398 		  void **, void **)
399   __asm__ (GOSYM_PREFIX "reflect.call");
400 void runtime_panic(Eface)
401   __asm__ (GOSYM_PREFIX "runtime.gopanic");
402 void runtime_panic(Eface)
403   __attribute__ ((noreturn));
404 
405 /*
406  * runtime c-called (but written in Go)
407  */
408 void	runtime_newTypeAssertionError(const String*, const String*, const String*, const String*, Eface*)
409      __asm__ (GOSYM_PREFIX "runtime.NewTypeAssertionError");
410 void	runtime_newErrorCString(const char*, Eface*)
411      __asm__ (GOSYM_PREFIX "runtime.NewErrorCString");
412 
413 /*
414  * wrapped for go users
415  */
416 void	runtime_procyield(uint32)
417   __asm__(GOSYM_PREFIX "runtime.procyield");
418 void	runtime_osyield(void)
419   __asm__(GOSYM_PREFIX "runtime.osyield");
420 
421 uintptr	runtime_memlimit(void);
422 
423 #define ISNAN(f) __builtin_isnan(f)
424 
425 enum
426 {
427 	UseSpanType = 1,
428 };
429 
430 #define runtime_setitimer setitimer
431 
432 void	runtime_check(void)
433   __asm__ (GOSYM_PREFIX "runtime.check");
434 
435 // Size of stack space allocated using Go's allocator.
436 // This will be 0 when using split stacks, as in that case
437 // the stacks are allocated by the splitstack library.
438 extern uintptr runtime_stacks_sys;
439 
440 /*
441  * ia64's register file is spilled to a separate stack, the register backing
442  * store, on window overflow, and must also be scanned. This occupies the other
443  * end of the normal stack allocation, growing upwards.
444  * We also need to ensure all register windows are flushed to the backing
445  * store, as unlike SPARC, __builtin_unwind_init doesn't do this on ia64.
446  */
447 #ifdef __ia64__
448 # define secondary_stack_pointer() __builtin_ia64_bsp()
449 # define initial_secondary_stack_pointer(stack_alloc) (stack_alloc)
450 # define flush_registers_to_secondary_stack() __builtin_ia64_flushrs()
451 #else
452 # define secondary_stack_pointer() nil
453 # define initial_secondary_stack_pointer(stack_alloc) nil
454 # define flush_registers_to_secondary_stack()
455 #endif
456 
457 struct backtrace_state;
458 extern struct backtrace_state *__go_get_backtrace_state(void);
459 extern void __go_syminfo_fnname_callback(void*, uintptr_t, const char*,
460 					 uintptr_t, uintptr_t);
461 extern void runtime_main(void*)
462   __asm__(GOSYM_PREFIX "runtime.main");
463 
464 int32 getproccount(void);
465 
466 #define PREFETCH(p) __builtin_prefetch(p)
467 
468 bool	runtime_gcwaiting(void);
469 void	runtime_badsignal(int);
470 Defer*	runtime_newdefer(void);
471 void	runtime_freedefer(Defer*);
472 
473 extern void _cgo_wait_runtime_init_done (void);
474 extern void _cgo_notify_runtime_init_done (void)
475   __asm__ (GOSYM_PREFIX "runtime._cgo_notify_runtime_init_done");
476 extern _Bool runtime_iscgo;
477 extern uintptr __go_end __attribute__ ((weak));
478 extern void *getitab(const struct __go_type_descriptor *,
479 		     const struct __go_type_descriptor *,
480 		     _Bool)
481   __asm__ (GOSYM_PREFIX "runtime.getitab");
482 
483 extern void runtime_cpuinit(void);
484 extern void setIsCgo(void)
485   __asm__ (GOSYM_PREFIX "runtime.setIsCgo");
486 extern void setCpuidECX(uint32)
487   __asm__ (GOSYM_PREFIX "runtime.setCpuidECX");
488 extern void setSupportAES(bool)
489   __asm__ (GOSYM_PREFIX "runtime.setSupportAES");
490 extern void typedmemmove(const Type *, void *, const void *)
491   __asm__ (GOSYM_PREFIX "runtime.typedmemmove");
492 extern void setncpu(int32)
493   __asm__(GOSYM_PREFIX "runtime.setncpu");
494 extern Sched* runtime_getsched(void)
495   __asm__ (GOSYM_PREFIX "runtime.getsched");
496 extern void setpagesize(uintptr_t)
497   __asm__(GOSYM_PREFIX "runtime.setpagesize");
498