2019-09-17 18:40:14 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
|
2019-12-31 10:49:11 +00:00
|
|
|
use super::Error as DeviceError;
|
|
|
|
use super::{
|
2021-10-21 10:41:16 +00:00
|
|
|
ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon, VirtioDevice,
|
|
|
|
VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_VERSION_1,
|
2019-12-31 10:49:11 +00:00
|
|
|
};
|
2021-09-03 10:43:30 +00:00
|
|
|
use crate::seccomp_filters::Thread;
|
|
|
|
use crate::thread_helper::spawn_virtio_thread;
|
2021-06-02 19:08:04 +00:00
|
|
|
use crate::GuestMemoryMmap;
|
2019-12-31 10:49:11 +00:00
|
|
|
use crate::{DmaRemapping, VirtioInterrupt, VirtioInterruptType};
|
2022-08-12 01:30:13 +00:00
|
|
|
use anyhow::anyhow;
|
2021-09-03 10:43:30 +00:00
|
|
|
use seccompiler::SeccompAction;
|
2019-09-17 18:40:14 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-07-16 09:34:51 +00:00
|
|
|
use std::io;
|
2019-09-17 18:40:14 +00:00
|
|
|
use std::mem::size_of;
|
|
|
|
use std::ops::Bound::Included;
|
2020-08-04 11:16:44 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-09-17 18:40:14 +00:00
|
|
|
use std::result;
|
2022-04-25 15:18:22 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2022-04-04 14:23:49 +00:00
|
|
|
use std::sync::{Arc, Barrier, Mutex, RwLock};
|
2022-10-13 19:05:45 +00:00
|
|
|
use thiserror::Error;
|
2021-05-06 13:34:31 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2022-07-08 12:30:50 +00:00
|
|
|
use virtio_queue::{DescriptorChain, Queue, QueueT};
|
2021-02-23 13:57:10 +00:00
|
|
|
use vm_device::dma_mapping::ExternalDmaMapping;
|
2021-12-21 09:03:15 +00:00
|
|
|
use vm_memory::{
|
2022-07-06 14:08:08 +00:00
|
|
|
Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic,
|
|
|
|
GuestMemoryError, GuestMemoryLoadGuard,
|
2021-12-21 09:03:15 +00:00
|
|
|
};
|
2021-05-06 13:34:31 +00:00
|
|
|
use vm_migration::VersionMapped;
|
2021-04-08 09:20:10 +00:00
|
|
|
use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable};
|
2022-01-26 16:33:36 +00:00
|
|
|
use vm_virtio::AccessPlatform;
|
2019-09-17 18:40:14 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
/// Queues sizes
|
|
|
|
const QUEUE_SIZE: u16 = 256;
|
|
|
|
const NUM_QUEUES: usize = 2;
|
|
|
|
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE; NUM_QUEUES];
|
|
|
|
|
|
|
|
/// New descriptors are pending on the request queue.
|
|
|
|
/// "requestq" is meant to be used anytime an action is required to be
|
|
|
|
/// performed on behalf of the guest driver.
|
2020-08-04 11:16:44 +00:00
|
|
|
const REQUEST_Q_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
2019-09-17 18:40:14 +00:00
|
|
|
/// New descriptors are pending on the event queue.
|
|
|
|
/// "eventq" lets the device report any fault or other asynchronous event to
|
|
|
|
/// the guest driver.
|
2022-10-20 15:15:00 +00:00
|
|
|
const _EVENT_Q_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2020-01-06 15:45:49 +00:00
|
|
|
/// PROBE properties size.
|
|
|
|
/// This is the minimal size to provide at least one RESV_MEM property.
|
|
|
|
/// Because virtio-iommu expects one MSI reserved region, we must provide it,
|
|
|
|
/// otherwise the driver in the guest will define a predefined one between
|
|
|
|
/// 0x8000000 and 0x80FFFFF, which is only relevant for ARM architecture, but
|
|
|
|
/// will conflict with x86.
|
|
|
|
const PROBE_PROP_SIZE: u32 =
|
|
|
|
(size_of::<VirtioIommuProbeProperty>() + size_of::<VirtioIommuProbeResvMem>()) as u32;
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
/// Virtio IOMMU features
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_F_INPUT_RANGE: u32 = 0;
|
|
|
|
#[allow(unused)]
|
2021-06-14 13:38:24 +00:00
|
|
|
const VIRTIO_IOMMU_F_DOMAIN_RANGE: u32 = 1;
|
2019-09-17 18:40:14 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_F_MAP_UNMAP: u32 = 2;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_F_BYPASS: u32 = 3;
|
|
|
|
const VIRTIO_IOMMU_F_PROBE: u32 = 4;
|
2020-01-28 11:06:21 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_F_MMIO: u32 = 5;
|
2021-06-14 13:38:24 +00:00
|
|
|
const VIRTIO_IOMMU_F_BYPASS_CONFIG: u32 = 6;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2019-10-17 19:55:23 +00:00
|
|
|
// Support 2MiB and 4KiB page sizes.
|
|
|
|
const VIRTIO_IOMMU_PAGE_SIZE_MASK: u64 = (2 << 20) | (4 << 10);
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2020-01-06 15:45:49 +00:00
|
|
|
struct VirtioIommuRange32 {
|
|
|
|
start: u32,
|
|
|
|
end: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2020-01-06 15:45:49 +00:00
|
|
|
struct VirtioIommuRange64 {
|
2019-09-17 18:40:14 +00:00
|
|
|
start: u64,
|
|
|
|
end: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2019-09-17 18:40:14 +00:00
|
|
|
struct VirtioIommuConfig {
|
|
|
|
page_size_mask: u64,
|
2020-01-06 15:45:49 +00:00
|
|
|
input_range: VirtioIommuRange64,
|
|
|
|
domain_range: VirtioIommuRange32,
|
2019-09-17 18:40:14 +00:00
|
|
|
probe_size: u32,
|
2021-06-14 13:38:24 +00:00
|
|
|
bypass: u8,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u8; 7],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio IOMMU request type
|
|
|
|
const VIRTIO_IOMMU_T_ATTACH: u8 = 1;
|
|
|
|
const VIRTIO_IOMMU_T_DETACH: u8 = 2;
|
|
|
|
const VIRTIO_IOMMU_T_MAP: u8 = 3;
|
|
|
|
const VIRTIO_IOMMU_T_UNMAP: u8 = 4;
|
|
|
|
const VIRTIO_IOMMU_T_PROBE: u8 = 5;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct VirtioIommuReqHead {
|
|
|
|
type_: u8,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u8; 3],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio IOMMU request status
|
|
|
|
const VIRTIO_IOMMU_S_OK: u8 = 0;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_IOERR: u8 = 1;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_UNSUPP: u8 = 2;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_DEVERR: u8 = 3;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_INVAL: u8 = 4;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_RANGE: u8 = 5;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_NOENT: u8 = 6;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_FAULT: u8 = 7;
|
2021-06-14 13:38:24 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_S_NOMEM: u8 = 8;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2019-09-17 18:40:14 +00:00
|
|
|
struct VirtioIommuReqTail {
|
|
|
|
status: u8,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u8; 3],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ATTACH request
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct VirtioIommuReqAttach {
|
|
|
|
domain: u32,
|
|
|
|
endpoint: u32,
|
2022-04-25 16:04:02 +00:00
|
|
|
flags: u32,
|
|
|
|
_reserved: [u8; 4],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 16:04:02 +00:00
|
|
|
const VIRTIO_IOMMU_ATTACH_F_BYPASS: u32 = 1;
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
/// DETACH request
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct VirtioIommuReqDetach {
|
|
|
|
domain: u32,
|
|
|
|
endpoint: u32,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u8; 8],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio IOMMU request MAP flags
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_MAP_F_READ: u32 = 1;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_MAP_F_WRITE: u32 = 1 << 1;
|
|
|
|
#[allow(unused)]
|
2021-06-14 13:38:24 +00:00
|
|
|
const VIRTIO_IOMMU_MAP_F_MMIO: u32 = 1 << 2;
|
2019-09-17 18:40:14 +00:00
|
|
|
#[allow(unused)]
|
2021-06-14 13:38:24 +00:00
|
|
|
const VIRTIO_IOMMU_MAP_F_MASK: u32 =
|
|
|
|
VIRTIO_IOMMU_MAP_F_READ | VIRTIO_IOMMU_MAP_F_WRITE | VIRTIO_IOMMU_MAP_F_MMIO;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
/// MAP request
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct VirtioIommuReqMap {
|
|
|
|
domain: u32,
|
|
|
|
virt_start: u64,
|
|
|
|
virt_end: u64,
|
|
|
|
phys_start: u64,
|
2021-10-19 14:01:42 +00:00
|
|
|
_flags: u32,
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// UNMAP request
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct VirtioIommuReqUnmap {
|
|
|
|
domain: u32,
|
|
|
|
virt_start: u64,
|
|
|
|
virt_end: u64,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u8; 4],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio IOMMU request PROBE types
|
|
|
|
#[allow(unused)]
|
2020-01-06 15:45:49 +00:00
|
|
|
const VIRTIO_IOMMU_PROBE_T_NONE: u16 = 0;
|
|
|
|
const VIRTIO_IOMMU_PROBE_T_RESV_MEM: u16 = 1;
|
2021-06-14 13:38:24 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_PROBE_T_MASK: u16 = 0xfff;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
/// PROBE request
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2019-09-17 18:40:14 +00:00
|
|
|
struct VirtioIommuReqProbe {
|
|
|
|
endpoint: u32,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u64; 8],
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2019-09-17 18:40:14 +00:00
|
|
|
struct VirtioIommuProbeProperty {
|
|
|
|
type_: u16,
|
|
|
|
length: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio IOMMU request PROBE property RESV_MEM subtypes
|
|
|
|
#[allow(unused)]
|
2020-01-06 15:45:49 +00:00
|
|
|
const VIRTIO_IOMMU_RESV_MEM_T_RESERVED: u8 = 0;
|
|
|
|
const VIRTIO_IOMMU_RESV_MEM_T_MSI: u8 = 1;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
2021-10-19 14:01:42 +00:00
|
|
|
#[allow(dead_code)]
|
2019-09-17 18:40:14 +00:00
|
|
|
struct VirtioIommuProbeResvMem {
|
|
|
|
subtype: u8,
|
2021-10-19 14:01:42 +00:00
|
|
|
_reserved: [u8; 3],
|
2019-09-17 18:40:14 +00:00
|
|
|
start: u64,
|
|
|
|
end: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio IOMMU fault flags
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_F_READ: u32 = 1;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_F_WRITE: u32 = 1 << 1;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_F_EXEC: u32 = 1 << 2;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_F_ADDRESS: u32 = 1 << 8;
|
|
|
|
|
|
|
|
/// Virtio IOMMU fault reasons
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_R_UNKNOWN: u32 = 0;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_R_DOMAIN: u32 = 1;
|
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_IOMMU_FAULT_R_MAPPING: u32 = 2;
|
|
|
|
|
|
|
|
/// Fault reporting through eventq
|
|
|
|
#[allow(unused)]
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(packed)]
|
|
|
|
struct VirtioIommuFault {
|
|
|
|
reason: u8,
|
|
|
|
reserved: [u8; 3],
|
|
|
|
flags: u32,
|
|
|
|
endpoint: u32,
|
2021-06-14 13:38:24 +00:00
|
|
|
reserved2: [u8; 4],
|
2019-09-17 18:40:14 +00:00
|
|
|
address: u64,
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:39:53 +00:00
|
|
|
// SAFETY: these data structures only contain integers and have no implicit padding
|
|
|
|
unsafe impl ByteValued for VirtioIommuRange32 {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuRange64 {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuConfig {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqHead {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqTail {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqAttach {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqDetach {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqMap {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqUnmap {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuReqProbe {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuProbeProperty {}
|
|
|
|
unsafe impl ByteValued for VirtioIommuProbeResvMem {}
|
2019-09-17 18:40:14 +00:00
|
|
|
unsafe impl ByteValued for VirtioIommuFault {}
|
|
|
|
|
2022-10-13 19:05:45 +00:00
|
|
|
#[derive(Error, Debug)]
|
2019-09-17 18:40:14 +00:00
|
|
|
enum Error {
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest gave us bad memory addresses: {0}")]
|
2019-09-17 18:40:14 +00:00
|
|
|
GuestMemory(GuestMemoryError),
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest gave us a write only descriptor that protocol says to read from")]
|
2019-09-17 18:40:14 +00:00
|
|
|
UnexpectedWriteOnlyDescriptor,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest gave us a read only descriptor that protocol says to write to")]
|
2019-09-17 18:40:14 +00:00
|
|
|
UnexpectedReadOnlyDescriptor,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest gave us too few descriptors in a descriptor chain")]
|
2019-09-17 18:40:14 +00:00
|
|
|
DescriptorChainTooShort,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest gave us a buffer that was too short to use")]
|
2019-09-17 18:40:14 +00:00
|
|
|
BufferLengthTooSmall,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest sent us invalid request")]
|
2019-09-17 18:40:14 +00:00
|
|
|
InvalidRequest,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest sent us invalid ATTACH request")]
|
2019-09-17 18:40:14 +00:00
|
|
|
InvalidAttachRequest,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest sent us invalid DETACH request")]
|
2019-09-17 18:40:14 +00:00
|
|
|
InvalidDetachRequest,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest sent us invalid MAP request")]
|
2019-09-17 18:40:14 +00:00
|
|
|
InvalidMapRequest,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Invalid to map because the domain is in bypass mode")]
|
2022-04-25 16:04:02 +00:00
|
|
|
InvalidMapRequestBypassDomain,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Invalid to map because the domain is missing")]
|
2022-04-25 16:04:02 +00:00
|
|
|
InvalidMapRequestMissingDomain,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest sent us invalid UNMAP request")]
|
2019-09-17 18:40:14 +00:00
|
|
|
InvalidUnmapRequest,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Invalid to unmap because the domain is in bypass mode")]
|
2022-04-25 16:04:02 +00:00
|
|
|
InvalidUnmapRequestBypassDomain,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Invalid to unmap because the domain is missing")]
|
2022-04-25 16:04:02 +00:00
|
|
|
InvalidUnmapRequestMissingDomain,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Guest sent us invalid PROBE request")]
|
2019-09-17 18:40:14 +00:00
|
|
|
InvalidProbeRequest,
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Failed to performing external mapping: {0}")]
|
2019-10-08 00:12:11 +00:00
|
|
|
ExternalMapping(io::Error),
|
2022-10-20 18:45:16 +00:00
|
|
|
#[error("Failed to performing external unmapping: {0}")]
|
2019-10-08 00:12:11 +00:00
|
|
|
ExternalUnmapping(io::Error),
|
2022-10-13 19:08:32 +00:00
|
|
|
#[error("Failed adding used index: {0}")]
|
|
|
|
QueueAddUsed(virtio_queue::Error),
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 15:45:49 +00:00
|
|
|
struct Request {}
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
impl Request {
|
2019-10-08 00:12:11 +00:00
|
|
|
// Parse the available vring buffer. Based on the hashmap table of external
|
|
|
|
// mappings required from various devices such as VFIO or vhost-user ones,
|
|
|
|
// this function might update the hashmap table of external mappings per
|
|
|
|
// domain.
|
|
|
|
// Basically, the VMM knows about the device_id <=> mapping relationship
|
|
|
|
// before running the VM, but at runtime, a new domain <=> mapping hashmap
|
|
|
|
// is created based on the information provided from the guest driver for
|
|
|
|
// virtio-iommu (giving the link device_id <=> domain).
|
2019-09-17 18:40:14 +00:00
|
|
|
fn parse(
|
2021-12-21 09:03:15 +00:00
|
|
|
desc_chain: &mut DescriptorChain<GuestMemoryLoadGuard<GuestMemoryMmap>>,
|
2019-09-17 18:40:14 +00:00
|
|
|
mapping: &Arc<IommuMapping>,
|
2019-10-08 00:12:11 +00:00
|
|
|
ext_mapping: &BTreeMap<u32, Arc<dyn ExternalDmaMapping>>,
|
2021-09-06 12:58:48 +00:00
|
|
|
msi_iova_space: (u64, u64),
|
2020-01-06 15:45:49 +00:00
|
|
|
) -> result::Result<usize, Error> {
|
2021-10-21 10:41:16 +00:00
|
|
|
let desc = desc_chain
|
|
|
|
.next()
|
|
|
|
.ok_or(Error::DescriptorChainTooShort)
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Missing head descriptor");
|
|
|
|
e
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// The descriptor contains the request type which MUST be readable.
|
|
|
|
if desc.is_write_only() {
|
2019-09-17 18:40:14 +00:00
|
|
|
return Err(Error::UnexpectedWriteOnlyDescriptor);
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
if (desc.len() as usize) < size_of::<VirtioIommuReqHead>() {
|
2019-09-17 18:40:14 +00:00
|
|
|
return Err(Error::InvalidRequest);
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
let req_head: VirtioIommuReqHead = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(desc.addr())
|
|
|
|
.map_err(Error::GuestMemory)?;
|
2019-09-17 18:40:14 +00:00
|
|
|
let req_offset = size_of::<VirtioIommuReqHead>();
|
2021-10-21 10:41:16 +00:00
|
|
|
let desc_size_left = (desc.len() as usize) - req_offset;
|
|
|
|
let req_addr = if let Some(addr) = desc.addr().checked_add(req_offset as u64) {
|
2019-09-17 18:40:14 +00:00
|
|
|
addr
|
|
|
|
} else {
|
|
|
|
return Err(Error::InvalidRequest);
|
|
|
|
};
|
|
|
|
|
2021-09-06 12:58:48 +00:00
|
|
|
let (msi_iova_start, msi_iova_end) = msi_iova_space;
|
|
|
|
|
2020-01-06 15:45:49 +00:00
|
|
|
// Create the reply
|
|
|
|
let mut reply: Vec<u8> = Vec::new();
|
2022-04-26 09:52:19 +00:00
|
|
|
let mut status = VIRTIO_IOMMU_S_OK;
|
|
|
|
let mut hdr_len = 0;
|
|
|
|
|
|
|
|
let result = (|| {
|
|
|
|
match req_head.type_ {
|
|
|
|
VIRTIO_IOMMU_T_ATTACH => {
|
|
|
|
if desc_size_left != size_of::<VirtioIommuReqAttach>() {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidAttachRequest);
|
|
|
|
}
|
2020-01-06 15:45:49 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
let req: VirtioIommuReqAttach = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(req_addr as GuestAddress)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
debug!("Attach request {:?}", req);
|
|
|
|
|
|
|
|
// Copy the value to use it as a proper reference.
|
|
|
|
let domain_id = req.domain;
|
|
|
|
let endpoint = req.endpoint;
|
|
|
|
let bypass =
|
|
|
|
(req.flags & VIRTIO_IOMMU_ATTACH_F_BYPASS) == VIRTIO_IOMMU_ATTACH_F_BYPASS;
|
|
|
|
|
|
|
|
// Add endpoint associated with specific domain
|
|
|
|
mapping
|
|
|
|
.endpoints
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.insert(endpoint, domain_id);
|
|
|
|
|
|
|
|
// Add new domain with no mapping if the entry didn't exist yet
|
|
|
|
let mut domains = mapping.domains.write().unwrap();
|
|
|
|
let domain = Domain {
|
|
|
|
mappings: BTreeMap::new(),
|
|
|
|
bypass,
|
|
|
|
};
|
|
|
|
domains.entry(domain_id).or_insert_with(|| domain);
|
2022-04-25 14:26:23 +00:00
|
|
|
}
|
2022-04-26 09:52:19 +00:00
|
|
|
VIRTIO_IOMMU_T_DETACH => {
|
|
|
|
if desc_size_left != size_of::<VirtioIommuReqDetach>() {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidDetachRequest);
|
|
|
|
}
|
2022-04-25 14:26:23 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
let req: VirtioIommuReqDetach = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(req_addr as GuestAddress)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
debug!("Detach request {:?}", req);
|
|
|
|
|
|
|
|
// Copy the value to use it as a proper reference.
|
|
|
|
let domain_id = req.domain;
|
|
|
|
let endpoint = req.endpoint;
|
|
|
|
|
|
|
|
// Remove endpoint associated with specific domain
|
|
|
|
mapping.endpoints.write().unwrap().remove(&endpoint);
|
|
|
|
|
|
|
|
// After all endpoints have been successfully detached from a
|
|
|
|
// domain, the domain can be removed. This means we must remove
|
|
|
|
// the mappings associated with this domain.
|
|
|
|
if mapping
|
|
|
|
.endpoints
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, &d)| d == domain_id)
|
|
|
|
.count()
|
|
|
|
== 0
|
|
|
|
{
|
|
|
|
mapping.domains.write().unwrap().remove(&domain_id);
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2022-04-26 09:52:19 +00:00
|
|
|
VIRTIO_IOMMU_T_MAP => {
|
|
|
|
if desc_size_left != size_of::<VirtioIommuReqMap>() {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidMapRequest);
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
let req: VirtioIommuReqMap = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(req_addr as GuestAddress)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
debug!("Map request {:?}", req);
|
|
|
|
|
|
|
|
// Copy the value to use it as a proper reference.
|
|
|
|
let domain_id = req.domain;
|
|
|
|
|
|
|
|
if let Some(domain) = mapping.domains.read().unwrap().get(&domain_id) {
|
|
|
|
if domain.bypass {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidMapRequestBypassDomain);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidMapRequestMissingDomain);
|
2022-04-25 16:04:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
// Find the list of endpoints attached to the given domain.
|
|
|
|
let endpoints: Vec<u32> = mapping
|
|
|
|
.endpoints
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, &d)| d == domain_id)
|
|
|
|
.map(|(&e, _)| e)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Trigger external mapping if necessary.
|
|
|
|
for endpoint in endpoints {
|
|
|
|
if let Some(ext_map) = ext_mapping.get(&endpoint) {
|
|
|
|
let size = req.virt_end - req.virt_start + 1;
|
|
|
|
ext_map
|
|
|
|
.map(req.virt_start, req.phys_start, size)
|
|
|
|
.map_err(Error::ExternalMapping)?;
|
|
|
|
}
|
2022-04-25 14:26:23 +00:00
|
|
|
}
|
2019-10-08 00:12:11 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
// Add new mapping associated with the domain
|
|
|
|
mapping
|
|
|
|
.domains
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get_mut(&domain_id)
|
|
|
|
.unwrap()
|
|
|
|
.mappings
|
|
|
|
.insert(
|
|
|
|
req.virt_start,
|
|
|
|
Mapping {
|
|
|
|
gpa: req.phys_start,
|
|
|
|
size: req.virt_end - req.virt_start + 1,
|
|
|
|
},
|
|
|
|
);
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2022-04-26 09:52:19 +00:00
|
|
|
VIRTIO_IOMMU_T_UNMAP => {
|
|
|
|
if desc_size_left != size_of::<VirtioIommuReqUnmap>() {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidUnmapRequest);
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
let req: VirtioIommuReqUnmap = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(req_addr as GuestAddress)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
debug!("Unmap request {:?}", req);
|
|
|
|
|
|
|
|
// Copy the value to use it as a proper reference.
|
|
|
|
let domain_id = req.domain;
|
|
|
|
let virt_start = req.virt_start;
|
|
|
|
|
|
|
|
if let Some(domain) = mapping.domains.read().unwrap().get(&domain_id) {
|
|
|
|
if domain.bypass {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidUnmapRequestBypassDomain);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidUnmapRequestMissingDomain);
|
2022-04-25 16:04:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
// Find the list of endpoints attached to the given domain.
|
|
|
|
let endpoints: Vec<u32> = mapping
|
|
|
|
.endpoints
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, &d)| d == domain_id)
|
|
|
|
.map(|(&e, _)| e)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Trigger external unmapping if necessary.
|
|
|
|
for endpoint in endpoints {
|
|
|
|
if let Some(ext_map) = ext_mapping.get(&endpoint) {
|
|
|
|
let size = req.virt_end - virt_start + 1;
|
|
|
|
ext_map
|
|
|
|
.unmap(virt_start, size)
|
|
|
|
.map_err(Error::ExternalUnmapping)?;
|
|
|
|
}
|
2022-04-25 14:26:23 +00:00
|
|
|
}
|
2019-10-08 00:12:11 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
// Remove mapping associated with the domain
|
|
|
|
mapping
|
|
|
|
.domains
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.get_mut(&domain_id)
|
|
|
|
.unwrap()
|
|
|
|
.mappings
|
|
|
|
.remove(&virt_start);
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2022-04-26 09:52:19 +00:00
|
|
|
VIRTIO_IOMMU_T_PROBE => {
|
|
|
|
if desc_size_left != size_of::<VirtioIommuReqProbe>() {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidProbeRequest);
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
let req: VirtioIommuReqProbe = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(req_addr as GuestAddress)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
debug!("Probe request {:?}", req);
|
|
|
|
|
|
|
|
let probe_prop = VirtioIommuProbeProperty {
|
|
|
|
type_: VIRTIO_IOMMU_PROBE_T_RESV_MEM,
|
|
|
|
length: size_of::<VirtioIommuProbeResvMem>() as u16,
|
|
|
|
};
|
|
|
|
reply.extend_from_slice(probe_prop.as_slice());
|
|
|
|
|
|
|
|
let resv_mem = VirtioIommuProbeResvMem {
|
|
|
|
subtype: VIRTIO_IOMMU_RESV_MEM_T_MSI,
|
|
|
|
start: msi_iova_start,
|
|
|
|
end: msi_iova_end,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
reply.extend_from_slice(resv_mem.as_slice());
|
|
|
|
|
|
|
|
hdr_len = PROBE_PROP_SIZE;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
status = VIRTIO_IOMMU_S_INVAL;
|
|
|
|
return Err(Error::InvalidRequest);
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2022-04-26 09:52:19 +00:00
|
|
|
Ok(())
|
|
|
|
})();
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
let status_desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
|
|
|
// The status MUST always be writable
|
|
|
|
if !status_desc.is_write_only() {
|
|
|
|
return Err(Error::UnexpectedReadOnlyDescriptor);
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
if status_desc.len() < hdr_len + size_of::<VirtioIommuReqTail>() as u32 {
|
2019-09-17 18:40:14 +00:00
|
|
|
return Err(Error::BufferLengthTooSmall);
|
|
|
|
}
|
|
|
|
|
2020-01-06 15:45:49 +00:00
|
|
|
let tail = VirtioIommuReqTail {
|
2022-04-26 09:52:19 +00:00
|
|
|
status,
|
2020-01-06 15:45:49 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
reply.extend_from_slice(tail.as_slice());
|
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
// Make sure we return the result of the request to the guest before
|
|
|
|
// we return a potential error internally.
|
2021-10-21 10:41:16 +00:00
|
|
|
desc_chain
|
|
|
|
.memory()
|
|
|
|
.write_slice(reply.as_slice(), status_desc.addr())
|
2020-01-06 15:45:49 +00:00
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
|
2022-04-26 09:52:19 +00:00
|
|
|
// Return the error if the result was not Ok().
|
|
|
|
result?;
|
|
|
|
|
2020-01-06 15:45:49 +00:00
|
|
|
Ok((hdr_len as usize) + size_of::<VirtioIommuReqTail>())
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct IommuEpollHandler {
|
2022-07-06 14:08:08 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2022-10-19 21:19:06 +00:00
|
|
|
request_queue: Queue,
|
|
|
|
_event_queue: Queue,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2022-10-19 21:19:06 +00:00
|
|
|
request_queue_evt: EventFd,
|
2022-10-20 15:15:00 +00:00
|
|
|
_event_queue_evt: EventFd,
|
2019-09-17 18:40:14 +00:00
|
|
|
kill_evt: EventFd,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: EventFd,
|
2019-09-17 18:40:14 +00:00
|
|
|
mapping: Arc<IommuMapping>,
|
2022-04-04 14:23:49 +00:00
|
|
|
ext_mapping: Arc<Mutex<BTreeMap<u32, Arc<dyn ExternalDmaMapping>>>>,
|
2021-09-06 12:58:48 +00:00
|
|
|
msi_iova_space: (u64, u64),
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IommuEpollHandler {
|
2022-10-13 19:08:32 +00:00
|
|
|
fn request_queue(&mut self) -> Result<bool, Error> {
|
2022-07-25 12:26:17 +00:00
|
|
|
let mut used_descs = false;
|
2022-10-19 21:19:06 +00:00
|
|
|
while let Some(mut desc_chain) = self.request_queue.pop_descriptor_chain(self.mem.memory())
|
|
|
|
{
|
2022-10-13 19:08:32 +00:00
|
|
|
let len = Request::parse(
|
2021-10-21 10:41:16 +00:00
|
|
|
&mut desc_chain,
|
2019-10-08 00:12:11 +00:00
|
|
|
&self.mapping,
|
2022-04-04 14:23:49 +00:00
|
|
|
&self.ext_mapping.lock().unwrap(),
|
2021-09-06 12:58:48 +00:00
|
|
|
self.msi_iova_space,
|
2022-10-13 19:08:32 +00:00
|
|
|
)?;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2022-10-19 21:19:06 +00:00
|
|
|
self.request_queue
|
2022-10-13 19:08:32 +00:00
|
|
|
.add_used(desc_chain.memory(), desc_chain.head_index(), len as u32)
|
|
|
|
.map_err(Error::QueueAddUsed)?;
|
|
|
|
|
2022-07-25 12:26:17 +00:00
|
|
|
used_descs = true;
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2022-07-25 12:26:17 +00:00
|
|
|
|
2022-10-13 19:08:32 +00:00
|
|
|
Ok(used_descs)
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 14:30:42 +00:00
|
|
|
fn signal_used_queue(&self, queue_index: u16) -> result::Result<(), DeviceError> {
|
2020-01-13 17:52:19 +00:00
|
|
|
self.interrupt_cb
|
2022-01-24 14:30:42 +00:00
|
|
|
.trigger(VirtioInterruptType::Queue(queue_index))
|
2020-01-13 17:52:19 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:05:06 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
paused: Arc<AtomicBool>,
|
|
|
|
paused_sync: Arc<Barrier>,
|
|
|
|
) -> result::Result<(), EpollHelperError> {
|
2020-08-04 11:16:44 +00:00
|
|
|
let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?;
|
2022-10-19 21:19:06 +00:00
|
|
|
helper.add_event(self.request_queue_evt.as_raw_fd(), REQUEST_Q_EVENT)?;
|
2020-08-11 14:05:06 +00:00
|
|
|
helper.run(paused, paused_sync, self)?;
|
2020-06-22 14:00:02 +00:00
|
|
|
|
2020-08-04 11:16:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2020-08-04 11:16:44 +00:00
|
|
|
impl EpollHelperHandler for IommuEpollHandler {
|
2022-08-12 01:30:13 +00:00
|
|
|
fn handle_event(
|
|
|
|
&mut self,
|
|
|
|
_helper: &mut EpollHelper,
|
|
|
|
event: &epoll::Event,
|
|
|
|
) -> result::Result<(), EpollHelperError> {
|
2020-08-11 17:12:02 +00:00
|
|
|
let ev_type = event.data as u16;
|
|
|
|
match ev_type {
|
2020-08-04 11:16:44 +00:00
|
|
|
REQUEST_Q_EVENT => {
|
2022-10-19 21:19:06 +00:00
|
|
|
self.request_queue_evt.read().map_err(|e| {
|
2022-08-12 01:30:13 +00:00
|
|
|
EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e))
|
|
|
|
})?;
|
|
|
|
|
2022-10-13 19:08:32 +00:00
|
|
|
let needs_notification = self.request_queue().map_err(|e| {
|
|
|
|
EpollHelperError::HandleEvent(anyhow!(
|
|
|
|
"Failed to process request queue : {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})?;
|
|
|
|
if needs_notification {
|
2022-08-12 01:30:13 +00:00
|
|
|
self.signal_used_queue(0).map_err(|e| {
|
|
|
|
EpollHelperError::HandleEvent(anyhow!(
|
|
|
|
"Failed to signal used queue: {:?}",
|
|
|
|
e
|
|
|
|
))
|
|
|
|
})?;
|
2020-08-04 11:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2022-08-12 01:30:13 +00:00
|
|
|
return Err(EpollHelperError::HandleEvent(anyhow!(
|
|
|
|
"Unexpected event: {}",
|
|
|
|
ev_type
|
|
|
|
)));
|
2020-08-04 11:16:44 +00:00
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2022-08-12 01:30:13 +00:00
|
|
|
Ok(())
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Versionize)]
|
2019-09-17 18:40:14 +00:00
|
|
|
struct Mapping {
|
|
|
|
gpa: u64,
|
|
|
|
size: u64,
|
|
|
|
}
|
|
|
|
|
2022-04-25 16:04:02 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-04-25 15:58:28 +00:00
|
|
|
struct Domain {
|
|
|
|
mappings: BTreeMap<u64, Mapping>,
|
2022-04-25 16:04:02 +00:00
|
|
|
bypass: bool,
|
2022-04-25 15:58:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
#[derive(Debug)]
|
2019-09-17 18:40:14 +00:00
|
|
|
pub struct IommuMapping {
|
|
|
|
// Domain related to an endpoint.
|
|
|
|
endpoints: Arc<RwLock<BTreeMap<u32, u32>>>,
|
2022-04-25 15:58:28 +00:00
|
|
|
// Information related to each domain.
|
|
|
|
domains: Arc<RwLock<BTreeMap<u32, Domain>>>,
|
2022-04-25 15:18:22 +00:00
|
|
|
// Global flag indicating if endpoints that are not attached to any domain
|
|
|
|
// are in bypass mode.
|
|
|
|
bypass: AtomicBool,
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DmaRemapping for IommuMapping {
|
2022-04-04 11:35:24 +00:00
|
|
|
fn translate_gva(&self, id: u32, addr: u64) -> std::result::Result<u64, std::io::Error> {
|
2022-04-04 12:11:29 +00:00
|
|
|
debug!("Translate GVA addr 0x{:x}", addr);
|
2022-04-25 15:58:28 +00:00
|
|
|
if let Some(domain_id) = self.endpoints.read().unwrap().get(&id) {
|
|
|
|
if let Some(domain) = self.domains.read().unwrap().get(domain_id) {
|
2022-04-25 16:04:02 +00:00
|
|
|
// Directly return identity mapping in case the domain is in
|
|
|
|
// bypass mode.
|
|
|
|
if domain.bypass {
|
|
|
|
return Ok(addr);
|
|
|
|
}
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
let range_start = if VIRTIO_IOMMU_PAGE_SIZE_MASK > addr {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
addr - VIRTIO_IOMMU_PAGE_SIZE_MASK
|
|
|
|
};
|
2022-04-25 15:58:28 +00:00
|
|
|
for (&key, &value) in domain
|
|
|
|
.mappings
|
|
|
|
.range((Included(&range_start), Included(&addr)))
|
|
|
|
{
|
2019-09-17 18:40:14 +00:00
|
|
|
if addr >= key && addr < key + value.size {
|
|
|
|
let new_addr = addr - key + value.gpa;
|
2022-04-04 12:11:29 +00:00
|
|
|
debug!("Into GPA addr 0x{:x}", new_addr);
|
|
|
|
return Ok(new_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 15:18:22 +00:00
|
|
|
} else if self.bypass.load(Ordering::Acquire) {
|
|
|
|
return Ok(addr);
|
2022-04-04 12:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:43:36 +00:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("failed to translate GVA addr 0x{:x}", addr),
|
|
|
|
))
|
2022-04-04 12:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn translate_gpa(&self, id: u32, addr: u64) -> std::result::Result<u64, std::io::Error> {
|
|
|
|
debug!("Translate GPA addr 0x{:x}", addr);
|
2022-04-25 15:58:28 +00:00
|
|
|
if let Some(domain_id) = self.endpoints.read().unwrap().get(&id) {
|
|
|
|
if let Some(domain) = self.domains.read().unwrap().get(domain_id) {
|
2022-04-25 16:04:02 +00:00
|
|
|
// Directly return identity mapping in case the domain is in
|
|
|
|
// bypass mode.
|
|
|
|
if domain.bypass {
|
|
|
|
return Ok(addr);
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:58:28 +00:00
|
|
|
for (&key, &value) in domain.mappings.iter() {
|
2022-04-04 12:11:29 +00:00
|
|
|
if addr >= value.gpa && addr < value.gpa + value.size {
|
|
|
|
let new_addr = addr - value.gpa + key;
|
|
|
|
debug!("Into GVA addr 0x{:x}", new_addr);
|
2019-09-17 18:40:14 +00:00
|
|
|
return Ok(new_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 15:18:22 +00:00
|
|
|
} else if self.bypass.load(Ordering::Acquire) {
|
|
|
|
return Ok(addr);
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:43:36 +00:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("failed to translate GPA addr 0x{:x}", addr),
|
|
|
|
))
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AccessPlatformMapping {
|
|
|
|
id: u32,
|
|
|
|
mapping: Arc<IommuMapping>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccessPlatformMapping {
|
|
|
|
pub fn new(id: u32, mapping: Arc<IommuMapping>) -> Self {
|
|
|
|
AccessPlatformMapping { id, mapping }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AccessPlatform for AccessPlatformMapping {
|
2022-04-04 11:35:24 +00:00
|
|
|
fn translate_gva(&self, base: u64, _size: u64) -> std::result::Result<u64, std::io::Error> {
|
|
|
|
self.mapping.translate_gva(self.id, base)
|
2021-10-21 10:41:16 +00:00
|
|
|
}
|
2022-04-04 12:11:29 +00:00
|
|
|
fn translate_gpa(&self, base: u64, _size: u64) -> std::result::Result<u64, std::io::Error> {
|
|
|
|
self.mapping.translate_gpa(self.id, base)
|
|
|
|
}
|
2021-10-21 10:41:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
pub struct Iommu {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2020-04-27 12:51:15 +00:00
|
|
|
id: String,
|
2019-09-17 18:40:14 +00:00
|
|
|
config: VirtioIommuConfig,
|
|
|
|
mapping: Arc<IommuMapping>,
|
2022-04-04 14:23:49 +00:00
|
|
|
ext_mapping: Arc<Mutex<BTreeMap<u32, Arc<dyn ExternalDmaMapping>>>>,
|
2020-08-14 21:37:01 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2021-09-06 12:58:48 +00:00
|
|
|
msi_iova_space: (u64, u64),
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 16:04:02 +00:00
|
|
|
type EndpointsState = Vec<(u32, u32)>;
|
|
|
|
type DomainsState = Vec<(u32, (Vec<(u64, Mapping)>, bool))>;
|
|
|
|
|
2021-05-11 14:02:43 +00:00
|
|
|
#[derive(Versionize)]
|
2022-10-18 15:14:43 +00:00
|
|
|
pub struct IommuState {
|
2020-05-18 12:27:52 +00:00
|
|
|
avail_features: u64,
|
|
|
|
acked_features: u64,
|
2022-04-25 16:04:02 +00:00
|
|
|
endpoints: EndpointsState,
|
|
|
|
domains: DomainsState,
|
2020-05-18 12:27:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 13:34:31 +00:00
|
|
|
impl VersionMapped for IommuState {}
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
impl Iommu {
|
2021-09-07 15:10:48 +00:00
|
|
|
pub fn new(
|
|
|
|
id: String,
|
|
|
|
seccomp_action: SeccompAction,
|
|
|
|
exit_evt: EventFd,
|
2021-09-06 12:58:48 +00:00
|
|
|
msi_iova_space: (u64, u64),
|
2022-10-18 15:14:43 +00:00
|
|
|
state: Option<IommuState>,
|
2021-09-07 15:10:48 +00:00
|
|
|
) -> io::Result<(Self, Arc<IommuMapping>)> {
|
2022-10-18 15:14:43 +00:00
|
|
|
let (avail_features, acked_features, endpoints, domains) = if let Some(state) = state {
|
|
|
|
info!("Restoring virtio-iommu {}", id);
|
|
|
|
(
|
|
|
|
state.avail_features,
|
|
|
|
state.acked_features,
|
|
|
|
state.endpoints.into_iter().collect(),
|
|
|
|
state
|
|
|
|
.domains
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
(
|
|
|
|
k,
|
|
|
|
Domain {
|
|
|
|
mappings: v.0.into_iter().collect(),
|
|
|
|
bypass: v.1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
let avail_features = 1u64 << VIRTIO_F_VERSION_1
|
|
|
|
| 1u64 << VIRTIO_IOMMU_F_MAP_UNMAP
|
|
|
|
| 1u64 << VIRTIO_IOMMU_F_PROBE
|
|
|
|
| 1u64 << VIRTIO_IOMMU_F_BYPASS_CONFIG;
|
|
|
|
|
|
|
|
(avail_features, 0, BTreeMap::new(), BTreeMap::new())
|
|
|
|
};
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
let config = VirtioIommuConfig {
|
|
|
|
page_size_mask: VIRTIO_IOMMU_PAGE_SIZE_MASK,
|
2020-01-06 15:45:49 +00:00
|
|
|
probe_size: PROBE_PROP_SIZE,
|
2019-09-17 18:40:14 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mapping = Arc::new(IommuMapping {
|
2022-10-18 15:14:43 +00:00
|
|
|
endpoints: Arc::new(RwLock::new(endpoints)),
|
|
|
|
domains: Arc::new(RwLock::new(domains)),
|
2022-04-25 15:18:22 +00:00
|
|
|
bypass: AtomicBool::new(true),
|
2019-09-17 18:40:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
Iommu {
|
2020-04-27 12:51:15 +00:00
|
|
|
id,
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2021-03-25 16:54:09 +00:00
|
|
|
device_type: VirtioDeviceType::Iommu as u32,
|
2020-09-04 08:37:37 +00:00
|
|
|
queue_sizes: QUEUE_SIZES.to_vec(),
|
2022-10-18 15:14:43 +00:00
|
|
|
avail_features,
|
|
|
|
acked_features,
|
2020-09-04 08:37:37 +00:00
|
|
|
paused_sync: Some(Arc::new(Barrier::new(2))),
|
2022-10-19 21:03:29 +00:00
|
|
|
min_queues: NUM_QUEUES as u16,
|
2020-09-03 15:56:32 +00:00
|
|
|
..Default::default()
|
2020-09-03 09:37:36 +00:00
|
|
|
},
|
2019-09-17 18:40:14 +00:00
|
|
|
config,
|
|
|
|
mapping: mapping.clone(),
|
2022-04-04 14:23:49 +00:00
|
|
|
ext_mapping: Arc::new(Mutex::new(BTreeMap::new())),
|
2020-08-14 21:37:01 +00:00
|
|
|
seccomp_action,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt,
|
2021-09-06 12:58:48 +00:00
|
|
|
msi_iova_space,
|
2019-09-17 18:40:14 +00:00
|
|
|
},
|
|
|
|
mapping,
|
|
|
|
))
|
|
|
|
}
|
2019-10-08 00:12:11 +00:00
|
|
|
|
2020-05-18 12:27:52 +00:00
|
|
|
fn state(&self) -> IommuState {
|
|
|
|
IommuState {
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
2021-04-23 09:55:05 +00:00
|
|
|
endpoints: self
|
|
|
|
.mapping
|
|
|
|
.endpoints
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
2022-04-25 15:58:28 +00:00
|
|
|
domains: self
|
2021-04-23 09:55:05 +00:00
|
|
|
.mapping
|
2022-04-25 15:58:28 +00:00
|
|
|
.domains
|
2021-04-23 09:55:05 +00:00
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
2022-04-25 16:04:02 +00:00
|
|
|
.map(|(k, v)| (k, (v.mappings.into_iter().collect(), v.bypass)))
|
2021-04-23 09:55:05 +00:00
|
|
|
.collect(),
|
2020-05-18 12:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:18:22 +00:00
|
|
|
fn update_bypass(&mut self) {
|
|
|
|
// Use bypass from config if VIRTIO_IOMMU_F_BYPASS_CONFIG has been negotiated
|
|
|
|
if !self
|
|
|
|
.common
|
|
|
|
.feature_acked(VIRTIO_IOMMU_F_BYPASS_CONFIG.into())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bypass = self.config.bypass == 1;
|
|
|
|
info!("Updating bypass mode to {}", bypass);
|
|
|
|
self.mapping.bypass.store(bypass, Ordering::Release);
|
|
|
|
}
|
|
|
|
|
2019-10-08 00:12:11 +00:00
|
|
|
pub fn add_external_mapping(&mut self, device_id: u32, mapping: Arc<dyn ExternalDmaMapping>) {
|
2022-04-04 14:23:49 +00:00
|
|
|
self.ext_mapping.lock().unwrap().insert(device_id, mapping);
|
2019-10-08 00:12:11 +00:00
|
|
|
}
|
2022-10-18 22:13:12 +00:00
|
|
|
|
|
|
|
#[cfg(fuzzing)]
|
|
|
|
pub fn wait_for_epoll_threads(&mut self) {
|
|
|
|
self.common.wait_for_epoll_threads();
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Iommu {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2019-09-17 18:40:14 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Iommu {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn features(&self) -> u64 {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn ack_features(&mut self, value: u64) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.ack_features(value)
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:34:51 +00:00
|
|
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
2021-06-14 13:38:24 +00:00
|
|
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
2020-07-16 09:34:51 +00:00
|
|
|
|
2022-04-25 15:18:22 +00:00
|
|
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
|
|
|
// The "bypass" field is the only mutable field
|
|
|
|
let bypass_offset =
|
|
|
|
(&self.config.bypass as *const _ as u64) - (&self.config as *const _ as u64);
|
|
|
|
if offset != bypass_offset || data.len() != std::mem::size_of_val(&self.config.bypass) {
|
|
|
|
error!(
|
|
|
|
"Attempt to write to read-only field: offset {:x} length {}",
|
|
|
|
offset,
|
|
|
|
data.len()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.config.bypass = data[0];
|
|
|
|
|
|
|
|
self.update_bypass();
|
|
|
|
}
|
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2022-07-06 14:08:08 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2022-10-19 21:19:06 +00:00
|
|
|
mut queues: Vec<(usize, Queue, EventFd)>,
|
2019-09-17 18:40:14 +00:00
|
|
|
) -> ActivateResult {
|
2022-07-20 14:45:49 +00:00
|
|
|
self.common.activate(&queues, &interrupt_cb)?;
|
2021-06-02 18:08:06 +00:00
|
|
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
2022-07-20 14:45:49 +00:00
|
|
|
|
2022-10-19 21:19:06 +00:00
|
|
|
let (_, request_queue, request_queue_evt) = queues.remove(0);
|
2022-10-20 15:15:00 +00:00
|
|
|
let (_, _event_queue, _event_queue_evt) = queues.remove(0);
|
2022-07-20 14:45:49 +00:00
|
|
|
|
2019-09-17 18:40:14 +00:00
|
|
|
let mut handler = IommuEpollHandler {
|
2022-07-06 14:08:08 +00:00
|
|
|
mem,
|
2022-10-19 21:19:06 +00:00
|
|
|
request_queue,
|
|
|
|
_event_queue,
|
2019-09-17 18:40:14 +00:00
|
|
|
interrupt_cb,
|
2022-10-19 21:19:06 +00:00
|
|
|
request_queue_evt,
|
2022-10-20 15:15:00 +00:00
|
|
|
_event_queue_evt,
|
2019-09-17 18:40:14 +00:00
|
|
|
kill_evt,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt,
|
2019-09-17 18:40:14 +00:00
|
|
|
mapping: self.mapping.clone(),
|
2019-10-08 00:12:11 +00:00
|
|
|
ext_mapping: self.ext_mapping.clone(),
|
2021-09-06 12:58:48 +00:00
|
|
|
msi_iova_space: self.msi_iova_space,
|
2019-09-17 18:40:14 +00:00
|
|
|
};
|
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
let paused = self.common.paused.clone();
|
|
|
|
let paused_sync = self.common.paused_sync.clone();
|
2020-01-27 12:56:05 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
2021-09-03 10:43:30 +00:00
|
|
|
spawn_virtio_thread(
|
|
|
|
&self.id,
|
|
|
|
&self.seccomp_action,
|
|
|
|
Thread::VirtioIommu,
|
|
|
|
&mut epoll_threads,
|
2021-09-07 15:10:48 +00:00
|
|
|
&self.exit_evt,
|
2022-08-12 00:16:27 +00:00
|
|
|
move || handler.run(paused, paused_sync.unwrap()),
|
2021-09-03 10:43:30 +00:00
|
|
|
)?;
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.epoll_threads = Some(epoll_threads);
|
2020-01-27 12:56:05 +00:00
|
|
|
|
2021-02-18 15:10:51 +00:00
|
|
|
event!("virtio-device", "activated", "id", &self.id);
|
2019-09-17 18:40:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:38:08 +00:00
|
|
|
fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
|
2021-02-18 15:10:51 +00:00
|
|
|
let result = self.common.reset();
|
|
|
|
event!("virtio-device", "reset", "id", &self.id);
|
|
|
|
result
|
2020-09-04 08:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for Iommu {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.pause()
|
|
|
|
}
|
2019-09-17 18:40:14 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.resume()
|
2019-09-17 18:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
2020-04-27 12:51:15 +00:00
|
|
|
impl Snapshottable for Iommu {
|
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
2020-05-18 12:27:52 +00:00
|
|
|
|
2020-08-21 12:31:58 +00:00
|
|
|
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
2021-05-06 13:34:31 +00:00
|
|
|
Snapshot::new_from_versioned_state(&self.id, &self.state())
|
2020-05-18 12:27:52 +00:00
|
|
|
}
|
2020-04-27 12:51:15 +00:00
|
|
|
}
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Iommu {}
|
2019-11-19 00:42:31 +00:00
|
|
|
impl Migratable for Iommu {}
|