[IMPROVE] x86: apply jumper for US probes installing
[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 #ifdef CONFIG_ARM
50
51 #define regs_return_value(regs)     ((regs)->ARM_r0)
52
53 #endif
54
55
56 /* kprobe_status settings */
57 /** Kprobe hit active */
58 #define KPROBE_HIT_ACTIVE       0x00000001
59 /** Kprobe hit ss */
60 #define KPROBE_HIT_SS           0x00000002
61 /** Kprobe reenter */
62 #define KPROBE_REENTER          0x00000004
63 /** Kprobe hit ss done */
64 #define KPROBE_HIT_SSDONE       0x00000008
65
66 /** High word */
67 #define HIWORD(x)               (((x) & 0xFFFF0000) >> 16)
68 /** Low word */
69 #define LOWORD(x)               ((x) & 0x0000FFFF)
70
71 /** Invalid value */
72 #define INVALID_VALUE           0xFFFFFFFF
73 /** Invalid pointer */
74 #define INVALID_POINTER         (void*)INVALID_VALUE
75
76 /** Jprobe entry */
77 #define JPROBE_ENTRY(pentry)    (kprobe_opcode_t *)pentry
78
79 /** Retprobe stack depth */
80 #define RETPROBE_STACK_DEPTH 64
81
82 struct kprobe;
83 struct pt_regs;
84 struct kretprobe;
85 struct kretprobe_instance;
86
87 /**
88  * @brief Kprobe pre-handler pointer.
89  */
90 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
91
92 /**
93  * @brief Kprobe break handler pointer.
94  */
95 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
96
97 /**
98  * @brief Kprobe post handler pointer.
99  */
100 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, unsigned long flags);
101
102 /**
103  * @brief Kprobe fault handler pointer.
104  */
105 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, int trapnr);
106
107 /**
108  * @brief Kretprobe handler pointer.
109  */
110 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, struct pt_regs *);
111
112 /**
113  * @struct kprobe
114  * @brief Main kprobe struct.
115  */
116 struct kprobe
117 {
118         struct hlist_node                               hlist;              /**< Hash list.*/
119         /** List of probes to search by instruction slot.*/
120         struct hlist_node                               is_hlist;
121         /** List of kprobes for multi-handler support.*/
122         struct list_head                                list;
123         /** Indicates that the corresponding module has been ref counted.*/
124         unsigned int                                    mod_refcounted;
125         /** Count the number of times this probe was temporarily disarmed.*/
126         unsigned long                                   nmissed;
127         /** Location of the probe point. */
128         kprobe_opcode_t                                 *addr;
129         /** Allow user to indicate symbol name of the probe point.*/
130         char                                            *symbol_name;
131         /** Offset into the symbol.*/
132         unsigned int                                    offset;
133         /** Called before addr is executed.*/
134         kprobe_pre_handler_t                            pre_handler;
135         /** Called after addr is executed, unless...*/
136         kprobe_post_handler_t                           post_handler;
137         /** ... called if executing addr causes a fault (eg. page fault).*/
138         kprobe_fault_handler_t                          fault_handler;
139         /** Return 1 if it handled fault, otherwise kernel will see it.*/
140         kprobe_break_handler_t                          break_handler;
141         /** Saved opcode (which has been replaced with breakpoint).*/
142         kprobe_opcode_t                                 opcode;
143         /** Copy of the original instruction.*/
144         struct arch_specific_insn                       ainsn;
145         /** Override single-step target address, may be used to redirect 
146          * control-flow to arbitrary address after probe point without
147          * invocation of original instruction; useful for functions
148          * replacement. If jprobe.entry should return address of function or
149          * NULL if original function should be called.
150          * Not supported for X86, not tested for MIPS. */
151         kprobe_opcode_t                                 *ss_addr[NR_CPUS];
152 #ifdef CONFIG_ARM
153         /** Safe/unsafe to use probe on ARM.*/
154         unsigned                                        safe_arm:1;
155         /** Safe/unsafe to use probe on Thumb.*/
156         unsigned                                        safe_thumb:1;
157 #endif
158 };
159
160 /**
161  * @brief Kprobe pre-entry handler pointer.
162  */
163 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg, struct pt_regs * regs);
164
165
166 /**
167  * @struct jprobe
168  * @brief Special probe type that uses setjmp-longjmp type tricks to resume
169  * execution at a specified entry with a matching prototype corresponding
170  * to the probed function - a trick to enable arguments to become
171  * accessible seamlessly by probe handling logic.
172  * Note:
173  * Because of the way compilers allocate stack space for local variables
174  * etc upfront, regardless of sub-scopes within a function, this mirroring
175  * principle currently works only for probes placed on function entry points.
176  */ 
177 struct jprobe
178 {
179         struct kprobe kp;                   /**< This probes kprobe.*/
180         kprobe_opcode_t *entry;             /**< Probe handling code to jump to.*/
181         /** Handler which will be called before 'entry'. */
182         kprobe_pre_entry_handler_t pre_entry;
183         void *priv_arg;                     /**< Private args.*/
184 };
185
186
187 /**
188  * @struct jprobe_instance
189  * @brief Jprobe instance struct.
190  */
191 struct jprobe_instance
192 {
193         // either on free list or used list
194         struct hlist_node uflist;            /**< Jprobes hash list. */
195         struct hlist_node hlist;             /**< Jprobes hash list. */
196         struct jprobe *jp;                   /**< Pointer to the target jprobe. */
197         /** Pointer to the target task_struct. */
198         struct task_struct *task;
199 };
200
201
202
203
204
205 /**
206  * @struct kretprobe
207  * @brief Function-return probe
208  * Note: User needs to provide a handler function, and initialize maxactive.
209  */
210 struct kretprobe
211 {
212         struct kprobe kp;                    /**< Kprobe of this kretprobe.*/
213         kretprobe_handler_t handler;         /**< Handler of this kretprobe.*/
214         kretprobe_handler_t entry_handler;   /**< Entry handler of this kretprobe.*/
215         /** The maximum number of instances of the probed function that can be
216          * active concurrently. */
217         int maxactive;
218         /** Tracks the number of times the probed function's return was ignored,
219          * due to maxactive being too low. */
220         int nmissed;
221         size_t data_size;                    /**< Size of the data. */
222         /** List of this probe's free_instances. */
223         struct hlist_head free_instances;
224         /** List of this probe's used_instances. */
225         struct hlist_head used_instances;
226
227 #ifdef CONFIG_ARM
228         unsigned                                        arm_noret:1;    /**< No-return flag for ARM.*/
229         unsigned                                        thumb_noret:1;  /**< No-return flag for Thumb.*/
230 #endif
231
232 };
233
234 /**
235  * @struct kretprobe_instance
236  * @brief Instance of kretprobe.
237  */
238 struct kretprobe_instance
239 {
240         // either on free list or used list
241         struct hlist_node uflist;       /**< Kretprobe hash list.*/
242         struct hlist_node hlist;        /**< Kretprobe hash list.*/
243         struct kretprobe *rp;           /**< Pointer to this instance's kretprobe.*/
244         unsigned long *ret_addr;        /**< Return address.*/
245         unsigned long *sp;              /**< Stack pointer.*/
246         struct task_struct *task;       /**< Pointer to the target task_struct.*/
247         char data[0];                   /**< Pointer to data.*/
248 };
249
250
251 extern void swap_kprobes_inc_nmissed_count(struct kprobe *p);
252
253 //
254 // Large value for fast but memory consuming implementation
255 // it is good when a lot of probes are instrumented
256 //
257 //#define KPROBE_HASH_BITS 6
258 #define KPROBE_HASH_BITS 16
259 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
260
261
262 /* Get the kprobe at this addr (if any) - called with preemption disabled */
263 struct kprobe *swap_get_kprobe(void *addr);
264
265
266 int swap_register_kprobe(struct kprobe *p);
267 void swap_unregister_kprobe(struct kprobe *p);
268
269 int swap_setjmp_pre_handler(struct kprobe *, struct pt_regs *);
270 int swap_longjmp_break_handler(struct kprobe *, struct pt_regs *);
271
272 int swap_register_jprobe(struct jprobe *p);
273 void swap_unregister_jprobe(struct jprobe *p);
274 void swap_jprobe_return(void);
275
276
277 int swap_register_kretprobe(struct kretprobe *rp);
278 void swap_unregister_kretprobe(struct kretprobe *rp);
279 void swap_unregister_kretprobes(struct kretprobe **rpp, size_t size);
280
281 /*
282  * use:
283  *      swap_unregister_kretprobe[s]_top();
284  *      synchronize_sched();
285  *      swap_unregister_kretprobe[s]_bottom();
286  *
287  * rp_disarm - indicates the need for restoration of the return address
288  */
289 void swap_unregister_kretprobe_top(struct kretprobe *rp, int rp_disarm);
290 void swap_unregister_kretprobes_top(struct kretprobe **rps, size_t size,
291                                    int rp_disarm);
292 void swap_unregister_kretprobe_bottom(struct kretprobe *rp);
293 void swap_unregister_kretprobes_bottom(struct kretprobe **rps, size_t size);
294
295
296 int swap_disarm_urp_inst_for_task(struct task_struct *parent, struct task_struct *task);
297
298 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
299
300
301 DECLARE_PER_CPU(struct kprobe *, swap_current_kprobe);
302 extern atomic_t kprobe_count;
303 extern unsigned long sched_addr;
304
305 struct kprobe *swap_kprobe_running(void);
306 void swap_reset_current_kprobe(void);
307 struct kprobe_ctlblk *swap_get_kprobe_ctlblk(void);
308
309 void prepare_singlestep(struct kprobe *p, struct pt_regs *regs);
310
311 #endif /* _SWAP_KPROBES_H */
312