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