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