From 2b47b08d9895a675551d1e06acf7df056aaa6c3d Mon Sep 17 00:00:00 2001 From: Jon Lange Date: Thu, 19 Sep 2024 09:36:40 -0700 Subject: [PATCH] platform: add virt_to_phys checks for debugging Some platforms will require translation from virtual to physical address in order to perform page validation by virtual address. Therefore, on other platforms, force a call to `virt_to_phys()` in debug builds to increas the number of test environments that can detect possible translation failures to add in debugging. Signed-off-by: Jon Lange --- kernel/src/platform/native.rs | 15 +++++++++++++++ kernel/src/platform/snp.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/kernel/src/platform/native.rs b/kernel/src/platform/native.rs index c8542089b..efa2d00b6 100644 --- a/kernel/src/platform/native.rs +++ b/kernel/src/platform/native.rs @@ -18,6 +18,9 @@ use crate::types::PageSize; use crate::utils::immut_after_init::ImmutAfterInitCell; use crate::utils::MemoryRegion; +#[cfg(debug_assertions)] +use crate::mm::virt_to_phys; + static CONSOLE_IO: NativeIOPort = NativeIOPort::new(); static CONSOLE_SERIAL: ImmutAfterInitCell> = ImmutAfterInitCell::uninit(); @@ -108,6 +111,18 @@ impl SvsmPlatform for NativePlatform { _region: MemoryRegion, _op: PageValidateOp, ) -> Result<(), SvsmError> { + #[cfg(debug_assertions)] + { + // Ensure that it is possible to translate this virtual address to + // a physical address. This is not necessary for correctness + // here, but since other platformss may rely on virtual-to-physical + // translation, it is helpful to force a translation here for + // debugging purposes just to help catch potential errors when + // testing on native. + for va in _region.iter_pages(PageSize::Regular) { + let _ = virt_to_phys(va); + } + } Ok(()) } diff --git a/kernel/src/platform/snp.rs b/kernel/src/platform/snp.rs index 42c60aea4..3ce5dd65d 100644 --- a/kernel/src/platform/snp.rs +++ b/kernel/src/platform/snp.rs @@ -25,6 +25,9 @@ use crate::types::PageSize; use crate::utils::immut_after_init::ImmutAfterInitCell; use crate::utils::MemoryRegion; +#[cfg(debug_assertions)] +use crate::mm::virt_to_phys; + use core::sync::atomic::{AtomicU32, Ordering}; static CONSOLE_IO: SVSMIOPort = SVSMIOPort::new(); @@ -182,6 +185,18 @@ impl SvsmPlatform for SnpPlatform { region: MemoryRegion, op: PageValidateOp, ) -> Result<(), SvsmError> { + #[cfg(debug_assertions)] + { + // Ensure that it is possible to translate this virtual address to + // a physical address. This is not necessary for correctness + // here, but since other platformss may rely on virtual-to-physical + // translation, it is helpful to force a translation here for + // debugging purposes just to help catch potential errors when + // testing on SNP. + for va in region.iter_pages(PageSize::Regular) { + let _ = virt_to_phys(va); + } + } pvalidate_range(region, PvalidateOp::from(op)) }