15c4224e5SLoGin use core::hint::spin_loop; 25c4224e5SLoGin 35c4224e5SLoGin use system_error::SystemError; 45c4224e5SLoGin 55c4224e5SLoGin use crate::{kdebug, kerror}; 65c4224e5SLoGin 75c4224e5SLoGin use super::TrapFrame; 85c4224e5SLoGin 95c4224e5SLoGin type ExceptionHandler = fn(&mut TrapFrame) -> Result<(), SystemError>; 105c4224e5SLoGin 115c4224e5SLoGin static EXCEPTION_HANDLERS: [ExceptionHandler; 16] = [ 125c4224e5SLoGin do_trap_insn_misaligned, // 0 135c4224e5SLoGin do_trap_insn_access_fault, // 1 145c4224e5SLoGin do_trap_insn_illegal, // 2 155c4224e5SLoGin do_trap_break, // 3 165c4224e5SLoGin do_trap_load_misaligned, // 4 175c4224e5SLoGin do_trap_load_access_fault, // 5 185c4224e5SLoGin do_trap_store_misaligned, // 6 195c4224e5SLoGin do_trap_store_access_fault, // 7 205c4224e5SLoGin do_trap_user_env_call, // 8 215c4224e5SLoGin default_handler, // 9 225c4224e5SLoGin default_handler, // 10 235c4224e5SLoGin default_handler, // 11 245c4224e5SLoGin do_trap_insn_page_fault, // 12 255c4224e5SLoGin do_trap_load_page_fault, // 13 265c4224e5SLoGin default_handler, // 14 275c4224e5SLoGin do_trap_store_page_fault, // 15 285c4224e5SLoGin ]; 295c4224e5SLoGin 305c4224e5SLoGin #[no_mangle] 315c4224e5SLoGin unsafe extern "C" fn riscv64_do_irq(trap_frame: &mut TrapFrame) { 325c4224e5SLoGin if trap_frame.cause.is_interrupt() { 335c4224e5SLoGin riscv64_do_interrupt(trap_frame); 345c4224e5SLoGin } else if trap_frame.cause.is_exception() { 355c4224e5SLoGin riscv64_do_exception(trap_frame); 365c4224e5SLoGin } 375c4224e5SLoGin } 385c4224e5SLoGin 395c4224e5SLoGin /// 处理中断 405c4224e5SLoGin fn riscv64_do_interrupt(_trap_frame: &mut TrapFrame) { 415c4224e5SLoGin kdebug!("todo: riscv64_do_irq: interrupt"); 425c4224e5SLoGin loop { 435c4224e5SLoGin spin_loop(); 445c4224e5SLoGin } 455c4224e5SLoGin } 465c4224e5SLoGin 475c4224e5SLoGin /// 处理异常 485c4224e5SLoGin fn riscv64_do_exception(trap_frame: &mut TrapFrame) { 495c4224e5SLoGin kdebug!( 505c4224e5SLoGin "riscv64_do_exception: from_user: {}", 51*b5b571e0SLoGin trap_frame.is_from_user() 525c4224e5SLoGin ); 535c4224e5SLoGin let code = trap_frame.cause.code(); 545c4224e5SLoGin 555c4224e5SLoGin if code < EXCEPTION_HANDLERS.len() { 565c4224e5SLoGin let handler = EXCEPTION_HANDLERS[code]; 575c4224e5SLoGin handler(trap_frame).ok(); 585c4224e5SLoGin } else { 595c4224e5SLoGin kerror!("riscv64_do_irq: exception code out of range"); 605c4224e5SLoGin loop { 615c4224e5SLoGin // kernel die 625c4224e5SLoGin spin_loop(); 635c4224e5SLoGin } 645c4224e5SLoGin }; 655c4224e5SLoGin } 665c4224e5SLoGin 675c4224e5SLoGin fn default_handler(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 685c4224e5SLoGin kerror!("riscv64_do_irq: handler not found"); 695c4224e5SLoGin loop { 705c4224e5SLoGin spin_loop(); 715c4224e5SLoGin } 725c4224e5SLoGin } 735c4224e5SLoGin 745c4224e5SLoGin /// 处理指令地址不对齐异常 #0 755c4224e5SLoGin fn do_trap_insn_misaligned(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 765c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_insn_misaligned"); 775c4224e5SLoGin loop { 785c4224e5SLoGin spin_loop(); 795c4224e5SLoGin } 805c4224e5SLoGin } 815c4224e5SLoGin 825c4224e5SLoGin /// 处理指令访问异常 #1 835c4224e5SLoGin fn do_trap_insn_access_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 845c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_insn_access_fault"); 855c4224e5SLoGin loop { 865c4224e5SLoGin spin_loop(); 875c4224e5SLoGin } 885c4224e5SLoGin } 895c4224e5SLoGin 905c4224e5SLoGin /// 处理非法指令异常 #2 915c4224e5SLoGin fn do_trap_insn_illegal(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 925c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_insn_illegal"); 935c4224e5SLoGin loop { 945c4224e5SLoGin spin_loop(); 955c4224e5SLoGin } 965c4224e5SLoGin } 975c4224e5SLoGin 985c4224e5SLoGin /// 处理断点异常 #3 995c4224e5SLoGin fn do_trap_break(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1005c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_break"); 1015c4224e5SLoGin loop { 1025c4224e5SLoGin spin_loop(); 1035c4224e5SLoGin } 1045c4224e5SLoGin } 1055c4224e5SLoGin 1065c4224e5SLoGin /// 处理加载地址不对齐异常 #4 1075c4224e5SLoGin fn do_trap_load_misaligned(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1085c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_load_misaligned"); 1095c4224e5SLoGin loop { 1105c4224e5SLoGin spin_loop(); 1115c4224e5SLoGin } 1125c4224e5SLoGin } 1135c4224e5SLoGin 1145c4224e5SLoGin /// 处理加载访问异常 #5 1155c4224e5SLoGin fn do_trap_load_access_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1165c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_load_access_fault"); 1175c4224e5SLoGin loop { 1185c4224e5SLoGin spin_loop(); 1195c4224e5SLoGin } 1205c4224e5SLoGin } 1215c4224e5SLoGin 1225c4224e5SLoGin /// 处理存储地址不对齐异常 #6 1235c4224e5SLoGin fn do_trap_store_misaligned(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1245c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_store_misaligned"); 1255c4224e5SLoGin loop { 1265c4224e5SLoGin spin_loop(); 1275c4224e5SLoGin } 1285c4224e5SLoGin } 1295c4224e5SLoGin 1305c4224e5SLoGin /// 处理存储访问异常 #7 1315c4224e5SLoGin fn do_trap_store_access_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1325c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_store_access_fault"); 1335c4224e5SLoGin loop { 1345c4224e5SLoGin spin_loop(); 1355c4224e5SLoGin } 1365c4224e5SLoGin } 1375c4224e5SLoGin 1385c4224e5SLoGin /// 处理环境调用异常 #8 1395c4224e5SLoGin fn do_trap_user_env_call(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1405c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_user_env_call"); 1415c4224e5SLoGin loop { 1425c4224e5SLoGin spin_loop(); 1435c4224e5SLoGin } 1445c4224e5SLoGin } 1455c4224e5SLoGin 1465c4224e5SLoGin // 9-11 reserved 1475c4224e5SLoGin 1485c4224e5SLoGin /// 处理指令页错误异常 #12 1495c4224e5SLoGin fn do_trap_insn_page_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1505c4224e5SLoGin kerror!("riscv64_do_irq: do_insn_page_fault"); 1515c4224e5SLoGin loop { 1525c4224e5SLoGin spin_loop(); 1535c4224e5SLoGin } 1545c4224e5SLoGin } 1555c4224e5SLoGin 1565c4224e5SLoGin /// 处理页加载错误异常 #13 1575c4224e5SLoGin fn do_trap_load_page_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1585c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_load_page_fault"); 1595c4224e5SLoGin loop { 1605c4224e5SLoGin spin_loop(); 1615c4224e5SLoGin } 1625c4224e5SLoGin } 1635c4224e5SLoGin 1645c4224e5SLoGin // 14 reserved 1655c4224e5SLoGin 1665c4224e5SLoGin /// 处理页存储错误异常 #15 1675c4224e5SLoGin fn do_trap_store_page_fault(_trap_frame: &mut TrapFrame) -> Result<(), SystemError> { 1685c4224e5SLoGin kerror!("riscv64_do_irq: do_trap_store_page_fault"); 1695c4224e5SLoGin loop { 1705c4224e5SLoGin spin_loop(); 1715c4224e5SLoGin } 1725c4224e5SLoGin } 173