Skip to content

Commit

Permalink
solo5: unix version by removing TLS and implementing the switches by …
Browse files Browse the repository at this point in the history
…hand
  • Loading branch information
Ricardo Koller authored and ricarkol committed Feb 15, 2018
1 parent ab3f013 commit a7c57e6
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 49 deletions.
4 changes: 2 additions & 2 deletions build-rr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,12 @@ buildrump ()

extracflags=
[ "${MACHINE_GNU_ARCH}" = "x86_64" ] \
&& extracflags='-F CFLAGS=-mno-red-zone'
&& extracflags='-F CFLAGS=-mno-red-zone -F CFLAGS=-D_PTHREAD_GETTCB_EXT=_lwp_get_tls_tcb'

# build tools
${BUILDRUMP}/buildrump.sh ${BUILD_QUIET} ${STDJ} -k \
-s ${RUMPSRC} -T ${RUMPTOOLS} -o ${BROBJ} -d ${STAGING} \
-V MKPIC=no -V RUMP_CURLWP=__thread \
-V MKPIC=no -V RUMP_CURLWP=hypercall \
-V RUMP_KERNEL_IS_LIBC=1 -V BUILDRUMP_SYSROOT=yes \
${extracflags} "$@" tools

Expand Down
9 changes: 8 additions & 1 deletion include/bmk-core/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ struct bmk_tcb {
};

struct bmk_thread;
struct lwp *bmk_get_current_lwp(void);
void bmk_set_current_lwp(struct lwp *lwp);

struct rumprun_lwp;
struct rumprun_lwp *bmk_get_current_lwp_me(void);
void bmk_set_thread_lwp_me(struct bmk_thread *thread, struct rumprun_lwp *lwp);

void bmk_sched_init(void);
void bmk_sched_startmain(void (*)(void *), void *) __attribute__((noreturn));
Expand Down Expand Up @@ -80,7 +86,8 @@ void bmk_cpu_sched_create(struct bmk_thread *, struct bmk_tcb *,
void bmk_sched_set_hook(void (*)(void *, void *));
struct bmk_thread *bmk_sched_init_mainlwp(void *);

extern __thread struct bmk_thread *bmk_current;
//extern __thread struct bmk_thread *bmk_current;
extern struct bmk_thread *bmk_current;

int *bmk_sched_geterrno(void);
const char *bmk_sched_threadname(struct bmk_thread *);
Expand Down
2 changes: 2 additions & 0 deletions include/bmk-core/solo5.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ uint64_t solo5_clock_wall(void);
*/
int solo5_poll(uint64_t until_nsecs);

int solo5_setfs(uint64_t new_fs);

#endif
79 changes: 56 additions & 23 deletions lib/libbmk_core/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include <bmk-core/string.h>
#include <bmk-core/sched.h>

#include <stddef.h>

void *bmk_mainstackbase;
unsigned long bmk_mainstacksize;

Expand All @@ -65,11 +67,16 @@ unsigned long bmk_mainstacksize;
#define THR_DEAD 0x0200
#define THR_BLOCKPREP 0x0400


#define _TLS_I // no offset

/*
#if !(defined(__i386__) || defined(__x86_64__))
#define _TLS_I
#else
#define _TLS_II
#endif
*/

extern const char _tdata_start[], _tdata_end[];
extern const char _tbss_start[], _tbss_end[];
Expand All @@ -84,6 +91,7 @@ extern const char _tbss_start[], _tbss_end[];
#endif
#define TLSAREASIZE (TMEMSIZE + BMK_TLS_EXTRA)


struct bmk_thread {
char bt_name[NAME_MAXLEN];

Expand All @@ -96,13 +104,47 @@ struct bmk_thread {

void *bt_cookie;

/* for rumpuser_synch.c: __thread lwp */
struct lwp *bt_lwp;

/* for _lwp: __thread me */
struct rumprun_lwp *bt_lwp_me;

/* MD thread control block */
struct bmk_tcb bt_tcb;

TAILQ_ENTRY(bmk_thread) bt_schedq;
TAILQ_ENTRY(bmk_thread) bt_threadq;
};
__thread struct bmk_thread *bmk_current;
//__thread struct bmk_thread *bmk_current;
struct bmk_thread *bmk_current;

struct lwp *bmk_get_current_lwp(void)
{
return bmk_current->bt_lwp;
}

void bmk_set_current_lwp(struct lwp *lwp)
{
bmk_current->bt_lwp = lwp;
}

struct rumprun_lwp *bmk_get_current_lwp_me(void)
{
return bmk_current->bt_lwp_me;
}


void bmk_set_thread_lwp_me(struct bmk_thread *thread, struct rumprun_lwp *lwp)
{
thread->bt_lwp_me = lwp;
}

struct bmk_thread *get_bmk_current(void) __attribute__ ((noinline));
struct bmk_thread *get_bmk_current(void)
{
return bmk_current;
}

TAILQ_HEAD(threadqueue, bmk_thread);
static struct threadqueue threadq = TAILQ_HEAD_INITIALIZER(threadq);
Expand Down Expand Up @@ -296,6 +338,11 @@ sched_switch(struct bmk_thread *prev, struct bmk_thread *next)
if (scheduler_hook)
scheduler_hook(prev->bt_cookie, next->bt_cookie);
bmk_platform_cpu_sched_settls(&next->bt_tcb);

__asm__ __volatile__("" ::: "memory");
bmk_current = next; // XXX
__asm__ __volatile__("" ::: "memory");

bmk_cpu_sched_switch(&prev->bt_tcb, &next->bt_tcb);
}

Expand Down Expand Up @@ -429,6 +476,7 @@ inittcb(struct bmk_tcb *tcb, void *tlsarea, unsigned long tlssize)
tcb->btcb_tpsize = tlssize;
}

/*
static long bmk_curoff;
static void
initcurrent(void *tcb, struct bmk_thread *value)
Expand All @@ -437,6 +485,7 @@ initcurrent(void *tcb, struct bmk_thread *value)
*dst = value;
}
*/

struct bmk_thread *
bmk_sched_create_withtls(const char *name, void *cookie, int joinable,
Expand Down Expand Up @@ -466,8 +515,10 @@ bmk_sched_create_withtls(const char *name, void *cookie, int joinable,
thread->bt_cookie = cookie;
thread->bt_wakeup_time = BMK_SCHED_BLOCK_INFTIME;

//tlsarea = (void *)thread->bt_tcb.btcb_tp;
bmk_memcpy(tlsarea, (void *)&thread, sizeof(void *));
inittcb(&thread->bt_tcb, tlsarea, TCBOFFSET);
initcurrent(tlsarea, thread);
//initcurrent(tlsarea, thread);

TAILQ_INSERT_TAIL(&threadq, thread, bt_threadq);

Expand All @@ -485,11 +536,13 @@ bmk_sched_create(const char *name, void *cookie, int joinable,
void (*f)(void *), void *data,
void *stack_base, unsigned long stack_size)
{
struct bmk_thread *thread;
void *tlsarea;

tlsarea = bmk_sched_tls_alloc();
return bmk_sched_create_withtls(name, cookie, joinable, f, data,
thread = bmk_sched_create_withtls(name, cookie, joinable, f, data,
stack_base, stack_size, tlsarea);
return thread;
}

struct join_waiter {
Expand Down Expand Up @@ -656,26 +709,6 @@ bmk_sched_wake(struct bmk_thread *thread)
void
bmk_sched_init(void)
{
unsigned long tlsinit;
struct bmk_tcb tcbinit;

inittcb(&tcbinit, &tlsinit, 0);
bmk_platform_cpu_sched_settls(&tcbinit);

/*
* Not sure if the membars are necessary, but better to be
* Marvin the Paranoid Paradroid than get eaten by 999
*/
__asm__ __volatile__("" ::: "memory");
bmk_curoff = (unsigned long)&bmk_current - (unsigned long)&tlsinit;
__asm__ __volatile__("" ::: "memory");

/*
* Set TLS back to 0 so that it's easier to catch someone trying
* to use it until we get TLS really initialized.
*/
tcbinit.btcb_tp = 0;
bmk_platform_cpu_sched_settls(&tcbinit);
}

void __attribute__((noreturn))
Expand Down
12 changes: 6 additions & 6 deletions lib/libbmk_rumpuser/rumpuser_synch.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ rumpuser_cv_has_waiters(struct rumpuser_cv *cv, int *rvp)
* curlwp
*/

static __thread struct lwp *current_lwp;
//static __thread struct lwp *current_lwp;
void
rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l)
{
Expand All @@ -465,12 +465,12 @@ rumpuser_curlwpop(int enum_rumplwpop, struct lwp *l)
case RUMPUSER_LWP_DESTROY:
break;
case RUMPUSER_LWP_SET:
bmk_assert(current_lwp == NULL);
current_lwp = l;
bmk_assert(bmk_get_current_lwp() == NULL);
bmk_set_current_lwp(l);
break;
case RUMPUSER_LWP_CLEAR:
bmk_assert(current_lwp == l);
current_lwp = NULL;
bmk_assert(bmk_get_current_lwp() == l);
bmk_set_current_lwp(NULL);
break;
}
}
Expand All @@ -479,5 +479,5 @@ struct lwp *
rumpuser_curlwp(void)
{

return current_lwp;
return bmk_get_current_lwp();
}
53 changes: 37 additions & 16 deletions lib/librumprun_base/_lwp.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ struct rumprun_lwp {
TAILQ_ENTRY(rumprun_lwp) rl_entries;
};
static TAILQ_HEAD(, rumprun_lwp) all_lwp = TAILQ_HEAD_INITIALIZER(all_lwp);
static __thread struct rumprun_lwp *me;
//static __thread struct rumprun_lwp *me;
static struct rumprun_lwp *me(void);
static struct rumprun_lwp *me(void)
{
return bmk_get_current_lwp_me();
}

#define FIRST_LWPID 1
static int curlwpid = FIRST_LWPID;
Expand All @@ -75,20 +80,23 @@ static struct rumprun_lwp mainthread = {

static void rumprun_makelwp_tramp(void *);

/*
static ptrdiff_t meoff;
static void
assignme(void *tcb, struct rumprun_lwp *value)
{
struct rumprun_lwp **dst = (void *)((uintptr_t)tcb + meoff);
//struct rumprun_lwp **dst = (void *)((uintptr_t)tcb + meoff);
*dst = value;
// *dst = value;
//bmk_set_thread_lwp_me(bmk_current, value);
}
*/

int
_lwp_ctl(int ctl, struct lwpctl **data)
{

*data = (struct lwpctl *)&me->rl_lwpctl;
*data = (struct lwpctl *)&(me()->rl_lwpctl);
return 0;
}

Expand All @@ -102,7 +110,7 @@ rumprun_makelwp(void (*start)(void *), void *arg, void *private,
rl = calloc(1, sizeof(*rl));
if (rl == NULL)
return errno;
assignme(private, rl);
//assignme(private, rl);

curlwp = rump_pub_lwproc_curlwp();
if ((errno = rump_pub_lwproc_newlwp(getpid())) != 0) {
Expand All @@ -121,6 +129,7 @@ rumprun_makelwp(void (*start)(void *), void *arg, void *private,
rump_pub_lwproc_switch(curlwp);
return EBUSY; /* ??? */
}
bmk_set_thread_lwp_me(rl->rl_thread, rl); // XXX
rump_pub_lwproc_switch(curlwp);

*lid = rl->rl_lwpid;
Expand All @@ -134,7 +143,7 @@ rumprun_makelwp_tramp(void *arg)
{

rump_pub_lwproc_switch(arg);
(me->rl_start)(me->rl_arg);
(me()->rl_start)(me()->rl_arg);
}

static struct rumprun_lwp *
Expand Down Expand Up @@ -206,15 +215,16 @@ schedhook(void *prevcookie, void *nextcookie)
void
rumprun_lwp_init(void)
{
void *tcb = bmk_sched_gettcb();
//void *tcb = bmk_sched_gettcb();

bmk_sched_set_hook(schedhook);

meoff = (uintptr_t)&me - (uintptr_t)tcb;
assignme(tcb, &mainthread);
//meoff = (uintptr_t)&me - (uintptr_t)tcb;
//assignme(tcb, &mainthread);
bmk_set_thread_lwp_me(bmk_current, &mainthread);
mainthread.rl_thread = bmk_sched_init_mainlwp(&mainthread);

TAILQ_INSERT_TAIL(&all_lwp, me, rl_entries);
TAILQ_INSERT_TAIL(&all_lwp, me(), rl_entries);
}

int
Expand All @@ -226,8 +236,8 @@ _lwp_park(clockid_t clock_id, int flags, const struct timespec *ts,
if (unpark)
_lwp_unpark(unpark, unparkhint);

if (me->rl_no_parking_hare) {
me->rl_no_parking_hare = 0;
if (me()->rl_no_parking_hare) {
me()->rl_no_parking_hare = 0;
return 0;
}

Expand Down Expand Up @@ -257,12 +267,13 @@ int
_lwp_exit(void)
{

me->rl_lwpctl.lc_curcpu = LWPCTL_CPU_EXITED;
me()->rl_lwpctl.lc_curcpu = LWPCTL_CPU_EXITED;
rump_pub_lwproc_releaselwp();
TAILQ_REMOVE(&all_lwp, me, rl_entries);
TAILQ_REMOVE(&all_lwp, me(), rl_entries);

/* could just assign it here, but for symmetry! */
assignme(bmk_sched_gettcb(), NULL);
//assignme(bmk_sched_gettcb(), NULL);
bmk_set_thread_lwp_me(bmk_current, NULL);

bmk_sched_exit_withtls();

Expand Down Expand Up @@ -323,7 +334,7 @@ lwpid_t
_lwp_self(void)
{

return me->rl_lwpid;
return me()->rl_lwpid;
}

/* XXX: messy. see sched.h, libc, libpthread, and all over */
Expand Down Expand Up @@ -358,6 +369,16 @@ _lwp_getprivate(void)
return bmk_sched_gettcb();
}


struct tls_tcb * _lwp_get_tls_tcb(void);

struct tls_tcb *
_lwp_get_tls_tcb(void)
{
return (struct tls_tcb *)bmk_sched_gettcb();
}


void _lwpnullop(void);
void _lwpnullop(void) { }

Expand Down
Loading

0 comments on commit a7c57e6

Please sign in to comment.