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) -> Result<(), anyhow::Error>;
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 ) -> Result<(), anyhow::Error>;
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) -> Result<(), anyhow::Error> {
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) -> Result<(), anyhow::Error> {
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 ) -> Result<(), anyhow::Error>;
63}