Skip to content

Commit 2acb8e5

Browse files
committed
chore: move vmm::vstate code under vmm::arch
Move the rest of vstate architecture-specific code under `vmm::arch`. Signed-off-by: Babis Chalios <[email protected]>
1 parent 57e49ef commit 2acb8e5

File tree

11 files changed

+54
-52
lines changed

11 files changed

+54
-52
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use kvm_ioctls::VcpuFd;
1313

1414
use super::get_fdt_addr;
1515
use super::regs::*;
16-
use crate::vstate::kvm::OptionalCapabilities;
16+
use crate::arch::OptionalCapabilities;
1717
use crate::vstate::memory::GuestMemoryMmap;
1818

1919
/// Errors thrown while setting aarch64 registers.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33

44
/// Architecture specific KVM-related code
55
pub mod kvm;
6+
/// Architecture specific vCPU code
7+
pub mod vcpu;
8+
/// Architecture specific VM state code
9+
pub mod vm;

src/vmm/src/vstate/vcpu/aarch64.rs renamed to src/vmm/src/arch/aarch64/vstate/vcpu.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ use kvm_bindings::{
1313
use kvm_ioctls::*;
1414
use serde::{Deserialize, Serialize};
1515

16-
use crate::arch::EntryPoint;
1716
use crate::arch::aarch64::regs::{Aarch64RegisterVec, KVM_REG_ARM64_SVE_VLS};
1817
use crate::arch::aarch64::vcpu::{
1918
VcpuError as ArchError, get_all_registers, get_all_registers_ids, get_mpidr, get_mpstate,
2019
get_registers, set_mpstate, set_register, setup_boot_regs,
2120
};
21+
use crate::arch::{EntryPoint, OptionalCapabilities};
2222
use crate::cpu_config::aarch64::custom_cpu_template::VcpuFeatures;
2323
use crate::cpu_config::templates::CpuConfiguration;
2424
use crate::logger::{IncMetric, METRICS, error};
2525
use crate::vcpu::{VcpuConfig, VcpuError};
26-
use crate::vstate::kvm::OptionalCapabilities;
2726
use crate::vstate::memory::{Address, GuestMemoryMmap};
2827
use crate::vstate::vcpu::VcpuEmulation;
2928
use crate::vstate::vm::Vm;
@@ -60,14 +59,14 @@ pub struct KvmVcpu {
6059
/// KVM vcpu fd.
6160
pub fd: VcpuFd,
6261
/// Vcpu peripherals, such as buses
63-
pub(super) peripherals: Peripherals,
62+
pub peripherals: Peripherals,
6463
mpidr: u64,
6564
kvi: kvm_vcpu_init,
6665
}
6766

6867
/// Vcpu peripherals
6968
#[derive(Default, Debug)]
70-
pub(super) struct Peripherals {
69+
pub struct Peripherals {
7170
/// mmio bus.
7271
pub mmio_bus: Option<crate::devices::Bus>,
7372
}

src/vmm/src/vstate/vm/aarch64.rs renamed to src/vmm/src/arch/aarch64/vstate/vm.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
use kvm_ioctls::VmFd;
55
use serde::{Deserialize, Serialize};
66

7-
use super::VmError;
87
use crate::Kvm;
98
use crate::arch::aarch64::gic::GicState;
9+
use crate::vstate::vm::VmError;
1010

1111
/// Structure representing the current architecture's understand of what a "virtual machine" is.
1212
#[derive(Debug)]
1313
pub struct ArchVm {
14-
pub(super) fd: VmFd,
14+
/// KVM file descriptor of microVM
15+
pub fd: VmFd,
1516
// On aarch64 we need to keep around the fd obtained by creating the VGIC device.
1617
irqchip_handle: Option<crate::arch::aarch64::gic::GICDevice>,
1718
}
@@ -37,11 +38,13 @@ impl ArchVm {
3738
})
3839
}
3940

40-
pub(super) fn arch_pre_create_vcpus(&mut self, _: u8) -> Result<(), ArchVmError> {
41+
/// Pre-vCPU creation setup.
42+
pub fn arch_pre_create_vcpus(&mut self, _: u8) -> Result<(), ArchVmError> {
4143
Ok(())
4244
}
4345

44-
pub(super) fn arch_post_create_vcpus(&mut self, nr_vcpus: u8) -> Result<(), ArchVmError> {
46+
/// Post-vCPU creation setup.
47+
pub fn arch_post_create_vcpus(&mut self, nr_vcpus: u8) -> Result<(), ArchVmError> {
4548
// On aarch64, the vCPUs need to be created (i.e call KVM_CREATE_VCPU) before setting up the
4649
// IRQ chip because the `KVM_CREATE_VCPU` ioctl will return error if the IRQCHIP
4750
// was already initialized.

src/vmm/src/arch/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ pub mod aarch64;
1515
#[cfg(target_arch = "aarch64")]
1616
pub use aarch64::vstate::kvm::{Kvm, KvmArchError, OptionalCapabilities};
1717
#[cfg(target_arch = "aarch64")]
18+
pub use aarch64::vstate::vcpu::*;
19+
#[cfg(target_arch = "aarch64")]
20+
pub use aarch64::vstate::vm::{ArchVm, ArchVmError, VmState};
21+
#[cfg(target_arch = "aarch64")]
1822
pub use aarch64::{
1923
ConfigurationError, MMIO_MEM_SIZE, MMIO_MEM_START, arch_memory_regions, configure_system,
2024
get_kernel_start, initrd_load_addr, layout::CMDLINE_MAX_SIZE, layout::IRQ_BASE,
@@ -26,14 +30,18 @@ pub use aarch64::{
2630
pub mod x86_64;
2731

2832
#[cfg(target_arch = "x86_64")]
29-
pub use crate::arch::x86_64::{
33+
pub use x86_64::vstate::kvm::{Kvm, KvmArchError};
34+
#[cfg(target_arch = "x86_64")]
35+
pub use x86_64::vstate::vcpu::*;
36+
#[cfg(target_arch = "x86_64")]
37+
pub use x86_64::vstate::vm::{ArchVm, ArchVmError, VmState};
38+
#[cfg(target_arch = "x86_64")]
39+
pub use x86_64::{
3040
ConfigurationError, MMIO_MEM_SIZE, MMIO_MEM_START, arch_memory_regions, configure_system,
3141
get_kernel_start, initrd_load_addr, layout::APIC_ADDR, layout::CMDLINE_MAX_SIZE,
3242
layout::IOAPIC_ADDR, layout::IRQ_BASE, layout::IRQ_MAX, layout::SYSTEM_MEM_SIZE,
3343
layout::SYSTEM_MEM_START,
3444
};
35-
#[cfg(target_arch = "x86_64")]
36-
pub use x86_64::vstate::kvm::{Kvm, KvmArchError};
3745

3846
/// Types of devices that can get attached to this platform.
3947
#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, Serialize, Deserialize)]
@@ -83,7 +91,7 @@ impl fmt::Display for DeviceType {
8391
}
8492
}
8593

86-
/// Suported boot protocols for
94+
/// Supported boot protocols for
8795
#[derive(Debug, Copy, Clone, PartialEq)]
8896
pub enum BootProtocol {
8997
/// Linux 64-bit boot protocol

src/vmm/src/arch/x86_64/vstate/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33

44
/// Architecture specific KVM-related code
55
pub mod kvm;
6+
/// Architecture specific vCPU code
7+
pub mod vcpu;
8+
/// Architecture specific VM state code
9+
pub mod vm;

src/vmm/src/vstate/vcpu/x86_64.rs renamed to src/vmm/src/arch/x86_64/vstate/vcpu.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::arch::x86_64::regs::{SetupFpuError, SetupRegistersError, SetupSpecial
2525
use crate::cpu_config::x86_64::{CpuConfiguration, cpuid};
2626
use crate::logger::{IncMetric, METRICS};
2727
use crate::vstate::memory::GuestMemoryMmap;
28-
use crate::vstate::vcpu::{VcpuConfig, VcpuEmulation};
28+
use crate::vstate::vcpu::{VcpuConfig, VcpuEmulation, VcpuError};
2929
use crate::vstate::vm::Vm;
3030

3131
// Tolerance for TSC frequency expected variation.
@@ -145,7 +145,7 @@ pub struct KvmVcpu {
145145
/// KVM vcpu fd.
146146
pub fd: VcpuFd,
147147
/// Vcpu peripherals, such as buses
148-
pub(super) peripherals: Peripherals,
148+
pub peripherals: Peripherals,
149149
/// The list of MSRs to include in a VM snapshot, in the same order as KVM returned them
150150
/// from KVM_GET_MSR_INDEX_LIST
151151
msrs_to_save: Vec<u32>,
@@ -157,7 +157,7 @@ pub struct KvmVcpu {
157157

158158
/// Vcpu peripherals
159159
#[derive(Default, Debug)]
160-
pub(super) struct Peripherals {
160+
pub struct Peripherals {
161161
/// Pio bus.
162162
pub pio_bus: Option<crate::devices::Bus>,
163163
/// Mmio bus.
@@ -334,7 +334,7 @@ impl KvmVcpu {
334334
///
335335
/// # Errors
336336
///
337-
/// When [`kvm_ioctls::VcpuFd::get_tsc_khz`] errrors.
337+
/// When [`kvm_ioctls::VcpuFd::get_tsc_khz`] errors.
338338
pub fn get_tsc_khz(&self) -> Result<u32, GetTscError> {
339339
let res = self.fd.get_tsc_khz()?;
340340
Ok(res)
@@ -477,7 +477,7 @@ impl KvmVcpu {
477477
/// # Arguments
478478
///
479479
/// * `msr_index_iter`: Iterator over MSR indices.
480-
/// * `chunk_size`: Lenght of a chunk.
480+
/// * `chunk_size`: Length of a chunk.
481481
///
482482
/// # Errors
483483
///
@@ -504,7 +504,7 @@ impl KvmVcpu {
504504
.map_err(KvmVcpuError::VcpuGetMsrs)?;
505505
// GET_MSRS returns a number of successfully set msrs.
506506
// If number of set msrs is not equal to the length of
507-
// `msrs`, then the value retuned by GET_MSRS can act
507+
// `msrs`, then the value returned by GET_MSRS can act
508508
// as an index to the problematic msr.
509509
if nmsrs != chunk_size {
510510
Err(KvmVcpuError::VcpuGetMsr(msrs.as_slice()[nmsrs].index))
@@ -621,7 +621,7 @@ impl KvmVcpu {
621621
// in the state. If they are different, we need to
622622
// scale the TSC to the freq found in the state.
623623
// We accept values within a tolerance of 250 parts
624-
// per million beacuse it is common for TSC frequency
624+
// per million because it is common for TSC frequency
625625
// to differ due to calibration at boot time.
626626
let diff = (i64::from(self.get_tsc_khz()?) - i64::from(state_tsc_freq)).abs();
627627
// Cannot overflow since u32::MAX * 250 < i64::MAX
@@ -705,7 +705,7 @@ impl Peripherals {
705705
/// Runs the vCPU in KVM context and handles the kvm exit reason.
706706
///
707707
/// Returns error or enum specifying whether emulation was handled or interrupted.
708-
pub fn run_arch_emulation(&self, exit: VcpuExit) -> Result<VcpuEmulation, super::VcpuError> {
708+
pub fn run_arch_emulation(&self, exit: VcpuExit) -> Result<VcpuEmulation, VcpuError> {
709709
match exit {
710710
VcpuExit::IoIn(addr, data) => {
711711
if let Some(pio_bus) = &self.pio_bus {
@@ -728,7 +728,7 @@ impl Peripherals {
728728
// TODO: Are we sure we want to finish running a vcpu upon
729729
// receiving a vm exit that is not necessarily an error?
730730
error!("Unexpected exit reason on vcpu run: {:?}", unexpected_exit);
731-
Err(super::VcpuError::UnhandledKvmExit(format!(
731+
Err(VcpuError::UnhandledKvmExit(format!(
732732
"{:?}",
733733
unexpected_exit
734734
)))

src/vmm/src/vstate/vm/x86_64.rs renamed to src/vmm/src/arch/x86_64/vstate/vm.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ pub enum ArchVmError {
4848
/// Structure representing the current architecture's understand of what a "virtual machine" is.
4949
#[derive(Debug)]
5050
pub struct ArchVm {
51-
pub(super) fd: VmFd,
51+
/// KVM file descriptor of microVM
52+
pub fd: VmFd,
5253
msrs_to_save: MsrList,
5354
/// Size in bytes requiring to hold the dynamically-sized `kvm_xsave` struct.
5455
///
@@ -93,12 +94,14 @@ impl ArchVm {
9394
})
9495
}
9596

96-
pub(super) fn arch_pre_create_vcpus(&mut self, _: u8) -> Result<(), ArchVmError> {
97+
/// Pre-vCPU creation setup.
98+
pub fn arch_pre_create_vcpus(&mut self, _: u8) -> Result<(), ArchVmError> {
9799
// For x86_64 we need to create the interrupt controller before calling `KVM_CREATE_VCPUS`
98100
self.setup_irqchip()
99101
}
100102

101-
pub(super) fn arch_post_create_vcpus(&mut self, _: u8) -> Result<(), ArchVmError> {
103+
/// Post-vCPU creation setup.
104+
pub fn arch_post_create_vcpus(&mut self, _: u8) -> Result<(), ArchVmError> {
102105
Ok(())
103106
}
104107

src/vmm/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,10 @@ pub enum BuildMicrovmFromSnapshotError {
420420
TscFrequencyNotPresent,
421421
#[cfg(target_arch = "x86_64")]
422422
/// Could not get TSC to check if TSC scaling was required with the snapshot: {0}
423-
GetTsc(#[from] crate::vstate::vcpu::GetTscError),
423+
GetTsc(#[from] crate::arch::GetTscError),
424424
#[cfg(target_arch = "x86_64")]
425425
/// Could not set TSC scaling within the snapshot: {0}
426-
SetTsc(#[from] crate::vstate::vcpu::SetTscError),
426+
SetTsc(#[from] crate::arch::SetTscError),
427427
/// Failed to restore microVM state: {0}
428428
RestoreState(#[from] crate::vstate::vm::ArchVmError),
429429
/// Failed to update microVM configuration: {0}

src/vmm/src/vstate/vcpu/mod.rs renamed to src/vmm/src/vstate/vcpu.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use vmm_sys_util::errno;
2323
use vmm_sys_util::eventfd::EventFd;
2424

2525
use crate::FcExitCode;
26+
pub use crate::arch::{KvmVcpu, KvmVcpuConfigureError, KvmVcpuError, Peripherals, VcpuState};
2627
use crate::cpu_config::templates::{CpuConfiguration, GuestConfigError};
2728
#[cfg(feature = "gdb")]
2829
use crate::gdb::target::{GdbTargetError, get_raw_tid};
@@ -32,18 +33,6 @@ use crate::utils::signal::{Killable, register_signal_handler, sigrtmin};
3233
use crate::utils::sm::StateMachine;
3334
use crate::vstate::vm::Vm;
3435

35-
/// Module with aarch64 vCPU implementation.
36-
#[cfg(target_arch = "aarch64")]
37-
pub mod aarch64;
38-
/// Module with x86_64 vCPU implementation.
39-
#[cfg(target_arch = "x86_64")]
40-
pub mod x86_64;
41-
42-
#[cfg(target_arch = "aarch64")]
43-
pub use aarch64::{KvmVcpuError, *};
44-
#[cfg(target_arch = "x86_64")]
45-
pub use x86_64::{KvmVcpuError, *};
46-
4736
/// Signal number (SIGRTMIN) used to kick Vcpus.
4837
pub const VCPU_RTSIG_OFFSET: i32 = 0;
4938

0 commit comments

Comments
 (0)