2018-02-14 06:09:43 +00:00
|
|
|
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2018-10-03 17:22:32 +00:00
|
|
|
extern crate devices;
|
2018-07-24 00:58:09 +00:00
|
|
|
extern crate io_jail;
|
2018-02-14 06:09:43 +00:00
|
|
|
extern crate kernel_cmdline;
|
|
|
|
extern crate kvm;
|
|
|
|
extern crate libc;
|
2018-10-03 17:22:32 +00:00
|
|
|
extern crate resources;
|
|
|
|
extern crate sys_util;
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
use std::fmt;
|
2018-02-14 06:09:43 +00:00
|
|
|
use std::fs::File;
|
2018-07-24 00:58:09 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2018-02-14 06:09:43 +00:00
|
|
|
use std::result;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
use devices::virtio::VirtioDevice;
|
2018-10-03 17:22:32 +00:00
|
|
|
use devices::{
|
|
|
|
Bus, BusError, PciDevice, PciDeviceError, PciInterruptPin, PciRoot, ProxyDevice, Serial,
|
|
|
|
};
|
2018-07-24 00:58:09 +00:00
|
|
|
use io_jail::Minijail;
|
2018-10-09 19:44:21 +00:00
|
|
|
use kvm::{Datamatch, IoeventAddress, Kvm, Vcpu, Vm};
|
2018-05-18 01:47:11 +00:00
|
|
|
use resources::SystemAllocator;
|
2018-10-03 17:22:32 +00:00
|
|
|
use sys_util::{syslog, EventFd, GuestMemory};
|
2018-02-14 06:09:43 +00:00
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, Box<std::error::Error>>;
|
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
/// Holds the pieces needed to build a VM. Passed to `build_vm` in the `LinuxArch` trait below to
|
|
|
|
/// create a `RunnableLinuxVm`.
|
|
|
|
pub struct VmComponents {
|
2018-10-03 17:22:32 +00:00
|
|
|
pub pci_devices: Vec<(Box<PciDevice + 'static>, Minijail)>,
|
2018-07-24 00:58:09 +00:00
|
|
|
pub memory_mb: u64,
|
|
|
|
pub vcpu_count: u32,
|
|
|
|
pub kernel_image: File,
|
|
|
|
pub extra_kernel_params: Vec<String>,
|
|
|
|
pub wayland_dmabuf: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Holds the elements needed to run a Linux VM. Created by `build_vm`.
|
|
|
|
pub struct RunnableLinuxVm {
|
|
|
|
pub vm: Vm,
|
|
|
|
pub kvm: Kvm,
|
|
|
|
pub resources: SystemAllocator,
|
|
|
|
pub stdio_serial: Arc<Mutex<Serial>>,
|
|
|
|
pub exit_evt: EventFd,
|
|
|
|
pub vcpus: Vec<Vcpu>,
|
|
|
|
pub irq_chip: Option<File>,
|
|
|
|
pub io_bus: Bus,
|
|
|
|
pub mmio_bus: Bus,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The device and optional jail.
|
|
|
|
pub struct VirtioDeviceStub {
|
|
|
|
pub dev: Box<VirtioDevice>,
|
|
|
|
pub jail: Option<Minijail>,
|
|
|
|
}
|
|
|
|
|
2018-02-14 06:09:43 +00:00
|
|
|
/// Trait which is implemented for each Linux Architecture in order to
|
|
|
|
/// set up the memory, cpus, and system devices and to boot the kernel.
|
|
|
|
pub trait LinuxArch {
|
2018-07-24 00:58:09 +00:00
|
|
|
/// Takes `VmComponents` and generates a `RunnableLinuxVm`.
|
2018-02-14 06:09:43 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
2018-07-24 00:58:09 +00:00
|
|
|
/// * `components` - Parts to use to build the VM.
|
|
|
|
/// * `virtio_devs` - Function to generate a list of virtio devices.
|
|
|
|
fn build_vm<F>(components: VmComponents, virtio_devs: F) -> Result<RunnableLinuxVm>
|
2018-10-03 17:22:32 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&GuestMemory, &EventFd) -> Result<Vec<VirtioDeviceStub>>;
|
2018-07-24 00:58:09 +00:00
|
|
|
}
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
/// Errors for device manager.
|
|
|
|
#[derive(Debug)]
|
2018-07-09 22:39:34 +00:00
|
|
|
pub enum DeviceRegistrationError {
|
|
|
|
/// Could not allocate IO space for the device.
|
|
|
|
AllocateIoAddrs(PciDeviceError),
|
|
|
|
/// Could not allocate an IRQ number.
|
|
|
|
AllocateIrq,
|
2018-07-24 00:58:09 +00:00
|
|
|
/// Could not create the mmio device to wrap a VirtioDevice.
|
|
|
|
CreateMmioDevice(sys_util::Error),
|
2018-07-09 22:39:34 +00:00
|
|
|
/// Could not create an event fd.
|
|
|
|
EventFdCreate(sys_util::Error),
|
|
|
|
/// Could not add a device to the mmio bus.
|
|
|
|
MmioInsert(BusError),
|
2018-07-24 00:58:09 +00:00
|
|
|
/// Failed to register ioevent with VM.
|
|
|
|
RegisterIoevent(sys_util::Error),
|
|
|
|
/// Failed to register irq eventfd with VM.
|
|
|
|
RegisterIrqfd(sys_util::Error),
|
|
|
|
/// Failed to initialize proxy device for jailed device.
|
|
|
|
ProxyDeviceCreation(devices::ProxyError),
|
|
|
|
/// Appending to kernel command line failed.
|
|
|
|
Cmdline(kernel_cmdline::Error),
|
|
|
|
/// No more IRQs are available.
|
|
|
|
IrqsExhausted,
|
|
|
|
/// No more MMIO space available.
|
|
|
|
AddrsExhausted,
|
|
|
|
}
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-09 22:39:34 +00:00
|
|
|
impl fmt::Display for DeviceRegistrationError {
|
2018-07-24 00:58:09 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2018-07-09 22:39:34 +00:00
|
|
|
&DeviceRegistrationError::AllocateIoAddrs(ref e) => {
|
|
|
|
write!(f, "Allocating IO addresses: {:?}", e)
|
|
|
|
}
|
2018-10-03 17:22:32 +00:00
|
|
|
&DeviceRegistrationError::AllocateIrq => write!(f, "Allocating IRQ number"),
|
2018-07-09 22:39:34 +00:00
|
|
|
&DeviceRegistrationError::CreateMmioDevice(ref e) => {
|
|
|
|
write!(f, "failed to create mmio device: {:?}", e)
|
|
|
|
}
|
|
|
|
&DeviceRegistrationError::Cmdline(ref e) => {
|
2018-07-24 00:58:09 +00:00
|
|
|
write!(f, "unable to add device to kernel command line: {}", e)
|
|
|
|
}
|
2018-07-09 22:39:34 +00:00
|
|
|
&DeviceRegistrationError::EventFdCreate(ref e) => {
|
|
|
|
write!(f, "failed to create eventfd: {:?}", e)
|
|
|
|
}
|
|
|
|
&DeviceRegistrationError::MmioInsert(ref e) => {
|
|
|
|
write!(f, "failed to add to mmio bus: {:?}", e)
|
|
|
|
}
|
|
|
|
&DeviceRegistrationError::RegisterIoevent(ref e) => {
|
2018-07-24 00:58:09 +00:00
|
|
|
write!(f, "failed to register ioevent to VM: {:?}", e)
|
|
|
|
}
|
2018-07-09 22:39:34 +00:00
|
|
|
&DeviceRegistrationError::RegisterIrqfd(ref e) => {
|
2018-07-24 00:58:09 +00:00
|
|
|
write!(f, "failed to register irq eventfd to VM: {:?}", e)
|
|
|
|
}
|
2018-07-09 22:39:34 +00:00
|
|
|
&DeviceRegistrationError::ProxyDeviceCreation(ref e) => {
|
|
|
|
write!(f, "failed to create proxy device: {}", e)
|
|
|
|
}
|
|
|
|
&DeviceRegistrationError::IrqsExhausted => write!(f, "no more IRQs are available"),
|
2018-10-03 17:22:32 +00:00
|
|
|
&DeviceRegistrationError::AddrsExhausted => {
|
|
|
|
write!(f, "no more addresses are available")
|
|
|
|
}
|
2018-07-09 22:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a root PCI device for use by this Vm.
|
2018-10-03 17:22:32 +00:00
|
|
|
pub fn generate_pci_root(
|
|
|
|
devices: Vec<(Box<PciDevice + 'static>, Minijail)>,
|
|
|
|
mmio_bus: &mut Bus,
|
|
|
|
resources: &mut SystemAllocator,
|
|
|
|
vm: &mut Vm,
|
|
|
|
) -> std::result::Result<(PciRoot, Vec<(u32, PciInterruptPin)>), DeviceRegistrationError> {
|
2018-07-09 22:39:34 +00:00
|
|
|
let mut root = PciRoot::new();
|
|
|
|
let mut pci_irqs = Vec::new();
|
|
|
|
for (dev_idx, (mut device, jail)) in devices.into_iter().enumerate() {
|
2018-09-20 17:59:06 +00:00
|
|
|
let mut keep_fds = device.keep_fds();
|
2018-07-09 22:39:34 +00:00
|
|
|
syslog::push_fds(&mut keep_fds);
|
|
|
|
|
|
|
|
let irqfd = EventFd::new().map_err(DeviceRegistrationError::EventFdCreate)?;
|
2018-07-09 20:35:40 +00:00
|
|
|
let irq_num = resources
|
|
|
|
.allocate_irq()
|
|
|
|
.ok_or(DeviceRegistrationError::AllocateIrq)? as u32;
|
2018-07-09 22:39:34 +00:00
|
|
|
let pci_irq_pin = match dev_idx % 4 {
|
|
|
|
0 => PciInterruptPin::IntA,
|
|
|
|
1 => PciInterruptPin::IntB,
|
|
|
|
2 => PciInterruptPin::IntC,
|
|
|
|
3 => PciInterruptPin::IntD,
|
|
|
|
_ => panic!(""), // Obviously not possible, but the compiler is not smart enough.
|
|
|
|
};
|
2018-09-17 21:42:59 +00:00
|
|
|
vm.register_irqfd(&irqfd, irq_num)
|
|
|
|
.map_err(DeviceRegistrationError::RegisterIrqfd)?;
|
|
|
|
keep_fds.push(irqfd.as_raw_fd());
|
2018-07-09 22:39:34 +00:00
|
|
|
device.assign_irq(irqfd, irq_num, pci_irq_pin);
|
2018-09-17 21:42:59 +00:00
|
|
|
pci_irqs.push((dev_idx as u32, pci_irq_pin));
|
2018-07-09 22:39:34 +00:00
|
|
|
|
|
|
|
let ranges = device
|
|
|
|
.allocate_io_bars(resources)
|
|
|
|
.map_err(DeviceRegistrationError::AllocateIoAddrs)?;
|
2018-07-09 20:35:40 +00:00
|
|
|
for (event, addr) in device.ioeventfds() {
|
|
|
|
let io_addr = IoeventAddress::Mmio(addr);
|
2018-10-09 19:44:21 +00:00
|
|
|
vm.register_ioevent(&event, io_addr, Datamatch::AnyLength)
|
2018-07-09 20:35:40 +00:00
|
|
|
.map_err(DeviceRegistrationError::RegisterIoevent)?;
|
|
|
|
keep_fds.push(event.as_raw_fd());
|
|
|
|
}
|
2018-07-09 22:39:34 +00:00
|
|
|
let proxy = ProxyDevice::new(device, &jail, keep_fds)
|
|
|
|
.map_err(DeviceRegistrationError::ProxyDeviceCreation)?;
|
|
|
|
let arced_dev = Arc::new(Mutex::new(proxy));
|
|
|
|
root.add_device(arced_dev.clone());
|
|
|
|
for range in &ranges {
|
2018-10-03 17:22:32 +00:00
|
|
|
mmio_bus
|
|
|
|
.insert(arced_dev.clone(), range.0, range.1, true)
|
2018-07-09 22:39:34 +00:00
|
|
|
.map_err(DeviceRegistrationError::MmioInsert)?;
|
2018-07-24 00:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 22:39:34 +00:00
|
|
|
Ok((root, pci_irqs))
|
2018-07-24 00:58:09 +00:00
|
|
|
}
|
2018-05-18 01:47:11 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
/// Register a device to be used via MMIO transport.
|
2018-10-03 17:22:32 +00:00
|
|
|
pub fn register_mmio(
|
|
|
|
bus: &mut devices::Bus,
|
|
|
|
vm: &mut Vm,
|
|
|
|
device: Box<devices::virtio::VirtioDevice>,
|
|
|
|
jail: Option<Minijail>,
|
|
|
|
resources: &mut SystemAllocator,
|
|
|
|
cmdline: &mut kernel_cmdline::Cmdline,
|
|
|
|
) -> std::result::Result<(), DeviceRegistrationError> {
|
2018-07-24 00:58:09 +00:00
|
|
|
let irq = match resources.allocate_irq() {
|
2018-07-09 22:39:34 +00:00
|
|
|
None => return Err(DeviceRegistrationError::IrqsExhausted),
|
2018-07-24 00:58:09 +00:00
|
|
|
Some(i) => i,
|
|
|
|
};
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
// List of FDs to keep open in the child after it forks.
|
|
|
|
let mut keep_fds: Vec<RawFd> = device.keep_fds();
|
|
|
|
syslog::push_fds(&mut keep_fds);
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-09 22:39:34 +00:00
|
|
|
let mmio_device = devices::virtio::MmioDevice::new((*vm.get_memory()).clone(), device)
|
|
|
|
.map_err(DeviceRegistrationError::CreateMmioDevice)?;
|
2018-07-24 00:58:09 +00:00
|
|
|
let mmio_len = 0x1000; // TODO(dgreid) - configurable per arch?
|
2018-10-03 17:22:32 +00:00
|
|
|
let mmio_base = resources
|
|
|
|
.allocate_mmio_addresses(mmio_len)
|
2018-07-09 22:39:34 +00:00
|
|
|
.ok_or(DeviceRegistrationError::AddrsExhausted)?;
|
2018-07-24 00:58:09 +00:00
|
|
|
for (i, queue_evt) in mmio_device.queue_evts().iter().enumerate() {
|
2018-10-03 17:22:32 +00:00
|
|
|
let io_addr = IoeventAddress::Mmio(mmio_base + devices::virtio::NOTIFY_REG_OFFSET as u64);
|
2018-10-09 19:44:21 +00:00
|
|
|
vm.register_ioevent(&queue_evt, io_addr, Datamatch::U32(Some(i as u32)))
|
2018-10-03 17:22:32 +00:00
|
|
|
.map_err(DeviceRegistrationError::RegisterIoevent)?;
|
2018-07-24 00:58:09 +00:00
|
|
|
keep_fds.push(queue_evt.as_raw_fd());
|
|
|
|
}
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
if let Some(interrupt_evt) = mmio_device.interrupt_evt() {
|
|
|
|
vm.register_irqfd(&interrupt_evt, irq)
|
2018-10-03 17:22:32 +00:00
|
|
|
.map_err(DeviceRegistrationError::RegisterIrqfd)?;
|
2018-07-24 00:58:09 +00:00
|
|
|
keep_fds.push(interrupt_evt.as_raw_fd());
|
|
|
|
}
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
if let Some(jail) = jail {
|
|
|
|
let proxy_dev = devices::ProxyDevice::new(mmio_device, &jail, keep_fds)
|
2018-07-09 22:39:34 +00:00
|
|
|
.map_err(DeviceRegistrationError::ProxyDeviceCreation)?;
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-10-03 17:22:32 +00:00
|
|
|
bus.insert(Arc::new(Mutex::new(proxy_dev)), mmio_base, mmio_len, false)
|
|
|
|
.unwrap();
|
2018-07-24 00:58:09 +00:00
|
|
|
} else {
|
2018-10-03 17:22:32 +00:00
|
|
|
bus.insert(
|
|
|
|
Arc::new(Mutex::new(mmio_device)),
|
|
|
|
mmio_base,
|
|
|
|
mmio_len,
|
|
|
|
false,
|
|
|
|
).unwrap();
|
2018-07-24 00:58:09 +00:00
|
|
|
}
|
2018-02-14 06:09:43 +00:00
|
|
|
|
2018-07-24 00:58:09 +00:00
|
|
|
cmdline
|
2018-10-03 17:22:32 +00:00
|
|
|
.insert(
|
|
|
|
"virtio_mmio.device",
|
|
|
|
&format!("4K@0x{:08x}:{}", mmio_base, irq),
|
|
|
|
).map_err(DeviceRegistrationError::Cmdline)?;
|
2018-07-24 00:58:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-02-14 06:09:43 +00:00
|
|
|
}
|