2019-04-18 17:24:06 +00:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
2019-05-08 10:22:53 +00:00
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
|
|
//
|
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
2019-04-18 17:24:06 +00:00
|
|
|
|
2020-07-02 12:25:19 +00:00
|
|
|
//! Implements virtio queues
|
2019-12-31 10:49:11 +00:00
|
|
|
|
2022-01-26 16:33:36 +00:00
|
|
|
use std::fmt::{self, Debug};
|
2022-01-26 22:44:31 +00:00
|
|
|
use std::sync::Arc;
|
2022-05-12 13:26:44 +00:00
|
|
|
use virtio_queue::Queue;
|
|
|
|
use vm_memory::{bitmap::AtomicBitmap, GuestAddress, GuestMemoryAtomic};
|
2022-01-26 22:44:31 +00:00
|
|
|
|
2022-05-12 13:26:44 +00:00
|
|
|
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
|
2019-04-18 17:24:06 +00:00
|
|
|
|
2020-02-21 10:34:51 +00:00
|
|
|
pub mod queue;
|
2020-07-02 12:25:19 +00:00
|
|
|
pub use queue::*;
|
2019-04-18 17:24:06 +00:00
|
|
|
|
2021-03-16 18:19:28 +00:00
|
|
|
pub const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff;
|
|
|
|
|
2019-04-18 17:24:06 +00:00
|
|
|
// Types taken from linux/virtio_ids.h
|
2020-04-27 14:55:52 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2019-04-18 17:24:06 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[repr(C)]
|
2020-04-27 14:55:52 +00:00
|
|
|
pub enum VirtioDeviceType {
|
2021-03-25 16:54:09 +00:00
|
|
|
Net = 1,
|
|
|
|
Block = 2,
|
|
|
|
Console = 3,
|
|
|
|
Rng = 4,
|
|
|
|
Balloon = 5,
|
|
|
|
Fs9P = 9,
|
|
|
|
Gpu = 16,
|
|
|
|
Input = 18,
|
|
|
|
Vsock = 19,
|
|
|
|
Iommu = 23,
|
|
|
|
Mem = 24,
|
|
|
|
Fs = 26,
|
|
|
|
Pmem = 27,
|
|
|
|
Watchdog = 35, // Temporary until official number allocated
|
|
|
|
Unknown = 0xFF,
|
2019-07-02 15:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<u32> for VirtioDeviceType {
|
|
|
|
fn from(t: u32) -> Self {
|
|
|
|
match t {
|
2021-03-25 16:54:09 +00:00
|
|
|
1 => VirtioDeviceType::Net,
|
|
|
|
2 => VirtioDeviceType::Block,
|
|
|
|
3 => VirtioDeviceType::Console,
|
|
|
|
4 => VirtioDeviceType::Rng,
|
|
|
|
5 => VirtioDeviceType::Balloon,
|
|
|
|
9 => VirtioDeviceType::Fs9P,
|
|
|
|
16 => VirtioDeviceType::Gpu,
|
|
|
|
18 => VirtioDeviceType::Input,
|
|
|
|
19 => VirtioDeviceType::Vsock,
|
|
|
|
23 => VirtioDeviceType::Iommu,
|
|
|
|
24 => VirtioDeviceType::Mem,
|
|
|
|
26 => VirtioDeviceType::Fs,
|
|
|
|
27 => VirtioDeviceType::Pmem,
|
|
|
|
35 => VirtioDeviceType::Watchdog,
|
|
|
|
_ => VirtioDeviceType::Unknown,
|
2019-07-02 15:11:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 17:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In order to use the `{}` marker, the trait `fmt::Display` must be implemented
|
|
|
|
// manually for the type VirtioDeviceType.
|
|
|
|
impl fmt::Display for VirtioDeviceType {
|
|
|
|
// This trait requires `fmt` with this exact signature.
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let output = match *self {
|
2021-03-25 16:54:09 +00:00
|
|
|
VirtioDeviceType::Net => "net",
|
|
|
|
VirtioDeviceType::Block => "block",
|
|
|
|
VirtioDeviceType::Console => "console",
|
|
|
|
VirtioDeviceType::Rng => "rng",
|
|
|
|
VirtioDeviceType::Balloon => "balloon",
|
|
|
|
VirtioDeviceType::Gpu => "gpu",
|
|
|
|
VirtioDeviceType::Fs9P => "9p",
|
|
|
|
VirtioDeviceType::Input => "input",
|
|
|
|
VirtioDeviceType::Vsock => "vsock",
|
|
|
|
VirtioDeviceType::Iommu => "iommu",
|
|
|
|
VirtioDeviceType::Mem => "mem",
|
|
|
|
VirtioDeviceType::Fs => "fs",
|
|
|
|
VirtioDeviceType::Pmem => "pmem",
|
|
|
|
VirtioDeviceType::Watchdog => "watchdog",
|
|
|
|
VirtioDeviceType::Unknown => "UNKNOWN",
|
2019-04-18 17:24:06 +00:00
|
|
|
};
|
|
|
|
write!(f, "{}", output)
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 16:33:36 +00:00
|
|
|
|
|
|
|
/// Trait for devices with access to data in memory being limited and/or
|
|
|
|
/// translated.
|
|
|
|
pub trait AccessPlatform: Send + Sync + Debug {
|
2022-04-04 11:35:24 +00:00
|
|
|
/// Provide a way to translate GVA address ranges into GPAs.
|
|
|
|
fn translate_gva(&self, base: u64, size: u64) -> std::result::Result<u64, std::io::Error>;
|
2022-04-04 12:11:29 +00:00
|
|
|
/// Provide a way to translate GPA address ranges into GVAs.
|
|
|
|
fn translate_gpa(&self, base: u64, size: u64) -> std::result::Result<u64, std::io::Error>;
|
2022-01-26 16:33:36 +00:00
|
|
|
}
|
2022-01-26 22:44:31 +00:00
|
|
|
|
|
|
|
pub trait Translatable {
|
2022-04-04 11:35:24 +00:00
|
|
|
fn translate_gva(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self;
|
2022-04-04 12:11:29 +00:00
|
|
|
fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self;
|
2022-01-26 22:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Translatable for GuestAddress {
|
2022-04-04 11:35:24 +00:00
|
|
|
fn translate_gva(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self {
|
2022-01-26 22:44:31 +00:00
|
|
|
if let Some(access_platform) = access_platform {
|
2022-04-04 11:35:24 +00:00
|
|
|
GuestAddress(access_platform.translate_gva(self.0, len as u64).unwrap())
|
2022-01-26 22:44:31 +00:00
|
|
|
} else {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 12:11:29 +00:00
|
|
|
fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self {
|
|
|
|
if let Some(access_platform) = access_platform {
|
|
|
|
GuestAddress(access_platform.translate_gpa(self.0, len as u64).unwrap())
|
|
|
|
} else {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 22:44:31 +00:00
|
|
|
}
|
2022-05-12 13:26:44 +00:00
|
|
|
|
|
|
|
/// Helper for cloning a Queue since QueueState doesn't derive Clone
|
|
|
|
pub fn clone_queue(
|
|
|
|
queue: &Queue<GuestMemoryAtomic<GuestMemoryMmap>>,
|
|
|
|
) -> Queue<GuestMemoryAtomic<GuestMemoryMmap>> {
|
|
|
|
Queue::<GuestMemoryAtomic<GuestMemoryMmap>, virtio_queue::QueueState> {
|
|
|
|
mem: queue.mem.clone(),
|
|
|
|
state: virtio_queue::QueueState {
|
|
|
|
max_size: queue.state.max_size,
|
|
|
|
next_avail: queue.state.next_avail,
|
|
|
|
next_used: queue.state.next_used,
|
|
|
|
event_idx_enabled: queue.state.event_idx_enabled,
|
2022-05-11 02:08:42 +00:00
|
|
|
num_added: queue.state.num_added,
|
2022-05-12 13:26:44 +00:00
|
|
|
size: queue.state.size,
|
|
|
|
ready: queue.state.ready,
|
|
|
|
desc_table: queue.state.desc_table,
|
|
|
|
avail_ring: queue.state.avail_ring,
|
|
|
|
used_ring: queue.state.used_ring,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|