Skip to content

Commit

Permalink
deallocate kernel stack
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6D70 committed Jan 23, 2024
1 parent 72e6a03 commit 0770c2a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 0 additions & 3 deletions xernel/kernel/src/sched/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ impl Process {
);
}

// TODO: how to unmap this stack later??
// We can't add it to Vm because it's not in the lower half of the address space

stack_top
}

Expand Down
24 changes: 24 additions & 0 deletions xernel/kernel/src/sched/thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use alloc::sync::Arc;
use libxernel::sync::Spinlock;
use x86_64::structures::paging::{Page, PageSize, PhysFrame, Size4KiB};

use crate::mem::frame::FRAME_ALLOCATOR;
use crate::mem::paging::KERNEL_PAGE_MAPPER;
use crate::mem::STACK_SIZE;

use super::context::CpuContext;
use super::process::{Process, KERNEL_PROCESS};
Expand Down Expand Up @@ -167,3 +172,22 @@ impl Thread {
self.process.upgrade()
}
}

impl Drop for Thread {
fn drop(&mut self) {
if self.is_kernel_thread() {
let mut page_mapper = KERNEL_PAGE_MAPPER.lock();
let mut frame_allocator = FRAME_ALLOCATOR.lock();

for addr in (self.thread_stack..self.thread_stack + STACK_SIZE as usize).step_by(Size4KiB::SIZE as usize) {
unsafe {
let page = Page::<Size4KiB>::from_start_address(VirtAddr::new(addr as u64)).unwrap();
let phys_addr = page_mapper.translate(page.start_address()).unwrap();

frame_allocator.deallocate_frame(PhysFrame::<Size4KiB>::containing_address(phys_addr));
page_mapper.unmap(page.start_address());
}
}
}
}
}

0 comments on commit 0770c2a

Please sign in to comment.