From 94dd17a46168baf56f13cca2d82b1d261c2ba59f Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Thu, 12 Feb 2026 20:17:23 -0800 Subject: [PATCH 1/3] refactor(snp): convert SnpPageType to const u8 Signed-off-by: Changyuan Lyu --- alioth/src/arch/x86_64/sev.rs | 37 ++++++++++++++++--------------- alioth/src/board/board_x86_64.rs | 8 +++---- alioth/src/hv/kvm/vm/vm_x86_64.rs | 2 +- alioth/src/sys/linux/sev.rs | 4 ++-- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/alioth/src/arch/x86_64/sev.rs b/alioth/src/arch/x86_64/sev.rs index eedf1e7c..dc4c66a8 100644 --- a/alioth/src/arch/x86_64/sev.rs +++ b/alioth/src/arch/x86_64/sev.rs @@ -85,24 +85,25 @@ bitfield! { pub ciphertext_hiding, set_ciphertext_hiding: 24; } -/// AMD SEV-SNP launch update page type. -/// -/// From SEV SNP Firmware ABI Specification, Revision 1.55, Table 67. -#[repr(u8)] -#[derive(Debug, Clone, Copy)] -pub enum SnpPageType { - /// A normal data page. - Normal = 1, - /// A VMSA page. - Vmsa = 2, - /// A page full of zeros. - Zero = 3, - /// A page that is encrypted but not measured. - Unmeasured = 4, - /// A page for the firmware to store secrets for the guest. - Secrets = 5, - /// A page for the hypervisor to provide CPUID function values. - Cpuid = 6, +consts! { + /// AMD SEV-SNP launch update page type. + /// + /// From SEV SNP Firmware ABI Specification, Revision 1.55, Table 67. + #[derive(Default)] + pub struct SnpPageType(u8) { + /// A normal data page. + NORMAL = 1; + /// A VMSA page. + VMSA = 2; + /// A page full of zeros. + ZERO = 3; + /// A page that is encrypted but not measured. + UNMEASURED = 4; + /// A page for the firmware to store secrets for the guest. + SECRETS = 5; + /// A page for the hypervisor to provide CPUID function values. + CPUID = 6; + } } #[repr(C, packed)] diff --git a/alioth/src/board/board_x86_64.rs b/alioth/src/board/board_x86_64.rs index b6b1a33e..8080a2f4 100644 --- a/alioth/src/board/board_x86_64.rs +++ b/alioth/src/board/board_x86_64.rs @@ -230,15 +230,15 @@ where let ram = ram_bus.lock_layout(); let (desc, _) = SevMetadataDesc::read_from_prefix(&fw_range[offset..]).unwrap(); let snp_page_type = match desc.type_ { - SevDescType::SNP_DESC_MEM => SnpPageType::Unmeasured, - SevDescType::SNP_SECRETS => SnpPageType::Secrets, + SevDescType::SNP_DESC_MEM => SnpPageType::UNMEASURED, + SevDescType::SNP_SECRETS => SnpPageType::SECRETS, SevDescType::CPUID => { assert!(desc.len as usize >= size_of::()); assert!(cpuid_table.entries.len() >= self.arch.cpuids.len()); cpuid_table.count = self.arch.cpuids.len() as u32; self.fill_snp_cpuid(&mut cpuid_table.entries); ram.write_t(desc.base as _, &cpuid_table)?; - SnpPageType::Cpuid + SnpPageType::CPUID } _ => unimplemented!(), }; @@ -309,7 +309,7 @@ where self.memory .mark_private_memory(fw_gpa, fw_range.len() as _, true)?; self.vm - .snp_launch_update(fw_range, fw_gpa, SnpPageType::Normal) + .snp_launch_update(fw_range, fw_gpa, SnpPageType::NORMAL) .unwrap(); } Coco::IntelTdx { attr } => todo!("Intel TDX {attr:?}"), diff --git a/alioth/src/hv/kvm/vm/vm_x86_64.rs b/alioth/src/hv/kvm/vm/vm_x86_64.rs index 9af58669..a76b5f5b 100644 --- a/alioth/src/hv/kvm/vm/vm_x86_64.rs +++ b/alioth/src/hv/kvm/vm/vm_x86_64.rs @@ -207,7 +207,7 @@ impl KvmVm { uaddr: range.as_mut_ptr() as _, len: range.len() as _, gfn_start: gpa >> 12, - type_: type_ as _, + type_, ..Default::default() }; self.sev_op(KvmSevCmdId::SNP_LAUNCH_UPDATE, Some(&mut update))?; diff --git a/alioth/src/sys/linux/sev.rs b/alioth/src/sys/linux/sev.rs index 4f18859e..c831dd23 100644 --- a/alioth/src/sys/linux/sev.rs +++ b/alioth/src/sys/linux/sev.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::arch::sev::{SevPolicy, SevStatus, SnpPolicy}; +use crate::arch::sev::{SevPolicy, SevStatus, SnpPageType, SnpPolicy}; use crate::{consts, ioctl_writeread}; consts! { @@ -206,7 +206,7 @@ pub struct KvmSevSnpLaunchUpdate { pub gfn_start: u64, pub uaddr: u64, pub len: u64, - pub type_: u8, + pub type_: SnpPageType, pub pad0: u8, pub flags: u16, pub pad1: u32, From b4fb02bdbe93623bfd91e8bc50455fce7f0c8d42 Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Thu, 12 Feb 2026 20:00:37 -0800 Subject: [PATCH 2/3] refactor(virtio): convert DeviceId to const u16 Signed-off-by: Changyuan Lyu --- alioth-cli/src/boot/boot.rs | 6 ++--- alioth/src/virtio/dev/balloon.rs | 2 +- alioth/src/virtio/dev/blk.rs | 2 +- alioth/src/virtio/dev/entropy.rs | 2 +- alioth/src/virtio/dev/entropy_test.rs | 2 +- alioth/src/virtio/dev/fs/fs.rs | 2 +- alioth/src/virtio/dev/fs/vu.rs | 4 +-- alioth/src/virtio/dev/net/tap.rs | 2 +- alioth/src/virtio/dev/net/vmnet.rs | 2 +- alioth/src/virtio/dev/vsock/uds_vsock.rs | 2 +- alioth/src/virtio/dev/vsock/uds_vsock_test.rs | 2 +- alioth/src/virtio/dev/vsock/vhost_vsock.rs | 2 +- alioth/src/virtio/pci.rs | 12 ++++----- alioth/src/virtio/virtio.rs | 25 ++++++++++--------- 14 files changed, 34 insertions(+), 33 deletions(-) diff --git a/alioth-cli/src/boot/boot.rs b/alioth-cli/src/boot/boot.rs index 2a473273..eba2c29c 100644 --- a/alioth-cli/src/boot/boot.rs +++ b/alioth-cli/src/boot/boot.rs @@ -375,7 +375,7 @@ fn create(hypervisor: &H, config: Config) -> Result, a #[cfg(target_os = "linux")] NetParam::Vu(sock) => { let param = VuFrontendParam { - id: DeviceId::Net, + id: DeviceId::NET, socket: sock.socket, }; vm.add_virtio_dev(format!("vu-net-{index}"), param) @@ -391,7 +391,7 @@ fn create(hypervisor: &H, config: Config) -> Result, a #[cfg(target_os = "linux")] BlkParam::Vu(s) => { let p = VuFrontendParam { - id: DeviceId::Block, + id: DeviceId::BLOCK, socket: s.socket, }; vm.add_virtio_dev(format!("vu-net-{index}"), p) @@ -415,7 +415,7 @@ fn create(hypervisor: &H, config: Config) -> Result, a #[cfg(target_os = "linux")] VsockParam::Vu(s) => { let p = VuFrontendParam { - id: DeviceId::Socket, + id: DeviceId::SOCKET, socket: s.socket, }; vm.add_virtio_dev("vu-vsock", p) diff --git a/alioth/src/virtio/dev/balloon.rs b/alioth/src/virtio/dev/balloon.rs index 055b8a15..062edafb 100644 --- a/alioth/src/virtio/dev/balloon.rs +++ b/alioth/src/virtio/dev/balloon.rs @@ -190,7 +190,7 @@ impl Virtio for Balloon { type Feature = BalloonFeature; fn id(&self) -> DeviceId { - DeviceId::Balloon + DeviceId::BALLOON } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/blk.rs b/alioth/src/virtio/dev/blk.rs index ef25bd6e..aa97dc33 100644 --- a/alioth/src/virtio/dev/blk.rs +++ b/alioth/src/virtio/dev/blk.rs @@ -285,7 +285,7 @@ impl Virtio for Block { type Feature = BlockFeature; fn id(&self) -> DeviceId { - DeviceId::Block + DeviceId::BLOCK } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/entropy.rs b/alioth/src/virtio/dev/entropy.rs index be640a2e..92f4b703 100644 --- a/alioth/src/virtio/dev/entropy.rs +++ b/alioth/src/virtio/dev/entropy.rs @@ -85,7 +85,7 @@ impl Virtio for Entropy { type Feature = EntropyFeature; fn id(&self) -> DeviceId { - DeviceId::Entropy + DeviceId::ENTROPY } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/entropy_test.rs b/alioth/src/virtio/dev/entropy_test.rs index 90d58285..05a6c5dc 100644 --- a/alioth/src/virtio/dev/entropy_test.rs +++ b/alioth/src/virtio/dev/entropy_test.rs @@ -72,7 +72,7 @@ fn entropy_test(fixture_ram_bus: RamBus, fixture_queues: Box<[QueueReg]>) { }; let dev = param.build("entropy").unwrap(); - assert_matches!(dev.id(), DeviceId::Entropy); + assert_matches!(dev.id(), DeviceId::ENTROPY); assert_eq!(dev.name(), "entropy"); assert_eq!(dev.num_queues(), 1); assert_matches!(*dev.config(), EntropyConfig); diff --git a/alioth/src/virtio/dev/fs/fs.rs b/alioth/src/virtio/dev/fs/fs.rs index e7246d55..6f30bf56 100644 --- a/alioth/src/virtio/dev/fs/fs.rs +++ b/alioth/src/virtio/dev/fs/fs.rs @@ -399,7 +399,7 @@ where type Feature = FsFeature; fn id(&self) -> DeviceId { - DeviceId::FileSystem + DeviceId::FILE_SYSTEM } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/fs/vu.rs b/alioth/src/virtio/dev/fs/vu.rs index 67d12665..f9502e21 100644 --- a/alioth/src/virtio/dev/fs/vu.rs +++ b/alioth/src/virtio/dev/fs/vu.rs @@ -64,7 +64,7 @@ impl VuFs { if param.tag.is_none() { extra_features |= VuFeature::CONFIG; } - let frontend = VuFrontend::new(name, ¶m.socket, DeviceId::FileSystem, extra_features)?; + let frontend = VuFrontend::new(name, ¶m.socket, DeviceId::FILE_SYSTEM, extra_features)?; let config = if let Some(tag) = param.tag { assert!(tag.len() <= 36); assert_ne!(tag.len(), 0); @@ -131,7 +131,7 @@ impl Virtio for VuFs { type Feature = FsFeature; fn id(&self) -> DeviceId { - DeviceId::FileSystem + DeviceId::FILE_SYSTEM } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/net/tap.rs b/alioth/src/virtio/dev/net/tap.rs index 5649c411..4e666ddc 100644 --- a/alioth/src/virtio/dev/net/tap.rs +++ b/alioth/src/virtio/dev/net/tap.rs @@ -203,7 +203,7 @@ impl Virtio for Net { type Feature = NetFeature; fn id(&self) -> DeviceId { - DeviceId::Net + DeviceId::NET } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/net/vmnet.rs b/alioth/src/virtio/dev/net/vmnet.rs index cd510a4d..d0c9ae63 100644 --- a/alioth/src/virtio/dev/net/vmnet.rs +++ b/alioth/src/virtio/dev/net/vmnet.rs @@ -236,7 +236,7 @@ impl Virtio for Net { type Feature = NetFeature; fn id(&self) -> DeviceId { - DeviceId::Net + DeviceId::NET } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/vsock/uds_vsock.rs b/alioth/src/virtio/dev/vsock/uds_vsock.rs index 878a6021..72c3cecf 100644 --- a/alioth/src/virtio/dev/vsock/uds_vsock.rs +++ b/alioth/src/virtio/dev/vsock/uds_vsock.rs @@ -695,7 +695,7 @@ impl Virtio for UdsVsock { type Feature = VsockFeature; fn id(&self) -> DeviceId { - DeviceId::Socket + DeviceId::SOCKET } fn name(&self) -> &str { diff --git a/alioth/src/virtio/dev/vsock/uds_vsock_test.rs b/alioth/src/virtio/dev/vsock/uds_vsock_test.rs index 10c200d5..ad653549 100644 --- a/alioth/src/virtio/dev/vsock/uds_vsock_test.rs +++ b/alioth/src/virtio/dev/vsock/uds_vsock_test.rs @@ -124,7 +124,7 @@ fn vsock_conn_test(fixture_ram_bus: RamBus, #[with(3)] fixture_queues: Box<[Queu }; let dev = param.build("vsock").unwrap(); - assert_matches!(dev.id(), DeviceId::Socket); + assert_matches!(dev.id(), DeviceId::SOCKET); assert_eq!(dev.name(), "vsock"); assert_eq!(dev.num_queues(), 3); assert_eq!(dev.config().guest_cid, GUEST_CID as u32); diff --git a/alioth/src/virtio/dev/vsock/vhost_vsock.rs b/alioth/src/virtio/dev/vsock/vhost_vsock.rs index dc37a357..25a95910 100644 --- a/alioth/src/virtio/dev/vsock/vhost_vsock.rs +++ b/alioth/src/virtio/dev/vsock/vhost_vsock.rs @@ -104,7 +104,7 @@ impl Virtio for VhostVsock { type Feature = VsockFeature; fn id(&self) -> DeviceId { - DeviceId::Socket + DeviceId::SOCKET } fn name(&self) -> &str { diff --git a/alioth/src/virtio/pci.rs b/alioth/src/virtio/pci.rs index a1444746..ae472fa8 100644 --- a/alioth/src/virtio/pci.rs +++ b/alioth/src/virtio/pci.rs @@ -576,10 +576,10 @@ const VIRTIO_DEVICE_ID_BASE: u16 = 0x1040; fn get_class(id: DeviceId) -> (u8, u8) { match id { - DeviceId::Net => (0x02, 0x00), - DeviceId::FileSystem => (0x01, 0x80), - DeviceId::Block => (0x01, 0x00), - DeviceId::Socket => (0x02, 0x80), + DeviceId::NET => (0x02, 0x00), + DeviceId::FILE_SYSTEM => (0x01, 0x80), + DeviceId::BLOCK => (0x01, 0x00), + DeviceId::SOCKET => (0x02, 0x80), _ => (0xff, 0x00), } } @@ -690,14 +690,14 @@ where let mut header = DeviceHeader { common: CommonHeader { vendor: VIRTIO_VENDOR_ID, - device: VIRTIO_DEVICE_ID_BASE + dev.id as u16, + device: VIRTIO_DEVICE_ID_BASE + dev.id.raw(), revision: 0x1, header_type: HeaderType::DEVICE, class, subclass, ..Default::default() }, - subsystem: VIRTIO_DEVICE_ID_BASE + dev.id as u16, + subsystem: VIRTIO_DEVICE_ID_BASE + dev.id.raw(), ..Default::default() }; let device_config = dev.device_config.clone(); diff --git a/alioth/src/virtio/virtio.rs b/alioth/src/virtio/virtio.rs index 51ee5dd7..08f5a0be 100644 --- a/alioth/src/virtio/virtio.rs +++ b/alioth/src/virtio/virtio.rs @@ -31,8 +31,8 @@ use std::path::Path; use snafu::Snafu; -use crate::bitflags; use crate::errors::{DebugTrace, trace_error}; +use crate::{bitflags, consts}; #[trace_error] #[derive(Snafu, DebugTrace)] @@ -99,17 +99,18 @@ const FEATURE_BUILT_IN: u128 = VirtioFeature::EVENT_IDX.bits() | VirtioFeature::RING_PACKED.bits() | VirtioFeature::VERSION_1.bits(); -#[derive(Debug, Clone, Copy)] -pub enum DeviceId { - Net = 1, - Block = 2, - Entropy = 4, - Balloon = 5, - Socket = 19, - Iommu = 23, - Mem = 24, - FileSystem = 26, - Pmem = 27, +consts! { + pub struct DeviceId(u16) { + NET = 1; + BLOCK = 2; + ENTROPY = 4; + BALLOON = 5; + SOCKET = 19; + IOMMU = 23; + MEM = 24; + FILE_SYSTEM = 26; + PMEM = 27; + } } bitflags! { From e7ababb7f0dd8104a136674715e5726247db7f6b Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Thu, 12 Feb 2026 20:24:20 -0800 Subject: [PATCH 3/3] refactor(virtio): convert VirtioPciCfg to const u8 Signed-off-by: Changyuan Lyu --- alioth/src/virtio/pci.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/alioth/src/virtio/pci.rs b/alioth/src/virtio/pci.rs index ae472fa8..69dec1d9 100644 --- a/alioth/src/virtio/pci.rs +++ b/alioth/src/virtio/pci.rs @@ -42,7 +42,7 @@ use crate::utils::{get_atomic_high32, get_atomic_low32, set_atomic_high32, set_a use crate::virtio::dev::{Register, StartParam, VirtioDevice, WakeEvent}; use crate::virtio::queue::QueueReg; use crate::virtio::{DevStatus, DeviceId, IrqSender, Result, error}; -use crate::{impl_mmio_for_zerocopy, mem}; +use crate::{consts, impl_mmio_for_zerocopy, mem}; const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff; @@ -584,15 +584,17 @@ fn get_class(id: DeviceId) -> (u8, u8) { } } -#[repr(u8)] -pub enum VirtioPciCfg { - Common = 1, - Notify = 2, - Isr = 3, - Device = 4, - Pci = 5, - SharedMemory = 8, - Vendor = 9, +consts! { + #[derive(Default, FromZeros, Immutable, IntoBytes)] + pub struct VirtioPciCfg(u8) { + COMMON = 1; + NOTIFY = 2; + ISR = 3; + DEVICE = 4; + PCI = 5; + SHARED_MEMORY = 8; + VENDOR = 9; + } } #[repr(C, align(4))] @@ -600,7 +602,7 @@ pub enum VirtioPciCfg { pub struct VirtioPciCap { header: PciCapHdr, cap_len: u8, - cfg_type: u8, + cfg_type: VirtioPciCfg, bar: u8, id: u8, padding: [u8; 2], @@ -730,7 +732,7 @@ where ..Default::default() }, cap_len: size_of::() as u8, - cfg_type: VirtioPciCfg::Common as u8, + cfg_type: VirtioPciCfg::COMMON, bar: 0, id: 0, offset: (virtio_register_offset + VirtioPciRegister::OFFSET_COMMON) as u32, @@ -743,7 +745,7 @@ where ..Default::default() }, cap_len: size_of::() as u8, - cfg_type: VirtioPciCfg::Isr as u8, + cfg_type: VirtioPciCfg::ISR, bar: 0, id: 0, offset: (virtio_register_offset + VirtioPciRegister::OFFSET_ISR_STATUS) as u32, @@ -757,7 +759,7 @@ where ..Default::default() }, cap_len: size_of::() as u8, - cfg_type: VirtioPciCfg::Notify as u8, + cfg_type: VirtioPciCfg::NOTIFY, bar: 0, id: 0, offset: (virtio_register_offset + VirtioPciRegister::OFFSET_QUEUE_NOTIFY) as u32, @@ -772,7 +774,7 @@ where ..Default::default() }, cap_len: size_of::() as u8, - cfg_type: VirtioPciCfg::Device as u8, + cfg_type: VirtioPciCfg::DEVICE, bar: 0, id: 0, offset: device_config_offset as u32, @@ -814,7 +816,7 @@ where ..Default::default() }, cap_len: size_of::() as u8, - cfg_type: VirtioPciCfg::SharedMemory as u8, + cfg_type: VirtioPciCfg::SHARED_MEMORY, bar: 2, id: index as u8, offset: offset as u32,