Skip to main content

openvmm_hypervisors/
whp.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! WHP hypervisor backend.
5
6#![cfg(all(target_os = "windows", feature = "virt_whp", guest_is_native))]
7
8use crate::parse_bool_param;
9use hypervisor_resources::HypervisorKind;
10use hypervisor_resources::WhpHandle;
11use vm_resource::Resource;
12
13/// WHP probe for auto-detection.
14pub struct WhpProbe;
15
16impl hypervisor_resources::HypervisorProbe for WhpProbe {
17    fn name(&self) -> &str {
18        "whp"
19    }
20
21    fn try_new_resource(&self) -> anyhow::Result<Option<Resource<HypervisorKind>>> {
22        Ok(virt_whp::is_available()?.then(|| Resource::new(WhpHandle::default())))
23    }
24
25    fn new_resource(&self, params: &[(&str, &str)]) -> anyhow::Result<Resource<HypervisorKind>> {
26        let mut handle = WhpHandle::default();
27        for &(key, val) in params {
28            match key {
29                "user_mode_apic" => {
30                    if cfg!(guest_arch = "x86_64") {
31                        handle.user_mode_apic = parse_bool_param(key, val)?;
32                    } else {
33                        anyhow::bail!("whp parameter {key} is only supported for x86_64 guests");
34                    }
35                }
36                "no_enlightenments" => {
37                    if cfg!(guest_arch = "x86_64") {
38                        handle.offload_enlightenments = !parse_bool_param(key, val)?;
39                    } else {
40                        anyhow::bail!("whp parameter {key} is only supported for x86_64 guests");
41                    }
42                }
43                "nested_virt" => {
44                    if cfg!(guest_arch = "x86_64") {
45                        handle.nested_virt = parse_bool_param(key, val)?;
46                    } else {
47                        anyhow::bail!("whp parameter {key} is only supported for x86_64 guests");
48                    }
49                }
50                _ => anyhow::bail!("unknown whp parameter: {key}"),
51            }
52        }
53        anyhow::ensure!(virt_whp::is_available()?, "WHP is not available");
54        Ok(Resource::new(handle))
55    }
56}