Skip to content

Commit

Permalink
create helper function to get next page table
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6D70 committed Jan 25, 2024
1 parent fc6b265 commit 32d3d85
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions xernel/kernel/src/mem/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use limine::KernelAddressRequest;
use x86_64::{
align_down,
registers::control::{Cr3, Cr3Flags},
structures::paging::{Page, PageSize, Size1GiB, Size2MiB, Size4KiB},
structures::paging::{Page, PageSize, PageTableIndex, Size1GiB, Size2MiB, Size4KiB},
};
use x86_64::{
structures::paging::{PageTable, PageTableFlags, PhysFrame},
Expand Down Expand Up @@ -294,6 +294,10 @@ impl Pagemap {
}
}

unsafe fn get_next_pt(pt: *mut PageTable, pt_index: PageTableIndex) -> *mut PageTable {
(*pt)[pt_index].addr().as_u64() as *mut PageTable
}

/// Only works with 4KiB pages
pub fn translate(&self, virt: VirtAddr) -> Option<PhysAddr> {
let pml4 = self.page_table;
Expand All @@ -303,19 +307,19 @@ impl Pagemap {
return None;
}

let pml3 = (*pml4)[virt.p4_index()].addr().as_u64() as *mut PageTable;
let pml3 = Self::get_next_pt(pml4, virt.p4_index());

if !(*pml3)[virt.p3_index()].flags().contains(PageTableFlags::PRESENT) {
return None;
}

let pml2 = (*pml3)[virt.p3_index()].addr().as_u64() as *mut PageTable;
let pml2 = Self::get_next_pt(pml3, virt.p3_index());

if !(*pml2)[virt.p2_index()].flags().contains(PageTableFlags::PRESENT) {
return None;
}

let pml1 = (*pml2)[virt.p2_index()].addr().as_u64() as *mut PageTable;
let pml1 = Self::get_next_pt(pml2, virt.p2_index());

if !(*pml1)[virt.p1_index()].flags().contains(PageTableFlags::PRESENT) {
return None;
Expand Down

0 comments on commit 32d3d85

Please sign in to comment.