message: Paging: Add debugging functions
diff: @@ -235,3 +235,79 @@ pub fn init_page_tables() {
  tlb::flush_all();
  }
  }
  +
  +#[allow(dead_code)]
  +unsafe fn disect(virt_addr: x86_64::VirtAddr) {
  + use x86_64::structures::paging::{
  + mapper::{MappedFrame, TranslateResult},
  + Translate,
  + };
  + use x86_64::structures::paging::{Size1GiB, Size4KiB};
  +
  + let recursive_page_table = unsafe { recursive_page_table() };
  +
  + match recursive_page_table.translate(virt_addr) {
  + TranslateResult::Mapped {
  + frame,
  + offset,
  + flags,
  + } => {
  + let phys_addr = frame.start_address() + offset;
  + println!("virt_addr: {virt_addr:p}, phys_addr: {phys_addr:p}, flags: {flags:?}");
  + match frame {
  + MappedFrame::Size4KiB(_) => {
  + let page = Page::<Size4KiB>::containing_address(virt_addr);
  + println!(
  + "p4: {}, p3: {}, p2: {}, p1: {}",
  + u16::from(page.p4_index()),
  + u16::from(page.p3_index()),
  + u16::from(page.p2_index()),
  + u16::from(page.p1_index())
  + );
  + }
  + MappedFrame::Size2MiB(_) => {
  + let page = Page::<Size2MiB>::containing_address(virt_addr);
  + println!(
  + "p4: {}, p3: {}, p2: {}",
  + u16::from(page.p4_index()),
  + u16::from(page.p3_index()),
  + u16::from(page.p2_index()),
  + );
  + }
  + MappedFrame::Size1GiB(_) => {
  + let page = Page::<Size1GiB>::containing_address(virt_addr);
  + println!(
  + "p4: {}, p3: {}",
  + u16::from(page.p4_index()),
  + u16::from(page.p3_index()),
  + );
  + }
  + }
  + }
  + TranslateResult::NotMapped => todo!(),
  + TranslateResult::InvalidFrameAddress(_) => todo!(),
  + }
  +}
  +
  +#[allow(dead_code)]
  +unsafe fn print_page_tables(levels: usize) {
  + assert!((1..=4).contains(&levels));
  +
  + fn print(table: &x86_64::structures::paging::PageTable, level: usize, min_level: usize) {
  + for (i, entry) in table.iter().filter(|entry| !entry.is_unused()).enumerate() {
  + let indent = &" "[0..2 * (4 - level)];
  + println!("{indent}L{level} Entry {i}: {entry:?}",);
  +
  + if level > min_level && !entry.flags().contains(PageTableEntryFlags::HUGE_PAGE) {
  + let phys = entry.frame().unwrap().start_address();
  + let virt = x86_64::VirtAddr::new(phys.as_u64());
  + let entry_table = unsafe { &*virt.as_mut_ptr() };
  +
  + print(entry_table, level - 1, min_level);
  + }
  + }
  + }
  +
  + let mut recursive_page_table = unsafe { recursive_page_table() };
  + print(recursive_page_table.level_4_table(), 4, 5 - levels);
  +}
  