Skip to content

Commit e0d25e1

Browse files
committed
interrupt: introduce traits to manage interrupt sources
Introduce traits InterruptManager and InterruptSourceGroup to manage interrupt sources for virtual devices. Signed-off-by: Liu Jiang <[email protected]> Signed-off-by: Bin Zha <[email protected]>
1 parent b1c8f2b commit e0d25e1

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ license = "Apache-2.0"
77
edition = "2018"
88

99
[dependencies]
10+
libc = ">=0.2.39"
11+
vmm-sys-util = "~0"
12+
13+
[features]
14+
legacy-irq = []
15+
msi-irq = []

src/interrupt/mod.rs

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Copyright (C) 2019-2020 Alibaba Cloud. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Traits and Structs to manage interrupt sources for devices.
5+
//!
6+
//! In system programming, an interrupt is a signal to the processor emitted by hardware or
7+
//! software indicating an event that needs immediate attention. An interrupt alerts the processor
8+
//! to a high-priority condition requiring the interruption of the current code the processor is
9+
//! executing. The processor responds by suspending its current activities, saving its state, and
10+
//! executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal
11+
//! with the event. This interruption is temporary, and, after the interrupt handler finishes,
12+
//! unless handling the interrupt has emitted a fatal error, the processor resumes normal
13+
//! activities.
14+
//!
15+
//! Hardware interrupts are used by devices to communicate that they require attention from the
16+
//! operating system, or a bare-metal program running on the CPU if there are no OSes. The act of
17+
//! initiating a hardware interrupt is referred to as an interrupt request (IRQ). Different devices
18+
//! are usually associated with different interrupts using a unique value associated with each
19+
//! interrupt. This makes it possible to know which hardware device caused which interrupts.
20+
//! These interrupt values are often called IRQ lines, or just interrupt lines.
21+
//!
22+
//! Nowadays, IRQ lines is not the only mechanism to deliver device interrupts to processors.
23+
//! MSI [(Message Signaled Interrupt)](https://en.wikipedia.org/wiki/Message_Signaled_Interrupts)
24+
//! is another commonly used alternative in-band method of signaling an interrupt, using special
25+
//! in-band messages to replace traditional out-of-band assertion of dedicated interrupt lines.
26+
//! While more complex to implement in a device, message signaled interrupts have some significant
27+
//! advantages over pin-based out-of-band interrupt signaling. Message signaled interrupts are
28+
//! supported in PCI bus since its version 2.2, and in later available PCI Express bus. Some non-PCI
29+
//! architectures also use message signaled interrupts.
30+
//!
31+
//! While IRQ is a term commonly used by Operating Systems when dealing with hardware
32+
//! interrupts, the IRQ numbers managed by OSes are independent of the ones managed by VMM.
33+
//! For simplicity sake, the term `Interrupt Source` is used instead of IRQ to represent both pin-based
34+
//! interrupts and MSI interrupts.
35+
//!
36+
//! A device may support multiple types of interrupts, and each type of interrupt may support one
37+
//! or multiple interrupt sources. For example, a PCI device may support:
38+
//! * Legacy Irq: exactly one interrupt source.
39+
//! * PCI MSI Irq: 1,2,4,8,16,32 interrupt sources.
40+
//! * PCI MSIx Irq: 2^n(n=0-11) interrupt sources.
41+
//!
42+
//! A distinct Interrupt Source Identifier (ISID) will be assigned to each interrupt source.
43+
//! An ID allocator will be used to allocate and free Interrupt Source Identifiers for devices.
44+
//! To decouple the vm-device crate from the ID allocator, the vm-device crate doesn't take the
45+
//! responsibility to allocate/free Interrupt Source IDs but only makes use of assigned IDs.
46+
//!
47+
//! The overall flow to deal with interrupts is:
48+
//! * the VMM creates an interrupt manager
49+
//! * the VMM creates a device manager, passing on an reference to the interrupt manager
50+
//! * the device manager passes on an reference to the interrupt manager to all registered devices
51+
//! * guest kernel loads drivers for virtual devices
52+
//! * guest device driver determines the type and number of interrupts needed, and update the
53+
//! device configuration
54+
//! * the virtual device backend requests the interrupt manager to create an interrupt group
55+
//! according to guest configuration information
56+
57+
use std::sync::Arc;
58+
use vmm_sys_util::eventfd::EventFd;
59+
60+
/// Reuse std::io::Result to simplify interoperability among crates.
61+
pub type Result<T> = std::io::Result<T>;
62+
63+
/// Data type to store an interrupt source identifier.
64+
pub type InterruptIndex = u32;
65+
66+
/// Type of interrupt source.
67+
#[derive(Clone, Debug)]
68+
pub enum InterruptSourceType {
69+
#[cfg(feature = "legacy-irq")]
70+
/// Legacy Pin-based Interrupt.
71+
/// On x86 platforms, legacy interrupts are routed through 8259 PICs and/or IOAPICs.
72+
LegacyIrq,
73+
#[cfg(feature = "msi-irq")]
74+
/// Message Signaled Interrupt (PCI MSI/PCI MSIx etc).
75+
/// Some non-PCI devices (like HPET on x86) make use of generic MSI in platform specific ways.
76+
MsiIrq,
77+
}
78+
79+
/// Configuration data for an interrupt source.
80+
#[derive(Clone, Debug)]
81+
pub enum InterruptSourceConfig {
82+
#[cfg(feature = "legacy-irq")]
83+
/// Configuration data for Legacy interrupts.
84+
LegacyIrq(LegacyIrqSourceConfig),
85+
#[cfg(feature = "msi-irq")]
86+
/// Configuration data for PciMsi, PciMsix and generic MSI interrupts.
87+
MsiIrq(MsiIrqSourceConfig),
88+
}
89+
90+
/// Configuration data for legacy interrupts.
91+
///
92+
/// On x86 platforms, legacy interrupts means those interrupts routed through PICs or IOAPICs.
93+
#[cfg(feature = "legacy-irq")]
94+
#[derive(Clone, Debug)]
95+
pub struct LegacyIrqSourceConfig {}
96+
97+
/// Configuration data for GenericMsi, PciMsi, PciMsix interrupts.
98+
#[cfg(feature = "msi-irq")]
99+
#[derive(Copy, Clone, Debug, Default)]
100+
pub struct MsiIrqSourceConfig {
101+
/// High address to deliver message signaled interrupt.
102+
pub high_addr: u32,
103+
/// Low address to deliver message signaled interrupt.
104+
pub low_addr: u32,
105+
/// Data to write to deliver message signaled interrupt.
106+
pub data: u32,
107+
}
108+
109+
/// Trait to manage interrupt sources for virtual device backends.
110+
///
111+
/// The InterruptManager implementations should protect itself from concurrent accesses internally,
112+
/// so it could be invoked from multi-threaded context.
113+
pub trait InterruptManager {
114+
/// Create an [InterruptSourceGroup](trait.InterruptSourceGroup.html) object to manage
115+
/// interrupt sources for a virtual device
116+
///
117+
/// An [InterruptSourceGroup](trait.InterruptSourceGroup.html) object manages all interrupt
118+
/// sources of the same type for a virtual device.
119+
///
120+
/// # Arguments
121+
/// * type_: type of interrupt source.
122+
/// * base: base Interrupt Source ID to be managed by the group object.
123+
/// * count: number of Interrupt Sources to be managed by the group object.
124+
fn create_group(
125+
&self,
126+
type_: InterruptSourceType,
127+
base: InterruptIndex,
128+
count: InterruptIndex,
129+
) -> Result<Arc<Box<dyn InterruptSourceGroup>>>;
130+
131+
/// Destroy an [InterruptSourceGroup](trait.InterruptSourceGroup.html) object created by
132+
/// [create_group()](trait.InterruptManager.html#tymethod.create_group).
133+
///
134+
/// Assume the caller takes the responsibility to disable all interrupt sources of the group
135+
/// before calling destroy_group(). This assumption helps to simplify InterruptSourceGroup
136+
/// implementations.
137+
fn destroy_group(&self, group: Arc<Box<dyn InterruptSourceGroup>>) -> Result<()>;
138+
}
139+
140+
/// Trait to manage a group of interrupt sources for a device.
141+
///
142+
/// A device may support several types of interrupts, and each type of interrupt may contain one or
143+
/// multiple continuous interrupt sources. For example, a PCI device may concurrently support:
144+
/// * Legacy Irq: exactly one interrupt source.
145+
/// * PCI MSI Irq: 1,2,4,8,16,32 interrupt sources.
146+
/// * PCI MSIx Irq: 2^n(n=0-11) interrupt sources.
147+
///
148+
/// PCI MSI interrupts of a device may not be configured individually, and must configured as a
149+
/// whole block. So all interrupts of the same type of a device are abstracted as an
150+
/// [InterruptSourceGroup](trait.InterruptSourceGroup.html) object, instead of abstracting each
151+
/// interrupt source as a distinct InterruptSource.
152+
#[allow(clippy::len_without_is_empty)]
153+
#[allow(clippy::trivially_copy_pass_by_ref)]
154+
pub trait InterruptSourceGroup: Send + Sync {
155+
/// Get type of interrupt sources managed by the group.
156+
fn interrupt_type(&self) -> InterruptSourceType;
157+
158+
/// Get number of interrupt sources managed by the group.
159+
fn len(&self) -> InterruptIndex;
160+
161+
/// Get base of the assigned Interrupt Source Identifiers.
162+
fn base(&self) -> InterruptIndex;
163+
164+
/// Enable the interrupt sources in the group to generate interrupts.
165+
fn enable(&self, configs: &[InterruptSourceConfig]) -> Result<()>;
166+
167+
/// Disable the interrupt sources in the group to generate interrupts.
168+
fn disable(&self) -> Result<()>;
169+
170+
/// Update the interrupt source group configuration.
171+
///
172+
/// # Arguments
173+
/// * index: sub-index into the group.
174+
/// * config: configuration data for the interrupt source.
175+
fn update(&self, index: InterruptIndex, config: &InterruptSourceConfig) -> Result<()>;
176+
177+
/// Returns an interrupt notifier from this interrupt.
178+
///
179+
/// An interrupt notifier allows for external components and processes
180+
/// to inject interrupts into a guest, by writing to the file returned
181+
/// by this method.
182+
fn notifier(&self, _index: InterruptIndex) -> Option<&EventFd> {
183+
None
184+
}
185+
186+
/// Inject an interrupt from this interrupt source into the guest.
187+
///
188+
/// If the interrupt has an associated `interrupt_status` register, all bits set in `flag`
189+
/// will be atomically ORed into the `interrupt_status` register.
190+
fn trigger(&self, index: InterruptIndex) -> Result<()>;
191+
192+
/// Mask an interrupt from this interrupt source.
193+
fn mask(&self, _index: InterruptIndex) -> Result<()> {
194+
// Not all interrupt sources can be disabled.
195+
// To accommodate this, we can have a no-op here.
196+
Ok(())
197+
}
198+
199+
/// Unmask an interrupt from this interrupt source.
200+
fn unmask(&self, _index: InterruptIndex) -> Result<()> {
201+
// Not all interrupt sources can be disabled.
202+
// To accommodate this, we can have a no-op here.
203+
Ok(())
204+
}
205+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::cmp::{Ord, PartialOrd};
99
use std::sync::Mutex;
1010

1111
pub mod device_manager;
12+
pub mod interrupt;
1213
pub mod resources;
1314

1415
use self::resources::DeviceResources;

0 commit comments

Comments
 (0)