hypervisor: Implement getter/setter for StandardRegisters

This would be used to abstract away differences in KVM/MSHV
representation of StandardRegisters.

Signed-off-by: Jinank Jain <jinankjain@microsoft.com>
This commit is contained in:
Jinank Jain 2024-08-07 22:42:06 +05:30 committed by Bo Chen
parent feb0a36067
commit 8f3bd4d9ec
3 changed files with 87 additions and 0 deletions

11
Cargo.lock generated
View File

@ -456,6 +456,16 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "concat-idents"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f76990911f2267d837d9d0ad060aa63aaad170af40904b29461734c339030d4d"
dependencies = [
"quote",
"syn 2.0.66",
]
[[package]]
name = "concurrent-queue"
version = "2.5.0"
@ -982,6 +992,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"byteorder",
"concat-idents",
"env_logger",
"iced-x86",
"igvm",

View File

@ -14,6 +14,7 @@ tdx = []
[dependencies]
anyhow = "1.0.86"
byteorder = "1.5.0"
concat-idents = "1.1.5"
igvm = { version = "0.3.3", optional = true }
igvm_defs = { version = "0.3.1", optional = true }
kvm-bindings = { version = "0.8.1", optional = true, features = ["serde"] }

View File

@ -49,6 +49,7 @@ mod cpu;
mod device;
pub use crate::hypervisor::{Hypervisor, HypervisorError};
use concat_idents::concat_idents;
#[cfg(target_arch = "x86_64")]
pub use cpu::CpuVendor;
pub use cpu::{HypervisorCpuError, Vcpu, VmExit};
@ -195,3 +196,77 @@ pub enum StandardRegisters {
#[cfg(all(feature = "mshv", target_arch = "x86_64"))]
Mshv(mshv_bindings::StandardRegisters),
}
macro_rules! set_x86_64_reg {
($reg_name:ident) => {
concat_idents!(method_name = "set_", $reg_name {
#[cfg(target_arch = "x86_64")]
impl StandardRegisters {
pub fn method_name(&mut self, val: u64) {
match self {
#[cfg(feature = "kvm")]
StandardRegisters::Kvm(s) => s.$reg_name = val,
#[cfg(feature = "mshv")]
StandardRegisters::Mshv(s) => s.$reg_name = val,
}
}
}
});
}
}
macro_rules! get_x86_64_reg {
($reg_name:ident) => {
concat_idents!(method_name = "get_", $reg_name {
#[cfg(target_arch = "x86_64")]
impl StandardRegisters {
pub fn method_name(&self) -> u64 {
match self {
#[cfg(feature = "kvm")]
StandardRegisters::Kvm(s) => s.$reg_name,
#[cfg(feature = "mshv")]
StandardRegisters::Mshv(s) => s.$reg_name,
}
}
}
});
}
}
set_x86_64_reg!(rax);
set_x86_64_reg!(rbx);
set_x86_64_reg!(rcx);
set_x86_64_reg!(rdx);
set_x86_64_reg!(rsi);
set_x86_64_reg!(rdi);
set_x86_64_reg!(rsp);
set_x86_64_reg!(rbp);
set_x86_64_reg!(r8);
set_x86_64_reg!(r9);
set_x86_64_reg!(r10);
set_x86_64_reg!(r11);
set_x86_64_reg!(r12);
set_x86_64_reg!(r13);
set_x86_64_reg!(r14);
set_x86_64_reg!(r15);
set_x86_64_reg!(rip);
set_x86_64_reg!(rflags);
get_x86_64_reg!(rax);
get_x86_64_reg!(rbx);
get_x86_64_reg!(rcx);
get_x86_64_reg!(rdx);
get_x86_64_reg!(rsi);
get_x86_64_reg!(rdi);
get_x86_64_reg!(rsp);
get_x86_64_reg!(rbp);
get_x86_64_reg!(r8);
get_x86_64_reg!(r9);
get_x86_64_reg!(r10);
get_x86_64_reg!(r11);
get_x86_64_reg!(r12);
get_x86_64_reg!(r13);
get_x86_64_reg!(r14);
get_x86_64_reg!(r15);
get_x86_64_reg!(rip);
get_x86_64_reg!(rflags);