Skip to content

Commit

Permalink
mshv-bindings: Derive constant instead of hard-coding
Browse files Browse the repository at this point in the history
Constants like LAPIC_STATE size could be derived by calling size_of on
the struct LapicState. In the similar fashion, other values can also be
derived. Use a const function to intialize the various component sizes.

Signed-off-by: Jinank Jain <[email protected]>
  • Loading branch information
jinankjain authored and Jinank Jain committed Apr 13, 2024
1 parent 5673170 commit dadc44e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion mshv-bindings/src/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,30 @@ pub struct MiscRegs {
pub hypercall: u64,
}

const fn initialize_comp_sizes() -> [usize; MSHV_VP_STATE_COUNT as usize] {
let mut vp_state_comp_size = [0; MSHV_VP_STATE_COUNT as usize];

vp_state_comp_size[MSHV_VP_STATE_LAPIC as usize] = std::mem::size_of::<LapicState>();
vp_state_comp_size[MSHV_VP_STATE_XSAVE as usize] = std::mem::size_of::<XSave>();
vp_state_comp_size[MSHV_VP_STATE_SIMP as usize] = HV_PAGE_SIZE; // Assuming SIMP page is
// allocatedd by the Hypervisor
// which is of PAGE_SIZE
vp_state_comp_size[MSHV_VP_STATE_SIEFP as usize] = HV_PAGE_SIZE; // Assumong SIEFP page is
// allocated by the Hypervisor
// which is of PAGE_SIZE
vp_state_comp_size[MSHV_VP_STATE_SYNTHETIC_TIMERS as usize] =
std::mem::size_of::<hv_synthetic_timers_state>();

vp_state_comp_size
}

// Total size: 13512 bytes
// 1. MSHV_VP_STATE_LAPIC, Size: 1024 bytes;
// 2. MSHV_VP_STATE_XSAVE, Size: 4096 bytes;
// 3. MSHV_VP_STATE_SIMP, Size: 4096 bytes;
// 4. MSHV_VP_STATE_SIEFP, Size: 4096 bytes;
// 5. MSHV_VP_STATE_SYNTHETIC_TIMERS, Size: 200 bytes;
const VP_STATE_COMP_SIZES: [usize; 5] = [0x400, 0x1000, 0x1000, 0x1000, 0xC8];
const VP_STATE_COMP_SIZES: [usize; MSHV_VP_STATE_COUNT as usize] = initialize_comp_sizes();

pub const VP_STATE_COMPONENTS_BUFFER_SIZE: usize = VP_STATE_COMP_SIZES
[MSHV_VP_STATE_LAPIC as usize]
Expand Down

0 comments on commit dadc44e

Please sign in to comment.