1 // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 // 3 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE-BSD-3-Clause file. 6 // 7 // Copyright © 2019 Intel Corporation 8 // 9 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause 10 11 //! Implements virtio queues 12 13 use std::fmt::{self, Debug}; 14 use std::sync::Arc; 15 16 use vm_memory::GuestAddress; 17 18 pub mod queue; 19 pub use queue::*; 20 21 pub const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff; 22 23 // Types taken from linux/virtio_ids.h 24 #[derive(Copy, Clone, Debug)] 25 #[allow(dead_code)] 26 #[allow(non_camel_case_types)] 27 #[repr(C)] 28 pub enum VirtioDeviceType { 29 Net = 1, 30 Block = 2, 31 Console = 3, 32 Rng = 4, 33 Balloon = 5, 34 Fs9P = 9, 35 Gpu = 16, 36 Input = 18, 37 Vsock = 19, 38 Iommu = 23, 39 Mem = 24, 40 Fs = 26, 41 Pmem = 27, 42 Watchdog = 35, // Temporary until official number allocated 43 Unknown = 0xFF, 44 } 45 46 impl From<u32> for VirtioDeviceType { 47 fn from(t: u32) -> Self { 48 match t { 49 1 => VirtioDeviceType::Net, 50 2 => VirtioDeviceType::Block, 51 3 => VirtioDeviceType::Console, 52 4 => VirtioDeviceType::Rng, 53 5 => VirtioDeviceType::Balloon, 54 9 => VirtioDeviceType::Fs9P, 55 16 => VirtioDeviceType::Gpu, 56 18 => VirtioDeviceType::Input, 57 19 => VirtioDeviceType::Vsock, 58 23 => VirtioDeviceType::Iommu, 59 24 => VirtioDeviceType::Mem, 60 26 => VirtioDeviceType::Fs, 61 27 => VirtioDeviceType::Pmem, 62 35 => VirtioDeviceType::Watchdog, 63 _ => VirtioDeviceType::Unknown, 64 } 65 } 66 } 67 68 // In order to use the `{}` marker, the trait `fmt::Display` must be implemented 69 // manually for the type VirtioDeviceType. 70 impl fmt::Display for VirtioDeviceType { 71 // This trait requires `fmt` with this exact signature. 72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 73 let output = match *self { 74 VirtioDeviceType::Net => "net", 75 VirtioDeviceType::Block => "block", 76 VirtioDeviceType::Console => "console", 77 VirtioDeviceType::Rng => "rng", 78 VirtioDeviceType::Balloon => "balloon", 79 VirtioDeviceType::Gpu => "gpu", 80 VirtioDeviceType::Fs9P => "9p", 81 VirtioDeviceType::Input => "input", 82 VirtioDeviceType::Vsock => "vsock", 83 VirtioDeviceType::Iommu => "iommu", 84 VirtioDeviceType::Mem => "mem", 85 VirtioDeviceType::Fs => "fs", 86 VirtioDeviceType::Pmem => "pmem", 87 VirtioDeviceType::Watchdog => "watchdog", 88 VirtioDeviceType::Unknown => "UNKNOWN", 89 }; 90 write!(f, "{}", output) 91 } 92 } 93 94 /// Trait for devices with access to data in memory being limited and/or 95 /// translated. 96 pub trait AccessPlatform: Send + Sync + Debug { 97 /// Provide a way to translate GVA address ranges into GPAs. 98 fn translate_gva(&self, base: u64, size: u64) -> std::result::Result<u64, std::io::Error>; 99 /// Provide a way to translate GPA address ranges into GVAs. 100 fn translate_gpa(&self, base: u64, size: u64) -> std::result::Result<u64, std::io::Error>; 101 } 102 103 pub trait Translatable { 104 fn translate_gva(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self; 105 fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self; 106 } 107 108 impl Translatable for GuestAddress { 109 fn translate_gva(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self { 110 if let Some(access_platform) = access_platform { 111 GuestAddress(access_platform.translate_gva(self.0, len as u64).unwrap()) 112 } else { 113 *self 114 } 115 } 116 fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self { 117 if let Some(access_platform) = access_platform { 118 GuestAddress(access_platform.translate_gpa(self.0, len as u64).unwrap()) 119 } else { 120 *self 121 } 122 } 123 } 124