11use core:: arch:: naked_asm;
2+
23use memory_addr:: VirtAddr ;
34use riscv:: register:: sstatus:: { self , FS } ;
45
@@ -85,7 +86,8 @@ impl FpState {
8586
8687 /// Handles floating-point state context switching
8788 ///
88- /// Saves the current task's FP state (if needed) and restores the next task's FP state
89+ /// Saves the current task's FP state (if needed) and restores the next
90+ /// task's FP state
8991 pub fn switch_to ( & mut self , next_fp_state : & FpState ) {
9092 // get the real FP state of the current task
9193 let current_fs = sstatus:: read ( ) . fs ( ) ;
@@ -98,9 +100,12 @@ impl FpState {
98100 }
99101 // restore the next task's FP state
100102 match next_fp_state. fs {
101- FS :: Clean => next_fp_state. restore ( ) , // the next task's FP state is clean, we should restore it
102- FS :: Initial => FpState :: clear ( ) , // restore the FP state as constant values(all 0)
103- FS :: Off => { } // do nothing
103+ // the next task's FP state is clean, we should restore it
104+ FS :: Clean => next_fp_state. restore ( ) ,
105+ // restore the FP state as constant values(all 0)
106+ FS :: Initial => FpState :: clear ( ) ,
107+ // do nothing
108+ FS :: Off => { }
104109 FS :: Dirty => unreachable ! ( "FP state of the next task should not be dirty" ) ,
105110 }
106111 unsafe { sstatus:: set_fs ( next_fp_state. fs ) } ; // set the FP state to the next task's FP state
@@ -326,8 +331,8 @@ impl TaskContext {
326331
327332 /// Switches to another task.
328333 ///
329- /// It first saves the current task's context from CPU to this place, and then
330- /// restores the next task's context from `next_ctx` to CPU.
334+ /// It first saves the current task's context from CPU to this place, and
335+ /// then restores the next task's context from `next_ctx` to CPU.
331336 pub fn switch_to ( & mut self , next_ctx : & Self ) {
332337 #[ cfg( feature = "tls" ) ]
333338 {
@@ -425,3 +430,87 @@ unsafe extern "C" fn context_switch(_current_task: &mut TaskContext, _next_task:
425430 ret" ,
426431 )
427432}
433+
434+ #[ cfg( feature = "kprobe" ) ]
435+ impl From < & TrapFrame > for kprobe:: PtRegs {
436+ fn from ( tf : & TrapFrame ) -> Self {
437+ kprobe:: PtRegs {
438+ epc : tf. sepc ,
439+ ra : tf. regs . ra ,
440+ sp : tf. regs . sp ,
441+ gp : tf. regs . gp ,
442+ tp : tf. regs . tp ,
443+ t0 : tf. regs . t0 ,
444+ t1 : tf. regs . t1 ,
445+ t2 : tf. regs . t2 ,
446+ s0 : tf. regs . s0 ,
447+ s1 : tf. regs . s1 ,
448+ a0 : tf. regs . a0 ,
449+ a1 : tf. regs . a1 ,
450+ a2 : tf. regs . a2 ,
451+ a3 : tf. regs . a3 ,
452+ a4 : tf. regs . a4 ,
453+ a5 : tf. regs . a5 ,
454+ a6 : tf. regs . a6 ,
455+ a7 : tf. regs . a7 ,
456+ s2 : tf. regs . s2 ,
457+ s3 : tf. regs . s3 ,
458+ s4 : tf. regs . s4 ,
459+ s5 : tf. regs . s5 ,
460+ s6 : tf. regs . s6 ,
461+ s7 : tf. regs . s7 ,
462+ s8 : tf. regs . s8 ,
463+ s9 : tf. regs . s9 ,
464+ s10 : tf. regs . s10 ,
465+ s11 : tf. regs . s11 ,
466+ t3 : tf. regs . t3 ,
467+ t4 : tf. regs . t4 ,
468+ t5 : tf. regs . t5 ,
469+ t6 : tf. regs . t6 ,
470+ status : tf. sstatus . bits ( ) ,
471+ // todo : other fields
472+ badaddr : 0 ,
473+ cause : 0 ,
474+ orig_a0 : 0 ,
475+ }
476+ }
477+ }
478+
479+ impl TrapFrame {
480+ /// Update the TrapFrame from kprobe::PtRegs
481+ pub fn update_from_ptregs ( & mut self , ptregs : kprobe:: PtRegs ) {
482+ self . sepc = ptregs. epc ;
483+ self . regs . ra = ptregs. ra ;
484+ self . regs . sp = ptregs. sp ;
485+ self . regs . gp = ptregs. gp ;
486+ self . regs . tp = ptregs. tp ;
487+ self . regs . t0 = ptregs. t0 ;
488+ self . regs . t1 = ptregs. t1 ;
489+ self . regs . t2 = ptregs. t2 ;
490+ self . regs . s0 = ptregs. s0 ;
491+ self . regs . s1 = ptregs. s1 ;
492+ self . regs . a0 = ptregs. a0 ;
493+ self . regs . a1 = ptregs. a1 ;
494+ self . regs . a2 = ptregs. a2 ;
495+ self . regs . a3 = ptregs. a3 ;
496+ self . regs . a4 = ptregs. a4 ;
497+ self . regs . a5 = ptregs. a5 ;
498+ self . regs . a6 = ptregs. a6 ;
499+ self . regs . a7 = ptregs. a7 ;
500+ self . regs . s2 = ptregs. s2 ;
501+ self . regs . s3 = ptregs. s3 ;
502+ self . regs . s4 = ptregs. s4 ;
503+ self . regs . s5 = ptregs. s5 ;
504+ self . regs . s6 = ptregs. s6 ;
505+ self . regs . s7 = ptregs. s7 ;
506+ self . regs . s8 = ptregs. s8 ;
507+ self . regs . s9 = ptregs. s9 ;
508+ self . regs . s10 = ptregs. s10 ;
509+ self . regs . s11 = ptregs. s11 ;
510+ self . regs . t3 = ptregs. t3 ;
511+ self . regs . t4 = ptregs. t4 ;
512+ self . regs . t5 = ptregs. t5 ;
513+ self . regs . t6 = ptregs. t6 ;
514+ self . sstatus = sstatus:: Sstatus :: from_bits ( ptregs. status ) ;
515+ }
516+ }
0 commit comments