1 //! 处理中断和异常 2 //! 3 //! 架构相关的处理逻辑参考: https://code.dragonos.org.cn/xref/linux-6.6.21/arch/riscv/kernel/traps.c 4 use core::hint::spin_loop; 5 6 use system_error::SystemError; 7 8 use crate::{arch::syscall::syscall_handler, driver::irqchip::riscv_intc::riscv_intc_irq, kerror}; 9 10 use super::TrapFrame; 11 12 type ExceptionHandler = fn(&mut TrapFrame) -> Result<(), SystemError>; 13 14 static EXCEPTION_HANDLERS: [ExceptionHandler; 16] = [ 15 do_trap_insn_misaligned, // 0 16 do_trap_insn_access_fault, // 1 17 do_trap_insn_illegal, // 2 18 do_trap_break, // 3 19 do_trap_load_misaligned, // 4 20 do_trap_load_access_fault, // 5 21 do_trap_store_misaligned, // 6 22 do_trap_store_access_fault, // 7 23 do_trap_user_env_call, // 8 24 default_handler, // 9 25 default_handler, // 10 26 default_handler, // 11 27 do_trap_insn_page_fault, // 12 28 do_trap_load_page_fault, // 13 29 default_handler, // 14 30 do_trap_store_page_fault, // 15 31 ]; 32 33 #[no_mangle] 34 unsafe extern "C" fn riscv64_do_irq(trap_frame: &mut TrapFrame) { 35 if trap_frame.cause.is_interrupt() { 36 riscv64_do_interrupt(trap_frame); 37 } else if trap_frame.cause.is_exception() { 38 riscv64_do_exception(trap_frame); 39 } 40 } 41 42 /// 处理中断 43 fn riscv64_do_interrupt(trap_frame: &mut TrapFrame) { 44 riscv_intc_irq(trap_frame); 45 } 46 47 /// 处理异常 48 fn riscv64_do_exception(trap_frame: &mut TrapFrame) { 49 let code = trap_frame.cause.code(); 50 51 if code < EXCEPTION_HANDLERS.len() { 52 let handler = EXCEPTION_HANDLERS[code]; 53 handler(trap_frame).ok(); 54 } else { 55 kerror!("riscv64_do_irq: exception code out of range"); 56 loop { 57 // kernel die 58 spin_loop(); 59 } 60 }; 61 } 62 63 fn default_handler(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 64 kerror!("riscv64_do_irq: handler not found"); 65 loop { 66 spin_loop(); 67 } 68 } 69 70 /// 处理指令地址不对齐异常 #0 71 fn do_trap_insn_misaligned(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 72 kerror!("riscv64_do_irq: do_trap_insn_misaligned"); 73 loop { 74 spin_loop(); 75 } 76 } 77 78 /// 处理指令访问异常 #1 79 fn do_trap_insn_access_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 80 kerror!("riscv64_do_irq: do_trap_insn_access_fault"); 81 loop { 82 spin_loop(); 83 } 84 } 85 86 /// 处理非法指令异常 #2 87 fn do_trap_insn_illegal(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 88 kerror!("riscv64_do_irq: do_trap_insn_illegal"); 89 loop { 90 spin_loop(); 91 } 92 } 93 94 /// 处理断点异常 #3 95 fn do_trap_break(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 96 kerror!("riscv64_do_irq: do_trap_break"); 97 loop { 98 spin_loop(); 99 } 100 } 101 102 /// 处理加载地址不对齐异常 #4 103 fn do_trap_load_misaligned(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 104 kerror!("riscv64_do_irq: do_trap_load_misaligned"); 105 loop { 106 spin_loop(); 107 } 108 } 109 110 /// 处理加载访问异常 #5 111 fn do_trap_load_access_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 112 kerror!("riscv64_do_irq: do_trap_load_access_fault"); 113 loop { 114 spin_loop(); 115 } 116 } 117 118 /// 处理存储地址不对齐异常 #6 119 fn do_trap_store_misaligned(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 120 kerror!("riscv64_do_irq: do_trap_store_misaligned"); 121 loop { 122 spin_loop(); 123 } 124 } 125 126 /// 处理存储访问异常 #7 127 fn do_trap_store_access_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 128 kerror!("riscv64_do_irq: do_trap_store_access_fault"); 129 loop { 130 spin_loop(); 131 } 132 } 133 134 /// 处理环境调用异常 #8 135 fn do_trap_user_env_call(trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 136 if trap_frame.is_from_user() { 137 let syscall_num = trap_frame.a7; 138 trap_frame.epc += 4; 139 trap_frame.origin_a0 = trap_frame.a0; 140 syscall_handler(syscall_num, trap_frame); 141 } else { 142 panic!("do_trap_user_env_call: not from user mode") 143 } 144 Ok(()) 145 } 146 147 // 9-11 reserved 148 149 /// 处理指令页错误异常 #12 150 fn do_trap_insn_page_fault(trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 151 let vaddr = trap_frame.badaddr; 152 let cause = trap_frame.cause; 153 let epc = trap_frame.epc; 154 kerror!( 155 "riscv64_do_irq: do_insn_page_fault vaddr: {:#x}, cause: {:?} epc: {:#x}", 156 vaddr, 157 cause, 158 epc 159 ); 160 loop { 161 spin_loop(); 162 } 163 } 164 165 /// 处理页加载错误异常 #13 166 fn do_trap_load_page_fault(trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 167 let vaddr = trap_frame.badaddr; 168 let cause = trap_frame.cause; 169 let epc = trap_frame.epc; 170 kerror!( 171 "riscv64_do_irq: do_trap_load_page_fault: epc: {epc:#x}, vaddr={:#x}, cause={:?}", 172 vaddr, 173 cause 174 ); 175 176 loop { 177 spin_loop(); 178 } 179 } 180 181 // 14 reserved 182 183 /// 处理页存储错误异常 #15 184 fn do_trap_store_page_fault(trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 185 kerror!( 186 "riscv64_do_irq: do_trap_store_page_fault: epc: {:#x}, vaddr={:#x}, cause={:?}", 187 trap_frame.epc, 188 trap_frame.badaddr, 189 trap_frame.cause 190 ); 191 loop { 192 spin_loop(); 193 } 194 } 195