From 193c006669ee98d9efe6a3e03d77f36fcff62e76 Mon Sep 17 00:00:00 2001 From: SamrutGadde Date: Wed, 1 May 2024 18:49:50 -0500 Subject: [PATCH] arch: Use thiserror for errors Added thiserror crate for missing files in the arch package Signed-off-by: SamrutGadde --- arch/src/aarch64/fdt.rs | 4 +++- arch/src/aarch64/mod.rs | 10 +++++++++- arch/src/aarch64/uefi.rs | 7 ++++++- arch/src/x86_64/mod.rs | 22 +++++++++++++++++++++- arch/src/x86_64/mptable.rs | 14 +++++++++++++- arch/src/x86_64/regs.rs | 15 ++++++++++++++- arch/src/x86_64/smbios.rs | 33 ++++++++------------------------- 7 files changed, 74 insertions(+), 31 deletions(-) diff --git a/arch/src/aarch64/fdt.rs b/arch/src/aarch64/fdt.rs index af2695d01..f66d93b5d 100644 --- a/arch/src/aarch64/fdt.rs +++ b/arch/src/aarch64/fdt.rs @@ -26,6 +26,7 @@ use super::layout::{ }; use std::fs; use std::path::Path; +use thiserror::Error; use vm_fdt::{FdtWriter, FdtWriterResult}; use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryError, GuestMemoryRegion}; @@ -80,9 +81,10 @@ pub trait DeviceInfoForFdt { } /// Errors thrown while configuring the Flattened Device Tree for aarch64. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Failure in writing FDT in memory. + #[error("Failure in writing FDT in memory: {0}")] WriteFdtToMemory(GuestMemoryError), } type Result = result::Result; diff --git a/arch/src/aarch64/mod.rs b/arch/src/aarch64/mod.rs index 7b1102826..ce35956b8 100644 --- a/arch/src/aarch64/mod.rs +++ b/arch/src/aarch64/mod.rs @@ -18,32 +18,40 @@ use log::{log_enabled, Level}; use std::collections::HashMap; use std::fmt::Debug; use std::sync::{Arc, Mutex}; +use thiserror::Error; use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryAtomic}; pub const _NSIG: i32 = 65; /// Errors thrown while configuring aarch64 system. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Failed to create a FDT. + #[error("Failed to create a FDT")] SetupFdt, /// Failed to write FDT to memory. + #[error("Failed to write FDT to memory: {0}")] WriteFdtToMemory(fdt::Error), /// Failed to create a GIC. + #[error("Failed to create a GIC")] SetupGic, /// Failed to compute the initramfs address. + #[error("Failed to compute the initramfs address")] InitramfsAddress, /// Error configuring the general purpose registers + #[error("Error configuring the general purpose registers: {0}")] RegsConfiguration(hypervisor::HypervisorCpuError), /// Error configuring the MPIDR register + #[error("Error configuring the MPIDR register: {0}")] VcpuRegMpidr(hypervisor::HypervisorCpuError), /// Error initializing PMU for vcpu + #[error("Error initializing PMU for vcpu")] VcpuInitPmu, } diff --git a/arch/src/aarch64/uefi.rs b/arch/src/aarch64/uefi.rs index a920acb0c..60c0e8693 100644 --- a/arch/src/aarch64/uefi.rs +++ b/arch/src/aarch64/uefi.rs @@ -5,18 +5,23 @@ use std::io::{Read, Seek, SeekFrom}; use std::os::fd::AsFd; use std::result; +use thiserror::Error; use vm_memory::{GuestAddress, GuestMemory}; /// Errors thrown while loading UEFI binary -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Unable to seek to UEFI image start. + #[error("Unable to seek to UEFI image start")] SeekUefiStart, /// Unable to seek to UEFI image end. + #[error("Unable to seek to UEFI image end")] SeekUefiEnd, /// UEFI image too big. + #[error("UEFI image too big")] UefiTooBig, /// Unable to read UEFI image + #[error("Unable to read UEFI image")] ReadUefiImage, } type Result = result::Result; diff --git a/arch/src/x86_64/mod.rs b/arch/src/x86_64/mod.rs index 896a74d23..16dc2eb46 100644 --- a/arch/src/x86_64/mod.rs +++ b/arch/src/x86_64/mod.rs @@ -23,6 +23,7 @@ use linux_loader::loader::elf::start_info::{ }; use std::collections::BTreeMap; use std::mem; +use thiserror::Error; use vm_memory::{ Address, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, GuestMemoryRegion, GuestUsize, @@ -127,64 +128,83 @@ pub struct CpuidConfig { pub amx: bool, } -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Error writing MP table to memory. + #[error("Error writing MP table to memory: {0}")] MpTableSetup(mptable::Error), /// Error configuring the general purpose registers + #[error("Error configuring the general purpose registers: {0}")] RegsConfiguration(regs::Error), /// Error configuring the special registers + #[error("Error configuring the special registers: {0}")] SregsConfiguration(regs::Error), /// Error configuring the floating point related registers + #[error("Error configuring the floating point related registers: {0}")] FpuConfiguration(regs::Error), /// Error configuring the MSR registers + #[error("Error configuring the MSR registers: {0}")] MsrsConfiguration(regs::Error), /// Failed to set supported CPUs. + #[error("Failed to set supported CPUs: {0}")] SetSupportedCpusFailed(anyhow::Error), /// Cannot set the local interruption due to bad configuration. + #[error("Cannot set the local interruption due to bad configuration: {0}")] LocalIntConfiguration(anyhow::Error), /// Error setting up SMBIOS table + #[error("Error setting up SMBIOS table: {0}")] SmbiosSetup(smbios::Error), /// Could not find any SGX EPC section + #[error("Could not find any SGX EPC section")] NoSgxEpcSection, /// Missing SGX CPU feature + #[error("Missing SGX CPU feature")] MissingSgxFeature, /// Missing SGX_LC CPU feature + #[error("Missing SGX_LC CPU feature")] MissingSgxLaunchControlFeature, /// Error getting supported CPUID through the hypervisor (kvm/mshv) API + #[error("Error getting supported CPUID through the hypervisor API: {0}")] CpuidGetSupported(HypervisorError), /// Error populating CPUID with KVM HyperV emulation details + #[error("Error populating CPUID with KVM HyperV emulation details: {0}")] CpuidKvmHyperV(vmm_sys_util::fam::Error), /// Error populating CPUID with CPU identification + #[error("Error populating CPUID with CPU identification: {0}")] CpuidIdentification(vmm_sys_util::fam::Error), /// Error checking CPUID compatibility + #[error("Error checking CPUID compatibility")] CpuidCheckCompatibility, // Error writing EBDA address + #[error("Error writing EBDA address: {0}")] EbdaSetup(vm_memory::GuestMemoryError), // Error getting CPU TSC frequency + #[error("Error getting CPU TSC frequency: {0}")] GetTscFrequency(HypervisorCpuError), /// Error retrieving TDX capabilities through the hypervisor (kvm/mshv) API #[cfg(feature = "tdx")] + #[error("Error retrieving TDX capabilities through the hypervisor API: {0}")] TdxCapabilities(HypervisorError), /// Failed to configure E820 map for bzImage + #[error("Failed to configure E820 map for bzImage")] E820Configuration, } diff --git a/arch/src/x86_64/mptable.rs b/arch/src/x86_64/mptable.rs index 9b683fc78..433d90dc8 100644 --- a/arch/src/x86_64/mptable.rs +++ b/arch/src/x86_64/mptable.rs @@ -12,6 +12,7 @@ use libc::c_uchar; use std::mem; use std::result; use std::slice; +use thiserror::Error; use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError}; // This is a workaround to the Rust enforcement specifying that any implementation of a foreign @@ -49,29 +50,40 @@ unsafe impl ByteValued for MpcLintsrcWrapper {} // SAFETY: see above unsafe impl ByteValued for MpfIntelWrapper {} -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// There was too little guest memory to store the entire MP table. + #[error("There was too little guest memory to store the entire MP table")] NotEnoughMemory, /// The MP table has too little address space to be stored. + #[error("The MP table has too little address space to be stored")] AddressOverflow, /// Failure while zeroing out the memory for the MP table. + #[error("Failure while zeroing out the memory for the MP table: {0}")] Clear(GuestMemoryError), /// Number of CPUs exceeds the maximum supported CPUs + #[error("Number of CPUs exceeds the maximum supported CPUs")] TooManyCpus, /// Failure to write the MP floating pointer. + #[error("Failure to write the MP floating pointer: {0}")] WriteMpfIntel(GuestMemoryError), /// Failure to write MP CPU entry. + #[error("Failure to write MP CPU entry: {0}")] WriteMpcCpu(GuestMemoryError), /// Failure to write MP ioapic entry. + #[error("Failure to write MP ioapic entry: {0}")] WriteMpcIoapic(GuestMemoryError), /// Failure to write MP bus entry. + #[error("Failure to write MP bus entry: {0}")] WriteMpcBus(GuestMemoryError), /// Failure to write MP interrupt source entry. + #[error("Failure to write MP interrupt source entry: {0}")] WriteMpcIntsrc(GuestMemoryError), /// Failure to write MP local interrupt source entry. + #[error("Failure to write MP local interrupt source entry: {0}")] WriteMpcLintsrc(GuestMemoryError), /// Failure to write MP table header. + #[error("Failure to write MP table header: {0}")] WriteMpcTable(GuestMemoryError), } diff --git a/arch/src/x86_64/regs.rs b/arch/src/x86_64/regs.rs index 16db5621c..e41d7b156 100644 --- a/arch/src/x86_64/regs.rs +++ b/arch/src/x86_64/regs.rs @@ -15,33 +15,46 @@ use hypervisor::arch::x86::regs::CR0_PE; use hypervisor::arch::x86::{FpuState, SpecialRegisters, StandardRegisters}; use std::sync::Arc; use std::{mem, result}; +use thiserror::Error; use vm_memory::{Address, Bytes, GuestMemory, GuestMemoryError}; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// Failed to get SREGs for this CPU. + #[error("Failed to get SREGs for this CPU: {0}")] GetStatusRegisters(hypervisor::HypervisorCpuError), /// Failed to set base registers for this CPU. + #[error("Failed to set base registers for this CPU: {0}")] SetBaseRegisters(hypervisor::HypervisorCpuError), /// Failed to configure the FPU. + #[error("Failed to configure the FPU: {0}")] SetFpuRegisters(hypervisor::HypervisorCpuError), /// Setting up MSRs failed. + #[error("Setting up MSRs failed: {0}")] SetModelSpecificRegisters(hypervisor::HypervisorCpuError), /// Failed to set SREGs for this CPU. + #[error("Failed to set SREGs for this CPU: {0}")] SetStatusRegisters(hypervisor::HypervisorCpuError), /// Checking the GDT address failed. + #[error("Checking the GDT address failed")] CheckGdtAddr, /// Writing the GDT to RAM failed. + #[error("Writing the GDT to RAM failed: {0}")] WriteGdt(GuestMemoryError), /// Writing the IDT to RAM failed. + #[error("Writing the IDT to RAM failed: {0}")] WriteIdt(GuestMemoryError), /// Writing PDPTE to RAM failed. + #[error("Writing PDPTE to RAM failed: {0}")] WritePdpteAddress(GuestMemoryError), /// Writing PDE to RAM failed. + #[error("Writing PDE to RAM failed: {0}")] WritePdeAddress(GuestMemoryError), /// Writing PML4 to RAM failed. + #[error("Writing PML4 to RAM failed: {0}")] WritePml4Address(GuestMemoryError), /// Writing PML5 to RAM failed. + #[error("Writing PML5 to RAM failed: {0}")] WritePml5Address(GuestMemoryError), } diff --git a/arch/src/x86_64/smbios.rs b/arch/src/x86_64/smbios.rs index ef083dc8c..7d2e152b1 100644 --- a/arch/src/x86_64/smbios.rs +++ b/arch/src/x86_64/smbios.rs @@ -8,53 +8,36 @@ use crate::layout::SMBIOS_START; use crate::GuestMemoryMmap; -use std::fmt::{self, Display}; use std::mem; use std::result; use std::slice; +use thiserror::Error; use uuid::Uuid; use vm_memory::ByteValued; use vm_memory::{Address, Bytes, GuestAddress}; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum Error { /// There was too little guest memory to store the entire SMBIOS table. + #[error("There was too little guest memory to store the SMBIOS table")] NotEnoughMemory, /// The SMBIOS table has too little address space to be stored. + #[error("The SMBIOS table has too little address space to be stored")] AddressOverflow, /// Failure while zeroing out the memory for the SMBIOS table. + #[error("Failure while zeroing out the memory for the SMBIOS table")] Clear, /// Failure to write SMBIOS entrypoint structure + #[error("Failure to write SMBIOS entrypoint structure")] WriteSmbiosEp, /// Failure to write additional data to memory + #[error("Failure to write additional data to memory")] WriteData, /// Failure to parse uuid, uuid format may be error + #[error("Failure to parse uuid: {0}")] ParseUuid(uuid::Error), } -impl std::error::Error for Error {} - -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use self::Error::*; - - let description = match self { - NotEnoughMemory => { - "There was too little guest memory to store the SMBIOS table".to_string() - } - AddressOverflow => { - "The SMBIOS table has too little address space to be stored".to_string() - } - Clear => "Failure while zeroing out the memory for the SMBIOS table".to_string(), - WriteSmbiosEp => "Failure to write SMBIOS entrypoint structure".to_string(), - WriteData => "Failure to write additional data to memory".to_string(), - ParseUuid(e) => format!("Failure to parse uuid: {e}"), - }; - - write!(f, "SMBIOS error: {description}") - } -} - pub type Result = result::Result; // Constants sourced from SMBIOS Spec 3.2.0.