Skip to content

Commit

Permalink
platform: add virt_to_phys checks for debugging
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
msft-jlange committed Sep 19, 2024
1 parent 45ff857 commit 89b77ed
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions kernel/src/platform/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SerialPort<'_>> = ImmutAfterInitCell::uninit();

Expand Down Expand Up @@ -108,6 +111,18 @@ impl SvsmPlatform for NativePlatform {
_region: MemoryRegion<VirtAddr>,
_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(())
}

Expand Down
15 changes: 15 additions & 0 deletions kernel/src/platform/snp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -182,6 +185,18 @@ impl SvsmPlatform for SnpPlatform {
region: MemoryRegion<VirtAddr>,
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))
}

Expand Down

0 comments on commit 89b77ed

Please sign in to comment.