Skip to content

Commit 14963ed

Browse files
roypatShadowCurse
andcommitted
refactor(test): Eliminate mocking/cfg(not(test)) from vcpu/mod.rs
Separate the call to `KVM_RUN` from the handling of the result value. This makes the handling of the `VcpuExit` unit-testable without needing to hack in `cfg(not(test))` code that compiles out the `KVM_RUN` call at compile time. For this, separate the parts of KvmVcpu that the exit handler needs mutable access to into a `Peripherals` structure, which is passed to `handle_kvm_exit`. This is needed so that we can shared the `&mut` to `kvm_vcpu` into `fd` and "the rest". Co-Authored-By: Egor Lazarchuk <[email protected]> Signed-off-by: Patrick Roy <[email protected]>
1 parent 59e6520 commit 14963ed

File tree

3 files changed

+175
-177
lines changed

3 files changed

+175
-177
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ pub struct KvmVcpu {
5454
pub index: u8,
5555
/// KVM vcpu fd.
5656
pub fd: VcpuFd,
57-
/// Mmio bus.
58-
pub mmio_bus: Option<crate::devices::Bus>,
57+
/// Vcpu peripherals, such as buses
58+
pub(super) peripherals: Peripherals,
5959
mpidr: u64,
6060
kvi: Option<kvm_bindings::kvm_vcpu_init>,
6161
}
6262

63+
/// Vcpu peripherals
64+
#[derive(Default, Debug)]
65+
pub(super) struct Peripherals {
66+
/// mmio bus.
67+
pub mmio_bus: Option<crate::devices::Bus>,
68+
}
69+
6370
impl KvmVcpu {
6471
/// Constructs a new kvm vcpu with arch specific functionality.
6572
///
@@ -76,7 +83,7 @@ impl KvmVcpu {
7683
Ok(KvmVcpu {
7784
index,
7885
fd: kvm_vcpu,
79-
mmio_bus: None,
86+
peripherals: Default::default(),
8087
mpidr: 0,
8188
kvi: None,
8289
})
@@ -222,18 +229,6 @@ impl KvmVcpu {
222229

223230
Ok(CpuConfiguration { regs })
224231
}
225-
226-
/// Runs the vCPU in KVM context and handles the kvm exit reason.
227-
///
228-
/// Returns error or enum specifying whether emulation was handled or interrupted.
229-
pub fn run_arch_emulation(&self, exit: VcpuExit) -> Result<VcpuEmulation, VcpuError> {
230-
METRICS.vcpu.failures.inc();
231-
// TODO: Are we sure we want to finish running a vcpu upon
232-
// receiving a vm exit that is not necessarily an error?
233-
error!("Unexpected exit reason on vcpu run: {:?}", exit);
234-
Err(VcpuError::UnhandledKvmExit(format!("{:?}", exit)))
235-
}
236-
237232
/// Initializes internal vcpufd.
238233
fn init_vcpu(&self, kvi: &kvm_bindings::kvm_vcpu_init) -> Result<(), KvmVcpuError> {
239234
self.fd.vcpu_init(kvi).map_err(KvmVcpuError::Init)?;
@@ -253,6 +248,19 @@ impl KvmVcpu {
253248
}
254249
}
255250

251+
impl Peripherals {
252+
/// Runs the vCPU in KVM context and handles the kvm exit reason.
253+
///
254+
/// Returns error or enum specifying whether emulation was handled or interrupted.
255+
pub fn run_arch_emulation(&self, exit: VcpuExit) -> Result<VcpuEmulation, VcpuError> {
256+
METRICS.vcpu.failures.inc();
257+
// TODO: Are we sure we want to finish running a vcpu upon
258+
// receiving a vm exit that is not necessarily an error?
259+
error!("Unexpected exit reason on vcpu run: {:?}", exit);
260+
Err(VcpuError::UnhandledKvmExit(format!("{:?}", exit)))
261+
}
262+
}
263+
256264
/// Structure holding VCPU kvm state.
257265
#[derive(Default, Clone, Serialize, Deserialize)]
258266
pub struct VcpuState {

0 commit comments

Comments
 (0)