Skip to main content

virt/generic/
partition_memory_map.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4// UNSAFETY: Declaring unsafe trait functions for manual memory management.
5#![expect(unsafe_code)]
6
7/// Trait for mapping process memory into a partition.
8pub trait PartitionMemoryMap: Send + Sync {
9    /// Unmaps any ranges in the given guest physical address range.
10    ///
11    /// The specified range may overlap zero, one, or many ranges mapped with
12    /// `map_range`. Any overlapped ranges must be completely contained in the
13    /// specified range.
14    ///
15    /// The hypervisor must ensure that this operation does not fail as long as
16    /// the preconditions are satisfied.
17    fn unmap_range(&self, addr: u64, size: u64) -> anyhow::Result<()>;
18
19    /// Maps a range from process memory into the VM.
20    ///
21    /// This may fail if the range overlaps any other mapped range.
22    ///
23    /// # Safety
24    /// The caller must ensure that the VA region (data..data+size) is not
25    /// reused for the lifetime of this mapping.
26    unsafe fn map_range(
27        &self,
28        data: *mut u8,
29        size: usize,
30        addr: u64,
31        writable: bool,
32        exec: bool,
33    ) -> anyhow::Result<()>;
34
35    /// Prefetches any memory in the given range so that it can be accessed
36    /// quickly by the partition without exits.
37    fn prefetch_range(&self, _addr: u64, _size: u64) -> anyhow::Result<()> {
38        Ok(())
39    }
40
41    /// Pins a range in memory so that it can be accessed by assigned devices.
42    fn pin_range(&self, _addr: u64, _size: u64) -> anyhow::Result<()> {
43        Ok(())
44    }
45
46    /// Maps a range residing in a remote process.
47    ///
48    /// This may fail if the range overlaps any other mapped range.
49    ///
50    /// # Safety
51    /// The caller must ensure that the VA region (data..data+size) within
52    /// `process` is not reused for the lifetime of this mapping.
53    #[cfg(windows)]
54    unsafe fn map_remote_range(
55        &self,
56        process: std::os::windows::io::BorrowedHandle<'_>,
57        data: *mut u8,
58        size: usize,
59        addr: u64,
60        writable: bool,
61        exec: bool,
62    ) -> anyhow::Result<()>;
63}
64
65/// Interface for acquiring host access to guest memory.
66///
67/// Some isolated hypervisors do not make a guest page accessible to userspace
68/// merely because the guest marked it shared. The VMM must also ask the
69/// hypervisor to grant the host permission to touch the existing backing.
70pub trait PartitionHostAccess: Send + Sync {
71    /// Acquires host access without changing guest visibility.
72    ///
73    /// TODO: This trait is sufficient for MSHV bring-up, but a redesign is
74    /// required to safely lower host access and track that pages are not
75    /// currently in use before revoking access.
76    fn acquire_host_access(&self, addr: u64, size: u64, write: bool) -> anyhow::Result<()>;
77}