Skip to content

Commit 339173c

Browse files
committed
refactor(vmm/aarch64): improved errors
- Removed redefinition of `Result` - Fixed all different ways were defined. - Removed duplicated vcpu test Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 7157dd6 commit 339173c

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

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

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
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::result;
9-
108
use kvm_ioctls::*;
119
use logger::{error, IncMetric, METRICS};
1210
use utils::vm_memory::{Address, GuestAddress, GuestMemoryMmap};
@@ -18,7 +16,7 @@ use crate::arch::aarch64::regs::{
1816
set_registers, setup_boot_regs, Aarch64Register, Error as ArchError, KVM_REG_ARM_TIMER_CNT,
1917
};
2018
use crate::cpu_config::templates::CpuConfiguration;
21-
use crate::vcpu::VcpuConfig;
19+
use crate::vcpu::{Error as VcpuError, VcpuConfig};
2220
use crate::vstate::vcpu::VcpuEmulation;
2321
use crate::vstate::vm::Vm;
2422

@@ -43,8 +41,6 @@ pub enum Error {
4341
SaveState(ArchError),
4442
}
4543

46-
type Result<T> = result::Result<T, Error>;
47-
4844
pub type KvmVcpuConfigureError = Error;
4945

5046
/// A wrapper around creating and using a kvm aarch64 vcpu.
@@ -64,7 +60,7 @@ impl KvmVcpu {
6460
///
6561
/// * `index` - Represents the 0-based CPU index between [0, max vcpus).
6662
/// * `vm` - The vm to which this vcpu will get attached.
67-
pub fn new(index: u8, vm: &Vm) -> Result<Self> {
63+
pub fn new(index: u8, vm: &Vm) -> Result<Self, Error> {
6864
let kvm_vcpu = vm
6965
.fd()
7066
.create_vcpu(index.into())
@@ -95,7 +91,7 @@ impl KvmVcpu {
9591
guest_mem: &GuestMemoryMmap,
9692
kernel_load_addr: GuestAddress,
9793
vcpu_config: &VcpuConfig,
98-
) -> std::result::Result<(), Error> {
94+
) -> Result<(), Error> {
9995
for Aarch64Register { id, value } in vcpu_config.cpu_config.regs.iter() {
10096
self.fd
10197
.set_one_reg(*id, *value)
@@ -120,7 +116,7 @@ impl KvmVcpu {
120116
/// # Arguments
121117
///
122118
/// * `vm_fd` - The kvm `VmFd` for this microvm.
123-
pub fn init(&self, vm_fd: &VmFd) -> Result<()> {
119+
pub fn init(&self, vm_fd: &VmFd) -> Result<(), Error> {
124120
let mut kvi: kvm_bindings::kvm_vcpu_init = kvm_bindings::kvm_vcpu_init::default();
125121

126122
// This reads back the kernel's preferred target type.
@@ -137,7 +133,7 @@ impl KvmVcpu {
137133
}
138134

139135
/// Save the KVM internal state.
140-
pub fn save_state(&self) -> Result<VcpuState> {
136+
pub fn save_state(&self) -> Result<VcpuState, Error> {
141137
let mut state = VcpuState {
142138
mp_state: get_mpstate(&self.fd).map_err(Error::SaveState)?,
143139
..Default::default()
@@ -148,14 +144,14 @@ impl KvmVcpu {
148144
}
149145

150146
/// Use provided state to populate KVM internal state.
151-
pub fn restore_state(&self, state: &VcpuState) -> Result<()> {
147+
pub fn restore_state(&self, state: &VcpuState) -> Result<(), Error> {
152148
set_registers(&self.fd, &state.regs).map_err(Error::RestoreState)?;
153149
set_mpstate(&self.fd, state.mp_state).map_err(Error::RestoreState)?;
154150
Ok(())
155151
}
156152

157153
/// Dumps CPU configuration.
158-
pub fn dump_cpu_config(&self) -> Result<CpuConfiguration> {
154+
pub fn dump_cpu_config(&self) -> Result<CpuConfiguration, Error> {
159155
let mut reg_list = get_all_registers_ids(&self.fd).map_err(Error::DumpCpuConfig)?;
160156

161157
// KVM_REG_ARM_TIMER_CNT should be removed, because it depends on the elapsed time and
@@ -173,12 +169,12 @@ impl KvmVcpu {
173169
/// Runs the vCPU in KVM context and handles the kvm exit reason.
174170
///
175171
/// Returns error or enum specifying whether emulation was handled or interrupted.
176-
pub fn run_arch_emulation(&self, exit: VcpuExit) -> super::Result<VcpuEmulation> {
172+
pub fn run_arch_emulation(&self, exit: VcpuExit) -> Result<VcpuEmulation, VcpuError> {
177173
METRICS.vcpu.failures.inc();
178174
// TODO: Are we sure we want to finish running a vcpu upon
179175
// receiving a vm exit that is not necessarily an error?
180176
error!("Unexpected exit reason on vcpu run: {:?}", exit);
181-
Err(super::Error::UnhandledKvmExit(format!("{:?}", exit)))
177+
Err(VcpuError::UnhandledKvmExit(format!("{:?}", exit)))
182178
}
183179
}
184180

@@ -200,7 +196,7 @@ mod tests {
200196

201197
use utils::vm_memory::GuestMemoryMmap;
202198

203-
use super::*;
199+
use super::{Error, *};
204200
use crate::cpu_config::aarch64::CpuConfiguration;
205201
use crate::vcpu::VcpuConfig;
206202
use crate::vstate::vm::tests::setup_vm;
@@ -216,7 +212,7 @@ mod tests {
216212
}
217213

218214
fn init_vcpu(vcpu: &VcpuFd, vm: &VmFd) {
219-
let mut kvi: kvm_bindings::kvm_vcpu_init = kvm_bindings::kvm_vcpu_init::default();
215+
let mut kvi = kvm_bindings::kvm_vcpu_init::default();
220216
vm.get_preferred_target(&mut kvi).unwrap();
221217
vcpu.vcpu_init(&kvi).unwrap();
222218
}
@@ -262,23 +258,8 @@ mod tests {
262258
assert!(err.is_err());
263259
assert_eq!(
264260
err.unwrap_err(),
265-
super::Error::ConfigureRegisters(ArchError::SetOneReg(
266-
6931039826524241986,
267-
kvm_ioctls::Error::new(9)
268-
))
269-
);
270-
271-
let (_vm, mut vcpu, vm_mem) = setup_vcpu(0x10000);
272-
unsafe { libc::close(vcpu.fd.as_raw_fd()) };
273-
let err = vcpu.configure(
274-
&vm_mem,
275-
GuestAddress(crate::arch::get_kernel_start()),
276-
&vcpu_config,
277-
);
278-
assert_eq!(
279-
err.unwrap_err(),
280-
super::Error::ConfigureRegisters(ArchError::SetOneReg(
281-
6931039826524241986,
261+
Error::ConfigureRegisters(ArchError::SetOneReg(
262+
0x6030000000100042,
282263
kvm_ioctls::Error::new(9)
283264
))
284265
);
@@ -307,7 +288,7 @@ mod tests {
307288
assert!(res.is_err());
308289
assert_eq!(
309290
res.unwrap_err(),
310-
super::Error::SaveState(ArchError::GetRegList(kvm_ioctls::Error::new(8))),
291+
Error::SaveState(ArchError::GetRegList(kvm_ioctls::Error::new(8))),
311292
);
312293

313294
// Try to restore the register using a faulty state.
@@ -320,7 +301,7 @@ mod tests {
320301
assert!(res.is_err());
321302
assert_eq!(
322303
res.unwrap_err(),
323-
super::Error::RestoreState(ArchError::SetOneReg(0, kvm_ioctls::Error::new(8)))
304+
Error::RestoreState(ArchError::SetOneReg(0, kvm_ioctls::Error::new(8)))
324305
);
325306

326307
init_vcpu(&vcpu.fd, vm.fd());

0 commit comments

Comments
 (0)