11pub mod boot;
22pub mod handler;
33
4+ use crate :: fail:: unsupported_trap;
5+
46use fast_trap:: { FastContext , FastResult } ;
7+ use riscv:: interrupt:: machine:: { Exception , Interrupt } ;
58use riscv:: register:: {
6- mcause:: { self , Exception as E , Interrupt , Trap as T } ,
7- mepc, mip, mstatus, mtval ,
9+ mcause:: { self , Trap } ,
10+ mepc, mip, mstatus,
811} ;
912
1013/// Fast trap handler for all trap.
@@ -25,47 +28,48 @@ pub extern "C" fn fast_handler(
2528 ctx. regs ( ) . a = [ ctx. a0 ( ) , a1, a2, a3, a4, a5, a6, a7] ;
2629 } ;
2730
28- match mcause:: read ( ) . cause ( ) {
29- // Handle Msoft
30- T :: Interrupt ( Interrupt :: MachineSoft ) => {
31- save_regs ( & mut ctx) ;
32- handler:: msoft_handler ( ctx)
33- }
34- // Handle MTimer
35- T :: Interrupt ( Interrupt :: MachineTimer ) => {
36- use crate :: sbi:: ipi;
31+ match mcause:: read ( ) . cause ( ) . try_into ( ) {
32+ Ok ( cause) => {
33+ match cause {
34+ // Handle Msoft
35+ Trap :: Interrupt ( Interrupt :: MachineSoft ) => {
36+ save_regs ( & mut ctx) ;
37+ handler:: msoft_handler ( ctx)
38+ }
39+ // Handle MTimer
40+ Trap :: Interrupt ( Interrupt :: MachineTimer ) => {
41+ use crate :: sbi:: ipi;
3742
38- ipi:: clear_mtime ( ) ;
39- unsafe {
40- mip:: clear_stimer ( ) ;
41- }
42- save_regs ( & mut ctx) ;
43- ctx. restore ( )
44- }
45- // Handle SBI calls
46- T :: Exception ( E :: SupervisorEnvCall ) => {
47- handler:: sbi_call_handler ( ctx, a1, a2, a3, a4, a5, a6, a7)
48- }
49- // Handle illegal instructions
50- T :: Exception ( E :: IllegalInstruction ) => {
51- if mstatus:: read ( ) . mpp ( ) == mstatus:: MPP :: Machine {
52- panic ! ( "Cannot handle illegal instruction exception from M-MODE" ) ;
53- }
43+ ipi:: clear_mtime ( ) ;
44+ unsafe {
45+ mip:: clear_stimer ( ) ;
46+ }
47+ save_regs ( & mut ctx) ;
48+ ctx. restore ( )
49+ }
50+ // Handle SBI calls
51+ Trap :: Exception ( Exception :: SupervisorEnvCall ) => {
52+ handler:: sbi_call_handler ( ctx, a1, a2, a3, a4, a5, a6, a7)
53+ }
54+ // Handle illegal instructions
55+ Trap :: Exception ( Exception :: IllegalInstruction ) => {
56+ if mstatus:: read ( ) . mpp ( ) == mstatus:: MPP :: Machine {
57+ panic ! ( "Cannot handle illegal instruction exception from M-MODE" ) ;
58+ }
5459
55- save_regs ( & mut ctx) ;
56- if !handler:: illegal_instruction_handler ( & mut ctx) {
57- handler:: delegate ( & mut ctx) ;
60+ save_regs ( & mut ctx) ;
61+ if !handler:: illegal_instruction_handler ( & mut ctx) {
62+ handler:: delegate ( & mut ctx) ;
63+ }
64+ ctx. restore ( )
65+ }
66+ // Handle other traps
67+ trap => unsupported_trap ( Some ( trap) ) ,
5868 }
59- ctx. restore ( )
6069 }
61- // Handle other traps
62- trap => {
63- error ! ( "-----------------------------" ) ;
64- error ! ( "trap: {trap:?}" ) ;
65- error ! ( "mepc: {:#018x}" , mepc:: read( ) ) ;
66- error ! ( "mtval: {:#018x}" , mtval:: read( ) ) ;
67- error ! ( "-----------------------------" ) ;
68- panic ! ( "Stopped with unsupported trap" )
70+ Err ( err) => {
71+ error ! ( "Failed to parse mcause: {:?}" , err) ;
72+ unsupported_trap ( None ) ;
6973 }
7074 }
7175}
0 commit comments