From d0f81e3d2afdf7a89fd466799f2d900da9f2ac40 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 14 Mar 2024 20:57:12 -0400 Subject: kernel: sched: Add exit routine Signed-off-by: Ian Moffett --- sys/include/sys/sched.h | 1 + sys/kern/kern_sched.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) (limited to 'sys') diff --git a/sys/include/sys/sched.h b/sys/include/sys/sched.h index d803df0..f06c104 100644 --- a/sys/include/sys/sched.h +++ b/sys/include/sys/sched.h @@ -38,6 +38,7 @@ #include void sched_init(void); +void sched_exit(void); void sched_context_switch(struct trapframe *tf); __noreturn diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index f985f62..c3d1e09 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -280,6 +280,50 @@ sched_create_td(uintptr_t rip, char *argvp[], char *envp[], struct auxval auxv, return td; } +static void +sched_destroy_td(struct proc *td) +{ + processor_free_pcb(td); + + /* + * User stacks are allocated with vm_alloc_pageframe(), + * while kernel stacks are allocated with dynalloc(). + * We want to check if we are a user program or kernel + * program to perform the proper deallocation method. + */ + if (td->is_user) { + vm_free_pageframe(td->stack_base, STACK_PAGES); + } else { + dynfree((void *)td->stack_base); + } + + pmap_free_vas(vm_get_ctx(), td->addrsp); + dynfree(td); +} + +void +sched_exit(void) +{ + struct sched_state *state; + struct cpu_info *ci; + struct proc *td; + struct vas kvas = vm_get_kvas(); + + intr_mask(); + + /* Get the thread running on the current processor */ + ci = this_cpu(); + state = &ci->sched_state; + td = state->td; + + /* Switch back to the kernel address space and destroy ourself */ + pmap_switch_vas(vm_get_ctx(), kvas); + sched_destroy_td(td); + + intr_unmask(); + sched_enter(); +} + /* * Thread context switch routine */ -- cgit v1.2.3