xref: /DragonOS/kernel/src/arch/x86_64/interrupt/mod.rs (revision 7cc4a02c7ff7bafd798b185beb7b0c2986b9f32f)
1 #![allow(dead_code)]
2 
3 pub mod ipi;
4 
5 use core::{
6     arch::asm,
7     sync::atomic::{compiler_fence, Ordering},
8 };
9 
10 use crate::exception::{InterruptArch, IrqFlags, IrqFlagsGuard};
11 
12 use super::asm::irqflags::{local_irq_restore, local_irq_save};
13 
14 /// @brief 关闭中断
15 #[inline]
16 pub fn cli() {
17     unsafe {
18         asm!("cli");
19     }
20 }
21 
22 /// @brief 开启中断
23 #[inline]
24 pub fn sti() {
25     unsafe {
26         asm!("sti");
27     }
28 }
29 
30 pub struct X86_64InterruptArch;
31 
32 impl InterruptArch for X86_64InterruptArch {
33     unsafe fn interrupt_enable() {
34         sti();
35     }
36 
37     unsafe fn interrupt_disable() {
38         cli();
39     }
40 
41     fn is_irq_enabled() -> bool {
42         let rflags: u64;
43         unsafe {
44             asm!("pushfq; pop {}", out(reg) rflags, options(nomem, preserves_flags));
45         }
46         return rflags & (1 << 9) != 0;
47     }
48 
49     unsafe fn save_and_disable_irq() -> IrqFlagsGuard {
50         compiler_fence(Ordering::SeqCst);
51         let rflags = local_irq_save();
52         let flags = IrqFlags::new(rflags);
53         let guard = IrqFlagsGuard::new(flags);
54         compiler_fence(Ordering::SeqCst);
55         return guard;
56     }
57 
58     unsafe fn restore_irq(flags: IrqFlags) {
59         compiler_fence(Ordering::SeqCst);
60         local_irq_restore(flags.flags());
61         compiler_fence(Ordering::SeqCst);
62     }
63 }
64