Skip to content

Commit

Permalink
mshv-ioctls: update get_regs to use register page
Browse files Browse the repository at this point in the history
Signed-off-by: Muminul Islam <[email protected]>
  • Loading branch information
russell-islam committed Jan 9, 2025
1 parent b695670 commit 53c3cea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
12 changes: 12 additions & 0 deletions mshv-bindings/src/x86_64/regs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,18 @@ macro_rules! set_gp_regs_field_ptr {
};
}

#[macro_export]
macro_rules! get_gp_regs_field_ptr {
($this: ident, $name: ident) => {
(*$this)
.__bindgen_anon_1
.__bindgen_anon_1
.__bindgen_anon_1
.__bindgen_anon_1
.$name
};
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
66 changes: 20 additions & 46 deletions mshv-ioctls/src/ioctls/vcpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,59 +199,33 @@ impl VcpuFd {
/// Returns the vCPU general purpose registers.
#[cfg(not(target_arch = "aarch64"))]
pub fn get_regs(&self) -> Result<StandardRegisters> {
let reg_names = [
hv_register_name_HV_X64_REGISTER_RAX,
hv_register_name_HV_X64_REGISTER_RBX,
hv_register_name_HV_X64_REGISTER_RCX,
hv_register_name_HV_X64_REGISTER_RDX,
hv_register_name_HV_X64_REGISTER_RSI,
hv_register_name_HV_X64_REGISTER_RDI,
hv_register_name_HV_X64_REGISTER_RSP,
hv_register_name_HV_X64_REGISTER_RBP,
hv_register_name_HV_X64_REGISTER_R8,
hv_register_name_HV_X64_REGISTER_R9,
hv_register_name_HV_X64_REGISTER_R10,
hv_register_name_HV_X64_REGISTER_R11,
hv_register_name_HV_X64_REGISTER_R12,
hv_register_name_HV_X64_REGISTER_R13,
hv_register_name_HV_X64_REGISTER_R14,
hv_register_name_HV_X64_REGISTER_R15,
hv_register_name_HV_X64_REGISTER_RIP,
hv_register_name_HV_X64_REGISTER_RFLAGS,
];
let mut reg_assocs: Vec<hv_register_assoc> = reg_names
.iter()
.map(|name| hv_register_assoc {
name: *name,
..Default::default()
})
.collect();
self.get_reg(&mut reg_assocs)?;
let vp_reg_page = self.get_vp_reg_page();
let mut ret_regs = StandardRegisters::default();
// SAFETY: access union fields
unsafe {
ret_regs.rax = reg_assocs[0].value.reg64;
ret_regs.rbx = reg_assocs[1].value.reg64;
ret_regs.rcx = reg_assocs[2].value.reg64;
ret_regs.rdx = reg_assocs[3].value.reg64;
ret_regs.rsi = reg_assocs[4].value.reg64;
ret_regs.rdi = reg_assocs[5].value.reg64;
ret_regs.rsp = reg_assocs[6].value.reg64;
ret_regs.rbp = reg_assocs[7].value.reg64;
ret_regs.r8 = reg_assocs[8].value.reg64;
ret_regs.r9 = reg_assocs[9].value.reg64;
ret_regs.r10 = reg_assocs[10].value.reg64;
ret_regs.r11 = reg_assocs[11].value.reg64;
ret_regs.r12 = reg_assocs[12].value.reg64;
ret_regs.r13 = reg_assocs[13].value.reg64;
ret_regs.r14 = reg_assocs[14].value.reg64;
ret_regs.r15 = reg_assocs[15].value.reg64;
ret_regs.rip = reg_assocs[16].value.reg64;
ret_regs.rflags = reg_assocs[17].value.reg64;
ret_regs.rax = get_gp_regs_field_ptr!(vp_reg_page, rax);
ret_regs.rbx = get_gp_regs_field_ptr!(vp_reg_page, rbx);
ret_regs.rcx = get_gp_regs_field_ptr!(vp_reg_page, rcx);
ret_regs.rdx = get_gp_regs_field_ptr!(vp_reg_page, rdx);
ret_regs.rsi = get_gp_regs_field_ptr!(vp_reg_page, rsi);
ret_regs.rdi = get_gp_regs_field_ptr!(vp_reg_page, rdi);
ret_regs.rsp = get_gp_regs_field_ptr!(vp_reg_page, rsp);
ret_regs.rbp = get_gp_regs_field_ptr!(vp_reg_page, rbp);
ret_regs.r8 = get_gp_regs_field_ptr!(vp_reg_page, r8);
ret_regs.r9 = get_gp_regs_field_ptr!(vp_reg_page, r9);
ret_regs.r10 = get_gp_regs_field_ptr!(vp_reg_page, r10);
ret_regs.r11 = get_gp_regs_field_ptr!(vp_reg_page, r11);
ret_regs.r12 = get_gp_regs_field_ptr!(vp_reg_page, r12);
ret_regs.r13 = get_gp_regs_field_ptr!(vp_reg_page, r13);
ret_regs.r14 = get_gp_regs_field_ptr!(vp_reg_page, r14);
ret_regs.r15 = get_gp_regs_field_ptr!(vp_reg_page, r15);
ret_regs.rip = (*vp_reg_page).__bindgen_anon_1.__bindgen_anon_1.rip;
ret_regs.rflags = (*vp_reg_page).__bindgen_anon_1.__bindgen_anon_1.rflags;
}

Ok(ret_regs)
}

/// Returns the vCPU special registers.
#[cfg(not(target_arch = "aarch64"))]
pub fn get_sregs(&self) -> Result<SpecialRegisters> {
Expand Down

0 comments on commit 53c3cea

Please sign in to comment.