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 <errno.h>
6 #include <limits.h>
7 #include <signal.h>
8 #include <stdlib.h>
9 #include <pthread.h>
10 #include <unistd.h>
11 
12 #include "config.h"
13 
14 #ifdef HAVE_DL_ITERATE_PHDR
15 #include <link.h>
16 #endif
17 
18 #include "runtime.h"
19 #include "arch.h"
20 #include "defs.h"
21 #include "go-type.h"
22 
23 #ifdef USING_SPLIT_STACK
24 
25 /* FIXME: These are not declared anywhere.  */
26 
27 extern void __splitstack_getcontext(void *context[10]);
28 
29 extern void __splitstack_setcontext(void *context[10]);
30 
31 extern void *__splitstack_makecontext(size_t, void *context[10], size_t *);
32 
33 extern void * __splitstack_resetcontext(void *context[10], size_t *);
34 
35 extern void __splitstack_releasecontext(void *context[10]);
36 
37 extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
38 			       void **);
39 
40 extern void __splitstack_block_signals (int *, int *);
41 
42 extern void __splitstack_block_signals_context (void *context[10], int *,
43 						int *);
44 
45 #endif
46 
47 #ifndef PTHREAD_STACK_MIN
48 # define PTHREAD_STACK_MIN 8192
49 #endif
50 
51 #if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
52 # define StackMin PTHREAD_STACK_MIN
53 #else
54 # define StackMin ((sizeof(char *) < 8) ? 2 * 1024 * 1024 : 4 * 1024 * 1024)
55 #endif
56 
57 uintptr runtime_stacks_sys;
58 
59 void gtraceback(G*)
60   __asm__(GOSYM_PREFIX "runtime.gtraceback");
61 
62 static void gscanstack(G*);
63 
64 #ifdef __rtems__
65 #define __thread
66 #endif
67 
68 static __thread G *g;
69 
70 #ifndef SETCONTEXT_CLOBBERS_TLS
71 
72 static inline void
initcontext(void)73 initcontext(void)
74 {
75 }
76 
77 static inline void
fixcontext(ucontext_t * c)78 fixcontext(ucontext_t *c __attribute__ ((unused)))
79 {
80 }
81 
82 #else
83 
84 # if defined(__x86_64__) && defined(__sun__)
85 
86 // x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
87 // register to that of the thread which called getcontext.  The effect
88 // is that the address of all __thread variables changes.  This bug
89 // also affects pthread_self() and pthread_getspecific.  We work
90 // around it by clobbering the context field directly to keep %fs the
91 // same.
92 
93 static __thread greg_t fs;
94 
95 static inline void
initcontext(void)96 initcontext(void)
97 {
98 	ucontext_t c;
99 
100 	getcontext(&c);
101 	fs = c.uc_mcontext.gregs[REG_FSBASE];
102 }
103 
104 static inline void
fixcontext(ucontext_t * c)105 fixcontext(ucontext_t* c)
106 {
107 	c->uc_mcontext.gregs[REG_FSBASE] = fs;
108 }
109 
110 # elif defined(__NetBSD__)
111 
112 // NetBSD has a bug: setcontext clobbers tlsbase, we need to save
113 // and restore it ourselves.
114 
115 static __thread __greg_t tlsbase;
116 
117 static inline void
initcontext(void)118 initcontext(void)
119 {
120 	ucontext_t c;
121 
122 	getcontext(&c);
123 	tlsbase = c.uc_mcontext._mc_tlsbase;
124 }
125 
126 static inline void
fixcontext(ucontext_t * c)127 fixcontext(ucontext_t* c)
128 {
129 	c->uc_mcontext._mc_tlsbase = tlsbase;
130 }
131 
132 # elif defined(__sparc__)
133 
134 static inline void
initcontext(void)135 initcontext(void)
136 {
137 }
138 
139 static inline void
fixcontext(ucontext_t * c)140 fixcontext(ucontext_t *c)
141 {
142 	/* ??? Using
143 	     register unsigned long thread __asm__("%g7");
144 	     c->uc_mcontext.gregs[REG_G7] = thread;
145 	   results in
146 	     error: variable ‘thread’ might be clobbered by \
147 		‘longjmp’ or ‘vfork’ [-Werror=clobbered]
148 	   which ought to be false, as %g7 is a fixed register.  */
149 
150 	if (sizeof (c->uc_mcontext.gregs[REG_G7]) == 8)
151 		asm ("stx %%g7, %0" : "=m"(c->uc_mcontext.gregs[REG_G7]));
152 	else
153 		asm ("st %%g7, %0" : "=m"(c->uc_mcontext.gregs[REG_G7]));
154 }
155 
156 # elif defined(_AIX)
157 
158 static inline void
initcontext(void)159 initcontext(void)
160 {
161 }
162 
163 static inline void
fixcontext(ucontext_t * c)164 fixcontext(ucontext_t* c)
165 {
166 	// Thread pointer is in r13, per 64-bit ABI.
167 	if (sizeof (c->uc_mcontext.jmp_context.gpr[13]) == 8)
168 		asm ("std 13, %0" : "=m"(c->uc_mcontext.jmp_context.gpr[13]));
169 }
170 
171 # else
172 
173 #  error unknown case for SETCONTEXT_CLOBBERS_TLS
174 
175 # endif
176 
177 #endif
178 
179 // ucontext_arg returns a properly aligned ucontext_t value.  On some
180 // systems a ucontext_t value must be aligned to a 16-byte boundary.
181 // The g structure that has fields of type ucontext_t is defined in
182 // Go, and Go has no simple way to align a field to such a boundary.
183 // So we make the field larger in runtime2.go and pick an appropriate
184 // offset within the field here.
185 static ucontext_t*
ucontext_arg(uintptr_t * go_ucontext)186 ucontext_arg(uintptr_t* go_ucontext)
187 {
188 	uintptr_t p = (uintptr_t)go_ucontext;
189 	size_t align = __alignof__(ucontext_t);
190 	if(align > 16) {
191 		// We only ensured space for up to a 16 byte alignment
192 		// in libgo/go/runtime/runtime2.go.
193 		runtime_throw("required alignment of ucontext_t too large");
194 	}
195 	p = (p + align - 1) &~ (uintptr_t)(align - 1);
196 	return (ucontext_t*)p;
197 }
198 
199 // We can not always refer to the TLS variables directly.  The
200 // compiler will call tls_get_addr to get the address of the variable,
201 // and it may hold it in a register across a call to schedule.  When
202 // we get back from the call we may be running in a different thread,
203 // in which case the register now points to the TLS variable for a
204 // different thread.  We use non-inlinable functions to avoid this
205 // when necessary.
206 
207 G* runtime_g(void) __attribute__ ((noinline, no_split_stack));
208 
209 G*
runtime_g(void)210 runtime_g(void)
211 {
212 	return g;
213 }
214 
215 M* runtime_m(void) __attribute__ ((noinline, no_split_stack));
216 
217 M*
runtime_m(void)218 runtime_m(void)
219 {
220 	if(g == nil)
221 		return nil;
222 	return g->m;
223 }
224 
225 // Set g.
226 void
runtime_setg(G * gp)227 runtime_setg(G* gp)
228 {
229 	g = gp;
230 }
231 
232 void runtime_newosproc(M *)
233   __asm__(GOSYM_PREFIX "runtime.newosproc");
234 
235 // Start a new thread.
236 void
runtime_newosproc(M * mp)237 runtime_newosproc(M *mp)
238 {
239 	pthread_attr_t attr;
240 	sigset_t clear, old;
241 	pthread_t tid;
242 	int tries;
243 	int ret;
244 
245 	if(pthread_attr_init(&attr) != 0)
246 		runtime_throw("pthread_attr_init");
247 	if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
248 		runtime_throw("pthread_attr_setdetachstate");
249 
250 	// Block signals during pthread_create so that the new thread
251 	// starts with signals disabled.  It will enable them in minit.
252 	sigfillset(&clear);
253 
254 #ifdef SIGTRAP
255 	// Blocking SIGTRAP reportedly breaks gdb on Alpha GNU/Linux.
256 	sigdelset(&clear, SIGTRAP);
257 #endif
258 
259 	sigemptyset(&old);
260 	pthread_sigmask(SIG_BLOCK, &clear, &old);
261 
262 	for (tries = 0; tries < 20; tries++) {
263 		ret = pthread_create(&tid, &attr, runtime_mstart, mp);
264 		if (ret != EAGAIN) {
265 			break;
266 		}
267 		runtime_usleep((tries + 1) * 1000); // Milliseconds.
268 	}
269 
270 	pthread_sigmask(SIG_SETMASK, &old, nil);
271 
272 	if (ret != 0) {
273 		runtime_printf("pthread_create failed: %d\n", ret);
274 		runtime_throw("pthread_create");
275 	}
276 
277 	if(pthread_attr_destroy(&attr) != 0)
278 		runtime_throw("pthread_attr_destroy");
279 }
280 
281 // Switch context to a different goroutine.  This is like longjmp.
282 void runtime_gogo(G*) __attribute__ ((noinline));
283 void
runtime_gogo(G * newg)284 runtime_gogo(G* newg)
285 {
286 #ifdef USING_SPLIT_STACK
287 	__splitstack_setcontext((void*)(&newg->stackcontext[0]));
288 #endif
289 	g = newg;
290 	newg->fromgogo = true;
291 	fixcontext(ucontext_arg(&newg->context[0]));
292 	setcontext(ucontext_arg(&newg->context[0]));
293 	runtime_throw("gogo setcontext returned");
294 }
295 
296 // Save context and call fn passing g as a parameter.  This is like
297 // setjmp.  Because getcontext always returns 0, unlike setjmp, we use
298 // g->fromgogo as a code.  It will be true if we got here via
299 // setcontext.  g == nil the first time this is called in a new m.
300 void runtime_mcall(FuncVal *) __attribute__ ((noinline));
301 void
runtime_mcall(FuncVal * fv)302 runtime_mcall(FuncVal *fv)
303 {
304 	M *mp;
305 	G *gp;
306 #ifndef USING_SPLIT_STACK
307 	void *afterregs;
308 #endif
309 
310 	// Ensure that all registers are on the stack for the garbage
311 	// collector.
312 	__builtin_unwind_init();
313 	flush_registers_to_secondary_stack();
314 
315 	gp = g;
316 	mp = gp->m;
317 	if(gp == mp->g0)
318 		runtime_throw("runtime: mcall called on m->g0 stack");
319 
320 	if(gp != nil) {
321 
322 #ifdef USING_SPLIT_STACK
323 		__splitstack_getcontext((void*)(&g->stackcontext[0]));
324 #else
325 		// We have to point to an address on the stack that is
326 		// below the saved registers.
327 		gp->gcnextsp = (uintptr)(&afterregs);
328 		gp->gcnextsp2 = (uintptr)(secondary_stack_pointer());
329 #endif
330 		gp->fromgogo = false;
331 		getcontext(ucontext_arg(&gp->context[0]));
332 
333 		// When we return from getcontext, we may be running
334 		// in a new thread.  That means that g may have
335 		// changed.  It is a global variables so we will
336 		// reload it, but the address of g may be cached in
337 		// our local stack frame, and that address may be
338 		// wrong.  Call the function to reload the value for
339 		// this thread.
340 		gp = runtime_g();
341 		mp = gp->m;
342 
343 		if(gp->traceback != 0)
344 			gtraceback(gp);
345 		if(gp->scang != 0)
346 			gscanstack(gp);
347 	}
348 	if (gp == nil || !gp->fromgogo) {
349 #ifdef USING_SPLIT_STACK
350 		__splitstack_setcontext((void*)(&mp->g0->stackcontext[0]));
351 #endif
352 		mp->g0->entry = fv;
353 		mp->g0->param = gp;
354 
355 		// It's OK to set g directly here because this case
356 		// can not occur if we got here via a setcontext to
357 		// the getcontext call just above.
358 		g = mp->g0;
359 
360 		fixcontext(ucontext_arg(&mp->g0->context[0]));
361 		setcontext(ucontext_arg(&mp->g0->context[0]));
362 		runtime_throw("runtime: mcall function returned");
363 	}
364 }
365 
366 // Goroutine scheduler
367 // The scheduler's job is to distribute ready-to-run goroutines over worker threads.
368 //
369 // The main concepts are:
370 // G - goroutine.
371 // M - worker thread, or machine.
372 // P - processor, a resource that is required to execute Go code.
373 //     M must have an associated P to execute Go code, however it can be
374 //     blocked or in a syscall w/o an associated P.
375 //
376 // Design doc at http://golang.org/s/go11sched.
377 
378 extern G* allocg(void)
379   __asm__ (GOSYM_PREFIX "runtime.allocg");
380 
381 Sched*	runtime_sched;
382 
383 bool	runtime_isarchive;
384 
385 extern void kickoff(void)
386   __asm__(GOSYM_PREFIX "runtime.kickoff");
387 extern void minit(void)
388   __asm__(GOSYM_PREFIX "runtime.minit");
389 extern void mstart1()
390   __asm__(GOSYM_PREFIX "runtime.mstart1");
391 extern void stopm(void)
392   __asm__(GOSYM_PREFIX "runtime.stopm");
393 extern void mexit(bool)
394   __asm__(GOSYM_PREFIX "runtime.mexit");
395 extern void handoffp(P*)
396   __asm__(GOSYM_PREFIX "runtime.handoffp");
397 extern void wakep(void)
398   __asm__(GOSYM_PREFIX "runtime.wakep");
399 extern void stoplockedm(void)
400   __asm__(GOSYM_PREFIX "runtime.stoplockedm");
401 extern void schedule(void)
402   __asm__(GOSYM_PREFIX "runtime.schedule");
403 extern void execute(G*, bool)
404   __asm__(GOSYM_PREFIX "runtime.execute");
405 extern void reentersyscall(uintptr, uintptr)
406   __asm__(GOSYM_PREFIX "runtime.reentersyscall");
407 extern void reentersyscallblock(uintptr, uintptr)
408   __asm__(GOSYM_PREFIX "runtime.reentersyscallblock");
409 extern G* gfget(P*)
410   __asm__(GOSYM_PREFIX "runtime.gfget");
411 extern void acquirep(P*)
412   __asm__(GOSYM_PREFIX "runtime.acquirep");
413 extern P* releasep(void)
414   __asm__(GOSYM_PREFIX "runtime.releasep");
415 extern void incidlelocked(int32)
416   __asm__(GOSYM_PREFIX "runtime.incidlelocked");
417 extern void globrunqput(G*)
418   __asm__(GOSYM_PREFIX "runtime.globrunqput");
419 extern P* pidleget(void)
420   __asm__(GOSYM_PREFIX "runtime.pidleget");
421 extern struct mstats* getMemstats(void)
422   __asm__(GOSYM_PREFIX "runtime.getMemstats");
423 
424 bool runtime_isstarted;
425 
426 // Used to determine the field alignment.
427 
428 struct field_align
429 {
430   char c;
431   Hchan *p;
432 };
433 
434 void getTraceback(G*, G*) __asm__(GOSYM_PREFIX "runtime.getTraceback");
435 
436 // getTraceback stores a traceback of gp in the g's traceback field
437 // and then returns to me.  We expect that gp's traceback is not nil.
438 // It works by saving me's current context, and checking gp's traceback field.
439 // If gp's traceback field is not nil, it starts running gp.
440 // In places where we call getcontext, we check the traceback field.
441 // If it is not nil, we collect a traceback, and then return to the
442 // goroutine stored in the traceback field, which is me.
getTraceback(G * me,G * gp)443 void getTraceback(G* me, G* gp)
444 {
445 	M* holdm;
446 
447 	holdm = gp->m;
448 	gp->m = me->m;
449 
450 #ifdef USING_SPLIT_STACK
451 	__splitstack_getcontext((void*)(&me->stackcontext[0]));
452 #endif
453 	getcontext(ucontext_arg(&me->context[0]));
454 
455 	if (gp->traceback != 0) {
456 		runtime_gogo(gp);
457 	}
458 
459 	gp->m = holdm;
460 }
461 
462 // Do a stack trace of gp, and then restore the context to
463 // gp->traceback->gp.
464 
465 void
gtraceback(G * gp)466 gtraceback(G* gp)
467 {
468 	Traceback* traceback;
469 
470 	traceback = (Traceback*)gp->traceback;
471 	gp->traceback = 0;
472 	traceback->c = runtime_callers(1, traceback->locbuf,
473 		sizeof traceback->locbuf / sizeof traceback->locbuf[0], false);
474 	runtime_gogo(traceback->gp);
475 }
476 
477 void doscanstackswitch(G*, G*) __asm__(GOSYM_PREFIX "runtime.doscanstackswitch");
478 
479 // Switch to gp and let it scan its stack.
480 // The first time gp->scang is set (to me). The second time here
481 // gp is done scanning, and has unset gp->scang, so we just return.
482 void
doscanstackswitch(G * me,G * gp)483 doscanstackswitch(G* me, G* gp)
484 {
485 	M* holdm;
486 
487 	__go_assert(me->entry == nil);
488 	me->fromgogo = false;
489 
490 	holdm = gp->m;
491 	gp->m = me->m;
492 
493 #ifdef USING_SPLIT_STACK
494 	__splitstack_getcontext((void*)(&me->stackcontext[0]));
495 #endif
496 	getcontext(ucontext_arg(&me->context[0]));
497 
498 	if(me->entry != nil) {
499 		// Got here from mcall.
500 		// The stack scanning code may call systemstack, which calls
501 		// mcall, which calls setcontext.
502 		// Run the function, which at the end will switch back to gp.
503 		FuncVal *fv = me->entry;
504 		void (*pfn)(G*) = (void (*)(G*))fv->fn;
505 		G* gp1 = (G*)me->param;
506 		__go_assert(gp1 == gp);
507 		me->entry = nil;
508 		me->param = nil;
509 		__builtin_call_with_static_chain(pfn(gp1), fv);
510 		abort();
511 	}
512 
513 	if (gp->scang != 0)
514 		runtime_gogo(gp);
515 
516 	gp->m = holdm;
517 }
518 
519 // Do a stack scan, then switch back to the g that triggers this scan.
520 // We come here from doscanstackswitch.
521 static void
gscanstack(G * gp)522 gscanstack(G *gp)
523 {
524 	G *oldg, *oldcurg;
525 
526 	oldg = (G*)gp->scang;
527 	oldcurg = oldg->m->curg;
528 	oldg->m->curg = gp;
529 	gp->scang = 0;
530 
531 	doscanstack(gp, (void*)gp->scangcw);
532 
533 	gp->scangcw = 0;
534 	oldg->m->curg = oldcurg;
535 	runtime_gogo(oldg);
536 }
537 
538 // Called by pthread_create to start an M.
539 void*
runtime_mstart(void * arg)540 runtime_mstart(void *arg)
541 {
542 	M* mp;
543 	G* gp;
544 
545 	mp = (M*)(arg);
546 	gp = mp->g0;
547 	gp->m = mp;
548 
549 	g = gp;
550 
551 	gp->entry = nil;
552 	gp->param = nil;
553 
554 	// We have to call minit before we call getcontext,
555 	// because getcontext will copy the signal mask.
556 	minit();
557 
558 	initcontext();
559 
560 	// Record top of stack for use by mcall.
561 	// Once we call schedule we're never coming back,
562 	// so other calls can reuse this stack space.
563 #ifdef USING_SPLIT_STACK
564 	__splitstack_getcontext((void*)(&gp->stackcontext[0]));
565 #else
566 	gp->gcinitialsp = &arg;
567 	// Setting gcstacksize to 0 is a marker meaning that gcinitialsp
568 	// is the top of the stack, not the bottom.
569 	gp->gcstacksize = 0;
570 	gp->gcnextsp = (uintptr)(&arg);
571 	gp->gcinitialsp2 = secondary_stack_pointer();
572 	gp->gcnextsp2 = (uintptr)(gp->gcinitialsp2);
573 #endif
574 
575 	// Save the currently active context.  This will return
576 	// multiple times via the setcontext call in mcall.
577 	getcontext(ucontext_arg(&gp->context[0]));
578 
579 	if(gp->traceback != 0) {
580 		// Got here from getTraceback.
581 		// I'm not sure this ever actually happens--getTraceback
582 		// may always go to the getcontext call in mcall.
583 		gtraceback(gp);
584 	}
585 	if(gp->scang != 0)
586 		// Got here from doscanswitch. Should not happen.
587 		runtime_throw("mstart with scang");
588 
589 	if(gp->entry != nil) {
590 		// Got here from mcall.
591 		FuncVal *fv = gp->entry;
592 		void (*pfn)(G*) = (void (*)(G*))fv->fn;
593 		G* gp1 = (G*)gp->param;
594 		gp->entry = nil;
595 		gp->param = nil;
596 		__builtin_call_with_static_chain(pfn(gp1), fv);
597 		*(int*)0x21 = 0x21;
598 	}
599 
600 	if(mp->exiting) {
601 		mexit(true);
602 		return nil;
603 	}
604 
605 	// Initial call to getcontext--starting thread.
606 
607 #ifdef USING_SPLIT_STACK
608 	{
609 		int dont_block_signals = 0;
610 		__splitstack_block_signals(&dont_block_signals, nil);
611 	}
612 #endif
613 
614 	mstart1();
615 
616 	// mstart1 does not return, but we need a return statement
617 	// here to avoid a compiler warning.
618 	return nil;
619 }
620 
621 typedef struct CgoThreadStart CgoThreadStart;
622 struct CgoThreadStart
623 {
624 	M *m;
625 	G *g;
626 	uintptr *tls;
627 	void (*fn)(void);
628 };
629 
630 void setGContext(void) __asm__ (GOSYM_PREFIX "runtime.setGContext");
631 
632 // setGContext sets up a new goroutine context for the current g.
633 void
setGContext(void)634 setGContext(void)
635 {
636 	int val;
637 	G *gp;
638 
639 	initcontext();
640 	gp = g;
641 	gp->entry = nil;
642 	gp->param = nil;
643 #ifdef USING_SPLIT_STACK
644 	__splitstack_getcontext((void*)(&gp->stackcontext[0]));
645 	val = 0;
646 	__splitstack_block_signals(&val, nil);
647 #else
648 	gp->gcinitialsp = &val;
649 	gp->gcstack = 0;
650 	gp->gcstacksize = 0;
651 	gp->gcnextsp = (uintptr)(&val);
652 	gp->gcinitialsp2 = secondary_stack_pointer();
653 	gp->gcnextsp2 = (uintptr)(gp->gcinitialsp2);
654 #endif
655 	getcontext(ucontext_arg(&gp->context[0]));
656 
657 	if(gp->entry != nil) {
658 		// Got here from mcall.
659 		FuncVal *fv = gp->entry;
660 		void (*pfn)(G*) = (void (*)(G*))fv->fn;
661 		G* gp1 = (G*)gp->param;
662 		gp->entry = nil;
663 		gp->param = nil;
664 		__builtin_call_with_static_chain(pfn(gp1), fv);
665 		*(int*)0x22 = 0x22;
666 	}
667 }
668 
669 void makeGContext(G*, byte*, uintptr)
670 	__asm__(GOSYM_PREFIX "runtime.makeGContext");
671 
672 // makeGContext makes a new context for a g.
673 void
makeGContext(G * gp,byte * sp,uintptr spsize)674 makeGContext(G* gp, byte* sp, uintptr spsize) {
675 	ucontext_t *uc;
676 
677 	uc = ucontext_arg(&gp->context[0]);
678 	getcontext(uc);
679 	uc->uc_stack.ss_sp = sp;
680 	uc->uc_stack.ss_size = (size_t)spsize;
681 	makecontext(uc, kickoff, 0);
682 }
683 
684 // The goroutine g is about to enter a system call.
685 // Record that it's not using the cpu anymore.
686 // This is called only from the go syscall library and cgocall,
687 // not from the low-level system calls used by the runtime.
688 //
689 // Entersyscall cannot split the stack: the runtime_gosave must
690 // make g->sched refer to the caller's stack segment, because
691 // entersyscall is going to return immediately after.
692 
693 void runtime_entersyscall() __attribute__ ((no_split_stack));
694 static void doentersyscall(uintptr, uintptr)
695   __attribute__ ((no_split_stack, noinline));
696 
697 void
runtime_entersyscall()698 runtime_entersyscall()
699 {
700 	// Save the registers in the g structure so that any pointers
701 	// held in registers will be seen by the garbage collector.
702 	if (!runtime_usestackmaps)
703 		getcontext(ucontext_arg(&g->gcregs[0]));
704 
705 	// Note that if this function does save any registers itself,
706 	// we might store the wrong value in the call to getcontext.
707 	// FIXME: This assumes that we do not need to save any
708 	// callee-saved registers to access the TLS variable g.  We
709 	// don't want to put the ucontext_t on the stack because it is
710 	// large and we can not split the stack here.
711 	doentersyscall((uintptr)runtime_getcallerpc(),
712 		       (uintptr)runtime_getcallersp());
713 }
714 
715 static void
doentersyscall(uintptr pc,uintptr sp)716 doentersyscall(uintptr pc, uintptr sp)
717 {
718 	// Leave SP around for GC and traceback.
719 #ifdef USING_SPLIT_STACK
720 	{
721 	  size_t gcstacksize;
722 	  g->gcstack = (uintptr)(__splitstack_find(nil, nil, &gcstacksize,
723 						   (void**)(&g->gcnextsegment),
724 						   (void**)(&g->gcnextsp),
725 						   &g->gcinitialsp));
726 	  g->gcstacksize = (uintptr)gcstacksize;
727 	}
728 #else
729 	{
730 		void *v;
731 
732 		g->gcnextsp = (uintptr)(&v);
733 		g->gcnextsp2 = (uintptr)(secondary_stack_pointer());
734 	}
735 #endif
736 
737 	reentersyscall(pc, sp);
738 }
739 
740 static void doentersyscallblock(uintptr, uintptr)
741   __attribute__ ((no_split_stack, noinline));
742 
743 // The same as runtime_entersyscall(), but with a hint that the syscall is blocking.
744 void
runtime_entersyscallblock()745 runtime_entersyscallblock()
746 {
747 	// Save the registers in the g structure so that any pointers
748 	// held in registers will be seen by the garbage collector.
749 	if (!runtime_usestackmaps)
750 		getcontext(ucontext_arg(&g->gcregs[0]));
751 
752 	// See comment in runtime_entersyscall.
753 	doentersyscallblock((uintptr)runtime_getcallerpc(),
754 			    (uintptr)runtime_getcallersp());
755 }
756 
757 static void
doentersyscallblock(uintptr pc,uintptr sp)758 doentersyscallblock(uintptr pc, uintptr sp)
759 {
760 	// Leave SP around for GC and traceback.
761 #ifdef USING_SPLIT_STACK
762 	{
763 	  size_t gcstacksize;
764 	  g->gcstack = (uintptr)(__splitstack_find(nil, nil, &gcstacksize,
765 						   (void**)(&g->gcnextsegment),
766 						   (void**)(&g->gcnextsp),
767 						   &g->gcinitialsp));
768 	  g->gcstacksize = (uintptr)gcstacksize;
769 	}
770 #else
771 	{
772 		void *v;
773 
774 		g->gcnextsp = (uintptr)(&v);
775 		g->gcnextsp2 = (uintptr)(secondary_stack_pointer());
776 	}
777 #endif
778 
779 	reentersyscallblock(pc, sp);
780 }
781 
782 // Allocate a new g, with a stack big enough for stacksize bytes.
783 G*
runtime_malg(bool allocatestack,bool signalstack,byte ** ret_stack,uintptr * ret_stacksize)784 runtime_malg(bool allocatestack, bool signalstack, byte** ret_stack, uintptr* ret_stacksize)
785 {
786 	uintptr stacksize;
787 	G *newg;
788 	byte* unused_stack;
789 	uintptr unused_stacksize;
790 #ifdef USING_SPLIT_STACK
791 	int dont_block_signals = 0;
792 	size_t ss_stacksize;
793 #endif
794 
795 	if (ret_stack == nil) {
796 		ret_stack = &unused_stack;
797 	}
798 	if (ret_stacksize == nil) {
799 		ret_stacksize = &unused_stacksize;
800 	}
801 	newg = allocg();
802 	if(allocatestack) {
803 		stacksize = StackMin;
804 		if(signalstack) {
805 			stacksize = 32 * 1024; // OS X wants >= 8K, GNU/Linux >= 2K
806 #ifdef SIGSTKSZ
807 			if(stacksize < (uintptr)(SIGSTKSZ))
808 				stacksize = (uintptr)(SIGSTKSZ);
809 #endif
810 		}
811 
812 #ifdef USING_SPLIT_STACK
813 		*ret_stack = __splitstack_makecontext(stacksize,
814 						      (void*)(&newg->stackcontext[0]),
815 						      &ss_stacksize);
816 		*ret_stacksize = (uintptr)ss_stacksize;
817 		__splitstack_block_signals_context((void*)(&newg->stackcontext[0]),
818 						   &dont_block_signals, nil);
819 #else
820                 // In 64-bit mode, the maximum Go allocation space is
821                 // 128G.  Our stack size is 4M, which only permits 32K
822                 // goroutines.  In order to not limit ourselves,
823                 // allocate the stacks out of separate memory.  In
824                 // 32-bit mode, the Go allocation space is all of
825                 // memory anyhow.
826 		if(sizeof(void*) == 8) {
827 			void *p = runtime_sysAlloc(stacksize, &getMemstats()->stacks_sys);
828 			if(p == nil)
829 				runtime_throw("runtime: cannot allocate memory for goroutine stack");
830 			*ret_stack = (byte*)p;
831 		} else {
832 			*ret_stack = runtime_mallocgc(stacksize, nil, false);
833 			runtime_xadd(&runtime_stacks_sys, stacksize);
834 		}
835 		*ret_stacksize = (uintptr)stacksize;
836 		newg->gcinitialsp = *ret_stack;
837 		newg->gcstacksize = (uintptr)stacksize;
838 		newg->gcinitialsp2 = initial_secondary_stack_pointer(*ret_stack);
839 #endif
840 	}
841 	return newg;
842 }
843 
844 void stackfree(G*)
845   __asm__(GOSYM_PREFIX "runtime.stackfree");
846 
847 // stackfree frees the stack of a g.
848 void
stackfree(G * gp)849 stackfree(G* gp)
850 {
851 #ifdef USING_SPLIT_STACK
852   __splitstack_releasecontext((void*)(&gp->stackcontext[0]));
853 #else
854   // If gcstacksize is 0, the stack is allocated by libc and will be
855   // released when the thread exits. Otherwise, in 64-bit mode it was
856   // allocated using sysAlloc and in 32-bit mode it was allocated
857   // using garbage collected memory.
858   if (gp->gcstacksize != 0) {
859     if (sizeof(void*) == 8) {
860       runtime_sysFree(gp->gcinitialsp, gp->gcstacksize, &getMemstats()->stacks_sys);
861     }
862     gp->gcinitialsp = nil;
863     gp->gcstacksize = 0;
864   }
865 #endif
866 }
867 
868 void resetNewG(G*, void **, uintptr*)
869   __asm__(GOSYM_PREFIX "runtime.resetNewG");
870 
871 // Reset stack information for g pulled out of the cache to start a
872 // new goroutine.
873 void
resetNewG(G * newg,void ** sp,uintptr * spsize)874 resetNewG(G *newg, void **sp, uintptr *spsize)
875 {
876 #ifdef USING_SPLIT_STACK
877   int dont_block_signals = 0;
878   size_t ss_spsize;
879 
880   *sp = __splitstack_resetcontext((void*)(&newg->stackcontext[0]), &ss_spsize);
881   *spsize = ss_spsize;
882   __splitstack_block_signals_context((void*)(&newg->stackcontext[0]),
883 				     &dont_block_signals, nil);
884 #else
885   *sp = newg->gcinitialsp;
886   *spsize = newg->gcstacksize;
887   if(*spsize == 0)
888     runtime_throw("bad spsize in resetNewG");
889   newg->gcnextsp = (uintptr)(*sp);
890   newg->gcnextsp2 = (uintptr)(newg->gcinitialsp2);
891 #endif
892 }
893 
894 // Return whether we are waiting for a GC.  This gc toolchain uses
895 // preemption instead.
896 bool
runtime_gcwaiting(void)897 runtime_gcwaiting(void)
898 {
899 	return runtime_sched->gcwaiting;
900 }
901