Fix virtual_memory::drop_table freeing each mapping frame multiple times

This commit is contained in:
pjht 2024-07-07 08:06:15 -05:00
parent a98324c3b4
commit e2d248b609
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -1,6 +1,6 @@
mod holes;
use crate::{bootinfo::BOOTINFO, dbg, physical_memory::PHYSICAL_MEMORY, println};
use crate::{bootinfo::BOOTINFO, physical_memory::PHYSICAL_MEMORY};
use alloc::alloc::{AllocError, Allocator, Layout};
use core::{fmt, ops::Deref, ptr::NonNull, slice};
use replace_with::replace_with_or_abort;
@ -44,7 +44,7 @@ pub enum PagingError {
PageAlreadyMapped,
PagesAlreadyMapped,
PageNotMapped,
InvalidFrameAddress(PhysAddr),
InvalidFrameAddress(#[allow(dead_code)] PhysAddr),
}
impl From<MapToError<Size4KiB>> for PagingError {
@ -474,14 +474,14 @@ impl Drop for AddressSpace {
}
fn drop_table(table: &PageTable, level: u8) {
for (i, entry) in table.iter().enumerate() {
if level == 4 && i >= 256 {
continue;
}
if entry.flags().contains(PageTableFlags::PRESENT)
&& !entry.flags().contains(PageTableFlags::HUGE_PAGE)
{
if level > 2 {
if level > 2 {
for (i, entry) in table.iter().enumerate() {
if level == 4 && i >= 256 {
continue;
}
if entry.flags().contains(PageTableFlags::PRESENT)
&& !entry.flags().contains(PageTableFlags::HUGE_PAGE)
{
// SAFETY: The present flag is set on the entry, which means the child frame must
// contain a valid page table, so making a reference to it must be ok.
// Unwrap: from_start_address requires it's input to be 4KiB aligned (have none of its lower 12 bits
@ -493,17 +493,12 @@ fn drop_table(table: &PageTable, level: u8) {
level - 1,
);
}
let phys_addr = match ACTIVE_SPACE.lock().translate(VirtAddr::from_ptr(table)) {
TranslateResult::Mapped { frame, .. } => frame.start_address(),
_ => panic!("Refrence must point to mapped page!"),
};
unsafe {
PHYSICAL_MEMORY
.lock()
.deallocate_frame(PhysFrame::from_start_address(phys_addr).unwrap());
};
}
}
let phys_addr = ACTIVE_SPACE.lock().translate_addr(VirtAddr::from_ptr(table)).unwrap();
unsafe {
PHYSICAL_MEMORY.lock().deallocate_frame(PhysFrame::from_start_address(phys_addr).unwrap());
};
}
impl Translate for AddressSpace {