[FIX] arch includes for kprobe, uprobe, ks_{manager,feature}
[kernel/swap-modules.git] / kprobe / dbi_kprobes.h
1 #ifndef _DBI_KPROBES_H
2 #define _DBI_KPROBES_H
3
4 /*
5  *  Kernel Probes (KProbes)
6  *  include/linux/kprobes.h
7  *
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.
12  *
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.
17  *
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.
21  *
22  * Copyright (C) IBM Corporation, 2002, 2004
23  */
24
25 /*
26  *  Dynamic Binary Instrumentation Module based on KProbes
27  *  modules/kprobe/dbi_kprobes.h
28  *
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.
33  *
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.
38  *
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.
42  *
43  * Copyright (C) Samsung Electronics, 2006-2010
44  *
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
49  *
50  */
51
52
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>
60
61 #include <kprobe/arch/asm/dbi_kprobes.h>
62
63
64 #ifdef CONFIG_ARM
65
66 #define regs_return_value(regs)     ((regs)->ARM_r0)
67
68 #endif
69
70
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
76
77 #define HIWORD(x)               (((x) & 0xFFFF0000) >> 16)
78 #define LOWORD(x)               ((x) & 0x0000FFFF)
79
80 #define INVALID_VALUE           0xFFFFFFFF
81 #define INVALID_POINTER         (void*)INVALID_VALUE
82
83 #define JPROBE_ENTRY(pentry)    (kprobe_opcode_t *)pentry
84
85 #define RETPROBE_STACK_DEPTH 64
86
87 struct kprobe;
88 struct pt_regs;
89 struct kretprobe;
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 *);
96
97 struct kprobe
98 {
99         struct hlist_node                               hlist;
100         /*list of probes to search by instruction slot*/
101 #ifdef CONFIG_ARM
102         struct hlist_node                               is_hlist_arm;
103         struct hlist_node                               is_hlist_thumb;
104 #else /* CONFIG_ARM */
105         struct hlist_node                               is_hlist;
106 #endif /* CONFIG_ARM */
107         /* list of kprobes for multi-handler support */
108         struct list_head                                list;
109         /* Indicates that the corresponding module has been ref counted */
110         unsigned int                                    mod_refcounted;
111         /*count the number of times this probe was temporarily disarmed */
112         unsigned long                                   nmissed;
113         /* location of the probe point */
114         kprobe_opcode_t                                 *addr;
115         /* Allow user to indicate symbol name of the probe point */
116         char                                            *symbol_name;
117         /* Offset into the symbol */
118         unsigned int                                    offset;
119         /* Called before addr is executed. */
120         kprobe_pre_handler_t                            pre_handler;
121         /* Called after addr is executed, unless... */
122         kprobe_post_handler_t                           post_handler;
123         /* ... called if executing addr causes a fault (eg. page fault).
124          * Return 1 if it handled fault, otherwise kernel will see it. */
125         kprobe_fault_handler_t                          fault_handler;
126         /* ... called if breakpoint trap occurs in probe handler.
127          * Return 1 if it handled break, otherwise kernel will see it. */
128         kprobe_break_handler_t                          break_handler;
129         /* Saved opcode (which has been replaced with breakpoint) */
130         kprobe_opcode_t                                 opcode;
131         /* copy of the original instruction */
132         struct arch_specific_insn                       ainsn;
133         // override single-step target address,
134         // may be used to redirect control-flow to arbitrary address after probe point
135         // without invocation of original instruction;
136         // useful for functions replacement
137         // if jprobe.entry should return address of function or NULL
138         // if original function should be called
139         // not supported for X86, not tested for MIPS
140         kprobe_opcode_t                                 *ss_addr;
141         // safe/unsafe to use probe
142 #ifdef CONFIG_ARM
143         unsigned                                        safe_arm:1;
144         unsigned                                        safe_thumb:1;
145 #endif
146 };
147
148 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg, struct pt_regs * regs);
149
150 /*
151  * Special probe type that uses setjmp-longjmp type tricks to resume
152  * execution at a specified entry with a matching prototype corresponding
153  * to the probed function - a trick to enable arguments to become
154  * accessible seamlessly by probe handling logic.
155  * Note:
156  * Because of the way compilers allocate stack space for local variables
157  * etc upfront, regardless of sub-scopes within a function, this mirroring
158  * principle currently works only for probes placed on function entry points.
159  */
160 struct jprobe
161 {
162         struct kprobe kp;
163         // probe handling code to jump to
164         kprobe_opcode_t *entry;
165         // handler whichw willb bec called before 'entry'
166         kprobe_pre_entry_handler_t pre_entry;
167         void *priv_arg;
168 };
169
170 struct jprobe_instance
171 {
172         // either on free list or used list
173         struct hlist_node uflist;
174         struct hlist_node hlist;
175         struct jprobe *jp;
176         struct task_struct *task;
177 };
178
179
180
181
182
183 /*
184  * Function-return probe -
185  * Note:
186  * User needs to provide a handler function, and initialize maxactive.
187  * maxactive - The maximum number of instances of the probed function that
188  * can be active concurrently.
189  * nmissed - tracks the number of times the probed function's return was
190  * ignored, due to maxactive being too low.
191  *
192  */
193 struct kretprobe
194 {
195         struct kprobe kp;
196         kretprobe_handler_t handler;
197         kretprobe_handler_t entry_handler;
198         int maxactive;
199         int nmissed;
200         size_t data_size;
201         struct hlist_head free_instances;
202         struct hlist_head used_instances;
203
204 #ifdef CONFIG_ARM
205         // probe with noreturn (bl,blx)
206         unsigned                                        arm_noret:1;
207         unsigned                                        thumb_noret:1;
208 #endif
209
210 };
211
212 struct kretprobe_instance
213 {
214         // either on free list or used list
215         struct hlist_node uflist;
216         struct hlist_node hlist;
217         struct kretprobe *rp;
218         unsigned long *ret_addr;
219         unsigned long *sp;
220         struct task_struct *task;
221         char data[0];
222 };
223
224
225 extern void show_registers (struct pt_regs *regs);
226 extern void kprobes_inc_nmissed_count (struct kprobe *p);
227
228 //
229 // Large value for fast but memory consuming implementation
230 // it is good when a lot of probes are instrumented
231 //
232 //#define KPROBE_HASH_BITS 6
233 #define KPROBE_HASH_BITS 16
234 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
235
236
237 /* Get the kprobe at this addr (if any) - called with preemption disabled */
238 struct kprobe *get_kprobe(void *addr);
239 struct hlist_head *kretprobe_inst_table_head (void *hash_key);
240
241
242 int dbi_register_kprobe (struct kprobe *p);
243 void dbi_unregister_kprobe(struct kprobe *p);
244
245 int register_aggr_kprobe (struct kprobe *old_p, struct kprobe *p);
246
247 int setjmp_pre_handler (struct kprobe *, struct pt_regs *);
248 int longjmp_break_handler (struct kprobe *, struct pt_regs *);
249
250 int dbi_register_jprobe (struct jprobe *p);
251 void dbi_unregister_jprobe (struct jprobe *p);
252 void dbi_jprobe_return (void);
253 void dbi_jprobe_return_end (void);
254
255 struct kretprobe * clone_kretprobe (struct kretprobe *rp);
256 struct kretprobe_instance * get_used_rp_inst (struct kretprobe *rp);
257
258
259 int alloc_nodes_kretprobe(struct kretprobe *rp);
260 int dbi_register_kretprobe (struct kretprobe *rp);
261 void dbi_unregister_kretprobe (struct kretprobe *rp);
262
263 void kretprobe_assert (struct kretprobe_instance *ri,
264                 unsigned long orig_ret_address, unsigned long trampoline_address);
265
266
267 struct kretprobe_instance *get_free_rp_inst (struct kretprobe *rp);
268 struct kretprobe_instance *get_free_rp_inst_no_alloc (struct kretprobe *rp);
269 void free_rp_inst (struct kretprobe *rp);
270 void add_rp_inst (struct kretprobe_instance *ri);
271 void recycle_rp_inst (struct kretprobe_instance *ri);
272 int dbi_disarm_urp_inst_for_task(struct task_struct *parent, struct task_struct *task);
273
274 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
275
276 #ifdef KPROBES_PROFILE
277 int pre_handler_kretprobe (struct kprobe *p, struct pt_regs *regs, struct vm_area_struct **vma, struct page **page, unsigned long **kaddr);
278 void set_normalized_timeval (struct timeval *tv, time_t sec, suseconds_t usec);
279 #endif
280
281 extern DEFINE_PER_CPU (struct kprobe *, current_kprobe);
282 extern spinlock_t kretprobe_lock;
283 extern struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
284 //extern struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
285 extern atomic_t kprobe_count;
286 extern unsigned long sched_addr;
287
288 struct kprobe *kprobe_running (void);
289 void reset_current_kprobe (void);
290 struct kprobe_ctlblk *get_kprobe_ctlblk (void);
291
292 void prepare_singlestep(struct kprobe *p, struct pt_regs *regs);
293
294 #endif /* _DBI_KPROBES_H */
295