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