chipset_arc_mutex_device/
services.rs

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Chipset-facing traits required to wire-up a Arc + Mutex backed
//! [`ChipsetDevice`](chipset_device::ChipsetDevice).

use chipset_device::mmio::RegisterMmioIntercept;
use chipset_device::pio::RegisterPortIoIntercept;

/// Compile-time type metadata bundle specified by concrete [`ChipsetServices`]
/// implementations.
///
/// If an implementation doesn't support a particular service, the service's
/// corresponding type should be set to [`Unimplemented`].
///
/// Rather than having `ChipsetServices` directly encode all these type (and
/// make the type signatures for the `supports_XXX` methods absolutely
/// _nightmarish_), we split them all out into their own trait, which can be
/// references via a single type parameter.
pub trait ChipsetServicesMeta {
    // DEVNOTE: ideally, these would all have a default `= Unimplemented`, but
    // associated type defaults are still unstable...
    //
    /// Concrete type that impls `RegisterMmioIntercept`
    type RegisterMmioIntercept: RegisterMmioIntercept;
    /// Concrete type that impls `RegisterPortIoIntercept`
    type RegisterPortIoIntercept: RegisterPortIoIntercept;
}

/// The intermediary that allows a device to wire itself up to various VM
/// chipset services.
pub trait ChipsetServices {
    /// A bundle of associated types used by the concrete implementation.
    type M: ChipsetServicesMeta;

    /// Support for MMIO intercepts.
    #[inline(always)]
    fn supports_mmio(&mut self) -> Option<&mut dyn MmioInterceptServices<M = Self::M>> {
        None
    }

    /// Support for Port IO intercepts.
    #[inline(always)]
    fn supports_pio(&mut self) -> Option<&mut dyn PortIoInterceptServices<M = Self::M>> {
        None
    }

    /// Support for PCI configuration space.
    #[inline(always)]
    fn supports_pci(&mut self) -> Option<&mut dyn PciConfigSpaceServices<M = Self::M>> {
        None
    }

    /// Support for poll.
    #[inline(always)]
    fn supports_poll_device(&mut self) -> Option<&mut dyn PollDeviceServices<M = Self::M>> {
        None
    }
}

/// Implemented by chipsets that can support [`MmioIntercept`] devices.
///
/// [`MmioIntercept`]: chipset_device::mmio::MmioIntercept
pub trait MmioInterceptServices: ChipsetServices {
    /// Obtain an instance of [`RegisterMmioIntercept`]
    fn register_mmio(&self) -> <Self::M as ChipsetServicesMeta>::RegisterMmioIntercept;

    /// Return `true` if any [`MmioInterceptServices`] method has been invoked.
    fn is_being_used(&self) -> bool;
}

/// Implemented by chipsets that can support [`PciConfigSpace`] devices.
///
/// [`PciConfigSpace`]: chipset_device::pci::PciConfigSpace
pub trait PciConfigSpaceServices: ChipsetServices {
    /// Register the device at the specified (bus, device, function)
    fn register_static_pci(&mut self, bus: u8, device: u8, function: u8);

    /// Return `true` if any [`PciConfigSpaceServices`] method has been invoked.
    fn is_being_used(&self) -> bool;
}

/// Implemented by chipsets that can support [`PortIoIntercept`] devices.
///
/// [`PortIoIntercept`]: chipset_device::pio::PortIoIntercept
pub trait PortIoInterceptServices: ChipsetServices {
    /// Obtain an instance of [`RegisterPortIoIntercept`]
    fn register_pio(&self) -> <Self::M as ChipsetServicesMeta>::RegisterPortIoIntercept;

    /// Return `true` if any [`PortIoInterceptServices`] method has been invoked.
    fn is_being_used(&self) -> bool;
}

/// Implemented by chipsets that can support [`PollDevice`] devices.
///
/// [`PollDevice`]: chipset_device::poll_device::PollDevice
pub trait PollDeviceServices: ChipsetServices {
    /// Register for asynchronous polling.
    fn register_poll(&mut self);

    /// Return `true` if [`Self::register_poll`] has been invoked.
    fn is_being_used(&self) -> bool;
}

/// A placeholder type to represent a bit of unimplemented functionality.
pub enum Unimplemented {}

impl RegisterMmioIntercept for Unimplemented {
    fn new_io_region(
        &mut self,
        _: &str,
        _: u64,
    ) -> Box<dyn chipset_device::mmio::ControlMmioIntercept> {
        unreachable!()
    }
}

impl RegisterPortIoIntercept for Unimplemented {
    fn new_io_region(
        &mut self,
        _: &str,
        _: u16,
    ) -> Box<dyn chipset_device::pio::ControlPortIoIntercept> {
        unreachable!()
    }
}