microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
openhcl/get_lower_vtl/src/lib.rs
43lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #![cfg(target_os = "linux")] |
| 5 | |
| 6 | //! This is an implementation of [`virt::VtlMemoryProtection`] that uses the |
| 7 | //! [`hcl::ioctl::MshvHvcall`] type. This is only to be used for the GET, as we |
| 8 | //! cannot use the normal partition implementation in OpenHCL due to ordering |
| 9 | //! requirements for struct initialization. |
| 10 | |
| 11 | use anyhow::Context; |
| 12 | use anyhow::Result; |
| 13 | use hvdef::hypercall::HvInputVtl; |
| 14 | use inspect::Inspect; |
| 15 | use memory_range::MemoryRange; |
| 16 | use std::sync::Arc; |
| 17 | use virt::VtlMemoryProtection; |
| 18 | |
| 19 | #[derive(Inspect)] |
| 20 | pub struct GetLowerVtl { |
| 21 | #[inspect(skip)] |
| 22 | mshv_hvcall: hcl::ioctl::MshvHvcall, |
| 23 | } |
| 24 | |
| 25 | impl GetLowerVtl { |
| 26 | pub fn new() -> Result<Arc<Self>> { |
| 27 | let mshv_hvcall = hcl::ioctl::MshvHvcall::new().context("failed to open mshv_hvcall")?; |
| 28 | mshv_hvcall.set_allowed_hypercalls(&[hvdef::HypercallCode::HvCallModifyVtlProtectionMask]); |
| 29 | Ok(Arc::new(Self { mshv_hvcall })) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl VtlMemoryProtection for GetLowerVtl { |
| 34 | fn modify_vtl_page_setting(&self, pfn: u64, flags: hvdef::HvMapGpaFlags) -> Result<()> { |
| 35 | self.mshv_hvcall |
| 36 | .modify_vtl_protection_mask( |
| 37 | MemoryRange::from_4k_gpn_range(pfn..pfn + 1), |
| 38 | flags, |
| 39 | HvInputVtl::CURRENT_VTL, |
| 40 | ) |
| 41 | .context("failed to modify VTL page permissions") |
| 42 | } |
| 43 | } |
| 44 | |