1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! A declarative builder API to init and wire-up virtual devices onto a
//! "virtual motherboard".
//!
//! At a high level: Given a [`BaseChipsetBuilder`] + a list of
//! [`BaseChipsetDevices`](options::BaseChipsetDevices), return [`Chipset`].

#![warn(missing_docs)]
#![forbid(unsafe_code)]

mod base_chipset;
mod chipset;

pub use self::base_chipset::options;
pub use self::base_chipset::BaseChipsetBuilder;
pub use self::base_chipset::BaseChipsetBuilderError;
pub use self::base_chipset::BaseChipsetBuilderOutput;
pub use self::base_chipset::BaseChipsetDeviceInterfaces;
pub use self::chipset::Chipset;
pub use self::chipset::ChipsetDevices;

// API wart: future changes should avoid exposing the `ChipsetBuilder`, and move
// _all_ device instantiation into `vmotherboard` itself.
pub use self::chipset::backing::arc_mutex::device::ArcMutexChipsetDeviceBuilder;
pub use self::chipset::ChipsetBuilder;

use chipset_device::ChipsetDevice;
use inspect::InspectMut;
use mesh::MeshPayload;
use std::marker::PhantomData;
use std::sync::Arc;
use vm_resource::kind::ChipsetDeviceHandleKind;
use vm_resource::Resource;
use vmcore::device_state::ChangeDeviceState;
use vmcore::save_restore::ProtobufSaveRestore;

/// A supertrait of `ChipsetDevice` that requires devices to also support
/// InspectMut and SaveRestore.
///
/// We don't want to put these bounds on `ChipsetDevice` directly, as that would
/// tightly couple `ChipsetDevice` devices with HvLite-specific infrastructure,
/// making it difficult to share device implementations across VMMs.
pub trait VmmChipsetDevice:
    ChipsetDevice + InspectMut + ProtobufSaveRestore + ChangeDeviceState
{
}

impl<T> VmmChipsetDevice for T where
    T: ChipsetDevice + InspectMut + ProtobufSaveRestore + ChangeDeviceState
{
}

/// A device-triggered power event.
pub enum PowerEvent {
    /// Initiate Power Off
    PowerOff,
    /// Initiate Reset
    Reset,
    /// Initiate Hibernate
    Hibernate,
}

/// Handler for device-triggered power events.
pub trait PowerEventHandler: Send + Sync {
    /// Called when there is a device-triggered power event.
    fn on_power_event(&self, evt: PowerEvent);
}

/// Handler for device-triggered debug events.
pub trait DebugEventHandler: Send + Sync {
    /// Called when a device has requested a debug break.
    fn on_debug_break(&self, vp: Option<u32>);
}

/// Generic Bus Identifier. Used to describe VM bus topologies.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BusId<T> {
    name: Arc<str>,
    _kind: PhantomData<T>,
}

impl<T> BusId<T> {
    /// Create a new `BusId` with the given `name`.
    pub fn new(name: &str) -> Self {
        BusId {
            name: name.into(),
            _kind: PhantomData,
        }
    }
}

#[doc(hidden)]
pub mod bus_kind {
    #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub enum Pci {}
}

/// Type-safe PCI bus ID.
pub type BusIdPci = BusId<bus_kind::Pci>;

/// A handle to instantiate a chipset device.
#[derive(MeshPayload, Debug)]
pub struct ChipsetDeviceHandle {
    /// The name of the device.
    pub name: String,
    /// The device resource handle.
    pub resource: Resource<ChipsetDeviceHandleKind>,
}