11#[ cfg( feature = "kprobe_test" ) ]
22pub mod kprobe_test;
33
4- use alloc:: sync:: Arc ;
4+ use alloc:: { sync:: Arc , vec :: Vec } ;
55use core:: alloc:: Layout ;
66
77use config:: FRAME_SIZE ;
8- use kprobe:: { Kprobe , KprobeAuxiliaryOps , KprobeBuilder , KprobeManager , KprobePointList } ;
8+ use kprobe:: {
9+ Kprobe , KprobeAuxiliaryOps , KprobeBuilder , KprobeManager , KprobePointList , KretprobeInstance ,
10+ } ;
911use ksync:: Mutex ;
1012use mem:: { alloc_kernel_free_region, kernel_space} ;
1113use page_table:: { addr:: VirtAddr , pte:: MappingFlags } ;
1214
13- use crate :: trap:: CommonTrapFrame ;
15+ use crate :: { task :: current_task , trap:: CommonTrapFrame } ;
1416
1517pub type KernelKprobe = Kprobe < Mutex < ( ) > , KprobeAuxiliary > ;
1618
17- #[ repr( C ) ]
18- #[ derive( Debug , Copy , Clone ) ]
19- pub struct KProbeContext {
20- pub pc : usize ,
21- pub ra : usize ,
22- pub sp : usize ,
23- pub gp : usize ,
24- pub tp : usize ,
25- pub t0 : usize ,
26- pub t1 : usize ,
27- pub t2 : usize ,
28- pub s0 : usize ,
29- pub s1 : usize ,
30- pub a0 : usize ,
31- pub a1 : usize ,
32- pub a2 : usize ,
33- pub a3 : usize ,
34- pub a4 : usize ,
35- pub a5 : usize ,
36- pub a6 : usize ,
37- pub a7 : usize ,
38- pub s2 : usize ,
39- pub s3 : usize ,
40- pub s4 : usize ,
41- pub s5 : usize ,
42- pub s6 : usize ,
43- pub s7 : usize ,
44- pub s8 : usize ,
45- pub s9 : usize ,
46- pub s10 : usize ,
47- pub s11 : usize ,
48- pub t3 : usize ,
49- pub t4 : usize ,
50- pub t5 : usize ,
51- pub t6 : usize ,
52- }
53-
54- impl From < & CommonTrapFrame > for KProbeContext {
55- fn from ( value : & CommonTrapFrame ) -> Self {
56- KProbeContext {
57- pc : value. pc ( ) ,
58- ra : value. regs ( ) [ 1 ] , // x1
59- sp : value. regs ( ) [ 2 ] , // x2
60- gp : value. regs ( ) [ 3 ] , // x3
61- tp : value. regs ( ) [ 4 ] , // x4
62- t0 : value. regs ( ) [ 5 ] , // x5
63- t1 : value. regs ( ) [ 6 ] , // x6
64- t2 : value. regs ( ) [ 7 ] , // x7
65- s0 : value. regs ( ) [ 8 ] , // x8
66- s1 : value. regs ( ) [ 9 ] , // x9
67- a0 : value. regs ( ) [ 10 ] , // x10
68- a1 : value. regs ( ) [ 11 ] , // x11
69- a2 : value. regs ( ) [ 12 ] , // x12
70- a3 : value. regs ( ) [ 13 ] , // x13
71- a4 : value. regs ( ) [ 14 ] , // x14
72- a5 : value. regs ( ) [ 15 ] , // x15
73- a6 : value. regs ( ) [ 16 ] , // x16
74- a7 : value. regs ( ) [ 17 ] , // x17
75- s2 : value. regs ( ) [ 18 ] , // x18
76- s3 : value. regs ( ) [ 19 ] , // x19
77- s4 : value. regs ( ) [ 20 ] , // x20
78- s5 : value. regs ( ) [ 21 ] , // x21
79- s6 : value. regs ( ) [ 22 ] , // x22
80- s7 : value. regs ( ) [ 23 ] , // x23
81- s8 : value. regs ( ) [ 24 ] , // x24
82- s9 : value. regs ( ) [ 25 ] , // x25
83- s10 : value. regs ( ) [ 26 ] , // x26
84- s11 : value. regs ( ) [ 27 ] , // x27
85- t3 : value. regs ( ) [ 28 ] , // x28
86- t4 : value. regs ( ) [ 29 ] , // x29
87- t5 : value. regs ( ) [ 30 ] , // x30
88- t6 : value. regs ( ) [ 31 ] , // x31
89- }
90- }
91- }
92-
9319#[ derive( Debug ) ]
9420pub struct KprobeAuxiliary ;
9521
@@ -135,6 +61,32 @@ impl KprobeAuxiliaryOps for KprobeAuxiliary {
13561 . unmap_region ( VirtAddr :: from ( region_start) , FRAME_SIZE )
13662 . unwrap ( ) ;
13763 }
64+
65+ fn insert_kretprobe_instance_to_task ( instance : kprobe:: KretprobeInstance ) {
66+ static INSTANCE : Mutex < Vec < KretprobeInstance > > = Mutex :: new ( Vec :: new ( ) ) ;
67+ let task = current_task ( ) ;
68+ if let Some ( task) = task {
69+ let mut inner = task. access_inner ( ) ;
70+ inner. kretprobe_instances . push ( instance) ;
71+ } else {
72+ // If the current task is None, we can store it in a static variable
73+ let mut instances = INSTANCE . lock ( ) ;
74+ instances. push ( instance) ;
75+ }
76+ }
77+
78+ fn pop_kretprobe_instance_from_task ( ) -> kprobe:: KretprobeInstance {
79+ static INSTANCE : Mutex < Vec < KretprobeInstance > > = Mutex :: new ( Vec :: new ( ) ) ;
80+ let task = current_task ( ) ;
81+ if let Some ( task) = task {
82+ let mut inner = task. access_inner ( ) ;
83+ inner. kretprobe_instances . pop ( ) . unwrap ( )
84+ } else {
85+ // If the current task is None, we can pop it from the static variable
86+ let mut instances = INSTANCE . lock ( ) ;
87+ instances. pop ( ) . unwrap ( )
88+ }
89+ }
13890}
13991
14092pub static KPROBE_MANAGER : Mutex < KprobeManager < Mutex < ( ) > , KprobeAuxiliary > > =
@@ -159,5 +111,8 @@ pub fn register_kprobe(kprobe_builder: KprobeBuilder<KprobeAuxiliary>) -> Arc<Ke
159111
160112pub fn run_all_kprobe ( frame : & mut CommonTrapFrame ) -> Option < ( ) > {
161113 let mut manager = KPROBE_MANAGER . lock ( ) ;
162- kprobe:: kprobe_handler_from_break ( & mut manager, frame)
114+ let mut pt_regs = frame. to_pt_regs ( ) ;
115+ let res = kprobe:: kprobe_handler_from_break ( & mut manager, & mut pt_regs) ;
116+ frame. update_from_pt_regs ( & pt_regs) ;
117+ res
163118}
0 commit comments