Add wrappers for memory sharing HVCs.
Bug: 261439403
Test: Ran pVM firmware manually.
Change-Id: Ic63863aff8945c8c84641c8777db7ccb8d255e8f
diff --git a/pvmfw/src/memory.rs b/pvmfw/src/memory.rs
index 892089e..5e4874f 100644
--- a/pvmfw/src/memory.rs
+++ b/pvmfw/src/memory.rs
@@ -14,9 +14,11 @@
//! Low-level allocation and tracking of main memory.
-use crate::helpers::{self, page_4kb_of, SIZE_4KB};
+use crate::helpers::{self, align_down, page_4kb_of, SIZE_4KB};
+use crate::hvc::{hyp_meminfo, mem_share, mem_unshare};
use crate::mmio_guard;
use crate::mmu;
+use crate::smccc;
use core::cmp::max;
use core::cmp::min;
use core::fmt;
@@ -267,6 +269,36 @@
}
}
+/// 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.
+#[allow(unused)]
+pub fn share_range(range: &MemoryRange) -> smccc::Result<()> {
+ let granule = hyp_meminfo()? as usize;
+ for base in (align_down(range.start, granule)
+ .expect("Memory protection granule was not a power of two")..range.end)
+ .step_by(granule)
+ {
+ mem_share(base as u64)?;
+ }
+ Ok(())
+}
+
+/// 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.
+#[allow(unused)]
+pub fn unshare_range(range: &MemoryRange) -> smccc::Result<()> {
+ let granule = hyp_meminfo()? as usize;
+ for base in (align_down(range.start, granule)
+ .expect("Memory protection granule was not a power of two")..range.end)
+ .step_by(granule)
+ {
+ mem_unshare(base as u64)?;
+ }
+ Ok(())
+}
+
/// Returns an iterator which yields the base address of each 4 KiB page within the given range.
fn page_iterator(range: &MemoryRange) -> impl Iterator<Item = usize> {
(page_4kb_of(range.start)..range.end).step_by(SIZE_4KB)