vmbase: Move MMIO_GUARD granule check out of ::hyp

Validate the granule where it's being used to prepare for non-4KiB
granule sizes.

Note: No functional change intended.

Test: m libpvmfw libvmbase_example librialto
Change-Id: Ic8af83b8565815412127c8bc881e3bc795ae2639
diff --git a/vmbase/src/entry.rs b/vmbase/src/entry.rs
index 8b1d7c6..892dc9f 100644
--- a/vmbase/src/entry.rs
+++ b/vmbase/src/entry.rs
@@ -15,7 +15,9 @@
 //! Rust entry point.
 
 use crate::{
-    bionic, console, heap, hyp, logger,
+    bionic, console, heap,
+    hyp::{self, MMIO_GUARD_GRANULE_SIZE},
+    logger,
     power::{reboot, shutdown},
     rand,
 };
@@ -26,7 +28,11 @@
 
     if let Some(mmio_guard) = hyp::get_mmio_guard() {
         mmio_guard.enroll()?;
-        mmio_guard.validate_granule()?;
+
+        // TODO(ptosi): Use MmioSharer::share() to properly track this MMIO_GUARD_MAP.
+        //
+        // MmioSharer only supports MMIO_GUARD_GRANULE_SIZE so fail early here if needed.
+        assert_eq!(mmio_guard.granule()?, MMIO_GUARD_GRANULE_SIZE);
         mmio_guard.map(console::BASE_ADDRESS)?;
     }
 
diff --git a/vmbase/src/hyp/error.rs b/vmbase/src/hyp/error.rs
index bbf706e..e9c37e1 100644
--- a/vmbase/src/hyp/error.rs
+++ b/vmbase/src/hyp/error.rs
@@ -33,8 +33,6 @@
     GeniezoneError(GeniezoneError, u32),
     /// Unsupported Hypervisor
     UnsupportedHypervisorUuid(Uuid),
-    /// The MMIO_GUARD granule used by the hypervisor is not supported.
-    UnsupportedMmioGuardGranule(usize),
 }
 
 impl fmt::Display for Error {
@@ -53,9 +51,6 @@
             Self::UnsupportedHypervisorUuid(u) => {
                 write!(f, "Unsupported Hypervisor UUID {u}")
             }
-            Self::UnsupportedMmioGuardGranule(g) => {
-                write!(f, "Unsupported MMIO guard granule: {g}")
-            }
         }
     }
 }
diff --git a/vmbase/src/hyp/hypervisor/common.rs b/vmbase/src/hyp/hypervisor/common.rs
index c1995af..1ed0696 100644
--- a/vmbase/src/hyp/hypervisor/common.rs
+++ b/vmbase/src/hyp/hypervisor/common.rs
@@ -14,10 +14,7 @@
 
 //! This module regroups some common traits shared by all the hypervisors.
 
-use crate::{
-    hyp::{Error, Result},
-    memory::SIZE_4KB,
-};
+use crate::{hyp::Result, memory::SIZE_4KB};
 
 /// Expected MMIO guard granule size, validated during MMIO guard initialization.
 pub const MMIO_GUARD_GRANULE_SIZE: usize = SIZE_4KB;
@@ -55,15 +52,6 @@
 
     /// Returns the MMIO guard granule size in bytes.
     fn granule(&self) -> Result<usize>;
-
-    // TODO(ptosi): Fully move granule validation to client code.
-    /// Validates the MMIO guard granule size.
-    fn validate_granule(&self) -> Result<()> {
-        match self.granule()? {
-            MMIO_GUARD_GRANULE_SIZE => Ok(()),
-            granule => Err(Error::UnsupportedMmioGuardGranule(granule)),
-        }
-    }
 }
 
 pub trait MemSharingHypervisor {
diff --git a/vmbase/src/memory/error.rs b/vmbase/src/memory/error.rs
index 901621d..4d08f1e 100644
--- a/vmbase/src/memory/error.rs
+++ b/vmbase/src/memory/error.rs
@@ -51,6 +51,8 @@
     SetPteDirtyFailed,
     /// Attempting to MMIO_GUARD_MAP more than once the same region.
     DuplicateMmioShare(usize),
+    /// The MMIO_GUARD granule used by the hypervisor is not supported.
+    UnsupportedMmioGuardGranule(usize),
 }
 
 impl fmt::Display for MemoryTrackerError {
@@ -73,6 +75,9 @@
             Self::DuplicateMmioShare(addr) => {
                 write!(f, "Attempted to share the same MMIO region at {addr:#x} twice")
             }
+            Self::UnsupportedMmioGuardGranule(g) => {
+                write!(f, "Unsupported MMIO guard granule: {g}")
+            }
         }
     }
 }
diff --git a/vmbase/src/memory/shared.rs b/vmbase/src/memory/shared.rs
index d44d58a..e3a5978 100644
--- a/vmbase/src/memory/shared.rs
+++ b/vmbase/src/memory/shared.rs
@@ -386,8 +386,7 @@
 
 impl MmioSharer {
     fn new() -> Result<Self> {
-        let granule = MMIO_GUARD_GRANULE_SIZE;
-        const_assert_eq!(MMIO_GUARD_GRANULE_SIZE, PAGE_SIZE); // For good measure.
+        let granule = Self::get_granule()?;
         let frames = BTreeSet::new();
 
         // Allows safely calling util::unchecked_align_down().
@@ -396,6 +395,17 @@
         Ok(Self { granule, frames })
     }
 
+    fn get_granule() -> Result<usize> {
+        const_assert_eq!(MMIO_GUARD_GRANULE_SIZE, PAGE_SIZE); // For good measure.
+        let Some(mmio_guard) = get_mmio_guard() else {
+            return Ok(PAGE_SIZE);
+        };
+        match mmio_guard.granule()? {
+            MMIO_GUARD_GRANULE_SIZE => Ok(MMIO_GUARD_GRANULE_SIZE),
+            granule => Err(MemoryTrackerError::UnsupportedMmioGuardGranule(granule)),
+        }
+    }
+
     /// Share the MMIO region aligned to the granule size containing addr (not validated as MMIO).
     fn share(&mut self, addr: VirtualAddress) -> Result<VaRange> {
         // This can't use virt_to_phys() since 0x0 is a valid MMIO address and we are ID-mapped.