From d96de000f2b9acaf50f79856082b7602bd4ae5fe Mon Sep 17 00:00:00 2001 From: Oliver Anderson Date: Mon, 13 Apr 2026 11:04:18 +0200 Subject: [PATCH] arch: CpuProfile enum Introduce a CpuProfile enum that will be deserialized from the user's selected CPU profile. Currently we only have a "host" variant, but in the future there will be a build script automatically constructing this enum based on pre-generated CPU profiles. Signed-off-by: Oliver Anderson On-behalf-of: SAP oliver.anderson@sap.com --- arch/src/lib.rs | 27 +++++++++++++++++++++++++-- arch/src/x86_64/cpu_profile/mod.rs | 10 ++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/arch/src/lib.rs b/arch/src/lib.rs index c1c197366..79561f098 100644 --- a/arch/src/lib.rs +++ b/arch/src/lib.rs @@ -9,9 +9,11 @@ //! Supported platforms: x86_64, aarch64, riscv64. use std::collections::BTreeMap; +use std::str::FromStr; use std::sync::Arc; use std::{fmt, result}; +use serde::de::IntoDeserializer; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -53,6 +55,26 @@ pub enum Error { /// Type for returning public functions outcome. pub type Result = result::Result; +// If the target_arch is x86_64 we import CpuProfile from the x86_64 module, otherwise we +// declare it here with only "host" as a selectable CPU profile. This trick is useful to prevent +// excessive conditional compilation throughout the codebase. +#[cfg(not(target_arch = "x86_64"))] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +/// A [`CpuProfile`] is a mechanism for ensuring live migration compatibility +/// between host's with potentially different CPU models. +pub enum CpuProfile { + #[default] + Host, +} + +// Note that this trait impl is architecture agnostic and may thus reside here. +impl FromStr for CpuProfile { + type Err = serde::de::value::Error; + fn from_str(s: &str) -> result::Result { + Self::deserialize(s.into_deserializer()) + } +} + /// Type for memory region types. #[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum RegionType { @@ -100,8 +122,9 @@ pub mod x86_64; #[cfg(target_arch = "x86_64")] pub use x86_64::{ _NSIG, CpuidConfig, CpuidFeatureEntry, EntryPoint, arch_memory_regions, configure_system, - configure_vcpu, generate_common_cpuid, generate_ram_ranges, get_host_cpu_phys_bits, - initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE, layout::CMDLINE_START, regs, + configure_vcpu, cpu_profile::CpuProfile, generate_common_cpuid, generate_ram_ranges, + get_host_cpu_phys_bits, initramfs_load_addr, layout, layout::CMDLINE_MAX_SIZE, + layout::CMDLINE_START, regs, }; /// Safe wrapper for `sysconf(_SC_PAGESIZE)`. diff --git a/arch/src/x86_64/cpu_profile/mod.rs b/arch/src/x86_64/cpu_profile/mod.rs index a2780443c..3bb6bdd7d 100644 --- a/arch/src/x86_64/cpu_profile/mod.rs +++ b/arch/src/x86_64/cpu_profile/mod.rs @@ -4,3 +4,13 @@ // pub mod cpuid_adjustments; + +// TODO: Auto generate the CpuProfile enum with a build script once we introduce user facing CPU profiles. + +/// A [`CpuProfile`] is a mechanism for ensuring live migration compatibility +/// between hosts with potentially different CPU models. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +pub enum CpuProfile { + #[default] + Host, +}