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.
14 #ifdef HAVE_DL_ITERATE_PHDR
23 #ifdef USING_SPLIT_STACK
25 /* FIXME: These are not declared anywhere. */
27 extern void __splitstack_getcontext(void *context
[10]);
29 extern void __splitstack_setcontext(void *context
[10]);
31 extern void *__splitstack_makecontext(size_t, void *context
[10], size_t *);
33 extern void * __splitstack_resetcontext(void *context
[10], size_t *);
35 extern void __splitstack_releasecontext(void *context
[10]);
37 extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
40 extern void __splitstack_block_signals (int *, int *);
42 extern void __splitstack_block_signals_context (void *context
[10], int *,
47 #ifndef PTHREAD_STACK_MIN
48 # define PTHREAD_STACK_MIN 8192
51 #if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
52 # define StackMin PTHREAD_STACK_MIN
54 # define StackMin ((sizeof(char *) < 8) ? 2 * 1024 * 1024 : 4 * 1024 * 1024)
57 uintptr runtime_stacks_sys
;
60 __asm__(GOSYM_PREFIX
"runtime.gtraceback");
68 #ifndef SETCONTEXT_CLOBBERS_TLS
76 fixcontext(ucontext_t
*c
__attribute__ ((unused
)))
82 # if defined(__x86_64__) && defined(__sun__)
84 // x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
85 // register to that of the thread which called getcontext. The effect
86 // is that the address of all __thread variables changes. This bug
87 // also affects pthread_self() and pthread_getspecific. We work
88 // around it by clobbering the context field directly to keep %fs the
91 static __thread greg_t fs
;
99 fs
= c
.uc_mcontext
.gregs
[REG_FSBASE
];
103 fixcontext(ucontext_t
* c
)
105 c
->uc_mcontext
.gregs
[REG_FSBASE
] = fs
;
108 # elif defined(__NetBSD__)
110 // NetBSD has a bug: setcontext clobbers tlsbase, we need to save
111 // and restore it ourselves.
113 static __thread __greg_t tlsbase
;
121 tlsbase
= c
.uc_mcontext
._mc_tlsbase
;
125 fixcontext(ucontext_t
* c
)
127 c
->uc_mcontext
._mc_tlsbase
= tlsbase
;
130 # elif defined(__sparc__)
138 fixcontext(ucontext_t
*c
)
141 register unsigned long thread __asm__("%g7");
142 c->uc_mcontext.gregs[REG_G7] = thread;
144 error: variable ‘thread’ might be clobbered by \
145 ‘longjmp’ or ‘vfork’ [-Werror=clobbered]
146 which ought to be false, as %g7 is a fixed register. */
148 if (sizeof (c
->uc_mcontext
.gregs
[REG_G7
]) == 8)
149 asm ("stx %%g7, %0" : "=m"(c
->uc_mcontext
.gregs
[REG_G7
]));
151 asm ("st %%g7, %0" : "=m"(c
->uc_mcontext
.gregs
[REG_G7
]));
162 fixcontext(ucontext_t
* c
)
164 // Thread pointer is in r13, per 64-bit ABI.
165 if (sizeof (c
->uc_mcontext
.jmp_context
.gpr
[13]) == 8)
166 asm ("std 13, %0" : "=m"(c
->uc_mcontext
.jmp_context
.gpr
[13]));
171 # error unknown case for SETCONTEXT_CLOBBERS_TLS
177 // ucontext_arg returns a properly aligned ucontext_t value. On some
178 // systems a ucontext_t value must be aligned to a 16-byte boundary.
179 // The g structure that has fields of type ucontext_t is defined in
180 // Go, and Go has no simple way to align a field to such a boundary.
181 // So we make the field larger in runtime2.go and pick an appropriate
182 // offset within the field here.
184 ucontext_arg(uintptr_t* go_ucontext
)
186 uintptr_t p
= (uintptr_t)go_ucontext
;
187 size_t align
= __alignof__(ucontext_t
);
189 // We only ensured space for up to a 16 byte alignment
190 // in libgo/go/runtime/runtime2.go.
191 runtime_throw("required alignment of ucontext_t too large");
193 p
= (p
+ align
- 1) &~ (uintptr_t)(align
- 1);
194 return (ucontext_t
*)p
;
197 // We can not always refer to the TLS variables directly. The
198 // compiler will call tls_get_addr to get the address of the variable,
199 // and it may hold it in a register across a call to schedule. When
200 // we get back from the call we may be running in a different thread,
201 // in which case the register now points to the TLS variable for a
202 // different thread. We use non-inlinable functions to avoid this
205 G
* runtime_g(void) __attribute__ ((noinline
, no_split_stack
));
213 M
* runtime_m(void) __attribute__ ((noinline
, no_split_stack
));
230 void runtime_newosproc(M
*)
231 __asm__(GOSYM_PREFIX
"runtime.newosproc");
233 // Start a new thread.
235 runtime_newosproc(M
*mp
)
243 if(pthread_attr_init(&attr
) != 0)
244 runtime_throw("pthread_attr_init");
245 if(pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
) != 0)
246 runtime_throw("pthread_attr_setdetachstate");
248 // Block signals during pthread_create so that the new thread
249 // starts with signals disabled. It will enable them in minit.
253 // Blocking SIGTRAP reportedly breaks gdb on Alpha GNU/Linux.
254 sigdelset(&clear
, SIGTRAP
);
258 pthread_sigmask(SIG_BLOCK
, &clear
, &old
);
260 for (tries
= 0; tries
< 20; tries
++) {
261 ret
= pthread_create(&tid
, &attr
, runtime_mstart
, mp
);
265 runtime_usleep((tries
+ 1) * 1000); // Milliseconds.
268 pthread_sigmask(SIG_SETMASK
, &old
, nil
);
271 runtime_printf("pthread_create failed: %d\n", ret
);
272 runtime_throw("pthread_create");
275 if(pthread_attr_destroy(&attr
) != 0)
276 runtime_throw("pthread_attr_destroy");
279 // Switch context to a different goroutine. This is like longjmp.
280 void runtime_gogo(G
*) __attribute__ ((noinline
));
282 runtime_gogo(G
* newg
)
284 #ifdef USING_SPLIT_STACK
285 __splitstack_setcontext((void*)(&newg
->stackcontext
[0]));
288 newg
->fromgogo
= true;
289 fixcontext(ucontext_arg(&newg
->context
[0]));
290 setcontext(ucontext_arg(&newg
->context
[0]));
291 runtime_throw("gogo setcontext returned");
294 // Save context and call fn passing g as a parameter. This is like
295 // setjmp. Because getcontext always returns 0, unlike setjmp, we use
296 // g->fromgogo as a code. It will be true if we got here via
297 // setcontext. g == nil the first time this is called in a new m.
298 void runtime_mcall(FuncVal
*) __attribute__ ((noinline
));
300 runtime_mcall(FuncVal
*fv
)
304 #ifndef USING_SPLIT_STACK
308 // Ensure that all registers are on the stack for the garbage
310 __builtin_unwind_init();
311 flush_registers_to_secondary_stack();
316 runtime_throw("runtime: mcall called on m->g0 stack");
320 #ifdef USING_SPLIT_STACK
321 __splitstack_getcontext((void*)(&g
->stackcontext
[0]));
323 // We have to point to an address on the stack that is
324 // below the saved registers.
325 gp
->gcnextsp
= (uintptr
)(&afterregs
);
326 gp
->gcnextsp2
= (uintptr
)(secondary_stack_pointer());
328 gp
->fromgogo
= false;
329 getcontext(ucontext_arg(&gp
->context
[0]));
331 // When we return from getcontext, we may be running
332 // in a new thread. That means that g may have
333 // changed. It is a global variables so we will
334 // reload it, but the address of g may be cached in
335 // our local stack frame, and that address may be
336 // wrong. Call the function to reload the value for
341 if(gp
->traceback
!= nil
)
344 if (gp
== nil
|| !gp
->fromgogo
) {
345 #ifdef USING_SPLIT_STACK
346 __splitstack_setcontext((void*)(&mp
->g0
->stackcontext
[0]));
351 // It's OK to set g directly here because this case
352 // can not occur if we got here via a setcontext to
353 // the getcontext call just above.
356 fixcontext(ucontext_arg(&mp
->g0
->context
[0]));
357 setcontext(ucontext_arg(&mp
->g0
->context
[0]));
358 runtime_throw("runtime: mcall function returned");
362 // Goroutine scheduler
363 // The scheduler's job is to distribute ready-to-run goroutines over worker threads.
365 // The main concepts are:
367 // M - worker thread, or machine.
368 // P - processor, a resource that is required to execute Go code.
369 // M must have an associated P to execute Go code, however it can be
370 // blocked or in a syscall w/o an associated P.
372 // Design doc at http://golang.org/s/go11sched.
374 extern G
* allocg(void)
375 __asm__ (GOSYM_PREFIX
"runtime.allocg");
377 Sched
* runtime_sched
;
379 bool runtime_isarchive
;
381 extern void kickoff(void)
382 __asm__(GOSYM_PREFIX
"runtime.kickoff");
383 extern void minit(void)
384 __asm__(GOSYM_PREFIX
"runtime.minit");
385 extern void mstart1(int32
)
386 __asm__(GOSYM_PREFIX
"runtime.mstart1");
387 extern void stopm(void)
388 __asm__(GOSYM_PREFIX
"runtime.stopm");
389 extern void mexit(bool)
390 __asm__(GOSYM_PREFIX
"runtime.mexit");
391 extern void handoffp(P
*)
392 __asm__(GOSYM_PREFIX
"runtime.handoffp");
393 extern void wakep(void)
394 __asm__(GOSYM_PREFIX
"runtime.wakep");
395 extern void stoplockedm(void)
396 __asm__(GOSYM_PREFIX
"runtime.stoplockedm");
397 extern void schedule(void)
398 __asm__(GOSYM_PREFIX
"runtime.schedule");
399 extern void execute(G
*, bool)
400 __asm__(GOSYM_PREFIX
"runtime.execute");
401 extern void reentersyscall(uintptr
, uintptr
)
402 __asm__(GOSYM_PREFIX
"runtime.reentersyscall");
403 extern void reentersyscallblock(uintptr
, uintptr
)
404 __asm__(GOSYM_PREFIX
"runtime.reentersyscallblock");
406 __asm__(GOSYM_PREFIX
"runtime.gfget");
407 extern void acquirep(P
*)
408 __asm__(GOSYM_PREFIX
"runtime.acquirep");
409 extern P
* releasep(void)
410 __asm__(GOSYM_PREFIX
"runtime.releasep");
411 extern void incidlelocked(int32
)
412 __asm__(GOSYM_PREFIX
"runtime.incidlelocked");
413 extern void globrunqput(G
*)
414 __asm__(GOSYM_PREFIX
"runtime.globrunqput");
415 extern P
* pidleget(void)
416 __asm__(GOSYM_PREFIX
"runtime.pidleget");
417 extern struct mstats
* getMemstats(void)
418 __asm__(GOSYM_PREFIX
"runtime.getMemstats");
420 bool runtime_isstarted
;
422 // Used to determine the field alignment.
430 void getTraceback(G
*, G
*) __asm__(GOSYM_PREFIX
"runtime.getTraceback");
432 // getTraceback stores a traceback of gp in the g's traceback field
433 // and then returns to me. We expect that gp's traceback is not nil.
434 // It works by saving me's current context, and checking gp's traceback field.
435 // If gp's traceback field is not nil, it starts running gp.
436 // In places where we call getcontext, we check the traceback field.
437 // If it is not nil, we collect a traceback, and then return to the
438 // goroutine stored in the traceback field, which is me.
439 void getTraceback(G
* me
, G
* gp
)
441 #ifdef USING_SPLIT_STACK
442 __splitstack_getcontext((void*)(&me
->stackcontext
[0]));
444 getcontext(ucontext_arg(&me
->context
[0]));
446 if (gp
->traceback
!= nil
) {
451 // Do a stack trace of gp, and then restore the context to
452 // gp->traceback->gp.
457 Traceback
* traceback
;
460 traceback
= gp
->traceback
;
463 if(holdm
!= nil
&& holdm
!= g
->m
)
464 runtime_throw("gtraceback: m is not nil");
465 gp
->m
= traceback
->gp
->m
;
466 traceback
->c
= runtime_callers(1, traceback
->locbuf
,
467 sizeof traceback
->locbuf
/ sizeof traceback
->locbuf
[0], false);
469 runtime_gogo(traceback
->gp
);
472 // Called by pthread_create to start an M.
474 runtime_mstart(void *arg
)
488 // We have to call minit before we call getcontext,
489 // because getcontext will copy the signal mask.
494 // Record top of stack for use by mcall.
495 // Once we call schedule we're never coming back,
496 // so other calls can reuse this stack space.
497 #ifdef USING_SPLIT_STACK
498 __splitstack_getcontext((void*)(&gp
->stackcontext
[0]));
500 gp
->gcinitialsp
= &arg
;
501 // Setting gcstacksize to 0 is a marker meaning that gcinitialsp
502 // is the top of the stack, not the bottom.
504 gp
->gcnextsp
= (uintptr
)(&arg
);
505 gp
->gcinitialsp2
= secondary_stack_pointer();
506 gp
->gcnextsp2
= (uintptr
)(gp
->gcinitialsp2
);
509 // Save the currently active context. This will return
510 // multiple times via the setcontext call in mcall.
511 getcontext(ucontext_arg(&gp
->context
[0]));
513 if(gp
->traceback
!= nil
) {
514 // Got here from getTraceback.
515 // I'm not sure this ever actually happens--getTraceback
516 // may always go to the getcontext call in mcall.
520 if(gp
->entry
!= nil
) {
521 // Got here from mcall.
522 FuncVal
*fv
= gp
->entry
;
523 void (*pfn
)(G
*) = (void (*)(G
*))fv
->fn
;
524 G
* gp1
= (G
*)gp
->param
;
527 __builtin_call_with_static_chain(pfn(gp1
), fv
);
536 // Initial call to getcontext--starting thread.
538 #ifdef USING_SPLIT_STACK
540 int dont_block_signals
= 0;
541 __splitstack_block_signals(&dont_block_signals
, nil
);
547 // mstart1 does not return, but we need a return statement
548 // here to avoid a compiler warning.
552 typedef struct CgoThreadStart CgoThreadStart
;
553 struct CgoThreadStart
561 void setGContext(void) __asm__ (GOSYM_PREFIX
"runtime.setGContext");
563 // setGContext sets up a new goroutine context for the current g.
574 #ifdef USING_SPLIT_STACK
575 __splitstack_getcontext((void*)(&gp
->stackcontext
[0]));
577 __splitstack_block_signals(&val
, nil
);
579 gp
->gcinitialsp
= &val
;
582 gp
->gcnextsp
= (uintptr
)(&val
);
583 gp
->gcinitialsp2
= secondary_stack_pointer();
584 gp
->gcnextsp2
= (uintptr
)(gp
->gcinitialsp2
);
586 getcontext(ucontext_arg(&gp
->context
[0]));
588 if(gp
->entry
!= nil
) {
589 // Got here from mcall.
590 FuncVal
*fv
= gp
->entry
;
591 void (*pfn
)(G
*) = (void (*)(G
*))fv
->fn
;
592 G
* gp1
= (G
*)gp
->param
;
595 __builtin_call_with_static_chain(pfn(gp1
), fv
);
600 void makeGContext(G
*, byte
*, uintptr
)
601 __asm__(GOSYM_PREFIX
"runtime.makeGContext");
603 // makeGContext makes a new context for a g.
605 makeGContext(G
* gp
, byte
* sp
, uintptr spsize
) {
608 uc
= ucontext_arg(&gp
->context
[0]);
610 uc
->uc_stack
.ss_sp
= sp
;
611 uc
->uc_stack
.ss_size
= (size_t)spsize
;
612 makecontext(uc
, kickoff
, 0);
615 // The goroutine g is about to enter a system call.
616 // Record that it's not using the cpu anymore.
617 // This is called only from the go syscall library and cgocall,
618 // not from the low-level system calls used by the runtime.
620 // Entersyscall cannot split the stack: the runtime_gosave must
621 // make g->sched refer to the caller's stack segment, because
622 // entersyscall is going to return immediately after.
624 void runtime_entersyscall(int32
) __attribute__ ((no_split_stack
));
625 static void doentersyscall(uintptr
, uintptr
)
626 __attribute__ ((no_split_stack
, noinline
));
629 runtime_entersyscall(int32 dummy
__attribute__ ((unused
)))
631 // Save the registers in the g structure so that any pointers
632 // held in registers will be seen by the garbage collector.
633 getcontext(ucontext_arg(&g
->gcregs
[0]));
635 // Note that if this function does save any registers itself,
636 // we might store the wrong value in the call to getcontext.
637 // FIXME: This assumes that we do not need to save any
638 // callee-saved registers to access the TLS variable g. We
639 // don't want to put the ucontext_t on the stack because it is
640 // large and we can not split the stack here.
641 doentersyscall((uintptr
)runtime_getcallerpc(&dummy
),
642 (uintptr
)runtime_getcallersp(&dummy
));
646 doentersyscall(uintptr pc
, uintptr sp
)
648 // Leave SP around for GC and traceback.
649 #ifdef USING_SPLIT_STACK
652 g
->gcstack
= (uintptr
)(__splitstack_find(nil
, nil
, &gcstacksize
,
653 (void**)(&g
->gcnextsegment
),
654 (void**)(&g
->gcnextsp
),
656 g
->gcstacksize
= (uintptr
)gcstacksize
;
662 g
->gcnextsp
= (uintptr
)(&v
);
663 g
->gcnextsp2
= (uintptr
)(secondary_stack_pointer());
667 reentersyscall(pc
, sp
);
670 static void doentersyscallblock(uintptr
, uintptr
)
671 __attribute__ ((no_split_stack
, noinline
));
673 // The same as runtime_entersyscall(), but with a hint that the syscall is blocking.
675 runtime_entersyscallblock(int32 dummy
__attribute__ ((unused
)))
677 // Save the registers in the g structure so that any pointers
678 // held in registers will be seen by the garbage collector.
679 getcontext(ucontext_arg(&g
->gcregs
[0]));
681 // See comment in runtime_entersyscall.
682 doentersyscallblock((uintptr
)runtime_getcallerpc(&dummy
),
683 (uintptr
)runtime_getcallersp(&dummy
));
687 doentersyscallblock(uintptr pc
, uintptr sp
)
689 // Leave SP around for GC and traceback.
690 #ifdef USING_SPLIT_STACK
693 g
->gcstack
= (uintptr
)(__splitstack_find(nil
, nil
, &gcstacksize
,
694 (void**)(&g
->gcnextsegment
),
695 (void**)(&g
->gcnextsp
),
697 g
->gcstacksize
= (uintptr
)gcstacksize
;
703 g
->gcnextsp
= (uintptr
)(&v
);
704 g
->gcnextsp2
= (uintptr
)(secondary_stack_pointer());
708 reentersyscallblock(pc
, sp
);
711 // Allocate a new g, with a stack big enough for stacksize bytes.
713 runtime_malg(bool allocatestack
, bool signalstack
, byte
** ret_stack
, uintptr
* ret_stacksize
)
718 uintptr unused_stacksize
;
719 #if USING_SPLIT_STACK
720 int dont_block_signals
= 0;
724 if (ret_stack
== nil
) {
725 ret_stack
= &unused_stack
;
727 if (ret_stacksize
== nil
) {
728 ret_stacksize
= &unused_stacksize
;
732 stacksize
= StackMin
;
734 stacksize
= 32 * 1024; // OS X wants >= 8K, GNU/Linux >= 2K
736 if(stacksize
< SIGSTKSZ
)
737 stacksize
= SIGSTKSZ
;
741 #if USING_SPLIT_STACK
742 *ret_stack
= __splitstack_makecontext(stacksize
,
743 (void*)(&newg
->stackcontext
[0]),
745 *ret_stacksize
= (uintptr
)ss_stacksize
;
746 __splitstack_block_signals_context((void*)(&newg
->stackcontext
[0]),
747 &dont_block_signals
, nil
);
749 // In 64-bit mode, the maximum Go allocation space is
750 // 128G. Our stack size is 4M, which only permits 32K
751 // goroutines. In order to not limit ourselves,
752 // allocate the stacks out of separate memory. In
753 // 32-bit mode, the Go allocation space is all of
755 if(sizeof(void*) == 8) {
756 void *p
= runtime_sysAlloc(stacksize
, &getMemstats()->stacks_sys
);
758 runtime_throw("runtime: cannot allocate memory for goroutine stack");
759 *ret_stack
= (byte
*)p
;
761 *ret_stack
= runtime_mallocgc(stacksize
, nil
, false);
762 runtime_xadd(&runtime_stacks_sys
, stacksize
);
764 *ret_stacksize
= (uintptr
)stacksize
;
765 newg
->gcinitialsp
= *ret_stack
;
766 newg
->gcstacksize
= (uintptr
)stacksize
;
767 newg
->gcinitialsp2
= initial_secondary_stack_pointer(*ret_stack
);
774 __asm__(GOSYM_PREFIX
"runtime.stackfree");
776 // stackfree frees the stack of a g.
780 #if USING_SPLIT_STACK
781 __splitstack_releasecontext((void*)(&gp
->stackcontext
[0]));
783 // If gcstacksize is 0, the stack is allocated by libc and will be
784 // released when the thread exits. Otherwise, in 64-bit mode it was
785 // allocated using sysAlloc and in 32-bit mode it was allocated
786 // using garbage collected memory.
787 if (gp
->gcstacksize
!= 0) {
788 if (sizeof(void*) == 8) {
789 runtime_sysFree(gp
->gcinitialsp
, gp
->gcstacksize
, &getMemstats()->stacks_sys
);
791 gp
->gcinitialsp
= nil
;
797 void resetNewG(G
*, void **, uintptr
*)
798 __asm__(GOSYM_PREFIX
"runtime.resetNewG");
800 // Reset stack information for g pulled out of the cache to start a
803 resetNewG(G
*newg
, void **sp
, uintptr
*spsize
)
805 #ifdef USING_SPLIT_STACK
806 int dont_block_signals
= 0;
809 *sp
= __splitstack_resetcontext((void*)(&newg
->stackcontext
[0]), &ss_spsize
);
811 __splitstack_block_signals_context((void*)(&newg
->stackcontext
[0]),
812 &dont_block_signals
, nil
);
814 *sp
= newg
->gcinitialsp
;
815 *spsize
= newg
->gcstacksize
;
817 runtime_throw("bad spsize in resetNewG");
818 newg
->gcnextsp
= (uintptr
)(*sp
);
819 newg
->gcnextsp2
= (uintptr
)(newg
->gcinitialsp2
);
823 // Return whether we are waiting for a GC. This gc toolchain uses
824 // preemption instead.
826 runtime_gcwaiting(void)
828 return runtime_sched
->gcwaiting
;