pvmfw: Improve memory sharing with the host
Instead of issuing memory sharing/unsharing HVCs every time alloc_shared
and dealloc_shared are called, introduce a MemorySharer object to share
pages from the global allocator on demand and keep them in a pool until
its destructor is called, at which point, it unshares all the regions
and returns them to the heap.
This allows the backends for memory sharing to be unified behind a
unique SHARED_POOL heap that is either populated by the MemorySharer on
pKVM or covers the statically shared region on Gunyah. On pKVM,
alloc_shared calls will only result in MEM_SHARE HVCs if SHARED_POOL
isn't already large enough to fulfill the request.
This also guarantees that all pages that have been been shared during
pvmfw execution have been unshared by the time the guest OS is entered,
even if an alloc_shared hasn't been matched with a dealloc_shared call.
By caching the granule size in the MemorySharer ctor, this removes the
need to issue a granule-discovery HVC every time memory is being shared
or unshared with the host.
Bug: 280644106
Test: atest MicrodroidHostTests
Change-Id: I3992e7c59ea79897e93bccc043d4438acbc0586c
diff --git a/pvmfw/src/helpers.rs b/pvmfw/src/helpers.rs
index a6f0dd5..c230784 100644
--- a/pvmfw/src/helpers.rs
+++ b/pvmfw/src/helpers.rs
@@ -98,6 +98,7 @@
/// Aligns the given address to the given alignment, if it is a power of two.
///
/// Returns `None` if the alignment isn't a power of two.
+#[allow(dead_code)] // Currently unused but might be needed again.
pub const fn align_down(addr: usize, alignment: usize) -> Option<usize> {
if !alignment.is_power_of_two() {
None