Skip to content

Commit e512db4

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 8375255 commit e512db4

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed
File renamed without changes.
File renamed without changes.

src/vmm/src/arch/mod.rs

Lines changed: 9 additions & 1 deletion
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,
@@ -34,6 +38,10 @@ pub use crate::arch::x86_64::{
3438
};
3539
#[cfg(target_arch = "x86_64")]
3640
pub use x86_64::vstate::kvm::{Kvm, KvmArchError};
41+
#[cfg(target_arch = "x86_64")]
42+
pub use x86_64::vstate::vcpu::*;
43+
#[cfg(target_arch = "x86_64")]
44+
pub use x86_64::vstate::vm::{ArchVm, ArchVmError, VmState};
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/vstate/vcpu/mod.rs renamed to src/vmm/src/vstate/vcpu.rs

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

2525
use crate::FcExitCode;
26+
pub use crate::arch::{
27+
GetTscError, KvmVcpu, KvmVcpuConfigureError, KvmVcpuError, Peripherals, SetTscError, VcpuState,
28+
};
2629
use crate::cpu_config::templates::{CpuConfiguration, GuestConfigError};
2730
#[cfg(feature = "gdb")]
2831
use crate::gdb::target::{GdbTargetError, get_raw_tid};
@@ -32,18 +35,6 @@ use crate::utils::signal::{Killable, register_signal_handler, sigrtmin};
3235
use crate::utils::sm::StateMachine;
3336
use crate::vstate::vm::Vm;
3437

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-
4738
/// Signal number (SIGRTMIN) used to kick Vcpus.
4839
pub const VCPU_RTSIG_OFFSET: i32 = 0;
4940

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,10 @@ use kvm_bindings::{KVM_MEM_LOG_DIRTY_PAGES, kvm_userspace_memory_region};
99
use kvm_ioctls::VmFd;
1010
use vmm_sys_util::eventfd::EventFd;
1111

12+
use crate::Vcpu;
13+
pub use crate::arch::{ArchVm as Vm, ArchVmError, VmState};
1214
use crate::logger::info;
1315
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
14-
15-
#[cfg(target_arch = "x86_64")]
16-
#[path = "x86_64.rs"]
17-
mod arch;
18-
#[cfg(target_arch = "aarch64")]
19-
#[path = "aarch64.rs"]
20-
mod arch;
21-
22-
pub use arch::{ArchVm as Vm, ArchVmError, VmState};
23-
24-
use crate::Vcpu;
2516
use crate::vstate::vcpu::VcpuError;
2617

2718
/// Errors associated with the wrappers over KVM ioctls.
@@ -43,7 +34,8 @@ pub enum VmError {
4334

4435
/// Contains Vm functions that are usable across CPU architectures
4536
impl Vm {
46-
fn create_vm(kvm: &crate::vstate::kvm::Kvm) -> Result<VmFd, VmError> {
37+
/// Create a KVM VM
38+
pub fn create_vm(kvm: &crate::vstate::kvm::Kvm) -> Result<VmFd, VmError> {
4739
// It is known that KVM_CREATE_VM occasionally fails with EINTR on heavily loaded machines
4840
// with many VMs.
4941
//
@@ -57,7 +49,7 @@ impl Vm {
5749
// KVM_CREATE_VM returns EINTR.
5850
// https://lore.kernel.org/qemu-devel/[email protected]/
5951
//
60-
// To mitigate it, QEMU does an inifinite retry on EINTR that greatly improves reliabiliy:
52+
// To mitigate it, QEMU does an infinite retry on EINTR that greatly improves reliabiliy:
6153
// - https://github.com/qemu/qemu/commit/94ccff133820552a859c0fb95e33a539e0b90a75
6254
// - https://github.com/qemu/qemu/commit/bbde13cd14ad4eec18529ce0bf5876058464e124
6355
//

0 commit comments

Comments
 (0)