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 use virtio_queue::{Queue, QueueT}; 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 GuestAddress(self.0.translate_gva(access_platform, len)) 111 } 112 fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self { 113 GuestAddress(self.0.translate_gpa(access_platform, len)) 114 } 115 } 116 117 impl Translatable for u64 { 118 fn translate_gva(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self { 119 if let Some(access_platform) = access_platform { 120 access_platform.translate_gva(*self, len as u64).unwrap() 121 } else { 122 *self 123 } 124 } 125 fn translate_gpa(&self, access_platform: Option<&Arc<dyn AccessPlatform>>, len: usize) -> Self { 126 if let Some(access_platform) = access_platform { 127 access_platform.translate_gpa(*self, len as u64).unwrap() 128 } else { 129 *self 130 } 131 } 132 } 133 134 /// Helper for cloning a Queue since QueueState doesn't derive Clone 135 pub fn clone_queue(queue: &Queue) -> Queue { 136 let mut q = Queue::new(queue.max_size()).unwrap(); 137 138 q.set_next_avail(queue.next_avail()); 139 q.set_next_used(queue.next_used()); 140 q.set_event_idx(queue.event_idx_enabled()); 141 q.set_size(queue.size()); 142 q.set_ready(queue.ready()); 143 q.try_set_desc_table_address(GuestAddress(queue.desc_table())) 144 .unwrap(); 145 q.try_set_avail_ring_address(GuestAddress(queue.avail_ring())) 146 .unwrap(); 147 q.try_set_used_ring_address(GuestAddress(queue.used_ring())) 148 .unwrap(); 149 150 q 151 } 152