Merge branch 'tizen_2.4_dev' into tizen
[kernel/swap-modules.git] / kprobe / swap_kprobes.h
1 /**
2  * @file kprobe/swap_kprobes.h
3  * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: initial implementation for ARM and MIPS
4  * @author Alexey Gerenkov <a.gerenkov@samsung.com> User-Space Probes initial implementation;
5  * Support x86/ARM/MIPS for both user and kernel spaces.
6  * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
7  *
8  * @section LICENSE
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  *
24  * @section COPYRIGHT
25  *
26  * Copyright (C) IBM Corporation, 2002, 2004
27  * Copyright (C) Samsung Electronics, 2006-2010
28  *
29  * @section DESCRIPTION
30  *
31  * SWAP kprobe interface definition.
32  */
33
34
35 #ifndef _SWAP_KPROBES_H
36 #define _SWAP_KPROBES_H
37
38 #include <linux/version.h>      /*  LINUX_VERSION_CODE, KERNEL_VERSION() */
39 #include <linux/notifier.h>
40 #include <linux/percpu.h>
41 #include <linux/spinlock.h>
42 #include <linux/rcupdate.h>
43 #include <linux/sched.h>
44 #include <linux/pagemap.h>
45
46 #include <swap-asm/swap_kprobes.h>
47
48
49 /* kprobe_status settings */
50 /** Kprobe hit active */
51 #define KPROBE_HIT_ACTIVE       0x00000001
52 /** Kprobe hit ss */
53 #define KPROBE_HIT_SS           0x00000002
54 /** Kprobe reenter */
55 #define KPROBE_REENTER          0x00000004
56 /** Kprobe hit ss done */
57 #define KPROBE_HIT_SSDONE       0x00000008
58
59 /** High word */
60 #define HIWORD(x)               (((x) & 0xFFFF0000) >> 16)
61 /** Low word */
62 #define LOWORD(x)               ((x) & 0x0000FFFF)
63
64 /** Invalid value */
65 #define INVALID_VALUE           0xFFFFFFFF
66 /** Invalid pointer */
67 #define INVALID_POINTER         (void *)INVALID_VALUE
68
69 /** Jprobe entry */
70 #define JPROBE_ENTRY(pentry)    (kprobe_opcode_t *)pentry
71
72 /** Retprobe stack depth */
73 #define RETPROBE_STACK_DEPTH 64
74
75 struct kprobe;
76 struct pt_regs;
77 struct kretprobe;
78 struct kretprobe_instance;
79
80 /**
81  * @brief Kprobe pre-handler pointer.
82  */
83 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
84
85 /**
86  * @brief Kprobe break handler pointer.
87  */
88 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
89
90 /**
91  * @brief Kprobe post handler pointer.
92  */
93 typedef void (*kprobe_post_handler_t) (struct kprobe *,
94                                        struct pt_regs *,
95                                        unsigned long flags);
96
97 /**
98  * @brief Kprobe fault handler pointer.
99  */
100 typedef int (*kprobe_fault_handler_t) (struct kprobe *,
101                                        struct pt_regs *,
102                                        int trapnr);
103
104 /**
105  * @brief Kretprobe handler pointer.
106  */
107 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
108                                     struct pt_regs *);
109
110 /**
111  * @struct kprobe
112  * @brief Main kprobe struct.
113  */
114 struct kprobe {
115         struct hlist_node                               hlist; /**< Hash list.*/
116         /** List of probes to search by instruction slot.*/
117         struct hlist_node                               is_hlist;
118         /** List of kprobes for multi-handler support.*/
119         struct list_head                                list;
120         /** Count the number of times this probe was temporarily disarmed.*/
121         unsigned long                                   nmissed;
122         /** Location of the probe point. */
123         kprobe_opcode_t                                 *addr;
124         /** Allow user to indicate symbol name of the probe point.*/
125         char                                            *symbol_name;
126         /** Offset into the symbol.*/
127         unsigned int                                    offset;
128         /** Called before addr is executed.*/
129         kprobe_pre_handler_t                            pre_handler;
130         /** Called after addr is executed, unless...*/
131         kprobe_post_handler_t                           post_handler;
132         /** ... called if executing addr causes a fault (eg. page fault).*/
133         kprobe_fault_handler_t                          fault_handler;
134         /** Return 1 if it handled fault, otherwise kernel will see it.*/
135         kprobe_break_handler_t                          break_handler;
136         /** Saved opcode (which has been replaced with breakpoint).*/
137         kprobe_opcode_t                                 opcode;
138         /** Copy of the original instruction.*/
139         struct arch_specific_insn                       ainsn;
140         /** Override single-step target address, may be used to redirect
141          * control-flow to arbitrary address after probe point without
142          * invocation of original instruction; useful for functions
143          * replacement. If jprobe.entry should return address of function or
144          * NULL if original function should be called.
145          * Not supported for X86, not tested for MIPS. */
146         kprobe_opcode_t                                 *ss_addr[NR_CPUS];
147 };
148
149 /**
150  * @brief Kprobe pre-entry handler pointer.
151  */
152 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg,
153                                                      struct pt_regs *regs);
154
155
156 /**
157  * @struct jprobe
158  * @brief Special probe type that uses setjmp-longjmp type tricks to resume
159  * execution at a specified entry with a matching prototype corresponding
160  * to the probed function - a trick to enable arguments to become
161  * accessible seamlessly by probe handling logic.
162  * Note:
163  * Because of the way compilers allocate stack space for local variables
164  * etc upfront, regardless of sub-scopes within a function, this mirroring
165  * principle currently works only for probes placed on function entry points.
166  */
167 struct jprobe {
168         struct kprobe kp;                   /**< This probes kprobe.*/
169         kprobe_opcode_t *entry;             /**< Probe handling code to jump to.*/
170         /** Handler which will be called before 'entry'. */
171         kprobe_pre_entry_handler_t pre_entry;
172         void *priv_arg;                     /**< Private args.*/
173 };
174
175
176 /**
177  * @struct jprobe_instance
178  * @brief Jprobe instance struct.
179  */
180 struct jprobe_instance {
181         /*  either on free list or used list */
182         struct hlist_node uflist;            /**< Jprobes hash list. */
183         struct hlist_node hlist;             /**< Jprobes hash list. */
184         struct jprobe *jp;                   /**< Pointer to the target jprobe. */
185         /** Pointer to the target task_struct. */
186         struct task_struct *task;
187 };
188
189
190
191
192
193 /**
194  * @struct kretprobe
195  * @brief Function-return probe
196  * Note: User needs to provide a handler function, and initialize maxactive.
197  */
198 struct kretprobe {
199         struct kprobe kp;                    /**< Kprobe of this kretprobe.*/
200         kretprobe_handler_t handler;         /**< Handler of this kretprobe.*/
201         kretprobe_handler_t entry_handler;   /**< Entry handler of this kretprobe.*/
202         /** The maximum number of instances of the probed function that can be
203          * active concurrently. */
204         int maxactive;
205         /** Tracks the number of times the probed function's return was ignored,
206          * due to maxactive being too low. */
207         int nmissed;
208         size_t data_size;                    /**< Size of the data. */
209         /** List of this probe's free_instances. */
210         struct hlist_head free_instances;
211         /** List of this probe's used_instances. */
212         struct hlist_head used_instances;
213
214 #ifdef CONFIG_ARM
215         unsigned arm_noret:1;    /**< No-return flag for ARM.*/
216         unsigned thumb_noret:1;  /**< No-return flag for Thumb.*/
217 #endif
218
219 };
220
221 /**
222  * @struct kretprobe_instance
223  * @brief Instance of kretprobe.
224  */
225 struct kretprobe_instance {
226         /*  either on free list or used list */
227         struct hlist_node uflist;       /**< Kretprobe hash list.*/
228         struct hlist_node hlist;        /**< Kretprobe hash list.*/
229         struct kretprobe *rp;           /**< Pointer to this instance's kretprobe.*/
230         unsigned long *ret_addr;        /**< Return address.*/
231         unsigned long *sp;              /**< Stack pointer.*/
232         struct task_struct *task;       /**< Pointer to the target task_struct.*/
233         char data[0];                   /**< Pointer to data.*/
234 };
235
236
237 extern void swap_kprobes_inc_nmissed_count(struct kprobe *p);
238
239 /*
240  * Large value for fast but memory consuming implementation
241  * it is good when a lot of probes are instrumented
242  */
243 /* #define KPROBE_HASH_BITS 6 */
244 #define KPROBE_HASH_BITS 16
245 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
246
247
248 /* Get the kprobe at this addr (if any) - called with preemption disabled */
249 struct kprobe *swap_get_kprobe(void *addr);
250
251
252 int swap_register_kprobe(struct kprobe *p);
253 void swap_unregister_kprobe(struct kprobe *p);
254
255 int swap_setjmp_pre_handler(struct kprobe *, struct pt_regs *);
256 int swap_longjmp_break_handler(struct kprobe *, struct pt_regs *);
257
258 int swap_register_jprobe(struct jprobe *p);
259 void swap_unregister_jprobe(struct jprobe *p);
260 void swap_jprobe_return(void);
261
262
263 int swap_register_kretprobe(struct kretprobe *rp);
264 void swap_unregister_kretprobe(struct kretprobe *rp);
265 void swap_unregister_kretprobes(struct kretprobe **rpp, size_t size);
266
267 /*
268  * use:
269  *      swap_unregister_kretprobe[s]_top();
270  *      synchronize_sched();
271  *      swap_unregister_kretprobe[s]_bottom();
272  *
273  * rp_disarm - indicates the need for restoration of the return address
274  */
275 void swap_unregister_kretprobe_top(struct kretprobe *rp, int rp_disarm);
276 void swap_unregister_kretprobes_top(struct kretprobe **rps, size_t size,
277                                    int rp_disarm);
278 void swap_unregister_kretprobe_bottom(struct kretprobe *rp);
279 void swap_unregister_kretprobes_bottom(struct kretprobe **rps, size_t size);
280
281
282 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
283
284
285 extern atomic_t kprobe_count;
286 extern unsigned long sched_addr;
287
288 struct kprobe *swap_kprobe_running(void);
289 void swap_kprobe_running_set(struct kprobe *p);
290 void swap_reset_current_kprobe(void);
291 struct kprobe_ctlblk *swap_get_kprobe_ctlblk(void);
292
293
294 #endif /* _SWAP_KPROBES_H */
295