From 2d7b6cc55e0ed954542c8dee794e9e4c79ba7fab Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Sun, 29 Nov 2026 13:15:40 +0530 Subject: [PATCH] Add page table mapping tests for aarch64 The amd64 page table code had unit tests for map(), virt_to_phys() and modify_mapping(), but the aarch64 implementation had none. Add a mock TableOps and mirror those tests, plus coverage for COW and zero-init mappings. Signed-off-by: Anurag Singh --- .../src/arch/aarch64/vmem.rs | 415 ++++++++++++++++++ 1 file changed, 415 insertions(+) diff --git a/src/hyperlight_common/src/arch/aarch64/vmem.rs b/src/hyperlight_common/src/arch/aarch64/vmem.rs index 15ce844dc..7863eca2e 100644 --- a/src/hyperlight_common/src/arch/aarch64/vmem.rs +++ b/src/hyperlight_common/src/arch/aarch64/vmem.rs @@ -611,3 +611,418 @@ pub unsafe fn space_aware_map( // in reaching this right now. todo: implement this properly debug_assert!(false, "space_aware_map is not yet supported on aarch64"); } + +#[cfg(test)] +mod tests { + use alloc::vec; + use alloc::vec::Vec; + use core::cell::RefCell; + + use super::*; + use crate::vmem::{ + BasicMapping, MapRequest, Mapping, MappingKind, MayNotMoveTable, + PAGE_TABLE_ENTRIES_PER_TABLE, TableOps, TableReadOps, UpdateParentNone, Void, bits, + modify_ptes, + }; + + const AP_READ_ONLY: u64 = 1 << 7; + const XN_EL0_BIT: u64 = 1 << 53; + + /// A mock TableOps implementation that stores page tables in + /// memory, so the mapping code can be tested without hardware + /// page tables. + struct MockTableOps { + tables: RefCell>, + } + + impl core::convert::AsRef for MockTableOps { + fn as_ref(&self) -> &Self { + self + } + } + + impl MockTableOps { + fn new() -> Self { + Self { + tables: RefCell::new(vec![[0u64; PAGE_TABLE_ENTRIES_PER_TABLE]]), + } + } + + fn table_count(&self) -> usize { + self.tables.borrow().len() + } + + fn get_entry(&self, table_idx: usize, entry_idx: usize) -> u64 { + self.tables.borrow()[table_idx][entry_idx] + } + + fn leaf_entry(&self, virt_base: u64) -> u64 { + let l3_index = bits::<20, 12>(virt_base) as usize; + self.get_entry(3, l3_index) + } + } + + impl TableReadOps for MockTableOps { + type TableAddr = (usize, usize); + + fn entry_addr(addr: Self::TableAddr, entry_offset: u64) -> Self::TableAddr { + let phys = Self::to_phys(addr) + entry_offset; + Self::from_phys(phys) + } + + unsafe fn read_entry(&self, addr: Self::TableAddr) -> u64 { + self.tables.borrow()[addr.0][addr.1] + } + + fn to_phys(addr: Self::TableAddr) -> PhysAddr { + (addr.0 as u64 * PAGE_TABLE_SIZE as u64) + (addr.1 as u64 * 8) + } + + fn from_phys(addr: PhysAddr) -> Self::TableAddr { + let table_idx = (addr / PAGE_TABLE_SIZE as u64) as usize; + let entry_idx = ((addr % PAGE_TABLE_SIZE as u64) / 8) as usize; + (table_idx, entry_idx) + } + + fn root_table(&self) -> Self::TableAddr { + (0, 0) + } + } + + impl TableOps for MockTableOps { + type TableMovability = MayNotMoveTable; + + unsafe fn alloc_table(&self) -> Self::TableAddr { + let mut tables = self.tables.borrow_mut(); + let idx = tables.len(); + tables.push([0u64; PAGE_TABLE_ENTRIES_PER_TABLE]); + (idx, 0) + } + + unsafe fn write_entry(&self, addr: Self::TableAddr, entry: u64) -> Option { + self.tables.borrow_mut()[addr.0][addr.1] = entry; + None + } + + unsafe fn update_root(&self, impossible: Void) { + match impossible {} + } + } + + fn basic_mapping(phys_base: u64, virt_base: u64, len: u64) -> Mapping { + Mapping { + phys_base, + virt_base, + len, + kind: MappingKind::Basic(BasicMapping { + readable: true, + writable: true, + executable: false, + }), + } + } + + #[test] + fn test_map_single_page() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + + assert_eq!(ops.table_count(), 4); + + let root_entry = ops.get_entry(0, 0); + assert_ne!(root_entry & PAGE_PRESENT, 0); + assert_eq!(root_entry & 0b11, 0b11, "root entry must be a table"); + + let pte = ops.leaf_entry(0x1000); + assert_ne!(pte & PAGE_PRESENT, 0); + assert_eq!(pte & PTE_ADDR_MASK, 0x1000); + assert_eq!(pte & AP_READ_ONLY, 0, "page should be writable"); + assert_ne!(pte & XN_EL0_BIT, 0, "page should not be executable"); + assert_ne!(pte & (1 << 10), 0, "AF should be set"); + } + + #[test] + fn test_map_executable_read_only_page() { + let ops = MockTableOps::new(); + let mapping = Mapping { + phys_base: 0x2000, + virt_base: 0x2000, + len: PAGE_SIZE as u64, + kind: MappingKind::Basic(BasicMapping { + readable: true, + writable: false, + executable: true, + }), + }; + unsafe { map(&ops, mapping) }; + + let pte = ops.leaf_entry(0x2000); + assert_eq!(pte & PTE_ADDR_MASK, 0x2000); + assert_ne!(pte & AP_READ_ONLY, 0, "page should be read-only"); + assert_eq!(pte & XN_EL0_BIT, 0, "page should be executable"); + } + + #[test] + fn test_map_multiple_pages() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x10000, 0x10000, 4 * PAGE_SIZE as u64)) }; + + for i in 0..4 { + let virt = 0x10000 + i * PAGE_SIZE as u64; + let pte = ops.leaf_entry(virt); + assert_ne!(pte & PAGE_PRESENT, 0, "page {i} should be present"); + assert_eq!(pte & PTE_ADDR_MASK, virt, "page {i} phys address"); + } + } + + #[test] + fn test_map_reuses_existing_tables() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + let tables_after_first = ops.table_count(); + + unsafe { map(&ops, basic_mapping(0x5000, 0x5000, PAGE_SIZE as u64)) }; + + assert_eq!(ops.table_count(), tables_after_first); + } + + #[test] + fn test_map_cow_page() { + let ops = MockTableOps::new(); + let mapping = Mapping { + phys_base: 0x3000, + virt_base: 0x3000, + len: PAGE_SIZE as u64, + kind: MappingKind::Cow(CowMapping { + readable: true, + executable: false, + }), + }; + unsafe { map(&ops, mapping) }; + + let pte = ops.leaf_entry(0x3000); + assert_eq!(bits::<58, 55>(pte), SOFTWARE_USE_COW as u64); + assert_ne!(pte & AP_READ_ONLY, 0, "COW pages are read-only"); + } + + #[test] + fn test_map_zero_init_page() { + let ops = MockTableOps::new(); + let kind = MappingKind::ZeroInit(BasicMapping { + readable: true, + writable: true, + executable: true, + }); + unsafe { + map( + &ops, + Mapping { + phys_base: 0, + virt_base: 0x4000, + len: PAGE_SIZE as u64, + kind, + }, + ) + }; + + let pte = ops.leaf_entry(0x4000); + assert_eq!(pte & PAGE_PRESENT, 0, "zero-init pages are not present"); + assert_eq!( + bits::(pte), + INVALID_KIND_ZERO_INIT as u64 + ); + + let mappings = unsafe { virt_to_phys(&ops, 0x4000, 1).collect::>() }; + assert_eq!(mappings.len(), 1); + assert_eq!(mappings[0].kind, kind); + } + + #[test] + fn test_virt_to_phys_mapped_address() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + + let mapping = unsafe { virt_to_phys(&ops, 0x1000, 1).next() }.unwrap(); + assert_eq!(mapping.phys_base, 0x1000); + assert_eq!(mapping.virt_base, 0x1000); + assert_eq!(mapping.len, PAGE_SIZE as u64); + } + + #[test] + fn test_virt_to_phys_unaligned_address() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + + let mapping = unsafe { virt_to_phys(&ops, 0x1234, 1).next() }.unwrap(); + assert_eq!(mapping.phys_base, 0x1000); + } + + #[test] + fn test_virt_to_phys_spanning_pages() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, 3 * PAGE_SIZE as u64)) }; + + let mappings = unsafe { virt_to_phys(&ops, 0x1F00, 0x300).collect::>() }; + assert_eq!(mappings.len(), 2); + assert_eq!(mappings[0].phys_base, 0x1000); + assert_eq!(mappings[1].phys_base, 0x2000); + + let mappings = + unsafe { virt_to_phys(&ops, 0x1234, 2 * PAGE_SIZE as u64 + 0x10).collect::>() }; + assert_eq!(mappings.len(), 3); + assert_eq!(mappings[2].phys_base, 0x3000); + } + + #[test] + fn test_virt_to_phys_perms() { + for kind in [ + MappingKind::Basic(BasicMapping { + readable: true, + writable: false, + executable: false, + }), + MappingKind::Basic(BasicMapping { + readable: true, + writable: false, + executable: true, + }), + MappingKind::Basic(BasicMapping { + readable: true, + writable: true, + executable: false, + }), + MappingKind::Basic(BasicMapping { + readable: true, + writable: true, + executable: true, + }), + MappingKind::Cow(CowMapping { + readable: true, + executable: false, + }), + MappingKind::Cow(CowMapping { + readable: true, + executable: true, + }), + ] { + let ops = MockTableOps::new(); + let mapping = Mapping { + phys_base: 0x1000, + virt_base: 0x1000, + len: PAGE_SIZE as u64, + kind, + }; + unsafe { map(&ops, mapping) }; + let read_back = unsafe { virt_to_phys(&ops, 0x1000, 1).next() }.unwrap(); + assert_eq!(read_back.kind, kind); + } + } + + #[test] + fn test_virt_to_phys_unmapped_address() { + let ops = MockTableOps::new(); + assert!(unsafe { virt_to_phys(&ops, 0x1000, 1).next() }.is_none()); + } + + #[test] + fn test_virt_to_phys_partially_mapped() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + + let mappings = + unsafe { virt_to_phys(&ops, 0x1000, 2 * PAGE_SIZE as u64).collect::>() }; + assert_eq!(mappings.len(), 1); + } + + #[test] + fn test_modify_mapping_downgrades_page() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + assert_eq!(ops.leaf_entry(0x1000) & AP_READ_ONLY, 0); + + unsafe { + modify_mapping(&ops, 0x1000, PAGE_SIZE as u64, |_, mapping| { + match mapping.kind { + MappingKind::Basic(_) => MappingKind::Basic(BasicMapping { + readable: true, + writable: false, + executable: false, + }), + other => other, + } + }) + }; + + let pte = ops.leaf_entry(0x1000); + assert_ne!(pte & AP_READ_ONLY, 0, "page should now be read-only"); + assert_eq!(pte & PTE_ADDR_MASK, 0x1000, "phys address should be kept"); + } + + #[test] + fn test_modify_mapping_unmaps_page() { + let ops = MockTableOps::new(); + unsafe { map(&ops, basic_mapping(0x1000, 0x1000, PAGE_SIZE as u64)) }; + + unsafe { modify_mapping(&ops, 0x1000, PAGE_SIZE as u64, |_, _| MappingKind::Unmapped) }; + + assert_eq!(ops.leaf_entry(0x1000), 0); + assert!(unsafe { virt_to_phys(&ops, 0x1000, 1).next() }.is_none()); + } + + #[test] + fn test_modify_ptes_single_page() { + let ops = MockTableOps::new(); + let request = MapRequest { + table_base: ops.root_table(), + vmin: 0x1000, + len: PAGE_SIZE as u64, + update_parent: UpdateParentNone {}, + }; + + let responses: Vec<_> = modify_ptes::<20, 12, MockTableOps, _>(request).collect(); + assert_eq!(responses.len(), 1); + assert_eq!(responses[0].vmin, 0x1000); + assert_eq!(responses[0].len, PAGE_SIZE as u64); + } + + #[test] + fn test_modify_ptes_unaligned_start() { + let ops = MockTableOps::new(); + let request = MapRequest { + table_base: ops.root_table(), + vmin: 0x1800, + len: 0x1000, + update_parent: UpdateParentNone {}, + }; + + let responses: Vec<_> = modify_ptes::<20, 12, MockTableOps, _>(request).collect(); + assert_eq!(responses.len(), 2); + assert_eq!(responses[0].vmin, 0x1800); + assert_eq!(responses[0].len, 0x800); + assert_eq!(responses[1].vmin, 0x2000); + } + + #[test] + fn test_modify_ptes_zero_length() { + let ops = MockTableOps::new(); + let request = MapRequest { + table_base: ops.root_table(), + vmin: 0x1000, + len: 0, + update_parent: UpdateParentNone {}, + }; + + assert_eq!(modify_ptes::<20, 12, MockTableOps, _>(request).count(), 0); + } + + #[test] + fn test_entry_addr_from_table_base() { + assert_eq!(MockTableOps::entry_addr((2, 0), 40), (2, 5)); + } + + #[test] + fn test_to_phys_from_phys_roundtrip() { + let addr = (3, 42); + assert_eq!(MockTableOps::from_phys(MockTableOps::to_phys(addr)), addr); + } +}