Skip to content

Commit

Permalink
Refactor in progress. Needs further work to ideate on refactor path i…
Browse files Browse the repository at this point in the history
…n addition to continuing to reduce compile time conditionals.

Signed-off-by: Andrew Yao <[email protected]>
Co-authored-by: Himanshu Reddy <[email protected]>
Co-authored-by: Goldin Vo <[email protected]>
Signed-off-by: Andrew Yao <[email protected]>
  • Loading branch information
3 people committed May 7, 2024
1 parent 68980ac commit 71198b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
43 changes: 22 additions & 21 deletions src/vmm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use crate::vmm_config::machine_config::{VmConfig, VmConfigError};
use crate::vstate::memory::{GuestAddress, GuestMemory, GuestMemoryExtension, GuestMemoryMmap};
use crate::vstate::vcpu::{Vcpu, VcpuConfig, VcpuError};
use crate::vstate::vm::Vm;
use crate::{device_manager, EventManager, Vmm, VmmError, X86_64_Devices};
use crate::{device_manager, EventManager, Vmm, VmmError};


/// Errors associated with starting the instance.
Expand Down Expand Up @@ -148,6 +148,7 @@ impl std::convert::From<linux_loader::cmdline::Error> for StartMicrovmError {


// this module is for code specific to the aarch64 architecture
#[cfg(target_arch = "aarch64")]
mod Aarch64 {
use super::*;
use utils::eventfd::EventFd;
Expand Down Expand Up @@ -192,6 +193,7 @@ mod Aarch64 {
vcpus
};

#[cfg(target_arch = "aarch64")]
let vmm = Vmm {
events_observer: Some(std::io::stdin()),
instance_info: instance_info.clone(),
Expand All @@ -203,7 +205,6 @@ mod Aarch64 {
vcpus_exit_evt,
resource_allocator,
mmio_device_manager,
devices_for_x86_64 : None,
};

Ok((vmm, vcpus))
Expand Down Expand Up @@ -281,11 +282,6 @@ mod X86_64 {

(vcpus, pio_device_manager)
};

let x86_dev = X86_64_Devices {
pio_device_manager,
acpi_device_manager
};

let vmm = Vmm {
events_observer: Some(std::io::stdin()),
Expand All @@ -298,7 +294,8 @@ mod X86_64 {
vcpus_exit_evt,
resource_allocator,
mmio_device_manager,
devices_for_x86_64: Some(x86_dev),
pio_device_manager,
acpi_device_manager,
};

Ok((vmm, vcpus))
Expand Down Expand Up @@ -640,13 +637,13 @@ pub fn build_microvm_from_snapshot(
vm: vmm.vm.fd(),
};

vmm.devices_for_x86_64.unwrap().acpi_device_manager =
vmm.acpi_device_manager =
ACPIDeviceManager::restore(acpi_ctor_args, &microvm_state.acpi_dev_state)?;

// Inject the notification to VMGenID that we have resumed from a snapshot.
// This needs to happen before we resume vCPUs, so that we minimize the time between vCPUs
// resuming and notification being handled by the driver.
vmm.devices_for_x86_64.unwrap().acpi_device_manager
vmm.acpi_device_manager
.notify_vmgenid()
.map_err(BuildMicrovmFromSnapshotError::VMGenIDUpdate)?;
}
Expand Down Expand Up @@ -945,7 +942,7 @@ pub fn configure_system_for_boot(
&vmm.guest_memory,
&mut vmm.resource_allocator,
&vmm.mmio_device_manager,
&vmm.devices_for_x86_64.unwrap().acpi_device_manager,
&vmm.acpi_device_manager,
vcpus,
)?;
}
Expand Down Expand Up @@ -1016,7 +1013,7 @@ fn attach_vmgenid_device(vmm: &mut Vmm) -> Result<(), StartMicrovmError> {
let vmgenid = VmGenId::new(&vmm.guest_memory, &mut vmm.resource_allocator)
.map_err(StartMicrovmError::CreateVMGenID)?;

vmm.devices_for_x86_64.unwrap().acpi_device_manager
vmm.acpi_device_manager
.attach_vmgenid(vmgenid, vmm.vm.fd())
.map_err(StartMicrovmError::AttachVmgenidDevice)?;

Expand Down Expand Up @@ -1131,6 +1128,12 @@ pub(crate) fn set_stdout_nonblocking() {
}
}

// This wrapper is here since this function is called in other files
#[cfg(target_arch = "x86_64")]
pub fn setup_interrupt_controller(vm: &mut Vm) -> Result<(), StartMicrovmError> {
return X86_64::setup_interrupt_controller(vm);
}

#[cfg(test)]
pub mod tests {
use std::io::Write;
Expand Down Expand Up @@ -1239,18 +1242,15 @@ pub mod tests {
.unwrap();

#[cfg(target_arch = "x86_64")]
setup_interrupt_controller(&mut vm).unwrap();
X86_64::setup_interrupt_controller(&mut vm).unwrap();

#[cfg(target_arch = "aarch64")]
{
let exit_evt = EventFd::new(libc::EFD_NONBLOCK).unwrap();
let _vcpu = Vcpu::new(1, &vm, exit_evt).unwrap();
setup_interrupt_controller(&mut vm, 1).unwrap();
Aarch64::setup_interrupt_controller(&mut vm, 1).unwrap();
}
let x86_dev = X86_64_Devices {
pio_device_manager,
acpi_device_manager
};



Vmm {
Expand All @@ -1264,7 +1264,8 @@ pub mod tests {
vcpus_exit_evt,
resource_allocator: ResourceAllocator::new().unwrap(),
mmio_device_manager,
devices_for_x86_64: Some(x86_dev),
pio_device_manager,
acpi_device_manager
}
}

Expand Down Expand Up @@ -1385,7 +1386,7 @@ pub mod tests {
#[cfg(target_arch = "x86_64")]
pub(crate) fn insert_vmgenid_device(vmm: &mut Vmm) {
attach_vmgenid_device(vmm).unwrap();
assert!(vmm.devices_for_x86_64.unwrap().acpi_device_manager.vmgenid.is_some());
assert!(vmm.acpi_device_manager.vmgenid.is_some());
}

pub(crate) fn insert_balloon_device(
Expand Down Expand Up @@ -1478,7 +1479,7 @@ pub mod tests {
let evfd = EventFd::new(libc::EFD_NONBLOCK).unwrap();

#[cfg(target_arch = "x86_64")]
setup_interrupt_controller(&mut vm).unwrap();
X86_64::setup_interrupt_controller(&mut vm).unwrap();

let vcpu_vec = create_vcpus(&vm, vcpu_count, &evfd).unwrap();
assert_eq!(vcpu_vec.len(), vcpu_count as usize);
Expand Down
23 changes: 10 additions & 13 deletions src/vmm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,7 @@ pub enum DumpCpuConfigError {
}


#[derive(Debug)]
pub struct X86_64_Devices {
pio_device_manager: PortIODeviceManager,
acpi_device_manager: ACPIDeviceManager
}


/// Contains the state and associated methods required for the Firecracker VMM.
#[derive(Debug)]
pub struct Vmm {
events_observer: Option<std::io::Stdin>,
Expand All @@ -330,10 +324,13 @@ pub struct Vmm {
resource_allocator: ResourceAllocator,
// Guest VM devices.
mmio_device_manager: MMIODeviceManager,
// Devices specifically for the x86_64 platform
devices_for_x86_64: Option<X86_64_Devices>,
#[cfg(target_arch = "x86_64")]
pio_device_manager: PortIODeviceManager,
#[cfg(target_arch = "x86_64")]
acpi_device_manager: ACPIDeviceManager,
}


impl Vmm {
/// Gets Vmm version.
pub fn version(&self) -> String {
Expand Down Expand Up @@ -396,7 +393,7 @@ impl Vmm {
vcpu.set_mmio_bus(self.mmio_device_manager.bus.clone());
#[cfg(target_arch = "x86_64")]
vcpu.kvm_vcpu
.set_pio_bus(self.devices_for_x86_64.unwrap().pio_device_manager.io_bus.clone());
.set_pio_bus(self.pio_device_manager.io_bus.clone());

self.vcpus_handles
.push(vcpu.start_threaded(vcpu_seccomp_filter.clone(), barrier.clone())?);
Expand Down Expand Up @@ -495,7 +492,7 @@ impl Vmm {
// // serialization. For now we set that bit manually

let mut guard = self
.devices_for_x86_64.unwrap().pio_device_manager
.pio_device_manager
.stdio_serial
.lock()
.expect("Poisoned lock");
Expand Down Expand Up @@ -556,7 +553,7 @@ impl Vmm {
/// Injects CTRL+ALT+DEL keystroke combo in the i8042 device.
#[cfg(target_arch = "x86_64")]
pub fn send_ctrl_alt_del(&mut self) -> Result<(), VmmError> {
self.devices_for_x86_64.unwrap().pio_device_manager
self.pio_device_manager
.i8042
.lock()
.expect("i8042 lock was poisoned")
Expand Down Expand Up @@ -586,7 +583,7 @@ impl Vmm {

let memory_state = self.guest_memory().describe();
#[cfg(target_arch = "x86_64")]
let acpi_dev_state = self.devices_for_x86_64.unwrap().acpi_device_manager.save();
let acpi_dev_state = self.acpi_device_manager.save();

Ok(MicrovmState {
vm_info: vm_info.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ mod tests {
#[cfg(target_arch = "x86_64")]
vm_state: vmm.vm.save_state().unwrap(),
#[cfg(target_arch = "x86_64")]
acpi_dev_state: vmm.devices_for_x86_64.unwrap().acpi_device_manager.save(),
acpi_dev_state: vmm.acpi_device_manager.save(),
};

let mut buf = vec![0; 10000];
Expand Down

0 comments on commit 71198b3

Please sign in to comment.