15c4224e5SLoGin use riscv::register::{scause::Scause, sstatus::Sstatus}; 2f2022a8aSLoGin use system_error::SystemError; 3f2022a8aSLoGin 4338f6903SLoGin use crate::{ 5338f6903SLoGin driver::irqchip::riscv_intc::riscv_intc_init, 6338f6903SLoGin exception::{InterruptArch, IrqFlags, IrqFlagsGuard, IrqNumber}, 75c4224e5SLoGin libs::align::align_up, 8338f6903SLoGin }; 94fda81ceSLoGin 105c4224e5SLoGin use super::cpu::STACK_ALIGN; 115c4224e5SLoGin 125c4224e5SLoGin pub(super) mod entry; 135c4224e5SLoGin mod handle; 144fda81ceSLoGin pub mod ipi; 154fda81ceSLoGin 164fda81ceSLoGin pub struct RiscV64InterruptArch; 174fda81ceSLoGin 184fda81ceSLoGin impl InterruptArch for RiscV64InterruptArch { 19f2022a8aSLoGin unsafe fn arch_irq_init() -> Result<(), SystemError> { 20338f6903SLoGin riscv_intc_init()?; 21338f6903SLoGin 22338f6903SLoGin Ok(()) 23f2022a8aSLoGin } 244fda81ceSLoGin unsafe fn interrupt_enable() { 257a29d4fcSLoGin riscv::interrupt::enable(); 264fda81ceSLoGin } 274fda81ceSLoGin 284fda81ceSLoGin unsafe fn interrupt_disable() { 297a29d4fcSLoGin riscv::interrupt::disable(); 304fda81ceSLoGin } 314fda81ceSLoGin 324fda81ceSLoGin fn is_irq_enabled() -> bool { 337a29d4fcSLoGin riscv::register::sstatus::read().sie() 344fda81ceSLoGin } 354fda81ceSLoGin 364fda81ceSLoGin unsafe fn save_and_disable_irq() -> IrqFlagsGuard { 377a29d4fcSLoGin let sie = riscv::register::sstatus::read().sie(); 387a29d4fcSLoGin IrqFlagsGuard::new(IrqFlags::new(sie.into())) 394fda81ceSLoGin } 404fda81ceSLoGin 414fda81ceSLoGin unsafe fn restore_irq(flags: IrqFlags) { 427a29d4fcSLoGin let sie: bool = flags.flags() != 0; 437a29d4fcSLoGin if sie { 447a29d4fcSLoGin riscv::register::sstatus::set_sie(); 457a29d4fcSLoGin } else { 467a29d4fcSLoGin riscv::register::sstatus::clear_sie(); 477a29d4fcSLoGin } 484fda81ceSLoGin } 493bc96fa4SLoGin 503bc96fa4SLoGin fn probe_total_irq_num() -> u32 { 513bc96fa4SLoGin // todo: 获取中断总数 523bc96fa4SLoGin 256 533bc96fa4SLoGin } 543bc96fa4SLoGin 553bc96fa4SLoGin fn ack_bad_irq(irq: IrqNumber) { 563bc96fa4SLoGin todo!("ack_bad_irq: {}", irq.data()); 573bc96fa4SLoGin } 584fda81ceSLoGin } 594fda81ceSLoGin 604fda81ceSLoGin /// 中断栈帧结构体 614fda81ceSLoGin #[repr(C)] 624fda81ceSLoGin #[derive(Debug, Copy, Clone)] 634fda81ceSLoGin pub struct TrapFrame { 64*9b96c5b5SLoGin pub epc: usize, 65*9b96c5b5SLoGin pub ra: usize, 66*9b96c5b5SLoGin pub sp: usize, 67*9b96c5b5SLoGin pub gp: usize, 68*9b96c5b5SLoGin pub tp: usize, 69*9b96c5b5SLoGin pub t0: usize, 70*9b96c5b5SLoGin pub t1: usize, 71*9b96c5b5SLoGin pub t2: usize, 72*9b96c5b5SLoGin pub s0: usize, 73*9b96c5b5SLoGin pub s1: usize, 74*9b96c5b5SLoGin pub a0: usize, 75*9b96c5b5SLoGin pub a1: usize, 76*9b96c5b5SLoGin pub a2: usize, 77*9b96c5b5SLoGin pub a3: usize, 78*9b96c5b5SLoGin pub a4: usize, 79*9b96c5b5SLoGin pub a5: usize, 80*9b96c5b5SLoGin pub a6: usize, 81*9b96c5b5SLoGin pub a7: usize, 82*9b96c5b5SLoGin pub s2: usize, 83*9b96c5b5SLoGin pub s3: usize, 84*9b96c5b5SLoGin pub s4: usize, 85*9b96c5b5SLoGin pub s5: usize, 86*9b96c5b5SLoGin pub s6: usize, 87*9b96c5b5SLoGin pub s7: usize, 88*9b96c5b5SLoGin pub s8: usize, 89*9b96c5b5SLoGin pub s9: usize, 90*9b96c5b5SLoGin pub s10: usize, 91*9b96c5b5SLoGin pub s11: usize, 92*9b96c5b5SLoGin pub t3: usize, 93*9b96c5b5SLoGin pub t4: usize, 94*9b96c5b5SLoGin pub t5: usize, 95*9b96c5b5SLoGin pub t6: usize, 965c4224e5SLoGin // 以下是中断发生时自动保存的寄存器 97*9b96c5b5SLoGin pub status: Sstatus, 98*9b96c5b5SLoGin pub badaddr: usize, 99*9b96c5b5SLoGin pub cause: Scause, 1005c4224e5SLoGin /// a0 value before the syscall 101*9b96c5b5SLoGin pub origin_a0: usize, 1024fda81ceSLoGin } 1034fda81ceSLoGin 1044fda81ceSLoGin impl TrapFrame { 1055c4224e5SLoGin /// 中断栈帧结构体的大小 1065c4224e5SLoGin pub const SIZE: usize = core::mem::size_of::<TrapFrame>(); 1075c4224e5SLoGin 1085c4224e5SLoGin /// 中断栈帧在栈上的大小 1095c4224e5SLoGin pub const SIZE_ON_STACK: usize = align_up(Self::SIZE, STACK_ALIGN); 1104fda81ceSLoGin /// 判断当前中断是否来自用户模式 111b5b571e0SLoGin pub fn is_from_user(&self) -> bool { 1125c4224e5SLoGin self.status.spp() == riscv::register::sstatus::SPP::User 1134fda81ceSLoGin } 1144fda81ceSLoGin } 115