[hypervisor] Add mmio_guard_init() to Hypervisor trait
Since MMIO guard is currently KVM specific, we can move the details
to hypervisor/kvm.rs. This makes it easier to extend the Hypervisor
trait for the hypervisors that do not support MMIO guard.
Bug: 272226230
Test: atest rialto_test && m pvmfw_img
Change-Id: Ib313130946f7f27968fca79c7954a27bc283d0b5
diff --git a/pvmfw/src/entry.rs b/pvmfw/src/entry.rs
index e0af856..1309d73 100644
--- a/pvmfw/src/entry.rs
+++ b/pvmfw/src/entry.rs
@@ -25,7 +25,7 @@
use core::arch::asm;
use core::num::NonZeroUsize;
use core::slice;
-use hyp::mmio_guard;
+use hyp::get_hypervisor;
use log::debug;
use log::error;
use log::info;
@@ -172,12 +172,12 @@
// Use debug!() to avoid printing to the UART if we failed to configure it as only local
// builds that have tweaked the logger::init() call will actually attempt to log the message.
- mmio_guard::init().map_err(|e| {
+ get_hypervisor().mmio_guard_init().map_err(|e| {
debug!("{e}");
RebootReason::InternalError
})?;
- mmio_guard::map(console::BASE_ADDRESS).map_err(|e| {
+ get_hypervisor().mmio_guard_map(console::BASE_ADDRESS).map_err(|e| {
debug!("Failed to configure the UART: {e}");
RebootReason::InternalError
})?;
@@ -236,7 +236,7 @@
error!("Failed to unshare MMIO ranges: {e}");
RebootReason::InternalError
})?;
- mmio_guard::unmap(console::BASE_ADDRESS).map_err(|e| {
+ get_hypervisor().mmio_guard_unmap(console::BASE_ADDRESS).map_err(|e| {
error!("Failed to unshare the UART: {e}");
RebootReason::InternalError
})?;
diff --git a/pvmfw/src/memory.rs b/pvmfw/src/memory.rs
index 3f44f8a..2d5eb5c 100644
--- a/pvmfw/src/memory.rs
+++ b/pvmfw/src/memory.rs
@@ -29,7 +29,7 @@
use core::ops::Range;
use core::ptr::NonNull;
use core::result;
-use hyp::{get_hypervisor, mmio_guard};
+use hyp::get_hypervisor;
use log::error;
use tinyvec::ArrayVec;
@@ -102,8 +102,8 @@
Overlaps,
/// Region couldn't be mapped.
FailedToMap,
- /// Error from an MMIO guard call.
- MmioGuard(mmio_guard::Error),
+ /// Error from the interaction with the hypervisor.
+ Hypervisor(hyp::Error),
}
impl fmt::Display for MemoryTrackerError {
@@ -116,14 +116,14 @@
Self::OutOfRange => write!(f, "Region is out of the tracked memory address space"),
Self::Overlaps => write!(f, "New region overlaps with tracked regions"),
Self::FailedToMap => write!(f, "Failed to map the new region"),
- Self::MmioGuard(e) => e.fmt(f),
+ Self::Hypervisor(e) => e.fmt(f),
}
}
}
-impl From<mmio_guard::Error> for MemoryTrackerError {
- fn from(e: mmio_guard::Error) -> Self {
- Self::MmioGuard(e)
+impl From<hyp::Error> for MemoryTrackerError {
+ fn from(e: hyp::Error) -> Self {
+ Self::Hypervisor(e)
}
}
@@ -213,7 +213,7 @@
})?;
for page_base in page_iterator(&range) {
- mmio_guard::map(page_base)?;
+ get_hypervisor().mmio_guard_map(page_base)?;
}
if self.mmio_regions.try_push(range).is_some() {
@@ -253,7 +253,7 @@
pub fn mmio_unmap_all(&self) -> Result<()> {
for region in &self.mmio_regions {
for page_base in page_iterator(region) {
- mmio_guard::unmap(page_base)?;
+ get_hypervisor().mmio_guard_unmap(page_base)?;
}
}
@@ -278,7 +278,7 @@
/// Gives the KVM host read, write and execute permissions on the given memory range. If the range
/// is not aligned with the memory protection granule then it will be extended on either end to
/// align.
-fn share_range(range: &MemoryRange, granule: usize) -> smccc::Result<()> {
+fn share_range(range: &MemoryRange, granule: usize) -> hyp::Result<()> {
for base in (align_down(range.start, granule)
.expect("Memory protection granule was not a power of two")..range.end)
.step_by(granule)
@@ -291,7 +291,7 @@
/// Removes permission from the KVM host to access the given memory range which was previously
/// shared. If the range is not aligned with the memory protection granule then it will be extended
/// on either end to align.
-fn unshare_range(range: &MemoryRange, granule: usize) -> smccc::Result<()> {
+fn unshare_range(range: &MemoryRange, granule: usize) -> hyp::Result<()> {
for base in (align_down(range.start, granule)
.expect("Memory protection granule was not a power of two")..range.end)
.step_by(granule)
@@ -305,7 +305,7 @@
/// with the host. Returns a pointer to the buffer.
///
/// It will be aligned to the memory sharing granule size supported by the hypervisor.
-pub fn alloc_shared(size: usize) -> smccc::Result<NonNull<u8>> {
+pub fn alloc_shared(size: usize) -> hyp::Result<NonNull<u8>> {
let layout = shared_buffer_layout(size)?;
let granule = layout.align();
@@ -333,7 +333,7 @@
///
/// The memory must have been allocated by `alloc_shared` with the same size, and not yet
/// deallocated.
-pub unsafe fn dealloc_shared(vaddr: NonNull<u8>, size: usize) -> smccc::Result<()> {
+pub unsafe fn dealloc_shared(vaddr: NonNull<u8>, size: usize) -> hyp::Result<()> {
let layout = shared_buffer_layout(size)?;
let granule = layout.align();
@@ -352,7 +352,7 @@
/// It will be aligned to the memory sharing granule size supported by the hypervisor.
///
/// Panics if `size` is 0.
-fn shared_buffer_layout(size: usize) -> smccc::Result<Layout> {
+fn shared_buffer_layout(size: usize) -> hyp::Result<Layout> {
assert_ne!(size, 0);
let granule = get_hypervisor().memory_protection_granule()?;
let allocated_size =