pvmfw: virtio: Clean up hal.rs

Replace contains_range() with the RangeExt::is_within helper.

Turn the logging calls tracking VirtIO buffer management into trace!().

Move logging for allocation of shared memory to the allocators.

Avoid using as when casting pointers and use methods instead; in
particular, explicitly const_cast the source of a copy_nonoverlapping.

Make the logs for copying to/from bounce buffers easier to grep/parse.

Minimize scope of unsafe blocks, add missing SAFETY comments, and
clarify that HalImpl methods safety requirements are documented in the
trait.

Add copyright header and module docstring.

Bug: 280644106
Test: atest MicrodroidTests
Change-Id: I1b4daade70f5f43b6bc0f222568fbaaa29edf27f
diff --git a/pvmfw/src/memory.rs b/pvmfw/src/memory.rs
index a2b7e09..16c1a37 100644
--- a/pvmfw/src/memory.rs
+++ b/pvmfw/src/memory.rs
@@ -34,6 +34,7 @@
 use core::result;
 use hyp::get_hypervisor;
 use log::error;
+use log::trace;
 use once_cell::race::OnceBox;
 use spin::mutex::SpinMutex;
 use tinyvec::ArrayVec;
@@ -317,6 +318,7 @@
 /// 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) -> hyp::Result<()> {
+    trace!("Sharing memory region {range:#x?}");
     for base in (align_down(range.start, granule)
         .expect("Memory protection granule was not a power of two")..range.end)
         .step_by(granule)
@@ -330,6 +332,7 @@
 /// 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) -> hyp::Result<()> {
+    trace!("Unsharing memory region {range:#x?}");
     for base in (align_down(range.start, granule)
         .expect("Memory protection granule was not a power of two")..range.end)
         .step_by(granule)
@@ -356,6 +359,7 @@
             handle_alloc_error(layout);
         };
 
+        trace!("Allocated shared buffer at {buffer:?} with {layout:?}");
         return Ok(buffer);
     }
 
@@ -372,6 +376,7 @@
     // be reused while maybe still partially shared with the host.
     share_range(&(paddr..paddr + layout.size()), granule)?;
 
+    trace!("Allocated shared memory at {buffer:?} with {layout:?}");
     Ok(buffer)
 }
 
@@ -392,6 +397,7 @@
         // the same allocator, and the layout is the same as was used then.
         unsafe { shared_pool.dealloc(vaddr.as_ptr(), layout) };
 
+        trace!("Deallocated shared buffer at {vaddr:?} with {layout:?}");
         return Ok(());
     }
 
@@ -401,6 +407,7 @@
     // the layout is the same as was used then.
     unsafe { dealloc(vaddr.as_ptr(), layout) };
 
+    trace!("Deallocated shared memory at {vaddr:?} with {layout:?}");
     Ok(())
 }