Vcore lists now track vcore statuses
[akaros.git] / kern / src / process.c
index fd7d801..c9463be 100644 (file)
@@ -184,6 +184,8 @@ struct proc *pid2proc(pid_t pid)
  * any process related function. */
 void proc_init(void)
 {
+       /* Catch issues with the vcoremap and TAILQ_ENTRY sizes */
+       static_assert(sizeof(TAILQ_ENTRY(vcore)) == sizeof(void*) * 2);
        proc_cache = kmem_cache_create("proc", sizeof(struct proc),
                     MAX(HW_CACHE_ALIGN, __alignof__(struct proc)), 0, 0, 0);
        /* Init PID mask and hash.  pid 0 is reserved. */
@@ -238,23 +240,38 @@ void proc_init(void)
        atomic_init(&num_envs, 0);
 }
 
-void
-proc_init_procinfo(struct proc* p)
+/* Be sure you init'd the vcore lists before calling this. */
+static void proc_init_procinfo(struct proc* p)
 {
-       memset(&p->procinfo->vcoremap, 0, sizeof(p->procinfo->vcoremap));
-       memset(&p->procinfo->pcoremap, 0, sizeof(p->procinfo->pcoremap));
-       p->procinfo->num_vcores = 0;
-       p->procinfo->coremap_seqctr = SEQCTR_INITIALIZER;
-       // TODO: change these too
        p->procinfo->pid = p->pid;
        p->procinfo->ppid = p->ppid;
-       p->procinfo->tsc_freq = system_timing.tsc_freq;
        // TODO: maybe do something smarter here
 #ifdef __CONFIG_DISABLE_SMT__
        p->procinfo->max_vcores = num_cpus >> 1;
 #else
        p->procinfo->max_vcores = MAX(1,num_cpus-num_mgmtcores);
 #endif /* __CONFIG_DISABLE_SMT__ */
+       p->procinfo->tsc_freq = system_timing.tsc_freq;
+       p->procinfo->heap_bottom = (void*)UTEXT;
+       /* 0'ing the arguments.  Some higher function will need to set them */
+       memset(p->procinfo->argp, 0, sizeof(p->procinfo->argp));
+       memset(p->procinfo->argbuf, 0, sizeof(p->procinfo->argbuf));
+       /* 0'ing the vcore/pcore map.  Will link the vcores later. */
+       memset(&p->procinfo->vcoremap, 0, sizeof(p->procinfo->vcoremap));
+       memset(&p->procinfo->pcoremap, 0, sizeof(p->procinfo->pcoremap));
+       p->procinfo->num_vcores = 0;
+       p->procinfo->coremap_seqctr = SEQCTR_INITIALIZER;
+       /* For now, we'll go up to the max num_cpus (at runtime).  In the future,
+        * there may be cases where we can have more vcores than num_cpus, but for
+        * now we'll leave it like this. */
+       for (int i = 0; i < num_cpus; i++) {
+               TAILQ_INSERT_TAIL(&p->inactive_vcs, &p->procinfo->vcoremap[i], list);
+       }
+}
+
+static void proc_init_procdata(struct proc *p)
+{
+       memset(p->procdata, 0, sizeof(struct procdata));
 }
 
 /* Allocates and initializes a process, with the given parent.  Currently
@@ -293,16 +310,19 @@ error_t proc_alloc(struct proc **pp, struct proc *parent)
        p->state = PROC_CREATED; /* shouldn't go through state machine for init */
        p->env_flags = 0;
        p->env_entry = 0; // cheating.  this really gets set later
-       p->procinfo->heap_bottom = (void*)UTEXT;
-       p->heap_top = (void*)UTEXT;
+       p->heap_top = (void*)UTEXT;     /* heap_bottom set in proc_init_procinfo */
        memset(&p->resources, 0, sizeof(p->resources));
        memset(&p->env_ancillary_state, 0, sizeof(p->env_ancillary_state));
        memset(&p->env_tf, 0, sizeof(p->env_tf));
        TAILQ_INIT(&p->vm_regions); /* could init this in the slab */
-
-       /* Initialize the contents of the e->procinfo structure */
+       /* Initialize the vcore lists, we'll build the inactive list so that it includes
+        * all vcores when we initialize procinfo.  Do this before initing procinfo. */
+       TAILQ_INIT(&p->online_vcs);
+       TAILQ_INIT(&p->bulk_preempted_vcs);
+       TAILQ_INIT(&p->inactive_vcs);
+       /* Init procinfo/procdata.  Procinfo's argp/argb are 0'd */
        proc_init_procinfo(p);
-       /* Initialize the contents of the e->procdata structure */
+       proc_init_procdata(p);
 
        /* Initialize the generic sysevent ring buffer */
        SHARED_RING_INIT(&p->procdata->syseventring);
@@ -488,7 +508,9 @@ void proc_run(struct proc *p)
                         * Also, this is the signal used in trap.c to know to save the tf in
                         * env_tf. */
                        __seq_start_write(&p->procinfo->coremap_seqctr);
-                       p->procinfo->num_vcores = 0;
+                       p->procinfo->num_vcores = 0;    /* TODO (VC#) */
+                       /* TODO: For now, we won't count this as an active vcore (on the
+                        * lists).  This gets unmapped in resource.c, and needs work. */
                        __map_vcore(p, 0, core_id()); // sort of.  this needs work.
                        __seq_end_write(&p->procinfo->coremap_seqctr);
                        /* __set_proc_current assumes the reference we give it is for
@@ -710,7 +732,7 @@ static uint32_t get_free_vcoreid(struct proc *SAFE p, uint32_t prev)
 {
        uint32_t i;
        for (i = prev; i < MAX_NUM_CPUS; i++)
-               if (!p->procinfo->vcoremap[i].valid)
+               if (!vcore_is_mapped(p, i))
                        break;
        if (i + 1 >= MAX_NUM_CPUS)
                warn("At the end of the vcorelist.  Might want to check that out.");
@@ -724,7 +746,7 @@ static uint32_t get_busy_vcoreid(struct proc *SAFE p, uint32_t prev)
 {
        uint32_t i;
        for (i = prev; i < MAX_NUM_CPUS; i++)
-               if (p->procinfo->vcoremap[i].valid)
+               if (vcore_is_mapped(p, i))
                        break;
        if (i + 1 >= MAX_NUM_CPUS)
                warn("At the end of the vcorelist.  Might want to check that out.");
@@ -750,7 +772,7 @@ static uint32_t get_vcoreid(struct proc *p, uint32_t pcoreid)
  * No locking involved, be careful.  Panics on failure. */
 static uint32_t get_pcoreid(struct proc *p, uint32_t vcoreid)
 {
-       assert(p->procinfo->vcoremap[vcoreid].valid);
+       assert(vcore_is_mapped(p, vcoreid));
        return p->procinfo->vcoremap[vcoreid].pcoreid;
 }
 
@@ -783,7 +805,7 @@ void __proc_yield_s(struct proc *p, struct trapframe *tf)
 void proc_yield(struct proc *SAFE p, bool being_nice)
 {
        uint32_t vcoreid = get_vcoreid(p, core_id());
-       struct vcore *vc = &p->procinfo->vcoremap[vcoreid];
+       struct vcore *vc = vcoreid2vcore(p, vcoreid);
 
        /* no reason to be nice, return */
        if (being_nice && !vc->preempt_pending)
@@ -816,8 +838,12 @@ void proc_yield(struct proc *SAFE p, bool being_nice)
                                return;
                        }
                        __seq_start_write(&p->procinfo->coremap_seqctr);
-                       // give up core
-                       __unmap_vcore(p, get_vcoreid(p, core_id()));
+                       /* Remove from the online list, add to the yielded list, and unmap
+                        * the vcore, which gives up the core. */
+                       TAILQ_REMOVE(&p->online_vcs, vc, list);
+                       TAILQ_INSERT_HEAD(&p->inactive_vcs, vc, list);
+                       __unmap_vcore(p, vcoreid);
+                       /* Adjust implied resource desires */
                        p->resources[RES_CORES].amt_granted = --(p->procinfo->num_vcores);
                        if (!being_nice)
                                p->resources[RES_CORES].amt_wanted = p->procinfo->num_vcores;
@@ -870,14 +896,14 @@ void proc_notify(struct proc *p, uint32_t vcoreid)
                if (vcpd->notif_enabled) {
                        /* GIANT WARNING: we aren't using the proc-lock to protect the
                         * vcoremap.  We want to be able to use this from interrupt context,
-                        * and don't want the proc_lock to be an irqsave. */
+                        * and don't want the proc_lock to be an irqsave.  Spurious
+                        * __notify() kmsgs are okay (it checks to see if the right receiver
+                        * is current). */
                        if ((p->state & PROC_RUNNING_M) && // TODO: (VC#) (_S state)
-                                     (p->procinfo->vcoremap[vcoreid].valid)) {
+                                     vcore_is_mapped(p, vcoreid)) {
                                printd("[kernel] sending notif to vcore %d\n", vcoreid);
                                send_kernel_message(get_pcoreid(p, vcoreid), __notify, (long)p,
                                                    0, 0, KMSG_ROUTINE);
-                       } else { // TODO: think about this, fallback, etc
-                               warn("Vcore unmapped, not receiving an active notif");
                        }
                }
        }
@@ -1057,6 +1083,23 @@ uint32_t proc_get_vcoreid(struct proc *SAFE p, uint32_t pcoreid)
        }
 }
 
+/* TODO: make all of these static inlines when we gut the env crap */
+bool vcore_is_mapped(struct proc *p, uint32_t vcoreid)
+{
+       return p->procinfo->vcoremap[vcoreid].valid;
+}
+
+/* Can do this, or just create a new field and save it in the vcoremap */
+uint32_t vcore2vcoreid(struct proc *p, struct vcore *vc)
+{
+       return (vc - p->procinfo->vcoremap);
+}
+
+struct vcore *vcoreid2vcore(struct proc *p, uint32_t vcoreid)
+{
+       return &p->procinfo->vcoremap[vcoreid];
+}
+
 /* Gives process p the additional num cores listed in pcorelist.  You must be
  * RUNNABLE_M or RUNNING_M before calling this.  If you're RUNNING_M, this will
  * startup your new cores at the entry point with their virtual IDs (or restore
@@ -1098,15 +1141,21 @@ bool __proc_give_cores(struct proc *SAFE p, uint32_t *pcorelist, size_t num)
                                // somewhere, like someone forgot to take vcores after
                                // preempting.
                                for (int i = 0; i < p->procinfo->num_vcores; i++)
-                                       assert(p->procinfo->vcoremap[i].valid);
+                                       assert(vcore_is_mapped(p, i));
                        }
                        // add new items to the vcoremap
                        __seq_start_write(&p->procinfo->coremap_seqctr);
+                       /* TODO: consider bulk preemption */
                        for (int i = 0; i < num; i++) {
+                               /* TODO: (VCL) should be the head item, and could be empty */
                                // find the next free slot, which should be the next one
                                free_vcoreid = get_free_vcoreid(p, free_vcoreid);
                                printd("setting vcore %d to pcore %d\n", free_vcoreid,
                                       pcorelist[i]);
+                               TAILQ_REMOVE(&p->inactive_vcs, vcoreid2vcore(p, free_vcoreid),
+                                            list);
+                               TAILQ_INSERT_TAIL(&p->online_vcs, vcoreid2vcore(p, free_vcoreid),
+                                                 list);
                                __map_vcore(p, free_vcoreid, pcorelist[i]);
                                p->procinfo->num_vcores++;
                        }
@@ -1118,9 +1167,14 @@ bool __proc_give_cores(struct proc *SAFE p, uint32_t *pcorelist, size_t num)
                        proc_incref(p, num);
                        __seq_start_write(&p->procinfo->coremap_seqctr);
                        for (int i = 0; i < num; i++) {
+                               /* TODO: (VCL) should be the head item, and could be empty */
                                free_vcoreid = get_free_vcoreid(p, free_vcoreid);
                                printd("setting vcore %d to pcore %d\n", free_vcoreid,
                                       pcorelist[i]);
+                               TAILQ_REMOVE(&p->inactive_vcs, vcoreid2vcore(p, free_vcoreid),
+                                            list);
+                               TAILQ_INSERT_TAIL(&p->online_vcs, vcoreid2vcore(p, free_vcoreid),
+                                                 list);
                                __map_vcore(p, free_vcoreid, pcorelist[i]);
                                p->procinfo->num_vcores++;
                                send_kernel_message(pcorelist[i], __startcore, (long)p, 0, 0,
@@ -1187,6 +1241,11 @@ bool __proc_take_cores(struct proc *p, uint32_t *pcorelist, size_t num,
                // while ugly, this is done to facilitate merging with take_all_cores
                pcoreid = get_pcoreid(p, vcoreid);
                assert(pcoreid == pcorelist[i]);
+               /* Change lists for the vcore.  We do this before either unmapping or
+                * sending the message, so the lists represent what will be very soon
+                * (before we unlock, the messages are in flight). */
+               TAILQ_REMOVE(&p->online_vcs, vcoreid2vcore(p, vcoreid), list);
+               TAILQ_INSERT_HEAD(&p->inactive_vcs, vcoreid2vcore(p, vcoreid), list);
                if (message) {
                        if (pcoreid == core_id())
                                self_ipi_pending = TRUE;
@@ -1232,10 +1291,17 @@ bool __proc_take_allcores(struct proc *p, amr_t message, long arg0, long arg1,
        assert(num_idlecores + p->procinfo->num_vcores <= num_cpus); // sanity
        spin_unlock(&idle_lock);
        __seq_start_write(&p->procinfo->coremap_seqctr);
+       /* TODO: (VCL) use the active list, make it a while loop, assert, etc */
        for (int i = 0; i < p->procinfo->num_vcores; i++) {
                // find next active vcore
                active_vcoreid = get_busy_vcoreid(p, active_vcoreid);
                pcoreid = get_pcoreid(p, active_vcoreid);
+               /* Change lists for the vcore.  We do this before either unmapping or
+                * sending the message, so the lists represent what will be very soon
+                * (before we unlock, the messages are in flight). */
+               TAILQ_REMOVE(&p->online_vcs, vcoreid2vcore(p, active_vcoreid), list);
+               TAILQ_INSERT_HEAD(&p->inactive_vcs, vcoreid2vcore(p, active_vcoreid),
+                                 list);
                if (message) {
                        if (pcoreid == core_id())
                                self_ipi_pending = TRUE;
@@ -1400,9 +1466,15 @@ void __startcore(struct trapframe *tf, uint32_t srcid, long a0, long a1, long a2
                proc_decref(p_to_run);
        vcoreid = get_vcoreid(p_to_run, pcoreid);
        vcpd = &p_to_run->procdata->vcore_preempt_data[vcoreid];
+       /* We could let userspace do this, though they come into vcore entry many
+        * times, and we just need this to happen when the cores comes online the
+        * first time.  That, and they want this turned on as soon as we know a
+        * vcore *WILL* be online.  We could also do this earlier, when we map the
+        * vcore to its pcore, though we don't always have current loaded or
+        * otherwise mess with the VCPD in those code paths. */
+       vcpd->can_rcv_msg = TRUE;
        printd("[kernel] startcore on physical core %d for process %d's vcore %d\n",
               pcoreid, p_to_run->pid, vcoreid);
-
        if (seq_is_locked(vcpd->preempt_tf_valid)) {
                __seq_end_write(&vcpd->preempt_tf_valid); /* mark tf as invalid */
                restore_fp_state(&vcpd->preempt_anc);
@@ -1555,6 +1627,7 @@ void print_proc_info(pid_t pid)
 {
        int j = 0;
        struct proc *p = pid2proc(pid);
+       struct vcore *vc_i;
        if (!p) {
                printk("Bad PID.\n");
                return;
@@ -1569,13 +1642,23 @@ void print_proc_info(pid_t pid)
        printk("Flags: 0x%08x\n", p->env_flags);
        printk("CR3(phys): 0x%08x\n", p->env_cr3);
        printk("Num Vcores: %d\n", p->procinfo->num_vcores);
-       printk("Vcoremap:\n");
+       printk("Vcoremap (old style):\n");
        for (int i = 0; i < p->procinfo->num_vcores; i++) {
                j = get_busy_vcoreid(p, j);
                printk("\tVcore %d: Pcore %d\n", j, get_pcoreid(p, j));
                j++;
        }
-       printk("Resources:\n");
+       printk("Vcore Lists:\n----------------------\n");
+       printk("Online:\n");
+       TAILQ_FOREACH(vc_i, &p->online_vcs, list)
+               printk("\tVcore %d -> Pcore %d\n", vcore2vcoreid(p, vc_i), vc_i->pcoreid);
+       printk("Bulk Preempted:\n");
+       TAILQ_FOREACH(vc_i, &p->bulk_preempted_vcs, list)
+               printk("\tVcore %d\n", vcore2vcoreid(p, vc_i));
+       printk("Inactive / Yielded:\n");
+       TAILQ_FOREACH(vc_i, &p->inactive_vcs, list)
+               printk("\tVcore %d\n", vcore2vcoreid(p, vc_i));
+       printk("Resources:\n------------------------\n");
        for (int i = 0; i < MAX_NUM_RESOURCES; i++)
                printk("\tRes type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
                       p->resources[i].amt_wanted, p->resources[i].amt_granted);