Retro68/gcc/libgo/runtime/proc.c

830 lines
21 KiB
C
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include <errno.h>
2012-03-27 23:13:14 +00:00
#include <limits.h>
2014-09-21 17:33:12 +00:00
#include <signal.h>
2012-03-27 23:13:14 +00:00
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "config.h"
2014-09-21 17:33:12 +00:00
#ifdef HAVE_DL_ITERATE_PHDR
#include <link.h>
#endif
2012-03-27 23:13:14 +00:00
#include "runtime.h"
#include "arch.h"
#include "defs.h"
2014-09-21 17:33:12 +00:00
#include "go-type.h"
2012-03-27 23:13:14 +00:00
#ifdef USING_SPLIT_STACK
/* FIXME: These are not declared anywhere. */
extern void __splitstack_getcontext(void *context[10]);
extern void __splitstack_setcontext(void *context[10]);
extern void *__splitstack_makecontext(size_t, void *context[10], size_t *);
extern void * __splitstack_resetcontext(void *context[10], size_t *);
2018-12-28 15:30:48 +00:00
extern void __splitstack_releasecontext(void *context[10]);
2012-03-27 23:13:14 +00:00
extern void *__splitstack_find(void *, void *, size_t *, void **, void **,
void **);
extern void __splitstack_block_signals (int *, int *);
extern void __splitstack_block_signals_context (void *context[10], int *,
int *);
#endif
2014-09-21 17:33:12 +00:00
#ifndef PTHREAD_STACK_MIN
# define PTHREAD_STACK_MIN 8192
#endif
2012-03-27 23:13:14 +00:00
#if defined(USING_SPLIT_STACK) && defined(LINKER_SUPPORTS_SPLIT_STACK)
2014-09-21 17:33:12 +00:00
# define StackMin PTHREAD_STACK_MIN
2012-03-27 23:13:14 +00:00
#else
2015-08-28 15:33:40 +00:00
# define StackMin ((sizeof(char *) < 8) ? 2 * 1024 * 1024 : 4 * 1024 * 1024)
2012-03-27 23:13:14 +00:00
#endif
2014-09-21 17:33:12 +00:00
uintptr runtime_stacks_sys;
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
void gtraceback(G*)
__asm__(GOSYM_PREFIX "runtime.gtraceback");
2012-03-27 23:13:14 +00:00
#ifdef __rtems__
#define __thread
#endif
static __thread G *g;
#ifndef SETCONTEXT_CLOBBERS_TLS
static inline void
initcontext(void)
{
}
static inline void
fixcontext(ucontext_t *c __attribute__ ((unused)))
{
}
2014-09-21 17:33:12 +00:00
#else
2012-03-27 23:13:14 +00:00
# if defined(__x86_64__) && defined(__sun__)
// x86_64 Solaris 10 and 11 have a bug: setcontext switches the %fs
// register to that of the thread which called getcontext. The effect
// is that the address of all __thread variables changes. This bug
// also affects pthread_self() and pthread_getspecific. We work
// around it by clobbering the context field directly to keep %fs the
// same.
static __thread greg_t fs;
static inline void
initcontext(void)
{
ucontext_t c;
getcontext(&c);
fs = c.uc_mcontext.gregs[REG_FSBASE];
}
static inline void
fixcontext(ucontext_t* c)
{
c->uc_mcontext.gregs[REG_FSBASE] = fs;
}
2014-09-21 17:33:12 +00:00
# elif defined(__NetBSD__)
// NetBSD has a bug: setcontext clobbers tlsbase, we need to save
// and restore it ourselves.
static __thread __greg_t tlsbase;
static inline void
initcontext(void)
{
ucontext_t c;
getcontext(&c);
tlsbase = c.uc_mcontext._mc_tlsbase;
}
static inline void
fixcontext(ucontext_t* c)
{
c->uc_mcontext._mc_tlsbase = tlsbase;
}
2015-08-28 15:33:40 +00:00
# elif defined(__sparc__)
static inline void
initcontext(void)
{
}
static inline void
fixcontext(ucontext_t *c)
{
/* ??? Using
register unsigned long thread __asm__("%g7");
c->uc_mcontext.gregs[REG_G7] = thread;
results in
error: variable thread might be clobbered by \
longjmp or vfork [-Werror=clobbered]
which ought to be false, as %g7 is a fixed register. */
if (sizeof (c->uc_mcontext.gregs[REG_G7]) == 8)
asm ("stx %%g7, %0" : "=m"(c->uc_mcontext.gregs[REG_G7]));
else
asm ("st %%g7, %0" : "=m"(c->uc_mcontext.gregs[REG_G7]));
}
2018-12-28 15:30:48 +00:00
# elif defined(_AIX)
static inline void
initcontext(void)
{
}
static inline void
fixcontext(ucontext_t* c)
{
// Thread pointer is in r13, per 64-bit ABI.
if (sizeof (c->uc_mcontext.jmp_context.gpr[13]) == 8)
asm ("std 13, %0" : "=m"(c->uc_mcontext.jmp_context.gpr[13]));
}
2012-03-27 23:13:14 +00:00
# else
# error unknown case for SETCONTEXT_CLOBBERS_TLS
# endif
#endif
// ucontext_arg returns a properly aligned ucontext_t value. On some
// systems a ucontext_t value must be aligned to a 16-byte boundary.
// The g structure that has fields of type ucontext_t is defined in
// Go, and Go has no simple way to align a field to such a boundary.
// So we make the field larger in runtime2.go and pick an appropriate
// offset within the field here.
static ucontext_t*
2018-12-28 15:30:48 +00:00
ucontext_arg(uintptr_t* go_ucontext)
{
uintptr_t p = (uintptr_t)go_ucontext;
size_t align = __alignof__(ucontext_t);
if(align > 16) {
// We only ensured space for up to a 16 byte alignment
// in libgo/go/runtime/runtime2.go.
runtime_throw("required alignment of ucontext_t too large");
}
p = (p + align - 1) &~ (uintptr_t)(align - 1);
return (ucontext_t*)p;
}
2012-03-27 23:13:14 +00:00
// We can not always refer to the TLS variables directly. The
// compiler will call tls_get_addr to get the address of the variable,
// and it may hold it in a register across a call to schedule. When
// we get back from the call we may be running in a different thread,
// in which case the register now points to the TLS variable for a
// different thread. We use non-inlinable functions to avoid this
// when necessary.
G* runtime_g(void) __attribute__ ((noinline, no_split_stack));
G*
runtime_g(void)
{
return g;
}
M* runtime_m(void) __attribute__ ((noinline, no_split_stack));
M*
runtime_m(void)
{
if(g == nil)
return nil;
return g->m;
2012-03-27 23:13:14 +00:00
}
// Set g.
2014-09-21 17:33:12 +00:00
void
runtime_setg(G* gp)
2014-09-21 17:33:12 +00:00
{
g = gp;
}
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
void runtime_newosproc(M *)
__asm__(GOSYM_PREFIX "runtime.newosproc");
2014-09-21 17:33:12 +00:00
// Start a new thread.
2018-12-28 15:30:48 +00:00
void
2014-09-21 17:33:12 +00:00
runtime_newosproc(M *mp)
{
pthread_attr_t attr;
sigset_t clear, old;
pthread_t tid;
int tries;
2014-09-21 17:33:12 +00:00
int ret;
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
if(pthread_attr_init(&attr) != 0)
runtime_throw("pthread_attr_init");
if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
runtime_throw("pthread_attr_setdetachstate");
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
// Block signals during pthread_create so that the new thread
// starts with signals disabled. It will enable them in minit.
sigfillset(&clear);
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
#ifdef SIGTRAP
// Blocking SIGTRAP reportedly breaks gdb on Alpha GNU/Linux.
sigdelset(&clear, SIGTRAP);
#endif
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
sigemptyset(&old);
pthread_sigmask(SIG_BLOCK, &clear, &old);
for (tries = 0; tries < 20; tries++) {
ret = pthread_create(&tid, &attr, runtime_mstart, mp);
if (ret != EAGAIN) {
break;
}
runtime_usleep((tries + 1) * 1000); // Milliseconds.
}
2014-09-21 17:33:12 +00:00
pthread_sigmask(SIG_SETMASK, &old, nil);
2012-03-27 23:13:14 +00:00
if (ret != 0) {
runtime_printf("pthread_create failed: %d\n", ret);
2014-09-21 17:33:12 +00:00
runtime_throw("pthread_create");
}
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
if(pthread_attr_destroy(&attr) != 0)
runtime_throw("pthread_attr_destroy");
2012-03-27 23:13:14 +00:00
}
// Switch context to a different goroutine. This is like longjmp.
2014-09-21 17:33:12 +00:00
void runtime_gogo(G*) __attribute__ ((noinline));
void
2012-03-27 23:13:14 +00:00
runtime_gogo(G* newg)
{
#ifdef USING_SPLIT_STACK
2018-12-28 15:30:48 +00:00
__splitstack_setcontext((void*)(&newg->stackcontext[0]));
2012-03-27 23:13:14 +00:00
#endif
g = newg;
newg->fromgogo = true;
fixcontext(ucontext_arg(&newg->context[0]));
setcontext(ucontext_arg(&newg->context[0]));
2012-03-27 23:13:14 +00:00
runtime_throw("gogo setcontext returned");
}
// Save context and call fn passing g as a parameter. This is like
// setjmp. Because getcontext always returns 0, unlike setjmp, we use
// g->fromgogo as a code. It will be true if we got here via
// setcontext. g == nil the first time this is called in a new m.
2018-12-28 15:30:48 +00:00
void runtime_mcall(FuncVal *) __attribute__ ((noinline));
2014-09-21 17:33:12 +00:00
void
2018-12-28 15:30:48 +00:00
runtime_mcall(FuncVal *fv)
2012-03-27 23:13:14 +00:00
{
M *mp;
G *gp;
#ifndef USING_SPLIT_STACK
void *afterregs;
#endif
2012-03-27 23:13:14 +00:00
// Ensure that all registers are on the stack for the garbage
// collector.
__builtin_unwind_init();
2018-12-28 15:30:48 +00:00
flush_registers_to_secondary_stack();
2012-03-27 23:13:14 +00:00
gp = g;
mp = gp->m;
2012-03-27 23:13:14 +00:00
if(gp == mp->g0)
runtime_throw("runtime: mcall called on m->g0 stack");
if(gp != nil) {
#ifdef USING_SPLIT_STACK
2018-12-28 15:30:48 +00:00
__splitstack_getcontext((void*)(&g->stackcontext[0]));
2012-03-27 23:13:14 +00:00
#else
// We have to point to an address on the stack that is
// below the saved registers.
2018-12-28 15:30:48 +00:00
gp->gcnextsp = (uintptr)(&afterregs);
gp->gcnextsp2 = (uintptr)(secondary_stack_pointer());
2012-03-27 23:13:14 +00:00
#endif
gp->fromgogo = false;
getcontext(ucontext_arg(&gp->context[0]));
2012-03-27 23:13:14 +00:00
// When we return from getcontext, we may be running
// in a new thread. That means that g may have
// changed. It is a global variables so we will
// reload it, but the address of g may be cached in
// our local stack frame, and that address may be
// wrong. Call the function to reload the value for
// this thread.
2012-03-27 23:13:14 +00:00
gp = runtime_g();
mp = gp->m;
2014-09-21 17:33:12 +00:00
if(gp->traceback != nil)
gtraceback(gp);
2012-03-27 23:13:14 +00:00
}
if (gp == nil || !gp->fromgogo) {
#ifdef USING_SPLIT_STACK
2018-12-28 15:30:48 +00:00
__splitstack_setcontext((void*)(&mp->g0->stackcontext[0]));
2012-03-27 23:13:14 +00:00
#endif
2018-12-28 15:30:48 +00:00
mp->g0->entry = fv;
2012-03-27 23:13:14 +00:00
mp->g0->param = gp;
// It's OK to set g directly here because this case
// can not occur if we got here via a setcontext to
// the getcontext call just above.
g = mp->g0;
fixcontext(ucontext_arg(&mp->g0->context[0]));
setcontext(ucontext_arg(&mp->g0->context[0]));
2012-03-27 23:13:14 +00:00
runtime_throw("runtime: mcall function returned");
}
}
2014-09-21 17:33:12 +00:00
// Goroutine scheduler
// The scheduler's job is to distribute ready-to-run goroutines over worker threads.
//
// The main concepts are:
// G - goroutine.
// M - worker thread, or machine.
// P - processor, a resource that is required to execute Go code.
// M must have an associated P to execute Go code, however it can be
// blocked or in a syscall w/o an associated P.
//
// Design doc at http://golang.org/s/go11sched.
extern G* allocg(void)
__asm__ (GOSYM_PREFIX "runtime.allocg");
Sched* runtime_sched;
2015-08-28 15:33:40 +00:00
bool runtime_isarchive;
2018-12-28 15:30:48 +00:00
extern void kickoff(void)
__asm__(GOSYM_PREFIX "runtime.kickoff");
extern void minit(void)
__asm__(GOSYM_PREFIX "runtime.minit");
extern void mstart1(int32)
__asm__(GOSYM_PREFIX "runtime.mstart1");
extern void stopm(void)
__asm__(GOSYM_PREFIX "runtime.stopm");
2018-12-28 15:30:48 +00:00
extern void mexit(bool)
__asm__(GOSYM_PREFIX "runtime.mexit");
extern void handoffp(P*)
__asm__(GOSYM_PREFIX "runtime.handoffp");
extern void wakep(void)
__asm__(GOSYM_PREFIX "runtime.wakep");
extern void stoplockedm(void)
__asm__(GOSYM_PREFIX "runtime.stoplockedm");
extern void schedule(void)
__asm__(GOSYM_PREFIX "runtime.schedule");
extern void execute(G*, bool)
__asm__(GOSYM_PREFIX "runtime.execute");
2018-12-28 15:30:48 +00:00
extern void reentersyscall(uintptr, uintptr)
__asm__(GOSYM_PREFIX "runtime.reentersyscall");
extern void reentersyscallblock(uintptr, uintptr)
__asm__(GOSYM_PREFIX "runtime.reentersyscallblock");
extern G* gfget(P*)
__asm__(GOSYM_PREFIX "runtime.gfget");
extern void acquirep(P*)
__asm__(GOSYM_PREFIX "runtime.acquirep");
extern P* releasep(void)
__asm__(GOSYM_PREFIX "runtime.releasep");
extern void incidlelocked(int32)
__asm__(GOSYM_PREFIX "runtime.incidlelocked");
extern void globrunqput(G*)
__asm__(GOSYM_PREFIX "runtime.globrunqput");
extern P* pidleget(void)
__asm__(GOSYM_PREFIX "runtime.pidleget");
2018-12-28 15:30:48 +00:00
extern struct mstats* getMemstats(void)
__asm__(GOSYM_PREFIX "runtime.getMemstats");
2015-08-28 15:33:40 +00:00
bool runtime_isstarted;
2012-03-27 23:13:14 +00:00
2015-08-28 15:33:40 +00:00
// Used to determine the field alignment.
struct field_align
{
char c;
Hchan *p;
};
void getTraceback(G*, G*) __asm__(GOSYM_PREFIX "runtime.getTraceback");
2014-09-21 17:33:12 +00:00
// getTraceback stores a traceback of gp in the g's traceback field
// and then returns to me. We expect that gp's traceback is not nil.
// It works by saving me's current context, and checking gp's traceback field.
// If gp's traceback field is not nil, it starts running gp.
// In places where we call getcontext, we check the traceback field.
// If it is not nil, we collect a traceback, and then return to the
// goroutine stored in the traceback field, which is me.
void getTraceback(G* me, G* gp)
2014-09-21 17:33:12 +00:00
{
#ifdef USING_SPLIT_STACK
2018-12-28 15:30:48 +00:00
__splitstack_getcontext((void*)(&me->stackcontext[0]));
2014-09-21 17:33:12 +00:00
#endif
getcontext(ucontext_arg(&me->context[0]));
2014-09-21 17:33:12 +00:00
if (gp->traceback != nil) {
runtime_gogo(gp);
2014-09-21 17:33:12 +00:00
}
}
// Do a stack trace of gp, and then restore the context to
2018-12-28 15:30:48 +00:00
// gp->traceback->gp.
2014-09-21 17:33:12 +00:00
2018-12-28 15:30:48 +00:00
void
2014-09-21 17:33:12 +00:00
gtraceback(G* gp)
2012-03-27 23:13:14 +00:00
{
2014-09-21 17:33:12 +00:00
Traceback* traceback;
2018-12-28 15:30:48 +00:00
M* holdm;
2014-09-21 17:33:12 +00:00
traceback = gp->traceback;
gp->traceback = nil;
2018-12-28 15:30:48 +00:00
holdm = gp->m;
if(holdm != nil && holdm != g->m)
runtime_throw("gtraceback: m is not nil");
gp->m = traceback->gp->m;
2014-09-21 17:33:12 +00:00
traceback->c = runtime_callers(1, traceback->locbuf,
2015-08-28 15:33:40 +00:00
sizeof traceback->locbuf / sizeof traceback->locbuf[0], false);
2018-12-28 15:30:48 +00:00
gp->m = holdm;
2014-09-21 17:33:12 +00:00
runtime_gogo(traceback->gp);
2012-03-27 23:13:14 +00:00
}
2018-12-28 15:30:48 +00:00
// Called by pthread_create to start an M.
2012-03-27 23:13:14 +00:00
void*
2018-12-28 15:30:48 +00:00
runtime_mstart(void *arg)
2012-03-27 23:13:14 +00:00
{
2018-12-28 15:30:48 +00:00
M* mp;
G* gp;
2018-12-28 15:30:48 +00:00
mp = (M*)(arg);
gp = mp->g0;
gp->m = mp;
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
g = gp;
2012-03-27 23:13:14 +00:00
gp->entry = nil;
gp->param = nil;
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
// We have to call minit before we call getcontext,
// because getcontext will copy the signal mask.
minit();
initcontext();
2012-03-27 23:13:14 +00:00
// Record top of stack for use by mcall.
// Once we call schedule we're never coming back,
// so other calls can reuse this stack space.
#ifdef USING_SPLIT_STACK
2018-12-28 15:30:48 +00:00
__splitstack_getcontext((void*)(&gp->stackcontext[0]));
2012-03-27 23:13:14 +00:00
#else
2018-12-28 15:30:48 +00:00
gp->gcinitialsp = &arg;
// Setting gcstacksize to 0 is a marker meaning that gcinitialsp
2012-03-27 23:13:14 +00:00
// is the top of the stack, not the bottom.
gp->gcstacksize = 0;
2018-12-28 15:30:48 +00:00
gp->gcnextsp = (uintptr)(&arg);
gp->gcinitialsp2 = secondary_stack_pointer();
gp->gcnextsp2 = (uintptr)(gp->gcinitialsp2);
2012-03-27 23:13:14 +00:00
#endif
2018-12-28 15:30:48 +00:00
// Save the currently active context. This will return
// multiple times via the setcontext call in mcall.
getcontext(ucontext_arg(&gp->context[0]));
2018-12-28 15:30:48 +00:00
if(gp->traceback != nil) {
// Got here from getTraceback.
// I'm not sure this ever actually happens--getTraceback
// may always go to the getcontext call in mcall.
gtraceback(gp);
2018-12-28 15:30:48 +00:00
}
2012-03-27 23:13:14 +00:00
if(gp->entry != nil) {
2012-03-27 23:13:14 +00:00
// Got here from mcall.
2018-12-28 15:30:48 +00:00
FuncVal *fv = gp->entry;
void (*pfn)(G*) = (void (*)(G*))fv->fn;
G* gp1 = (G*)gp->param;
gp->entry = nil;
gp->param = nil;
2018-12-28 15:30:48 +00:00
__builtin_call_with_static_chain(pfn(gp1), fv);
2012-03-27 23:13:14 +00:00
*(int*)0x21 = 0x21;
}
2018-12-28 15:30:48 +00:00
if(mp->exiting) {
mexit(true);
return nil;
}
// Initial call to getcontext--starting thread.
2012-03-27 23:13:14 +00:00
#ifdef USING_SPLIT_STACK
{
2014-09-21 17:33:12 +00:00
int dont_block_signals = 0;
__splitstack_block_signals(&dont_block_signals, nil);
2012-03-27 23:13:14 +00:00
}
#endif
2018-12-28 15:30:48 +00:00
mstart1(0);
2014-09-21 17:33:12 +00:00
2018-12-28 15:30:48 +00:00
// mstart1 does not return, but we need a return statement
// here to avoid a compiler warning.
2012-03-27 23:13:14 +00:00
return nil;
}
typedef struct CgoThreadStart CgoThreadStart;
struct CgoThreadStart
{
M *m;
G *g;
2015-08-28 15:33:40 +00:00
uintptr *tls;
2012-03-27 23:13:14 +00:00
void (*fn)(void);
};
void setGContext(void) __asm__ (GOSYM_PREFIX "runtime.setGContext");
2015-08-28 15:33:40 +00:00
// setGContext sets up a new goroutine context for the current g.
2014-09-21 17:33:12 +00:00
void
2018-12-28 15:30:48 +00:00
setGContext(void)
2012-03-27 23:13:14 +00:00
{
int val;
G *gp;
2012-03-27 23:13:14 +00:00
2014-09-21 17:33:12 +00:00
initcontext();
gp = g;
gp->entry = nil;
gp->param = nil;
2014-09-21 17:33:12 +00:00
#ifdef USING_SPLIT_STACK
2018-12-28 15:30:48 +00:00
__splitstack_getcontext((void*)(&gp->stackcontext[0]));
val = 0;
__splitstack_block_signals(&val, nil);
2014-09-21 17:33:12 +00:00
#else
gp->gcinitialsp = &val;
2018-12-28 15:30:48 +00:00
gp->gcstack = 0;
gp->gcstacksize = 0;
2018-12-28 15:30:48 +00:00
gp->gcnextsp = (uintptr)(&val);
gp->gcinitialsp2 = secondary_stack_pointer();
gp->gcnextsp2 = (uintptr)(gp->gcinitialsp2);
2014-09-21 17:33:12 +00:00
#endif
getcontext(ucontext_arg(&gp->context[0]));
2012-03-27 23:13:14 +00:00
if(gp->entry != nil) {
2014-09-21 17:33:12 +00:00
// Got here from mcall.
2018-12-28 15:30:48 +00:00
FuncVal *fv = gp->entry;
void (*pfn)(G*) = (void (*)(G*))fv->fn;
G* gp1 = (G*)gp->param;
gp->entry = nil;
gp->param = nil;
2018-12-28 15:30:48 +00:00
__builtin_call_with_static_chain(pfn(gp1), fv);
2014-09-21 17:33:12 +00:00
*(int*)0x22 = 0x22;
}
}
2012-03-27 23:13:14 +00:00
void makeGContext(G*, byte*, uintptr)
__asm__(GOSYM_PREFIX "runtime.makeGContext");
2014-09-21 17:33:12 +00:00
// makeGContext makes a new context for a g.
2014-09-21 17:33:12 +00:00
void
makeGContext(G* gp, byte* sp, uintptr spsize) {
ucontext_t *uc;
2014-09-21 17:33:12 +00:00
uc = ucontext_arg(&gp->context[0]);
getcontext(uc);
uc->uc_stack.ss_sp = sp;
uc->uc_stack.ss_size = (size_t)spsize;
makecontext(uc, kickoff, 0);
2014-09-21 17:33:12 +00:00
}
2012-03-27 23:13:14 +00:00
// The goroutine g is about to enter a system call.
// Record that it's not using the cpu anymore.
// This is called only from the go syscall library and cgocall,
// not from the low-level system calls used by the runtime.
//
// Entersyscall cannot split the stack: the runtime_gosave must
// make g->sched refer to the caller's stack segment, because
// entersyscall is going to return immediately after.
void runtime_entersyscall(int32) __attribute__ ((no_split_stack));
static void doentersyscall(uintptr, uintptr)
__attribute__ ((no_split_stack, noinline));
2014-09-21 17:33:12 +00:00
void
runtime_entersyscall(int32 dummy __attribute__ ((unused)))
2014-09-21 17:33:12 +00:00
{
// Save the registers in the g structure so that any pointers
// held in registers will be seen by the garbage collector.
getcontext(ucontext_arg(&g->gcregs[0]));
2014-09-21 17:33:12 +00:00
2018-12-28 15:30:48 +00:00
// Note that if this function does save any registers itself,
// we might store the wrong value in the call to getcontext.
2014-09-21 17:33:12 +00:00
// FIXME: This assumes that we do not need to save any
// callee-saved registers to access the TLS variable g. We
// don't want to put the ucontext_t on the stack because it is
// large and we can not split the stack here.
doentersyscall((uintptr)runtime_getcallerpc(&dummy),
(uintptr)runtime_getcallersp(&dummy));
2014-09-21 17:33:12 +00:00
}
static void
doentersyscall(uintptr pc, uintptr sp)
2014-09-21 17:33:12 +00:00
{
// Leave SP around for GC and traceback.
#ifdef USING_SPLIT_STACK
{
size_t gcstacksize;
2018-12-28 15:30:48 +00:00
g->gcstack = (uintptr)(__splitstack_find(nil, nil, &gcstacksize,
(void**)(&g->gcnextsegment),
(void**)(&g->gcnextsp),
&g->gcinitialsp));
g->gcstacksize = (uintptr)gcstacksize;
}
2014-09-21 17:33:12 +00:00
#else
{
2015-08-28 15:33:40 +00:00
void *v;
2014-09-21 17:33:12 +00:00
2018-12-28 15:30:48 +00:00
g->gcnextsp = (uintptr)(&v);
g->gcnextsp2 = (uintptr)(secondary_stack_pointer());
2014-09-21 17:33:12 +00:00
}
#endif
2018-12-28 15:30:48 +00:00
reentersyscall(pc, sp);
2014-09-21 17:33:12 +00:00
}
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
static void doentersyscallblock(uintptr, uintptr)
__attribute__ ((no_split_stack, noinline));
2014-09-21 17:33:12 +00:00
// The same as runtime_entersyscall(), but with a hint that the syscall is blocking.
2012-03-27 23:13:14 +00:00
void
runtime_entersyscallblock(int32 dummy __attribute__ ((unused)))
2012-03-27 23:13:14 +00:00
{
2018-12-28 15:30:48 +00:00
// Save the registers in the g structure so that any pointers
// held in registers will be seen by the garbage collector.
getcontext(ucontext_arg(&g->gcregs[0]));
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
// See comment in runtime_entersyscall.
doentersyscallblock((uintptr)runtime_getcallerpc(&dummy),
(uintptr)runtime_getcallersp(&dummy));
}
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
static void
doentersyscallblock(uintptr pc, uintptr sp)
{
2014-09-21 17:33:12 +00:00
// Leave SP around for GC and traceback.
2012-03-27 23:13:14 +00:00
#ifdef USING_SPLIT_STACK
{
size_t gcstacksize;
2018-12-28 15:30:48 +00:00
g->gcstack = (uintptr)(__splitstack_find(nil, nil, &gcstacksize,
(void**)(&g->gcnextsegment),
(void**)(&g->gcnextsp),
&g->gcinitialsp));
g->gcstacksize = (uintptr)gcstacksize;
}
2012-03-27 23:13:14 +00:00
#else
2018-12-28 15:30:48 +00:00
{
void *v;
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
g->gcnextsp = (uintptr)(&v);
g->gcnextsp2 = (uintptr)(secondary_stack_pointer());
2012-03-27 23:13:14 +00:00
}
#endif
2018-12-28 15:30:48 +00:00
reentersyscallblock(pc, sp);
2012-03-27 23:13:14 +00:00
}
// Allocate a new g, with a stack big enough for stacksize bytes.
G*
runtime_malg(bool allocatestack, bool signalstack, byte** ret_stack, uintptr* ret_stacksize)
2012-03-27 23:13:14 +00:00
{
uintptr stacksize;
2012-03-27 23:13:14 +00:00
G *newg;
byte* unused_stack;
uintptr unused_stacksize;
#if USING_SPLIT_STACK
int dont_block_signals = 0;
size_t ss_stacksize;
#endif
2012-03-27 23:13:14 +00:00
if (ret_stack == nil) {
ret_stack = &unused_stack;
}
if (ret_stacksize == nil) {
ret_stacksize = &unused_stacksize;
}
2015-08-28 15:33:40 +00:00
newg = allocg();
if(allocatestack) {
stacksize = StackMin;
if(signalstack) {
stacksize = 32 * 1024; // OS X wants >= 8K, GNU/Linux >= 2K
#ifdef SIGSTKSZ
if(stacksize < SIGSTKSZ)
stacksize = SIGSTKSZ;
#endif
}
2012-03-27 23:13:14 +00:00
#if USING_SPLIT_STACK
2012-03-27 23:13:14 +00:00
*ret_stack = __splitstack_makecontext(stacksize,
2018-12-28 15:30:48 +00:00
(void*)(&newg->stackcontext[0]),
&ss_stacksize);
*ret_stacksize = (uintptr)ss_stacksize;
2018-12-28 15:30:48 +00:00
__splitstack_block_signals_context((void*)(&newg->stackcontext[0]),
2012-03-27 23:13:14 +00:00
&dont_block_signals, nil);
#else
2017-04-10 11:32:00 +00:00
// In 64-bit mode, the maximum Go allocation space is
// 128G. Our stack size is 4M, which only permits 32K
// goroutines. In order to not limit ourselves,
// allocate the stacks out of separate memory. In
// 32-bit mode, the Go allocation space is all of
// memory anyhow.
if(sizeof(void*) == 8) {
2018-12-28 15:30:48 +00:00
void *p = runtime_sysAlloc(stacksize, &getMemstats()->stacks_sys);
2017-04-10 11:32:00 +00:00
if(p == nil)
runtime_throw("runtime: cannot allocate memory for goroutine stack");
*ret_stack = (byte*)p;
} else {
2018-12-28 15:30:48 +00:00
*ret_stack = runtime_mallocgc(stacksize, nil, false);
2017-04-10 11:32:00 +00:00
runtime_xadd(&runtime_stacks_sys, stacksize);
}
*ret_stacksize = (uintptr)stacksize;
newg->gcinitialsp = *ret_stack;
newg->gcstacksize = (uintptr)stacksize;
2018-12-28 15:30:48 +00:00
newg->gcinitialsp2 = initial_secondary_stack_pointer(*ret_stack);
2012-03-27 23:13:14 +00:00
#endif
}
return newg;
}
2018-12-28 15:30:48 +00:00
void stackfree(G*)
__asm__(GOSYM_PREFIX "runtime.stackfree");
2012-03-27 23:13:14 +00:00
2018-12-28 15:30:48 +00:00
// stackfree frees the stack of a g.
2012-03-27 23:13:14 +00:00
void
2018-12-28 15:30:48 +00:00
stackfree(G* gp)
2012-03-27 23:13:14 +00:00
{
2018-12-28 15:30:48 +00:00
#if USING_SPLIT_STACK
__splitstack_releasecontext((void*)(&gp->stackcontext[0]));
#else
// If gcstacksize is 0, the stack is allocated by libc and will be
// released when the thread exits. Otherwise, in 64-bit mode it was
// allocated using sysAlloc and in 32-bit mode it was allocated
// using garbage collected memory.
if (gp->gcstacksize != 0) {
if (sizeof(void*) == 8) {
runtime_sysFree(gp->gcinitialsp, gp->gcstacksize, &getMemstats()->stacks_sys);
}
gp->gcinitialsp = nil;
gp->gcstacksize = 0;
}
#endif
2012-03-27 23:13:14 +00:00
}
2018-12-28 15:30:48 +00:00
void resetNewG(G*, void **, uintptr*)
__asm__(GOSYM_PREFIX "runtime.resetNewG");
2015-08-28 15:33:40 +00:00
2018-12-28 15:30:48 +00:00
// Reset stack information for g pulled out of the cache to start a
// new goroutine.
2012-03-27 23:13:14 +00:00
void
2018-12-28 15:30:48 +00:00
resetNewG(G *newg, void **sp, uintptr *spsize)
2012-03-27 23:13:14 +00:00
{
2018-12-28 15:30:48 +00:00
#ifdef USING_SPLIT_STACK
int dont_block_signals = 0;
size_t ss_spsize;
2014-09-21 17:33:12 +00:00
2018-12-28 15:30:48 +00:00
*sp = __splitstack_resetcontext((void*)(&newg->stackcontext[0]), &ss_spsize);
*spsize = ss_spsize;
__splitstack_block_signals_context((void*)(&newg->stackcontext[0]),
&dont_block_signals, nil);
#else
*sp = newg->gcinitialsp;
*spsize = newg->gcstacksize;
if(*spsize == 0)
runtime_throw("bad spsize in resetNewG");
newg->gcnextsp = (uintptr)(*sp);
newg->gcnextsp2 = (uintptr)(newg->gcinitialsp2);
#endif
2014-09-21 17:33:12 +00:00
}
// Return whether we are waiting for a GC. This gc toolchain uses
// preemption instead.
bool
runtime_gcwaiting(void)
{
return runtime_sched->gcwaiting;
2012-03-27 23:13:14 +00:00
}