build: Consolidate sev_snp feature usage

Since igvm is a required feature of sev_snp and also sev_snp is x86-64
only the cfg attributes at build time can be consolidated & simplified.

Signed-off-by: Rob Bradford <rbradford@meta.com>
This commit is contained in:
Rob Bradford
2026-06-12 16:22:11 +01:00
committed by Bo Chen
parent bf3bc325e2
commit 3b9229e434
7 changed files with 29 additions and 115 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
#![allow(dead_code)] #![allow(dead_code)]
mod common; mod common;
#[cfg(all(feature = "sev_snp", target_arch = "x86_64"))] #[cfg(feature = "sev_snp")]
mod common_cvm { mod common_cvm {
use block::ImageType; use block::ImageType;
use common::tests_wrappers::*; use common::tests_wrappers::*;
+5 -5
View File
@@ -576,9 +576,9 @@ pub struct KvmVm {
fd: Arc<VmFd>, fd: Arc<VmFd>,
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
msrs: Vec<MsrEntry>, msrs: Vec<MsrEntry>,
#[cfg(all(feature = "sev_snp", target_arch = "x86_64"))] #[cfg(feature = "sev_snp")]
sev_fd: Option<x86_64::sev::SevFd>, sev_fd: Option<x86_64::sev::SevFd>,
#[cfg(all(feature = "sev_snp", target_arch = "x86_64"))] #[cfg(feature = "sev_snp")]
snp_guest_policy: std::sync::OnceLock<u64>, snp_guest_policy: std::sync::OnceLock<u64>,
dirty_log_slots: RwLock<HashMap<u32, KvmDirtyLogSlot>>, dirty_log_slots: RwLock<HashMap<u32, KvmDirtyLogSlot>>,
memory_slots: Option<Arc<RwLock<HashMap<u32, KvmMemorySlot>>>>, memory_slots: Option<Arc<RwLock<HashMap<u32, KvmMemorySlot>>>>,
@@ -700,7 +700,7 @@ impl KvmVm {
/// let vm = hypervisor.create_vm(HypervisorVmConfig::default()).expect("new VM fd creation failed"); /// let vm = hypervisor.create_vm(HypervisorVmConfig::default()).expect("new VM fd creation failed");
/// ``` /// ```
impl vm::Vm for KvmVm { impl vm::Vm for KvmVm {
#[cfg(all(feature = "sev_snp", target_arch = "x86_64"))] #[cfg(feature = "sev_snp")]
fn sev_snp_init(&self, guest_policy: igvm_defs::SnpPolicy) -> vm::Result<()> { fn sev_snp_init(&self, guest_policy: igvm_defs::SnpPolicy) -> vm::Result<()> {
self.sev_fd self.sev_fd
.as_ref() .as_ref()
@@ -713,7 +713,7 @@ impl vm::Vm for KvmVm {
Ok(()) Ok(())
} }
#[cfg(all(feature = "sev_snp", target_arch = "x86_64"))] #[cfg(feature = "sev_snp")]
fn import_isolated_pages( fn import_isolated_pages(
&self, &self,
page_type: u32, page_type: u32,
@@ -751,7 +751,7 @@ impl vm::Vm for KvmVm {
Ok(()) Ok(())
} }
#[cfg(all(feature = "sev_snp", target_arch = "x86_64"))] #[cfg(feature = "sev_snp")]
fn complete_isolated_import( fn complete_isolated_import(
&self, &self,
snp_id_block: igvm_defs::IGVM_VHS_SNP_ID_BLOCK, snp_id_block: igvm_defs::IGVM_VHS_SNP_ID_BLOCK,
+1 -4
View File
@@ -447,10 +447,7 @@ pub struct MshvVcpu {
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
msrs: Vec<MsrEntry>, msrs: Vec<MsrEntry>,
vm_ops: Option<Arc<dyn vm::VmOps>>, vm_ops: Option<Arc<dyn vm::VmOps>>,
#[cfg_attr( #[cfg_attr(not(target_arch = "x86_64"), expect(dead_code))]
all(not(target_arch = "x86_64"), not(feature = "sev_snp")),
expect(dead_code)
)]
vm_fd: Arc<VmFd>, vm_fd: Arc<VmFd>,
#[cfg(feature = "sev_snp")] #[cfg(feature = "sev_snp")]
ghcb: Option<Ghcb>, ghcb: Option<Ghcb>,
+1 -1
View File
@@ -368,7 +368,7 @@ pub enum ValidationError {
#[cfg(feature = "sev_snp")] #[cfg(feature = "sev_snp")]
#[error("Invalid host data format")] #[error("Invalid host data format")]
InvalidHostData, InvalidHostData,
#[cfg(all(feature = "sev_snp", feature = "igvm"))] #[cfg(feature = "sev_snp")]
#[error("SEV-SNP requires an IGVM payload (--payload igvm=<path>)")] #[error("SEV-SNP requires an IGVM payload (--payload igvm=<path>)")]
SevSnpRequiresIgvm, SevSnpRequiresIgvm,
/// Restore expects all net ids that have fds /// Restore expects all net ids that have fds
+13 -67
View File
@@ -41,12 +41,7 @@ use crate::cpu::CpuManager;
use crate::igvm::loader::Loader; use crate::igvm::loader::Loader;
use crate::igvm::{BootPageAcceptance, HV_PAGE_SIZE, IgvmLoadedInfo, StartupMemoryType}; use crate::igvm::{BootPageAcceptance, HV_PAGE_SIZE, IgvmLoadedInfo, StartupMemoryType};
use crate::memory_manager::{Error as MemoryManagerError, MemoryManager}; use crate::memory_manager::{Error as MemoryManagerError, MemoryManager};
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
use crate::sev::{MeasuredBootInfo, SEV_HASH_BLOCK_ADDRESS, SEV_HASH_BLOCK_SIZE}; use crate::sev::{MeasuredBootInfo, SEV_HASH_BLOCK_ADDRESS, SEV_HASH_BLOCK_SIZE};
#[cfg(feature = "sev_snp")] #[cfg(feature = "sev_snp")]
@@ -107,20 +102,10 @@ pub enum Error {
MissingIgvm, MissingIgvm,
#[error("Error applying VMSA to vCPU registers: {0}")] #[error("Error applying VMSA to vCPU registers: {0}")]
SetVmsa(#[source] crate::cpu::Error), SetVmsa(#[source] crate::cpu::Error),
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
#[error("Error building SEV-SNP measured boot hash block")] #[error("Error building SEV-SNP measured boot hash block")]
MeasuredBoot(#[source] vmm_sys_util::errno::Error), MeasuredBoot(#[source] vmm_sys_util::errno::Error),
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
#[error( #[error(
"igvmfile inserts unmeasured parameter area [0x{region_start:x}, 0x{region_end:x}) over SEV-SNP kernel hashes region [0x{hash_start:x}, 0x{hash_end:x})" "igvmfile inserts unmeasured parameter area [0x{region_start:x}, 0x{region_end:x}) over SEV-SNP kernel hashes region [0x{hash_start:x}, 0x{hash_end:x})"
)] )]
@@ -279,13 +264,9 @@ pub fn load_igvm(
memory_manager: Arc<Mutex<MemoryManager>>, memory_manager: Arc<Mutex<MemoryManager>>,
cpu_manager: Arc<Mutex<CpuManager>>, cpu_manager: Arc<Mutex<CpuManager>>,
cmdline: &str, cmdline: &str,
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))] measured_boot: Option<
feature = "kvm", MeasuredBootInfo,
feature = "sev_snp", >,
feature = "fw_cfg",
target_arch = "x86_64"
))]
measured_boot: Option<MeasuredBootInfo>,
#[cfg(feature = "sev_snp")] host_data: &Option<String>, #[cfg(feature = "sev_snp")] host_data: &Option<String>,
) -> Result<Box<IgvmLoadedInfo>, Error> { ) -> Result<Box<IgvmLoadedInfo>, Error> {
let hypervisor_type = cpu_manager.lock().unwrap().hypervisor_type(); let hypervisor_type = cpu_manager.lock().unwrap().hypervisor_type();
@@ -338,12 +319,7 @@ pub fn load_igvm(
let mut loader = Loader::new(memory); let mut loader = Loader::new(memory);
let mut parameter_areas: HashMap<u32, ParameterAreaState> = HashMap::new(); let mut parameter_areas: HashMap<u32, ParameterAreaState> = HashMap::new();
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
let measured_boot_hash_block = if hypervisor_type == HypervisorType::Kvm { let measured_boot_hash_block = if hypervisor_type == HypervisorType::Kvm {
measured_boot measured_boot
.as_ref() .as_ref()
@@ -356,26 +332,11 @@ pub fn load_igvm(
} else { } else {
None None
}; };
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
let measured_boot_hash_page_base = SEV_HASH_BLOCK_ADDRESS / HV_PAGE_SIZE; let measured_boot_hash_page_base = SEV_HASH_BLOCK_ADDRESS / HV_PAGE_SIZE;
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
let measured_boot_hash_offset = (SEV_HASH_BLOCK_ADDRESS % HV_PAGE_SIZE) as usize; let measured_boot_hash_offset = (SEV_HASH_BLOCK_ADDRESS % HV_PAGE_SIZE) as usize;
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
let mut measured_boot_hash_block_inserted = measured_boot_hash_block.is_none(); let mut measured_boot_hash_block_inserted = measured_boot_hash_block.is_none();
for header in igvm_file.directives() { for header in igvm_file.directives() {
@@ -529,12 +490,7 @@ pub fn load_igvm(
.map_err(Error::Loader)?; .map_err(Error::Loader)?;
imported_page = true; imported_page = true;
} }
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
if let Some(hash_block) = measured_boot_hash_block.as_ref().filter(|_| { if let Some(hash_block) = measured_boot_hash_block.as_ref().filter(|_| {
!imported_page && gpa / HV_PAGE_SIZE == measured_boot_hash_page_base !imported_page && gpa / HV_PAGE_SIZE == measured_boot_hash_page_base
}) { }) {
@@ -732,12 +688,7 @@ pub fn load_igvm(
}; };
match area { match area {
ParameterAreaState::Allocated { data, max_size } => { ParameterAreaState::Allocated { data, max_size } => {
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
if measured_boot_hash_block.is_some() { if measured_boot_hash_block.is_some() {
let region_end = *gpa + *max_size; let region_end = *gpa + *max_size;
let hash_end = SEV_HASH_BLOCK_ADDRESS + SEV_HASH_BLOCK_SIZE as u64; let hash_end = SEV_HASH_BLOCK_ADDRESS + SEV_HASH_BLOCK_SIZE as u64;
@@ -794,12 +745,7 @@ pub fn load_igvm(
} }
} }
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
if let Some(hash_block) = measured_boot_hash_block.as_ref() if let Some(hash_block) = measured_boot_hash_block.as_ref()
&& !measured_boot_hash_block_inserted && !measured_boot_hash_block_inserted
{ {
+1 -6
View File
@@ -89,12 +89,7 @@ pub mod migration_transport;
mod pci_segment; mod pci_segment;
pub mod seccomp_filters; pub mod seccomp_filters;
mod serial_manager; mod serial_manager;
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
pub(crate) mod sev; pub(crate) mod sev;
mod sigwinch_listener; mod sigwinch_listener;
mod sync_utils; mod sync_utils;
+7 -31
View File
@@ -105,12 +105,7 @@ use crate::migration::get_vm_snapshot;
#[cfg(all(target_arch = "x86_64", feature = "guest_debug"))] #[cfg(all(target_arch = "x86_64", feature = "guest_debug"))]
use crate::migration::url_to_file; use crate::migration::url_to_file;
use crate::migration::{SNAPSHOT_CONFIG_FILE, SNAPSHOT_STATE_FILE, url_to_path}; use crate::migration::{SNAPSHOT_CONFIG_FILE, SNAPSHOT_STATE_FILE, url_to_path};
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
use crate::sev::MeasuredBootInfo; use crate::sev::MeasuredBootInfo;
#[cfg(feature = "fw_cfg")] #[cfg(feature = "fw_cfg")]
use crate::vm_config::FwCfgConfig; use crate::vm_config::FwCfgConfig;
@@ -1566,13 +1561,9 @@ impl Vm {
igvm_file: IgvmFile, igvm_file: IgvmFile,
memory_manager: Arc<Mutex<MemoryManager>>, memory_manager: Arc<Mutex<MemoryManager>>,
cpu_manager: Arc<Mutex<cpu::CpuManager>>, cpu_manager: Arc<Mutex<cpu::CpuManager>>,
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))] measured_boot: Option<
feature = "kvm", MeasuredBootInfo,
feature = "sev_snp", >,
feature = "fw_cfg",
target_arch = "x86_64"
))]
measured_boot: Option<MeasuredBootInfo>,
#[cfg(feature = "sev_snp")] host_data: &Option<String>, #[cfg(feature = "sev_snp")] host_data: &Option<String>,
) -> Result<EntryPoint> { ) -> Result<EntryPoint> {
// Only reserve bootloader/VMSA regions for KVM + SEV-SNP; other hypervisors // Only reserve bootloader/VMSA regions for KVM + SEV-SNP; other hypervisors
@@ -1589,12 +1580,7 @@ impl Vm {
memory_manager, memory_manager,
cpu_manager.clone(), cpu_manager.clone(),
"", "",
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
measured_boot, measured_boot,
#[cfg(feature = "sev_snp")] #[cfg(feature = "sev_snp")]
host_data, host_data,
@@ -1690,12 +1676,7 @@ impl Vm {
if payload.igvm.is_some() { if payload.igvm.is_some() {
let igvm_file = let igvm_file =
igvm_file.ok_or(Error::IgvmLoad(igvm_loader::Error::MissingIgvm))?; igvm_file.ok_or(Error::IgvmLoad(igvm_loader::Error::MissingIgvm))?;
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
let measured_boot = if let (true, Some(kernel_path), Some(_cmdline)) = ( let measured_boot = if let (true, Some(kernel_path), Some(_cmdline)) = (
payload payload
.fw_cfg_config .fw_cfg_config
@@ -1738,12 +1719,7 @@ impl Vm {
igvm_file, igvm_file,
memory_manager, memory_manager,
cpu_manager, cpu_manager,
#[cfg(all( #[cfg(all(feature = "kvm", feature = "sev_snp", feature = "fw_cfg"))]
feature = "kvm",
feature = "sev_snp",
feature = "fw_cfg",
target_arch = "x86_64"
))]
measured_boot, measured_boot,
#[cfg(feature = "sev_snp")] #[cfg(feature = "sev_snp")]
&payload.host_data, &payload.host_data,