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
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.
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.
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.
26 * Copyright (C) IBM Corporation, 2002, 2004
27 * Copyright (C) Samsung Electronics, 2006-2010
29 * @section DESCRIPTION
31 * SWAP kprobe interface definition.
35 #ifndef _SWAP_KPROBES_H
36 #define _SWAP_KPROBES_H
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>
46 #include <swap-asm/swap_kprobes.h>
51 #define regs_return_value(regs) ((regs)->ARM_r0)
56 /* kprobe_status settings */
57 /** Kprobe hit active */
58 #define KPROBE_HIT_ACTIVE 0x00000001
60 #define KPROBE_HIT_SS 0x00000002
62 #define KPROBE_REENTER 0x00000004
63 /** Kprobe hit ss done */
64 #define KPROBE_HIT_SSDONE 0x00000008
67 #define HIWORD(x) (((x) & 0xFFFF0000) >> 16)
69 #define LOWORD(x) ((x) & 0x0000FFFF)
72 #define INVALID_VALUE 0xFFFFFFFF
73 /** Invalid pointer */
74 #define INVALID_POINTER (void *)INVALID_VALUE
77 #define JPROBE_ENTRY(pentry) (kprobe_opcode_t *)pentry
79 /** Retprobe stack depth */
80 #define RETPROBE_STACK_DEPTH 64
85 struct kretprobe_instance;
88 * @brief Kprobe pre-handler pointer.
90 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
93 * @brief Kprobe break handler pointer.
95 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
98 * @brief Kprobe post handler pointer.
100 typedef void (*kprobe_post_handler_t) (struct kprobe *,
102 unsigned long flags);
105 * @brief Kprobe fault handler pointer.
107 typedef int (*kprobe_fault_handler_t) (struct kprobe *,
112 * @brief Kretprobe handler pointer.
114 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
119 * @brief Main kprobe struct.
122 struct hlist_node hlist; /**< Hash list.*/
123 /** List of probes to search by instruction slot.*/
124 struct hlist_node is_hlist;
125 /** List of kprobes for multi-handler support.*/
126 struct list_head list;
127 /** Indicates that the corresponding module has been ref counted.*/
128 unsigned int mod_refcounted;
129 /** Count the number of times this probe was temporarily disarmed.*/
130 unsigned long nmissed;
131 /** Location of the probe point. */
132 kprobe_opcode_t *addr;
133 /** Allow user to indicate symbol name of the probe point.*/
135 /** Offset into the symbol.*/
137 /** Called before addr is executed.*/
138 kprobe_pre_handler_t pre_handler;
139 /** Called after addr is executed, unless...*/
140 kprobe_post_handler_t post_handler;
141 /** ... called if executing addr causes a fault (eg. page fault).*/
142 kprobe_fault_handler_t fault_handler;
143 /** Return 1 if it handled fault, otherwise kernel will see it.*/
144 kprobe_break_handler_t break_handler;
145 /** Saved opcode (which has been replaced with breakpoint).*/
146 kprobe_opcode_t opcode;
147 /** Copy of the original instruction.*/
148 struct arch_specific_insn ainsn;
149 /** Override single-step target address, may be used to redirect
150 * control-flow to arbitrary address after probe point without
151 * invocation of original instruction; useful for functions
152 * replacement. If jprobe.entry should return address of function or
153 * NULL if original function should be called.
154 * Not supported for X86, not tested for MIPS. */
155 kprobe_opcode_t *ss_addr[NR_CPUS];
159 * @brief Kprobe pre-entry handler pointer.
161 typedef unsigned long (*kprobe_pre_entry_handler_t) (void *priv_arg,
162 struct pt_regs *regs);
167 * @brief Special probe type that uses setjmp-longjmp type tricks to resume
168 * execution at a specified entry with a matching prototype corresponding
169 * to the probed function - a trick to enable arguments to become
170 * accessible seamlessly by probe handling logic.
172 * Because of the way compilers allocate stack space for local variables
173 * etc upfront, regardless of sub-scopes within a function, this mirroring
174 * principle currently works only for probes placed on function entry points.
177 struct kprobe kp; /**< This probes kprobe.*/
178 kprobe_opcode_t *entry; /**< Probe handling code to jump to.*/
179 /** Handler which will be called before 'entry'. */
180 kprobe_pre_entry_handler_t pre_entry;
181 void *priv_arg; /**< Private args.*/
186 * @struct jprobe_instance
187 * @brief Jprobe instance struct.
189 struct jprobe_instance {
190 /* either on free list or used list */
191 struct hlist_node uflist; /**< Jprobes hash list. */
192 struct hlist_node hlist; /**< Jprobes hash list. */
193 struct jprobe *jp; /**< Pointer to the target jprobe. */
194 /** Pointer to the target task_struct. */
195 struct task_struct *task;
204 * @brief Function-return probe
205 * Note: User needs to provide a handler function, and initialize maxactive.
208 struct kprobe kp; /**< Kprobe of this kretprobe.*/
209 kretprobe_handler_t handler; /**< Handler of this kretprobe.*/
210 kretprobe_handler_t entry_handler; /**< Entry handler of this kretprobe.*/
211 /** The maximum number of instances of the probed function that can be
212 * active concurrently. */
214 /** Tracks the number of times the probed function's return was ignored,
215 * due to maxactive being too low. */
217 size_t data_size; /**< Size of the data. */
218 /** List of this probe's free_instances. */
219 struct hlist_head free_instances;
220 /** List of this probe's used_instances. */
221 struct hlist_head used_instances;
224 unsigned arm_noret:1; /**< No-return flag for ARM.*/
225 unsigned thumb_noret:1; /**< No-return flag for Thumb.*/
231 * @struct kretprobe_instance
232 * @brief Instance of kretprobe.
234 struct kretprobe_instance {
235 /* either on free list or used list */
236 struct hlist_node uflist; /**< Kretprobe hash list.*/
237 struct hlist_node hlist; /**< Kretprobe hash list.*/
238 struct kretprobe *rp; /**< Pointer to this instance's kretprobe.*/
239 unsigned long *ret_addr; /**< Return address.*/
240 unsigned long *sp; /**< Stack pointer.*/
241 struct task_struct *task; /**< Pointer to the target task_struct.*/
242 char data[0]; /**< Pointer to data.*/
246 extern void swap_kprobes_inc_nmissed_count(struct kprobe *p);
249 * Large value for fast but memory consuming implementation
250 * it is good when a lot of probes are instrumented
252 /* #define KPROBE_HASH_BITS 6 */
253 #define KPROBE_HASH_BITS 16
254 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
257 /* Get the kprobe at this addr (if any) - called with preemption disabled */
258 struct kprobe *swap_get_kprobe(void *addr);
261 int swap_register_kprobe(struct kprobe *p);
262 void swap_unregister_kprobe(struct kprobe *p);
264 int swap_setjmp_pre_handler(struct kprobe *, struct pt_regs *);
265 int swap_longjmp_break_handler(struct kprobe *, struct pt_regs *);
267 int swap_register_jprobe(struct jprobe *p);
268 void swap_unregister_jprobe(struct jprobe *p);
269 void swap_jprobe_return(void);
272 int swap_register_kretprobe(struct kretprobe *rp);
273 void swap_unregister_kretprobe(struct kretprobe *rp);
274 void swap_unregister_kretprobes(struct kretprobe **rpp, size_t size);
278 * swap_unregister_kretprobe[s]_top();
279 * synchronize_sched();
280 * swap_unregister_kretprobe[s]_bottom();
282 * rp_disarm - indicates the need for restoration of the return address
284 void swap_unregister_kretprobe_top(struct kretprobe *rp, int rp_disarm);
285 void swap_unregister_kretprobes_top(struct kretprobe **rps, size_t size,
287 void swap_unregister_kretprobe_bottom(struct kretprobe *rp);
288 void swap_unregister_kretprobes_bottom(struct kretprobe **rps, size_t size);
291 int trampoline_probe_handler (struct kprobe *p, struct pt_regs *regs);
294 DECLARE_PER_CPU(struct kprobe *, swap_current_kprobe);
295 extern atomic_t kprobe_count;
296 extern unsigned long sched_addr;
298 struct kprobe *swap_kprobe_running(void);
299 void swap_reset_current_kprobe(void);
300 struct kprobe_ctlblk *swap_get_kprobe_ctlblk(void);
302 void prepare_singlestep(struct kprobe *p, struct pt_regs *regs);
304 #endif /* _SWAP_KPROBES_H */