Vcore lists now track vcore statuses
[akaros.git] / kern / src / process.c
index 4262ee1..c9463be 100644 (file)
@@ -241,22 +241,26 @@ void proc_init(void)
 }
 
 /* Be sure you init'd the vcore lists before calling this. */
-void proc_init_procinfo(struct proc* p)
+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. */
@@ -265,6 +269,11 @@ void proc_init_procinfo(struct proc* p)
        }
 }
 
+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
  * writes the *p into **pp, and returns 0 on success, < 0 for an error.
  * Errors include:
@@ -301,8 +310,7 @@ 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));
@@ -312,9 +320,9 @@ error_t proc_alloc(struct proc **pp, struct proc *parent)
        TAILQ_INIT(&p->online_vcs);
        TAILQ_INIT(&p->bulk_preempted_vcs);
        TAILQ_INIT(&p->inactive_vcs);
-       /* Initialize the contents of the e->procinfo structure */
+       /* 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);
@@ -500,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
@@ -795,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)
@@ -828,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;
@@ -1131,11 +1145,17 @@ bool __proc_give_cores(struct proc *SAFE p, uint32_t *pcorelist, size_t num)
                        }
                        // 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++;
                        }
@@ -1147,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,
@@ -1216,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;
@@ -1261,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;