Skip to content

Commit 6abc075

Browse files
roypatzulinx86
authored andcommitted
fix: Use offset_of macro
The `offset_of` macro is stable since Rust 1.77.0, meaning we can use it to replace various home-grown alternatives. Fixes #4504 Signed-off-by: Patrick Roy <[email protected]>
1 parent 0c2d2af commit 6abc075

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

src/vmm/src/arch/aarch64/regs.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Use of this source code is governed by a BSD-style license that can be
66
// found in the THIRD-PARTY file.
77

8+
use std::mem::offset_of;
9+
810
use kvm_bindings::*;
911
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1012

@@ -19,28 +21,9 @@ const PSR_D_BIT: u64 = 0x0000_0200;
1921
/// Taken from arch/arm64/kvm/inject_fault.c.
2022
pub const PSTATE_FAULT_BITS_64: u64 = PSR_MODE_EL1h | PSR_A_BIT | PSR_F_BIT | PSR_I_BIT | PSR_D_BIT;
2123

22-
// Following are macros that help with getting the ID of a aarch64 core register.
23-
// The core register are represented by the user_pt_regs structure. Look for it in
24-
// arch/arm64/include/uapi/asm/ptrace.h.
25-
26-
/// Gets offset of a member (`field`) within a struct (`container`).
27-
/// Same as bindgen offset tests.
28-
macro_rules! offset__of {
29-
($container:ty, $field:ident) => {
30-
// SAFETY: The implementation closely matches that of the memoffset crate,
31-
// which have been under extensive review.
32-
unsafe {
33-
let uninit = std::mem::MaybeUninit::<$container>::uninit();
34-
let ptr = uninit.as_ptr();
35-
std::ptr::addr_of!((*ptr).$field) as usize - ptr as usize
36-
}
37-
};
38-
}
39-
pub(crate) use offset__of;
40-
4124
/// Gets a core id.
4225
macro_rules! arm64_core_reg_id {
43-
($size: tt, $offset: tt) => {
26+
($size: ident, $offset: expr) => {
4427
// The core registers of an arm64 machine are represented
4528
// in kernel by the `kvm_regs` structure. This structure is a
4629
// mix of 32, 64 and 128 bit fields:
@@ -130,15 +113,11 @@ pub const KVM_REG_ARM64_SVE_VLS: u64 =
130113
///
131114
/// https://github.com/torvalds/linux/blob/master/Documentation/virt/kvm/api.rst#L2578
132115
/// > 0x6030 0000 0010 0040 PC 64 regs.pc
133-
pub const PC: u64 = arm64_core_reg_id!(KVM_REG_SIZE_U64, 0x100);
134-
135-
// TODO: Once `core::mem::offset_of!()` macro becomes stable (maybe since 1.77.0?), use the
136-
// following instead: https://github.com/firecracker-microvm/firecracker/issues/4504
137-
// pub const PC: u64 = {
138-
// let kreg_off = offset_of!(kvm_regs, regs);
139-
// let pc_off = offset_of!(user_pt_regs, pc);
140-
// arm64_core_reg_id!(KVM_REG_SIZE_U64, kreg_off + pc_off)
141-
// };
116+
pub const PC: u64 = {
117+
let kreg_off = offset_of!(kvm_regs, regs);
118+
let pc_off = offset_of!(user_pt_regs, pc);
119+
arm64_core_reg_id!(KVM_REG_SIZE_U64, kreg_off + pc_off)
120+
};
142121

143122
/// Different aarch64 registers sizes
144123
#[derive(Debug)]

src/vmm/src/arch/aarch64/vcpu.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Use of this source code is governed by a BSD-style license that can be
66
// found in the THIRD-PARTY file.
77

8+
use std::mem::offset_of;
89
use std::path::PathBuf;
910

1011
use kvm_bindings::*;
@@ -78,10 +79,10 @@ pub fn setup_boot_regs(
7879
boot_ip: u64,
7980
mem: &GuestMemoryMmap,
8081
) -> Result<(), VcpuError> {
81-
let kreg_off = offset__of!(kvm_regs, regs);
82+
let kreg_off = offset_of!(kvm_regs, regs);
8283

8384
// Get the register index of the PSTATE (Processor State) register.
84-
let pstate = offset__of!(user_pt_regs, pstate) + kreg_off;
85+
let pstate = offset_of!(user_pt_regs, pstate) + kreg_off;
8586
let id = arm64_core_reg_id!(KVM_REG_SIZE_U64, pstate);
8687
vcpufd
8788
.set_one_reg(id, &PSTATE_FAULT_BITS_64.to_le_bytes())
@@ -90,7 +91,7 @@ pub fn setup_boot_regs(
9091
// Other vCPUs are powered off initially awaiting PSCI wakeup.
9192
if cpu_id == 0 {
9293
// Setting the PC (Processor Counter) to the current program address (kernel address).
93-
let pc = offset__of!(user_pt_regs, pc) + kreg_off;
94+
let pc = offset_of!(user_pt_regs, pc) + kreg_off;
9495
let id = arm64_core_reg_id!(KVM_REG_SIZE_U64, pc);
9596
vcpufd
9697
.set_one_reg(id, &boot_ip.to_le_bytes())
@@ -100,7 +101,7 @@ pub fn setup_boot_regs(
100101
// "The device tree blob (dtb) must be placed on an 8-byte boundary and must
101102
// not exceed 2 megabytes in size." -> https://www.kernel.org/doc/Documentation/arm64/booting.txt.
102103
// We are choosing to place it the end of DRAM. See `get_fdt_addr`.
103-
let regs0 = offset__of!(user_pt_regs, regs) + kreg_off;
104+
let regs0 = offset_of!(user_pt_regs, regs) + kreg_off;
104105
let id = arm64_core_reg_id!(KVM_REG_SIZE_U64, regs0);
105106
vcpufd
106107
.set_one_reg(id, &get_fdt_addr(mem).to_le_bytes())

0 commit comments

Comments
 (0)