|
| 1 | +use core::fmt::{self, Write}; |
| 2 | + |
| 3 | +use crate::structures::paging::mapper::{ |
| 4 | + MappedPageRangeInclusive, MappedPageRangeInclusiveItem, MappedPageTableRangeInclusiveIter, |
| 5 | + PageTableFrameMapping, |
| 6 | +}; |
| 7 | +use crate::structures::paging::{MappedPageTable, PageSize}; |
| 8 | + |
| 9 | +impl<P: PageTableFrameMapping + Clone> MappedPageTable<'_, P> { |
| 10 | + pub fn display(&self) -> MappedPageTableDisplay<'_, &P> { |
| 11 | + MappedPageTableDisplay { |
| 12 | + inner: self.range_iter(), |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct MappedPageTableDisplay<'a, P: PageTableFrameMapping + Clone> { |
| 19 | + inner: MappedPageTableRangeInclusiveIter<'a, P>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<P: PageTableFrameMapping + Clone> fmt::Display for MappedPageTableDisplay<'_, P> { |
| 23 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 24 | + let mut has_fields = false; |
| 25 | + |
| 26 | + for mapped_page_range in self.inner.clone() { |
| 27 | + if has_fields { |
| 28 | + f.write_char('\n')?; |
| 29 | + } |
| 30 | + write!(f, "{}", mapped_page_range.display())?; |
| 31 | + |
| 32 | + has_fields = true; |
| 33 | + } |
| 34 | + |
| 35 | + Ok(()) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl MappedPageRangeInclusiveItem { |
| 40 | + pub fn display(&self) -> MappedPageRangeInclusiveItemDisplay<'_> { |
| 41 | + MappedPageRangeInclusiveItemDisplay { inner: self } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Debug)] |
| 46 | +pub struct MappedPageRangeInclusiveItemDisplay<'a> { |
| 47 | + inner: &'a MappedPageRangeInclusiveItem, |
| 48 | +} |
| 49 | + |
| 50 | +impl fmt::Display for MappedPageRangeInclusiveItemDisplay<'_> { |
| 51 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 52 | + match self.inner { |
| 53 | + MappedPageRangeInclusiveItem::Size4KiB(range) => range.display().fmt(f), |
| 54 | + MappedPageRangeInclusiveItem::Size2MiB(range) => range.display().fmt(f), |
| 55 | + MappedPageRangeInclusiveItem::Size1GiB(range) => range.display().fmt(f), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<S: PageSize> MappedPageRangeInclusive<S> { |
| 61 | + pub fn display(&self) -> MappedPageRangeInclusiveDisplay<'_, S> { |
| 62 | + MappedPageRangeInclusiveDisplay { inner: self } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Debug)] |
| 67 | +pub struct MappedPageRangeInclusiveDisplay<'a, S: PageSize> { |
| 68 | + inner: &'a MappedPageRangeInclusive<S>, |
| 69 | +} |
| 70 | + |
| 71 | +impl<S: PageSize> fmt::Display for MappedPageRangeInclusiveDisplay<'_, S> { |
| 72 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 73 | + let size = S::DEBUG_STR; |
| 74 | + let len = self.inner.page_range.len(); |
| 75 | + let page_start = self.inner.page_range.start.start_address(); |
| 76 | + let page_end = self.inner.page_range.end.start_address(); |
| 77 | + let frame_start = self.inner.frame_range.start.start_address(); |
| 78 | + let frame_end = self.inner.frame_range.end.start_address(); |
| 79 | + let flags = self.inner.flags; |
| 80 | + let format_phys = if page_start.as_u64() == frame_start.as_u64() { |
| 81 | + assert_eq!(page_end.as_u64(), frame_end.as_u64()); |
| 82 | + format_args!("{:>39}", "identity mapped") |
| 83 | + } else { |
| 84 | + format_args!("{frame_start:18p}..={frame_end:18p}") |
| 85 | + }; |
| 86 | + write!( |
| 87 | + f, |
| 88 | + "size: {size}, len: {len:5}, virt: {page_start:18p}..={page_end:18p}, phys: {format_phys}, flags: {flags:?}" |
| 89 | + ) |
| 90 | + } |
| 91 | +} |
0 commit comments