Skip to content

Commit de011a0

Browse files
committed
refactor: Split struct Vm into arch-specific structs
Similarly to what we do in the vcpu module for the Vcpu/KvmVcpu structs. The difference is that we do not implement any function on `ArchVm`, so that common fields like `fd` can live in `Vm`, instead of needing to be duplicated into all the `ArchVm` types. Signed-off-by: Patrick Roy <[email protected]>
1 parent 5eaf2eb commit de011a0

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

src/vmm/src/vstate/vm/aarch64.rs

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

4+
use kvm_ioctls::VmFd;
45
use serde::{Deserialize, Serialize};
56

7+
use super::VmError;
68
use crate::arch::aarch64::gic::GicState;
7-
use crate::Vm;
9+
use crate::vstate::kvm::Kvm;
10+
11+
/// Structure representing the current architecture's understand of what a "virtual machine" is.
12+
#[derive(Debug)]
13+
pub struct ArchVm {
14+
pub(super) fd: VmFd,
15+
// On aarch64 we need to keep around the fd obtained by creating the VGIC device.
16+
irqchip_handle: Option<crate::arch::aarch64::gic::GICDevice>,
17+
}
818

919
/// Error type for [`Vm::restore_state`]
1020
#[derive(Debug, PartialEq, Eq, thiserror::Error, displaydoc::Display)]
@@ -17,7 +27,16 @@ pub enum ArchVmError {
1727
RestoreGic(crate::arch::aarch64::gic::GicError),
1828
}
1929

20-
impl Vm {
30+
impl ArchVm {
31+
/// Create a new `Vm` struct.
32+
pub fn new(kvm: &Kvm) -> Result<ArchVm, VmError> {
33+
let fd = kvm.fd.create_vm().map_err(VmError::VmFd)?;
34+
Ok(ArchVm {
35+
fd,
36+
irqchip_handle: None,
37+
})
38+
}
39+
2140
/// Creates the GIC (Global Interrupt Controller).
2241
pub fn setup_irqchip(&mut self, vcpu_count: u8) -> Result<(), ArchVmError> {
2342
self.irqchip_handle = Some(

src/vmm/src/vstate/vm/mod.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
use kvm_bindings::{kvm_userspace_memory_region, KVM_MEM_LOG_DIRTY_PAGES};
99
use kvm_ioctls::VmFd;
1010

11-
#[cfg(target_arch = "aarch64")]
12-
use crate::arch::aarch64::gic::GICDevice;
1311
#[cfg(target_arch = "x86_64")]
1412
use crate::utils::u64_to_usize;
15-
use crate::vstate::kvm::Kvm;
1613
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
1714

1815
#[cfg(target_arch = "x86_64")]
@@ -22,7 +19,7 @@ mod arch;
2219
#[path = "aarch64.rs"]
2320
mod arch;
2421

25-
pub use arch::{ArchVmError, VmState};
22+
pub use arch::{ArchVm as Vm, ArchVmError, VmState};
2623

2724
/// Errors associated with the wrappers over KVM ioctls.
2825
/// Needs `rustfmt::skip` to make multiline comments work
@@ -39,38 +36,8 @@ pub enum VmError {
3936
Arch(#[from] ArchVmError),
4037
}
4138

42-
/// A wrapper around creating and using a VM.
43-
#[derive(Debug)]
44-
pub struct Vm {
45-
fd: VmFd,
46-
47-
// Arm specific fields.
48-
// On aarch64 we need to keep around the fd obtained by creating the VGIC device.
49-
#[cfg(target_arch = "aarch64")]
50-
irqchip_handle: Option<GICDevice>,
51-
}
52-
5339
/// Contains Vm functions that are usable across CPU architectures
5440
impl Vm {
55-
/// Create a new `Vm` struct.
56-
pub fn new(kvm: &Kvm) -> Result<Self, VmError> {
57-
// Create fd for interacting with kvm-vm specific functions.
58-
let vm_fd = kvm.fd.create_vm().map_err(VmError::VmFd)?;
59-
60-
#[cfg(target_arch = "aarch64")]
61-
{
62-
Ok(Vm {
63-
fd: vm_fd,
64-
irqchip_handle: None,
65-
})
66-
}
67-
68-
#[cfg(target_arch = "x86_64")]
69-
{
70-
Ok(Vm { fd: vm_fd })
71-
}
72-
}
73-
7441
/// Initializes the guest memory.
7542
pub fn memory_init(&self, guest_mem: &GuestMemoryMmap) -> Result<(), VmError> {
7643
self.set_kvm_memory_regions(guest_mem)?;
@@ -122,6 +89,7 @@ impl Vm {
12289
pub(crate) mod tests {
12390
use super::*;
12491
use crate::test_utils::single_region_mem;
92+
use crate::vstate::kvm::Kvm;
12593
use crate::vstate::memory::GuestMemoryMmap;
12694

12795
// Auxiliary function being used throughout the tests.

src/vmm/src/vstate/vm/x86_64.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use kvm_bindings::{
77
kvm_clock_data, kvm_irqchip, kvm_pit_config, kvm_pit_state2, KVM_CLOCK_TSC_STABLE,
88
KVM_IRQCHIP_IOAPIC, KVM_IRQCHIP_PIC_MASTER, KVM_IRQCHIP_PIC_SLAVE, KVM_PIT_SPEAKER_DUMMY,
99
};
10+
use kvm_ioctls::VmFd;
1011
use serde::{Deserialize, Serialize};
1112

12-
use crate::Vm;
13+
use crate::vstate::vm::VmError;
1314

1415
/// Error type for [`Vm::restore_state`]
1516
#[allow(missing_docs)]
@@ -40,7 +41,19 @@ pub enum ArchVmError {
4041
VmSetIrqChip(kvm_ioctls::Error),
4142
}
4243

43-
impl Vm {
44+
/// Structure representing the current architecture's understand of what a "virtual machine" is.
45+
#[derive(Debug)]
46+
pub struct ArchVm {
47+
pub(super) fd: VmFd,
48+
}
49+
50+
impl ArchVm {
51+
/// Create a new `Vm` struct.
52+
pub fn new(kvm: &crate::vstate::kvm::Kvm) -> Result<ArchVm, VmError> {
53+
let fd = kvm.fd.create_vm().map_err(VmError::VmFd)?;
54+
Ok(ArchVm { fd })
55+
}
56+
4457
/// Restores the KVM VM state.
4558
///
4659
/// # Errors

0 commit comments

Comments
 (0)