[PATCH 2/2] Adds some resolv functionality (XCC)
[akaros.git] / user / parlib / uthread.c
index 44093b9..a77ae77 100644 (file)
@@ -24,6 +24,11 @@ static int __uthread_reinit_tls(struct uthread *uthread);
 static void __uthread_free_tls(struct uthread *uthread);
 static void __run_current_uthread_raw(void);
 
+static void handle_vc_preempt(struct event_msg *ev_msg, unsigned int ev_type,
+                              void *data);
+static void handle_vc_indir(struct event_msg *ev_msg, unsigned int ev_type,
+                            void *data);
+
 /* Block the calling uthread on sysc until it makes progress or is done */
 static void __ros_mcp_syscall_blockon(struct syscall *sysc);
 
@@ -50,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
@@ -73,6 +90,7 @@ void uthread_lib_init(struct uthread *uthread)
        init_once_racy(return);
        vcore_init();
        uthread_manage_thread0(uthread);
+       register_ev_handler(EV_EVENT, handle_ev_ev, 0);
        /* Receive preemption events.  Note that this merely tells the kernel how to
         * send the messages, and does not necessarily provide storage space for the
         * messages.  What we're doing is saying that all PREEMPT and CHECK_MSGS
@@ -81,8 +99,8 @@ void uthread_lib_init(struct uthread *uthread)
         *
         * It is critical that these are either SPAM_PUB or INDIR|FALLBACK, so that
         * yielding vcores do not miss the preemption messages. */
-       ev_handlers[EV_VCORE_PREEMPT] = handle_vc_preempt;
-       ev_handlers[EV_CHECK_MSGS] = handle_vc_indir;
+       register_ev_handler(EV_VCORE_PREEMPT, handle_vc_preempt, 0);
+       register_ev_handler(EV_CHECK_MSGS, handle_vc_indir, 0);
        preempt_ev_q = get_event_q();   /* small ev_q, mostly a vehicle for flags */
        preempt_ev_q->ev_flags = EVENT_IPI | EVENT_SPAM_PUBLIC | EVENT_VCORE_APPRO |
                                                         EVENT_VCORE_MUST_RUN;
@@ -220,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)
@@ -227,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;
        }
@@ -273,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);
@@ -339,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
@@ -372,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
@@ -397,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 */
@@ -411,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;
@@ -441,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();
        }
 }
 
@@ -452,18 +502,29 @@ 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;
        }
 }
 
+/* Attempts to handle a fault for uth, etc */
+static void handle_refl_fault(struct uthread *uth, struct user_context *ctx)
+{
+       sched_ops->thread_refl_fault(uth, __arch_refl_get_nr(ctx),
+                                    __arch_refl_get_err(ctx),
+                                    __arch_refl_get_aux(ctx));
+}
+
 /* Run the thread that was current_uthread, from a previous run.  Should be
  * called only when the uthread already was running, and we were interrupted by
  * the kernel (event, etc).  Do not call this to run a fresh uthread, even if
  * you've set it to be current. */
 void run_current_uthread(void)
 {
+       struct uthread *uth;
        uint32_t vcoreid = vcore_id();
        struct preempt_data *vcpd = vcpd_of(vcoreid);
        assert(current_uthread);
@@ -473,6 +534,22 @@ void run_current_uthread(void)
        assert(!(current_uthread->flags & UTHREAD_FPSAVED));
        printd("[U] Vcore %d is restarting uthread %08p\n", vcoreid,
               current_uthread);
+       if (has_refl_fault(&vcpd->uthread_ctx)) {
+               clear_refl_fault(&vcpd->uthread_ctx);
+               /* we preemptively copy out and make non-running, so that there is a
+                * consistent state for the handler.  it can then block the uth or
+                * whatever. */
+               uth = current_uthread;
+               current_uthread = 0;
+               uth->u_ctx = vcpd->uthread_ctx;
+               save_fp_state(&uth->as);
+               uth->state = UT_NOT_RUNNING;
+               uth->flags |= UTHREAD_SAVED | UTHREAD_FPSAVED;
+               handle_refl_fault(uth, &vcpd->uthread_ctx);
+               /* we abort no matter what.  up to the 2LS to reschedule the thread */
+               set_stack_pointer((void*)vcpd->transition_stack);
+               vcore_entry();
+       }
        /* Go ahead and start the uthread */
        set_uthread_tls(current_uthread, vcoreid);
        /* Run, using the TF in the VCPD.  FP state should already be loaded */
@@ -507,6 +584,13 @@ void run_uthread(struct uthread *uthread)
                assert(uthread->flags & UTHREAD_FPSAVED);
        else
                assert(!(uthread->flags & UTHREAD_FPSAVED));
+       if (has_refl_fault(&uthread->u_ctx)) {
+               clear_refl_fault(&uthread->u_ctx);
+               handle_refl_fault(uthread, &uthread->u_ctx);
+               /* we abort no matter what.  up to the 2LS to reschedule the thread */
+               set_stack_pointer((void*)vcpd->transition_stack);
+               vcore_entry();
+       }
        uthread->state = UT_RUNNING;
        /* Save a ptr to the uthread we'll run in the transition context's TLS */
        current_uthread = uthread;
@@ -528,6 +612,10 @@ static void __run_current_uthread_raw(void)
 {
        uint32_t vcoreid = vcore_id();
        struct preempt_data *vcpd = vcpd_of(vcoreid);
+       if (has_refl_fault(&vcpd->uthread_ctx)) {
+               printf("Raw / DONT_MIGRATE uthread took a fault, exiting.\n");
+               exit(-1);
+       }
        /* We need to manually say we have a notif pending, so we eventually return
         * to vcore context.  (note the kernel turned it off for us) */
        vcpd->notif_pending = TRUE;
@@ -652,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());
        }
@@ -800,7 +895,8 @@ out_we_returned:
 
 /* This handles a preemption message.  When this is done, either we recovered,
  * or recovery *for our message* isn't needed. */
-void handle_vc_preempt(struct event_msg *ev_msg, unsigned int ev_type)
+static void handle_vc_preempt(struct event_msg *ev_msg, unsigned int ev_type,
+                              void *data)
 {
        uint32_t vcoreid = vcore_id();
        struct preempt_data *vcpd = vcpd_of(vcoreid);
@@ -867,7 +963,7 @@ 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
@@ -899,7 +995,8 @@ out_stealing:
  * their indirs, or the vcore restarted enough so that checking them is
  * unnecessary.  If that happens and they got preempted quickly, then another
  * preempt/check_indirs was sent out. */
-void handle_vc_indir(struct event_msg *ev_msg, unsigned int ev_type)
+static void handle_vc_indir(struct event_msg *ev_msg, unsigned int ev_type,
+                            void *data)
 {
        uint32_t vcoreid = vcore_id();
        uint32_t rem_vcoreid = ev_msg->ev_arg2;