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