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