diff --git a/aarch64/Cargo.toml b/aarch64/Cargo.toml index e86c6e367c..c52d39ab46 100644 --- a/aarch64/Cargo.toml +++ b/aarch64/Cargo.toml @@ -13,6 +13,7 @@ kernel_cmdline = { path = "../kernel_cmdline" } kvm = { path = "../kvm" } kvm_sys = { path = "../kvm_sys" } libc = "*" +memoffset = "0.6.5" minijail = "*" remain = "*" resources = { path = "../resources" } diff --git a/hypervisor/Cargo.toml b/hypervisor/Cargo.toml index 0e607c4b9f..6a5ae1d7dd 100644 --- a/hypervisor/Cargo.toml +++ b/hypervisor/Cargo.toml @@ -12,6 +12,7 @@ enumn = "0.1.0" kvm = { path = "../kvm" } kvm_sys = { path = "../kvm_sys" } libc = "*" +memoffset = "0.6.5" serde = { version = "1", features = [ "derive" ] } sync = { path = "../common/sync" } base = { path = "../common/base" } diff --git a/hypervisor/src/kvm/aarch64.rs b/hypervisor/src/kvm/aarch64.rs index ffbe98513c..60a5fc1802 100644 --- a/hypervisor/src/kvm/aarch64.rs +++ b/hypervisor/src/kvm/aarch64.rs @@ -17,23 +17,6 @@ use crate::{ VcpuExit, VcpuFeature, Vm, VmAArch64, VmCap, }; -#[macro_export] -macro_rules! offset__of { - ($type:path, $field:tt) => {{ - // Check that the field actually exists. This will generate a compiler error if the field is - // accessed through a Deref impl. - #[allow(clippy::unneeded_field_pattern)] - let $type { $field: _, .. }; - - // Get a pointer to the uninitialized field. This is taken from the docs for `addr_of_mut`. - let mut uninit = ::std::mem::MaybeUninit::<$type>::uninit(); - let field_ptr = unsafe { ::std::ptr::addr_of_mut!((*uninit.as_mut_ptr()).$field) }; - - // Now get the offset. - (field_ptr as usize) - (uninit.as_mut_ptr() as usize) - }}; -} - /// Gives the ID for a register to be used with `set_one_reg`. /// /// Pass the name of a field in `user_pt_regs` to get the corresponding register @@ -41,18 +24,18 @@ macro_rules! offset__of { /// /// To get ID for registers `x0`-`x31`, refer to the `regs` field along with the /// register number, e.g. `arm64_core_reg!(regs, 5)` for `x5`. This is different -/// to work around `offset__of!(kvm_sys::user_pt_regs, regs[$x])` not working. +/// to work around `offset_of!(kvm_sys::user_pt_regs, regs[$x])` not working. #[macro_export] macro_rules! arm64_core_reg { ($reg: tt) => {{ - let off = ($crate::offset__of!(::kvm_sys::user_pt_regs, $reg) / 4) as u64; + let off = (memoffset::offset_of!(::kvm_sys::user_pt_regs, $reg) / 4) as u64; ::kvm_sys::KVM_REG_ARM64 | ::kvm_sys::KVM_REG_SIZE_U64 | ::kvm_sys::KVM_REG_ARM_CORE as u64 | off }}; (regs, $x: literal) => {{ - let off = (($crate::offset__of!(::kvm_sys::user_pt_regs, regs) + let off = ((memoffset::offset_of!(::kvm_sys::user_pt_regs, regs) + ($x * ::std::mem::size_of::())) / 4) as u64; ::kvm_sys::KVM_REG_ARM64