ostd/trap/irq.rs
1// SPDX-License-Identifier: MPL-2.0
2//! IRQ line and IRQ guards.
3use vstd::prelude::*;
4
5use core::{fmt::Debug, ops::Deref};
6
7// use id_alloc::IdAlloc;
8// use spin::Once;
9
10use crate::{
11 arch::{
12 irq::{self /* , IrqRemapping, IRQ_NUM_MAX, IRQ_NUM_MIN */},
13 /* trap::TrapFrame, */
14 },
15 prelude::*,
16 sync::{GuardTransfer, RwLock, SpinLock /* WriteIrqDisabled */},
17 // task::atomic_mode::InAtomicMode,
18 // Error,
19};
20/*
21/// A type alias for the IRQ callback function.
22pub type IrqCallbackFunction = dyn Fn(&TrapFrame) + Sync + Send + 'static;
23
24/// An Interrupt ReQuest (IRQ) line.
25///
26/// Users can use [`alloc`] or [`alloc_specific`] to allocate a (specific) IRQ line.
27///
28/// The IRQ number is guaranteed to be an external IRQ number and users can use [`on_active`] to
29/// safely register callback functions on this IRQ line. When the IRQ line is dropped, all the
30/// registered callbacks will be unregistered automatically.
31///
32/// [`alloc`]: Self::alloc
33/// [`alloc_specific`]: Self::alloc_specific
34/// [`on_active`]: Self::on_active
35#[derive(Debug)]
36#[must_use]
37pub struct IrqLine {
38 inner: Arc<InnerHandle>,
39 callbacks: Vec<CallbackHandle>,
40}
41
42impl IrqLine {
43 /// Allocates an available IRQ line.
44 pub fn alloc() -> Result<Self> {
45 get_or_init_allocator()
46 .lock()
47 .alloc()
48 .map(|id| Self::new(id as u8))
49 .ok_or(Error::NotEnoughResources)
50 }
51
52 /// Allocates a specific IRQ line.
53 pub fn alloc_specific(irq_num: u8) -> Result<Self> {
54 get_or_init_allocator()
55 .lock()
56 .alloc_specific((irq_num - IRQ_NUM_MIN) as usize)
57 .map(|id| Self::new(id as u8))
58 .ok_or(Error::NotEnoughResources)
59 }
60
61 fn new(index: u8) -> Self {
62 let inner = InnerHandle { index };
63 inner.remapping.init(index + IRQ_NUM_MIN);
64
65 Self {
66 inner: Arc::new(inner),
67 callbacks: Vec::new(),
68 }
69 }
70
71 /// Gets the IRQ number.
72 pub fn num(&self) -> u8 {
73 self.inner.index + IRQ_NUM_MIN
74 }
75
76 /// Registers a callback that will be invoked when the IRQ is active.
77 ///
78 /// For each IRQ line, multiple callbacks may be registered.
79 pub fn on_active<F>(&mut self, callback: F)
80 where
81 F: Fn(&TrapFrame) + Sync + Send + 'static,
82 {
83 let callback_handle = {
84 let callback_box = Box::new(callback);
85 let callback_addr = core::ptr::from_ref(&*callback_box).addr();
86
87 let mut callbacks = self.inner.callbacks.write();
88 callbacks.push(callback_box);
89
90 CallbackHandle {
91 irq_index: self.inner.index,
92 callback_addr,
93 }
94 };
95
96 self.callbacks.push(callback_handle);
97 }
98
99 /// Checks if there are no registered callbacks.
100 pub fn is_empty(&self) -> bool {
101 self.callbacks.is_empty()
102 }
103
104 /// Gets the remapping index of the IRQ line.
105 ///
106 /// This method will return `None` if interrupt remapping is disabled or
107 /// not supported by the architecture.
108 pub fn remapping_index(&self) -> Option<u16> {
109 self.inner.remapping.remapping_index()
110 }
111}
112
113impl Clone for IrqLine {
114 fn clone(&self) -> Self {
115 Self {
116 inner: self.inner.clone(),
117 callbacks: Vec::new(),
118 }
119 }
120}
121
122struct Inner {
123 callbacks: RwLock<Vec<Box<IrqCallbackFunction>>, WriteIrqDisabled>,
124 remapping: IrqRemapping,
125}
126
127impl Inner {
128 const fn new() -> Self {
129 Self {
130 callbacks: RwLock::new(Vec::new()),
131 remapping: IrqRemapping::new(),
132 }
133 }
134}
135
136const NUMBER_OF_IRQS: usize = (IRQ_NUM_MAX - IRQ_NUM_MIN) as usize + 1;
137
138static INNERS: [Inner; NUMBER_OF_IRQS] = [const { Inner::new() }; NUMBER_OF_IRQS];
139static ALLOCATOR: Once<SpinLock<IdAlloc>> = Once::new();
140
141fn get_or_init_allocator() -> &'static SpinLock<IdAlloc> {
142 ALLOCATOR.call_once(|| SpinLock::new(IdAlloc::with_capacity(NUMBER_OF_IRQS)))
143}
144
145/// A handle for an allocated IRQ line.
146///
147/// When the handle is dropped, the IRQ line will be released automatically.
148#[must_use]
149#[derive(Debug)]
150struct InnerHandle {
151 index: u8,
152}
153
154impl Deref for InnerHandle {
155 type Target = Inner;
156
157 fn deref(&self) -> &Self::Target {
158 &INNERS[self.index as usize]
159 }
160}
161
162impl Drop for InnerHandle {
163 fn drop(&mut self) {
164 ALLOCATOR.get().unwrap().lock().free(self.index as usize);
165 }
166}
167
168/// A handle for a registered callback on an IRQ line.
169///
170/// When the handle is dropped, the callback will be unregistered automatically.
171#[must_use]
172#[derive(Debug)]
173struct CallbackHandle {
174 irq_index: u8,
175 callback_addr: usize,
176}
177
178impl Drop for CallbackHandle {
179 fn drop(&mut self) {
180 let mut callbacks = INNERS[self.irq_index as usize].callbacks.write();
181
182 let pos = callbacks
183 .iter()
184 .position(|element| core::ptr::from_ref(&**element).addr() == self.callback_addr);
185 let _ = callbacks.swap_remove(pos.unwrap());
186 }
187}
188
189pub(super) fn process_top_half(trap_frame: &TrapFrame, irq_num: usize) {
190 let inner = &INNERS[irq_num - (IRQ_NUM_MIN as usize)];
191 for callback in &*inner.callbacks.read() {
192 callback(trap_frame);
193 }
194}*/
195
196// ####### IRQ Guards #######
197verus! {
198
199/// Disables all IRQs on the current CPU (i.e., locally).
200///
201/// This function returns a guard object, which will automatically enable local IRQs again when
202/// it is dropped. This function works correctly even when it is called in a _nested_ way.
203/// The local IRQs shall only be re-enabled when the most outer guard is dropped.
204///
205/// This function can play nicely with [`SpinLock`] as the type uses this function internally.
206/// One can invoke this function even after acquiring a spin lock. And the reversed order is also ok.
207///
208/// [`SpinLock`]: crate::sync::SpinLock
209///
210/// # Example
211///
212/// ```rust
213/// use ostd::trap;
214///
215/// {
216/// let _ = trap::irq::disable_local();
217/// todo!("do something when irqs are disabled");
218/// }
219/// ```
220pub fn disable_local() -> DisabledLocalIrqGuard {
221 DisabledLocalIrqGuard::new()
222}
223
224/// A guard for disabled local IRQs.
225#[clippy::has_significant_drop]
226#[must_use]
227#[derive(Debug)]
228pub struct DisabledLocalIrqGuard {
229 was_enabled: bool,
230}
231
232impl !Send for DisabledLocalIrqGuard {
233
234}
235
236// Required by `InAtomicMode`. See the safety section of its doc comments.
237impl !Sync for DisabledLocalIrqGuard {
238
239}
240
241// SAFETY: The guard disables local IRQs, which meets the first
242// sufficient condition for atomic mode.
243/* unsafe impl InAtomicMode for DisabledLocalIrqGuard {} */
244
245impl DisabledLocalIrqGuard {
246 /// Whether this guard recorded that local IRQs were enabled.
247 pub closed spec fn was_enabled_spec(self) -> bool {
248 self.was_enabled
249 }
250
251 #[verifier::external_body]
252 fn new() -> Self {
253 let was_enabled = irq::is_local_enabled();
254 if was_enabled {
255 irq::disable_local();
256 }
257 Self { was_enabled }
258 }
259}
260
261impl GuardTransfer for DisabledLocalIrqGuard {
262 /// # Verified Properties
263 /// ## Postconditions
264 /// - Transfers the recorded IRQ-enable flag to the returned guard and clears the receiver's flag.
265 fn transfer_to(&mut self) -> (ret: Self)
266 ensures
267 ret.was_enabled_spec() == old(self).was_enabled_spec(),
268 !final(self).was_enabled_spec(),
269 {
270 let was_enabled = self.was_enabled;
271 self.was_enabled = false;
272 Self { was_enabled }
273 }
274}
275
276} // verus!
277/*impl Drop for DisabledLocalIrqGuard {
278 fn drop(&mut self) {
279 if self.was_enabled {
280 irq::enable_local();
281 }
282 }
283} */
284#[cfg(ktest)]
285mod test {
286 use super::*;
287
288 const IRQ_NUM: u8 = 64;
289 const IRQ_INDEX: usize = (IRQ_NUM - IRQ_NUM_MIN) as usize;
290
291 #[ktest]
292 fn alloc_and_free_irq() {
293 let irq_line = IrqLine::alloc_specific(IRQ_NUM).unwrap();
294 assert!(IrqLine::alloc_specific(IRQ_NUM).is_err());
295
296 let irq_line_cloned = irq_line.clone();
297 assert!(IrqLine::alloc_specific(IRQ_NUM).is_err());
298
299 drop(irq_line);
300 assert!(IrqLine::alloc_specific(IRQ_NUM).is_err());
301
302 drop(irq_line_cloned);
303 assert!(IrqLine::alloc_specific(IRQ_NUM).is_ok());
304 }
305
306 #[ktest]
307 fn register_and_unregister_callback() {
308 let mut irq_line = IrqLine::alloc_specific(IRQ_NUM).unwrap();
309 let mut irq_line_cloned = irq_line.clone();
310
311 assert_eq!(INNERS[IRQ_INDEX].callbacks.read().len(), 0);
312
313 irq_line.on_active(|_| {});
314 assert_eq!(INNERS[IRQ_INDEX].callbacks.read().len(), 1);
315
316 irq_line_cloned.on_active(|_| {});
317 assert_eq!(INNERS[IRQ_INDEX].callbacks.read().len(), 2);
318
319 irq_line_cloned.on_active(|_| {});
320 assert_eq!(INNERS[IRQ_INDEX].callbacks.read().len(), 3);
321
322 drop(irq_line);
323 assert_eq!(INNERS[IRQ_INDEX].callbacks.read().len(), 2);
324
325 drop(irq_line_cloned);
326 assert_eq!(INNERS[IRQ_INDEX].callbacks.read().len(), 0);
327 }
328}