microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
openhcl/hcl_mapper/src/lib.rs
62lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | //! Provides a mapper implementation for the page pool that uses the hcl ioctl |
| 5 | //! crate to map guest memory. |
| 6 | |
| 7 | #![cfg(target_os = "linux")] |
| 8 | |
| 9 | use anyhow::Context; |
| 10 | use hcl::ioctl::MshvVtlLow; |
| 11 | use inspect::Inspect; |
| 12 | use page_pool_alloc::PoolSource; |
| 13 | use std::os::fd::AsFd; |
| 14 | |
| 15 | /// A mapper that uses [`MshvVtlLow`] to map pages. |
| 16 | #[derive(Inspect)] |
| 17 | pub struct HclMapper { |
| 18 | #[inspect(skip)] |
| 19 | fd: MshvVtlLow, |
| 20 | gpa_bias: u64, |
| 21 | is_shared: bool, |
| 22 | } |
| 23 | |
| 24 | impl HclMapper { |
| 25 | /// Creates an instance for mapping shared memory, with shared memory |
| 26 | /// appearing to the guest starting at `vtom`. |
| 27 | pub fn new_shared(vtom: u64) -> anyhow::Result<Self> { |
| 28 | Self::new_inner(vtom, true) |
| 29 | } |
| 30 | |
| 31 | /// Creates an instance for mapping private memory. |
| 32 | pub fn new_private() -> anyhow::Result<Self> { |
| 33 | Self::new_inner(0, false) |
| 34 | } |
| 35 | |
| 36 | fn new_inner(gpa_bias: u64, is_shared: bool) -> anyhow::Result<Self> { |
| 37 | let fd = MshvVtlLow::new().context("failed to open gpa fd")?; |
| 38 | Ok(Self { |
| 39 | fd, |
| 40 | gpa_bias, |
| 41 | is_shared, |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl PoolSource for HclMapper { |
| 47 | fn address_bias(&self) -> u64 { |
| 48 | self.gpa_bias |
| 49 | } |
| 50 | |
| 51 | fn file_offset(&self, address: u64) -> u64 { |
| 52 | address.wrapping_add(if self.is_shared { |
| 53 | MshvVtlLow::SHARED_MEMORY_FLAG |
| 54 | } else { |
| 55 | 0 |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | fn mappable(&self) -> sparse_mmap::MappableRef<'_> { |
| 60 | self.fd.get().as_fd() |
| 61 | } |
| 62 | } |
| 63 | |