Skip to main content

msb_krun_devices/virtio/
mod.rs

1// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
5// Use of this source code is governed by a BSD-style license that can be
6// found in the THIRD-PARTY file.
7
8//! Implements virtio devices, queues, and transport mechanisms.
9use std;
10use std::any::Any;
11use std::io::Error as IOError;
12
13#[cfg(not(feature = "tee"))]
14pub mod balloon;
15#[allow(dead_code)]
16#[allow(non_camel_case_types)]
17pub mod bindings;
18#[cfg(feature = "blk")]
19pub mod block;
20pub mod console;
21pub mod descriptor_utils;
22pub mod device;
23pub mod file_traits;
24#[cfg(not(any(feature = "tee", feature = "aws-nitro")))]
25pub mod fs;
26#[cfg(feature = "gpu")]
27pub mod gpu;
28#[cfg(feature = "input")]
29pub mod input;
30pub mod linux_errno;
31mod mmio;
32#[cfg(feature = "net")]
33pub mod net;
34mod queue;
35#[cfg(not(feature = "tee"))]
36pub mod rng;
37#[cfg(feature = "snd")]
38pub mod snd;
39pub mod vsock;
40
41#[cfg(not(feature = "tee"))]
42pub use self::balloon::*;
43#[cfg(feature = "blk")]
44pub use self::block::{Block, CacheType};
45pub use self::console::*;
46pub use self::device::*;
47#[cfg(not(any(feature = "tee", feature = "aws-nitro")))]
48pub use self::fs::*;
49#[cfg(feature = "gpu")]
50pub use self::gpu::*;
51pub use self::mmio::*;
52#[cfg(feature = "net")]
53pub use self::net::Net;
54pub use self::queue::{Descriptor, DescriptorChain, Queue};
55#[cfg(not(feature = "tee"))]
56pub use self::rng::*;
57#[cfg(feature = "snd")]
58pub use self::snd::Snd;
59pub use self::vsock::*;
60
61/// When the driver initializes the device, it lets the device know about the
62/// completed stages using the Device Status Field.
63///
64/// These following consts are defined in the order in which the bits would
65/// typically be set by the driver. INIT -> ACKNOWLEDGE -> DRIVER and so on.
66///
67/// This module is a 1:1 mapping for the Device Status Field in the virtio 1.0
68/// specification, section 2.1.
69mod device_status {
70    pub const INIT: u32 = 0;
71    pub const ACKNOWLEDGE: u32 = 1;
72    pub const DRIVER: u32 = 2;
73    pub const FAILED: u32 = 128;
74    pub const FEATURES_OK: u32 = 8;
75    pub const DRIVER_OK: u32 = 4;
76}
77
78/// Types taken from linux/virtio_ids.h.
79/// Type 0 is not used by virtio. Use it as wildcard for non-virtio devices
80pub const TYPE_NET: u32 = 1;
81pub const TYPE_BLOCK: u32 = 2;
82
83/// Interrupt flags (re: interrupt status & acknowledge registers).
84/// See linux/virtio_mmio.h.
85pub const VIRTIO_MMIO_INT_VRING: u32 = 0x01;
86pub const VIRTIO_MMIO_INT_CONFIG: u32 = 0x02;
87
88/// Offset from the base MMIO address of a virtio device used by the guest to notify the device of
89/// queue events.
90pub const NOTIFY_REG_OFFSET: u32 = 0x50;
91
92#[derive(Debug)]
93pub enum ActivateError {
94    EpollCtl(IOError),
95    BadActivate,
96}
97
98pub type ActivateResult = std::result::Result<(), ActivateError>;
99
100/// Trait that helps in upcasting an object to Any
101pub trait AsAny {
102    fn as_any(&self) -> &dyn Any;
103
104    fn as_mut_any(&mut self) -> &mut dyn Any;
105}
106impl<T: Any> AsAny for T {
107    fn as_any(&self) -> &dyn Any {
108        self
109    }
110
111    fn as_mut_any(&mut self) -> &mut dyn Any {
112        self
113    }
114}