Skip to content

Commit e34ba7a

Browse files
committed
chore(aarch64): update set_reg error message
Format register id in hex and print the value was attempted to be set. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent b93fdee commit e34ba7a

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

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

Lines changed: 36 additions & 0 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::fmt::Write;
89
use std::mem::offset_of;
910

1011
use kvm_bindings::*;
@@ -404,6 +405,15 @@ impl<'a> Aarch64RegisterRef<'a> {
404405
T::from_slice(self.data)
405406
}
406407

408+
/// Returns a string with hex formatted value of the register.
409+
pub fn value_str(&self) -> String {
410+
let hex = self.data.iter().rev().fold(String::new(), |mut acc, byte| {
411+
write!(&mut acc, "{:02x}", byte).unwrap();
412+
acc
413+
});
414+
format!("0x{hex}")
415+
}
416+
407417
/// Returns register data as a byte slice
408418
pub fn as_slice(&self) -> &[u8] {
409419
self.data
@@ -695,6 +705,32 @@ mod tests {
695705
assert_eq!(reg_ref.value::<u64, 8>(), 69);
696706
}
697707

708+
#[test]
709+
fn test_reg_ref_value_str() {
710+
let bytes = 0x10_u8.to_le_bytes();
711+
let reg_ref = Aarch64RegisterRef::new(KVM_REG_SIZE_U8 as u64, &bytes);
712+
assert_eq!(reg_ref.value_str(), "0x10");
713+
714+
let bytes = 0x1020_u16.to_le_bytes();
715+
let reg_ref = Aarch64RegisterRef::new(KVM_REG_SIZE_U16, &bytes);
716+
assert_eq!(reg_ref.value_str(), "0x1020");
717+
718+
let bytes = 0x10203040_u32.to_le_bytes();
719+
let reg_ref = Aarch64RegisterRef::new(KVM_REG_SIZE_U32, &bytes);
720+
assert_eq!(reg_ref.value_str(), "0x10203040");
721+
722+
let bytes = 0x1020304050607080_u64.to_le_bytes();
723+
let reg_ref = Aarch64RegisterRef::new(KVM_REG_SIZE_U64, &bytes);
724+
assert_eq!(reg_ref.value_str(), "0x1020304050607080");
725+
726+
let bytes = [
727+
0x71, 0x61, 0x51, 0x41, 0x31, 0x21, 0x11, 0x90, 0x80, 0x70, 0x60, 0x50, 0x40, 0x30,
728+
0x20, 0x10,
729+
];
730+
let reg_ref = Aarch64RegisterRef::new(KVM_REG_SIZE_U128, &bytes);
731+
assert_eq!(reg_ref.value_str(), "0x10203040506070809011213141516171");
732+
}
733+
698734
/// Should panic because ID has different size from a slice length.
699735
/// - Size in ID: 128
700736
/// - Length of slice: 1

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::vstate::vm::Vm;
3030
pub enum VcpuArchError {
3131
/// Failed to get register {0}: {1}
3232
GetOneReg(u64, kvm_ioctls::Error),
33-
/// Failed to set register {0}: {1}
34-
SetOneReg(u64, kvm_ioctls::Error),
33+
/// Failed to set register {0:#x} to value {1}: {2}
34+
SetOneReg(u64, String, kvm_ioctls::Error),
3535
/// Failed to retrieve list of registers: {0}
3636
GetRegList(kvm_ioctls::Error),
3737
/// Failed to get multiprocessor state: {0}
@@ -178,7 +178,11 @@ impl KvmVcpu {
178178
) -> Result<(), KvmVcpuError> {
179179
for reg in vcpu_config.cpu_config.regs.iter() {
180180
self.fd.set_one_reg(reg.id, reg.as_slice()).map_err(|err| {
181-
KvmVcpuError::ApplyCpuTemplate(VcpuArchError::SetOneReg(reg.id, err))
181+
KvmVcpuError::ApplyCpuTemplate(VcpuArchError::SetOneReg(
182+
reg.id,
183+
reg.value_str(),
184+
err,
185+
))
182186
})?;
183187
}
184188

@@ -322,7 +326,9 @@ impl KvmVcpu {
322326
let id = arm64_core_reg_id!(KVM_REG_SIZE_U64, pstate);
323327
self.fd
324328
.set_one_reg(id, &PSTATE_FAULT_BITS_64.to_le_bytes())
325-
.map_err(|err| VcpuArchError::SetOneReg(id, err))?;
329+
.map_err(|err| {
330+
VcpuArchError::SetOneReg(id, format!("{PSTATE_FAULT_BITS_64:#x}"), err)
331+
})?;
326332

327333
// Other vCPUs are powered off initially awaiting PSCI wakeup.
328334
if self.index == 0 {
@@ -331,17 +337,18 @@ impl KvmVcpu {
331337
let id = arm64_core_reg_id!(KVM_REG_SIZE_U64, pc);
332338
self.fd
333339
.set_one_reg(id, &boot_ip.to_le_bytes())
334-
.map_err(|err| VcpuArchError::SetOneReg(id, err))?;
340+
.map_err(|err| VcpuArchError::SetOneReg(id, format!("{boot_ip:#x}"), err))?;
335341

336342
// Last mandatory thing to set -> the address pointing to the FDT (also called DTB).
337343
// "The device tree blob (dtb) must be placed on an 8-byte boundary and must
338344
// not exceed 2 megabytes in size." -> https://www.kernel.org/doc/Documentation/arm64/booting.txt.
339345
// We are choosing to place it the end of DRAM. See `get_fdt_addr`.
340346
let regs0 = offset_of!(user_pt_regs, regs) + kreg_off;
341347
let id = arm64_core_reg_id!(KVM_REG_SIZE_U64, regs0);
348+
let fdt_addr = get_fdt_addr(mem);
342349
self.fd
343-
.set_one_reg(id, &get_fdt_addr(mem).to_le_bytes())
344-
.map_err(|err| VcpuArchError::SetOneReg(id, err))?;
350+
.set_one_reg(id, &fdt_addr.to_le_bytes())
351+
.map_err(|err| VcpuArchError::SetOneReg(id, format!("{fdt_addr:#x}"), err))?;
345352

346353
// Reset the physical counter for the guest. This way we avoid guest reading
347354
// host physical counter.
@@ -357,7 +364,9 @@ impl KvmVcpu {
357364
if optional_capabilities.counter_offset {
358365
self.fd
359366
.set_one_reg(KVM_REG_ARM_PTIMER_CNT, &[0; 8])
360-
.map_err(|err| VcpuArchError::SetOneReg(id, err))?;
367+
.map_err(|err| {
368+
VcpuArchError::SetOneReg(id, format!("{KVM_REG_ARM_PTIMER_CNT:#x}"), err)
369+
})?;
361370
}
362371
}
363372
Ok(())
@@ -409,7 +418,7 @@ impl KvmVcpu {
409418
pub fn set_register(&self, reg: Aarch64RegisterRef) -> Result<(), VcpuArchError> {
410419
self.fd
411420
.set_one_reg(reg.id, reg.as_slice())
412-
.map_err(|e| VcpuArchError::SetOneReg(reg.id, e))?;
421+
.map_err(|e| VcpuArchError::SetOneReg(reg.id, reg.value_str(), e))?;
413422
Ok(())
414423
}
415424

@@ -569,6 +578,7 @@ mod tests {
569578
err.unwrap_err(),
570579
KvmVcpuError::ConfigureRegisters(VcpuArchError::SetOneReg(
571580
0x6030000000100042,
581+
"0x3c5".to_string(),
572582
kvm_ioctls::Error::new(9)
573583
))
574584
);
@@ -626,7 +636,7 @@ mod tests {
626636
let res = vcpu.restore_state(&faulty_vcpu_state);
627637
assert!(matches!(
628638
res.unwrap_err(),
629-
KvmVcpuError::RestoreState(VcpuArchError::SetOneReg(0, _))
639+
KvmVcpuError::RestoreState(VcpuArchError::SetOneReg(0, _, _))
630640
));
631641

632642
vcpu.init(&[]).unwrap();
@@ -696,7 +706,7 @@ mod tests {
696706
let res = vcpu.setup_boot_regs(0x0, &mem, &optional_capabilities);
697707
assert!(matches!(
698708
res.unwrap_err(),
699-
VcpuArchError::SetOneReg(0x6030000000100042, _)
709+
VcpuArchError::SetOneReg(0x6030000000100042, _, _)
700710
));
701711

702712
vcpu.init_vcpu().unwrap();

0 commit comments

Comments
 (0)