From a5dc93df5b96728226bd3fcbc4298e6fb54f146d Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Fri, 13 Mar 2020 22:21:00 +0000 Subject: move MemoryParams to devices --- Cargo.lock | 1 - aarch64/src/lib.rs | 26 ++++++++++---------------- arch/src/lib.rs | 16 ++++++++++++++-- devices/src/lib.rs | 11 +++++++++++ src/linux.rs | 8 +++++++- src/wl.rs | 5 +++-- x86_64/Cargo.toml | 1 - x86_64/src/lib.rs | 31 +++++-------------------------- 8 files changed, 50 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index efbcb10..3db8e4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -851,7 +851,6 @@ dependencies = [ "kvm 0.1.0", "kvm_sys 0.1.0", "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)", - "msg_socket 0.1.0", "remain 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "resources 0.1.0", "sync 0.1.0", diff --git a/aarch64/src/lib.rs b/aarch64/src/lib.rs index d5d0066..20ce8f8 100644 --- a/aarch64/src/lib.rs +++ b/aarch64/src/lib.rs @@ -15,7 +15,7 @@ use arch::{ get_serial_cmdline, GetSerialCmdlineError, RunnableLinuxVm, SerialHardware, SerialParameters, VmComponents, VmImage, }; -use devices::{Bus, BusError, PciAddress, PciConfigMmio, PciDevice, PciInterruptPin}; +use devices::{Bus, BusError, MemoryParams, PciAddress, PciConfigMmio, PciDevice, PciInterruptPin}; use io_jail::Minijail; use msg_socket::MsgOnSocket; use remain::sorted; @@ -130,6 +130,7 @@ pub enum Error { CreateVm(sys_util::Error), GetSerialCmdline(GetSerialCmdlineError), InitrdLoadFailure(arch::LoadImageError), + JailServers(servers::ProxyError), KernelLoadFailure(arch::LoadImageError), KernelMissing, ReadPreferredTarget(sys_util::Error), @@ -163,6 +164,7 @@ impl Display for Error { CreateVm(e) => write!(f, "failed to create vm: {}", e), GetSerialCmdline(e) => write!(f, "failed to get serial cmdline: {}", e), InitrdLoadFailure(e) => write!(f, "initrd cound not be loaded: {}", e), + JailServers(e) => write!(f, "failed to jail servers: {}", e), KernelLoadFailure(e) => write!(f, "kernel cound not be loaded: {}", e), KernelMissing => write!(f, "aarch64 requires a kernel"), ReadPreferredTarget(e) => write!(f, "failed to read preferred target: {}", e), @@ -181,19 +183,6 @@ pub type Result = std::result::Result; impl std::error::Error for Error {} -#[derive(Clone, Copy, Debug, MsgOnSocket)] -pub struct MemoryParams { - pub size: u64, -} - -impl MemoryParams { - fn new(components: &VmComponents) -> Self { - MemoryParams { - size: components.memory_size, - } - } -} - /// Returns a Vec of the valid memory addresses. /// These should be used to configure the GuestMemory structure for the platfrom. pub fn arch_memory_regions(params: MemoryParams) -> Vec<(GuestAddress, u64)> { @@ -219,11 +208,13 @@ impl arch::LinuxArch for AArch64 { _ioapic_device_socket: VmIrqRequestSocket, serial_parameters: &BTreeMap<(SerialHardware, u8), SerialParameters>, serial_jail: Option, + servers: Vec, create_devices: F, ) -> Result where F: FnOnce( &GuestMemory, + MemoryParams, &mut Vm, &mut SystemAllocator, &EventFd, @@ -232,7 +223,7 @@ impl arch::LinuxArch for AArch64 { { let mut resources = Self::get_resource_allocator(components.memory_size, components.wayland_dmabuf); - let mem_params = MemoryParams::new(&components); + let mem_params = components.memory_params(); let mem = Self::setup_memory(mem_params)?; let kvm = Kvm::new().map_err(Error::CreateKvm)?; let mut vm = Vm::new(&kvm, mem.clone()).map_err(Error::CreateVm)?; @@ -269,7 +260,9 @@ impl arch::LinuxArch for AArch64 { // guest OS is trying to suspend. let suspend_evt = EventFd::new().map_err(Error::CreateEventFd)?; - let pci_devices = create_devices(&mem, &mut vm, &mut resources, &exit_evt) + let servers = jail_servers(servers).map_err(Error::JailServers)?; + + let pci_devices = create_devices(&mem, mem_params, &mut vm, &mut resources, &exit_evt) .map_err(|e| Error::CreateDevices(Box::new(e)))?; let (pci, pci_irqs, pid_debug_label_map) = arch::generate_pci_root( pci_devices, @@ -352,6 +345,7 @@ impl arch::LinuxArch for AArch64 { irq_chip, split_irqchip: None, gsi_relay: None, + servers, io_bus, mmio_bus, pid_debug_label_map, diff --git a/arch/src/lib.rs b/arch/src/lib.rs index db31a9d..8f957a8 100644 --- a/arch/src/lib.rs +++ b/arch/src/lib.rs @@ -20,8 +20,8 @@ use acpi_tables::sdt::SDT; use devices::split_irqchip_common::GsiRelay; use devices::virtio::VirtioDevice; use devices::{ - Bus, BusDevice, BusError, JailedDevice, PciAddress, PciDevice, PciDeviceError, PciInterruptPin, - PciRoot, + Bus, BusDevice, BusError, JailedDevice, MemoryParams, PciAddress, PciDevice, PciDeviceError, + PciInterruptPin, PciRoot, }; use io_jail::Minijail; use kvm::{IoeventAddress, Kvm, Vcpu, Vm}; @@ -61,6 +61,17 @@ pub struct VmComponents { pub acpi_sdts: Vec, } +impl VmComponents { + pub fn memory_params(&self) -> MemoryParams { + MemoryParams { + size: self.memory_size, + + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + has_bios: matches!(self.vm_image, VmImage::Bios(_)), + } + } +} + /// Holds the elements needed to run a Linux VM. Created by `build_vm`. pub struct RunnableLinuxVm { pub vm: Vm, @@ -116,6 +127,7 @@ pub trait LinuxArch { where F: FnOnce( &GuestMemory, + Self::MemoryParams, &mut Vm, &mut SystemAllocator, &EventFd, diff --git a/devices/src/lib.rs b/devices/src/lib.rs index 294a8cb..9d39fbd 100644 --- a/devices/src/lib.rs +++ b/devices/src/lib.rs @@ -48,3 +48,14 @@ pub use self::usb::host_backend::host_backend_device_provider::HostBackendDevice pub use self::usb::xhci::xhci_controller::XhciController; pub use self::vfio::{VfioContainer, VfioDevice}; pub use self::virtio::VirtioPciDevice; + +use msg_socket::MsgOnSocket; + +#[derive(Clone, Copy, Debug, MsgOnSocket)] +pub struct MemoryParams { + /// Physical memory size in bytes for the VM. + pub size: u64, + + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + pub has_bios: bool, +} diff --git a/src/linux.rs b/src/linux.rs index 7cb4b9c..f43d48f 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -73,6 +73,8 @@ use aarch64::AArch64 as Arch; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] use x86_64::X8664arch as Arch; +type MemoryParams = ::MemoryParams; + #[sorted] #[derive(Debug)] pub enum Error { @@ -1066,6 +1068,7 @@ fn create_console_device(cfg: &Config, param: &SerialParameters) -> DeviceResult fn create_virtio_devices( cfg: &Config, mem: &GuestMemory, + mem_params: MemoryParams, vm: &mut Vm, resources: &mut SystemAllocator, _exit_evt: &EventFd, @@ -1267,6 +1270,7 @@ fn create_virtio_devices( fn create_devices( cfg: &Config, mem: &GuestMemory, + mem_params: MemoryParams, vm: &mut Vm, resources: &mut SystemAllocator, exit_evt: &EventFd, @@ -1281,6 +1285,7 @@ fn create_devices( let stubs = create_virtio_devices( &cfg, mem, + mem_params, vm, resources, exit_evt, @@ -1827,10 +1832,11 @@ pub fn run_config(cfg: Config) -> Result<()> { ioapic_device_socket, &cfg.serial_parameters, simple_jail(&cfg, "serial")?, - |mem, vm, sys_allocator, exit_evt| { + |mem, mem_params, vm, sys_allocator, exit_evt| { create_devices( &cfg, mem, + mem_params, vm, sys_allocator, exit_evt, diff --git a/src/wl.rs b/src/wl.rs index 333be87..87dcec2 100644 --- a/src/wl.rs +++ b/src/wl.rs @@ -4,6 +4,7 @@ use devices::virtio::{ BincodeRequest, BincodeResponse, InterruptProxy, InterruptProxyEvent, MsgOnSocketRequest, MsgOnSocketResponse, VirtioDevice, Wl, }; +use devices::MemoryParams; use msg_socket::MsgSocket; use poly_msg_socket::PolyMsgSocket; use std::collections::BTreeMap; @@ -11,9 +12,9 @@ use std::fs::remove_file; use sys_util::{error, net::UnixSeqpacketListener, warn, GuestMemory}; #[cfg(any(target_arch = "arm", target_arch = "aarch64"))] -pub use aarch64::{arch_memory_regions, MemoryParams}; +pub use aarch64::arch_memory_regions; #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -pub use x86_64::{arch_memory_regions, MemoryParams}; +pub use x86_64::arch_memory_regions; type Socket = PolyMsgSocket; diff --git a/x86_64/Cargo.toml b/x86_64/Cargo.toml index cc247c3..49ef53f 100644 --- a/x86_64/Cargo.toml +++ b/x86_64/Cargo.toml @@ -15,7 +15,6 @@ kernel_loader = { path = "../kernel_loader" } kvm = { path = "../kvm" } kvm_sys = { path = "../kvm_sys" } libc = "*" -msg_socket = { path = "../msg_socket" } remain = "*" resources = { path = "../resources" } sync = { path = "../sync" } diff --git a/x86_64/src/lib.rs b/x86_64/src/lib.rs index 09940cf..3aff53d 100644 --- a/x86_64/src/lib.rs +++ b/x86_64/src/lib.rs @@ -62,12 +62,11 @@ use arch::{ }; use devices::split_irqchip_common::GsiRelay; use devices::{ - Ioapic, PciAddress, PciConfigIo, PciDevice, PciInterruptPin, Pic, IOAPIC_BASE_ADDRESS, - IOAPIC_MEM_LENGTH_BYTES, + Ioapic, MemoryParams, PciAddress, PciConfigIo, PciDevice, PciInterruptPin, Pic, + IOAPIC_BASE_ADDRESS, IOAPIC_MEM_LENGTH_BYTES, }; use io_jail::Minijail; use kvm::*; -use msg_socket::MsgOnSocket; use remain::sorted; use resources::SystemAllocator; use sync::Mutex; @@ -309,27 +308,6 @@ fn add_e820_entry(params: &mut boot_params, addr: u64, size: u64, mem_type: u32) Ok(()) } -#[derive(Clone, Copy, Debug, MsgOnSocket)] -pub struct MemoryParams { - /// Physical memory size in bytes for the VM. - pub size: u64, - pub has_bios: bool, -} - -impl MemoryParams { - fn new(components: &VmComponents) -> Self { - let has_bios = match components.vm_image { - VmImage::Bios(_) => true, - _ => false, - }; - - MemoryParams { - size: components.memory_size, - has_bios, - } - } -} - /// Returns a Vec of the valid memory addresses. /// These should be used to configure the GuestMemory structure for the platform. /// For x86_64 all addresses are valid from the start of the kernel except a @@ -374,13 +352,14 @@ impl arch::LinuxArch for X8664arch { where F: FnOnce( &GuestMemory, + MemoryParams, &mut Vm, &mut SystemAllocator, &EventFd, ) -> std::result::Result, Option)>, E>, E: StdError + 'static, { - let mem_params = MemoryParams::new(&components); + let mem_params = components.memory_params(); let mem = Self::setup_memory(mem_params)?; let mut resources = Self::get_resource_allocator(&mem, components.wayland_dmabuf); @@ -429,7 +408,7 @@ impl arch::LinuxArch for X8664arch { } else { (None, None) }; - let pci_devices = create_devices(&mem, &mut vm, &mut resources, &exit_evt) + let pci_devices = create_devices(&mem, mem_params, &mut vm, &mut resources, &exit_evt) .map_err(|e| Error::CreateDevices(Box::new(e)))?; let (pci, pci_irqs, pid_debug_label_map) = arch::generate_pci_root( pci_devices, -- cgit 1.4.1