diff options
author | Sonny Rao <sonnyrao@chromium.org> | 2018-02-22 13:26:49 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-02-27 22:26:07 -0800 |
commit | 1aa03e00a0b6cfff12e7298bf91ab55bac9a4012 (patch) | |
tree | 962b8cf1d14c2d82bb0e6df50e58ec67bb51312b | |
parent | c73d39052258a7c40417424ecadabccba6233041 (diff) | |
download | crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.tar crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.tar.gz crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.tar.bz2 crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.tar.lz crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.tar.xz crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.tar.zst crosvm-1aa03e00a0b6cfff12e7298bf91ab55bac9a4012.zip |
x86_64: implement error trait for sub-modules in x86_64 crate
Implement the std::error::Error Trait for Error types within the x86_64 crate. We will make use of these implementations later on when we are using the architecture Trait to pass architecture-specific errors up with meaningful descriptions. BUG=chromium:797868 TEST=./build_test passes on all architectures TEST=crosvm runs on caroline Change-Id: I7a30db69437990608e3a0f5e6e3a200ef6c2d0c3 Reviewed-on: https://chromium-review.googlesource.com/932976 Commit-Ready: Sonny Rao <sonnyrao@chromium.org> Tested-by: Sonny Rao <sonnyrao@chromium.org> Reviewed-by: Sonny Rao <sonnyrao@chromium.org>
-rw-r--r-- | x86_64/src/cpuid.rs | 19 | ||||
-rw-r--r-- | x86_64/src/interrupts.rs | 17 | ||||
-rw-r--r-- | x86_64/src/mptable.rs | 28 | ||||
-rw-r--r-- | x86_64/src/regs.rs | 34 |
4 files changed, 98 insertions, 0 deletions
diff --git a/x86_64/src/cpuid.rs b/x86_64/src/cpuid.rs index a23d24b..f1c7b56 100644 --- a/x86_64/src/cpuid.rs +++ b/x86_64/src/cpuid.rs @@ -3,6 +3,8 @@ // found in the LICENSE file. use std::result; +use std::fmt::{self, Display}; +use std::error::{self, Error as CpuidError}; use kvm; use sys_util; @@ -18,6 +20,23 @@ pub enum Error { } pub type Result<T> = result::Result<T, Error>; +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::GetSupportedCpusFailed(_) => + "GetSupportedCpus ioctl failed", + &Error::SetSupportedCpusFailed(_) => + "SetSupportedCpus ioctl failed", + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "CPUID Error: {}", Error::description(self)) + } +} + // CPUID bits in ebx, ecx, and edx. const EBX_CLFLUSH_CACHELINE: u32 = 8; // Flush a cache line size. const EBX_CLFLUSH_SIZE_SHIFT: u32 = 8; // Bytes flushed when executing CLFLUSH. diff --git a/x86_64/src/interrupts.rs b/x86_64/src/interrupts.rs index 72ba2e9..65bc621 100644 --- a/x86_64/src/interrupts.rs +++ b/x86_64/src/interrupts.rs @@ -5,6 +5,8 @@ use std::io::Cursor; use std::mem; use std::result; +use std::error::{self, Error as InterruptsError}; +use std::fmt::{self, Display}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -19,6 +21,21 @@ pub enum Error { } pub type Result<T> = result::Result<T, Error>; +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::GetLapic(_) => "GetLapic ioctl failed", + &Error::SetLapic(_) => "SetLapic ioctl failed", + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Interrupt Error: {}", Error::description(self)) + } +} + // Defines poached from apicdef.h kernel header. const APIC_LVT0: usize = 0x350; const APIC_LVT1: usize = 0x360; diff --git a/x86_64/src/mptable.rs b/x86_64/src/mptable.rs index fea1622..d433433 100644 --- a/x86_64/src/mptable.rs +++ b/x86_64/src/mptable.rs @@ -6,6 +6,8 @@ use std::io; use std::mem; use std::result; use std::slice; +use std::error::{self, Error as MptableError}; +use std::fmt::{self, Display}; use libc::c_char; @@ -37,6 +39,32 @@ pub enum Error { WriteMpcTable, } +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::NotEnoughMemory => + "There was too little guest memory to store the MP table", + &Error::AddressOverflow => + "The MP table has too little address space to be stored", + &Error::Clear => "Failure while zeroing out the memory for the MP table", + &Error::WriteMpfIntel => "Failure to write the MP floating pointer", + &Error::WriteMpcCpu => "Failure to write MP CPU entry", + &Error::WriteMpcIoapic => "Failure to write MP ioapic entry", + &Error::WriteMpcBus => "Failure to write MP bus entry", + &Error::WriteMpcIntsrc => "Failure to write MP interrupt source entry", + &Error::WriteMpcLintsrc => + "Failure to write MP local interrupt source entry", + &Error::WriteMpcTable => "Failure to write MP table header", + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "MPTable Error: {}", Error::description(self)) + } +} + pub type Result<T> = result::Result<T, Error>; // Convenience macro for making arrays of diverse character types. diff --git a/x86_64/src/regs.rs b/x86_64/src/regs.rs index d43c899..31b6bab 100644 --- a/x86_64/src/regs.rs +++ b/x86_64/src/regs.rs @@ -3,6 +3,8 @@ // found in the LICENSE file. use std::{mem, result}; +use std::error::{self, Error as RegsError}; +use std::fmt::{self, Display}; use kvm; use kvm_sys::kvm_fpu; @@ -37,6 +39,38 @@ pub enum Error { } pub type Result<T> = result::Result<T, Error>; +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::MsrIoctlFailed(_) => + "Setting up msrs failed", + &Error::FpuIoctlFailed(_) => + "Failed to configure the FPU", + &Error::SettingRegistersIoctl(_) => + "Failed to set base registers for this cpu", + &Error::SRegsIoctlFailed(_) => + "Failed to set sregs for this cpu", + &Error::WriteGDTFailure => + "Writing the GDT to RAM failed", + &Error::WriteIDTFailure => + "Writing the IDT to RAM failed", + &Error::WritePML4Address => + "Writing PML4 to RAM failed", + &Error::WritePDPTEAddress => + "Writing PDPTE to RAM failed", + &Error::WritePDEAddress => + "Writing PDE to RAM failed", + + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Interrupt Error: {}", Error::description(self)) + } +} + fn create_msr_entries() -> Vec<kvm_msr_entry> { let mut entries = Vec::<kvm_msr_entry>::new(); |