Add function for addresses
This commit is contained in:
+7
-9
@@ -126,26 +126,24 @@ impl<A: Arch> Machine<A> {
|
||||
}
|
||||
|
||||
fn read_phys<T>(&self, phys: PhysicalAddress) -> T {
|
||||
let phys = phys.data();
|
||||
let size = mem::size_of::<T>();
|
||||
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<T>(&mut self, phys: PhysicalAddress, value: T) {
|
||||
let phys = phys.data();
|
||||
let size = mem::size_of::<T>();
|
||||
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<A: Arch> Machine<A> {
|
||||
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(),
|
||||
))
|
||||
}
|
||||
|
||||
+16
-2
@@ -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,
|
||||
}
|
||||
|
||||
@@ -12,22 +12,27 @@ pub struct PageEntry<A> {
|
||||
}
|
||||
|
||||
impl<A: Arch> PageEntry<A> {
|
||||
#[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
|
||||
}
|
||||
|
||||
+2
-6
@@ -55,9 +55,7 @@ impl<A: Arch> PageTable<A> {
|
||||
|
||||
pub fn entry_base(&self, i: usize) -> Option<VirtualAddress> {
|
||||
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<A: Arch> PageTable<A> {
|
||||
|
||||
pub unsafe fn entry_virt(&self, i: usize) -> Option<VirtualAddress> {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user