Skip to content

Commit 3e4d837

Browse files
roypatbchalios
authored andcommitted
refactor: stop caching mpidr in struct KvmVcpu
This cached field was only accessed precisely once, so the getter might as well compute it instead. While we're at it, merge the getting with the free-standing computation function. Signed-off-by: Patrick Roy <[email protected]>
1 parent 019dc6b commit 3e4d837

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/vmm/src/arch/aarch64/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ pub fn configure_system_for_boot(
9898
let vcpu_mpidr = vcpus
9999
.iter_mut()
100100
.map(|cpu| cpu.kvm_vcpu.get_mpidr())
101-
.collect();
101+
.collect::<Result<Vec<_>, _>>()
102+
.map_err(KvmVcpuError::ConfigureRegisters)?;
102103
let cmdline = boot_cmdline
103104
.as_cstring()
104105
.expect("Cannot create cstring from cmdline string");

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

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,6 @@ pub fn setup_boot_regs(
140140
Ok(())
141141
}
142142

143-
/// Read the MPIDR - Multiprocessor Affinity Register.
144-
pub fn get_mpidr(vcpufd: &VcpuFd) -> Result<u64, VcpuArchError> {
145-
// MPIDR register is 64 bit wide on aarch64
146-
let mut mpidr = [0_u8; 8];
147-
match vcpufd.get_one_reg(MPIDR_EL1, &mut mpidr) {
148-
Err(err) => Err(VcpuArchError::GetOneReg(MPIDR_EL1, err)),
149-
Ok(_) => Ok(u64::from_le_bytes(mpidr)),
150-
}
151-
}
152-
153143
/// Saves the states of the system registers into `state`.
154144
///
155145
/// # Arguments
@@ -277,7 +267,6 @@ pub struct KvmVcpu {
277267
pub fd: VcpuFd,
278268
/// Vcpu peripherals, such as buses
279269
pub peripherals: Peripherals,
280-
mpidr: u64,
281270
kvi: kvm_vcpu_init,
282271
}
283272

@@ -311,14 +300,18 @@ impl KvmVcpu {
311300
index,
312301
fd: kvm_vcpu,
313302
peripherals: Default::default(),
314-
mpidr: 0,
315303
kvi,
316304
})
317305
}
318306

319-
/// Gets the MPIDR register value.
320-
pub fn get_mpidr(&self) -> u64 {
321-
self.mpidr
307+
/// Read the MPIDR - Multiprocessor Affinity Register.
308+
pub fn get_mpidr(&self) -> Result<u64, VcpuArchError> {
309+
// MPIDR register is 64 bit wide on aarch64
310+
let mut mpidr = [0_u8; 8];
311+
match self.fd.get_one_reg(MPIDR_EL1, &mut mpidr) {
312+
Err(err) => Err(VcpuArchError::GetOneReg(MPIDR_EL1, err)),
313+
Ok(_) => Ok(u64::from_le_bytes(mpidr)),
314+
}
322315
}
323316

324317
/// Configures an aarch64 specific vcpu for booting Linux.
@@ -351,8 +344,6 @@ impl KvmVcpu {
351344
)
352345
.map_err(KvmVcpuError::ConfigureRegisters)?;
353346

354-
self.mpidr = get_mpidr(&self.fd).map_err(KvmVcpuError::ConfigureRegisters)?;
355-
356347
Ok(())
357348
}
358349

@@ -393,7 +384,7 @@ impl KvmVcpu {
393384
..Default::default()
394385
};
395386
get_all_registers(&self.fd, &mut state.regs).map_err(KvmVcpuError::SaveState)?;
396-
state.mpidr = get_mpidr(&self.fd).map_err(KvmVcpuError::SaveState)?;
387+
state.mpidr = self.get_mpidr().map_err(KvmVcpuError::SaveState)?;
397388

398389
state.kvi = self.kvi;
399390
// We don't save power off state in a snapshot, because
@@ -757,21 +748,17 @@ mod tests {
757748

758749
#[test]
759750
fn test_read_mpidr() {
760-
let kvm = Kvm::new(vec![]).unwrap();
761-
let vm = kvm.fd.create_vm().unwrap();
762-
let vcpu = vm.create_vcpu(0).unwrap();
763-
let mut kvi: kvm_bindings::kvm_vcpu_init = kvm_bindings::kvm_vcpu_init::default();
764-
vm.get_preferred_target(&mut kvi).unwrap();
751+
let (_, _, vcpu, _) = setup_vcpu(0x10000);
765752

766753
// Must fail when vcpu is not initialized yet.
767-
let res = get_mpidr(&vcpu);
754+
let res = vcpu.get_mpidr();
768755
assert!(matches!(
769756
res.unwrap_err(),
770757
VcpuArchError::GetOneReg(MPIDR_EL1, _)
771758
));
772759

773-
vcpu.vcpu_init(&kvi).unwrap();
774-
assert_eq!(get_mpidr(&vcpu).unwrap(), 0x8000_0000);
760+
vcpu.init_vcpu().unwrap();
761+
assert_eq!(vcpu.get_mpidr().unwrap(), 0x8000_0000);
775762
}
776763

777764
#[test]

0 commit comments

Comments
 (0)