[PATCH 2/2] Adds some resolv functionality (XCC)
[akaros.git] / user / parlib / uthread.c
index 31284cb..a77ae77 100644 (file)
@@ -55,20 +55,32 @@ static void uthread_manage_thread0(struct uthread *uthread)
        uthread->flags &= ~(UTHREAD_SAVED | UTHREAD_FPSAVED);
        /* need to track thread0 for TLS deallocation */
        uthread->flags |= UTHREAD_IS_THREAD0;
+       uthread->notif_disabled_depth = 0;
        /* Change temporarily to vcore0s tls region so we can save the newly created
         * tcb into its current_uthread variable and then restore it.  One minor
         * issue is that vcore0's transition-TLS isn't TLS_INITed yet.  Until it is
         * (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
@@ -226,6 +238,7 @@ void uthread_init(struct uthread *new_thread, struct uth_thread_attr *attr)
         * There is no FP context to be restored yet.  We only save the FPU when we
         * were interrupted off a core. */
        new_thread->flags |= UTHREAD_SAVED;
+       new_thread->notif_disabled_depth = 0;
        if (attr && attr->want_tls) {
                /* Get a TLS.  If we already have one, reallocate/refresh it */
                if (new_thread->tls_desc)
@@ -233,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;
        }
@@ -279,6 +299,10 @@ __uthread_yield(void)
         * uthread_destroy() */
        uthread->flags &= ~UTHREAD_DONT_MIGRATE;
        uthread->state = UT_NOT_RUNNING;
+       /* Any locks that were held before the yield must be unlocked in the
+        * callback.  That callback won't get a chance to update our disabled depth.
+        * This sets us up for the next time the uthread runs. */
+       uthread->notif_disabled_depth = 0;
        /* Do whatever the yielder wanted us to do */
        assert(uthread->yield_func);
        uthread->yield_func(uthread, uthread->yield_arg);
@@ -345,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
@@ -378,6 +405,12 @@ void uthread_sleep(unsigned int seconds)
 {
        sys_block(seconds * 1000000);   /* usec sleep */
 }
+/* If we are providing a dummy sleep function, might as well provide the more
+ * accurate/useful one. */
+void uthread_usleep(unsigned int usecs)
+{
+       sys_block(usecs);       /* usec sleep */
+}
 
 /* Cleans up the uthread (the stuff we did in uthread_init()).  If you want to
  * destroy a currently running uthread, you'll want something like
@@ -403,7 +436,12 @@ void __ros_mcp_syscall_blockon(struct syscall *sysc)
 {
        /* even if we are in 'vcore context', an _S can block */
        if (!in_multi_mode()) {
+               /* the SCP could have an alarm set to abort this sysc.  When we have a
+                * uth blocked on a sysc, we want this pointer set up (like we do below
+                * for MCP)s */
+               current_uthread->sysc = sysc;
                __ros_scp_syscall_blockon(sysc);
+               current_uthread->sysc = 0;
                return;
        }
        /* MCP vcore's don't know what to do yet, so we have to spin */
@@ -417,8 +455,12 @@ void __ros_mcp_syscall_blockon(struct syscall *sysc)
        assert(current_uthread);
        if (current_uthread->flags & UTHREAD_DONT_MIGRATE) {
                assert(!notif_is_enabled(vcore_id()));  /* catch bugs */
+               /* if we had a notif_disabled_depth, then we should also have
+                * DONT_MIGRATE set */
                __ros_syscall_spinon(sysc);
+               return;
        }
+       assert(!current_uthread->notif_disabled_depth);
        /* double check before doing all this crap */
        if (atomic_read(&sysc->flags) & (SC_DONE | SC_PROGRESS))
                return;
@@ -447,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();
        }
 }
 
@@ -458,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;
        }
@@ -694,19 +740,26 @@ bool __check_preempt_pending(uint32_t vcoreid)
 void uth_disable_notifs(void)
 {
        if (!in_vcore_context() && in_multi_mode()) {
-               if (current_uthread)
-                       current_uthread->flags |= UTHREAD_DONT_MIGRATE;
+               assert(current_uthread);
+               if (current_uthread->notif_disabled_depth++)
+                       goto out;
+               current_uthread->flags |= UTHREAD_DONT_MIGRATE;
                cmb();  /* don't issue the flag write before the vcore_id() read */
                disable_notifs(vcore_id());
        }
+out:
+       if (in_multi_mode())
+               assert(!notif_is_enabled(vcore_id()));
 }
 
 /* Helper: Pair this with uth_disable_notifs(). */
 void uth_enable_notifs(void)
 {
        if (!in_vcore_context() && in_multi_mode()) {
-               if (current_uthread)
-                       current_uthread->flags &= ~UTHREAD_DONT_MIGRATE;
+               assert(current_uthread);
+               if (--current_uthread->notif_disabled_depth)
+                       return;
+               current_uthread->flags &= ~UTHREAD_DONT_MIGRATE;
                cmb();  /* don't enable before ~DONT_MIGRATE */
                enable_notifs(vcore_id());
        }
@@ -910,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