From 704ab909fa30ef37762530ca7f1e5bff25c24e15 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Tue, 13 Feb 2024 00:29:28 +0000 Subject: got basic tests to work --- kernel/main/kmain.c | 3 -- kernel/proc/proc.c | 89 +++++++++++++++++++++++++++++------------------------ kernel/proc/sched.c | 21 +++++++------ 3 files changed, 61 insertions(+), 52 deletions(-) (limited to 'kernel') diff --git a/kernel/main/kmain.c b/kernel/main/kmain.c index e45299a..b1402c5 100644 --- a/kernel/main/kmain.c +++ b/kernel/main/kmain.c @@ -205,9 +205,6 @@ void initproc_start() sched_make_runnable(init_thread); - // update current thread to main thread - curthr = init_thread; - context_make_active(&curcore.kc_ctx); // start the scheduler // context_make_active(&init_thread->kt_ctx); // TODO: ask about how the core is linked to scheduler diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index ef8245d..73c6aa0 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -211,15 +211,13 @@ proc_t *proc_create(const char *name) if (proc_pid == PID_INIT) { - // if it's init proc, set to global variable + // if it's init proc, set to global variable proc_initproc = proc; } - else - { - // FIXME: ask in hours - // if it's not init proc, set parent proc to the init proc - proc->p_pproc = proc_initproc; - } + + proc->p_pproc = proc_initproc; + list_insert_tail(&curproc->p_children, &proc->p_child_link); + list_insert_tail(&proc_list, &proc->p_list_link); return proc; } @@ -245,36 +243,30 @@ void proc_cleanup(long status) { // NOT_YET_IMPLEMENTED("PROCS: proc_cleanup"); - // free resources of this proc (save slab_obj_free for proc_destroy) - pt_destroy(curproc->p_pml4); - - - // NOT_YET_IMPLEMENTED("PROCS: proc_cleanup"); if (curproc->p_pid == PID_INIT) { // if it's init proc, shutdown weenix initproc_finish(status); } - else - { - // if it's not init proc, reparent child processes to parent proc - if (curproc->p_pproc == NULL) - { - panic("parent process not found in proc_pleanup"); - } - list_link_t *link; - list_iterate(&curproc->p_children, child, proc_t, p_child_link) - { - // remove each child from current process and insert to init process - list_remove(&child->p_child_link); - list_insert_tail(&curproc->p_pproc->p_children, &child->p_child_link); - } + // if it's not init proc, reparent child processes to parent proc + if (curproc->p_pproc == NULL) + { + panic("parent process not found in proc_pleanup"); + } - // update state and status - curproc->p_state = PROC_DEAD; - curproc->p_status = status; + list_link_t *link; + list_iterate(&curproc->p_children, child, proc_t, p_child_link) + { + // remove insert to init process + list_insert_tail(&curproc->p_pproc->p_children, &child->p_child_link); + list_remove(&child->p_child_link); + child->p_pproc = proc_initproc; } + + // update state and status + curproc->p_state = PROC_DEAD; + curproc->p_status = status; } /* @@ -298,8 +290,8 @@ void proc_thread_exiting(void *retval) proc_cleanup((long)retval); curthr->kt_state = KT_EXITED; - sched_broadcast_on(&curproc->p_wait); - sched_switch(&curproc->p_wait); + sched_broadcast_on(&curproc->p_pproc->p_wait); + sched_switch(0); } /* @@ -425,6 +417,8 @@ pid_t do_waitpid(pid_t pid, int *status, int options) return -ENOTSUP; } + dbg(DBG_PROC, "waiting for pid=%d\n", pid); + if (pid > 0) { proc_t *child = proc_lookup(pid); @@ -445,6 +439,7 @@ pid_t do_waitpid(pid_t pid, int *status, int options) } proc_destroy(child); + dbg(DBG_PROC, "exited child pid=%d\n", child->p_pid); return pid; } else if (pid == -1) @@ -454,19 +449,31 @@ pid_t do_waitpid(pid_t pid, int *status, int options) return -ECHILD; } - proc_t *child; - list_iterate(&curproc->p_children, child, proc_t, p_child_link) - { - if (child->p_state == PROC_DEAD) + + while(1) { + proc_t *child; + list_iterate(&curproc->p_children, child, proc_t, p_child_link) { - if (status != NULL) + if (child->p_state == PROC_DEAD) { - *status = child->p_status; - } + dbg(DBG_PROC, "found a dead thread with pid=%d\n", child->p_pid); + if (status != NULL) + { + *status = child->p_status; + } + int child_pid = child->p_pid; - proc_destroy(child); - return child->p_pid; + + list_remove(&child->p_child_link); + proc_destroy(child); + + dbg(DBG_PROC, "exited child pid=%d\n", child->p_pid); + return child_pid; + } } + + dbg(DBG_PROC, "waiting for any child, calling sched_sleep_on\n"); + sched_sleep_on(&curproc->p_wait); } } @@ -480,6 +487,8 @@ void do_exit(long status) { // NOT_YET_IMPLEMENTED("PROCS: do_exit"); + dbg(DBG_PROC, "proc exiting with pid=%d", curproc->p_pid); + kthread_exit((void *)status); } diff --git a/kernel/proc/sched.c b/kernel/proc/sched.c index d0e634f..0461279 100644 --- a/kernel/proc/sched.c +++ b/kernel/proc/sched.c @@ -74,6 +74,7 @@ void sched_queue_init(ktqueue_t *queue) */ static void ktqueue_enqueue(ktqueue_t *queue, kthread_t *thr) { + // TODO: ask in mentor meeting about what causes this assertion error KASSERT(!thr->kt_wchan); list_assert_sanity(&queue->tq_list); @@ -229,19 +230,19 @@ void sched_switch(ktqueue_t *queue) { // NOT_YET_IMPLEMENTED("PROCS: sched_switch"); - KASSERT(intr_getipl == IPL_HIGH); + // KASSERT(intr_getipl == IPL_HIGH); intr_disable(); + int oldIPL = intr_setipl(IPL_LOW); // allow interrupts to wake up the idling core KASSERT(curthr->kt_state != KT_ON_CPU); - ktqueue_enqueue(queue, curthr); curcore.kc_queue = queue; last_thread_context = &curthr->kt_ctx; - intr_setipl(IPL_LOW); // allow interrupts to wake up the idling core - intr_enable(); context_switch(&curthr->kt_ctx, &curcore.kc_ctx); + intr_enable(); + intr_setipl(oldIPL); // TODO: ask about why we need the old thread context } @@ -271,11 +272,11 @@ void sched_make_runnable(kthread_t *thr) // NOT_YET_IMPLEMENTED("PROCS: sched_make_runnable"); dbg(DBG_SCHED, "Making thread with proc pid %d runnable\n in thread\n", thr->kt_proc->p_pid); - if (curthr == NULL) + if (curthr) { - dbg(DBG_SCHED, "I did this ^^ with a null thread!\n"); - } else { dbg(DBG_SCHED, "I did this ^^ with thread %d\n", curthr->kt_proc->p_pid); + } else { + dbg(DBG_SCHED, "I did this ^^ with a null thread!\n"); } KASSERT(thr != curthr); @@ -307,9 +308,8 @@ void sched_sleep_on(ktqueue_t *q) int oldIPL = intr_setipl(IPL_HIGH); curthr->kt_state = KT_SLEEP; - sched_switch(q); - // intr_setipl(oldIPL); not called, hold the lock into the sched_switch call + intr_setipl(oldIPL); } /* @@ -433,6 +433,9 @@ void core_switch() KASSERT(mapped_paddr == expected_paddr); curthr = next_thread; + + dbg(DBG_THR, "Switching to curthr thread %d\n", curthr->kt_proc->p_pid); + curthr->kt_state = KT_ON_CPU; curproc = curthr->kt_proc; context_switch(&curcore.kc_ctx, &curthr->kt_ctx); -- cgit v1.2.3-70-g09d2