5 #include <linux/list.h>
6 #include <linux/notifier.h>
8 #include <linux/percpu.h>
9 #include <linux/spinlock.h>
10 #include <linux/rcupdate.h>
11 //#include <linux/mutex.h>
12 #include <linux/sched.h>
14 #include "asm/kprobes.h"
16 /* kprobe_status settings */
17 #define KPROBE_HIT_ACTIVE 0x00000001
18 #define KPROBE_HIT_SS 0x00000002
19 #define KPROBE_REENTER 0x00000004
20 #define KPROBE_HIT_SSDONE 0x00000008
22 /* Attach to insert probes on any functions which should be ignored*/
23 #define __kprobes __attribute__((__section__(".kprobes.text")))
28 struct kretprobe_instance;
29 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs * /*, struct vm_area_struct **,
30 struct page **, unsigned long ** */ );
31 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs * /*, struct vm_area_struct **,
32 struct page **, unsigned long ** */ );
33 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, unsigned long flags);
34 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, int trapnr);
35 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, struct pt_regs *, void *);
39 struct hlist_node hlist;
41 /*list of probes to search by instruction slot*/
42 struct hlist_node is_hlist;
44 /* list of kprobes for multi-handler support */
45 struct list_head list;
47 /* Indicates that the corresponding module has been ref counted */
48 unsigned int mod_refcounted;
50 /*count the number of times this probe was temporarily disarmed */
51 unsigned long nmissed;
53 /* location of the probe point */
54 kprobe_opcode_t *addr;
56 /* Allow user to indicate symbol name of the probe point */
59 /* Offset into the symbol */
62 /* Called before addr is executed. */
63 kprobe_pre_handler_t pre_handler;
65 /* Called after addr is executed, unless... */
66 kprobe_post_handler_t post_handler;
68 /* ... called if executing addr causes a fault (eg. page fault).
69 * Return 1 if it handled fault, otherwise kernel will see it. */
70 kprobe_fault_handler_t fault_handler;
72 /* ... called if breakpoint trap occurs in probe handler.
73 * Return 1 if it handled break, otherwise kernel will see it. */
74 kprobe_break_handler_t break_handler;
76 /* Saved opcode (which has been replaced with breakpoint) */
77 kprobe_opcode_t opcode;
79 /* copy of the original instruction */
80 struct arch_specific_insn ainsn;
81 // TGID to which probe belongs
83 // override single-step target address,
84 // may be used to redirect control-flow to arbitrary address after probe point
85 // without invocation of original instruction;
86 // useful for functions replacement
87 // if jprobe.entry should return address of function or NULL
88 // if original function should be called
89 // not supported for X86, not tested for MIPS
90 kprobe_opcode_t *ss_addr;
92 unsigned long entry_count;
93 unsigned long step_count;
94 unsigned long exit_count;
97 #ifdef KPROBES_PROFILE
98 struct timeval start_tm;
99 struct timeval hnd_tm_sum;
104 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg, struct pt_regs * regs);
107 * Special probe type that uses setjmp-longjmp type tricks to resume
108 * execution at a specified entry with a matching prototype corresponding
109 * to the probed function - a trick to enable arguments to become
110 * accessible seamlessly by probe handling logic.
112 * Because of the way compilers allocate stack space for local variables
113 * etc upfront, regardless of sub-scopes within a function, this mirroring
114 * principle currently works only for probes placed on function entry points.
119 kprobe_opcode_t *entry; /* probe handling code to jump to */
120 kprobe_pre_entry_handler_t pre_entry; /*handler whichw willb bec called before 'entry' */
124 struct jprobe_instance
126 struct hlist_node uflist; /* either on free list or used list */
127 struct hlist_node hlist;
129 struct task_struct *task;
132 DECLARE_PER_CPU (struct kprobe *, current_kprobe);
133 DECLARE_PER_CPU (struct kprobe_ctlblk, kprobe_ctlblk);
135 extern void __arch_prepare_kretprobe (struct kretprobe *rp, struct pt_regs *regs);
138 * Function-return probe -
140 * User needs to provide a handler function, and initialize maxactive.
141 * maxactive - The maximum number of instances of the probed function that
142 * can be active concurrently.
143 * nmissed - tracks the number of times the probed function's return was
144 * ignored, due to maxactive being too low.
150 kretprobe_handler_t handler;
155 struct hlist_head free_instances;
156 struct hlist_head used_instances;
159 struct kretprobe_instance
161 struct hlist_node uflist; /* either on free list or used list */
162 struct hlist_node hlist;
163 struct kretprobe *rp;
164 kprobe_opcode_t *ret_addr;
165 struct kretprobe *rp2;
166 struct task_struct *task;
169 extern spinlock_t kretprobe_lock;
170 extern struct mutex kprobe_mutex;
171 extern int arch_prepare_kprobe (struct kprobe *p);
172 extern int arch_prepare_uprobe (struct kprobe *p, struct task_struct *task, int atomic);
173 extern int arch_prepare_kretprobe (struct kretprobe *p);
174 extern int arch_prepare_uretprobe (struct kretprobe *p, struct task_struct *task);
175 extern void arch_arm_kprobe (struct kprobe *p);
176 extern void arch_arm_kretprobe (struct kretprobe *p);
177 extern void arch_arm_uprobe (struct kprobe *p, struct task_struct *tsk);
178 extern void arch_arm_uretprobe (struct kretprobe *p, struct task_struct *tsk);
179 extern void arch_disarm_kprobe (struct kprobe *p);
180 extern void arch_disarm_kretprobe (struct kretprobe *p);
181 extern void arch_disarm_uprobe (struct kprobe *p, struct task_struct *tsk);
182 extern void arch_disarm_uretprobe (struct kretprobe *p, struct task_struct *tsk);
183 extern int arch_init_kprobes (void);
184 extern void arch_exit_kprobes (void);
185 extern void show_registers (struct pt_regs *regs);
186 extern void kprobes_inc_nmissed_count (struct kprobe *p);
188 /* Get the kprobe at this addr (if any) - called with preemption disabled */
189 struct kprobe *get_kprobe (void *addr, int pid, struct task_struct *ctask);
190 struct kprobe *get_kprobe_by_insn_slot (void *addr, int tgid, struct task_struct *ctask);
191 struct hlist_head *kretprobe_inst_table_head (struct task_struct *tsk);
193 /* kprobe_running() will just return the current_kprobe on this CPU */
194 static inline struct kprobe *
195 kprobe_running (void)
197 return (__get_cpu_var (current_kprobe));
201 reset_current_kprobe (void)
203 //__get_cpu_var (current_kprobe)->spid = -1;
204 __get_cpu_var (current_kprobe) = NULL;
207 static inline struct kprobe_ctlblk *
208 get_kprobe_ctlblk (void)
210 return (&__get_cpu_var (kprobe_ctlblk));
213 int register_kprobe (struct kprobe *p, int atomic);
214 void unregister_kprobe (struct kprobe *p, struct task_struct *task, int atomic);
215 int setjmp_pre_handler (struct kprobe *, struct pt_regs *);
216 int longjmp_break_handler (struct kprobe *, struct pt_regs *);
217 int register_jprobe (struct jprobe *p, int atomic);
218 void unregister_jprobe (struct jprobe *p, int atomic);
219 int register_ujprobe (struct task_struct *task, struct mm_struct *mm, struct jprobe *jp, int atomic);
220 void unregister_ujprobe (struct task_struct *task, struct jprobe *jp, int atomic);
221 void unregister_uprobe (struct kprobe *p, struct task_struct *task, int atomic);
222 void jprobe_return (void);
223 void uprobe_return (void);
225 int register_kretprobe (struct kretprobe *rp, int atomic);
226 void unregister_kretprobe (struct kretprobe *rp, int atomic);
227 int register_uretprobe (struct task_struct *task, struct mm_struct *mm, struct kretprobe *rp, int atomic);
228 void unregister_uretprobe (struct task_struct *task, struct kretprobe *rp, int atomic);
230 void unregister_all_uprobes (struct task_struct *task, int atomic);
232 struct kretprobe_instance *get_free_rp_inst (struct kretprobe *rp);
233 void add_rp_inst (struct kretprobe_instance *ri);
234 //void kprobe_flush_task(struct task_struct *tk);
235 void recycle_rp_inst (struct kretprobe_instance *ri, struct hlist_head *head);
237 //void arch_copy_kprobe(struct kprobe *p);
238 void arch_remove_kprobe (struct kprobe *p, struct task_struct *task);
239 void kretprobe_trampoline_holder (void);
240 int __kprobes trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
241 #ifdef KPROBES_PROFILE
242 int __kprobes pre_handler_kretprobe (struct kprobe *p, struct pt_regs *regs, struct vm_area_struct **vma, struct page **page, unsigned long **kaddr);
243 void set_normalized_timeval (struct timeval *tv, time_t sec, suseconds_t usec);
246 kprobe_opcode_t *get_insn_slot (struct task_struct *task, int atomic);
247 void free_insn_slot (struct hlist_head *page_list, struct task_struct *task, kprobe_opcode_t *slot, int dirty);
249 int access_process_vm_atomic(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
251 #define read_proc_vm_atomic(tsk, addr, buf, len) access_process_vm_atomic(tsk, addr, buf, len, 0)
252 #define write_proc_vm_atomic(tsk, addr, buf, len) access_process_vm_atomic(tsk, addr, buf, len, 1)
253 int page_present (struct mm_struct *mm, unsigned long addr);
254 /*int get_user_pages_atomic(struct task_struct *tsk, struct mm_struct *mm,
255 unsigned long start, int len, int write, int force,
256 struct page **pages, struct vm_area_struct **vmas);*/
257 #define get_user_pages_atomic get_user_pages
258 #ifdef KERNEL_HAS_ISPAGEPRESENT
259 #define page_present is_page_present
261 int page_present (struct mm_struct *mm, unsigned long addr);
263 void purge_garbage_uslots(struct task_struct *task, int atomic);
264 #endif /* _SRC_KPROBES_H */
266 extern kprobe_opcode_t *sched_addr;
267 extern kprobe_opcode_t *fork_addr;