Kaynağa Gözat

[LibOS] Use set_cur_thread() to associate shim_tcb_t and shim_thread

Uniformly use set_cur_thread() to associate shim_tcb_t to struct
shim_thread. Remove unnecessary logic from init_fs_base() and
update_fs_base().
Isaku Yamahata 4 yıl önce
ebeveyn
işleme
1902c3862b

+ 2 - 1
LibOS/shim/include/shim_thread.h

@@ -205,7 +205,8 @@ void set_cur_thread (struct shim_thread * thread)
 
     if (tcb->tid != tid) {
         tcb->tid = tid;
-        debug_setprefix(tcb);
+        if (tcb->debug_buf)
+            debug_setprefix(tcb);
     }
 }
 

+ 10 - 3
LibOS/shim/src/bookkeep/shim_thread.c

@@ -737,7 +737,13 @@ BEGIN_CP_FUNC(running_thread)
     if (thread->shim_tcb) {
         ptr_t toff = ADD_CP_OFFSET(sizeof(shim_tcb_t));
         new_thread->shim_tcb = (void *)(base + toff);
-        memcpy(new_thread->shim_tcb, thread->shim_tcb, sizeof(shim_tcb_t));
+        struct shim_tcb* new_tcb = new_thread->shim_tcb;
+        memcpy(new_tcb, thread->shim_tcb, sizeof(*new_tcb));
+        /* don't export stale pointers */
+        new_tcb->self = NULL;
+        new_tcb->tp = NULL;
+        new_tcb->context.next = NULL;
+        new_tcb->debug_buf = NULL;
     }
 }
 END_CP_FUNC(running_thread)
@@ -752,6 +758,7 @@ static int resume_wrapper (void * param)
     shim_tcb_init();
     shim_tcb_t* saved_tcb = thread->shim_tcb;
     assert(saved_tcb->context.regs && saved_tcb->context.regs->rsp);
+    set_cur_thread(thread);
     unsigned long fs_base = saved_tcb->context.fs_base;
     assert(fs_base);
     init_fs_base(fs_base, thread);
@@ -805,6 +812,7 @@ BEGIN_RS_FUNC(running_thread)
             shim_tcb_t* tcb = shim_get_tcb();
             memcpy(tcb, saved_tcb, sizeof(*tcb));
             __shim_tcb_init(tcb);
+            set_cur_thread(thread);
 
             assert(tcb->context.regs && tcb->context.regs->rsp);
             init_fs_base(tcb->context.fs_base, thread);
@@ -822,9 +830,8 @@ BEGIN_RS_FUNC(running_thread)
              * shim_tcb = NULL
              * in_vm = false
              */
-            thread->shim_tcb = shim_get_tcb();
-            debug_setbuf(thread->shim_tcb, false);
             set_cur_thread(thread);
+            debug_setbuf(thread->shim_tcb, false);
         }
 
         thread->in_vm = thread->is_alive = true;

+ 1 - 0
LibOS/shim/src/ipc/shim_ipc_helper.c

@@ -793,6 +793,7 @@ static void shim_ipc_helper_prepare(void* arg) {
         return;
 
     shim_tcb_init();
+    set_cur_thread(self);
     init_fs_base(0, self);
     debug_setbuf(shim_get_tcb(), true);
 

+ 1 - 0
LibOS/shim/src/shim_async.c

@@ -145,6 +145,7 @@ static void shim_async_helper(void * arg) {
         return;
 
     shim_tcb_init();
+    set_cur_thread(self);
     init_fs_base(0, self);
     debug_setbuf(shim_get_tcb(), true);
 

+ 1 - 16
LibOS/shim/src/shim_init.c

@@ -202,17 +202,8 @@ bool lock_enabled;
 /* This function is used to allocate tls before interpreter start running */
 void init_fs_base (unsigned long fs_base, struct shim_thread * thread)
 {
+    __UNUSED(thread);
     shim_tcb_t * shim_tcb = shim_get_tcb();
-
-    if (thread) {
-        thread->shim_tcb = shim_tcb;
-        shim_tcb->tp = thread;
-        shim_tcb->tid = thread->tid;
-    } else {
-        shim_tcb->tp = NULL;
-        shim_tcb->tid = 0;
-    }
-
     shim_tcb->context.fs_base = fs_base;
     DkSegmentRegister(PAL_SEGMENT_FS, (PAL_PTR)fs_base);
     assert(shim_tcb_check_canary());
@@ -221,12 +212,6 @@ void init_fs_base (unsigned long fs_base, struct shim_thread * thread)
 void update_fs_base (unsigned long fs_base)
 {
     shim_tcb_t * shim_tcb = shim_get_tcb();
-
-    struct shim_thread * thread = shim_tcb->tp;
-    if (thread) {
-        thread->shim_tcb = shim_tcb;
-    }
-
     shim_tcb->context.fs_base = fs_base;
     DkSegmentRegister(PAL_SEGMENT_FS, (PAL_PTR)fs_base);
     assert(shim_tcb_check_canary());

+ 2 - 1
LibOS/shim/src/sys/shim_clone.c

@@ -119,11 +119,12 @@ static int clone_implementation_wrapper(struct shim_clone_args * arg)
     assert(my_thread);
 
     shim_tcb_init();
+    set_cur_thread(my_thread);
     init_fs_base(arg->fs_base, my_thread);
     shim_tcb_t * tcb = my_thread->shim_tcb;
 
     /* only now we can call LibOS/PAL functions because they require a set-up TCB;
-     * do not move the below functions before init_fs_base()! */
+     * do not move the below functions before shim_tcb_init/set_cur_thread()! */
     object_wait_with_retry(arg->create_event);
     DkObjectClose(arg->create_event);