xref: /DragonOS/kernel/src/exception/mod.rs (revision c757940bd61b0125e037a59eb77565e42470201b)
1 use crate::arch::CurrentIrqArch;
2 
3 pub mod ipi;
4 pub mod softirq;
5 
6 /// @brief 中断相关的操作
7 pub trait InterruptArch: Send + Sync {
8     /// @brief 使能中断
9     unsafe fn interrupt_enable();
10     /// @brief 禁止中断
11     unsafe fn interrupt_disable();
12     /// @brief 检查中断是否被禁止
13     fn is_irq_enabled() -> bool;
14 
15     /// @brief 保存当前中断状态,并且禁止中断
16     unsafe fn save_and_disable_irq() -> IrqFlagsGuard;
17     unsafe fn restore_irq(flags: IrqFlags);
18 }
19 
20 #[derive(Debug, Clone, Copy)]
21 pub struct IrqFlags {
22     flags: usize,
23 }
24 
25 impl IrqFlags {
26     pub fn new(flags: usize) -> Self {
27         IrqFlags { flags }
28     }
29 
30     pub fn flags(&self) -> usize {
31         self.flags
32     }
33 }
34 
35 /// @brief 当前中断状态的保护器,当该对象被drop时,会恢复之前的中断状态
36 ///
37 /// # Example
38 ///
39 /// ```
40 /// use crate::arch::CurrentIrqArch;
41 ///
42 /// // disable irq and save irq state (这是唯一的获取IrqFlagsGuard的方法)
43 /// let guard = unsafe{CurrentIrqArch::save_and_disable_irq()};
44 ///
45 /// // do something
46 ///
47 /// // 销毁guard时,会恢复之前的中断状态
48 /// drop(guard);
49 ///
50 /// ```
51 #[derive(Debug)]
52 pub struct IrqFlagsGuard {
53     flags: IrqFlags,
54 }
55 
56 impl IrqFlagsGuard {
57     /// @brief 创建IrqFlagsGuard对象
58     ///
59     /// # Safety
60     ///
61     /// 该函数不安全,因为它不会检查flags是否是一个有效的IrqFlags对象, 而当它被drop时,会恢复flags中的中断状态
62     ///
63     /// 该函数只应被`CurrentIrqArch::save_and_disable_irq`调用
64     pub unsafe fn new(flags: IrqFlags) -> Self {
65         IrqFlagsGuard { flags }
66     }
67 }
68 impl Drop for IrqFlagsGuard {
69     fn drop(&mut self) {
70         unsafe {
71             CurrentIrqArch::restore_irq(self.flags);
72         }
73     }
74 }
75