Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Prerequisite for proper device manager. #4509

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(mmio): remove serial device specific method
Remove special method for serial device from MMIODeviceManager.
Now MMIODeviceManager does not have any device specific
methods.

Signed-off-by: Egor Lazarchuk <[email protected]>
  • Loading branch information
ShadowCurse committed Mar 16, 2024
commit a23efd7626dec6d36cf34efea7855de6d56fd6f7
19 changes: 17 additions & 2 deletions src/vmm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 663,27 @@ fn attach_legacy_devices_aarch64(
if cmdline_contains_console {
// Make stdout non-blocking.
set_stdout_nonblocking();
let serial = setup_serial_device(event_manager)?;

let serial = SerialDevice::new().map_err(VmmError::EventFd)?;

let device_info = vmm
.device_manager
.mmio_devices
.register_mmio_serial(vmm.vm.fd(), serial, None)
.allocate_device_info(1)
.map_err(VmmError::RegisterMMIODevice)?;
device_info
.register_kvm_irqfd(vmm.vm.fd(), serial.serial.interrupt_evt())
.map_err(VmmError::RegisterMMIODevice)?;

let serial = Arc::new(Mutex::new(BusDevice::Serial(serial)));
event_manager.add_subscriber(serial.clone());

let identifier = (DeviceType::Serial, DeviceType::Serial.to_string());
vmm.device_manager
.mmio_devices
.add_bus_device_with_info(identifier, serial, device_info.clone())
.map_err(VmmError::RegisterMMIODevice)?;

cmdline
.insert("earlycon", &format!("uart,mmio,0x{:08x}", device_info.addr))
.expect("All args are valid");
Expand Down
36 changes: 1 addition & 35 deletions src/vmm/src/device_manager/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 118,7 @@ impl MMIODeviceManager {
}

/// Allocates resources for a new device to be added.
fn allocate_device_info(&mut self, irq_count: u32) -> Result<MMIODeviceInfo, MmioError> {
pub fn allocate_device_info(&mut self, irq_count: u32) -> Result<MMIODeviceInfo, MmioError> {
let irqs = (0..irq_count)
.map(|_| self.irq_allocator.allocate_id())
.collect::<vm_allocator::Result<_>>()
Expand Down Expand Up @@ -199,40 199,6 @@ impl MMIODeviceManager {
)
}

#[cfg(target_arch = "aarch64")]
/// Register an early console at the specified MMIO configuration if given as parameter,
/// otherwise allocate a new MMIO resources for it.
pub fn register_mmio_serial(
&mut self,
vm: &VmFd,
serial: Arc<Mutex<BusDevice>>,
device_info_opt: Option<MMIODeviceInfo>,
) -> Result<MMIODeviceInfo, MmioError> {
// Create a new MMIODeviceInfo object on boot path or unwrap the
// existing object on restore path.
let device_info = if let Some(device_info) = device_info_opt {
device_info
} else {
self.allocate_device_info(1)?
};

device_info.register_kvm_irqfd(
vm,
serial
.lock()
.expect("Poisoned lock")
.serial_ref()
.unwrap()
.serial
.interrupt_evt(),
)?;

let identifier = (DeviceType::Serial, DeviceType::Serial.to_string());
// Register the newly created Serial object.
self.add_bus_device_with_info(identifier, serial, device_info.clone())?;
Ok(device_info)
}

/// Gets the information of the devices registered up to some point in time.
pub fn get_device_info(&self) -> &HashMap<(DeviceType, String), MMIODeviceInfo> {
&self.id_to_dev_info
Expand Down
21 changes: 16 additions & 5 deletions src/vmm/src/device_manager/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,8 @@ use vm_allocator::AllocPolicy;
use super::mmio::*;
#[cfg(target_arch = "aarch64")]
use crate::arch::DeviceType;
#[cfg(target_arch = "aarch64")]
use crate::devices::legacy::SerialDevice;
use crate::devices::virtio::balloon::persist::{BalloonConstructorArgs, BalloonState};
use crate::devices::virtio::balloon::{Balloon, BalloonError};
use crate::devices::virtio::block::device::Block;
Expand Down Expand Up @@ -372,8 374,7 @@ impl<'a> Persist<'a> for MMIODeviceManager {
{
for state in &state.legacy_devices {
if state.type_ == DeviceType::Serial {
let serial =
crate::builder::setup_serial_device(constructor_args.event_manager)?;
let serial = SerialDevice::new().map_err(crate::VmmError::EventFd)?;

dev_manager
.address_allocator
Expand All @@ -386,10 387,20 @@ impl<'a> Persist<'a> for MMIODeviceManager {
DevicePersistError::DeviceManager(super::mmio::MmioError::Allocator(e))
})?;

dev_manager.register_mmio_serial(
vm,
state
.device_info
.register_kvm_irqfd(vm, serial.serial.interrupt_evt())?;

let serial = Arc::new(Mutex::new(BusDevice::Serial(serial)));
constructor_args
.event_manager
.add_subscriber(serial.clone());

let identifier = (DeviceType::Serial, DeviceType::Serial.to_string());
dev_manager.add_bus_device_with_info(
identifier,
serial,
Some(state.device_info.clone()),
state.device_info.clone(),
)?;
}
if state.type_ == DeviceType::Rtc {
Expand Down