Imre Kis | 87cee5b | 2025-01-15 18:52:35 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: Copyright 2023-2025 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 2 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 3 | |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 4 | #![allow(dead_code)] |
| 5 | #![allow(non_camel_case_types)] |
| 6 | #![cfg_attr(not(test), no_std)] |
| 7 | |
| 8 | extern crate alloc; |
| 9 | |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 10 | use core::iter::zip; |
| 11 | use core::{fmt, panic}; |
| 12 | |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 13 | use address::{PhysicalAddress, VirtualAddress, VirtualAddressRange}; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 14 | use alloc::boxed::Box; |
| 15 | use alloc::format; |
| 16 | use alloc::string::{String, ToString}; |
| 17 | use alloc::vec::Vec; |
| 18 | use log::debug; |
| 19 | |
| 20 | use bitflags::bitflags; |
| 21 | use packed_struct::prelude::*; |
| 22 | |
| 23 | use self::descriptor::DescriptorType; |
| 24 | |
| 25 | use self::descriptor::{Attributes, DataAccessPermissions, Descriptor, Shareability}; |
| 26 | use self::kernel_space::KernelSpace; |
| 27 | use self::page_pool::{Page, PagePool, Pages}; |
| 28 | use self::region::{PhysicalRegion, VirtualRegion}; |
| 29 | use self::region_pool::{Region, RegionPool, RegionPoolError}; |
| 30 | |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 31 | pub mod address; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 32 | mod descriptor; |
| 33 | pub mod kernel_space; |
| 34 | pub mod page_pool; |
| 35 | mod region; |
| 36 | mod region_pool; |
| 37 | |
| 38 | /// The first level of memory descriptors table which |
| 39 | #[repr(C, align(512))] |
| 40 | pub struct BaseTable { |
| 41 | pub descriptors: [Descriptor; 64], |
| 42 | } |
| 43 | |
| 44 | impl BaseTable { |
| 45 | pub fn new() -> Self { |
| 46 | BaseTable { |
Imre Kis | f5f6fa7 | 2024-04-18 14:04:21 +0200 | [diff] [blame] | 47 | descriptors: core::array::from_fn(|_| Descriptor::default()), |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// Translation table error type |
| 53 | #[derive(Debug)] |
| 54 | pub enum XlatError { |
| 55 | InvalidParameterError(String), |
| 56 | AllocationError(String), |
| 57 | AlignmentError(String), |
| 58 | Overflow, |
| 59 | InvalidOperation(String), |
| 60 | Overlap, |
| 61 | NotFound, |
| 62 | RegionPoolError(RegionPoolError), |
| 63 | } |
| 64 | |
| 65 | /// Memory attributes |
| 66 | /// |
| 67 | /// MAIR_EL1 should be configured in the same way in startup.s |
| 68 | #[derive(PrimitiveEnum_u8, Clone, Copy, Debug, PartialEq, Eq, Default)] |
| 69 | pub enum MemoryAttributesIndex { |
| 70 | #[default] |
| 71 | Device_nGnRnE = 0x00, |
| 72 | Normal_IWBWA_OWBWA = 0x01, |
| 73 | } |
| 74 | |
| 75 | bitflags! { |
| 76 | #[derive(Debug, Clone, Copy)] |
| 77 | pub struct MemoryAccessRights : u32 { |
| 78 | const R = 0b00000001; |
| 79 | const W = 0b00000010; |
| 80 | const X = 0b00000100; |
| 81 | const NS = 0b00001000; |
| 82 | |
| 83 | const RW = Self::R.bits() | Self::W.bits(); |
| 84 | const RX = Self::R.bits() | Self::X.bits(); |
| 85 | const RWX = Self::R.bits() | Self::W.bits() | Self::X.bits(); |
| 86 | |
| 87 | const USER = 0b00010000; |
| 88 | const DEVICE = 0b00100000; |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 89 | const GLOBAL = 0b01000000; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | impl From<MemoryAccessRights> for Attributes { |
| 94 | fn from(access_rights: MemoryAccessRights) -> Self { |
| 95 | let data_access_permissions = match ( |
| 96 | access_rights.contains(MemoryAccessRights::USER), |
| 97 | access_rights.contains(MemoryAccessRights::W), |
| 98 | ) { |
| 99 | (false, false) => DataAccessPermissions::ReadOnly_None, |
| 100 | (false, true) => DataAccessPermissions::ReadWrite_None, |
| 101 | (true, false) => DataAccessPermissions::ReadOnly_ReadOnly, |
| 102 | (true, true) => DataAccessPermissions::ReadWrite_ReadWrite, |
| 103 | }; |
| 104 | |
| 105 | let mem_attr_index = if access_rights.contains(MemoryAccessRights::DEVICE) { |
| 106 | MemoryAttributesIndex::Device_nGnRnE |
| 107 | } else { |
| 108 | MemoryAttributesIndex::Normal_IWBWA_OWBWA |
| 109 | }; |
| 110 | |
| 111 | Attributes { |
| 112 | uxn: !access_rights.contains(MemoryAccessRights::X) |
| 113 | || !access_rights.contains(MemoryAccessRights::USER), |
| 114 | pxn: !access_rights.contains(MemoryAccessRights::X) |
| 115 | || access_rights.contains(MemoryAccessRights::USER), |
| 116 | contiguous: false, |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 117 | not_global: !access_rights.contains(MemoryAccessRights::GLOBAL), |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 118 | access_flag: true, |
| 119 | shareability: Shareability::NonShareable, |
| 120 | data_access_permissions, |
| 121 | non_secure: access_rights.contains(MemoryAccessRights::NS), |
| 122 | mem_attr_index, |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #[derive(PartialEq)] |
| 128 | struct Block { |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 129 | pa: PhysicalAddress, |
| 130 | va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 131 | granule: usize, |
| 132 | } |
| 133 | |
| 134 | impl Block { |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 135 | fn new(pa: PhysicalAddress, va: VirtualAddress, granule: usize) -> Self { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 136 | assert!(Xlat::GRANULE_SIZES.contains(&granule)); |
| 137 | Self { pa, va, granule } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | impl fmt::Debug for Block { |
| 142 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 143 | f.debug_struct("Block") |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 144 | .field("pa", &format_args!("{:#010x}", self.pa.0)) |
| 145 | .field("va", &format_args!("{:#010x}", self.va.0)) |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 146 | .field("granule", &format_args!("{:#010x}", self.granule)) |
| 147 | .finish() |
| 148 | } |
| 149 | } |
| 150 | |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 151 | pub enum RegimeVaRange { |
| 152 | Lower, |
| 153 | Upper, |
| 154 | } |
| 155 | |
| 156 | pub enum TranslationRegime { |
| 157 | EL1_0(RegimeVaRange, u8), // EL1 and EL0 stage 1, TTBRx_EL1 |
| 158 | #[cfg(target_feature = "vh")] |
| 159 | EL2_0(RegimeVaRange, u8), // EL2 and EL0 with VHE |
| 160 | EL2, // EL2 |
| 161 | EL3, // EL3, TTBR0_EL3 |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 164 | pub struct Xlat { |
| 165 | base_table: Box<BaseTable>, |
| 166 | page_pool: PagePool, |
| 167 | regions: RegionPool<VirtualRegion>, |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 168 | regime: TranslationRegime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /// Memory translation table handling |
| 172 | /// # High level interface |
| 173 | /// * allocate and map zero initialized region (with or without VA) |
| 174 | /// * allocate and map memory region and load contents (with or without VA) |
| 175 | /// * map memory region by PA (with or without VA) |
| 176 | /// * unmap memory region by PA |
| 177 | /// * query PA by VA |
| 178 | /// * set access rights of mapped memory areas |
| 179 | /// * active mapping |
| 180 | /// |
| 181 | /// # Debug features |
| 182 | /// * print translation table details |
| 183 | /// |
| 184 | /// # Region level interface |
| 185 | /// * map regions |
| 186 | /// * unmap region |
| 187 | /// * find a mapped region which contains |
| 188 | /// * find empty area for region |
| 189 | /// * set access rights for a region |
| 190 | /// * create blocks by region |
| 191 | /// |
| 192 | /// # Block level interface |
| 193 | /// * map block |
| 194 | /// * unmap block |
| 195 | /// * set access rights of block |
| 196 | impl Xlat { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 197 | pub const GRANULE_SIZES: [usize; 4] = [0, 0x4000_0000, 0x0020_0000, 0x0000_1000]; |
| 198 | |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 199 | pub fn new( |
| 200 | page_pool: PagePool, |
| 201 | address: VirtualAddressRange, |
| 202 | regime: TranslationRegime, |
| 203 | ) -> Self { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 204 | let mut regions = RegionPool::new(); |
| 205 | regions |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 206 | .add(VirtualRegion::new(address.start, address.len().unwrap())) |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 207 | .unwrap(); |
| 208 | Self { |
| 209 | base_table: Box::new(BaseTable::new()), |
| 210 | page_pool, |
| 211 | regions, |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 212 | regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | /// Allocate memory pages from the page pool, maps it to the given VA and fills it with the |
| 217 | /// initial data |
| 218 | /// # Arguments |
| 219 | /// * va: Virtual address of the memory area |
| 220 | /// * data: Data to be loaded to the memory area |
| 221 | /// * access_rights: Memory access rights of the area |
| 222 | /// # Return value |
| 223 | /// * Virtual address of the mapped memory |
| 224 | pub fn allocate_initalized_range( |
| 225 | &mut self, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 226 | va: Option<VirtualAddress>, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 227 | data: &[u8], |
| 228 | access_rights: MemoryAccessRights, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 229 | ) -> Result<VirtualAddress, XlatError> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 230 | let mut pages = self.page_pool.allocate_pages(data.len()).map_err(|e| { |
| 231 | XlatError::AllocationError(format!( |
| 232 | "Cannot allocate pages for {} bytes ({:?})", |
| 233 | data.len(), |
| 234 | e |
| 235 | )) |
| 236 | })?; |
| 237 | |
| 238 | pages.copy_data_to_page(data); |
| 239 | |
| 240 | let pages_length = pages.length(); |
| 241 | let physical_region = PhysicalRegion::Allocated(self.page_pool.clone(), pages); |
| 242 | let region = if let Some(required_va) = va { |
| 243 | self.regions |
| 244 | .acquire(required_va, pages_length, physical_region) |
| 245 | } else { |
| 246 | self.regions.allocate(pages_length, physical_region) |
| 247 | } |
| 248 | .map_err(XlatError::RegionPoolError)?; |
| 249 | |
| 250 | self.map_region(region, access_rights.into()) |
| 251 | } |
| 252 | |
| 253 | /// Allocate memory pages from the page pool, maps it to the given VA and fills it with zeros |
| 254 | /// # Arguments |
| 255 | /// * va: Virtual address of the memory area |
| 256 | /// * length: Length of the memory area in bytes |
| 257 | /// * access_rights: Memory access rights of the area |
| 258 | /// # Return value |
| 259 | /// * Virtual address of the mapped memory |
| 260 | pub fn allocate_zero_init_range( |
| 261 | &mut self, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 262 | va: Option<VirtualAddress>, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 263 | length: usize, |
| 264 | access_rights: MemoryAccessRights, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 265 | ) -> Result<VirtualAddress, XlatError> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 266 | let mut pages = self.page_pool.allocate_pages(length).map_err(|e| { |
| 267 | XlatError::AllocationError(format!("Cannot allocate pages for {length} bytes ({e:?})")) |
| 268 | })?; |
| 269 | |
| 270 | pages.zero_init(); |
| 271 | |
| 272 | let pages_length = pages.length(); |
| 273 | let physical_region = PhysicalRegion::Allocated(self.page_pool.clone(), pages); |
| 274 | let region = if let Some(required_va) = va { |
| 275 | self.regions |
| 276 | .acquire(required_va, pages_length, physical_region) |
| 277 | } else { |
| 278 | self.regions.allocate(pages_length, physical_region) |
| 279 | } |
| 280 | .map_err(XlatError::RegionPoolError)?; |
| 281 | |
| 282 | self.map_region(region, access_rights.into()) |
| 283 | } |
| 284 | |
| 285 | /// Map memory area by physical address |
| 286 | /// # Arguments |
| 287 | /// * va: Virtual address of the memory area |
| 288 | /// * pa: Physical address of the memory area |
| 289 | /// * length: Length of the memory area in bytes |
| 290 | /// * access_rights: Memory access rights of the area |
| 291 | /// # Return value |
| 292 | /// * Virtual address of the mapped memory |
| 293 | pub fn map_physical_address_range( |
| 294 | &mut self, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 295 | va: Option<VirtualAddress>, |
| 296 | pa: PhysicalAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 297 | length: usize, |
| 298 | access_rights: MemoryAccessRights, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 299 | ) -> Result<VirtualAddress, XlatError> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 300 | let resource = PhysicalRegion::PhysicalAddress(pa); |
| 301 | let region = if let Some(required_va) = va { |
| 302 | self.regions.acquire(required_va, length, resource) |
| 303 | } else { |
| 304 | self.regions.allocate(length, resource) |
| 305 | } |
| 306 | .map_err(XlatError::RegionPoolError)?; |
| 307 | |
| 308 | self.map_region(region, access_rights.into()) |
| 309 | } |
| 310 | |
| 311 | /// Unmap memory area by virtual address |
| 312 | /// # Arguments |
| 313 | /// * va: Virtual address |
| 314 | /// * length: Length of the memory area in bytes |
| 315 | pub fn unmap_virtual_address_range( |
| 316 | &mut self, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 317 | va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 318 | length: usize, |
| 319 | ) -> Result<(), XlatError> { |
| 320 | let pa = self.get_pa_by_va(va, length)?; |
| 321 | |
| 322 | let region_to_release = VirtualRegion::new_with_pa(pa, va, length); |
| 323 | |
| 324 | self.unmap_region(®ion_to_release)?; |
| 325 | |
| 326 | self.regions |
| 327 | .release(region_to_release) |
| 328 | .map_err(XlatError::RegionPoolError) |
| 329 | } |
| 330 | |
| 331 | /// Query physical address by virtual address range. Only returns a value if the memory area |
| 332 | /// mapped as continuous area. |
| 333 | /// # Arguments |
| 334 | /// * va: Virtual address of the memory area |
| 335 | /// * length: Length of the memory area in bytes |
| 336 | /// # Return value |
| 337 | /// * Physical address of the mapped memory |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 338 | pub fn get_pa_by_va( |
| 339 | &self, |
| 340 | va: VirtualAddress, |
| 341 | length: usize, |
| 342 | ) -> Result<PhysicalAddress, XlatError> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 343 | let containing_region = self |
| 344 | .find_containing_region(va, length) |
| 345 | .ok_or(XlatError::NotFound)?; |
| 346 | |
| 347 | if !containing_region.used() { |
| 348 | return Err(XlatError::NotFound); |
| 349 | } |
| 350 | |
| 351 | Ok(containing_region.get_pa_for_va(va)) |
| 352 | } |
| 353 | |
| 354 | /// Sets the memory access right of memory area |
| 355 | /// # Arguments |
| 356 | /// * va: Virtual address of the memory area |
| 357 | /// * length: Length of the memory area in bytes |
| 358 | /// * access_rights: New memory access rights of the area |
| 359 | pub fn set_access_rights( |
| 360 | &mut self, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 361 | va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 362 | length: usize, |
| 363 | access_rights: MemoryAccessRights, |
| 364 | ) -> Result<(), XlatError> { |
| 365 | let containing_region = self |
| 366 | .find_containing_region(va, length) |
| 367 | .ok_or(XlatError::NotFound)?; |
| 368 | |
| 369 | if !containing_region.used() { |
| 370 | return Err(XlatError::NotFound); |
| 371 | } |
| 372 | |
| 373 | let region = VirtualRegion::new_with_pa(containing_region.get_pa_for_va(va), va, length); |
| 374 | self.map_region(region, access_rights.into())?; |
| 375 | |
| 376 | Ok(()) |
| 377 | } |
| 378 | |
| 379 | /// Activate memory mapping represented by the object |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 380 | /// |
| 381 | /// # Safety |
| 382 | /// When activating memory mapping for the running exception level, the |
| 383 | /// caller must ensure that the new mapping will not break any existing |
| 384 | /// references. After activation the caller must ensure that there are no |
| 385 | /// active references when unmapping memory. |
| 386 | pub unsafe fn activate(&self) { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 387 | let base_table_pa = KernelSpace::kernel_to_pa(self.base_table.descriptors.as_ptr() as u64); |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 388 | |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 389 | #[cfg(target_arch = "aarch64")] |
| 390 | match &self.regime { |
| 391 | TranslationRegime::EL1_0(RegimeVaRange::Lower, asid) => core::arch::asm!( |
| 392 | "msr ttbr0_el1, {0} |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 393 | isb", |
Imre Kis | b5146b5 | 2024-10-31 14:03:06 +0100 | [diff] [blame] | 394 | in(reg) ((*asid as u64) << 48) | base_table_pa), |
| 395 | TranslationRegime::EL1_0(RegimeVaRange::Upper, asid) => core::arch::asm!( |
| 396 | "msr ttbr1_el1, {0} |
| 397 | isb", |
| 398 | in(reg) ((*asid as u64) << 48) | base_table_pa), |
| 399 | #[cfg(target_feature = "vh")] |
| 400 | TranslationRegime::EL2_0(RegimeVaRange::Lower, asid) => core::arch::asm!( |
| 401 | "msr ttbr0_el2, {0} |
| 402 | isb", |
| 403 | in(reg) ((*asid as u64) << 48) | base_table_pa), |
| 404 | #[cfg(target_feature = "vh")] |
| 405 | TranslationRegime::EL2_0(RegimeVaRange::Upper, asid) => core::arch::asm!( |
| 406 | "msr ttbr1_el2, {0} |
| 407 | isb", |
| 408 | in(reg) ((*asid as u64) << 48) | base_table_pa), |
| 409 | TranslationRegime::EL2 => core::arch::asm!( |
| 410 | "msr ttbr0_el2, {0} |
| 411 | isb", |
| 412 | in(reg) base_table_pa), |
| 413 | TranslationRegime::EL3 => core::arch::asm!( |
| 414 | "msr ttbr0_el3, {0} |
| 415 | isb", |
| 416 | in(reg) base_table_pa), |
Imre Kis | c1dab89 | 2024-03-26 12:03:58 +0100 | [diff] [blame] | 417 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /// Prints the translation tables to debug console recursively |
| 421 | pub fn print(&self) { |
| 422 | debug!( |
| 423 | "Xlat table -> {:#010x}", |
| 424 | self.base_table.descriptors.as_ptr() as u64 |
| 425 | ); |
| 426 | Self::print_table(1, 0, &self.base_table.descriptors); |
| 427 | } |
| 428 | |
| 429 | /// Prints a single translation table to the debug console |
| 430 | /// # Arguments |
| 431 | /// * level: Level of the translation table |
| 432 | /// * va: Base virtual address of the table |
| 433 | /// * table: Table entries |
| 434 | pub fn print_table(level: usize, va: usize, table: &[Descriptor]) { |
| 435 | let level_prefix = match level { |
| 436 | 0 | 1 => "|-", |
| 437 | 2 => "| |-", |
| 438 | _ => "| | |-", |
| 439 | }; |
| 440 | |
| 441 | for (descriptor, va) in zip(table, (va..).step_by(Self::GRANULE_SIZES[level])) { |
| 442 | match descriptor.get_descriptor_type(level) { |
| 443 | DescriptorType::Block => debug!( |
| 444 | "{} {:#010x} Block -> {:#010x}", |
| 445 | level_prefix, |
| 446 | va, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 447 | descriptor.get_block_output_address(level).0 |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 448 | ), |
| 449 | DescriptorType::Table => { |
| 450 | let next_level_table = unsafe { descriptor.get_next_level_table(level) }; |
| 451 | debug!( |
| 452 | "{} {:#010x} Table -> {:#010x}", |
| 453 | level_prefix, |
| 454 | va, |
| 455 | next_level_table.as_ptr() as usize |
| 456 | ); |
| 457 | Self::print_table(level + 1, va, next_level_table); |
| 458 | } |
| 459 | _ => {} |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /// Adds memory region from the translation table. The function splits the region to blocks and |
| 465 | /// uses the block level functions to do the mapping. |
| 466 | /// # Arguments |
| 467 | /// * region: Memory region object |
| 468 | /// # Return value |
| 469 | /// * Virtual address of the mapped memory |
| 470 | fn map_region( |
| 471 | &mut self, |
| 472 | region: VirtualRegion, |
| 473 | attributes: Attributes, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 474 | ) -> Result<VirtualAddress, XlatError> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 475 | let blocks = Self::split_region_to_blocks(region.get_pa(), region.base(), region.length())?; |
| 476 | for block in blocks { |
| 477 | self.map_block(block, attributes.clone()); |
| 478 | } |
| 479 | |
| 480 | Ok(region.base()) |
| 481 | } |
| 482 | |
| 483 | /// Remove memory region from the translation table. The function splits the region to blocks |
| 484 | /// and uses the block level functions to do the unmapping. |
| 485 | /// # Arguments |
| 486 | /// * region: Memory region object |
| 487 | fn unmap_region(&mut self, region: &VirtualRegion) -> Result<(), XlatError> { |
| 488 | let blocks = Self::split_region_to_blocks(region.get_pa(), region.base(), region.length())?; |
| 489 | for block in blocks { |
| 490 | self.unmap_block(block); |
| 491 | } |
| 492 | |
| 493 | Ok(()) |
| 494 | } |
| 495 | |
| 496 | /// Find mapped region that contains the whole region |
| 497 | /// # Arguments |
| 498 | /// * region: Virtual address to look for |
| 499 | /// # Return value |
| 500 | /// * Reference to virtual region if found |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 501 | fn find_containing_region(&self, va: VirtualAddress, length: usize) -> Option<&VirtualRegion> { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 502 | self.regions.find_containing_region(va, length).ok() |
| 503 | } |
| 504 | |
| 505 | /// Splits memory region to blocks that matches the granule size of the translation table. |
| 506 | /// # Arguments |
| 507 | /// * pa: Physical address |
| 508 | /// * va: Virtual address |
| 509 | /// * length: Region size in bytes |
| 510 | /// # Return value |
| 511 | /// * Vector of granule sized blocks |
| 512 | fn split_region_to_blocks( |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 513 | mut pa: PhysicalAddress, |
| 514 | mut va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 515 | mut length: usize, |
| 516 | ) -> Result<Vec<Block>, XlatError> { |
| 517 | let min_granule_mask = Self::GRANULE_SIZES.last().unwrap() - 1; |
| 518 | |
| 519 | if length == 0 { |
| 520 | return Err(XlatError::InvalidParameterError( |
| 521 | "Length cannot be 0".to_string(), |
| 522 | )); |
| 523 | } |
| 524 | |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 525 | if (pa.0 | va.0 | length) & min_granule_mask != 0 { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 526 | return Err(XlatError::InvalidParameterError(format!( |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 527 | "Addresses and length must be aligned {:#08x} {:#08x} {:#x}", |
| 528 | pa.0, va.0, length |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 529 | ))); |
| 530 | } |
| 531 | |
| 532 | let mut pages = Vec::new(); |
| 533 | |
| 534 | while length > 0 { |
| 535 | for granule in &Self::GRANULE_SIZES { |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 536 | if (pa.0 | va.0) & (*granule - 1) == 0 && length >= *granule { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 537 | pages.push(Block::new(pa, va, *granule)); |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 538 | pa = pa.add_offset(*granule).ok_or(XlatError::Overflow)?; |
| 539 | va = va.add_offset(*granule).ok_or(XlatError::Overflow)?; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 540 | |
| 541 | length -= *granule; |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | Ok(pages) |
| 548 | } |
| 549 | |
| 550 | /// Add block to memory mapping |
| 551 | /// # Arguments |
| 552 | /// * block: Memory block that can be represented by a single translation table entry |
| 553 | /// * attributes: Memory block's permissions, flags |
| 554 | fn map_block(&mut self, block: Block, attributes: Attributes) { |
| 555 | Self::set_block_descriptor_recursively( |
| 556 | attributes, |
| 557 | block.pa, |
| 558 | block.va, |
| 559 | block.granule, |
| 560 | 1, |
| 561 | self.base_table.descriptors.as_mut_slice(), |
| 562 | &self.page_pool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 563 | &self.regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 564 | ); |
| 565 | } |
| 566 | |
| 567 | /// Adds the block descriptor to the translation table along all the intermediate tables the |
| 568 | /// reach the required granule. |
| 569 | /// # Arguments |
| 570 | /// * attributes: Memory block's permssions, flags |
| 571 | /// * pa: Physical address |
| 572 | /// * va: Virtual address |
| 573 | /// * granule: Translation granule in bytes |
| 574 | /// * level: Translation table level |
| 575 | /// * table: Translation table on the given level |
| 576 | /// * page_pool: Page pool where the function can allocate pages for the translation tables |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 577 | #[allow(clippy::too_many_arguments)] |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 578 | fn set_block_descriptor_recursively( |
| 579 | attributes: Attributes, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 580 | pa: PhysicalAddress, |
| 581 | va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 582 | granule: usize, |
| 583 | level: usize, |
| 584 | table: &mut [Descriptor], |
| 585 | page_pool: &PagePool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 586 | regime: &TranslationRegime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 587 | ) { |
| 588 | // Get descriptor of the current level |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 589 | let descriptor = &mut table[va.get_level_index(level)]; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 590 | |
| 591 | // We reached the required granule level |
| 592 | if Self::GRANULE_SIZES[level] == granule { |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 593 | // Follow break-before-make sequence |
| 594 | descriptor.set_block_or_invalid_descriptor_to_invalid(level); |
| 595 | Self::invalidate(regime, Some(va)); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 596 | descriptor.set_block_descriptor(level, pa, attributes); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | // Need to iterate forward |
| 601 | match descriptor.get_descriptor_type(level) { |
| 602 | DescriptorType::Invalid => { |
| 603 | let mut page = page_pool.allocate_pages(Page::SIZE).unwrap(); |
| 604 | unsafe { |
| 605 | let next_table = page.get_as_slice(); |
| 606 | descriptor.set_table_descriptor(level, next_table, None); |
| 607 | } |
| 608 | Self::set_block_descriptor_recursively( |
| 609 | attributes, |
| 610 | pa, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 611 | va.mask_for_level(level), |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 612 | granule, |
| 613 | level + 1, |
| 614 | unsafe { descriptor.get_next_level_table_mut(level) }, |
| 615 | page_pool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 616 | regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 617 | ) |
| 618 | } |
| 619 | DescriptorType::Block => { |
| 620 | // Saving current descriptor details |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 621 | let current_va = va.mask_for_level(level); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 622 | let current_pa = descriptor.get_block_output_address(level); |
| 623 | let current_attributes = descriptor.get_block_attributes(level); |
| 624 | |
| 625 | // Replace block descriptor by table descriptor |
| 626 | let mut page = page_pool.allocate_pages(Page::SIZE).unwrap(); |
| 627 | unsafe { |
| 628 | let next_table = page.get_as_slice(); |
| 629 | descriptor.set_table_descriptor(level, next_table, None); |
| 630 | } |
| 631 | |
| 632 | // Explode block descriptor to table entries |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 633 | for exploded_va in VirtualAddressRange::new( |
| 634 | current_va, |
| 635 | current_va.add_offset(Self::GRANULE_SIZES[level]).unwrap(), |
| 636 | ) |
| 637 | .step_by(Self::GRANULE_SIZES[level + 1]) |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 638 | { |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 639 | let offset = exploded_va.diff(current_va).unwrap(); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 640 | Self::set_block_descriptor_recursively( |
| 641 | current_attributes.clone(), |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 642 | current_pa.add_offset(offset).unwrap(), |
| 643 | exploded_va.mask_for_level(level), |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 644 | Self::GRANULE_SIZES[level + 1], |
| 645 | level + 1, |
| 646 | unsafe { descriptor.get_next_level_table_mut(level) }, |
| 647 | page_pool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 648 | regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 649 | ) |
| 650 | } |
| 651 | |
| 652 | // Invoke self to continue recursion on the newly created level |
| 653 | Self::set_block_descriptor_recursively( |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 654 | attributes, pa, va, granule, level, table, page_pool, regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 655 | ); |
| 656 | } |
| 657 | DescriptorType::Table => Self::set_block_descriptor_recursively( |
| 658 | attributes, |
| 659 | pa, |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 660 | va.mask_for_level(level), |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 661 | granule, |
| 662 | level + 1, |
| 663 | unsafe { descriptor.get_next_level_table_mut(level) }, |
| 664 | page_pool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 665 | regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 666 | ), |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /// Remove block from memory mapping |
| 671 | /// # Arguments |
| 672 | /// * block: memory block that can be represented by a single translation entry |
| 673 | fn unmap_block(&mut self, block: Block) { |
| 674 | Self::remove_block_descriptor_recursively( |
| 675 | block.va, |
| 676 | block.granule, |
| 677 | 1, |
| 678 | self.base_table.descriptors.as_mut_slice(), |
| 679 | &self.page_pool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 680 | &self.regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 681 | ); |
| 682 | } |
| 683 | |
| 684 | /// Removes block descriptor from the translation table along all the intermediate tables which |
| 685 | /// become empty during the removal process. |
| 686 | /// # Arguments |
| 687 | /// * va: Virtual address |
| 688 | /// * granule: Translation granule in bytes |
| 689 | /// * level: Translation table level |
| 690 | /// * table: Translation table on the given level |
| 691 | /// * page_pool: Page pool where the function can release the pages of empty tables |
| 692 | fn remove_block_descriptor_recursively( |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 693 | va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 694 | granule: usize, |
| 695 | level: usize, |
| 696 | table: &mut [Descriptor], |
| 697 | page_pool: &PagePool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 698 | regime: &TranslationRegime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 699 | ) { |
| 700 | // Get descriptor of the current level |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 701 | let descriptor = &mut table[va.get_level_index(level)]; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 702 | |
| 703 | // We reached the required granule level |
| 704 | if Self::GRANULE_SIZES[level] == granule { |
| 705 | descriptor.set_block_descriptor_to_invalid(level); |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 706 | Self::invalidate(regime, Some(va)); |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
| 710 | // Need to iterate forward |
| 711 | match descriptor.get_descriptor_type(level) { |
| 712 | DescriptorType::Invalid => { |
| 713 | panic!("Cannot remove block from non-existing table"); |
| 714 | } |
| 715 | DescriptorType::Block => { |
| 716 | panic!("Cannot remove block with different granule"); |
| 717 | } |
| 718 | DescriptorType::Table => { |
| 719 | let next_level_table = unsafe { descriptor.get_next_level_table_mut(level) }; |
| 720 | Self::remove_block_descriptor_recursively( |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 721 | va.mask_for_level(level), |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 722 | granule, |
| 723 | level + 1, |
| 724 | next_level_table, |
| 725 | page_pool, |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 726 | regime, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 727 | ); |
| 728 | |
| 729 | if next_level_table.iter().all(|d| !d.is_valid()) { |
| 730 | // Empty table |
| 731 | let mut page = unsafe { |
| 732 | Pages::from_slice(descriptor.set_table_descriptor_to_invalid(level)) |
| 733 | }; |
| 734 | page.zero_init(); |
| 735 | page_pool.release_pages(page).unwrap(); |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 741 | fn get_descriptor(&mut self, va: VirtualAddress, granule: usize) -> &mut Descriptor { |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 742 | Self::walk_descriptors(va, granule, 0, &mut self.base_table.descriptors) |
| 743 | } |
| 744 | |
| 745 | fn walk_descriptors( |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 746 | va: VirtualAddress, |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 747 | granule: usize, |
| 748 | level: usize, |
| 749 | table: &mut [Descriptor], |
| 750 | ) -> &mut Descriptor { |
| 751 | // Get descriptor of the current level |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 752 | let descriptor = &mut table[va.get_level_index(level)]; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 753 | |
| 754 | if Self::GRANULE_SIZES[level] == granule { |
| 755 | return descriptor; |
| 756 | } |
| 757 | |
| 758 | // Need to iterate forward |
| 759 | match descriptor.get_descriptor_type(level) { |
| 760 | DescriptorType::Invalid => { |
| 761 | panic!("Invalid descriptor"); |
| 762 | } |
| 763 | DescriptorType::Block => { |
| 764 | panic!("Cannot split existing block descriptor to table"); |
| 765 | } |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 766 | DescriptorType::Table => { |
| 767 | Self::walk_descriptors(va.mask_for_level(level), granule, level + 1, unsafe { |
| 768 | descriptor.get_next_level_table_mut(level) |
| 769 | }) |
| 770 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 771 | } |
| 772 | } |
Imre Kis | 9a9d049 | 2024-10-31 15:19:46 +0100 | [diff] [blame^] | 773 | |
| 774 | fn invalidate(regime: &TranslationRegime, va: Option<VirtualAddress>) { |
| 775 | // SAFETY: The assembly code invalidates the translation table entry of |
| 776 | // the VA or all entries of the translation regime. |
| 777 | #[cfg(target_arch = "aarch64")] |
| 778 | unsafe { |
| 779 | if let Some(VirtualAddress(va)) = va { |
| 780 | match regime { |
| 781 | TranslationRegime::EL1_0(_, _) => { |
| 782 | core::arch::asm!( |
| 783 | "tlbi vaae1is, {0} |
| 784 | dsb nsh |
| 785 | isb", |
| 786 | in(reg) va) |
| 787 | } |
| 788 | #[cfg(target_feature = "vh")] |
| 789 | TranslationRegime::EL2_0(_, _) => { |
| 790 | core::arch::asm!( |
| 791 | "tlbi vaae1is, {0} |
| 792 | dsb nsh |
| 793 | isb", |
| 794 | in(reg) va) |
| 795 | } |
| 796 | TranslationRegime::EL2 => core::arch::asm!( |
| 797 | "tlbi vae2is, {0} |
| 798 | dsb nsh |
| 799 | isb", |
| 800 | in(reg) va), |
| 801 | TranslationRegime::EL3 => core::arch::asm!( |
| 802 | "tlbi vae3is, {0} |
| 803 | dsb nsh |
| 804 | isb", |
| 805 | in(reg) va), |
| 806 | } |
| 807 | } else { |
| 808 | match regime { |
| 809 | TranslationRegime::EL1_0(_, asid) => core::arch::asm!( |
| 810 | "tlbi aside1, {0} |
| 811 | dsb nsh |
| 812 | isb", |
| 813 | in(reg) (*asid as u64) << 48 |
| 814 | ), |
| 815 | #[cfg(target_feature = "vh")] |
| 816 | TranslationRegime::EL2_0(_, asid) => core::arch::asm!( |
| 817 | "tlbi aside1, {0} |
| 818 | dsb nsh |
| 819 | isb", |
| 820 | in(reg) (*asid as u64) << 48 |
| 821 | ), |
| 822 | TranslationRegime::EL2 => core::arch::asm!( |
| 823 | "tlbi alle2 |
| 824 | dsb nsh |
| 825 | isb" |
| 826 | ), |
| 827 | TranslationRegime::EL3 => core::arch::asm!( |
| 828 | "tlbi alle3 |
| 829 | dsb nsh |
| 830 | isb" |
| 831 | ), |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 838 | #[cfg(test)] |
| 839 | mod tests { |
| 840 | use super::*; |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 841 | |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 842 | fn make_block(pa: usize, va: usize, granule: usize) -> Block { |
| 843 | Block::new(PhysicalAddress(pa), VirtualAddress(va), granule) |
| 844 | } |
| 845 | |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 846 | #[test] |
| 847 | fn test_split_to_pages() { |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 848 | let pages = Xlat::split_region_to_blocks( |
| 849 | PhysicalAddress(0x3fff_c000), |
| 850 | VirtualAddress(0x3fff_c000), |
| 851 | 0x4020_5000, |
| 852 | ) |
| 853 | .unwrap(); |
| 854 | assert_eq!(make_block(0x3fff_c000, 0x3fff_c000, 0x1000), pages[0]); |
| 855 | assert_eq!(make_block(0x3fff_d000, 0x3fff_d000, 0x1000), pages[1]); |
| 856 | assert_eq!(make_block(0x3fff_e000, 0x3fff_e000, 0x1000), pages[2]); |
| 857 | assert_eq!(make_block(0x3fff_f000, 0x3fff_f000, 0x1000), pages[3]); |
| 858 | assert_eq!(make_block(0x4000_0000, 0x4000_0000, 0x4000_0000), pages[4]); |
| 859 | assert_eq!(make_block(0x8000_0000, 0x8000_0000, 0x0020_0000), pages[5]); |
| 860 | assert_eq!(make_block(0x8020_0000, 0x8020_0000, 0x1000), pages[6]); |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | #[test] |
| 864 | fn test_split_to_pages_unaligned() { |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 865 | let pages = Xlat::split_region_to_blocks( |
| 866 | PhysicalAddress(0x3fff_c000), |
| 867 | VirtualAddress(0x3f20_0000), |
| 868 | 0x200000, |
| 869 | ) |
| 870 | .unwrap(); |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 871 | for (i, block) in pages.iter().enumerate().take(512) { |
| 872 | assert_eq!( |
Imre Kis | d5b96fd | 2024-09-11 17:04:32 +0200 | [diff] [blame] | 873 | make_block(0x3fff_c000 + (i << 12), 0x3f20_0000 + (i << 12), 0x1000), |
Imre Kis | 42935a2 | 2024-10-17 11:30:16 +0200 | [diff] [blame] | 874 | *block |
| 875 | ); |
| 876 | } |
Imre Kis | 703482d | 2023-11-30 15:51:26 +0100 | [diff] [blame] | 877 | } |
| 878 | } |