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