Use memoffset::offset_of rather than implementing it ourselves.

TEST=tools/dev_container tools/run_tests
TEST=tools/dev_container tools/run_tests --target=vm:aarch64

Change-Id: Iccdf0653001599d76c742db27349e274f7a4bb23
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3497460
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Andrew Walbran <qwandor@google.com>
This commit is contained in:
Andrew Walbran 2022-03-01 12:50:30 +00:00 committed by Commit Bot
parent c2a133a2aa
commit 27f2133dba
3 changed files with 5 additions and 20 deletions

View file

@ -13,6 +13,7 @@ kernel_cmdline = { path = "../kernel_cmdline" }
kvm = { path = "../kvm" } kvm = { path = "../kvm" }
kvm_sys = { path = "../kvm_sys" } kvm_sys = { path = "../kvm_sys" }
libc = "*" libc = "*"
memoffset = "0.6.5"
minijail = "*" minijail = "*"
remain = "*" remain = "*"
resources = { path = "../resources" } resources = { path = "../resources" }

View file

@ -12,6 +12,7 @@ enumn = "0.1.0"
kvm = { path = "../kvm" } kvm = { path = "../kvm" }
kvm_sys = { path = "../kvm_sys" } kvm_sys = { path = "../kvm_sys" }
libc = "*" libc = "*"
memoffset = "0.6.5"
serde = { version = "1", features = [ "derive" ] } serde = { version = "1", features = [ "derive" ] }
sync = { path = "../common/sync" } sync = { path = "../common/sync" }
base = { path = "../common/base" } base = { path = "../common/base" }

View file

@ -17,23 +17,6 @@ use crate::{
VcpuExit, VcpuFeature, Vm, VmAArch64, VmCap, 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`. /// 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 /// 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 /// 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 /// 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_export]
macro_rules! arm64_core_reg { macro_rules! arm64_core_reg {
($reg: tt) => {{ ($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_ARM64
| ::kvm_sys::KVM_REG_SIZE_U64 | ::kvm_sys::KVM_REG_SIZE_U64
| ::kvm_sys::KVM_REG_ARM_CORE as u64 | ::kvm_sys::KVM_REG_ARM_CORE as u64
| off | off
}}; }};
(regs, $x: literal) => {{ (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::<u64>())) + ($x * ::std::mem::size_of::<u64>()))
/ 4) as u64; / 4) as u64;
::kvm_sys::KVM_REG_ARM64 ::kvm_sys::KVM_REG_ARM64