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