From 9b5975550d7b91f24e8f6ac407566f2416c15b27 Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Sun, 10 Sep 2023 18:15:29 +0200 Subject: [PATCH] Refactor to integer with access methods Signed-off-by: Marcus Weiner --- riscv64/src/mem.rs | 69 ++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/riscv64/src/mem.rs b/riscv64/src/mem.rs index be3ac9b..39d93f4 100644 --- a/riscv64/src/mem.rs +++ b/riscv64/src/mem.rs @@ -16,61 +16,46 @@ bitflags! { } } -#[derive(Clone, Copy, Debug)] -struct SizedInteger(u64); +/// Used as an index for PPN and VPN +pub enum PageNumberSegment { + _0, + _1, + _2, +} #[derive(Debug)] -pub struct NumberTooLarge; +pub struct PageTableEntry(u64); -impl TryFrom for SizedInteger { - type Error = NumberTooLarge; +impl PageTableEntry { + pub unsafe fn write_to(&self, addr: u64) { + unsafe { write_volatile(addr as *mut u64, self.0) } + } - fn try_from(value: u64) -> Result { - if (value.leading_zeros() as usize) < 64 - N { - return Err(NumberTooLarge); + pub fn ppn(&self, i: PageNumberSegment) -> u64 { + use PageNumberSegment::*; + match i { + _0 => self.0.get_bits(10..=18), + _1 => self.0.get_bits(19..=27), + _2 => self.0.get_bits(28..=53), } - Ok(Self(value)) } -} -impl From> for u64 { - fn from(value: SizedInteger) -> Self { - value.0 + pub fn flags(&self) -> PageTableFlags { + let bits = self.0.get_bits(0..=7) as u8; + // safe to unwrap since all bits of a u8 are defined flags + PageTableFlags::from_bits(bits).unwrap() } } -#[derive(Debug)] -pub struct PageTableEntry { - ppn2: SizedInteger<26>, - ppn1: SizedInteger<9>, - ppn0: SizedInteger<9>, - flags: PageTableFlags, -} - -impl PageTableEntry { - pub fn serialize(&self) -> u64 { - let mut out = 0u64; - out.set_bits(0..=7, self.flags.bits() as _); - out.set_bits(10..=18, self.ppn0.into()); - out.set_bits(19..=27, self.ppn1.into()); - out.set_bits(28..=53, self.ppn2.into()); - out - } - - pub unsafe fn write_to(&self, addr: u64) { - unsafe { write_volatile(addr as *mut u64, self.serialize()) } +impl From for PageTableEntry { + fn from(value: u64) -> Self { + Self(value) } } -impl From for PageTableEntry { - fn from(value: u64) -> Self { - let flags = PageTableFlags::from_bits(value.get_bits(0..=7) as _).unwrap(); - Self { - ppn2: value.get_bits(28..=53).try_into().unwrap(), - ppn1: value.get_bits(19..=27).try_into().unwrap(), - ppn0: value.get_bits(10..=18).try_into().unwrap(), - flags, - } +impl From for u64 { + fn from(value: PageTableEntry) -> Self { + value.0 } }