1 #ifndef _SWAP_KPROBES_H
2 #define _SWAP_KPROBES_H
5 * Kernel Probes (KProbes)
6 * include/linux/kprobes.h
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Copyright (C) IBM Corporation, 2002, 2004
26 * Dynamic Binary Instrumentation Module based on KProbes
27 * modules/kprobe/swap_kprobes.h
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 * Copyright (C) Samsung Electronics, 2006-2010
45 * 2006-2007 Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM and MIPS
46 * 2008-2009 Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
47 * Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
48 * 2010 Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
53 #include <linux/version.h> // LINUX_VERSION_CODE, KERNEL_VERSION()
54 #include <linux/notifier.h>
55 #include <linux/percpu.h>
56 #include <linux/spinlock.h>
57 #include <linux/rcupdate.h>
58 #include <linux/sched.h>
59 #include <linux/pagemap.h>
61 #include <kprobe/arch/asm/swap_kprobes.h>
66 #define regs_return_value(regs) ((regs)->ARM_r0)
71 /* kprobe_status settings */
72 #define KPROBE_HIT_ACTIVE 0x00000001
73 #define KPROBE_HIT_SS 0x00000002
74 #define KPROBE_REENTER 0x00000004
75 #define KPROBE_HIT_SSDONE 0x00000008
77 #define HIWORD(x) (((x) & 0xFFFF0000) >> 16)
78 #define LOWORD(x) ((x) & 0x0000FFFF)
80 #define INVALID_VALUE 0xFFFFFFFF
81 #define INVALID_POINTER (void*)INVALID_VALUE
83 #define JPROBE_ENTRY(pentry) (kprobe_opcode_t *)pentry
85 #define RETPROBE_STACK_DEPTH 64
90 struct kretprobe_instance;
91 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
92 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
93 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, unsigned long flags);
94 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, int trapnr);
95 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, struct pt_regs *);
99 struct hlist_node hlist;
100 /*list of probes to search by instruction slot*/
101 struct hlist_node is_hlist;
102 /* list of kprobes for multi-handler support */
103 struct list_head list;
104 /* Indicates that the corresponding module has been ref counted */
105 unsigned int mod_refcounted;
106 /*count the number of times this probe was temporarily disarmed */
107 unsigned long nmissed;
108 /* location of the probe point */
109 kprobe_opcode_t *addr;
110 /* Allow user to indicate symbol name of the probe point */
112 /* Offset into the symbol */
114 /* Called before addr is executed. */
115 kprobe_pre_handler_t pre_handler;
116 /* Called after addr is executed, unless... */
117 kprobe_post_handler_t post_handler;
118 /* ... called if executing addr causes a fault (eg. page fault).
119 * Return 1 if it handled fault, otherwise kernel will see it. */
120 kprobe_fault_handler_t fault_handler;
121 /* ... called if breakpoint trap occurs in probe handler.
122 * Return 1 if it handled break, otherwise kernel will see it. */
123 kprobe_break_handler_t break_handler;
124 /* Saved opcode (which has been replaced with breakpoint) */
125 kprobe_opcode_t opcode;
126 /* copy of the original instruction */
127 struct arch_specific_insn ainsn;
128 // override single-step target address,
129 // may be used to redirect control-flow to arbitrary address after probe point
130 // without invocation of original instruction;
131 // useful for functions replacement
132 // if jprobe.entry should return address of function or NULL
133 // if original function should be called
134 // not supported for X86, not tested for MIPS
135 kprobe_opcode_t *ss_addr[NR_CPUS];
136 // safe/unsafe to use probe
139 unsigned safe_thumb:1;
143 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg, struct pt_regs * regs);
146 * Special probe type that uses setjmp-longjmp type tricks to resume
147 * execution at a specified entry with a matching prototype corresponding
148 * to the probed function - a trick to enable arguments to become
149 * accessible seamlessly by probe handling logic.
151 * Because of the way compilers allocate stack space for local variables
152 * etc upfront, regardless of sub-scopes within a function, this mirroring
153 * principle currently works only for probes placed on function entry points.
158 // probe handling code to jump to
159 kprobe_opcode_t *entry;
160 // handler whichw willb bec called before 'entry'
161 kprobe_pre_entry_handler_t pre_entry;
165 struct jprobe_instance
167 // either on free list or used list
168 struct hlist_node uflist;
169 struct hlist_node hlist;
171 struct task_struct *task;
179 * Function-return probe -
181 * User needs to provide a handler function, and initialize maxactive.
182 * maxactive - The maximum number of instances of the probed function that
183 * can be active concurrently.
184 * nmissed - tracks the number of times the probed function's return was
185 * ignored, due to maxactive being too low.
191 kretprobe_handler_t handler;
192 kretprobe_handler_t entry_handler;
196 struct hlist_head free_instances;
197 struct hlist_head used_instances;
200 // probe with noreturn (bl,blx)
201 unsigned arm_noret:1;
202 unsigned thumb_noret:1;
207 struct kretprobe_instance
209 // either on free list or used list
210 struct hlist_node uflist;
211 struct hlist_node hlist;
212 struct kretprobe *rp;
213 unsigned long *ret_addr;
215 struct task_struct *task;
220 extern void kprobes_inc_nmissed_count (struct kprobe *p);
223 // Large value for fast but memory consuming implementation
224 // it is good when a lot of probes are instrumented
226 //#define KPROBE_HASH_BITS 6
227 #define KPROBE_HASH_BITS 16
228 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
231 /* Get the kprobe at this addr (if any) - called with preemption disabled */
232 struct kprobe *get_kprobe(void *addr);
235 int swap_register_kprobe(struct kprobe *p);
236 void swap_unregister_kprobe(struct kprobe *p);
238 int setjmp_pre_handler (struct kprobe *, struct pt_regs *);
239 int longjmp_break_handler (struct kprobe *, struct pt_regs *);
241 int swap_register_jprobe(struct jprobe *p);
242 void swap_unregister_jprobe(struct jprobe *p);
243 void swap_jprobe_return(void);
246 int swap_register_kretprobe(struct kretprobe *rp);
247 void swap_unregister_kretprobe(struct kretprobe *rp);
248 void swap_unregister_kretprobes(struct kretprobe **rpp, size_t size);
252 * swap_unregister_kretprobe[s]_top();
253 * synchronize_sched();
254 * swap_unregister_kretprobe[s]_bottom();
256 * rp_disarm - indicates the need for restoration of the return address
258 void swap_unregister_kretprobe_top(struct kretprobe *rp, int rp_disarm);
259 void swap_unregister_kretprobes_top(struct kretprobe **rps, size_t size,
261 void swap_unregister_kretprobe_bottom(struct kretprobe *rp);
262 void swap_unregister_kretprobes_bottom(struct kretprobe **rps, size_t size);
265 int swap_disarm_urp_inst_for_task(struct task_struct *parent, struct task_struct *task);
267 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
270 extern DEFINE_PER_CPU (struct kprobe *, current_kprobe);
271 extern struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
272 //extern struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
273 extern atomic_t kprobe_count;
274 extern unsigned long sched_addr;
276 struct kprobe *kprobe_running (void);
277 void reset_current_kprobe (void);
278 struct kprobe_ctlblk *get_kprobe_ctlblk (void);
280 void prepare_singlestep(struct kprobe *p, struct pt_regs *regs);
282 #endif /* _SWAP_KPROBES_H */