Improve some func/var naming

pull/143/head
Andre Richter 3 years ago
parent 57c6f72936
commit 431b18d17b
No known key found for this signature in database
GPG Key ID: 2116C1AB102F615E

@ -463,13 +463,13 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/_arch/aarch64/memory/mmu/trans
/// Create an instance.
- pub fn from_output_addr(phys_output_addr: usize, attribute_fields: &AttributeFields) -> Self {
+ pub fn from_output_page(
+ phys_output_page: *const Page<Physical>,
+ phys_output_page_ptr: *const Page<Physical>,
+ attribute_fields: &AttributeFields,
+ ) -> Self {
let val = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(0);
- let shifted = phys_output_addr as u64 >> Granule64KiB::SHIFT;
+ let shifted = phys_output_page as u64 >> Granule64KiB::SHIFT;
+ let shifted = phys_output_page_ptr as u64 >> Granule64KiB::SHIFT;
val.write(
STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB.val(shifted)
+ STAGE1_PAGE_DESCRIPTOR::AF::True
@ -544,9 +544,9 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/_arch/aarch64/memory/mmu/trans
+ #[inline(always)]
+ fn lvl2_lvl3_index_from_page(
+ &self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ ) -> Result<(usize, usize), &'static str> {
+ let addr = virt_page as usize;
+ let addr = virt_page_ptr as usize;
+ let lvl2_index = addr >> Granule512MiB::SHIFT;
+ let lvl3_index = (addr & Granule512MiB::MASK) >> Granule64KiB::SHIFT;
+
@ -568,10 +568,10 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/_arch/aarch64/memory/mmu/trans
+ #[inline(always)]
+ fn set_page_descriptor_from_page(
+ &mut self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ new_desc: &PageDescriptor,
+ ) -> Result<(), &'static str> {
+ let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
+ let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page_ptr)?;
+ let desc = &mut self.lvl3[lvl2_index][lvl3_index];
- for (l3_nr, l3_entry) in self.lvl3[l2_nr].iter_mut().enumerate() {
@ -637,7 +637,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/_arch/aarch64/memory/mmu/trans
+ return Err("Tried to map page slices with unequal sizes");
+ }
+
+ if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page() {
+ if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page_ptr() {
+ return Err("Tried to map outside of physical address space");
+ }
+
@ -1624,7 +1624,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/bsp/raspberrypi/memory/mmu.rs
+}
+
+/// Pointer to the last page of the physical address space.
+pub fn phys_addr_space_end_page() -> *const Page<Physical> {
+pub fn phys_addr_space_end_page_ptr() -> *const Page<Physical> {
+ common::align_down(
+ super::phys_addr_space_end().into_usize(),
+ KernelGranule::SIZE,
@ -2582,7 +2582,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/memory/mmu/types.rs 14_virtual
+ }
+
+ /// Return a pointer to the first page of the described slice.
+ const fn first_page(&self) -> *const Page<ATYPE> {
+ const fn first_page_ptr(&self) -> *const Page<ATYPE> {
+ self.start.into_usize() as *const _
+ }
+
@ -2622,7 +2622,7 @@ diff -uNr 13_exceptions_part2_peripheral_IRQs/src/memory/mmu/types.rs 14_virtual
+ ///
+ /// - Same as applies for `core::slice::from_raw_parts`.
+ pub unsafe fn as_slice(&self) -> &[Page<ATYPE>] {
+ core::slice::from_raw_parts(self.first_page(), self.num_pages)
+ core::slice::from_raw_parts(self.first_page_ptr(), self.num_pages)
+ }
+}
+

@ -230,12 +230,12 @@ impl PageDescriptor {
/// Create an instance.
pub fn from_output_page(
phys_output_page: *const Page<Physical>,
phys_output_page_ptr: *const Page<Physical>,
attribute_fields: &AttributeFields,
) -> Self {
let val = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(0);
let shifted = phys_output_page as u64 >> Granule64KiB::SHIFT;
let shifted = phys_output_page_ptr as u64 >> Granule64KiB::SHIFT;
val.write(
STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB.val(shifted)
+ STAGE1_PAGE_DESCRIPTOR::AF::True
@ -310,9 +310,9 @@ impl<const NUM_TABLES: usize> FixedSizeTranslationTable<NUM_TABLES> {
#[inline(always)]
fn lvl2_lvl3_index_from_page(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<(usize, usize), &'static str> {
let addr = virt_page as usize;
let addr = virt_page_ptr as usize;
let lvl2_index = addr >> Granule512MiB::SHIFT;
let lvl3_index = (addr & Granule512MiB::MASK) >> Granule64KiB::SHIFT;
@ -329,10 +329,10 @@ impl<const NUM_TABLES: usize> FixedSizeTranslationTable<NUM_TABLES> {
#[inline(always)]
fn set_page_descriptor_from_page(
&mut self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
new_desc: &PageDescriptor,
) -> Result<(), &'static str> {
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page_ptr)?;
let desc = &mut self.lvl3[lvl2_index][lvl3_index];
if desc.is_valid() {
@ -392,7 +392,7 @@ impl<const NUM_TABLES: usize> memory::mmu::translation_table::interface::Transla
return Err("Tried to map page slices with unequal sizes");
}
if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page() {
if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page_ptr() {
return Err("Tried to map outside of physical address space");
}

@ -101,7 +101,7 @@ pub fn kernel_translation_tables() -> &'static InitStateLock<KernelTranslationTa
}
/// Pointer to the last page of the physical address space.
pub fn phys_addr_space_end_page() -> *const Page<Physical> {
pub fn phys_addr_space_end_page_ptr() -> *const Page<Physical> {
common::align_down(
super::phys_addr_space_end().into_usize(),
KernelGranule::SIZE,

@ -92,7 +92,7 @@ impl<ATYPE: AddressType> PageSliceDescriptor<ATYPE> {
}
/// Return a pointer to the first page of the described slice.
const fn first_page(&self) -> *const Page<ATYPE> {
const fn first_page_ptr(&self) -> *const Page<ATYPE> {
self.start.into_usize() as *const _
}
@ -132,7 +132,7 @@ impl<ATYPE: AddressType> PageSliceDescriptor<ATYPE> {
///
/// - Same as applies for `core::slice::from_raw_parts`.
pub unsafe fn as_slice(&self) -> &[Page<ATYPE>] {
core::slice::from_raw_parts(self.first_page(), self.num_pages)
core::slice::from_raw_parts(self.first_page_ptr(), self.num_pages)
}
}

@ -916,13 +916,22 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/_arch/aarch64/memory/mmu/translati
impl PageDescriptor {
/// Create an instance.
///
@@ -229,7 +257,7 @@
}
/// Create an instance.
- pub fn from_output_page(
+ pub fn from_output_page_ptr(
phys_output_page_ptr: *const Page<Physical>,
attribute_fields: &AttributeFields,
) -> Self {
@@ -252,6 +280,20 @@
InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(self.value)
.is_set(STAGE1_PAGE_DESCRIPTOR::VALID)
}
+
+ /// Returns the output page.
+ fn output_page(&self) -> *const Page<Physical> {
+ fn output_page_ptr(&self) -> *const Page<Physical> {
+ let shifted = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(self.value)
+ .read(STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB);
+ let addr = shifted << Granule64KiB::SHIFT;
@ -969,17 +978,26 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/_arch/aarch64/memory/mmu/translati
/// The start address of the table's MMIO range.
#[inline(always)]
fn mmio_start_addr(&self) -> Address<Virtual> {
@@ -323,6 +374,18 @@
@@ -308,7 +359,7 @@
/// Helper to calculate the lvl2 and lvl3 indices from an address.
#[inline(always)]
- fn lvl2_lvl3_index_from_page(
+ fn lvl2_lvl3_index_from_page_ptr(
&self,
virt_page_ptr: *const Page<Virtual>,
) -> Result<(usize, usize), &'static str> {
@@ -323,16 +374,28 @@
Ok((lvl2_index, lvl3_index))
}
+ /// Returns the PageDescriptor corresponding to the supplied page address.
+ #[inline(always)]
+ fn page_descriptor_from_page(
+ fn page_descriptor_from_page_ptr(
+ &self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ ) -> Result<&PageDescriptor, &'static str> {
+ let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
+ let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page_ptr(virt_page_ptr)?;
+ let desc = &self.lvl3[lvl2_index][lvl3_index];
+
+ Ok(desc)
@ -988,6 +1006,18 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/_arch/aarch64/memory/mmu/translati
/// Sets the PageDescriptor corresponding to the supplied page address.
///
/// Doesn't allow overriding an already valid page.
#[inline(always)]
- fn set_page_descriptor_from_page(
+ fn set_page_descriptor_from_page_ptr(
&mut self,
virt_page_ptr: *const Page<Virtual>,
new_desc: &PageDescriptor,
) -> Result<(), &'static str> {
- let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page_ptr)?;
+ let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page_ptr(virt_page_ptr)?;
let desc = &mut self.lvl3[lvl2_index][lvl3_index];
if desc.is_valid() {
@@ -351,14 +414,15 @@
impl<const NUM_TABLES: usize> memory::mmu::translation_table::interface::TranslationTable
for FixedSizeTranslationTable<NUM_TABLES>
@ -1019,29 +1049,42 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/_arch/aarch64/memory/mmu/translati
}
unsafe fn map_pages_at(
@@ -398,10 +460,10 @@
let iter = p.iter().zip(v.iter());
for (phys_page, virt_page) in iter {
- let new_desc = PageDescriptor::from_output_page(phys_page.as_ptr(), attr);
+ let new_desc = PageDescriptor::from_output_page_ptr(phys_page.as_ptr(), attr);
let virt_page = virt_page.as_ptr();
- self.set_page_descriptor_from_page(virt_page, &new_desc)?;
+ self.set_page_descriptor_from_page_ptr(virt_page, &new_desc)?;
}
Ok(())
@@ -442,6 +504,44 @@
false
}
+
+ fn try_virt_page_to_phys_page(
+ fn try_virt_page_ptr_to_phys_page_ptr(
+ &self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ ) -> Result<*const Page<Physical>, &'static str> {
+ let page_desc = self.page_descriptor_from_page(virt_page)?;
+ let page_desc = self.page_descriptor_from_page_ptr(virt_page_ptr)?;
+
+ if !page_desc.is_valid() {
+ return Err("Page marked invalid");
+ }
+
+ Ok(page_desc.output_page())
+ Ok(page_desc.output_page_ptr())
+ }
+
+ fn try_page_attributes(
+ &self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ ) -> Result<AttributeFields, &'static str> {
+ let page_desc = self.page_descriptor_from_page(virt_page)?;
+ let page_desc = self.page_descriptor_from_page_ptr(virt_page_ptr)?;
+
+ if !page_desc.is_valid() {
+ return Err("Page marked invalid");
@ -1057,7 +1100,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/_arch/aarch64/memory/mmu/translati
+ &self,
+ virt_addr: Address<Virtual>,
+ ) -> Result<Address<Physical>, &'static str> {
+ let page = self.try_virt_page_to_phys_page(virt_addr.as_page_ptr())?;
+ let page = self.try_virt_page_ptr_to_phys_page_ptr(virt_addr.as_page_ptr())?;
+
+ Ok(Address::new(page as usize + virt_addr.offset_into_page()))
+ }
@ -1198,7 +1241,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/bsp/raspberrypi/memory/mmu.rs 15_v
/// Helper function for calculating the number of pages the given parameter spans.
const fn size_to_num_pages(size: usize) -> usize {
assert!(size > 0);
@@ -81,16 +101,22 @@
@@ -81,16 +101,23 @@
PageSliceDescriptor::from_addr(super::virt_boot_core_stack_start(), num_pages)
}
@ -1211,20 +1254,21 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/bsp/raspberrypi/memory/mmu.rs 15_v
) -> PageSliceDescriptor<Physical> {
- let phys_start_addr = Address::<Physical>::new(virt_slice.start_addr().into_usize());
+ let phys_first_page =
+ generic_mmu::try_kernel_virt_page_to_phys_page(virt_slice.first_page()).unwrap();
+ generic_mmu::try_kernel_virt_page_ptr_to_phys_page_ptr(virt_slice.first_page_ptr())
+ .unwrap();
+ let phys_start_addr = Address::new(phys_first_page as usize);
PageSliceDescriptor::from_addr(phys_start_addr, virt_slice.num_pages())
}
+fn kernel_page_attributes(virt_page: *const Page<Virtual>) -> AttributeFields {
+ generic_mmu::try_kernel_page_attributes(virt_page).unwrap()
+fn kernel_page_attributes(virt_page_ptr: *const Page<Virtual>) -> AttributeFields {
+ generic_mmu::try_kernel_page_attributes(virt_page_ptr).unwrap()
+}
+
//--------------------------------------------------------------------------------------------------
// Public Code
//--------------------------------------------------------------------------------------------------
@@ -108,112 +134,33 @@
@@ -108,112 +135,33 @@
) as *const Page<_>
}
@ -1253,7 +1297,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/bsp/raspberrypi/memory/mmu.rs 15_v
- )?;
+ &virt_rx_page_desc,
+ &kernel_virt_to_phys_page_slice(virt_rx_page_desc),
+ &kernel_page_attributes(virt_rx_page_desc.first_page()),
+ &kernel_page_attributes(virt_rx_page_desc.first_page_ptr()),
+ );
- generic_mmu::kernel_map_pages_at(
@ -1270,7 +1314,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/bsp/raspberrypi/memory/mmu.rs 15_v
- )?;
+ &virt_rw_page_desc,
+ &kernel_virt_to_phys_page_slice(virt_rw_page_desc),
+ &kernel_page_attributes(virt_rw_page_desc.first_page()),
+ &kernel_page_attributes(virt_rw_page_desc.first_page_ptr()),
+ );
- generic_mmu::kernel_map_pages_at(
@ -1357,7 +1401,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/bsp/raspberrypi/memory/mmu.rs 15_v
- }
+ &virt_boot_core_stack_page_desc,
+ &kernel_virt_to_phys_page_slice(virt_boot_core_stack_page_desc),
+ &kernel_page_attributes(virt_boot_core_stack_page_desc.first_page()),
+ &kernel_page_attributes(virt_boot_core_stack_page_desc.first_page_ptr()),
+ );
}
@ -1441,9 +1485,9 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/memory/mmu/translation_table.rs 15
+ /// Try to translate a virtual page pointer to a physical page pointer.
+ ///
+ /// Will only succeed if there exists a valid mapping for the input page.
+ fn try_virt_page_to_phys_page(
+ fn try_virt_page_ptr_to_phys_page_ptr(
+ &self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ ) -> Result<*const Page<Physical>, &'static str>;
+
+ /// Try to get the attributes of a page.
@ -1451,7 +1495,7 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/memory/mmu/translation_table.rs 15
+ /// Will only succeed if there exists a valid mapping for the input page.
+ fn try_page_attributes(
+ &self,
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+ ) -> Result<AttributeFields, &'static str>;
+
+ /// Try to translate a virtual address to a physical address.
@ -1493,8 +1537,8 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/memory/mmu/types.rs 15_virtual_mem
}
/// Return a pointer to the first page of the described slice.
- const fn first_page(&self) -> *const Page<ATYPE> {
+ pub const fn first_page(&self) -> *const Page<ATYPE> {
- const fn first_page_ptr(&self) -> *const Page<ATYPE> {
+ pub const fn first_page_ptr(&self) -> *const Page<ATYPE> {
self.start.into_usize() as *const _
}
@ -1549,11 +1593,11 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/memory/mmu.rs 15_virtual_mem_part3
- tables.phys_base_address()
- });
+/// Will only succeed if there exists a valid mapping for the input page.
+pub fn try_kernel_virt_page_to_phys_page(
+ virt_page: *const Page<Virtual>,
+pub fn try_kernel_virt_page_ptr_to_phys_page_ptr(
+ virt_page_ptr: *const Page<Virtual>,
+) -> Result<*const Page<Physical>, &'static str> {
+ bsp::memory::mmu::kernel_translation_tables()
+ .read(|tables| tables.try_virt_page_to_phys_page(virt_page))
+ .read(|tables| tables.try_virt_page_ptr_to_phys_page_ptr(virt_page_ptr))
+}
- bsp::memory::mmu::kernel_map_binary()?;
@ -1561,10 +1605,10 @@ diff -uNr 14_virtual_mem_part2_mmio_remap/src/memory/mmu.rs 15_virtual_mem_part3
+///
+/// Will only succeed if there exists a valid mapping for the input page.
+pub fn try_kernel_page_attributes(
+ virt_page: *const Page<Virtual>,
+ virt_page_ptr: *const Page<Virtual>,
+) -> Result<AttributeFields, &'static str> {
+ bsp::memory::mmu::kernel_translation_tables()
+ .read(|tables| tables.try_page_attributes(virt_page))
+ .read(|tables| tables.try_page_attributes(virt_page_ptr))
+}
- Ok(phys_kernel_tables_base_addr)

@ -257,13 +257,13 @@ impl PageDescriptor {
}
/// Create an instance.
pub fn from_output_page(
phys_output_page: *const Page<Physical>,
pub fn from_output_page_ptr(
phys_output_page_ptr: *const Page<Physical>,
attribute_fields: &AttributeFields,
) -> Self {
let val = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(0);
let shifted = phys_output_page as u64 >> Granule64KiB::SHIFT;
let shifted = phys_output_page_ptr as u64 >> Granule64KiB::SHIFT;
val.write(
STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB.val(shifted)
+ STAGE1_PAGE_DESCRIPTOR::AF::True
@ -282,7 +282,7 @@ impl PageDescriptor {
}
/// Returns the output page.
fn output_page(&self) -> *const Page<Physical> {
fn output_page_ptr(&self) -> *const Page<Physical> {
let shifted = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(self.value)
.read(STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB);
let addr = shifted << Granule64KiB::SHIFT;
@ -359,11 +359,11 @@ impl<const NUM_TABLES: usize> FixedSizeTranslationTable<NUM_TABLES> {
/// Helper to calculate the lvl2 and lvl3 indices from an address.
#[inline(always)]
fn lvl2_lvl3_index_from_page(
fn lvl2_lvl3_index_from_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<(usize, usize), &'static str> {
let addr = virt_page as usize;
let addr = virt_page_ptr as usize;
let lvl2_index = addr >> Granule512MiB::SHIFT;
let lvl3_index = (addr & Granule512MiB::MASK) >> Granule64KiB::SHIFT;
@ -376,11 +376,11 @@ impl<const NUM_TABLES: usize> FixedSizeTranslationTable<NUM_TABLES> {
/// Returns the PageDescriptor corresponding to the supplied page address.
#[inline(always)]
fn page_descriptor_from_page(
fn page_descriptor_from_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<&PageDescriptor, &'static str> {
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page_ptr(virt_page_ptr)?;
let desc = &self.lvl3[lvl2_index][lvl3_index];
Ok(desc)
@ -390,12 +390,12 @@ impl<const NUM_TABLES: usize> FixedSizeTranslationTable<NUM_TABLES> {
///
/// Doesn't allow overriding an already valid page.
#[inline(always)]
fn set_page_descriptor_from_page(
fn set_page_descriptor_from_page_ptr(
&mut self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
new_desc: &PageDescriptor,
) -> Result<(), &'static str> {
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page_ptr(virt_page_ptr)?;
let desc = &mut self.lvl3[lvl2_index][lvl3_index];
if desc.is_valid() {
@ -454,16 +454,16 @@ impl<const NUM_TABLES: usize> memory::mmu::translation_table::interface::Transla
return Err("Tried to map page slices with unequal sizes");
}
if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page() {
if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page_ptr() {
return Err("Tried to map outside of physical address space");
}
let iter = p.iter().zip(v.iter());
for (phys_page, virt_page) in iter {
let new_desc = PageDescriptor::from_output_page(phys_page.as_ptr(), attr);
let new_desc = PageDescriptor::from_output_page_ptr(phys_page.as_ptr(), attr);
let virt_page = virt_page.as_ptr();
self.set_page_descriptor_from_page(virt_page, &new_desc)?;
self.set_page_descriptor_from_page_ptr(virt_page, &new_desc)?;
}
Ok(())
@ -505,24 +505,24 @@ impl<const NUM_TABLES: usize> memory::mmu::translation_table::interface::Transla
false
}
fn try_virt_page_to_phys_page(
fn try_virt_page_ptr_to_phys_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<*const Page<Physical>, &'static str> {
let page_desc = self.page_descriptor_from_page(virt_page)?;
let page_desc = self.page_descriptor_from_page_ptr(virt_page_ptr)?;
if !page_desc.is_valid() {
return Err("Page marked invalid");
}
Ok(page_desc.output_page())
Ok(page_desc.output_page_ptr())
}
fn try_page_attributes(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<AttributeFields, &'static str> {
let page_desc = self.page_descriptor_from_page(virt_page)?;
let page_desc = self.page_descriptor_from_page_ptr(virt_page_ptr)?;
if !page_desc.is_valid() {
return Err("Page marked invalid");
@ -538,7 +538,7 @@ impl<const NUM_TABLES: usize> memory::mmu::translation_table::interface::Transla
&self,
virt_addr: Address<Virtual>,
) -> Result<Address<Physical>, &'static str> {
let page = self.try_virt_page_to_phys_page(virt_addr.as_page_ptr())?;
let page = self.try_virt_page_ptr_to_phys_page_ptr(virt_addr.as_page_ptr())?;
Ok(Address::new(page as usize + virt_addr.offset_into_page()))
}

@ -107,14 +107,15 @@ fn kernel_virt_to_phys_page_slice(
virt_slice: PageSliceDescriptor<Virtual>,
) -> PageSliceDescriptor<Physical> {
let phys_first_page =
generic_mmu::try_kernel_virt_page_to_phys_page(virt_slice.first_page()).unwrap();
generic_mmu::try_kernel_virt_page_ptr_to_phys_page_ptr(virt_slice.first_page_ptr())
.unwrap();
let phys_start_addr = Address::new(phys_first_page as usize);
PageSliceDescriptor::from_addr(phys_start_addr, virt_slice.num_pages())
}
fn kernel_page_attributes(virt_page: *const Page<Virtual>) -> AttributeFields {
generic_mmu::try_kernel_page_attributes(virt_page).unwrap()
fn kernel_page_attributes(virt_page_ptr: *const Page<Virtual>) -> AttributeFields {
generic_mmu::try_kernel_page_attributes(virt_page_ptr).unwrap()
}
//--------------------------------------------------------------------------------------------------
@ -127,7 +128,7 @@ pub fn kernel_translation_tables() -> &'static InitStateLock<KernelTranslationTa
}
/// Pointer to the last page of the physical address space.
pub fn phys_addr_space_end_page() -> *const Page<Physical> {
pub fn phys_addr_space_end_page_ptr() -> *const Page<Physical> {
common::align_down(
super::phys_addr_space_end().into_usize(),
KernelGranule::SIZE,
@ -145,7 +146,7 @@ pub fn kernel_add_mapping_records_for_precomputed() {
"Kernel code and RO data",
&virt_rx_page_desc,
&kernel_virt_to_phys_page_slice(virt_rx_page_desc),
&kernel_page_attributes(virt_rx_page_desc.first_page()),
&kernel_page_attributes(virt_rx_page_desc.first_page_ptr()),
);
let virt_rw_page_desc = virt_rw_page_desc();
@ -153,7 +154,7 @@ pub fn kernel_add_mapping_records_for_precomputed() {
"Kernel data and bss",
&virt_rw_page_desc,
&kernel_virt_to_phys_page_slice(virt_rw_page_desc),
&kernel_page_attributes(virt_rw_page_desc.first_page()),
&kernel_page_attributes(virt_rw_page_desc.first_page_ptr()),
);
let virt_boot_core_stack_page_desc = virt_boot_core_stack_page_desc();
@ -161,6 +162,6 @@ pub fn kernel_add_mapping_records_for_precomputed() {
"Kernel boot-core stack",
&virt_boot_core_stack_page_desc,
&kernel_virt_to_phys_page_slice(virt_boot_core_stack_page_desc),
&kernel_page_attributes(virt_boot_core_stack_page_desc.first_page()),
&kernel_page_attributes(virt_boot_core_stack_page_desc.first_page_ptr()),
);
}

@ -227,21 +227,21 @@ pub unsafe fn kernel_map_mmio(
/// Try to translate a kernel virtual page pointer to a physical page pointer.
///
/// Will only succeed if there exists a valid mapping for the input page.
pub fn try_kernel_virt_page_to_phys_page(
virt_page: *const Page<Virtual>,
pub fn try_kernel_virt_page_ptr_to_phys_page_ptr(
virt_page_ptr: *const Page<Virtual>,
) -> Result<*const Page<Physical>, &'static str> {
bsp::memory::mmu::kernel_translation_tables()
.read(|tables| tables.try_virt_page_to_phys_page(virt_page))
.read(|tables| tables.try_virt_page_ptr_to_phys_page_ptr(virt_page_ptr))
}
/// Try to get the attributes of a kernel page.
///
/// Will only succeed if there exists a valid mapping for the input page.
pub fn try_kernel_page_attributes(
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<AttributeFields, &'static str> {
bsp::memory::mmu::kernel_translation_tables()
.read(|tables| tables.try_page_attributes(virt_page))
.read(|tables| tables.try_page_attributes(virt_page_ptr))
}
/// Try to translate a kernel virtual address to a physical address.

@ -71,9 +71,9 @@ pub mod interface {
/// Try to translate a virtual page pointer to a physical page pointer.
///
/// Will only succeed if there exists a valid mapping for the input page.
fn try_virt_page_to_phys_page(
fn try_virt_page_ptr_to_phys_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<*const Page<Physical>, &'static str>;
/// Try to get the attributes of a page.
@ -81,7 +81,7 @@ pub mod interface {
/// Will only succeed if there exists a valid mapping for the input page.
fn try_page_attributes(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<AttributeFields, &'static str>;
/// Try to translate a virtual address to a physical address.

@ -92,7 +92,7 @@ impl<ATYPE: AddressType> PageSliceDescriptor<ATYPE> {
}
/// Return a pointer to the first page of the described slice.
pub const fn first_page(&self) -> *const Page<ATYPE> {
pub const fn first_page_ptr(&self) -> *const Page<ATYPE> {
self.start.into_usize() as *const _
}
@ -132,7 +132,7 @@ impl<ATYPE: AddressType> PageSliceDescriptor<ATYPE> {
///
/// - Same as applies for `core::slice::from_raw_parts`.
pub unsafe fn as_slice(&self) -> &[Page<ATYPE>] {
core::slice::from_raw_parts(self.first_page(), self.num_pages)
core::slice::from_raw_parts(self.first_page_ptr(), self.num_pages)
}
}

@ -431,10 +431,10 @@ diff -uNr 15_virtual_mem_part3_precomputed_tables/src/_arch/aarch64/memory/mmu/t
/// Helper to calculate the lvl2 and lvl3 indices from an address.
@@ -363,7 +384,12 @@
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<(usize, usize), &'static str> {
- let addr = virt_page as usize;
+ let mut addr = virt_page as usize;
- let addr = virt_page_ptr as usize;
+ let mut addr = virt_page_ptr as usize;
+
+ if START_FROM_TOP {
+ addr -= Self::START_FROM_TOP_OFFSET.into_usize()

@ -257,13 +257,13 @@ impl PageDescriptor {
}
/// Create an instance.
pub fn from_output_page(
phys_output_page: *const Page<Physical>,
pub fn from_output_page_ptr(
phys_output_page_ptr: *const Page<Physical>,
attribute_fields: &AttributeFields,
) -> Self {
let val = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(0);
let shifted = phys_output_page as u64 >> Granule64KiB::SHIFT;
let shifted = phys_output_page_ptr as u64 >> Granule64KiB::SHIFT;
val.write(
STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB.val(shifted)
+ STAGE1_PAGE_DESCRIPTOR::AF::True
@ -282,7 +282,7 @@ impl PageDescriptor {
}
/// Returns the output page.
fn output_page(&self) -> *const Page<Physical> {
fn output_page_ptr(&self) -> *const Page<Physical> {
let shifted = InMemoryRegister::<u64, STAGE1_PAGE_DESCRIPTOR::Register>::new(self.value)
.read(STAGE1_PAGE_DESCRIPTOR::OUTPUT_ADDR_64KiB);
let addr = shifted << Granule64KiB::SHIFT;
@ -380,11 +380,11 @@ impl<const NUM_TABLES: usize, const START_FROM_TOP: bool>
/// Helper to calculate the lvl2 and lvl3 indices from an address.
#[inline(always)]
fn lvl2_lvl3_index_from_page(
fn lvl2_lvl3_index_from_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<(usize, usize), &'static str> {
let mut addr = virt_page as usize;
let mut addr = virt_page_ptr as usize;
if START_FROM_TOP {
addr -= Self::START_FROM_TOP_OFFSET.into_usize()
@ -402,11 +402,11 @@ impl<const NUM_TABLES: usize, const START_FROM_TOP: bool>
/// Returns the PageDescriptor corresponding to the supplied page address.
#[inline(always)]
fn page_descriptor_from_page(
fn page_descriptor_from_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<&PageDescriptor, &'static str> {
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page_ptr(virt_page_ptr)?;
let desc = &self.lvl3[lvl2_index][lvl3_index];
Ok(desc)
@ -416,12 +416,12 @@ impl<const NUM_TABLES: usize, const START_FROM_TOP: bool>
///
/// Doesn't allow overriding an already valid page.
#[inline(always)]
fn set_page_descriptor_from_page(
fn set_page_descriptor_from_page_ptr(
&mut self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
new_desc: &PageDescriptor,
) -> Result<(), &'static str> {
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page(virt_page)?;
let (lvl2_index, lvl3_index) = self.lvl2_lvl3_index_from_page_ptr(virt_page_ptr)?;
let desc = &mut self.lvl3[lvl2_index][lvl3_index];
if desc.is_valid() {
@ -481,16 +481,16 @@ impl<const NUM_TABLES: usize, const START_FROM_TOP: bool>
return Err("Tried to map page slices with unequal sizes");
}
if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page() {
if p.last().unwrap().as_ptr() >= bsp::memory::mmu::phys_addr_space_end_page_ptr() {
return Err("Tried to map outside of physical address space");
}
let iter = p.iter().zip(v.iter());
for (phys_page, virt_page) in iter {
let new_desc = PageDescriptor::from_output_page(phys_page.as_ptr(), attr);
let new_desc = PageDescriptor::from_output_page_ptr(phys_page.as_ptr(), attr);
let virt_page = virt_page.as_ptr();
self.set_page_descriptor_from_page(virt_page, &new_desc)?;
self.set_page_descriptor_from_page_ptr(virt_page, &new_desc)?;
}
Ok(())
@ -536,24 +536,24 @@ impl<const NUM_TABLES: usize, const START_FROM_TOP: bool>
false
}
fn try_virt_page_to_phys_page(
fn try_virt_page_ptr_to_phys_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<*const Page<Physical>, &'static str> {
let page_desc = self.page_descriptor_from_page(virt_page)?;
let page_desc = self.page_descriptor_from_page_ptr(virt_page_ptr)?;
if !page_desc.is_valid() {
return Err("Page marked invalid");
}
Ok(page_desc.output_page())
Ok(page_desc.output_page_ptr())
}
fn try_page_attributes(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<AttributeFields, &'static str> {
let page_desc = self.page_descriptor_from_page(virt_page)?;
let page_desc = self.page_descriptor_from_page_ptr(virt_page_ptr)?;
if !page_desc.is_valid() {
return Err("Page marked invalid");
@ -569,7 +569,7 @@ impl<const NUM_TABLES: usize, const START_FROM_TOP: bool>
&self,
virt_addr: Address<Virtual>,
) -> Result<Address<Physical>, &'static str> {
let page = self.try_virt_page_to_phys_page(virt_addr.as_page_ptr())?;
let page = self.try_virt_page_ptr_to_phys_page_ptr(virt_addr.as_page_ptr())?;
Ok(Address::new(page as usize + virt_addr.offset_into_page()))
}

@ -107,14 +107,15 @@ fn kernel_virt_to_phys_page_slice(
virt_slice: PageSliceDescriptor<Virtual>,
) -> PageSliceDescriptor<Physical> {
let phys_first_page =
generic_mmu::try_kernel_virt_page_to_phys_page(virt_slice.first_page()).unwrap();
generic_mmu::try_kernel_virt_page_ptr_to_phys_page_ptr(virt_slice.first_page_ptr())
.unwrap();
let phys_start_addr = Address::new(phys_first_page as usize);
PageSliceDescriptor::from_addr(phys_start_addr, virt_slice.num_pages())
}
fn kernel_page_attributes(virt_page: *const Page<Virtual>) -> AttributeFields {
generic_mmu::try_kernel_page_attributes(virt_page).unwrap()
fn kernel_page_attributes(virt_page_ptr: *const Page<Virtual>) -> AttributeFields {
generic_mmu::try_kernel_page_attributes(virt_page_ptr).unwrap()
}
//--------------------------------------------------------------------------------------------------
@ -127,7 +128,7 @@ pub fn kernel_translation_tables() -> &'static InitStateLock<KernelTranslationTa
}
/// Pointer to the last page of the physical address space.
pub fn phys_addr_space_end_page() -> *const Page<Physical> {
pub fn phys_addr_space_end_page_ptr() -> *const Page<Physical> {
common::align_down(
super::phys_addr_space_end().into_usize(),
KernelGranule::SIZE,
@ -145,7 +146,7 @@ pub fn kernel_add_mapping_records_for_precomputed() {
"Kernel code and RO data",
&virt_rx_page_desc,
&kernel_virt_to_phys_page_slice(virt_rx_page_desc),
&kernel_page_attributes(virt_rx_page_desc.first_page()),
&kernel_page_attributes(virt_rx_page_desc.first_page_ptr()),
);
let virt_rw_page_desc = virt_rw_page_desc();
@ -153,7 +154,7 @@ pub fn kernel_add_mapping_records_for_precomputed() {
"Kernel data and bss",
&virt_rw_page_desc,
&kernel_virt_to_phys_page_slice(virt_rw_page_desc),
&kernel_page_attributes(virt_rw_page_desc.first_page()),
&kernel_page_attributes(virt_rw_page_desc.first_page_ptr()),
);
let virt_boot_core_stack_page_desc = virt_boot_core_stack_page_desc();
@ -161,6 +162,6 @@ pub fn kernel_add_mapping_records_for_precomputed() {
"Kernel boot-core stack",
&virt_boot_core_stack_page_desc,
&kernel_virt_to_phys_page_slice(virt_boot_core_stack_page_desc),
&kernel_page_attributes(virt_boot_core_stack_page_desc.first_page()),
&kernel_page_attributes(virt_boot_core_stack_page_desc.first_page_ptr()),
);
}

@ -232,21 +232,21 @@ pub unsafe fn kernel_map_mmio(
/// Try to translate a kernel virtual page pointer to a physical page pointer.
///
/// Will only succeed if there exists a valid mapping for the input page.
pub fn try_kernel_virt_page_to_phys_page(
virt_page: *const Page<Virtual>,
pub fn try_kernel_virt_page_ptr_to_phys_page_ptr(
virt_page_ptr: *const Page<Virtual>,
) -> Result<*const Page<Physical>, &'static str> {
bsp::memory::mmu::kernel_translation_tables()
.read(|tables| tables.try_virt_page_to_phys_page(virt_page))
.read(|tables| tables.try_virt_page_ptr_to_phys_page_ptr(virt_page_ptr))
}
/// Try to get the attributes of a kernel page.
///
/// Will only succeed if there exists a valid mapping for the input page.
pub fn try_kernel_page_attributes(
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<AttributeFields, &'static str> {
bsp::memory::mmu::kernel_translation_tables()
.read(|tables| tables.try_page_attributes(virt_page))
.read(|tables| tables.try_page_attributes(virt_page_ptr))
}
/// Try to translate a kernel virtual address to a physical address.

@ -71,9 +71,9 @@ pub mod interface {
/// Try to translate a virtual page pointer to a physical page pointer.
///
/// Will only succeed if there exists a valid mapping for the input page.
fn try_virt_page_to_phys_page(
fn try_virt_page_ptr_to_phys_page_ptr(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<*const Page<Physical>, &'static str>;
/// Try to get the attributes of a page.
@ -81,7 +81,7 @@ pub mod interface {
/// Will only succeed if there exists a valid mapping for the input page.
fn try_page_attributes(
&self,
virt_page: *const Page<Virtual>,
virt_page_ptr: *const Page<Virtual>,
) -> Result<AttributeFields, &'static str>;
/// Try to translate a virtual address to a physical address.

@ -92,7 +92,7 @@ impl<ATYPE: AddressType> PageSliceDescriptor<ATYPE> {
}
/// Return a pointer to the first page of the described slice.
pub const fn first_page(&self) -> *const Page<ATYPE> {
pub const fn first_page_ptr(&self) -> *const Page<ATYPE> {
self.start.into_usize() as *const _
}
@ -132,7 +132,7 @@ impl<ATYPE: AddressType> PageSliceDescriptor<ATYPE> {
///
/// - Same as applies for `core::slice::from_raw_parts`.
pub unsafe fn as_slice(&self) -> &[Page<ATYPE>] {
core::slice::from_raw_parts(self.first_page(), self.num_pages)
core::slice::from_raw_parts(self.first_page_ptr(), self.num_pages)
}
}

Loading…
Cancel
Save