vmm: hypervisor: simplify VM creation API

For MSHV customers don't want to make everything
default during partition creation. For example
nested support, some synthetic features could be
controlled from CLI through platform argument.
Create_vm API getting messy after adding more flags.
This patch introduces common data struct to be passed
from vmm crate to hypervisor crate during partition creation.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
This commit is contained in:
Muminul Islam
2025-09-12 17:30:26 -07:00
committed by Bo Chen
parent f122398262
commit 1e8996f94f
8 changed files with 167 additions and 216 deletions
+14 -22
View File
@@ -45,7 +45,7 @@ use gdbstub_arch::aarch64::reg::AArch64CoreRegs as CoreRegs;
use gdbstub_arch::x86::reg::X86_64CoreRegs as CoreRegs;
#[cfg(target_arch = "aarch64")]
use hypervisor::arch::aarch64::regs::AARCH64_PMU_IRQ;
use hypervisor::{HypervisorVmError, VmOps};
use hypervisor::{HypervisorVmConfig, HypervisorVmError, VmOps};
use libc::{SIGWINCH, termios};
use linux_loader::cmdline::Cmdline;
#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))]
@@ -1084,26 +1084,16 @@ impl Vm {
#[cfg(feature = "sev_snp")] mem_size: u64,
) -> Result<Arc<dyn hypervisor::Vm>> {
hypervisor.check_required_extensions().unwrap();
let config = HypervisorVmConfig {
#[cfg(feature = "tdx")]
tdx_enabled,
#[cfg(feature = "sev_snp")]
sev_snp_enabled,
#[cfg(feature = "sev_snp")]
mem_size,
};
cfg_if::cfg_if! {
if #[cfg(feature = "tdx")] {
// Passing KVM_X86_TDX_VM: 1 if tdx_enabled is true
// Otherwise KVM_X86_LEGACY_VM: 0
// value of tdx_enabled is mapped to KVM_X86_TDX_VM or KVM_X86_LEGACY_VM
let vm = hypervisor
.create_vm_with_type(u64::from(tdx_enabled))
.unwrap();
} else if #[cfg(feature = "sev_snp")] {
// Passing SEV_SNP_ENABLED: 1 if sev_snp_enabled is true
// Otherwise SEV_SNP_DISABLED: 0
// value of sev_snp_enabled is mapped to SEV_SNP_ENABLED for true or SEV_SNP_DISABLED for false
let vm = hypervisor
.create_vm_with_type_and_memory(u64::from(sev_snp_enabled), mem_size)
.unwrap();
} else {
let vm = hypervisor.create_vm().unwrap();
}
}
let vm = hypervisor.create_vm(config).unwrap();
#[cfg(target_arch = "x86_64")]
{
@@ -3497,7 +3487,7 @@ mod tests {
.collect();
let hv = hypervisor::new().unwrap();
let vm = hv.create_vm().unwrap();
let vm = hv.create_vm(HypervisorVmConfig::default()).unwrap();
let gic = vm
.create_vgic(Gic::create_default_config(1))
.expect("Cannot create gic");
@@ -3539,7 +3529,9 @@ pub fn test_vm() {
let mem = GuestMemoryMmap::from_ranges(&[(load_addr, mem_size)]).unwrap();
let hv = hypervisor::new().unwrap();
let vm = hv.create_vm().expect("new VM creation failed");
let vm = hv
.create_vm(HypervisorVmConfig::default())
.expect("new VM creation failed");
for (index, region) in mem.iter().enumerate() {
let mem_region = vm.make_user_memory_region(