mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-01-03 11:25:20 +00:00
hypervisor: simplify LapicState
Both KVM and MSHV share the same layout. We can drop one level of indirection. Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
parent
ddc9004471
commit
bec47ebcc9
@ -252,34 +252,51 @@ pub struct FpuState {
|
|||||||
pub mxcsr: u32,
|
pub mxcsr: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum LapicState {
|
pub struct LapicState {
|
||||||
#[cfg(feature = "kvm")]
|
pub(crate) regs: [::std::os::raw::c_char; 1024usize],
|
||||||
Kvm(kvm_bindings::kvm_lapic_state),
|
}
|
||||||
#[cfg(feature = "mshv")]
|
|
||||||
Mshv(mshv_bindings::LapicState),
|
impl Default for LapicState {
|
||||||
|
fn default() -> Self {
|
||||||
|
// SAFETY: this is plain old data structure
|
||||||
|
unsafe { ::std::mem::zeroed() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for LapicState {
|
||||||
|
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let regs: Vec<::std::os::raw::c_char> = Vec::deserialize(deserializer)?;
|
||||||
|
let mut val = LapicState::default();
|
||||||
|
// This panics if the source and destination have different lengths.
|
||||||
|
val.regs.copy_from_slice(®s[..]);
|
||||||
|
Ok(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for LapicState {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
let regs = &self.regs[..];
|
||||||
|
regs.serialize(serializer)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(feature = "kvm", feature = "mshv"))]
|
|
||||||
impl LapicState {
|
impl LapicState {
|
||||||
pub fn get_klapic_reg(&self, reg_offset: usize) -> u32 {
|
pub fn get_klapic_reg(&self, reg_offset: usize) -> u32 {
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
let sliceu8 = match self {
|
let sliceu8 = unsafe {
|
||||||
#[cfg(feature = "kvm")]
|
|
||||||
LapicState::Kvm(s) => unsafe {
|
|
||||||
// This array is only accessed as parts of a u32 word, so interpret it as a u8 array.
|
// This array is only accessed as parts of a u32 word, so interpret it as a u8 array.
|
||||||
// Cursors are only readable on arrays of u8, not i8(c_char).
|
// Cursors are only readable on arrays of u8, not i8(c_char).
|
||||||
mem::transmute::<&[i8], &[u8]>(&s.regs[reg_offset..])
|
mem::transmute::<&[i8], &[u8]>(&self.regs[reg_offset..])
|
||||||
},
|
|
||||||
#[cfg(feature = "mshv")]
|
|
||||||
LapicState::Mshv(s) => unsafe {
|
|
||||||
// This array is only accessed as parts of a u32 word, so interpret it as a u8 array.
|
|
||||||
// Cursors are only readable on arrays of u8, not i8(c_char).
|
|
||||||
mem::transmute::<&[i8], &[u8]>(&s.regs[reg_offset..])
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut reader = Cursor::new(sliceu8);
|
let mut reader = Cursor::new(sliceu8);
|
||||||
@ -294,19 +311,10 @@ impl LapicState {
|
|||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
let sliceu8 = match self {
|
let sliceu8 = unsafe {
|
||||||
#[cfg(feature = "kvm")]
|
|
||||||
LapicState::Kvm(s) => unsafe {
|
|
||||||
// This array is only accessed as parts of a u32 word, so interpret it as a u8 array.
|
// This array is only accessed as parts of a u32 word, so interpret it as a u8 array.
|
||||||
// Cursors are only readable on arrays of u8, not i8(c_char).
|
// Cursors are only readable on arrays of u8, not i8(c_char).
|
||||||
mem::transmute::<&mut [i8], &mut [u8]>(&mut s.regs[reg_offset..])
|
mem::transmute::<&mut [i8], &mut [u8]>(&mut self.regs[reg_offset..])
|
||||||
},
|
|
||||||
#[cfg(feature = "mshv")]
|
|
||||||
LapicState::Mshv(s) => unsafe {
|
|
||||||
// This array is only accessed as parts of a u32 word, so interpret it as a u8 array.
|
|
||||||
// Cursors are only readable on arrays of u8, not i8(c_char).
|
|
||||||
mem::transmute::<&mut [i8], &mut [u8]>(&mut s.regs[reg_offset..])
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut writer = Cursor::new(sliceu8);
|
let mut writer = Cursor::new(sliceu8);
|
||||||
|
@ -298,18 +298,13 @@ impl From<FpuState> for kvm_fpu {
|
|||||||
|
|
||||||
impl From<LapicState> for kvm_lapic_state {
|
impl From<LapicState> for kvm_lapic_state {
|
||||||
fn from(s: LapicState) -> Self {
|
fn from(s: LapicState) -> Self {
|
||||||
match s {
|
Self { regs: s.regs }
|
||||||
LapicState::Kvm(s) => s,
|
|
||||||
/* Needed in case other hypervisors are enabled */
|
|
||||||
#[allow(unreachable_patterns)]
|
|
||||||
_ => panic!("LapicState is not valid"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<kvm_lapic_state> for LapicState {
|
impl From<kvm_lapic_state> for LapicState {
|
||||||
fn from(s: kvm_lapic_state) -> Self {
|
fn from(s: kvm_lapic_state) -> Self {
|
||||||
LapicState::Kvm(s)
|
Self { regs: s.regs }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,18 +289,13 @@ impl From<FpuState> for FloatingPointUnit {
|
|||||||
|
|
||||||
impl From<LapicState> for MshvLapicState {
|
impl From<LapicState> for MshvLapicState {
|
||||||
fn from(s: LapicState) -> Self {
|
fn from(s: LapicState) -> Self {
|
||||||
match s {
|
Self { regs: s.regs }
|
||||||
LapicState::Mshv(s) => s,
|
|
||||||
/* Needed in case other hypervisors are enabled */
|
|
||||||
#[allow(unreachable_patterns)]
|
|
||||||
_ => panic!("LapicState is not valid"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<MshvLapicState> for LapicState {
|
impl From<MshvLapicState> for LapicState {
|
||||||
fn from(s: MshvLapicState) -> Self {
|
fn from(s: MshvLapicState) -> Self {
|
||||||
LapicState::Mshv(s)
|
Self { regs: s.regs }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user