Skip to content

Commit 96870ca

Browse files
committed
refactor(builder): address comments
address comments Signed-off-by: tommady <[email protected]>
1 parent 39b2264 commit 96870ca

File tree

3 files changed

+72
-57
lines changed

3 files changed

+72
-57
lines changed

src/vmm/src/builder.rs

+38-33
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ pub mod aarch64 {
274274
vcpus.push(vcpu);
275275
}
276276

277-
setup_interrupt_controller(vm, vcpu_count)?;
278-
279277
Ok(vcpus)
280278
}
281279

@@ -317,6 +315,9 @@ pub mod aarch64 {
317315
let vcpus = create_vcpus(&mut vmm.vm, vm_config.vcpu_count, &vmm.vcpus_exit_evt)
318316
.map_err(StartMicrovmError::Internal)?;
319317

318+
setup_interrupt_controller(&mut vmm.vm, vm_config.vcpu_count)
319+
.map_err(StartMicrovmError::Internal)?;
320+
320321
Ok((vmm, vcpus))
321322
}
322323
}
@@ -459,6 +460,33 @@ pub mod x86_64 {
459460
kvm_capabilities,
460461
)?;
461462

463+
let pio_device_manager = {
464+
// Serial device setup.
465+
let serial_device = setup_serial_device(std::io::stdin(), io::stdout())
466+
.map_err(StartMicrovmError::Internal)?;
467+
468+
// x86_64 uses the i8042 reset event as the Vmm exit event.
469+
let reset_evt = vmm
470+
.vcpus_exit_evt
471+
.try_clone()
472+
.map_err(VmmError::EventFd)
473+
.map_err(StartMicrovmError::Internal)?;
474+
475+
setup_interrupt_controller(&mut vmm.vm).map_err(StartMicrovmError::Internal)?;
476+
477+
// create pio dev manager with legacy devices
478+
let pio_device_manager = {
479+
// TODO Remove these unwraps.
480+
let mut pio_dev_mgr = PortIODeviceManager::new(serial_device, reset_evt).unwrap();
481+
pio_dev_mgr.register_devices(vmm.vm.fd()).unwrap();
482+
pio_dev_mgr
483+
};
484+
485+
pio_device_manager
486+
};
487+
488+
vmm.pio_device_manager = Some(pio_device_manager);
489+
462490
let vcpus = create_vcpus(&mut vmm.vm, vm_config.vcpu_count, &vmm.vcpus_exit_evt)
463491
.map_err(StartMicrovmError::Internal)?;
464492

@@ -499,33 +527,6 @@ fn build_vmm(
499527
// Instantiate ACPI device manager.
500528
let acpi_device_manager = ACPIDeviceManager::new();
501529

502-
// For x86_64 we need to create the interrupt controller before calling `KVM_CREATE_VCPUS`
503-
// while on aarch64 we need to do it the other way around.
504-
#[cfg(target_arch = "x86_64")]
505-
let pio_device_manager = {
506-
// Serial device setup.
507-
let serial_device =
508-
setup_serial_device(std::io::stdin(), io::stdout()).map_err(Internal)?;
509-
510-
// x86_64 uses the i8042 reset event as the Vmm exit event.
511-
let reset_evt = vcpus_exit_evt
512-
.try_clone()
513-
.map_err(VmmError::EventFd)
514-
.map_err(Internal)?;
515-
516-
x86_64::setup_interrupt_controller(&mut vm).map_err(Internal)?;
517-
518-
// create pio dev manager with legacy devices
519-
let pio_device_manager = {
520-
// TODO Remove these unwraps.
521-
let mut pio_dev_mgr = PortIODeviceManager::new(serial_device, reset_evt).unwrap();
522-
pio_dev_mgr.register_devices(vm.fd()).unwrap();
523-
pio_dev_mgr
524-
};
525-
526-
pio_device_manager
527-
};
528-
529530
Ok(Vmm {
530531
events_observer: Some(std::io::stdin()),
531532
instance_info: instance_info.clone(),
@@ -538,7 +539,7 @@ fn build_vmm(
538539
resource_allocator,
539540
mmio_device_manager,
540541
#[cfg(target_arch = "x86_64")]
541-
pio_device_manager,
542+
pio_device_manager: None,
542543
acpi_device_manager,
543544
})
544545
}
@@ -590,7 +591,9 @@ pub fn build_microvm_for_boot(
590591
cpu_template.kvm_capabilities.clone(),
591592
)?;
592593
#[cfg(target_arch = "x86_64")]
593-
event_manager.add_subscriber(vmm.pio_device_manager.stdio_serial.clone());
594+
if let Some(pio_manager) = &vmm.pio_device_manager {
595+
event_manager.add_subscriber(pio_manager.stdio_serial.clone());
596+
}
594597

595598
#[cfg(target_arch = "aarch64")]
596599
let (mut vmm, mut vcpus) = aarch64::create_vmm_and_vcpus(
@@ -801,7 +804,9 @@ pub fn build_microvm_from_snapshot(
801804
microvm_state.vm_state.kvm_cap_modifiers.clone(),
802805
)?;
803806
#[cfg(target_arch = "x86_64")]
804-
event_manager.add_subscriber(vmm.pio_device_manager.stdio_serial.clone());
807+
if let Some(pio_manager) = &vmm.pio_device_manager {
808+
event_manager.add_subscriber(pio_manager.stdio_serial.clone());
809+
}
805810
#[cfg(target_arch = "aarch64")]
806811
let (mut vmm, mut vcpus) = aarch64::create_vmm_and_vcpus(
807812
instance_info,
@@ -1281,7 +1286,7 @@ pub mod tests {
12811286
resource_allocator: ResourceAllocator::new().unwrap(),
12821287
mmio_device_manager,
12831288
#[cfg(target_arch = "x86_64")]
1284-
pio_device_manager,
1289+
pio_device_manager: Some(pio_device_manager),
12851290
acpi_device_manager,
12861291
}
12871292
}

src/vmm/src/device_manager/persist.rs

+3
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ impl<'a> Persist<'a> for MMIODeviceManager {
423423
for state in &state.legacy_devices {
424424
if state.type_ == DeviceType::Serial {
425425
let serial = setup_serial_device(std::io::stdin(), std::io::stdout())?;
426+
constructor_args
427+
.event_manager
428+
.add_subscriber(serial.clone());
426429

427430
constructor_args
428431
.resource_allocator

src/vmm/src/lib.rs

+31-24
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ pub struct Vmm {
322322
// Guest VM devices.
323323
mmio_device_manager: MMIODeviceManager,
324324
#[cfg(target_arch = "x86_64")]
325-
pio_device_manager: PortIODeviceManager,
325+
pio_device_manager: Option<PortIODeviceManager>,
326326
acpi_device_manager: ACPIDeviceManager,
327327
}
328328

@@ -387,8 +387,9 @@ impl Vmm {
387387
for mut vcpu in vcpus.drain(..) {
388388
vcpu.set_mmio_bus(self.mmio_device_manager.bus.clone());
389389
#[cfg(target_arch = "x86_64")]
390-
vcpu.kvm_vcpu
391-
.set_pio_bus(self.pio_device_manager.io_bus.clone());
390+
if let Some(pio_device_manager) = &self.pio_device_manager {
391+
vcpu.kvm_vcpu.set_pio_bus(pio_device_manager.io_bus.clone());
392+
}
392393

393394
self.vcpus_handles
394395
.push(vcpu.start_threaded(vcpu_seccomp_filter.clone(), barrier.clone())?);
@@ -480,33 +481,39 @@ impl Vmm {
480481
}
481482

482483
#[cfg(target_arch = "x86_64")]
483-
{
484-
let mut guard = self
485-
.pio_device_manager
486-
.stdio_serial
487-
.lock()
488-
.expect("Poisoned lock");
489-
let serial = guard.serial_mut().unwrap();
490-
491-
serial
492-
.serial
493-
.write(IER_RDA_OFFSET, IER_RDA_BIT)
494-
.map_err(|_| EmulateSerialInitError(std::io::Error::last_os_error()))?;
495-
Ok(())
484+
match &self.pio_device_manager {
485+
Some(pio_manager) => {
486+
let mut guard = pio_manager.stdio_serial.lock().expect("Poisoned lock");
487+
let serial = guard.serial_mut().unwrap();
488+
489+
serial
490+
.serial
491+
.write(IER_RDA_OFFSET, IER_RDA_BIT)
492+
.map_err(|_| EmulateSerialInitError(std::io::Error::last_os_error()))?;
493+
Ok(())
494+
}
495+
None => Err(EmulateSerialInitError(std::io::Error::new(
496+
std::io::ErrorKind::NotFound,
497+
"pio_device_manager is None",
498+
))),
496499
}
497500
}
498501

499502
/// Injects CTRL+ALT+DEL keystroke combo in the i8042 device.
500503
#[cfg(target_arch = "x86_64")]
501504
pub fn send_ctrl_alt_del(&mut self) -> Result<(), VmmError> {
502-
self.pio_device_manager
503-
.i8042
504-
.lock()
505-
.expect("i8042 lock was poisoned")
506-
.i8042_device_mut()
507-
.unwrap()
508-
.trigger_ctrl_alt_del()
509-
.map_err(VmmError::I8042Error)
505+
match &self.pio_device_manager {
506+
Some(pio_device_manager) => pio_device_manager
507+
.i8042
508+
.lock()
509+
.expect("i8042 lock was poisoned")
510+
.i8042_device_mut()
511+
.unwrap()
512+
.trigger_ctrl_alt_del()
513+
.map_err(VmmError::I8042Error),
514+
// should be unreachable
515+
None => panic!("pio_device_manager is None"),
516+
}
510517
}
511518

512519
/// Saves the state of a paused Microvm.

0 commit comments

Comments
 (0)