2019-04-18 17:24:06 +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
|
2019-05-08 10:22:53 +00:00
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
|
|
//
|
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
2019-04-18 17:24:06 +00:00
|
|
|
|
|
|
|
use super::*;
|
2019-08-20 22:43:23 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2019-08-05 23:51:31 +00:00
|
|
|
use vm_memory::{GuestAddress, GuestMemoryMmap, GuestUsize};
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-04-18 17:24:06 +00:00
|
|
|
|
2019-07-26 18:48:07 +00:00
|
|
|
pub enum VirtioInterruptType {
|
|
|
|
Config,
|
|
|
|
Queue,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type VirtioInterrupt = Box<
|
2019-08-15 15:41:40 +00:00
|
|
|
dyn Fn(&VirtioInterruptType, Option<&Queue>) -> std::result::Result<(), std::io::Error>
|
2019-07-26 18:48:07 +00:00
|
|
|
+ Send
|
|
|
|
+ Sync,
|
|
|
|
>;
|
2019-06-03 20:57:26 +00:00
|
|
|
|
2019-10-02 07:10:42 +00:00
|
|
|
pub type VirtioIommuRemapping =
|
|
|
|
Box<dyn Fn(u64) -> std::result::Result<u64, std::io::Error> + Send + Sync>;
|
|
|
|
|
2019-08-05 23:51:31 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct VirtioSharedMemory {
|
|
|
|
pub offset: u64,
|
|
|
|
pub len: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct VirtioSharedMemoryList {
|
|
|
|
pub addr: GuestAddress,
|
|
|
|
pub len: GuestUsize,
|
|
|
|
pub region_list: Vec<VirtioSharedMemory>,
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:24:06 +00:00
|
|
|
/// Trait for virtio devices to be driven by a virtio transport.
|
|
|
|
///
|
|
|
|
/// The lifecycle of a virtio device is to be moved to a virtio transport, which will then query the
|
|
|
|
/// device. Once the guest driver has configured the device, `VirtioDevice::activate` will be called
|
|
|
|
/// and all the events, memory, and queues for device operation will be moved into the device.
|
|
|
|
/// Optionally, a virtio device can implement device reset in which it returns said resources and
|
|
|
|
/// resets its internal.
|
|
|
|
pub trait VirtioDevice: Send {
|
|
|
|
/// The virtio device type.
|
|
|
|
fn device_type(&self) -> u32;
|
|
|
|
|
|
|
|
/// The maximum size of each queue that this device supports.
|
|
|
|
fn queue_max_sizes(&self) -> &[u16];
|
|
|
|
|
|
|
|
/// The set of feature bits shifted by `page * 32`.
|
|
|
|
fn features(&self, page: u32) -> u32 {
|
|
|
|
let _ = page;
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acknowledges that this set of features should be enabled.
|
|
|
|
fn ack_features(&mut self, page: u32, value: u32);
|
|
|
|
|
|
|
|
/// Reads this device configuration space at `offset`.
|
|
|
|
fn read_config(&self, offset: u64, data: &mut [u8]);
|
|
|
|
|
|
|
|
/// Writes to this device configuration space at `offset`.
|
|
|
|
fn write_config(&mut self, offset: u64, data: &[u8]);
|
|
|
|
|
|
|
|
/// Activates this device for real usage.
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2019-08-20 22:43:23 +00:00
|
|
|
mem: Arc<RwLock<GuestMemoryMmap>>,
|
2019-06-03 20:57:26 +00:00
|
|
|
interrupt_evt: Arc<VirtioInterrupt>,
|
2019-04-18 17:24:06 +00:00
|
|
|
queues: Vec<Queue>,
|
|
|
|
queue_evts: Vec<EventFd>,
|
|
|
|
) -> ActivateResult;
|
|
|
|
|
|
|
|
/// Optionally deactivates this device and returns ownership of the guest memory map, interrupt
|
|
|
|
/// event, and queue events.
|
2019-06-03 20:57:26 +00:00
|
|
|
fn reset(&mut self) -> Option<(Arc<VirtioInterrupt>, Vec<EventFd>)> {
|
2019-04-18 17:24:06 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-08-05 23:51:31 +00:00
|
|
|
/// Returns the list of shared memory regions required by the device.
|
|
|
|
fn get_shm_regions(&self) -> Option<VirtioSharedMemoryList> {
|
|
|
|
None
|
|
|
|
}
|
2019-10-02 07:10:42 +00:00
|
|
|
|
|
|
|
fn iommu_translate(&self, addr: u64) -> u64 {
|
|
|
|
addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait providing address translation the same way a physical DMA remapping
|
|
|
|
/// table would provide translation between an IOVA and a physical address.
|
|
|
|
/// The goal of this trait is to be used by virtio devices to perform the
|
|
|
|
/// address translation before they try to read from the guest physical address.
|
|
|
|
/// On the other side, the implementation itself should be provided by the code
|
|
|
|
/// emulating the IOMMU for the guest.
|
|
|
|
pub trait DmaRemapping: Send + Sync {
|
|
|
|
fn translate(&self, id: u32, addr: u64) -> std::result::Result<u64, std::io::Error>;
|
2019-04-18 17:24:06 +00:00
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! virtio_pausable_inner {
|
|
|
|
() => {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
debug!(
|
|
|
|
"Pausing virtio-{}",
|
|
|
|
VirtioDeviceType::from(self.device_type())
|
|
|
|
);
|
|
|
|
self.paused.store(true, Ordering::SeqCst);
|
|
|
|
if let Some(pause_evt) = &self.pause_evt {
|
|
|
|
pause_evt
|
|
|
|
.write(1)
|
|
|
|
.map_err(|e| MigratableError::Pause(e.into()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
debug!(
|
|
|
|
"Resuming virtio-{}",
|
|
|
|
VirtioDeviceType::from(self.device_type())
|
|
|
|
);
|
|
|
|
self.paused.store(false, Ordering::SeqCst);
|
|
|
|
if let Some(epoll_thread) = &self.epoll_thread {
|
|
|
|
epoll_thread.thread().unpark();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! virtio_pausable {
|
|
|
|
($name:ident) => {
|
|
|
|
impl Pausable for $name {
|
|
|
|
virtio_pausable_inner!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|