diff --git a/src/arch/emulate.rs b/src/arch/emulate.rs index 331c81fe8e..40274c6633 100644 --- a/src/arch/emulate.rs +++ b/src/arch/emulate.rs @@ -126,26 +126,24 @@ impl Machine { } fn read_phys(&self, phys: PhysicalAddress) -> T { - let phys = phys.data(); let size = mem::size_of::(); - if phys + size <= self.memory.len() { + if phys.add(size).data() <= self.memory.len() { unsafe { - ptr::read(self.memory.as_ptr().add(phys) as *const T) + ptr::read(self.memory.as_ptr().add(phys.data()) as *const T) } } else { - panic!("read_phys: 0x{:X} size 0x{:X} outside of memory", phys, size); + panic!("read_phys: 0x{:X} size 0x{:X} outside of memory", phys.data(), size); } } fn write_phys(&mut self, phys: PhysicalAddress, value: T) { - let phys = phys.data(); let size = mem::size_of::(); - if phys + size <= self.memory.len() { + if phys.add(size).data() <= self.memory.len() { unsafe { - ptr::write(self.memory.as_mut_ptr().add(phys) as *mut T, value); + ptr::write(self.memory.as_mut_ptr().add(phys.data()) as *mut T, value); } } else { - panic!("write_phys: 0x{:X} size 0x{:X} outside of memory", phys, size); + panic!("write_phys: 0x{:X} size 0x{:X} outside of memory", phys.data(), size); } } @@ -155,7 +153,7 @@ impl Machine { let offset = virt_data & A::PAGE_OFFSET_MASK; let entry = self.map.get(&VirtualAddress::new(page))?; Some(( - PhysicalAddress::new(entry.address().data() + offset), + entry.address().add(offset), entry.flags(), )) } diff --git a/src/lib.rs b/src/lib.rs index 2c43d12c0e..b174223b35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,13 +16,20 @@ mod page; pub struct PhysicalAddress(usize); impl PhysicalAddress { + #[inline(always)] pub const fn new(address: usize) -> Self { Self(address) } + #[inline(always)] pub fn data(&self) -> usize { self.0 } + + #[inline(always)] + pub fn add(self, offset: usize) -> Self { + Self(self.0 + offset) + } } // Virtual memory address @@ -31,16 +38,23 @@ impl PhysicalAddress { pub struct VirtualAddress(usize); impl VirtualAddress { + #[inline(always)] pub const fn new(address: usize) -> Self { Self(address) } + #[inline(always)] pub fn data(&self) -> usize { self.0 } + + #[inline(always)] + pub fn add(self, offset: usize) -> Self { + Self(self.0 + offset) + } } pub struct MemoryArea { - base: PhysicalAddress, - size: usize, + pub base: PhysicalAddress, + pub size: usize, } diff --git a/src/page/entry.rs b/src/page/entry.rs index f935ba287d..6732da4970 100644 --- a/src/page/entry.rs +++ b/src/page/entry.rs @@ -12,22 +12,27 @@ pub struct PageEntry { } impl PageEntry { + #[inline(always)] pub fn new(data: usize) -> Self { Self { data, phantom: PhantomData } } + #[inline(always)] pub fn data(&self) -> usize { self.data } + #[inline(always)] pub fn address(&self) -> PhysicalAddress { PhysicalAddress(self.data & A::ENTRY_ADDRESS_MASK) } + #[inline(always)] pub fn flags(&self) -> usize { self.data & A::ENTRY_FLAGS_MASK } + #[inline(always)] pub fn present(&self) -> bool { self.data & A::ENTRY_FLAG_PRESENT != 0 } diff --git a/src/page/table.rs b/src/page/table.rs index 44113a9d26..15a13f34d7 100644 --- a/src/page/table.rs +++ b/src/page/table.rs @@ -55,9 +55,7 @@ impl PageTable { pub fn entry_base(&self, i: usize) -> Option { if i < A::PAGE_ENTRIES { - Some(VirtualAddress::new( - self.base.data() + (i << (self.level * A::PAGE_ENTRY_SHIFT + A::PAGE_SHIFT)) - )) + Some(self.base.add(i << (self.level * A::PAGE_ENTRY_SHIFT + A::PAGE_SHIFT))) } else { None } @@ -65,9 +63,7 @@ impl PageTable { pub unsafe fn entry_virt(&self, i: usize) -> Option { if i < A::PAGE_ENTRIES { - Some(VirtualAddress::new( - self.virt().data() + i * A::PAGE_ENTRY_SIZE - )) + Some(self.virt().add(i * A::PAGE_ENTRY_SIZE)) } else { None }