2019-04-18 17:32:41 +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:32:41 +00:00
|
|
|
|
2020-01-09 18:11:18 +00:00
|
|
|
use super::VirtioPciCommonConfig;
|
|
|
|
use crate::transport::VirtioTransport;
|
2021-06-02 19:08:04 +00:00
|
|
|
use crate::GuestMemoryMmap;
|
2020-01-09 18:11:18 +00:00
|
|
|
use crate::{
|
2021-01-18 17:52:45 +00:00
|
|
|
ActivateResult, Queue, VirtioDevice, VirtioDeviceType, VirtioInterrupt, VirtioInterruptType,
|
2020-07-02 12:25:19 +00:00
|
|
|
DEVICE_ACKNOWLEDGE, DEVICE_DRIVER, DEVICE_DRIVER_OK, DEVICE_FAILED, DEVICE_FEATURES_OK,
|
2021-03-16 18:19:28 +00:00
|
|
|
DEVICE_INIT,
|
2020-01-09 18:11:18 +00:00
|
|
|
};
|
2020-05-05 15:53:21 +00:00
|
|
|
use anyhow::anyhow;
|
2019-12-31 10:49:11 +00:00
|
|
|
use libc::EFD_NONBLOCK;
|
2019-04-18 17:32:41 +00:00
|
|
|
use pci::{
|
2020-01-20 15:33:14 +00:00
|
|
|
BarReprogrammingParams, MsixCap, MsixConfig, PciBarConfiguration, PciBarRegionType,
|
2021-03-25 17:01:21 +00:00
|
|
|
PciCapability, PciCapabilityId, PciClassCode, PciConfiguration, PciDevice, PciDeviceError,
|
2020-01-20 15:33:14 +00:00
|
|
|
PciHeaderType, PciMassStorageSubclass, PciNetworkControllerSubclass, PciSubclass,
|
2019-04-18 17:32:41 +00:00
|
|
|
};
|
2019-12-31 10:49:11 +00:00
|
|
|
use std::any::Any;
|
2020-01-29 14:56:41 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::io::Write;
|
2020-05-05 15:53:21 +00:00
|
|
|
use std::num::Wrapping;
|
2019-12-31 10:49:11 +00:00
|
|
|
use std::result;
|
2020-12-16 18:00:48 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU16, AtomicUsize, Ordering};
|
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
2021-06-01 09:40:40 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2019-04-18 17:32:41 +00:00
|
|
|
use vm_allocator::SystemAllocator;
|
2020-02-04 11:04:10 +00:00
|
|
|
use vm_device::interrupt::{
|
|
|
|
InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig,
|
|
|
|
};
|
2020-09-09 14:30:31 +00:00
|
|
|
use vm_device::BusDevice;
|
2020-02-11 16:22:40 +00:00
|
|
|
use vm_memory::{
|
2021-06-02 19:08:04 +00:00
|
|
|
Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestUsize, Le32,
|
2020-02-11 16:22:40 +00:00
|
|
|
};
|
2021-06-01 09:40:40 +00:00
|
|
|
use vm_migration::{
|
|
|
|
Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable, VersionMapped,
|
|
|
|
};
|
2021-03-16 18:19:28 +00:00
|
|
|
use vm_virtio::{queue, VirtioIommuRemapping, VIRTIO_MSI_NO_VECTOR};
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::{errno::Result, eventfd::EventFd};
|
2019-04-18 17:32:41 +00:00
|
|
|
|
2020-05-05 15:53:21 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum Error {
|
|
|
|
/// Failed to retrieve queue ring's index.
|
2020-07-02 12:25:19 +00:00
|
|
|
QueueRingIndex(queue::Error),
|
2020-05-05 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
#[allow(clippy::enum_variant_names)]
|
|
|
|
enum PciCapabilityType {
|
|
|
|
CommonConfig = 1,
|
|
|
|
NotifyConfig = 2,
|
|
|
|
IsrConfig = 3,
|
|
|
|
DeviceConfig = 4,
|
|
|
|
PciConfig = 5,
|
2019-08-05 23:26:21 +00:00
|
|
|
SharedMemoryConfig = 8,
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 14:56:41 +00:00
|
|
|
// This offset represents the 2 bytes omitted from the VirtioPciCap structure
|
|
|
|
// as they are already handled through add_capability(). These 2 bytes are the
|
|
|
|
// fields cap_vndr (1 byte) and cap_next (1 byte) defined in the virtio spec.
|
|
|
|
const VIRTIO_PCI_CAP_OFFSET: usize = 2;
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(packed)]
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
|
struct VirtioPciCap {
|
|
|
|
cap_len: u8, // Generic PCI field: capability length
|
|
|
|
cfg_type: u8, // Identifies the structure.
|
|
|
|
pci_bar: u8, // Where to find it.
|
2019-08-05 23:26:21 +00:00
|
|
|
id: u8, // Multiple capabilities of the same type
|
|
|
|
padding: [u8; 2], // Pad to full dword.
|
2019-04-18 17:32:41 +00:00
|
|
|
offset: Le32, // Offset within bar.
|
|
|
|
length: Le32, // Length of the structure, in bytes.
|
|
|
|
}
|
|
|
|
// It is safe to implement ByteValued. All members are simple numbers and any value is valid.
|
|
|
|
unsafe impl ByteValued for VirtioPciCap {}
|
|
|
|
|
|
|
|
impl PciCapability for VirtioPciCap {
|
|
|
|
fn bytes(&self) -> &[u8] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
fn id(&self) -> PciCapabilityId {
|
|
|
|
PciCapabilityId::VendorSpecific
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:23:56 +00:00
|
|
|
const VIRTIO_PCI_CAP_LEN_OFFSET: u8 = 2;
|
2019-04-18 17:32:41 +00:00
|
|
|
|
|
|
|
impl VirtioPciCap {
|
|
|
|
pub fn new(cfg_type: PciCapabilityType, pci_bar: u8, offset: u32, length: u32) -> Self {
|
|
|
|
VirtioPciCap {
|
2019-08-06 21:23:56 +00:00
|
|
|
cap_len: (std::mem::size_of::<VirtioPciCap>() as u8) + VIRTIO_PCI_CAP_LEN_OFFSET,
|
2019-04-18 17:32:41 +00:00
|
|
|
cfg_type: cfg_type as u8,
|
|
|
|
pci_bar,
|
2019-08-05 23:26:21 +00:00
|
|
|
id: 0,
|
|
|
|
padding: [0; 2],
|
2019-04-18 17:32:41 +00:00
|
|
|
offset: Le32::from(offset),
|
|
|
|
length: Le32::from(length),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(packed)]
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
|
struct VirtioPciNotifyCap {
|
|
|
|
cap: VirtioPciCap,
|
|
|
|
notify_off_multiplier: Le32,
|
|
|
|
}
|
|
|
|
// It is safe to implement ByteValued. All members are simple numbers and any value is valid.
|
|
|
|
unsafe impl ByteValued for VirtioPciNotifyCap {}
|
|
|
|
|
|
|
|
impl PciCapability for VirtioPciNotifyCap {
|
|
|
|
fn bytes(&self) -> &[u8] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
fn id(&self) -> PciCapabilityId {
|
|
|
|
PciCapabilityId::VendorSpecific
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioPciNotifyCap {
|
|
|
|
pub fn new(
|
|
|
|
cfg_type: PciCapabilityType,
|
|
|
|
pci_bar: u8,
|
|
|
|
offset: u32,
|
|
|
|
length: u32,
|
|
|
|
multiplier: Le32,
|
|
|
|
) -> Self {
|
|
|
|
VirtioPciNotifyCap {
|
|
|
|
cap: VirtioPciCap {
|
2019-08-06 21:23:56 +00:00
|
|
|
cap_len: (std::mem::size_of::<VirtioPciNotifyCap>() as u8)
|
|
|
|
+ VIRTIO_PCI_CAP_LEN_OFFSET,
|
2019-04-18 17:32:41 +00:00
|
|
|
cfg_type: cfg_type as u8,
|
|
|
|
pci_bar,
|
2019-08-05 23:26:21 +00:00
|
|
|
id: 0,
|
|
|
|
padding: [0; 2],
|
2019-04-18 17:32:41 +00:00
|
|
|
offset: Le32::from(offset),
|
|
|
|
length: Le32::from(length),
|
|
|
|
},
|
|
|
|
notify_off_multiplier: multiplier,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 23:26:21 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(packed)]
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
|
struct VirtioPciCap64 {
|
|
|
|
cap: VirtioPciCap,
|
|
|
|
offset_hi: Le32,
|
|
|
|
length_hi: Le32,
|
|
|
|
}
|
|
|
|
// It is safe to implement ByteValued. All members are simple numbers and any value is valid.
|
|
|
|
unsafe impl ByteValued for VirtioPciCap64 {}
|
|
|
|
|
|
|
|
impl PciCapability for VirtioPciCap64 {
|
|
|
|
fn bytes(&self) -> &[u8] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
fn id(&self) -> PciCapabilityId {
|
|
|
|
PciCapabilityId::VendorSpecific
|
2019-08-05 23:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioPciCap64 {
|
|
|
|
pub fn new(cfg_type: PciCapabilityType, pci_bar: u8, id: u8, offset: u64, length: u64) -> Self {
|
|
|
|
VirtioPciCap64 {
|
|
|
|
cap: VirtioPciCap {
|
|
|
|
cap_len: (std::mem::size_of::<VirtioPciCap64>() as u8) + VIRTIO_PCI_CAP_LEN_OFFSET,
|
|
|
|
cfg_type: cfg_type as u8,
|
|
|
|
pci_bar,
|
|
|
|
id,
|
|
|
|
padding: [0; 2],
|
|
|
|
offset: Le32::from(offset as u32),
|
|
|
|
length: Le32::from(length as u32),
|
|
|
|
},
|
|
|
|
offset_hi: Le32::from((offset >> 32) as u32),
|
|
|
|
length_hi: Le32::from((length >> 32) as u32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 14:56:41 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(packed)]
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
|
struct VirtioPciCfgCap {
|
|
|
|
cap: VirtioPciCap,
|
|
|
|
pci_cfg_data: [u8; 4],
|
|
|
|
}
|
|
|
|
// It is safe to implement ByteValued. All members are simple numbers and any value is valid.
|
|
|
|
unsafe impl ByteValued for VirtioPciCfgCap {}
|
|
|
|
|
|
|
|
impl PciCapability for VirtioPciCfgCap {
|
|
|
|
fn bytes(&self) -> &[u8] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:01:21 +00:00
|
|
|
fn id(&self) -> PciCapabilityId {
|
|
|
|
PciCapabilityId::VendorSpecific
|
2020-01-29 14:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioPciCfgCap {
|
|
|
|
fn new() -> Self {
|
|
|
|
VirtioPciCfgCap {
|
|
|
|
cap: VirtioPciCap::new(PciCapabilityType::PciConfig, 0, 0, 0),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Default)]
|
|
|
|
struct VirtioPciCfgCapInfo {
|
|
|
|
offset: usize,
|
|
|
|
cap: VirtioPciCfgCap,
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub enum PciVirtioSubclass {
|
|
|
|
NonTransitionalBase = 0xff,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PciSubclass for PciVirtioSubclass {
|
|
|
|
fn get_register_value(&self) -> u8 {
|
|
|
|
*self as u8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate one bar for the structs pointed to by the capability structures.
|
2019-06-06 17:15:10 +00:00
|
|
|
// As per the PCI specification, because the same BAR shares MSI-X and non
|
|
|
|
// MSI-X structures, it is recommended to use 8KiB alignment for all those
|
|
|
|
// structures.
|
2019-04-18 17:32:41 +00:00
|
|
|
const COMMON_CONFIG_BAR_OFFSET: u64 = 0x0000;
|
|
|
|
const COMMON_CONFIG_SIZE: u64 = 56;
|
2019-06-06 17:15:10 +00:00
|
|
|
const ISR_CONFIG_BAR_OFFSET: u64 = 0x2000;
|
2019-04-18 17:32:41 +00:00
|
|
|
const ISR_CONFIG_SIZE: u64 = 1;
|
2019-06-06 17:15:10 +00:00
|
|
|
const DEVICE_CONFIG_BAR_OFFSET: u64 = 0x4000;
|
2019-04-18 17:32:41 +00:00
|
|
|
const DEVICE_CONFIG_SIZE: u64 = 0x1000;
|
2019-06-06 17:15:10 +00:00
|
|
|
const NOTIFICATION_BAR_OFFSET: u64 = 0x6000;
|
2019-04-18 17:32:41 +00:00
|
|
|
const NOTIFICATION_SIZE: u64 = 0x1000;
|
2019-06-06 17:15:10 +00:00
|
|
|
const MSIX_TABLE_BAR_OFFSET: u64 = 0x8000;
|
|
|
|
// The size is 256KiB because the table can hold up to 2048 entries, with each
|
|
|
|
// entry being 128 bits (4 DWORDS).
|
|
|
|
const MSIX_TABLE_SIZE: u64 = 0x40000;
|
|
|
|
const MSIX_PBA_BAR_OFFSET: u64 = 0x48000;
|
|
|
|
// The size is 2KiB because the Pending Bit Array has one bit per vector and it
|
|
|
|
// can support up to 2048 vectors.
|
|
|
|
const MSIX_PBA_SIZE: u64 = 0x800;
|
|
|
|
// The BAR size must be a power of 2.
|
|
|
|
const CAPABILITY_BAR_SIZE: u64 = 0x80000;
|
2019-04-18 17:32:41 +00:00
|
|
|
|
|
|
|
const NOTIFY_OFF_MULTIPLIER: u32 = 4; // A dword per notification address.
|
|
|
|
|
|
|
|
const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4;
|
|
|
|
const VIRTIO_PCI_DEVICE_ID_BASE: u16 = 0x1040; // Add to device type to get device ID.
|
|
|
|
|
2021-06-01 09:40:40 +00:00
|
|
|
#[derive(Versionize)]
|
2021-06-01 09:30:03 +00:00
|
|
|
struct QueueState {
|
|
|
|
max_size: u16,
|
|
|
|
size: u16,
|
|
|
|
ready: bool,
|
|
|
|
vector: u16,
|
|
|
|
desc_table: u64,
|
|
|
|
avail_ring: u64,
|
|
|
|
used_ring: u64,
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:40:40 +00:00
|
|
|
#[derive(Versionize)]
|
2020-05-05 15:53:21 +00:00
|
|
|
struct VirtioPciDeviceState {
|
|
|
|
device_activated: bool,
|
2021-06-01 09:30:03 +00:00
|
|
|
queues: Vec<QueueState>,
|
2020-05-05 15:53:21 +00:00
|
|
|
interrupt_status: usize,
|
|
|
|
}
|
|
|
|
|
2021-06-01 09:40:40 +00:00
|
|
|
impl VersionMapped for VirtioPciDeviceState {}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
pub struct VirtioPciDevice {
|
2020-04-27 14:19:40 +00:00
|
|
|
id: String,
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
// PCI configuration registers.
|
|
|
|
configuration: PciConfiguration,
|
|
|
|
|
|
|
|
// virtio PCI common configuration
|
|
|
|
common_config: VirtioPciCommonConfig,
|
|
|
|
|
2019-05-29 23:33:29 +00:00
|
|
|
// MSI-X config
|
2019-06-07 16:19:46 +00:00
|
|
|
msix_config: Option<Arc<Mutex<MsixConfig>>>,
|
2019-05-29 23:33:29 +00:00
|
|
|
|
|
|
|
// Number of MSI-X vectors
|
|
|
|
msix_num: u16,
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
// Virtio device reference and status
|
2019-11-18 23:10:42 +00:00
|
|
|
device: Arc<Mutex<dyn VirtioDevice>>,
|
2020-11-09 13:29:05 +00:00
|
|
|
device_activated: Arc<AtomicBool>,
|
2019-04-18 17:32:41 +00:00
|
|
|
|
|
|
|
// PCI interrupts.
|
|
|
|
interrupt_status: Arc<AtomicUsize>,
|
2020-01-13 17:52:19 +00:00
|
|
|
virtio_interrupt: Option<Arc<dyn VirtioInterrupt>>,
|
2021-07-29 09:15:10 +00:00
|
|
|
interrupt_source_group: Arc<dyn InterruptSourceGroup>,
|
2019-04-18 17:32:41 +00:00
|
|
|
|
|
|
|
// virtio queues
|
|
|
|
queues: Vec<Queue>,
|
|
|
|
queue_evts: Vec<EventFd>,
|
|
|
|
|
|
|
|
// Guest memory
|
2020-02-11 16:22:40 +00:00
|
|
|
memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
2019-04-18 17:32:41 +00:00
|
|
|
|
2020-05-11 17:11:27 +00:00
|
|
|
// Settings PCI BAR
|
2019-04-18 17:32:41 +00:00
|
|
|
settings_bar: u8,
|
2020-05-11 17:11:27 +00:00
|
|
|
settings_bar_addr: Option<GuestAddress>,
|
2019-09-17 16:34:51 +00:00
|
|
|
|
|
|
|
// Whether to use 64-bit bar location or 32-bit
|
|
|
|
use_64bit_bar: bool,
|
2020-01-29 14:56:41 +00:00
|
|
|
|
|
|
|
// Add a dedicated structure to hold information about the very specific
|
|
|
|
// virtio-pci capability VIRTIO_PCI_CAP_PCI_CFG. This is needed to support
|
|
|
|
// the legacy/backward compatible mechanism of letting the guest access the
|
|
|
|
// other virtio capabilities without mapping the PCI BARs. This can be
|
|
|
|
// needed when the guest tries to early access the virtio configuration of
|
|
|
|
// a device.
|
|
|
|
cap_pci_cfg_info: VirtioPciCfgCapInfo,
|
2020-04-08 15:27:08 +00:00
|
|
|
|
|
|
|
// Details of bar regions to free
|
|
|
|
bar_regions: Vec<(GuestAddress, GuestUsize, PciBarRegionType)>,
|
2020-11-09 13:29:05 +00:00
|
|
|
|
|
|
|
// EventFd to signal on to request activation
|
|
|
|
activate_evt: EventFd,
|
|
|
|
|
|
|
|
// Barrier that is used to wait on for activation
|
|
|
|
activate_barrier: Arc<Barrier>,
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioPciDevice {
|
|
|
|
/// Constructs a new PCI transport for the given virtio device.
|
2020-11-09 13:29:05 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2019-08-15 15:41:40 +00:00
|
|
|
pub fn new(
|
2020-04-27 14:19:40 +00:00
|
|
|
id: String,
|
2020-02-11 16:22:40 +00:00
|
|
|
memory: GuestMemoryAtomic<GuestMemoryMmap>,
|
2019-11-18 23:10:42 +00:00
|
|
|
device: Arc<Mutex<dyn VirtioDevice>>,
|
2019-08-15 15:41:40 +00:00
|
|
|
msix_num: u16,
|
2019-10-02 07:10:42 +00:00
|
|
|
iommu_mapping_cb: Option<Arc<VirtioIommuRemapping>>,
|
2020-02-04 11:04:10 +00:00
|
|
|
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = MsiIrqGroupConfig>>,
|
2020-06-17 13:58:15 +00:00
|
|
|
pci_device_bdf: u32,
|
2020-11-09 13:29:05 +00:00
|
|
|
activate_evt: EventFd,
|
2019-08-15 15:41:40 +00:00
|
|
|
) -> Result<Self> {
|
2019-11-18 23:10:42 +00:00
|
|
|
let device_clone = device.clone();
|
|
|
|
let locked_device = device_clone.lock().unwrap();
|
2019-04-18 17:32:41 +00:00
|
|
|
let mut queue_evts = Vec::new();
|
2019-11-18 23:10:42 +00:00
|
|
|
for _ in locked_device.queue_max_sizes().iter() {
|
2019-04-18 17:32:41 +00:00
|
|
|
queue_evts.push(EventFd::new(EFD_NONBLOCK)?)
|
|
|
|
}
|
2019-11-18 23:10:42 +00:00
|
|
|
let queues = locked_device
|
2019-04-18 17:32:41 +00:00
|
|
|
.queue_max_sizes()
|
|
|
|
.iter()
|
2019-10-02 07:10:42 +00:00
|
|
|
.map(|&s| {
|
|
|
|
let mut queue = Queue::new(s);
|
|
|
|
queue.iommu_mapping_cb = iommu_mapping_cb.clone();
|
|
|
|
queue
|
|
|
|
})
|
2019-04-18 17:32:41 +00:00
|
|
|
.collect();
|
|
|
|
|
2019-11-18 23:10:42 +00:00
|
|
|
let pci_device_id = VIRTIO_PCI_DEVICE_ID_BASE + locked_device.device_type() as u16;
|
2019-04-18 17:32:41 +00:00
|
|
|
|
2020-02-04 11:04:10 +00:00
|
|
|
let interrupt_source_group = interrupt_manager.create_group(MsiIrqGroupConfig {
|
|
|
|
base: 0,
|
|
|
|
count: msix_num as InterruptIndex,
|
|
|
|
})?;
|
2020-01-14 22:47:41 +00:00
|
|
|
|
2019-06-07 16:19:46 +00:00
|
|
|
let (msix_config, msix_config_clone) = if msix_num > 0 {
|
2020-01-09 17:51:10 +00:00
|
|
|
let msix_config = Arc::new(Mutex::new(MsixConfig::new(
|
|
|
|
msix_num,
|
2020-01-14 22:47:41 +00:00
|
|
|
interrupt_source_group.clone(),
|
2020-06-17 13:58:15 +00:00
|
|
|
pci_device_bdf,
|
2020-01-09 17:51:10 +00:00
|
|
|
)));
|
2019-06-07 16:19:46 +00:00
|
|
|
let msix_config_clone = msix_config.clone();
|
|
|
|
(Some(msix_config), Some(msix_config_clone))
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
2019-06-07 01:46:11 +00:00
|
|
|
|
2019-09-17 16:34:51 +00:00
|
|
|
// All device types *except* virtio block devices should be allocated a 64-bit bar
|
|
|
|
// The block devices should be given a 32-bit BAR so that they are easily accessible
|
|
|
|
// to firmware without requiring excessive identity mapping.
|
|
|
|
let mut use_64bit_bar = true;
|
2019-11-18 23:10:42 +00:00
|
|
|
let (class, subclass) = match VirtioDeviceType::from(locked_device.device_type()) {
|
2021-03-25 16:54:09 +00:00
|
|
|
VirtioDeviceType::Net => (
|
2019-07-02 15:16:11 +00:00
|
|
|
PciClassCode::NetworkController,
|
2019-08-15 15:41:40 +00:00
|
|
|
&PciNetworkControllerSubclass::EthernetController as &dyn PciSubclass,
|
2019-07-02 15:16:11 +00:00
|
|
|
),
|
2021-03-25 16:54:09 +00:00
|
|
|
VirtioDeviceType::Block => {
|
2019-09-17 16:34:51 +00:00
|
|
|
use_64bit_bar = false;
|
|
|
|
(
|
|
|
|
PciClassCode::MassStorage,
|
|
|
|
&PciMassStorageSubclass::MassStorage as &dyn PciSubclass,
|
|
|
|
)
|
|
|
|
}
|
2019-07-02 15:16:11 +00:00
|
|
|
_ => (
|
|
|
|
PciClassCode::Other,
|
2019-08-15 15:41:40 +00:00
|
|
|
&PciVirtioSubclass::NonTransitionalBase as &dyn PciSubclass,
|
2019-07-02 15:16:11 +00:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
let configuration = PciConfiguration::new(
|
|
|
|
VIRTIO_PCI_VENDOR_ID,
|
|
|
|
pci_device_id,
|
2020-04-17 10:42:51 +00:00
|
|
|
0x1, // For modern virtio-PCI devices
|
2019-07-02 15:16:11 +00:00
|
|
|
class,
|
|
|
|
subclass,
|
2019-04-18 17:32:41 +00:00
|
|
|
None,
|
|
|
|
PciHeaderType::Device,
|
|
|
|
VIRTIO_PCI_VENDOR_ID,
|
|
|
|
pci_device_id,
|
2019-06-07 16:19:46 +00:00
|
|
|
msix_config_clone,
|
2019-04-18 17:32:41 +00:00
|
|
|
);
|
|
|
|
|
2020-01-20 15:33:14 +00:00
|
|
|
let mut virtio_pci_device = VirtioPciDevice {
|
2020-04-27 14:19:40 +00:00
|
|
|
id,
|
2019-04-18 17:32:41 +00:00
|
|
|
configuration,
|
|
|
|
common_config: VirtioPciCommonConfig {
|
|
|
|
driver_status: 0,
|
|
|
|
config_generation: 0,
|
|
|
|
device_feature_select: 0,
|
|
|
|
driver_feature_select: 0,
|
|
|
|
queue_select: 0,
|
2021-03-16 18:19:28 +00:00
|
|
|
msix_config: Arc::new(AtomicU16::new(VIRTIO_MSI_NO_VECTOR)),
|
2019-04-18 17:32:41 +00:00
|
|
|
},
|
2019-06-07 01:46:11 +00:00
|
|
|
msix_config,
|
2019-05-29 23:33:29 +00:00
|
|
|
msix_num,
|
2019-04-18 17:32:41 +00:00
|
|
|
device,
|
2020-11-09 13:29:05 +00:00
|
|
|
device_activated: Arc::new(AtomicBool::new(false)),
|
2019-04-18 17:32:41 +00:00
|
|
|
interrupt_status: Arc::new(AtomicUsize::new(0)),
|
2020-01-13 17:52:19 +00:00
|
|
|
virtio_interrupt: None,
|
2019-04-18 17:32:41 +00:00
|
|
|
queues,
|
|
|
|
queue_evts,
|
|
|
|
memory: Some(memory),
|
|
|
|
settings_bar: 0,
|
2020-05-11 17:11:27 +00:00
|
|
|
settings_bar_addr: None,
|
2019-09-17 16:34:51 +00:00
|
|
|
use_64bit_bar,
|
2020-01-14 22:47:41 +00:00
|
|
|
interrupt_source_group,
|
2020-01-29 14:56:41 +00:00
|
|
|
cap_pci_cfg_info: VirtioPciCfgCapInfo::default(),
|
2020-04-08 15:27:08 +00:00
|
|
|
bar_regions: vec![],
|
2020-11-09 13:29:05 +00:00
|
|
|
activate_evt,
|
|
|
|
activate_barrier: Arc::new(Barrier::new(2)),
|
2020-01-20 15:33:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(msix_config) = &virtio_pci_device.msix_config {
|
|
|
|
virtio_pci_device.virtio_interrupt = Some(Arc::new(VirtioInterruptMsix::new(
|
|
|
|
msix_config.clone(),
|
|
|
|
virtio_pci_device.common_config.msix_config.clone(),
|
|
|
|
virtio_pci_device.interrupt_source_group.clone(),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(virtio_pci_device)
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 15:53:21 +00:00
|
|
|
fn state(&self) -> VirtioPciDeviceState {
|
|
|
|
VirtioPciDeviceState {
|
2020-11-09 13:29:05 +00:00
|
|
|
device_activated: self.device_activated.load(Ordering::Acquire),
|
2020-12-01 16:15:26 +00:00
|
|
|
interrupt_status: self.interrupt_status.load(Ordering::Acquire),
|
2021-06-01 09:30:03 +00:00
|
|
|
queues: self
|
|
|
|
.queues
|
|
|
|
.iter()
|
|
|
|
.map(|q| QueueState {
|
|
|
|
max_size: q.max_size,
|
|
|
|
size: q.size,
|
|
|
|
ready: q.ready,
|
|
|
|
vector: q.vector,
|
|
|
|
desc_table: q.desc_table.0,
|
|
|
|
avail_ring: q.avail_ring.0,
|
|
|
|
used_ring: q.used_ring.0,
|
|
|
|
})
|
|
|
|
.collect(),
|
2020-05-05 15:53:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &VirtioPciDeviceState) -> std::result::Result<(), Error> {
|
2020-11-09 13:29:05 +00:00
|
|
|
self.device_activated
|
|
|
|
.store(state.device_activated, Ordering::Release);
|
2020-05-05 15:53:21 +00:00
|
|
|
self.interrupt_status
|
2020-12-01 16:15:26 +00:00
|
|
|
.store(state.interrupt_status, Ordering::Release);
|
2020-05-05 15:53:21 +00:00
|
|
|
|
|
|
|
// Update virtqueues indexes for both available and used rings.
|
|
|
|
if let Some(mem) = self.memory.as_ref() {
|
|
|
|
let mem = mem.memory();
|
2020-05-18 12:35:10 +00:00
|
|
|
for (i, queue) in self.queues.iter_mut().enumerate() {
|
|
|
|
queue.max_size = state.queues[i].max_size;
|
|
|
|
queue.size = state.queues[i].size;
|
|
|
|
queue.ready = state.queues[i].ready;
|
|
|
|
queue.vector = state.queues[i].vector;
|
2021-06-01 09:30:03 +00:00
|
|
|
queue.desc_table = GuestAddress(state.queues[i].desc_table);
|
|
|
|
queue.avail_ring = GuestAddress(state.queues[i].avail_ring);
|
|
|
|
queue.used_ring = GuestAddress(state.queues[i].used_ring);
|
2020-05-05 15:53:21 +00:00
|
|
|
queue.next_avail = Wrapping(
|
|
|
|
queue
|
|
|
|
.used_index_from_memory(&mem)
|
|
|
|
.map_err(Error::QueueRingIndex)?,
|
|
|
|
);
|
|
|
|
queue.next_used = Wrapping(
|
|
|
|
queue
|
|
|
|
.used_index_from_memory(&mem)
|
|
|
|
.map_err(Error::QueueRingIndex)?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-06 17:27:40 +00:00
|
|
|
/// Gets the list of queue events that must be triggered whenever the VM writes to
|
|
|
|
/// `virtio::NOTIFY_REG_OFFSET` past the MMIO base. Each event must be triggered when the
|
|
|
|
/// value being written equals the index of the event in this list.
|
2019-10-30 18:03:02 +00:00
|
|
|
fn queue_evts(&self) -> &[EventFd] {
|
2019-05-06 17:27:40 +00:00
|
|
|
self.queue_evts.as_slice()
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
fn is_driver_ready(&self) -> bool {
|
|
|
|
let ready_bits =
|
|
|
|
(DEVICE_ACKNOWLEDGE | DEVICE_DRIVER | DEVICE_DRIVER_OK | DEVICE_FEATURES_OK) as u8;
|
|
|
|
self.common_config.driver_status == ready_bits
|
|
|
|
&& self.common_config.driver_status & DEVICE_FAILED as u8 == 0
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:59:39 +00:00
|
|
|
/// Determines if the driver has requested the device (re)init / reset itself
|
|
|
|
fn is_driver_init(&self) -> bool {
|
|
|
|
self.common_config.driver_status == DEVICE_INIT as u8
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:11:27 +00:00
|
|
|
// This function is used by the caller to provide the expected base address
|
|
|
|
// for the virtio-pci configuration BAR.
|
|
|
|
pub fn set_config_bar_addr(&mut self, bar_addr: u64) {
|
|
|
|
self.settings_bar_addr = Some(GuestAddress(bar_addr));
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:13:29 +00:00
|
|
|
pub fn config_bar_addr(&self) -> u64 {
|
|
|
|
self.configuration.get_bar_addr(self.settings_bar as usize)
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
fn add_pci_capabilities(
|
|
|
|
&mut self,
|
|
|
|
settings_bar: u8,
|
|
|
|
) -> std::result::Result<(), PciDeviceError> {
|
|
|
|
// Add pointers to the different configuration structures from the PCI capabilities.
|
|
|
|
let common_cap = VirtioPciCap::new(
|
|
|
|
PciCapabilityType::CommonConfig,
|
|
|
|
settings_bar,
|
|
|
|
COMMON_CONFIG_BAR_OFFSET as u32,
|
|
|
|
COMMON_CONFIG_SIZE as u32,
|
|
|
|
);
|
|
|
|
self.configuration
|
|
|
|
.add_capability(&common_cap)
|
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?;
|
|
|
|
|
|
|
|
let isr_cap = VirtioPciCap::new(
|
|
|
|
PciCapabilityType::IsrConfig,
|
|
|
|
settings_bar,
|
|
|
|
ISR_CONFIG_BAR_OFFSET as u32,
|
|
|
|
ISR_CONFIG_SIZE as u32,
|
|
|
|
);
|
|
|
|
self.configuration
|
|
|
|
.add_capability(&isr_cap)
|
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?;
|
|
|
|
|
|
|
|
// TODO(dgreid) - set based on device's configuration size?
|
|
|
|
let device_cap = VirtioPciCap::new(
|
|
|
|
PciCapabilityType::DeviceConfig,
|
|
|
|
settings_bar,
|
|
|
|
DEVICE_CONFIG_BAR_OFFSET as u32,
|
|
|
|
DEVICE_CONFIG_SIZE as u32,
|
|
|
|
);
|
|
|
|
self.configuration
|
|
|
|
.add_capability(&device_cap)
|
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?;
|
|
|
|
|
|
|
|
let notify_cap = VirtioPciNotifyCap::new(
|
|
|
|
PciCapabilityType::NotifyConfig,
|
|
|
|
settings_bar,
|
|
|
|
NOTIFICATION_BAR_OFFSET as u32,
|
|
|
|
NOTIFICATION_SIZE as u32,
|
|
|
|
Le32::from(NOTIFY_OFF_MULTIPLIER),
|
|
|
|
);
|
|
|
|
self.configuration
|
|
|
|
.add_capability(¬ify_cap)
|
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?;
|
|
|
|
|
2020-01-29 14:56:41 +00:00
|
|
|
let configuration_cap = VirtioPciCfgCap::new();
|
|
|
|
self.cap_pci_cfg_info.offset = self
|
|
|
|
.configuration
|
2019-04-18 17:32:41 +00:00
|
|
|
.add_capability(&configuration_cap)
|
2020-01-29 14:56:41 +00:00
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?
|
|
|
|
+ VIRTIO_PCI_CAP_OFFSET;
|
|
|
|
self.cap_pci_cfg_info.cap = configuration_cap;
|
2019-04-18 17:32:41 +00:00
|
|
|
|
2019-06-07 16:19:46 +00:00
|
|
|
if self.msix_config.is_some() {
|
|
|
|
let msix_cap = MsixCap::new(
|
|
|
|
settings_bar,
|
|
|
|
self.msix_num,
|
|
|
|
MSIX_TABLE_BAR_OFFSET as u32,
|
2019-07-17 00:19:21 +00:00
|
|
|
settings_bar,
|
2019-06-07 16:19:46 +00:00
|
|
|
MSIX_PBA_BAR_OFFSET as u32,
|
|
|
|
);
|
|
|
|
self.configuration
|
|
|
|
.add_capability(&msix_cap)
|
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?;
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
self.settings_bar = settings_bar;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-29 14:56:41 +00:00
|
|
|
|
|
|
|
fn read_cap_pci_cfg(&mut self, offset: usize, mut data: &mut [u8]) {
|
|
|
|
let cap_slice = self.cap_pci_cfg_info.cap.as_slice();
|
|
|
|
let data_len = data.len();
|
|
|
|
let cap_len = cap_slice.len();
|
|
|
|
if offset + data_len > cap_len {
|
|
|
|
error!("Failed to read cap_pci_cfg from config space");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < std::mem::size_of::<VirtioPciCap>() {
|
|
|
|
if let Some(end) = offset.checked_add(data_len) {
|
|
|
|
// This write can't fail, offset and end are checked against config_len.
|
|
|
|
data.write_all(&cap_slice[offset..cmp::min(end, cap_len)])
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Safe since we know self.cap_pci_cfg_info.cap.cap.offset is 32bits long.
|
|
|
|
let bar_offset: u32 =
|
|
|
|
unsafe { std::mem::transmute(self.cap_pci_cfg_info.cap.cap.offset) };
|
|
|
|
self.read_bar(0, bar_offset as u64, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:41:21 +00:00
|
|
|
fn write_cap_pci_cfg(&mut self, offset: usize, data: &[u8]) -> Option<Arc<Barrier>> {
|
2020-01-29 14:56:41 +00:00
|
|
|
let cap_slice = self.cap_pci_cfg_info.cap.as_mut_slice();
|
|
|
|
let data_len = data.len();
|
|
|
|
let cap_len = cap_slice.len();
|
|
|
|
if offset + data_len > cap_len {
|
|
|
|
error!("Failed to write cap_pci_cfg to config space");
|
2020-12-04 09:41:21 +00:00
|
|
|
return None;
|
2020-01-29 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset < std::mem::size_of::<VirtioPciCap>() {
|
|
|
|
let (_, right) = cap_slice.split_at_mut(offset);
|
2021-06-23 20:42:17 +00:00
|
|
|
right[..data_len].copy_from_slice(data);
|
2020-12-04 09:41:21 +00:00
|
|
|
None
|
2020-01-29 14:56:41 +00:00
|
|
|
} else {
|
|
|
|
// Safe since we know self.cap_pci_cfg_info.cap.cap.offset is 32bits long.
|
|
|
|
let bar_offset: u32 =
|
|
|
|
unsafe { std::mem::transmute(self.cap_pci_cfg_info.cap.cap.offset) };
|
|
|
|
self.write_bar(0, bar_offset as u64, data)
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 10:44:48 +00:00
|
|
|
|
|
|
|
pub fn virtio_device(&self) -> Arc<Mutex<dyn VirtioDevice>> {
|
|
|
|
self.device.clone()
|
|
|
|
}
|
2020-11-09 12:51:45 +00:00
|
|
|
|
2021-01-18 17:52:45 +00:00
|
|
|
fn activate(&mut self) -> ActivateResult {
|
|
|
|
if let Some(virtio_interrupt) = self.virtio_interrupt.take() {
|
|
|
|
if self.memory.is_some() {
|
|
|
|
let mem = self.memory.as_ref().unwrap().clone();
|
|
|
|
let mut device = self.device.lock().unwrap();
|
|
|
|
let mut queue_evts = Vec::new();
|
|
|
|
let mut queues = self.queues.clone();
|
|
|
|
queues.retain(|q| q.ready);
|
|
|
|
for (i, queue) in queues.iter().enumerate() {
|
|
|
|
queue_evts.push(self.queue_evts[i].try_clone().unwrap());
|
|
|
|
if !queue.is_valid(&mem.memory()) {
|
|
|
|
error!("Queue {} is not valid", i);
|
2021-01-18 12:38:08 +00:00
|
|
|
}
|
2020-11-09 12:51:45 +00:00
|
|
|
}
|
2021-01-18 17:52:45 +00:00
|
|
|
return device.activate(mem, virtio_interrupt, queues, queue_evts);
|
2020-11-09 12:51:45 +00:00
|
|
|
}
|
2021-01-18 17:52:45 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_activate(&mut self) {
|
|
|
|
if self.needs_activation() {
|
|
|
|
self.activate().expect("Failed to activate device");
|
|
|
|
self.device_activated.store(true, Ordering::SeqCst);
|
|
|
|
info!("{}: Waiting for barrier", self.id);
|
|
|
|
self.activate_barrier.wait();
|
|
|
|
info!("{}: Barrier released", self.id);
|
2021-01-07 06:57:17 +00:00
|
|
|
} else {
|
|
|
|
info!("{}: Device does not need activation", self.id)
|
2020-11-09 12:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn needs_activation(&self) -> bool {
|
2021-01-18 17:52:45 +00:00
|
|
|
!self.device_activated.load(Ordering::SeqCst) && self.is_driver_ready()
|
2020-11-09 12:51:45 +00:00
|
|
|
}
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 18:03:02 +00:00
|
|
|
impl VirtioTransport for VirtioPciDevice {
|
|
|
|
fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)> {
|
|
|
|
let notify_base = base_addr + NOTIFICATION_BAR_OFFSET;
|
|
|
|
self.queue_evts()
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, event)| {
|
|
|
|
(
|
|
|
|
event,
|
|
|
|
notify_base + i as u64 * u64::from(NOTIFY_OFF_MULTIPLIER),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
pub struct VirtioInterruptMsix {
|
|
|
|
msix_config: Arc<Mutex<MsixConfig>>,
|
|
|
|
config_vector: Arc<AtomicU16>,
|
2021-07-29 09:15:10 +00:00
|
|
|
interrupt_source_group: Arc<dyn InterruptSourceGroup>,
|
2020-01-13 17:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioInterruptMsix {
|
2020-01-14 22:47:41 +00:00
|
|
|
pub fn new(
|
|
|
|
msix_config: Arc<Mutex<MsixConfig>>,
|
|
|
|
config_vector: Arc<AtomicU16>,
|
2021-07-29 09:15:10 +00:00
|
|
|
interrupt_source_group: Arc<dyn InterruptSourceGroup>,
|
2020-01-14 22:47:41 +00:00
|
|
|
) -> Self {
|
2020-01-13 17:52:19 +00:00
|
|
|
VirtioInterruptMsix {
|
|
|
|
msix_config,
|
|
|
|
config_vector,
|
2020-01-14 22:47:41 +00:00
|
|
|
interrupt_source_group,
|
2020-01-13 17:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioInterrupt for VirtioInterruptMsix {
|
|
|
|
fn trigger(
|
|
|
|
&self,
|
|
|
|
int_type: &VirtioInterruptType,
|
|
|
|
queue: Option<&Queue>,
|
|
|
|
) -> std::result::Result<(), std::io::Error> {
|
|
|
|
let vector = match int_type {
|
2020-12-01 16:15:26 +00:00
|
|
|
VirtioInterruptType::Config => self.config_vector.load(Ordering::Acquire),
|
2020-01-13 17:52:19 +00:00
|
|
|
VirtioInterruptType::Queue => {
|
|
|
|
if let Some(q) = queue {
|
|
|
|
q.vector
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-06-03 20:57:26 +00:00
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
if vector == VIRTIO_MSI_NO_VECTOR {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-07-26 18:48:07 +00:00
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
let config = &mut self.msix_config.lock().unwrap();
|
|
|
|
let entry = &config.table_entries[vector as usize];
|
|
|
|
// In case the vector control register associated with the entry
|
|
|
|
// has its first bit set, this means the vector is masked and the
|
|
|
|
// device should not inject the interrupt.
|
|
|
|
// Instead, the Pending Bit Array table is updated to reflect there
|
|
|
|
// is a pending interrupt for this specific vector.
|
|
|
|
if config.masked() || entry.masked() {
|
|
|
|
config.set_pba_bit(vector, false);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-07-26 18:48:07 +00:00
|
|
|
|
2020-01-14 22:47:41 +00:00
|
|
|
self.interrupt_source_group
|
|
|
|
.trigger(vector as InterruptIndex)
|
2020-01-13 17:52:19 +00:00
|
|
|
}
|
2020-01-13 19:43:53 +00:00
|
|
|
|
2021-02-09 07:20:45 +00:00
|
|
|
fn notifier(&self, int_type: &VirtioInterruptType, queue: Option<&Queue>) -> Option<EventFd> {
|
2020-01-13 19:43:53 +00:00
|
|
|
let vector = match int_type {
|
2020-12-01 16:15:26 +00:00
|
|
|
VirtioInterruptType::Config => self.config_vector.load(Ordering::Acquire),
|
2020-01-13 19:43:53 +00:00
|
|
|
VirtioInterruptType::Queue => {
|
|
|
|
if let Some(q) = queue {
|
|
|
|
q.vector
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-14 22:47:41 +00:00
|
|
|
self.interrupt_source_group
|
|
|
|
.notifier(vector as InterruptIndex)
|
2020-01-13 19:43:53 +00:00
|
|
|
}
|
2020-01-13 17:52:19 +00:00
|
|
|
}
|
2019-06-03 20:57:26 +00:00
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
impl PciDevice for VirtioPciDevice {
|
2020-12-04 09:41:21 +00:00
|
|
|
fn write_config_register(
|
|
|
|
&mut self,
|
|
|
|
reg_idx: usize,
|
|
|
|
offset: u64,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Option<Arc<Barrier>> {
|
2020-01-29 14:56:41 +00:00
|
|
|
// Handle the special case where the capability VIRTIO_PCI_CAP_PCI_CFG
|
|
|
|
// is accessed. This capability has a special meaning as it allows the
|
|
|
|
// guest to access other capabilities without mapping the PCI BAR.
|
|
|
|
let base = reg_idx * 4;
|
|
|
|
if base + offset as usize >= self.cap_pci_cfg_info.offset
|
|
|
|
&& base + offset as usize + data.len()
|
|
|
|
<= self.cap_pci_cfg_info.offset + self.cap_pci_cfg_info.cap.bytes().len()
|
|
|
|
{
|
|
|
|
let offset = base + offset as usize - self.cap_pci_cfg_info.offset;
|
2020-12-04 09:41:21 +00:00
|
|
|
self.write_cap_pci_cfg(offset, data)
|
2020-01-29 14:56:41 +00:00
|
|
|
} else {
|
|
|
|
self.configuration
|
|
|
|
.write_config_register(reg_idx, offset, data);
|
2020-12-04 09:41:21 +00:00
|
|
|
None
|
2020-01-29 14:56:41 +00:00
|
|
|
}
|
2019-06-05 09:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 16:16:33 +00:00
|
|
|
fn read_config_register(&mut self, reg_idx: usize) -> u32 {
|
2020-01-29 14:56:41 +00:00
|
|
|
// Handle the special case where the capability VIRTIO_PCI_CAP_PCI_CFG
|
|
|
|
// is accessed. This capability has a special meaning as it allows the
|
|
|
|
// guest to access other capabilities without mapping the PCI BAR.
|
|
|
|
let base = reg_idx * 4;
|
|
|
|
if base >= self.cap_pci_cfg_info.offset
|
|
|
|
&& base + 4 <= self.cap_pci_cfg_info.offset + self.cap_pci_cfg_info.cap.bytes().len()
|
|
|
|
{
|
|
|
|
let offset = base - self.cap_pci_cfg_info.offset;
|
|
|
|
let mut data = [0u8; 4];
|
|
|
|
self.read_cap_pci_cfg(offset, &mut data);
|
|
|
|
u32::from_le_bytes(data)
|
|
|
|
} else {
|
|
|
|
self.configuration.read_reg(reg_idx)
|
|
|
|
}
|
2019-06-05 09:24:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 17:50:32 +00:00
|
|
|
fn detect_bar_reprogramming(
|
|
|
|
&mut self,
|
|
|
|
reg_idx: usize,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Option<BarReprogrammingParams> {
|
|
|
|
self.configuration.detect_bar_reprogramming(reg_idx, data)
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
fn allocate_bars(
|
|
|
|
&mut self,
|
|
|
|
allocator: &mut SystemAllocator,
|
2019-07-19 17:50:30 +00:00
|
|
|
) -> std::result::Result<Vec<(GuestAddress, GuestUsize, PciBarRegionType)>, PciDeviceError>
|
|
|
|
{
|
2019-04-18 17:32:41 +00:00
|
|
|
let mut ranges = Vec::new();
|
2019-11-18 23:10:42 +00:00
|
|
|
let device_clone = self.device.clone();
|
|
|
|
let device = device_clone.lock().unwrap();
|
2019-04-18 17:32:41 +00:00
|
|
|
|
|
|
|
// Allocate the virtio-pci capability BAR.
|
|
|
|
// See http://docs.oasis-open.org/virtio/virtio/v1.0/cs04/virtio-v1.0-cs04.html#x1-740004
|
2019-09-20 21:48:34 +00:00
|
|
|
let (virtio_pci_bar_addr, region_type) = if self.use_64bit_bar {
|
|
|
|
let region_type = PciBarRegionType::Memory64BitRegion;
|
2019-09-17 16:34:51 +00:00
|
|
|
let addr = allocator
|
2021-04-21 14:00:53 +00:00
|
|
|
.allocate_mmio_addresses(
|
|
|
|
self.settings_bar_addr,
|
|
|
|
CAPABILITY_BAR_SIZE,
|
|
|
|
Some(CAPABILITY_BAR_SIZE),
|
|
|
|
)
|
2019-09-17 16:34:51 +00:00
|
|
|
.ok_or(PciDeviceError::IoAllocationFailed(CAPABILITY_BAR_SIZE))?;
|
2019-09-20 21:48:34 +00:00
|
|
|
ranges.push((addr, CAPABILITY_BAR_SIZE, region_type));
|
|
|
|
(addr, region_type)
|
2019-09-17 16:34:51 +00:00
|
|
|
} else {
|
2019-09-20 21:48:34 +00:00
|
|
|
let region_type = PciBarRegionType::Memory32BitRegion;
|
2019-09-17 16:34:51 +00:00
|
|
|
let addr = allocator
|
2021-04-21 14:00:53 +00:00
|
|
|
.allocate_mmio_hole_addresses(
|
|
|
|
self.settings_bar_addr,
|
|
|
|
CAPABILITY_BAR_SIZE,
|
|
|
|
Some(CAPABILITY_BAR_SIZE),
|
|
|
|
)
|
2019-09-17 16:34:51 +00:00
|
|
|
.ok_or(PciDeviceError::IoAllocationFailed(CAPABILITY_BAR_SIZE))?;
|
2019-09-20 21:48:34 +00:00
|
|
|
ranges.push((addr, CAPABILITY_BAR_SIZE, region_type));
|
|
|
|
(addr, region_type)
|
2019-09-17 16:34:51 +00:00
|
|
|
};
|
2020-04-08 15:27:08 +00:00
|
|
|
self.bar_regions
|
|
|
|
.push((virtio_pci_bar_addr, CAPABILITY_BAR_SIZE, region_type));
|
2019-09-17 16:34:51 +00:00
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
let config = PciBarConfiguration::default()
|
|
|
|
.set_register_index(0)
|
|
|
|
.set_address(virtio_pci_bar_addr.raw_value())
|
2019-09-20 21:48:34 +00:00
|
|
|
.set_size(CAPABILITY_BAR_SIZE)
|
|
|
|
.set_region_type(region_type);
|
2019-04-18 17:32:41 +00:00
|
|
|
let virtio_pci_bar =
|
|
|
|
self.configuration.add_pci_bar(&config).map_err(|e| {
|
|
|
|
PciDeviceError::IoRegistrationFailed(virtio_pci_bar_addr.raw_value(), e)
|
|
|
|
})? as u8;
|
|
|
|
|
|
|
|
// Once the BARs are allocated, the capabilities can be added to the PCI configuration.
|
|
|
|
self.add_pci_capabilities(virtio_pci_bar)?;
|
|
|
|
|
2019-08-06 00:58:55 +00:00
|
|
|
// Allocate a dedicated BAR if there are some shared memory regions.
|
2019-11-18 23:10:42 +00:00
|
|
|
if let Some(shm_list) = device.get_shm_regions() {
|
2019-08-06 00:58:55 +00:00
|
|
|
let config = PciBarConfiguration::default()
|
|
|
|
.set_register_index(2)
|
|
|
|
.set_address(shm_list.addr.raw_value())
|
|
|
|
.set_size(shm_list.len);
|
|
|
|
let virtio_pci_shm_bar =
|
|
|
|
self.configuration.add_pci_bar(&config).map_err(|e| {
|
|
|
|
PciDeviceError::IoRegistrationFailed(shm_list.addr.raw_value(), e)
|
|
|
|
})? as u8;
|
|
|
|
|
2020-04-17 16:32:07 +00:00
|
|
|
let region_type = PciBarRegionType::Memory64BitRegion;
|
|
|
|
ranges.push((shm_list.addr, shm_list.len, region_type));
|
|
|
|
self.bar_regions
|
|
|
|
.push((shm_list.addr, shm_list.len, region_type));
|
|
|
|
|
2019-08-06 00:58:55 +00:00
|
|
|
for (idx, shm) in shm_list.region_list.iter().enumerate() {
|
|
|
|
let shm_cap = VirtioPciCap64::new(
|
|
|
|
PciCapabilityType::SharedMemoryConfig,
|
|
|
|
virtio_pci_shm_bar,
|
|
|
|
idx as u8,
|
|
|
|
shm.offset,
|
|
|
|
shm.len,
|
|
|
|
);
|
|
|
|
self.configuration
|
|
|
|
.add_capability(&shm_cap)
|
|
|
|
.map_err(PciDeviceError::CapabilitiesSetup)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
Ok(ranges)
|
|
|
|
}
|
2020-04-08 15:27:08 +00:00
|
|
|
|
|
|
|
fn free_bars(
|
|
|
|
&mut self,
|
|
|
|
allocator: &mut SystemAllocator,
|
|
|
|
) -> std::result::Result<(), PciDeviceError> {
|
|
|
|
for (addr, length, type_) in self.bar_regions.drain(..) {
|
|
|
|
match type_ {
|
|
|
|
PciBarRegionType::Memory32BitRegion => {
|
|
|
|
allocator.free_mmio_hole_addresses(addr, length);
|
|
|
|
}
|
|
|
|
PciBarRegionType::Memory64BitRegion => {
|
|
|
|
allocator.free_mmio_addresses(addr, length);
|
|
|
|
}
|
|
|
|
_ => error!("Unexpected PCI bar type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-09 12:15:18 +00:00
|
|
|
|
|
|
|
fn move_bar(&mut self, old_base: u64, new_base: u64) -> result::Result<(), std::io::Error> {
|
|
|
|
// We only update our idea of the bar in order to support free_bars() above.
|
|
|
|
// The majority of the reallocation is done inside DeviceManager.
|
|
|
|
for (addr, _, _) in self.bar_regions.iter_mut() {
|
|
|
|
if (*addr).0 == old_base {
|
|
|
|
*addr = GuestAddress(new_base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-18 17:32:41 +00:00
|
|
|
|
2019-07-02 23:27:38 +00:00
|
|
|
fn read_bar(&mut self, _base: u64, offset: u64, data: &mut [u8]) {
|
2019-04-18 17:32:41 +00:00
|
|
|
match offset {
|
|
|
|
o if o < COMMON_CONFIG_BAR_OFFSET + COMMON_CONFIG_SIZE => self.common_config.read(
|
|
|
|
o - COMMON_CONFIG_BAR_OFFSET,
|
|
|
|
data,
|
|
|
|
&mut self.queues,
|
2019-11-18 23:10:42 +00:00
|
|
|
self.device.clone(),
|
2019-04-18 17:32:41 +00:00
|
|
|
),
|
|
|
|
o if ISR_CONFIG_BAR_OFFSET <= o && o < ISR_CONFIG_BAR_OFFSET + ISR_CONFIG_SIZE => {
|
|
|
|
if let Some(v) = data.get_mut(0) {
|
|
|
|
// Reading this register resets it to 0.
|
2020-12-01 16:15:26 +00:00
|
|
|
*v = self.interrupt_status.swap(0, Ordering::AcqRel) as u8;
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
o if DEVICE_CONFIG_BAR_OFFSET <= o
|
|
|
|
&& o < DEVICE_CONFIG_BAR_OFFSET + DEVICE_CONFIG_SIZE =>
|
|
|
|
{
|
2019-11-18 23:10:42 +00:00
|
|
|
let device = self.device.lock().unwrap();
|
|
|
|
device.read_config(o - DEVICE_CONFIG_BAR_OFFSET, data);
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
o if NOTIFICATION_BAR_OFFSET <= o
|
|
|
|
&& o < NOTIFICATION_BAR_OFFSET + NOTIFICATION_SIZE =>
|
|
|
|
{
|
|
|
|
// Handled with ioeventfds.
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
o if MSIX_TABLE_BAR_OFFSET <= o && o < MSIX_TABLE_BAR_OFFSET + MSIX_TABLE_SIZE => {
|
2019-06-07 16:19:46 +00:00
|
|
|
if let Some(msix_config) = &self.msix_config {
|
|
|
|
msix_config
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.read_table(o - MSIX_TABLE_BAR_OFFSET, data);
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
}
|
|
|
|
o if MSIX_PBA_BAR_OFFSET <= o && o < MSIX_PBA_BAR_OFFSET + MSIX_PBA_SIZE => {
|
2019-06-07 16:19:46 +00:00
|
|
|
if let Some(msix_config) = &self.msix_config {
|
|
|
|
msix_config
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.read_pba(o - MSIX_PBA_BAR_OFFSET, data);
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
}
|
2019-04-18 17:32:41 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:41:21 +00:00
|
|
|
fn write_bar(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option<Arc<Barrier>> {
|
2019-04-18 17:32:41 +00:00
|
|
|
match offset {
|
|
|
|
o if o < COMMON_CONFIG_BAR_OFFSET + COMMON_CONFIG_SIZE => self.common_config.write(
|
|
|
|
o - COMMON_CONFIG_BAR_OFFSET,
|
|
|
|
data,
|
|
|
|
&mut self.queues,
|
2019-11-18 23:10:42 +00:00
|
|
|
self.device.clone(),
|
2019-04-18 17:32:41 +00:00
|
|
|
),
|
|
|
|
o if ISR_CONFIG_BAR_OFFSET <= o && o < ISR_CONFIG_BAR_OFFSET + ISR_CONFIG_SIZE => {
|
|
|
|
if let Some(v) = data.get(0) {
|
|
|
|
self.interrupt_status
|
2020-12-01 16:15:26 +00:00
|
|
|
.fetch_and(!(*v as usize), Ordering::AcqRel);
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
o if DEVICE_CONFIG_BAR_OFFSET <= o
|
|
|
|
&& o < DEVICE_CONFIG_BAR_OFFSET + DEVICE_CONFIG_SIZE =>
|
|
|
|
{
|
2019-11-18 23:10:42 +00:00
|
|
|
let mut device = self.device.lock().unwrap();
|
|
|
|
device.write_config(o - DEVICE_CONFIG_BAR_OFFSET, data);
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
o if NOTIFICATION_BAR_OFFSET <= o
|
|
|
|
&& o < NOTIFICATION_BAR_OFFSET + NOTIFICATION_SIZE =>
|
|
|
|
{
|
|
|
|
// Handled with ioeventfds.
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
o if MSIX_TABLE_BAR_OFFSET <= o && o < MSIX_TABLE_BAR_OFFSET + MSIX_TABLE_SIZE => {
|
2019-06-07 16:19:46 +00:00
|
|
|
if let Some(msix_config) = &self.msix_config {
|
|
|
|
msix_config
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.write_table(o - MSIX_TABLE_BAR_OFFSET, data);
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
}
|
|
|
|
o if MSIX_PBA_BAR_OFFSET <= o && o < MSIX_PBA_BAR_OFFSET + MSIX_PBA_SIZE => {
|
2019-06-07 16:19:46 +00:00
|
|
|
if let Some(msix_config) = &self.msix_config {
|
|
|
|
msix_config
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.write_pba(o - MSIX_PBA_BAR_OFFSET, data);
|
|
|
|
}
|
2019-05-29 23:33:29 +00:00
|
|
|
}
|
2019-04-18 17:32:41 +00:00
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
2020-11-09 12:51:45 +00:00
|
|
|
// Try and activate the device if the driver status has changed
|
2020-11-09 13:29:05 +00:00
|
|
|
if self.needs_activation() {
|
2021-01-07 06:57:17 +00:00
|
|
|
info!(
|
|
|
|
"{}: Needs activation; writing to activate event fd",
|
|
|
|
self.id
|
|
|
|
);
|
2020-11-09 13:29:05 +00:00
|
|
|
self.activate_evt.write(1).ok();
|
2021-01-07 06:57:17 +00:00
|
|
|
info!("{}: Needs activation; returning barrier", self.id);
|
2020-11-09 13:29:05 +00:00
|
|
|
return Some(self.activate_barrier.clone());
|
|
|
|
}
|
2019-05-08 14:59:39 +00:00
|
|
|
|
|
|
|
// Device has been reset by the driver
|
2020-11-09 13:29:05 +00:00
|
|
|
if self.device_activated.load(Ordering::SeqCst) && self.is_driver_init() {
|
2019-11-18 23:10:42 +00:00
|
|
|
let mut device = self.device.lock().unwrap();
|
2021-01-18 12:38:08 +00:00
|
|
|
if let Some(virtio_interrupt) = device.reset() {
|
|
|
|
// Upon reset the device returns its interrupt EventFD
|
2020-01-13 17:52:19 +00:00
|
|
|
self.virtio_interrupt = Some(virtio_interrupt);
|
2020-11-09 13:29:05 +00:00
|
|
|
self.device_activated.store(false, Ordering::SeqCst);
|
2019-05-08 14:59:39 +00:00
|
|
|
|
|
|
|
// Reset queue readiness (changes queue_enable), queue sizes
|
|
|
|
// and selected_queue as per spec for reset
|
|
|
|
self.queues.iter_mut().for_each(Queue::reset);
|
|
|
|
self.common_config.queue_select = 0;
|
|
|
|
} else {
|
|
|
|
error!("Attempt to reset device when not implemented in underlying device");
|
|
|
|
self.common_config.driver_status = crate::DEVICE_FAILED as u8;
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 09:41:21 +00:00
|
|
|
|
|
|
|
None
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
2019-10-30 15:15:38 +00:00
|
|
|
|
|
|
|
fn as_any(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
|
|
|
}
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BusDevice for VirtioPciDevice {
|
2019-07-02 23:27:38 +00:00
|
|
|
fn read(&mut self, base: u64, offset: u64, data: &mut [u8]) {
|
|
|
|
self.read_bar(base, offset, data)
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 09:23:47 +00:00
|
|
|
fn write(&mut self, base: u64, offset: u64, data: &[u8]) -> Option<Arc<Barrier>> {
|
2020-12-04 09:41:21 +00:00
|
|
|
self.write_bar(base, offset, data)
|
2019-04-18 17:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
|
|
|
impl Pausable for VirtioPciDevice {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 14:19:40 +00:00
|
|
|
impl Snapshottable for VirtioPciDevice {
|
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
2020-05-05 15:53:21 +00:00
|
|
|
|
2020-08-21 12:31:58 +00:00
|
|
|
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
2021-06-01 09:40:40 +00:00
|
|
|
let mut virtio_pci_dev_snapshot =
|
|
|
|
Snapshot::new_from_versioned_state(&self.id, &self.state())?;
|
2020-05-05 15:53:21 +00:00
|
|
|
|
|
|
|
// Snapshot PciConfiguration
|
|
|
|
virtio_pci_dev_snapshot.add_snapshot(self.configuration.snapshot()?);
|
|
|
|
|
|
|
|
// Snapshot VirtioPciCommonConfig
|
|
|
|
virtio_pci_dev_snapshot.add_snapshot(self.common_config.snapshot()?);
|
|
|
|
|
|
|
|
// Snapshot MSI-X
|
|
|
|
if let Some(msix_config) = &self.msix_config {
|
|
|
|
virtio_pci_dev_snapshot.add_snapshot(msix_config.lock().unwrap().snapshot()?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(virtio_pci_dev_snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
|
|
|
if let Some(virtio_pci_dev_section) =
|
|
|
|
snapshot.snapshot_data.get(&format!("{}-section", self.id))
|
|
|
|
{
|
|
|
|
// Restore MSI-X
|
|
|
|
if let Some(msix_config) = &self.msix_config {
|
|
|
|
let id = msix_config.lock().unwrap().id();
|
|
|
|
if let Some(msix_snapshot) = snapshot.snapshots.get(&id) {
|
|
|
|
msix_config
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.restore(*msix_snapshot.clone())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore VirtioPciCommonConfig
|
|
|
|
if let Some(virtio_config_snapshot) = snapshot.snapshots.get(&self.common_config.id()) {
|
|
|
|
self.common_config
|
|
|
|
.restore(*virtio_config_snapshot.clone())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore PciConfiguration
|
|
|
|
if let Some(pci_config_snapshot) = snapshot.snapshots.get(&self.configuration.id()) {
|
|
|
|
self.configuration.restore(*pci_config_snapshot.clone())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First restore the status of the virtqueues.
|
2021-06-01 09:40:40 +00:00
|
|
|
self.set_state(&virtio_pci_dev_section.to_versioned_state()?)
|
2021-04-14 15:49:49 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
MigratableError::Restore(anyhow!(
|
|
|
|
"Could not restore VIRTIO_PCI_DEVICE state {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})?;
|
2020-05-05 15:53:21 +00:00
|
|
|
|
|
|
|
// Then we can activate the device, as we know at this point that
|
|
|
|
// the virtqueues are in the right state and the device is ready
|
|
|
|
// to be activated, which will spawn each virtio worker thread.
|
2021-01-18 17:52:45 +00:00
|
|
|
if self.device_activated.load(Ordering::SeqCst) && self.is_driver_ready() {
|
|
|
|
self.activate().map_err(|e| {
|
|
|
|
MigratableError::Restore(anyhow!("Failed activating the device: {:?}", e))
|
|
|
|
})?;
|
2020-05-05 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not find VIRTIO_PCI_DEVICE snapshot section"
|
|
|
|
)))
|
|
|
|
}
|
2020-04-27 14:19:40 +00:00
|
|
|
}
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for VirtioPciDevice {}
|
2019-11-19 00:42:31 +00:00
|
|
|
impl Migratable for VirtioPciDevice {}
|