tmk_vmm/
paravisor_vmm.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Support for running as a paravisor VMM.
5
6#![cfg(target_os = "linux")]
7
8use crate::run::RunContext;
9use crate::run::RunnerBuilder;
10use crate::run::TestResult;
11use guestmem::GuestMemory;
12use std::sync::Arc;
13use virt::Partition;
14use virt_mshv_vtl::UhLateParams;
15use virt_mshv_vtl::UhPartitionNewParams;
16use virt_mshv_vtl::UhProcessorBox;
17
18impl RunContext<'_> {
19    pub async fn run_paravisor_vmm(
20        &mut self,
21        isolation: virt::IsolationType,
22        test: &crate::load::TestInfo,
23    ) -> anyhow::Result<TestResult> {
24        let params = UhPartitionNewParams {
25            isolation,
26            hide_isolation: false,
27            lower_vtl_memory_layout: &self.state.memory_layout,
28            topology: &self.state.processor_topology,
29            cvm_cpuid_info: None,
30            snp_secrets: None,
31            vtom: None,
32            handle_synic: true,
33            no_sidecar_hotplug: false,
34            use_mmio_hypercalls: false,
35            intercept_debug_exceptions: false,
36            disable_proxy_redirect: false,
37            // TODO: match openhcl defaults when TDX is supported.
38            disable_lower_vtl_timer_virt: true,
39        };
40        let p = virt_mshv_vtl::UhProtoPartition::new(params, |_| self.state.driver.clone())?;
41
42        let m = underhill_mem::init(&underhill_mem::Init {
43            processor_topology: &self.state.processor_topology,
44            isolation,
45            vtl0_alias_map_bit: None,
46            vtom: None,
47            mem_layout: &self.state.memory_layout,
48            complete_memory_layout: &self.state.memory_layout,
49            boot_init: None,
50            shared_pool: &[],
51            maximum_vtl: hvdef::Vtl::Vtl0,
52        })
53        .await?;
54
55        let (partition, vps) = p
56            .build(UhLateParams {
57                gm: [
58                    m.vtl0().clone(),
59                    m.vtl1().cloned().unwrap_or(GuestMemory::empty()),
60                ]
61                .into(),
62                vtl0_kernel_exec_gm: m.vtl0().clone(),
63                vtl0_user_exec_gm: m.vtl0().clone(),
64                #[cfg(guest_arch = "x86_64")]
65                cpuid: Vec::new(),
66                crash_notification_send: mesh::channel().0,
67                vmtime: self.vmtime_source,
68                cvm_params: None,
69                vmbus_relay: false,
70            })
71            .await?;
72
73        let partition = Arc::new(partition);
74
75        let mut threads = Vec::new();
76        let r = self
77            .run(m.vtl0(), partition.caps(), test, async |_this, runner| {
78                let [vp] = vps.try_into().ok().unwrap();
79                threads.push(start_vp(vp, runner).await?);
80                Ok(())
81            })
82            .await?;
83
84        for thread in threads {
85            thread.join().unwrap();
86        }
87
88        // Ensure the partition has not leaked.
89        Arc::into_inner(partition).expect("partition is no longer referenced");
90
91        Ok(r)
92    }
93}
94
95async fn start_vp(
96    mut vp: UhProcessorBox,
97    mut runner: RunnerBuilder,
98) -> anyhow::Result<std::thread::JoinHandle<()>> {
99    let vp_thread = std::thread::spawn(move || {
100        let pool = pal_uring::IoUringPool::new("vp", 256).unwrap();
101        let driver = pool.client().initiator().clone();
102        pool.client().set_idle_task(async move |mut control| {
103            let vp = vp
104                .bind_processor::<virt_mshv_vtl::HypervisorBacked>(&driver, Some(&mut control))
105                .unwrap();
106
107            runner.build(vp).unwrap().run_vp().await;
108        });
109        pool.run()
110    });
111    Ok(vp_thread)
112}