Skip to content

Commit 7cc160d

Browse files
committed
refactor(cpu-template-helper): Exclude meaningless registers for aarch64
As done for x86, limits `dump_cpu_config()`'s output to the dumpable registers instead excludes dumpable but not meaningful registers on the CPU template helper tool's side. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 48ff6e0 commit 7cc160d

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/cpu-template-helper/src/template/dump/aarch64.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use vmm::arch::aarch64::regs::RegSize;
4+
use vmm::arch::aarch64::regs::{RegSize, PC, SYS_CNTPCT_EL0, SYS_CNTV_CVAL_EL0};
55
use vmm::cpu_config::aarch64::custom_cpu_template::RegisterModifier;
66
use vmm::cpu_config::templates::{CpuConfiguration, CustomCpuTemplate, RegisterValueFilter};
77
use vmm::logger::warn;
@@ -26,6 +26,9 @@ pub fn config_to_template(cpu_config: &CpuConfiguration) -> CustomCpuTemplate {
2626
}
2727
})
2828
.collect();
29+
30+
reg_modifiers.retain(|modifier| !REG_EXCLUSION_LIST.contains(&modifier.addr));
31+
2932
reg_modifiers.sort_by_key(|modifier| modifier.addr);
3033

3134
CustomCpuTemplate {
@@ -34,9 +37,20 @@ pub fn config_to_template(cpu_config: &CpuConfiguration) -> CustomCpuTemplate {
3437
}
3538
}
3639

40+
// List of register IDs excluded from the CPU configuration dump.
41+
const REG_EXCLUSION_LIST: [u64; 3] = [
42+
// SYS_CNTV_CVAL_EL0 and SYS_CNTPCT_EL0 are timer registers and depend on the elapsed time.
43+
// This type of registers are not useful as guest CPU config dump.
44+
SYS_CNTV_CVAL_EL0,
45+
SYS_CNTPCT_EL0,
46+
// Program counter (PC) value is determined by the given kernel image. It should not be
47+
// overwritten by a custom CPU template and does not need to be tracked in a fingerprint file.
48+
PC,
49+
];
50+
3751
#[cfg(test)]
3852
mod tests {
39-
use vmm::arch::aarch64::regs::{Aarch64RegisterRef, Aarch64RegisterVec};
53+
use vmm::arch::aarch64::regs::{reg_size, Aarch64RegisterRef, Aarch64RegisterVec};
4054

4155
use super::*;
4256

@@ -64,10 +78,16 @@ mod tests {
6478
KVM_REG_SIZE_U64,
6579
&0x0000_ffff_0000_ffff_u64.to_le_bytes(),
6680
));
81+
// CPU templates only supports 32, 64 and 128 bit wide registers, so the following registers
82+
// should be excluded from the result.
6783
v.push(Aarch64RegisterRef::new(KVM_REG_SIZE_U256, &[0x69; 32]));
6884
v.push(Aarch64RegisterRef::new(KVM_REG_SIZE_U512, &[0x69; 64]));
6985
v.push(Aarch64RegisterRef::new(KVM_REG_SIZE_U1024, &[0x69; 128]));
7086
v.push(Aarch64RegisterRef::new(KVM_REG_SIZE_U2048, &[0x69; 256]));
87+
// The following registers should be excluded from the result.
88+
for id in REG_EXCLUSION_LIST {
89+
v.push(Aarch64RegisterRef::new(id, &vec![0; reg_size(id)]));
90+
}
7191
v
7292
}
7393

src/vmm/src/vstate/vcpu/aarch64.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use std::fmt::{Debug, Write};
1010
use kvm_ioctls::*;
1111
use serde::{Deserialize, Serialize};
1212

13-
use crate::arch::aarch64::regs::{
14-
Aarch64RegisterVec, KVM_REG_ARM64_SVE_VLS, PC, SYS_CNTPCT_EL0, SYS_CNTV_CVAL_EL0,
15-
};
13+
use crate::arch::aarch64::regs::{Aarch64RegisterVec, KVM_REG_ARM64_SVE_VLS};
1614
use crate::arch::aarch64::vcpu::{
1715
get_all_registers, get_all_registers_ids, get_mpidr, get_mpstate, get_registers, set_mpstate,
1816
set_register, setup_boot_regs, VcpuError as ArchError,
@@ -220,17 +218,7 @@ impl KvmVcpu {
220218

221219
/// Dumps CPU configuration.
222220
pub fn dump_cpu_config(&self) -> Result<CpuConfiguration, KvmVcpuError> {
223-
let mut reg_list = get_all_registers_ids(&self.fd).map_err(KvmVcpuError::DumpCpuConfig)?;
224-
225-
// SYS_CNTV_CVAL_EL0 and SYS_CNTPCT_EL0 are timer registers and depend on the elapsed time.
226-
// This type of registers are not useful as guest CPU config dump.
227-
//
228-
// The value of program counter (PC) is determined by the given kernel image. It should not
229-
// be overwritten by a custom CPU template and does not need to be tracked in a fingerprint
230-
// file.
231-
reg_list.retain(|&reg_id| {
232-
reg_id != SYS_CNTV_CVAL_EL0 && reg_id != SYS_CNTPCT_EL0 && reg_id != PC
233-
});
221+
let reg_list = get_all_registers_ids(&self.fd).map_err(KvmVcpuError::DumpCpuConfig)?;
234222

235223
let mut regs = Aarch64RegisterVec::default();
236224
get_registers(&self.fd, &reg_list, &mut regs).map_err(KvmVcpuError::DumpCpuConfig)?;

0 commit comments

Comments
 (0)