vmbase: Use heap for non-protected SHARED_POOL

On non-protected platforms, buffers shared with devices (i.e. the host)
may be located on the heap as there are no security constraints given
that the host already has full access to guest memory. As the PCI/VirtIO
drivers expect SHARED_POOL to have been initialized and as the
init_{dynamic,static}_shared_pool() functions aren't appropriate for
non-protected VMs, provide a third method allowing clients to signal
that they want SHARED_POOL to be initialized to simply wrap the heap.

Use this mechanism in Rialto.

Test: atest DebugPolicyHostTests#testNoAdbInDebugPolicy_withDebugLevelNone_boots
Test: atest rialto_test vmbase_example.integration_test
Change-Id: I7098c8dfd3a5f2f447763f5f43ba880f2d8b008f
diff --git a/rialto/src/main.rs b/rialto/src/main.rs
index 5e693c8..e7b6386 100644
--- a/rialto/src/main.rs
+++ b/rialto/src/main.rs
@@ -104,8 +104,8 @@
             error!("Failed to initialize dynamically shared pool.");
             e
         })?;
-    } else {
-        let range = SwiotlbInfo::new_from_fdt(fdt)?.fixed_range().ok_or_else(|| {
+    } else if let Ok(swiotlb_info) = SwiotlbInfo::new_from_fdt(fdt) {
+        let range = swiotlb_info.fixed_range().ok_or_else(|| {
             error!("Pre-shared pool range not specified in swiotlb node");
             Error::from(FdtError::BadValue)
         })?;
@@ -113,6 +113,12 @@
             error!("Failed to initialize pre-shared pool.");
             e
         })?;
+    } else {
+        info!("No MEM_SHARE capability detected or swiotlb found: allocating buffers from heap.");
+        MEMORY.lock().as_mut().unwrap().init_heap_shared_pool().map_err(|e| {
+            error!("Failed to initialize heap-based pseudo-shared pool.");
+            e
+        })?;
     }
 
     let pci_info = PciInfo::from_fdt(fdt)?;
diff --git a/vmbase/src/memory/shared.rs b/vmbase/src/memory/shared.rs
index 1e29c79..f07178e 100644
--- a/vmbase/src/memory/shared.rs
+++ b/vmbase/src/memory/shared.rs
@@ -26,6 +26,7 @@
 use alloc::vec::Vec;
 use buddy_system_allocator::{FrameAllocator, LockedFrameAllocator};
 use core::alloc::Layout;
+use core::mem::size_of;
 use core::num::NonZeroUsize;
 use core::ops::Range;
 use core::ptr::NonNull;
@@ -263,6 +264,19 @@
         Ok(())
     }
 
+    /// Initialize the shared heap to use heap memory directly.
+    ///
+    /// When running on "non-protected" hypervisors which permit host direct accesses to guest
+    /// memory, there is no need to perform any memory sharing and/or allocate buffers from a
+    /// dedicated region so this function instructs the shared pool to use the global allocator.
+    pub fn init_heap_shared_pool(&mut self) -> Result<()> {
+        // As MemorySharer only calls MEM_SHARE methods if the hypervisor supports them, internally
+        // using init_dynamic_shared_pool() on a non-protected platform will make use of the heap
+        // without any actual "dynamic memory sharing" taking place and, as such, the granule may
+        // be set to the one of the global_allocator i.e. a byte.
+        self.init_dynamic_shared_pool(size_of::<u8>())
+    }
+
     /// Unshares any memory that may have been shared.
     pub fn unshare_all_memory(&mut self) {
         drop(SHARED_MEMORY.lock().take());