[LibOS] Rename shim_thread::tcb to fs_base

Now, shim_thread::tcb is used only as an integer value for %fs_base,
and shim_thread::user_tcb is not needed anymore. This commit renames
shim_thread::tcb to shim_thread::fs_base and removes user_tcb.
This commit is contained in:
Isaku Yamahata
2019-11-12 19:46:54 -08:00
committed by Dmitrii Kuvaiskii
parent 0f0c64c4af
commit fde7cc1d6a
11 changed files with 47 additions and 73 deletions
+5 -7
View File
@@ -82,8 +82,7 @@ struct shim_thread {
struct shim_handle * exec;
void * stack, * stack_top, * stack_red;
__libc_tcb_t * tcb;
bool user_tcb; /* is tcb assigned by user? */
unsigned long fs_base;
shim_tcb_t * shim_tcb;
void * frameptr;
@@ -145,8 +144,8 @@ void put_thread (struct shim_thread * thread);
void get_simple_thread (struct shim_simple_thread * thread);
void put_simple_thread (struct shim_simple_thread * thread);
void allocate_tls (__libc_tcb_t * tcb_location, bool user, struct shim_thread * thread);
void populate_tls (__libc_tcb_t * tcb_location, bool user);
void allocate_tls (unsigned long fs_base, struct shim_thread * thread);
void populate_tls (unsigned long fs_base);
void debug_setprefix (shim_tcb_t * tcb);
@@ -186,7 +185,7 @@ void set_cur_thread (struct shim_thread * thread)
IDTYPE tid = 0;
if (thread) {
__libc_tcb_t * libc_tcb = tcb->tp ? tcb->tp->tcb : NULL;
unsigned long fs_base = tcb->tp ? tcb->tp->fs_base : 0;
if (tcb->tp && tcb->tp != thread)
put_thread(tcb->tp);
@@ -194,8 +193,7 @@ void set_cur_thread (struct shim_thread * thread)
get_thread(thread);
tcb->tp = thread;
if (libc_tcb)
thread->tcb = libc_tcb;
thread->fs_base = fs_base;
thread->shim_tcb = tcb;
tid = thread->tid;
-6
View File
@@ -73,12 +73,6 @@ struct shim_tcb {
#include <stddef.h>
void init_tcb (shim_tcb_t * tcb);
struct __libc_tcb_t;
typedef struct __libc_tcb_t __libc_tcb_t;
/* don't define struct __libc_tcb_t. just type to point to libc tls
* LibOS doesn't access this structure as it's private to libc.
*/
static inline shim_tcb_t * shim_get_tls(void)
{
+8 -11
View File
@@ -721,15 +721,15 @@ static int resume_wrapper (void * param)
struct shim_thread * thread = (struct shim_thread *) param;
assert(thread);
__libc_tcb_t * libc_tcb = thread->tcb;
assert(libc_tcb);
unsigned long fs_base = thread->fs_base;
assert(fs_base);
shim_tcb_t * tcb = thread->shim_tcb;
assert(tcb->context.regs && tcb->context.regs->rsp);
thread->in_vm = thread->is_alive = true;
allocate_tls(libc_tcb, thread->user_tcb, thread);
allocate_tls(fs_base, thread);
debug_setbuf(tcb, false);
debug("set tcb to %p\n", libc_tcb);
debug("set fs_base to 0x%lx\n", fs_base);
object_wait_with_retry(thread_start_event);
@@ -746,8 +746,6 @@ BEGIN_RS_FUNC(running_thread)
thread->vmid = cur_process.vmid;
if (!thread->user_tcb)
CP_REBASE(thread->tcb);
if (thread->shim_tcb)
CP_REBASE(thread->shim_tcb);
@@ -772,17 +770,17 @@ BEGIN_RS_FUNC(running_thread)
thread->shim_tcb = shim_get_tls();
}
debug_setbuf(thread->shim_tcb, false);
__libc_tcb_t * libc_tcb = thread->tcb;
unsigned long fs_base = thread->fs_base;
if (libc_tcb) {
if (fs_base) {
shim_tcb_t * tcb = thread->shim_tcb;
assert(tcb->context.regs && tcb->context.regs->rsp);
tcb->debug_buf = shim_get_tls()->debug_buf;
allocate_tls(libc_tcb, thread->user_tcb, thread);
allocate_tls(fs_base, thread);
/* Temporarily disable preemption until the thread resumes. */
__disable_preempt(tcb);
debug_setprefix(tcb);
debug("after resume, set tcb to %p\n", libc_tcb);
debug("after resume, set tcb to 0x%lx\n", fs_base);
} else {
/*
* In execve case, the following holds:
@@ -790,7 +788,6 @@ BEGIN_RS_FUNC(running_thread)
* stack_top = NULL
* frameptr = NULL
* tcb = NULL
* user_tcb = false
* shim_tcb = NULL
* in_vm = false
*/
+2 -3
View File
@@ -791,10 +791,9 @@ static void shim_ipc_helper_prepare(void* arg) {
if (!arg)
return;
__libc_tcb_t* tcb = NULL;
allocate_tls(tcb, false, self);
unsigned long fs_base = 0;
allocate_tls(fs_base, self);
debug_setbuf(shim_get_tls(), true);
debug("Set tcb to %p\n", tcb);
lock(&ipc_helper_lock);
bool notme = (self != ipc_helper_thread);
+2 -3
View File
@@ -144,10 +144,9 @@ static void shim_async_helper(void * arg) {
if (!arg)
return;
__libc_tcb_t* tcb = NULL;
allocate_tls(tcb, false, self);
unsigned long fs_base = 0;
allocate_tls(fs_base, self);
debug_setbuf(shim_get_tls(), true);
debug("Set tcb to %p\n", tcb);
lock(&async_helper_lock);
bool notme = (self != async_helper_thread);
+12 -17
View File
@@ -206,40 +206,36 @@ void init_tcb (shim_tcb_t * tcb)
}
/* This function is used to allocate tls before interpreter start running */
void allocate_tls (__libc_tcb_t * tcb, bool user, struct shim_thread * thread)
void allocate_tls (unsigned long fs_base, struct shim_thread * thread)
{
shim_tcb_t * shim_tcb;
shim_tcb = shim_get_tls();
shim_tcb_t * shim_tcb = shim_get_tls();
init_tcb(shim_tcb);
if (thread) {
thread->tcb = tcb;
thread->user_tcb = user;
thread->fs_base = fs_base;
thread->shim_tcb = shim_tcb;
shim_tcb->tp = thread;
shim_tcb->tp = thread;
shim_tcb->tid = thread->tid;
} else {
shim_tcb->tp = NULL;
shim_tcb->tp = NULL;
shim_tcb->tid = 0;
}
DkSegmentRegister(PAL_SEGMENT_FS, tcb);
DkSegmentRegister(PAL_SEGMENT_FS, (PAL_PTR)fs_base);
assert(shim_tls_check_canary());
}
void populate_tls (__libc_tcb_t * tcb, bool user)
void populate_tls (unsigned long fs_base)
{
shim_tcb_t * shim_tcb;
shim_tcb = shim_get_tls();
shim_tcb_t * shim_tcb = shim_get_tls();
struct shim_thread * thread = shim_tcb->tp;
if (thread) {
thread->tcb = tcb;
thread->user_tcb = user;
thread->fs_base = fs_base;
thread->shim_tcb = shim_tcb;
}
DkSegmentRegister(PAL_SEGMENT_FS, tcb);
DkSegmentRegister(PAL_SEGMENT_FS, (PAL_PTR)fs_base);
assert(shim_tls_check_canary());
}
@@ -679,12 +675,11 @@ noreturn void* shim_init (int argc, void * args)
cur_process.vmid = (IDTYPE) PAL_CB(process_id);
/* create the initial TCB, shim can not be run without a tcb */
__libc_tcb_t* tcb = NULL;
allocate_tls(tcb, false, NULL);
unsigned long fs_base = 0;
allocate_tls(fs_base, NULL);
__disable_preempt(shim_get_tls()); // Temporarily disable preemption for delaying any signal
// that arrives during initialization
debug_setbuf(shim_get_tls(), true);
debug("set tcb to %p\n", tcb);
#ifdef PROFILE
unsigned long begin_time = GET_PROFILE_INTERVAL();
+2 -2
View File
@@ -591,8 +591,8 @@ void* shim_do_arch_prctl(int code, void* addr) {
if (!addr)
return (void*)-EINVAL;
populate_tls(addr, true);
debug("set tcb to %p\n", (void*)addr);
populate_tls((unsigned long)addr);
debug("set fs_base to 0x%lx\n", (unsigned long)addr);
return NULL;
case ARCH_GET_FS:
+8 -10
View File
@@ -125,7 +125,7 @@ static int clone_implementation_wrapper(struct clone_args * arg)
struct shim_thread* my_thread = arg->thread;
assert(my_thread);
allocate_tls(my_thread->tcb, my_thread->user_tcb, my_thread); /* set up TCB */
allocate_tls(my_thread->fs_base, my_thread); /* set up TCB */
shim_tcb_t * tcb = my_thread->shim_tcb;
/* only now we can call LibOS/PAL functions because they require a set-up TCB;
@@ -136,7 +136,7 @@ static int clone_implementation_wrapper(struct clone_args * arg)
__disable_preempt(tcb); // Temporarily disable preemption, because the preemption
// will be re-enabled when the thread starts.
debug_setbuf(tcb, true);
debug("set tcb to %p\n", my_thread->tcb);
debug("set fs_base to 0x%lx\n", my_thread->fs_base);
struct shim_regs regs = *arg->parent->shim_tcb->context.regs;
if (my_thread->set_child_tid) {
@@ -295,10 +295,9 @@ int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
ret = -EINVAL;
goto failed;
}
thread->tcb = tls;
thread->user_tcb = true;
thread->fs_base = (unsigned long)tls;
} else {
thread->tcb = NULL;
thread->fs_base = 0;
}
if (!(flags & CLONE_THREAD))
@@ -320,17 +319,16 @@ int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
}
if (!(flags & CLONE_VM)) {
__libc_tcb_t * tcb;
unsigned long fs_base;
shim_tcb_t * old_shim_tcb = NULL;
void * parent_stack = NULL;
if (thread->tcb) {
tcb = thread->tcb;
if (thread->fs_base) {
fs_base = thread->fs_base;
} else {
thread->tcb = tcb = self->tcb;
thread->fs_base = fs_base = self->fs_base;
old_shim_tcb = __alloca(sizeof(shim_tcb_t));
memcpy(old_shim_tcb, self->shim_tcb, sizeof(shim_tcb_t));
thread->user_tcb = self->user_tcb;
thread->shim_tcb = self->shim_tcb;
}
+6 -10
View File
@@ -87,10 +87,9 @@ noreturn static void __shim_do_execve_rtld (struct execve_rtld_arg * __arg)
struct shim_thread * cur_thread = get_cur_thread();
int ret = 0;
/* libc tcb is not needed because PAL provides storage for shim_tcb */
__libc_tcb_t* tcb = NULL;
populate_tls(tcb, false);
debug("set tcb to %p\n", tcb);
unsigned long fs_base = 0;
populate_tls(fs_base);
debug("set fs_base to 0x%lx\n", fs_base);
UPDATE_PROFILE_INTERVAL();
@@ -490,16 +489,14 @@ err:
void * stack = cur_thread->stack;
void * stack_top = cur_thread->stack_top;
__libc_tcb_t * tcb = cur_thread->tcb;
unsigned long fs_base = cur_thread->fs_base;
shim_tcb_t * shim_tcb = cur_thread->shim_tcb;
bool user_tcb = cur_thread->user_tcb;
void * frameptr = cur_thread->frameptr;
cur_thread->stack = NULL;
cur_thread->stack_top = NULL;
cur_thread->frameptr = NULL;
cur_thread->tcb = NULL;
cur_thread->user_tcb = false;
cur_thread->fs_base = 0;
cur_thread->shim_tcb = NULL;
cur_thread->in_vm = false;
unlock(&cur_thread->lock);
@@ -510,8 +507,7 @@ err:
cur_thread->stack = stack;
cur_thread->stack_top = stack_top;
cur_thread->frameptr = frameptr;
cur_thread->tcb = tcb;
cur_thread->user_tcb = user_tcb;
cur_thread->fs_base = fs_base;
cur_thread->shim_tcb = shim_tcb;
if (ret < 0) {
+1 -2
View File
@@ -84,8 +84,7 @@ int shim_do_fork (void)
if (!new_thread)
return -ENOMEM;
new_thread->tcb = cur_thread->tcb;
new_thread->user_tcb = cur_thread->user_tcb;
new_thread->fs_base = cur_thread->fs_base;
new_thread->shim_tcb = cur_thread->shim_tcb;
new_thread->tgid = new_thread->tid;
new_thread->in_vm = false;
+1 -2
View File
@@ -76,8 +76,7 @@ int shim_do_vfork(void) {
new_thread->is_alive = true;
new_thread->stack = cur_thread->stack;
new_thread->stack_top = cur_thread->stack_top;
new_thread->tcb = cur_thread->tcb;
new_thread->user_tcb = cur_thread->user_tcb;
new_thread->fs_base = cur_thread->fs_base;
cur_thread->stack = dummy_stack;
cur_thread->stack_top = dummy_stack + stack_size;
cur_thread->frameptr = NULL;