Added some fixes for continuous data transfer
[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
54
55 #include <linux/version.h>      // LINUX_VERSION_CODE, KERNEL_VERSION()
56
57 //#include <linux/list.h>
58 #include <linux/notifier.h>
59 //#include <linux/smp.h>
60 #include <linux/percpu.h>
61 #include <linux/spinlock.h>
62 #include <linux/rcupdate.h>
63 #include <linux/sched.h>
64
65 #include "arch/asm/dbi_kprobes.h"
66
67 /* kprobe_status settings */
68 #define KPROBE_HIT_ACTIVE       0x00000001
69 #define KPROBE_HIT_SS           0x00000002
70 #define KPROBE_REENTER          0x00000004
71 #define KPROBE_HIT_SSDONE       0x00000008
72
73 #define HIWORD(x)               (((x) & 0xFFFF0000) >> 16)
74 #define LOWORD(x)               ((x) & 0x0000FFFF)
75
76 #define INVALID_VALUE           0xFFFFFFFF
77 #define INVALID_POINTER         (void*)INVALID_VALUE
78
79 #define JPROBE_ENTRY(pentry)    (kprobe_opcode_t *)pentry
80
81 struct kprobe;
82 struct pt_regs;
83 struct kretprobe;
84 struct kretprobe_instance;
85 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
86 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
87 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, unsigned long flags);
88 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, int trapnr);
89 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, struct pt_regs *, void *);
90
91 struct kprobe
92 {
93         struct hlist_node hlist;
94
95         /*list of probes to search by instruction slot*/
96         struct hlist_node is_hlist;
97
98         /* list of kprobes for multi-handler support */
99         struct list_head list;
100
101         /* Indicates that the corresponding module has been ref counted */
102         unsigned int mod_refcounted;
103
104         /*count the number of times this probe was temporarily disarmed */
105         unsigned long nmissed;
106
107         /* location of the probe point */
108         kprobe_opcode_t *addr;
109
110         /* Allow user to indicate symbol name of the probe point */
111         char *symbol_name;
112
113         /* Offset into the symbol */
114         unsigned int offset;
115
116         /* Called before addr is executed. */
117         kprobe_pre_handler_t pre_handler;
118
119         /* Called after addr is executed, unless... */
120         kprobe_post_handler_t post_handler;
121
122         /* ... called if executing addr causes a fault (eg. page fault).
123          * Return 1 if it handled fault, otherwise kernel will see it. */
124         kprobe_fault_handler_t fault_handler;
125
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
130         /* Saved opcode (which has been replaced with breakpoint) */
131         kprobe_opcode_t opcode;
132
133         /* copy of the original instruction */
134         struct arch_specific_insn ainsn;
135         // TGID to which probe belongs
136         pid_t tgid;
137         // override single-step target address,
138         // may be used to redirect control-flow to arbitrary address after probe point
139         // without invocation of original instruction;
140         // useful for functions replacement
141         // if jprobe.entry should return address of function or NULL
142         // if original function should be called
143         // not supported for X86, not tested for MIPS
144         kprobe_opcode_t *ss_addr;
145 #ifdef _DEBUG
146         unsigned long entry_count;
147         unsigned long step_count;
148         unsigned long exit_count;
149         unsigned long lr;
150 #endif
151 #ifdef KPROBES_PROFILE
152         struct timeval start_tm;
153         struct timeval hnd_tm_sum;
154         unsigned long count;
155 #endif
156 };
157
158 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg, struct pt_regs * regs);
159
160 /*
161  * Special probe type that uses setjmp-longjmp type tricks to resume
162  * execution at a specified entry with a matching prototype corresponding
163  * to the probed function - a trick to enable arguments to become
164  * accessible seamlessly by probe handling logic.
165  * Note:
166  * Because of the way compilers allocate stack space for local variables
167  * etc upfront, regardless of sub-scopes within a function, this mirroring
168  * principle currently works only for probes placed on function entry points.
169  */
170 struct jprobe
171 {
172         struct kprobe kp;
173         kprobe_opcode_t *entry; /* probe handling code to jump to */
174         kprobe_pre_entry_handler_t pre_entry;   /*handler whichw willb bec called before 'entry' */
175         void *priv_arg;
176 };
177
178 struct jprobe_instance
179 {
180         struct hlist_node uflist;       /* either on free list or used list */
181         struct hlist_node hlist;
182         struct jprobe *jp;
183         struct task_struct *task;
184 };
185
186
187
188
189
190 /*
191  * Function-return probe -
192  * Note:
193  * User needs to provide a handler function, and initialize maxactive.
194  * maxactive - The maximum number of instances of the probed function that
195  * can be active concurrently.
196  * nmissed - tracks the number of times the probed function's return was
197  * ignored, due to maxactive being too low.
198  *
199  */
200 struct kretprobe
201 {
202         struct kprobe kp;
203         kretprobe_handler_t handler;
204         void *priv_arg;
205         int maxactive;
206         int nmissed;
207         int disarm;
208         struct hlist_head free_instances;
209         struct hlist_head used_instances;
210 };
211
212 struct kretprobe_instance
213 {
214         struct hlist_node uflist;       /* either on free list or used list */
215         struct hlist_node hlist;
216         struct kretprobe *rp;
217         kprobe_opcode_t *ret_addr;
218         struct kretprobe *rp2;
219         struct task_struct *task;
220 };
221
222
223 extern void show_registers (struct pt_regs *regs);
224 extern void kprobes_inc_nmissed_count (struct kprobe *p);
225
226 #define KPROBE_HASH_BITS 6
227 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
228
229
230 /* Get the kprobe at this addr (if any) - called with preemption disabled */
231 struct kprobe *get_kprobe (void *addr, int pid, struct task_struct *ctask);
232 struct kprobe *get_kprobe_by_insn_slot (void *addr, int tgid, struct task_struct *ctask);
233 struct hlist_head *kretprobe_inst_table_head (struct task_struct *tsk);
234
235
236 int register_kprobe (struct kprobe *p, int atomic);
237 void unregister_kprobe (struct kprobe *p, struct task_struct *task, int atomic);
238
239 int register_aggr_kprobe (struct kprobe *old_p, struct kprobe *p);
240 int pre_handler_kretprobe (struct kprobe *p, struct pt_regs *regs);
241
242 int setjmp_pre_handler (struct kprobe *, struct pt_regs *);
243 int longjmp_break_handler (struct kprobe *, struct pt_regs *);
244
245 int register_jprobe (struct jprobe *p, int atomic);
246 void unregister_jprobe (struct jprobe *p, int atomic);
247 void jprobe_return (void);
248 void jprobe_return_end (void);
249
250 struct kretprobe * clone_kretprobe (struct kretprobe *rp);
251 struct kretprobe_instance * get_used_rp_inst (struct kretprobe *rp);
252
253
254 int register_kretprobe (struct kretprobe *rp, int atomic);
255 void unregister_kretprobe (struct kretprobe *rp, int atomic);
256
257 void kretprobe_assert (struct kretprobe_instance *ri, 
258                 unsigned long orig_ret_address, unsigned long trampoline_address);
259
260
261 struct kretprobe_instance *get_free_rp_inst (struct kretprobe *rp);
262 void free_rp_inst (struct kretprobe *rp);
263 void add_rp_inst (struct kretprobe_instance *ri);
264 void recycle_rp_inst (struct kretprobe_instance *ri, struct hlist_head *head);
265
266 //void kretprobe_trampoline_holder (void);
267 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
268
269 #ifdef KPROBES_PROFILE
270 int pre_handler_kretprobe (struct kprobe *p, struct pt_regs *regs, struct vm_area_struct **vma, struct page **page, unsigned long **kaddr);
271 void set_normalized_timeval (struct timeval *tv, time_t sec, suseconds_t usec);
272 #endif
273
274
275 #endif /* _DBI_KPROBES_H */
276