[PATCH 2/2] Adds some resolv functionality (XCC)
[akaros.git] / user / parlib / uthread.c
index fd9d8a3..a77ae77 100644 (file)
@@ -62,14 +62,25 @@ static void uthread_manage_thread0(struct uthread *uthread)
         * (right before vcore_entry(), don't try and take the address of any of
         * its TLS vars. */
        set_tls_desc(get_vcpd_tls_desc(0), 0);
+       begin_safe_access_tls_vars();
        /* We might have a basic uthread already installed (from slim_init), so
         * free it before installing the new one. */
        if (current_uthread)
                free(current_uthread);
        current_uthread = uthread;
+       /* We could consider setting __vcore_context = TRUE here, since this is
+        * probably the first time we're initializing vcore 0's TLS.  However, when
+        * we actually turn into an MCP, VC 0 will come up and set __vcore_context.
+        * I actually want it cleared until then, so that various asserts will catch
+        * if we call other uthread functions before the 2LS is set up (before we're
+        * an MCP).  For example, if someone calls uthread_yield from thread0 (which
+        * has TLS), we'll panic since VC 0's TLS doesn't know it's a VC yet. */
+       end_safe_access_tls_vars();
        set_tls_desc(uthread->tls_desc, 0);
+       begin_safe_access_tls_vars();
        __vcoreid = 0;  /* setting the uthread's TLS var */
        assert(!in_vcore_context());
+       end_safe_access_tls_vars();
 }
 
 /* The real 2LS calls this, passing in a uthread representing thread0.  When it
@@ -235,7 +246,14 @@ void uthread_init(struct uthread *new_thread, struct uth_thread_attr *attr)
                else
                        ret = __uthread_allocate_tls(new_thread);
                assert(!ret);
-               uthread_set_tls_var(new_thread, current_uthread, new_thread);
+               begin_access_tls_vars(new_thread->tls_desc);
+               current_uthread = new_thread;
+               /* ctypes stores locale info in TLS.  we need this only once per TLS, so
+                * we don't have to do it here, but it is convenient since we already
+                * loaded the uthread's TLS. */
+               extern void __ctype_init(void);
+               __ctype_init();
+               end_access_tls_vars();
        } else {
                new_thread->tls_desc = UTH_TLSDESC_NOTLS;
        }
@@ -351,8 +369,11 @@ void uthread_yield(bool save_state, void (*yield_func)(struct uthread*, void*),
        /* Change to the transition context (both TLS (if applicable) and stack). */
        if (__uthread_has_tls(uthread)) {
                set_tls_desc(get_vcpd_tls_desc(vcoreid), vcoreid);
+               begin_safe_access_tls_vars();
                assert(current_uthread == uthread);
+               /* If this assert fails, see the note in uthread_manage_thread0 */
                assert(in_vcore_context());
+               end_safe_access_tls_vars();
        } else {
                /* Since uthreads and vcores share TLS (it's always the vcore's TLS, the
                 * uthread one just bootstraps from it), we need to change our state at
@@ -468,7 +489,9 @@ void highjack_current_uthread(struct uthread *uthread)
        if (__uthread_has_tls(uthread)) {
                assert(uthread->tls_desc);
                set_tls_desc(uthread->tls_desc, vcoreid);
+               begin_safe_access_tls_vars();
                __vcoreid = vcoreid;    /* setting the uthread's TLS var */
+               end_safe_access_tls_vars();
        }
 }
 
@@ -479,7 +502,9 @@ static void set_uthread_tls(struct uthread *uthread, uint32_t vcoreid)
 {
        if (__uthread_has_tls(uthread)) {
                set_tls_desc(uthread->tls_desc, vcoreid);
+               begin_safe_access_tls_vars();
                __vcoreid = vcoreid;    /* setting the uthread's TLS var */
+               end_safe_access_tls_vars();
        } else {
                __vcore_context = FALSE;
        }
@@ -938,7 +963,7 @@ static void handle_vc_preempt(struct event_msg *ev_msg, unsigned int ev_type,
        /* At this point, we're clear to try and steal the uthread.  We used to
         * switch to their TLS to steal the uthread, but we can access their
         * current_uthread directly. */
-       rem_cur_uth = get_cur_uth_addr(rem_vcoreid);
+       rem_cur_uth = get_tlsvar_linaddr(rem_vcoreid, current_uthread);
        uthread_to_steal = *rem_cur_uth;
        if (uthread_to_steal) {
                /* Extremely rare: they have a uthread, but it can't migrate.  So we'll