Proc refcnting wrappers
[akaros.git] / kern / src / resource.c
index 46be3df..48a83d9 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <resource.h>
 #include <process.h>
+#include <smp.h>
 #include <stdio.h>
 #include <assert.h>
 #include <schedule.h>
@@ -42,33 +43,46 @@ ssize_t core_request(struct proc *p)
        bool need_to_idle = FALSE;
        bool self_ipi_pending = FALSE;
 
-       spin_lock_irqsave(&p->proc_lock);
+       /* There are a few things broken for now if you don't have a current_tf */
+       assert(current_tf);
+       spin_lock(&p->proc_lock);
+       if (p->state == PROC_DYING) {
+               return -EFAIL;
+               spin_unlock(&p->proc_lock);
+       }
        /* check to see if this is a full deallocation.  for cores, it's a
         * transition from _M to _S.  Will be issues with handling this async. */
        if (!p->resources[RES_CORES].amt_wanted) {
                assert(p->state == PROC_RUNNING_M); // TODO: (ACR) async core req
-               // save the context, to be restarted in _S mode
+               /* save the context, to be restarted in _S mode */
                p->env_tf = *current_tf;
-               env_push_ancillary_state(p);
-               proc_set_syscall_retval(&p->env_tf, ESUCCESS);
+               current_tf = 0;                 /* Make sure it isn't used in the future */
+               env_push_ancillary_state(p); // TODO: (HSS)
                /* sending death, since it's not our job to save contexts or anything in
                 * this case.  also, if this returns true, we will not return down
                 * below, and need to eat the reference to p */
                self_ipi_pending = __proc_take_allcores(p, __death, 0, 0, 0);
                __proc_set_state(p, PROC_RUNNABLE_S);
                schedule_proc(p);
-               __proc_unlock_ipi_pending(p, self_ipi_pending);
+               spin_unlock(&p->proc_lock);
+               __proc_kmsg_pending(p, self_ipi_pending);
                return 0;
        }
+       /* Fail if we can never handle this amount (based on how many we told the
+        * process it can get). */
+       if (p->resources[RES_CORES].amt_wanted > p->procinfo->max_vcores) {
+               spin_unlock(&p->proc_lock);
+               return -EFAIL;
+       }
        /* otherwise, see how many new cores are wanted */
        amt_new = p->resources[RES_CORES].amt_wanted -
                  p->resources[RES_CORES].amt_granted;
        if (amt_new < 0) {
                p->resources[RES_CORES].amt_wanted = p->resources[RES_CORES].amt_granted;
-               spin_unlock_irqsave(&p->proc_lock);
+               spin_unlock(&p->proc_lock);
                return -EINVAL;
        } else if (amt_new == 0) {
-               spin_unlock_irqsave(&p->proc_lock);
+               spin_unlock(&p->proc_lock);
                return 0;
        }
        // else, we try to handle the request
@@ -87,26 +101,33 @@ ssize_t core_request(struct proc *p)
                }
                num_granted = amt_new;
        } else {
+               /* In this case, you might want to preempt or do other fun things... */
                num_granted = 0;
        }
        spin_unlock(&idle_lock);
+
        // Now, actually give them out
        if (num_granted) {
-               p->resources[RES_CORES].amt_granted += num_granted;
                switch (p->state) {
                        case (PROC_RUNNING_S):
                                // issue with if we're async or not (need to preempt it)
                                // either of these should trip it. TODO: (ACR) async core req
                                // TODO: relies on vcore0 being the caller (VC#)
+                               // TODO: do this in process.c and use this line:
+                               //if ((current != p) || (get_pcoreid(p, 0) != core_id()))
                                if ((current != p) || (p->procinfo->vcoremap[0].pcoreid != core_id()))
                                        panic("We don't handle async RUNNING_S core requests yet.");
-                               /* save the tf to be restarted on another core (in proc_run) */
-                               p->env_tf = *current_tf;
-                               env_push_ancillary_state(p);
-                               /* set the return code to 0. since we're transitioning, vcore0
-                                * will start up with the tf manually, and not get the return
-                                * value through the regular syscall return path */
-                               proc_set_syscall_retval(&p->env_tf, ESUCCESS);
+                               /* save the tf so userspace can restart it.  Like in __notify,
+                                * this assumes a user tf is the same as a kernel tf.  We save
+                                * it in the preempt slot so that we can also save the silly
+                                * state. */
+                               struct preempt_data *vcpd = &p->procdata->vcore_preempt_data[0];
+                               vcpd->preempt_tf = *current_tf;
+                               save_fp_state(&vcpd->preempt_anc);
+                               __seq_start_write(&vcpd->preempt_tf_valid);
+                               /* If we remove this, vcore0 will start where the _S left off */
+                               vcpd->notif_pending = TRUE;
+                               assert(vcpd->notif_enabled);
                                /* in the async case, we'll need to remotely stop and bundle
                                 * vcore0's TF.  this is already done for the sync case (local
                                 * syscall). */
@@ -121,8 +142,6 @@ ssize_t core_request(struct proc *p)
                                need_to_idle = TRUE;
                                // change to runnable_m (it's TF is already saved)
                                __proc_set_state(p, PROC_RUNNABLE_M);
-                               // signals to proc_run that this is a _S to _M transition
-                               p->env_flags |= PROC_TRANSITION_TO_M;
                                break;
                        case (PROC_RUNNABLE_S):
                                /* Issues: being on the runnable_list, proc_set_state not liking
@@ -131,12 +150,16 @@ ssize_t core_request(struct proc *p)
                                 * descheduled? */
                                panic("Not supporting RUNNABLE_S -> RUNNABLE_M yet.\n");
                                break;
+                       case (PROC_DYING):
+                               warn("Dying, core request coming from %d\n", core_id());
                        default:
                                break;
                }
                /* give them the cores.  this will start up the extras if RUNNING_M. */
                self_ipi_pending = __proc_give_cores(p, corelist, num_granted);
-               __proc_unlock_ipi_pending(p, self_ipi_pending);
+               spin_unlock(&p->proc_lock);
+               // TODO: (RMS) think about this, esp when its called from a scheduler
+               __proc_kmsg_pending(p, self_ipi_pending);
                /* if there's a race on state (like DEATH), it'll get handled by
                 * proc_run or proc_destroy */
                if (p->state == PROC_RUNNABLE_M)
@@ -146,11 +169,12 @@ ssize_t core_request(struct proc *p)
                 * (just like in proc_destroy).  it also needs to decref, to consume the
                 * reference that came into this function (since we don't return).  */
                if (need_to_idle) {
-                       proc_decref(p, 1);
+                       proc_decref(p);
                        abandon_core();
+                       smp_idle();
                }
        } else { // nothing granted, just return
-               spin_unlock_irqsave(&p->proc_lock);
+               spin_unlock(&p->proc_lock);
        }
        return num_granted;
 }
@@ -166,23 +190,21 @@ error_t resource_req(struct proc *p, int type, size_t amt_wanted,
                printk("[kernel] Async requests treated synchronously for now.\n");
 
        /* set the desired resource amount in the process's resource list. */
-       spin_lock_irqsave(&p->proc_lock);
+       spin_lock(&p->proc_lock);
        size_t old_amount = p->resources[type].amt_wanted;
        p->resources[type].amt_wanted = amt_wanted;
        p->resources[type].amt_wanted_min = MIN(amt_wanted_min, amt_wanted);
        p->resources[type].flags = flags;
-       spin_unlock_irqsave(&p->proc_lock);
-
-       // no change in the amt_wanted
-       if (old_amount == amt_wanted)
-               return 0;
+       spin_unlock(&p->proc_lock);
 
        switch (type) {
                case RES_CORES:
                        retval = core_request(p);
                        // i don't like this retval hackery
-                       if (retval < 0)
-                               return retval;
+                       if (retval < 0) {
+                               set_errno(-retval);
+                               return -1;
+                       }
                        else
                                return 0;
                        break;