Skip to content

Commit 9e5bc2b

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 3daea68 commit 9e5bc2b

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ repository = "https://github.com/rust-vmm/vm-device"
66
license = "Apache-2.0"
77

88
[dependencies]
9+
vmm-sys-util = "~0"
10+
11+
[features]
12+
legacy-irq = []
13+
msi-irq = []

coverage_config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 79.9,
2+
"coverage_score": 79.5,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/interrupt/mod.rs

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
use vmm_sys_util::eventfd::EventFd;
48+
49+
/// Reuse std::io::Result to simplify interoperability among crates.
50+
pub type Result<T> = std::io::Result<T>;
51+
52+
/// Data type to store an interrupt source identifier.
53+
pub type InterruptIndex = u32;
54+
55+
/// Configuration data for an interrupt source.
56+
#[derive(Clone, Debug)]
57+
pub enum InterruptSourceConfig {
58+
#[cfg(feature = "legacy-irq")]
59+
/// Configuration data for Legacy interrupts.
60+
LegacyIrq(LegacyIrqSourceConfig),
61+
#[cfg(feature = "msi-irq")]
62+
/// Configuration data for PciMsi, PciMsix and generic MSI interrupts.
63+
MsiIrq(MsiIrqSourceConfig),
64+
}
65+
66+
/// Configuration data for legacy interrupts.
67+
///
68+
/// On x86 platforms, legacy interrupts means those interrupts routed through PICs or IOAPICs.
69+
#[cfg(feature = "legacy-irq")]
70+
#[derive(Clone, Debug)]
71+
pub struct LegacyIrqSourceConfig {}
72+
73+
/// Configuration data for GenericMsi, PciMsi, PciMsix interrupts.
74+
#[cfg(feature = "msi-irq")]
75+
#[derive(Copy, Clone, Debug, Default)]
76+
pub struct MsiIrqSourceConfig {
77+
/// High address to deliver message signaled interrupt.
78+
pub high_addr: u32,
79+
/// Low address to deliver message signaled interrupt.
80+
pub low_addr: u32,
81+
/// Data to write to deliver message signaled interrupt.
82+
pub data: u32,
83+
}
84+
85+
/// Trait to manage a group of interrupt sources for a device.
86+
///
87+
/// A device may support several types of interrupts, and each type of interrupt may contain one or
88+
/// multiple continuous interrupt sources. For example, a PCI device may concurrently support:
89+
/// * Legacy Irq: exactly one interrupt source.
90+
/// * PCI MSI Irq: 1,2,4,8,16,32 interrupt sources.
91+
/// * PCI MSIx Irq: 2^n(n=0-11) interrupt sources.
92+
///
93+
/// PCI MSI interrupts of a device may not be configured individually, and must configured as a
94+
/// whole block. So all interrupts of the same type of a device are abstracted as an
95+
/// [InterruptSourceGroup](trait.InterruptSourceGroup.html) object, instead of abstracting each
96+
/// interrupt source as a distinct InterruptSource.
97+
pub trait InterruptSourceGroup: Send + Sync {
98+
/// Enable the interrupt sources in the group to generate interrupts.
99+
///
100+
/// The `enable()` should be invoked before invoking other methods to manipulate the
101+
/// InterruptSourceGroup object.
102+
fn enable(&self, configs: &[InterruptSourceConfig]) -> Result<()>;
103+
104+
/// Disable the interrupt sources in the group to generate interrupts.
105+
fn disable(&self) -> Result<()>;
106+
107+
/// Update the interrupt source group configuration.
108+
///
109+
/// # Arguments
110+
/// * index: sub-index into the group.
111+
/// * config: configuration data for the interrupt source.
112+
fn update(&self, index: InterruptIndex, config: &InterruptSourceConfig) -> Result<()>;
113+
114+
/// Returns an interrupt notifier from this interrupt.
115+
///
116+
/// An interrupt notifier allows for external components and processes
117+
/// to inject interrupts into a guest, by writing to the file returned
118+
/// by this method.
119+
fn notifier(&self, _index: InterruptIndex) -> Option<&EventFd> {
120+
None
121+
}
122+
123+
/// Inject an interrupt from this interrupt source into the guest.
124+
///
125+
/// If the interrupt has an associated `interrupt_status` register, all bits set in `flag`
126+
/// will be atomically ORed into the `interrupt_status` register.
127+
fn trigger(&self, index: InterruptIndex) -> Result<()>;
128+
129+
/// Mask an interrupt from this interrupt source.
130+
fn mask(&self, _index: InterruptIndex) -> Result<()> {
131+
Err(std::io::Error::from(std::io::ErrorKind::InvalidInput))
132+
}
133+
134+
/// Unmask an interrupt from this interrupt source.
135+
fn unmask(&self, _index: InterruptIndex) -> Result<()> {
136+
Err(std::io::Error::from(std::io::ErrorKind::InvalidInput))
137+
}
138+
139+
/// Check whether there are pending interrupts.
140+
fn get_pending_state(&self, _index: InterruptIndex) -> bool {
141+
false
142+
}
143+
}

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
//! rust-vmm device model.
55
6+
extern crate vmm_sys_util;
7+
68
use std::cmp::{Ord, Ordering, PartialOrd};
79

810
pub mod device_manager;
11+
pub mod interrupt;
912
pub mod resources;
1013

1114
// IO Size.

0 commit comments

Comments
 (0)