blob: 6bec22f514b5877726babbd828d3df958938bdb9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2002, 2004
19 *
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010025 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010029 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
30 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
31 * <prasanna@in.ibm.com> added function-return probes.
32 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
33 * Added function return probes functionality
34 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
35 * kprobe-booster and kretprobe-booster for i386.
Masami Hiramatsuda07ab02008-01-30 13:31:21 +010036 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
37 * and kretprobe-booster for x86-64
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010038 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
39 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40 * unified x86 kprobes code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kprobes.h>
44#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/string.h>
46#include <linux/slab.h>
Quentin Barnesb506a9d2008-01-30 13:32:32 +010047#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/preempt.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080049#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070050#include <linux/kdebug.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040051#include <linux/kallsyms.h>
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -050052#include <linux/ftrace.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070053
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010054#include <asm/cacheflush.h>
55#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080057#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020058#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040059#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053060#include <asm/debugreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062void jprobe_return_end(void);
63
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080064DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
65DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
H. Peter Anvin98272ed2009-10-12 14:14:10 -070067#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010068
69#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
70 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
71 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
72 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
73 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
74 << (row % 32))
75 /*
76 * Undefined/reserved opcodes, conditional jump, Opcode Extension
77 * Groups, and some special opcodes can not boost.
Linus Torvalds7115e3f2011-10-26 17:03:38 +020078 * This is non-const and volatile to keep gcc from statically
79 * optimizing it out, as variable_test_bit makes gcc think only
80 * *(unsigned long*) is used.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010081 */
Linus Torvalds7115e3f2011-10-26 17:03:38 +020082static volatile u32 twobyte_is_boostable[256 / 32] = {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010083 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
84 /* ---------------------------------------------- */
85 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
86 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
87 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
88 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
89 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
90 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
91 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
92 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
93 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
94 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
95 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
96 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
97 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
98 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
99 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
100 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
101 /* ----------------------------------------------- */
102 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
103};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100104#undef W
105
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700106struct kretprobe_blackpoint kretprobe_blacklist[] = {
107 {"__switch_to", }, /* This function switches only current task, but
108 doesn't switch kernel stack.*/
109 {NULL, NULL} /* Terminator */
110};
111const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
112
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500113static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100114{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500115 struct __arch_relative_insn {
116 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100117 s32 raddr;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500118 } __attribute__((packed)) *insn;
119
120 insn = (struct __arch_relative_insn *)from;
121 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
122 insn->op = op;
123}
124
125/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
126static void __kprobes synthesize_reljump(void *from, void *to)
127{
128 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100129}
130
131/*
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900132 * Skip the prefixes of the instruction.
Harvey Harrison99309272008-01-30 13:32:14 +0100133 */
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900134static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
Harvey Harrison99309272008-01-30 13:32:14 +0100135{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900136 insn_attr_t attr;
137
138 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
139 while (inat_is_legacy_prefix(attr)) {
140 insn++;
141 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
142 }
Harvey Harrison99309272008-01-30 13:32:14 +0100143#ifdef CONFIG_X86_64
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900144 if (inat_is_rex_prefix(attr))
145 insn++;
Harvey Harrison99309272008-01-30 13:32:14 +0100146#endif
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900147 return insn;
Harvey Harrison99309272008-01-30 13:32:14 +0100148}
149
150/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100151 * Returns non-zero if opcode is boostable.
152 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100153 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100154static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100155{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100156 kprobe_opcode_t opcode;
157 kprobe_opcode_t *orig_opcodes = opcodes;
158
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530159 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400160 return 0; /* Page fault may occur on this address. */
161
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100162retry:
163 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
164 return 0;
165 opcode = *(opcodes++);
166
167 /* 2nd-byte opcode */
168 if (opcode == 0x0f) {
169 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
170 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100171 return test_bit(*opcodes,
172 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100173 }
174
175 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100176#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100177 case 0x40:
178 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100179#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100180 case 0x60:
181 if (0x63 < opcode && opcode < 0x67)
182 goto retry; /* prefixes */
183 /* can't boost Address-size override and bound */
184 return (opcode != 0x62 && opcode != 0x67);
185 case 0x70:
186 return 0; /* can't boost conditional jump */
187 case 0xc0:
188 /* can't boost software-interruptions */
189 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
190 case 0xd0:
191 /* can boost AA* and XLAT */
192 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
193 case 0xe0:
194 /* can boost in/out and absolute jmps */
195 return ((opcode & 0x04) || opcode == 0xea);
196 case 0xf0:
197 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
198 goto retry; /* lock/rep(ne) prefix */
199 /* clear and set flags are boostable */
200 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
201 default:
202 /* segment override prefixes are boostable */
203 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
204 goto retry; /* prefixes */
205 /* CS override prefix and call are not boostable */
206 return (opcode != 0x2e && opcode != 0x9a);
207 }
208}
209
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900210static unsigned long __recover_probed_insn(kprobe_opcode_t *buf,
211 unsigned long addr)
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400212{
213 struct kprobe *kp;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900214
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400215 kp = get_kprobe((void *)addr);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900216 /* There is no probe, return original address */
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400217 if (!kp)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900218 return addr;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400219
220 /*
221 * Basically, kp->ainsn.insn has an original instruction.
222 * However, RIP-relative instruction can not do single-stepping
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500223 * at different place, __copy_instruction() tweaks the displacement of
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400224 * that instruction. In that case, we can't recover the instruction
225 * from the kp->ainsn.insn.
226 *
227 * On the other hand, kp->opcode has a copy of the first byte of
228 * the probed instruction, which is overwritten by int3. And
229 * the instruction at kp->addr is not modified by kprobes except
230 * for the first byte, we can recover the original instruction
231 * from it and kp->opcode.
232 */
233 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
234 buf[0] = kp->opcode;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900235 return (unsigned long)buf;
236}
237
238#ifdef CONFIG_OPTPROBES
239static unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf,
240 unsigned long addr)
241{
242 struct optimized_kprobe *op;
243 struct kprobe *kp;
244 long offs;
245 int i;
246
247 for (i = 0; i < RELATIVEJUMP_SIZE; i++) {
248 kp = get_kprobe((void *)addr - i);
249 /* This function only handles jump-optimized kprobe */
250 if (kp && kprobe_optimized(kp)) {
251 op = container_of(kp, struct optimized_kprobe, kp);
252 /* If op->list is not empty, op is under optimizing */
253 if (list_empty(&op->list))
254 goto found;
255 }
256 }
257
258 return addr;
259found:
260 /*
261 * If the kprobe can be optimized, original bytes which can be
262 * overwritten by jump destination address. In this case, original
263 * bytes must be recovered from op->optinsn.copied_insn buffer.
264 */
265 memcpy(buf, (void *)addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
266 if (addr == (unsigned long)kp->addr) {
267 buf[0] = kp->opcode;
268 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
269 } else {
270 offs = addr - (unsigned long)kp->addr - 1;
271 memcpy(buf, op->optinsn.copied_insn + offs, RELATIVE_ADDR_SIZE - offs);
272 }
273
274 return (unsigned long)buf;
275}
276#else
277static inline unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf,
278 unsigned long addr)
279{
280 return addr;
281}
282#endif
283
284/*
285 * Recover the probed instruction at addr for further analysis.
286 * Caller must lock kprobes by kprobe_mutex, or disable preemption
287 * for preventing to release referencing kprobes.
288 */
289static unsigned long recover_probed_instruction(kprobe_opcode_t *buf,
290 unsigned long addr)
291{
292 unsigned long __addr;
293
294 __addr = __recover_optprobed_insn(buf, addr);
295 if (__addr != addr)
296 return __addr;
297
298 return __recover_probed_insn(buf, addr);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400299}
300
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400301/* Check if paddr is at an instruction boundary */
302static int __kprobes can_probe(unsigned long paddr)
303{
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900304 unsigned long addr, __addr, offset = 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400305 struct insn insn;
306 kprobe_opcode_t buf[MAX_INSN_SIZE];
307
Namhyung Kim6abded72010-09-15 10:04:29 +0900308 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400309 return 0;
310
311 /* Decode instructions */
312 addr = paddr - offset;
313 while (addr < paddr) {
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400314 /*
315 * Check if the instruction has been modified by another
316 * kprobe, in which case we replace the breakpoint by the
317 * original instruction in our buffer.
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900318 * Also, jump optimization will change the breakpoint to
319 * relative-jump. Since the relative-jump itself is
320 * normally used, we just go through if there is no kprobe.
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400321 */
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900322 __addr = recover_probed_instruction(buf, addr);
323 kernel_insn_init(&insn, (void *)__addr);
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400324 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900325
326 /*
327 * Another debugging subsystem might insert this breakpoint.
328 * In that case, we can't recover it.
329 */
330 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
331 return 0;
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400332 addr += insn.length;
333 }
334
335 return (addr == paddr);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100339 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Andrew Morton86454192007-11-26 20:42:19 +0100341static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900343 /* Skip prefixes */
344 insn = skip_prefixes(insn);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 switch (*insn) {
347 case 0xfa: /* cli */
348 case 0xfb: /* sti */
349 case 0xcf: /* iret/iretd */
350 case 0x9d: /* popf/popfd */
351 return 1;
352 }
Harvey Harrison99309272008-01-30 13:32:14 +0100353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
357/*
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500358 * Copy an instruction and adjust the displacement if the instruction
359 * uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100360 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100362 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500364static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400366 struct insn insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500367 kprobe_opcode_t buf[MAX_INSN_SIZE];
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900368 u8 *orig_src = src; /* Back up original src for RIP calculation */
369
370 if (recover)
371 src = (u8 *)recover_probed_instruction(buf, (unsigned long)src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500373 kernel_insn_init(&insn, src);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500374 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900375 /* Another subsystem puts a breakpoint, failed to recover */
376 if (recover && insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
377 return 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500378 memcpy(dest, insn.kaddr, insn.length);
379
380#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400381 if (insn_rip_relative(&insn)) {
382 s64 newdisp;
383 u8 *disp;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500384 kernel_insn_init(&insn, dest);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400385 insn_get_displacement(&insn);
386 /*
387 * The copied instruction uses the %rip-relative addressing
388 * mode. Adjust the displacement for the difference between
389 * the original location of this instruction and the location
390 * of the copy that will actually be run. The tricky bit here
391 * is making sure that the sign extension happens correctly in
392 * this calculation, since we need a signed 32-bit result to
393 * be sign-extended to 64 bits when it's added to the %rip
394 * value and yield the same 64-bit result that the sign-
395 * extension of the original signed 32-bit displacement would
396 * have given.
397 */
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900398 newdisp = (u8 *) orig_src + (s64) insn.displacement.value - (u8 *) dest;
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400399 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500400 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400401 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100403#endif
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500404 return insn.length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800407static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500409 /*
410 * Copy an instruction without recovering int3, because it will be
411 * put by another subsystem.
412 */
413 __copy_instruction(p->ainsn.insn, p->addr, 0);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100414
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100415 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100416 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100417 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100418 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100419
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700420 p->opcode = *p->addr;
421}
422
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100423int __kprobes arch_prepare_kprobe(struct kprobe *p)
424{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500425 if (alternatives_text_reserved(p->addr, p->addr))
426 return -EINVAL;
427
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400428 if (!can_probe((unsigned long)p->addr))
429 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100430 /* insn: must be on special executable page on x86. */
431 p->ainsn.insn = get_insn_slot();
432 if (!p->ainsn.insn)
433 return -ENOMEM;
434 arch_copy_kprobe(p);
435 return 0;
436}
437
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700438void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700439{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200440 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700441}
442
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700443void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700444{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200445 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800448void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800450 if (p->ainsn.insn) {
451 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
452 p->ainsn.insn = NULL;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700456static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700457{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800458 kcb->prev_kprobe.kp = kprobe_running();
459 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100460 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
461 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700462}
463
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700464static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700465{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600466 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800467 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100468 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
469 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700470}
471
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700472static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800473 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700474{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600475 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100476 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100477 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700478 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100479 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700480}
481
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100482static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100483{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100484 if (test_thread_flag(TIF_BLOCKSTEP)) {
485 unsigned long debugctl = get_debugctlmsr();
486
487 debugctl &= ~DEBUGCTLMSR_BTF;
488 update_debugctlmsr(debugctl);
489 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100490}
491
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100492static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100493{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100494 if (test_thread_flag(TIF_BLOCKSTEP)) {
495 unsigned long debugctl = get_debugctlmsr();
496
497 debugctl |= DEBUGCTLMSR_BTF;
498 update_debugctlmsr(debugctl);
499 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100500}
501
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700502void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700503 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700504{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100505 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700506
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700507 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100508
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700509 /* Replace the return addr with trampoline addr */
510 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700511}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100512
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500513#ifdef CONFIG_OPTPROBES
514static int __kprobes setup_detour_execution(struct kprobe *p,
515 struct pt_regs *regs,
516 int reenter);
517#else
518#define setup_detour_execution(p, regs, reenter) (0)
519#endif
520
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100521static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500522 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100523{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500524 if (setup_detour_execution(p, regs, reenter))
525 return;
526
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500527#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100528 if (p->ainsn.boostable == 1 && !p->post_handler) {
529 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500530 if (!reenter)
531 reset_current_kprobe();
532 /*
533 * Reentering boosted probe doesn't reset current_kprobe,
534 * nor set current_kprobe, because it doesn't use single
535 * stepping.
536 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100537 regs->ip = (unsigned long)p->ainsn.insn;
538 preempt_enable_no_resched();
539 return;
540 }
541#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500542 if (reenter) {
543 save_previous_kprobe(kcb);
544 set_current_kprobe(p, regs, kcb);
545 kcb->kprobe_status = KPROBE_REENTER;
546 } else
547 kcb->kprobe_status = KPROBE_HIT_SS;
548 /* Prepare real single stepping */
549 clear_btf();
550 regs->flags |= X86_EFLAGS_TF;
551 regs->flags &= ~X86_EFLAGS_IF;
552 /* single step inline if the instruction is an int3 */
553 if (p->opcode == BREAKPOINT_INSTRUCTION)
554 regs->ip = (unsigned long)p->addr;
555 else
556 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100557}
558
Harvey Harrison40102d42008-01-30 13:32:02 +0100559/*
560 * We have reentered the kprobe_handler(), since another probe was hit while
561 * within the handler. We save the original kprobes variables and just single
562 * step on the instruction of the new probe without calling any user handlers.
563 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100564static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
565 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100566{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100567 switch (kcb->kprobe_status) {
568 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100569 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100570 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500571 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100572 break;
573 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400574 /* A probe has been hit in the codepath leading up to, or just
575 * after, single-stepping of a probed instruction. This entire
576 * codepath should strictly reside in .kprobes.text section.
577 * Raise a BUG or we'll continue in an endless reentering loop
578 * and eventually a stack overflow.
579 */
580 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
581 p->addr);
582 dump_kprobe(p);
583 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100584 default:
585 /* impossible cases */
586 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100587 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100588 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100589
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100590 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100591}
Rusty Lynch73649da2005-06-23 00:09:23 -0700592
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100593/*
594 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200595 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100596 */
597static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100599 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100600 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800601 struct kprobe_ctlblk *kcb;
602
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100603 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800604 /*
605 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100606 * duration of kprobe processing. We conditionally
607 * re-enable preemption at the end of this function,
608 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800609 */
610 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100612 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100613 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100614
Harvey Harrisonb9760152008-01-30 13:32:19 +0100615 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100616 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100617 if (reenter_kprobe(p, regs, kcb))
618 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100619 } else {
620 set_current_kprobe(p, regs, kcb);
621 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100624 * If we have no pre-handler or it returned 0, we
625 * continue with normal processing. If we have a
626 * pre-handler and it returned non-zero, it prepped
627 * for calling the break_handler below on re-entry
628 * for jprobe processing, so get out doing nothing
629 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100631 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500632 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100633 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100634 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400635 } else if (*addr != BREAKPOINT_INSTRUCTION) {
636 /*
637 * The breakpoint instruction was removed right
638 * after we hit it. Another cpu has removed
639 * either a probepoint or a debugger breakpoint
640 * at this address. In either case, no further
641 * handling of this interrupt is appropriate.
642 * Back up over the (now missing) int3 and run
643 * the original instruction.
644 */
645 regs->ip = (unsigned long)addr;
646 preempt_enable_no_resched();
647 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100648 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600649 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100650 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500651 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100652 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100654 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800656 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500660#ifdef CONFIG_X86_64
661#define SAVE_REGS_STRING \
662 /* Skip cs, ip, orig_ax. */ \
663 " subq $24, %rsp\n" \
664 " pushq %rdi\n" \
665 " pushq %rsi\n" \
666 " pushq %rdx\n" \
667 " pushq %rcx\n" \
668 " pushq %rax\n" \
669 " pushq %r8\n" \
670 " pushq %r9\n" \
671 " pushq %r10\n" \
672 " pushq %r11\n" \
673 " pushq %rbx\n" \
674 " pushq %rbp\n" \
675 " pushq %r12\n" \
676 " pushq %r13\n" \
677 " pushq %r14\n" \
678 " pushq %r15\n"
679#define RESTORE_REGS_STRING \
680 " popq %r15\n" \
681 " popq %r14\n" \
682 " popq %r13\n" \
683 " popq %r12\n" \
684 " popq %rbp\n" \
685 " popq %rbx\n" \
686 " popq %r11\n" \
687 " popq %r10\n" \
688 " popq %r9\n" \
689 " popq %r8\n" \
690 " popq %rax\n" \
691 " popq %rcx\n" \
692 " popq %rdx\n" \
693 " popq %rsi\n" \
694 " popq %rdi\n" \
695 /* Skip orig_ax, ip, cs */ \
696 " addq $24, %rsp\n"
697#else
698#define SAVE_REGS_STRING \
699 /* Skip cs, ip, orig_ax and gs. */ \
700 " subl $16, %esp\n" \
701 " pushl %fs\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500702 " pushl %es\n" \
Roland McGratha1974792010-07-16 18:17:12 -0700703 " pushl %ds\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500704 " pushl %eax\n" \
705 " pushl %ebp\n" \
706 " pushl %edi\n" \
707 " pushl %esi\n" \
708 " pushl %edx\n" \
709 " pushl %ecx\n" \
710 " pushl %ebx\n"
711#define RESTORE_REGS_STRING \
712 " popl %ebx\n" \
713 " popl %ecx\n" \
714 " popl %edx\n" \
715 " popl %esi\n" \
716 " popl %edi\n" \
717 " popl %ebp\n" \
718 " popl %eax\n" \
719 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
720 " addl $24, %esp\n"
721#endif
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100724 * When a retprobed function returns, this code saves registers and
725 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700726 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800727static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100728{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100729 asm volatile (
730 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100731 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100732#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100733 /* We don't bother saving the ss register */
734 " pushq %rsp\n"
735 " pushfq\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500736 SAVE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100737 " movq %rsp, %rdi\n"
738 " call trampoline_handler\n"
739 /* Replace saved sp with true return address. */
740 " movq %rax, 152(%rsp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500741 RESTORE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100742 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100743#else
744 " pushf\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500745 SAVE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100746 " movl %esp, %eax\n"
747 " call trampoline_handler\n"
748 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400749 " movl 56(%esp), %edx\n"
750 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100751 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400752 " movl %eax, 56(%esp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500753 RESTORE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100754 " popf\n"
755#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100756 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100757}
Rusty Lynch73649da2005-06-23 00:09:23 -0700758
759/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100760 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700761 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800762static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700763{
bibo,mao62c27be2006-10-02 02:17:33 -0700764 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700765 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700766 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800767 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100768 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900769 kprobe_opcode_t *correct_ret_addr = NULL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700770
bibo,mao99219a32006-10-02 02:17:35 -0700771 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700772 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100773 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100774#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100775 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100776#else
777 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400778 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100779#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100780 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100781 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700782
Rusty Lynchba8af122005-06-27 15:17:10 -0700783 /*
784 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100785 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200786 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700787 * return probe was registered for a target function.
788 *
789 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100790 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700791 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100792 * function, the (chronologically) first instance's ret_addr
793 * will be the real return address, and all the rest will
794 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700795 */
796 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700797 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700798 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700799 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700800
Rusty Lynchba8af122005-06-27 15:17:10 -0700801 orig_ret_address = (unsigned long)ri->ret_addr;
Rusty Lynchba8af122005-06-27 15:17:10 -0700802
803 if (orig_ret_address != trampoline_address)
804 /*
805 * This is the real return address. Any other
806 * instances associated with this task are for
807 * other calls deeper on the call stack
808 */
809 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700810 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700811
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700812 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700813
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900814 correct_ret_addr = ri->ret_addr;
815 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
816 if (ri->task != current)
817 /* another task is sharing our hash bucket */
818 continue;
819
820 orig_ret_address = (unsigned long)ri->ret_addr;
821 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600822 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900823 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
824 ri->ret_addr = correct_ret_addr;
825 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600826 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900827 }
828
829 recycle_rp_inst(ri, &empty_rp);
830
831 if (orig_ret_address != trampoline_address)
832 /*
833 * This is the real return address. Any other
834 * instances associated with this task are for
835 * other calls deeper on the call stack
836 */
837 break;
838 }
839
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700840 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700841
bibo,mao99219a32006-10-02 02:17:35 -0700842 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
843 hlist_del(&ri->hlist);
844 kfree(ri);
845 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100846 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700847}
848
849/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * Called after single-stepping. p->addr is the address of the
851 * instruction whose first byte has been replaced by the "int 3"
852 * instruction. To avoid the SMP problems that can occur when we
853 * temporarily put back the original opcode to single-step, we
854 * single-stepped a copy of the instruction. The address of this
855 * copy is p->ainsn.insn.
856 *
857 * This function prepares to return from the post-single-step
858 * interrupt. We have to fix up the stack as follows:
859 *
860 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100861 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * it relative to the original instruction.
863 *
864 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100865 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *
867 * 2) If the single-stepped instruction was a call, the return address
868 * that is atop the stack is the address following the copied instruction.
869 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100870 *
871 * If this is the first time we've single-stepped the instruction at
872 * this probepoint, and the instruction is boostable, boost it: add a
873 * jump instruction after the copied instruction, that jumps to the next
874 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800876static void __kprobes resume_execution(struct kprobe *p,
877 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100879 unsigned long *tos = stack_addr(regs);
880 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
881 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 kprobe_opcode_t *insn = p->ainsn.insn;
883
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900884 /* Skip prefixes */
885 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100887 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100889 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100890 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100891 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100893 case 0xc2: /* iret/ret/lret */
894 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700895 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100896 case 0xcb:
897 case 0xcf:
898 case 0xea: /* jmp absolute -- ip is correct */
899 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100900 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100901 goto no_change;
902 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100903 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100905#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100906 case 0x9a: /* call absolute -- same as call absolute, indirect */
907 *tos = orig_ip + (*tos - copy_ip);
908 goto no_change;
909#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700911 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100912 /*
913 * call absolute, indirect
914 * Fix return addr; ip is correct.
915 * But this is not boostable
916 */
917 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100918 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100919 } else if (((insn[1] & 0x31) == 0x20) ||
920 ((insn[1] & 0x31) == 0x21)) {
921 /*
922 * jmp near and far, absolute indirect
923 * ip is correct. And this is boostable
924 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100925 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100926 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 default:
929 break;
930 }
931
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100932 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100933 if ((regs->ip > copy_ip) &&
934 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100935 /*
936 * These instructions can be executed directly if it
937 * jumps back to correct address.
938 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500939 synthesize_reljump((void *)regs->ip,
940 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100941 p->ainsn.boostable = 1;
942 } else {
943 p->ainsn.boostable = -1;
944 }
945 }
946
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100947 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100948
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100949no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100950 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100953/*
954 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200955 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100956 */
957static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800959 struct kprobe *cur = kprobe_running();
960 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
961
962 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return 0;
964
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500965 resume_execution(cur, regs, kcb);
966 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500967
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800968 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
969 kcb->kprobe_status = KPROBE_HIT_SSDONE;
970 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100973 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800974 if (kcb->kprobe_status == KPROBE_REENTER) {
975 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700976 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700977 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800978 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 preempt_enable_no_resched();
981
982 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100983 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * will have TF set, in which case, continue the remaining processing
985 * of do_debug, as if this is not a probe hit.
986 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100987 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 0;
989
990 return 1;
991}
992
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700993int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800995 struct kprobe *cur = kprobe_running();
996 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
997
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100998 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800999 case KPROBE_HIT_SS:
1000 case KPROBE_REENTER:
1001 /*
1002 * We are here because the instruction being single
1003 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001004 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001005 * and allow the page fault handler to continue as a
1006 * normal page fault.
1007 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001008 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001009 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001010 if (kcb->kprobe_status == KPROBE_REENTER)
1011 restore_previous_kprobe(kcb);
1012 else
1013 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001015 break;
1016 case KPROBE_HIT_ACTIVE:
1017 case KPROBE_HIT_SSDONE:
1018 /*
1019 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001020 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001021 * these specific fault cases.
1022 */
1023 kprobes_inc_nmissed_count(cur);
1024
1025 /*
1026 * We come here because instructions in the pre/post
1027 * handler caused the page_fault, this could happen
1028 * if handler tries to access user space by
1029 * copy_from_user(), get_user() etc. Let the
1030 * user-specified handler try to fix it first.
1031 */
1032 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1033 return 1;
1034
1035 /*
1036 * In case the user-specified fault handler returned
1037 * zero, try to fix up.
1038 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001039 if (fixup_exception(regs))
1040 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +01001041
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001042 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001043 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001044 * Let do_page_fault() fix it.
1045 */
1046 break;
1047 default:
1048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050 return 0;
1051}
1052
1053/*
1054 * Wrapper routine for handling exceptions.
1055 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001056int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
1057 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Jan Engelhardtade1af72008-01-30 13:33:23 +01001059 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001060 int ret = NOTIFY_DONE;
1061
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001062 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001063 return ret;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 switch (val) {
1066 case DIE_INT3:
1067 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001068 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
1070 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +05301071 if (post_kprobe_handler(args->regs)) {
1072 /*
1073 * Reset the BS bit in dr6 (pointed by args->err) to
1074 * denote completion of processing
1075 */
1076 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001077 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +05301078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 break;
1080 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001081 /*
1082 * To be potentially processing a kprobe fault and to
1083 * trust the result from kprobe_running(), we have
1084 * be non-preemptible.
1085 */
1086 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001088 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 break;
1090 default:
1091 break;
1092 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001093 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001096int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 struct jprobe *jp = container_of(p, struct jprobe, kp);
1099 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001100 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001102 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001103 kcb->jprobe_saved_sp = stack_addr(regs);
1104 addr = (unsigned long)(kcb->jprobe_saved_sp);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * As Linus pointed out, gcc assumes that the callee
1108 * owns the argument space and could overwrite it, e.g.
1109 * tailcall optimization. So, to be absolutely safe
1110 * we also save and restore enough stack bytes to cover
1111 * the argument area.
1112 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001113 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001114 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001115 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001116 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001117 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 1;
1119}
1120
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001121void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001123 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1124
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001125 asm volatile (
1126#ifdef CONFIG_X86_64
1127 " xchg %%rbx,%%rsp \n"
1128#else
1129 " xchgl %%ebx,%%esp \n"
1130#endif
1131 " int3 \n"
1132 " .globl jprobe_return_end\n"
1133 " jprobe_return_end: \n"
1134 " nop \n"::"b"
1135 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001138int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001140 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001141 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct jprobe *jp = container_of(p, struct jprobe, kp);
1143
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001144 if ((addr > (u8 *) jprobe_return) &&
1145 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001146 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001147 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001148 printk(KERN_ERR
1149 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001150 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001151 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001153 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 show_registers(regs);
1155 BUG();
1156 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001157 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001158 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1159 kcb->jprobes_stack,
1160 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001161 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return 1;
1163 }
1164 return 0;
1165}
Rusty Lynchba8af122005-06-27 15:17:10 -07001166
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001167
1168#ifdef CONFIG_OPTPROBES
1169
1170/* Insert a call instruction at address 'from', which calls address 'to'.*/
1171static void __kprobes synthesize_relcall(void *from, void *to)
1172{
1173 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1174}
1175
1176/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1177static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1178 unsigned long val)
1179{
1180#ifdef CONFIG_X86_64
1181 *addr++ = 0x48;
1182 *addr++ = 0xbf;
1183#else
1184 *addr++ = 0xb8;
1185#endif
1186 *(unsigned long *)addr = val;
1187}
1188
Namhyung Kim6376b222010-09-15 10:04:28 +09001189static void __used __kprobes kprobes_optinsn_template_holder(void)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001190{
1191 asm volatile (
1192 ".global optprobe_template_entry\n"
1193 "optprobe_template_entry: \n"
1194#ifdef CONFIG_X86_64
1195 /* We don't bother saving the ss register */
1196 " pushq %rsp\n"
1197 " pushfq\n"
1198 SAVE_REGS_STRING
1199 " movq %rsp, %rsi\n"
1200 ".global optprobe_template_val\n"
1201 "optprobe_template_val: \n"
1202 ASM_NOP5
1203 ASM_NOP5
1204 ".global optprobe_template_call\n"
1205 "optprobe_template_call: \n"
1206 ASM_NOP5
1207 /* Move flags to rsp */
1208 " movq 144(%rsp), %rdx\n"
1209 " movq %rdx, 152(%rsp)\n"
1210 RESTORE_REGS_STRING
1211 /* Skip flags entry */
1212 " addq $8, %rsp\n"
1213 " popfq\n"
1214#else /* CONFIG_X86_32 */
1215 " pushf\n"
1216 SAVE_REGS_STRING
1217 " movl %esp, %edx\n"
1218 ".global optprobe_template_val\n"
1219 "optprobe_template_val: \n"
1220 ASM_NOP5
1221 ".global optprobe_template_call\n"
1222 "optprobe_template_call: \n"
1223 ASM_NOP5
1224 RESTORE_REGS_STRING
1225 " addl $4, %esp\n" /* skip cs */
1226 " popf\n"
1227#endif
1228 ".global optprobe_template_end\n"
1229 "optprobe_template_end: \n");
1230}
1231
1232#define TMPL_MOVE_IDX \
1233 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1234#define TMPL_CALL_IDX \
1235 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1236#define TMPL_END_IDX \
1237 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1238
1239#define INT3_SIZE sizeof(kprobe_opcode_t)
1240
1241/* Optimized kprobe call back function: called from optinsn */
1242static void __kprobes optimized_callback(struct optimized_kprobe *op,
1243 struct pt_regs *regs)
1244{
1245 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001246 unsigned long flags;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001247
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001248 /* This is possible if op is under delayed unoptimizing */
1249 if (kprobe_disabled(&op->kp))
1250 return;
1251
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001252 local_irq_save(flags);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001253 if (kprobe_running()) {
1254 kprobes_inc_nmissed_count(&op->kp);
1255 } else {
1256 /* Save skipped registers */
1257#ifdef CONFIG_X86_64
1258 regs->cs = __KERNEL_CS;
1259#else
1260 regs->cs = __KERNEL_CS | get_kernel_rpl();
1261 regs->gs = 0;
1262#endif
1263 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1264 regs->orig_ax = ~0UL;
1265
Christoph Lameterb76834b2010-12-06 11:16:25 -06001266 __this_cpu_write(current_kprobe, &op->kp);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001267 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1268 opt_pre_handler(&op->kp, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -06001269 __this_cpu_write(current_kprobe, NULL);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001270 }
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001271 local_irq_restore(flags);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001272}
1273
1274static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1275{
1276 int len = 0, ret;
1277
1278 while (len < RELATIVEJUMP_SIZE) {
1279 ret = __copy_instruction(dest + len, src + len, 1);
1280 if (!ret || !can_boost(dest + len))
1281 return -EINVAL;
1282 len += ret;
1283 }
1284 /* Check whether the address range is reserved */
1285 if (ftrace_text_reserved(src, src + len - 1) ||
Jason Baron4c3ef6d2010-09-17 11:09:08 -04001286 alternatives_text_reserved(src, src + len - 1) ||
1287 jump_label_text_reserved(src, src + len - 1))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001288 return -EBUSY;
1289
1290 return len;
1291}
1292
1293/* Check whether insn is indirect jump */
1294static int __kprobes insn_is_indirect_jump(struct insn *insn)
1295{
1296 return ((insn->opcode.bytes[0] == 0xff &&
1297 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1298 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1299}
1300
1301/* Check whether insn jumps into specified address range */
1302static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1303{
1304 unsigned long target = 0;
1305
1306 switch (insn->opcode.bytes[0]) {
1307 case 0xe0: /* loopne */
1308 case 0xe1: /* loope */
1309 case 0xe2: /* loop */
1310 case 0xe3: /* jcxz */
1311 case 0xe9: /* near relative jump */
1312 case 0xeb: /* short relative jump */
1313 break;
1314 case 0x0f:
1315 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1316 break;
1317 return 0;
1318 default:
1319 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1320 break;
1321 return 0;
1322 }
1323 target = (unsigned long)insn->next_byte + insn->immediate.value;
1324
1325 return (start <= target && target <= start + len);
1326}
1327
1328/* Decode whole function to ensure any instructions don't jump into target */
1329static int __kprobes can_optimize(unsigned long paddr)
1330{
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +09001331 unsigned long addr, __addr, size = 0, offset = 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001332 struct insn insn;
1333 kprobe_opcode_t buf[MAX_INSN_SIZE];
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001334
1335 /* Lookup symbol including addr */
Namhyung Kim6abded72010-09-15 10:04:29 +09001336 if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001337 return 0;
1338
Jiri Olsa2a8247a2011-02-21 15:25:13 +01001339 /*
1340 * Do not optimize in the entry code due to the unstable
1341 * stack handling.
1342 */
1343 if ((paddr >= (unsigned long )__entry_text_start) &&
1344 (paddr < (unsigned long )__entry_text_end))
1345 return 0;
1346
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001347 /* Check there is enough space for a relative jump. */
1348 if (size - offset < RELATIVEJUMP_SIZE)
1349 return 0;
1350
1351 /* Decode instructions */
1352 addr = paddr - offset;
1353 while (addr < paddr - offset + size) { /* Decode until function end */
1354 if (search_exception_tables(addr))
1355 /*
1356 * Since some fixup code will jumps into this function,
1357 * we can't optimize kprobe in this function.
1358 */
1359 return 0;
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +09001360 __addr = recover_probed_instruction(buf, addr);
1361 kernel_insn_init(&insn, (void *)__addr);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001362 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +09001363 /* Another subsystem puts a breakpoint */
1364 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
1365 return 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001366 /* Recover address */
1367 insn.kaddr = (void *)addr;
1368 insn.next_byte = (void *)(addr + insn.length);
1369 /* Check any instructions don't jump into target */
1370 if (insn_is_indirect_jump(&insn) ||
1371 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1372 RELATIVE_ADDR_SIZE))
1373 return 0;
1374 addr += insn.length;
1375 }
1376
1377 return 1;
1378}
1379
1380/* Check optimized_kprobe can actually be optimized. */
1381int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1382{
1383 int i;
1384 struct kprobe *p;
1385
1386 for (i = 1; i < op->optinsn.size; i++) {
1387 p = get_kprobe(op->kp.addr + i);
1388 if (p && !kprobe_disabled(p))
1389 return -EEXIST;
1390 }
1391
1392 return 0;
1393}
1394
1395/* Check the addr is within the optimized instructions. */
1396int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1397 unsigned long addr)
1398{
1399 return ((unsigned long)op->kp.addr <= addr &&
1400 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1401}
1402
1403/* Free optimized instruction slot */
1404static __kprobes
1405void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1406{
1407 if (op->optinsn.insn) {
1408 free_optinsn_slot(op->optinsn.insn, dirty);
1409 op->optinsn.insn = NULL;
1410 op->optinsn.size = 0;
1411 }
1412}
1413
1414void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1415{
1416 __arch_remove_optimized_kprobe(op, 1);
1417}
1418
1419/*
1420 * Copy replacing target instructions
1421 * Target instructions MUST be relocatable (checked inside)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +09001422 * This is called when new aggr(opt)probe is allocated or reused.
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001423 */
1424int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1425{
1426 u8 *buf;
1427 int ret;
1428 long rel;
1429
1430 if (!can_optimize((unsigned long)op->kp.addr))
1431 return -EILSEQ;
1432
1433 op->optinsn.insn = get_optinsn_slot();
1434 if (!op->optinsn.insn)
1435 return -ENOMEM;
1436
1437 /*
1438 * Verify if the address gap is in 2GB range, because this uses
1439 * a relative jump.
1440 */
1441 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1442 if (abs(rel) > 0x7fffffff)
1443 return -ERANGE;
1444
1445 buf = (u8 *)op->optinsn.insn;
1446
1447 /* Copy instructions into the out-of-line buffer */
1448 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1449 if (ret < 0) {
1450 __arch_remove_optimized_kprobe(op, 0);
1451 return ret;
1452 }
1453 op->optinsn.size = ret;
1454
1455 /* Copy arch-dep-instance from template */
1456 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1457
1458 /* Set probe information */
1459 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1460
1461 /* Set probe function call */
1462 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1463
1464 /* Set returning jmp instruction at the tail of out-of-line buffer */
1465 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1466 (u8 *)op->kp.addr + op->optinsn.size);
1467
1468 flush_icache_range((unsigned long) buf,
1469 (unsigned long) buf + TMPL_END_IDX +
1470 op->optinsn.size + RELATIVEJUMP_SIZE);
1471 return 0;
1472}
1473
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001474#define MAX_OPTIMIZE_PROBES 256
1475static struct text_poke_param *jump_poke_params;
1476static struct jump_poke_buffer {
1477 u8 buf[RELATIVEJUMP_SIZE];
1478} *jump_poke_bufs;
1479
1480static void __kprobes setup_optimize_kprobe(struct text_poke_param *tprm,
1481 u8 *insn_buf,
1482 struct optimized_kprobe *op)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001483{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001484 s32 rel = (s32)((long)op->optinsn.insn -
1485 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1486
1487 /* Backup instructions which will be replaced by jump address */
1488 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1489 RELATIVE_ADDR_SIZE);
1490
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001491 insn_buf[0] = RELATIVEJUMP_OPCODE;
1492 *(s32 *)(&insn_buf[1]) = rel;
1493
1494 tprm->addr = op->kp.addr;
1495 tprm->opcode = insn_buf;
1496 tprm->len = RELATIVEJUMP_SIZE;
1497}
1498
1499/*
1500 * Replace breakpoints (int3) with relative jumps.
1501 * Caller must call with locking kprobe_mutex and text_mutex.
1502 */
1503void __kprobes arch_optimize_kprobes(struct list_head *oplist)
1504{
1505 struct optimized_kprobe *op, *tmp;
1506 int c = 0;
1507
1508 list_for_each_entry_safe(op, tmp, oplist, list) {
1509 WARN_ON(kprobe_disabled(&op->kp));
1510 /* Setup param */
1511 setup_optimize_kprobe(&jump_poke_params[c],
1512 jump_poke_bufs[c].buf, op);
1513 list_del_init(&op->list);
1514 if (++c >= MAX_OPTIMIZE_PROBES)
1515 break;
1516 }
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001517
1518 /*
1519 * text_poke_smp doesn't support NMI/MCE code modifying.
1520 * However, since kprobes itself also doesn't support NMI/MCE
1521 * code probing, it's not a problem.
1522 */
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001523 text_poke_smp_batch(jump_poke_params, c);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001524}
1525
Masami Hiramatsuf984ba42010-12-03 18:54:34 +09001526static void __kprobes setup_unoptimize_kprobe(struct text_poke_param *tprm,
1527 u8 *insn_buf,
1528 struct optimized_kprobe *op)
1529{
1530 /* Set int3 to first byte for kprobes */
1531 insn_buf[0] = BREAKPOINT_INSTRUCTION;
1532 memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1533
1534 tprm->addr = op->kp.addr;
1535 tprm->opcode = insn_buf;
1536 tprm->len = RELATIVEJUMP_SIZE;
1537}
1538
1539/*
1540 * Recover original instructions and breakpoints from relative jumps.
1541 * Caller must call with locking kprobe_mutex.
1542 */
1543extern void arch_unoptimize_kprobes(struct list_head *oplist,
1544 struct list_head *done_list)
1545{
1546 struct optimized_kprobe *op, *tmp;
1547 int c = 0;
1548
1549 list_for_each_entry_safe(op, tmp, oplist, list) {
1550 /* Setup param */
1551 setup_unoptimize_kprobe(&jump_poke_params[c],
1552 jump_poke_bufs[c].buf, op);
1553 list_move(&op->list, done_list);
1554 if (++c >= MAX_OPTIMIZE_PROBES)
1555 break;
1556 }
1557
1558 /*
1559 * text_poke_smp doesn't support NMI/MCE code modifying.
1560 * However, since kprobes itself also doesn't support NMI/MCE
1561 * code probing, it's not a problem.
1562 */
1563 text_poke_smp_batch(jump_poke_params, c);
1564}
1565
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001566/* Replace a relative jump with a breakpoint (int3). */
1567void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1568{
1569 u8 buf[RELATIVEJUMP_SIZE];
1570
1571 /* Set int3 to first byte for kprobes */
1572 buf[0] = BREAKPOINT_INSTRUCTION;
1573 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1574 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1575}
1576
1577static int __kprobes setup_detour_execution(struct kprobe *p,
1578 struct pt_regs *regs,
1579 int reenter)
1580{
1581 struct optimized_kprobe *op;
1582
1583 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1584 /* This kprobe is really able to run optimized path. */
1585 op = container_of(p, struct optimized_kprobe, kp);
1586 /* Detour through copied instructions */
1587 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1588 if (!reenter)
1589 reset_current_kprobe();
1590 preempt_enable_no_resched();
1591 return 1;
1592 }
1593 return 0;
1594}
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001595
1596static int __kprobes init_poke_params(void)
1597{
1598 /* Allocate code buffer and parameter array */
1599 jump_poke_bufs = kmalloc(sizeof(struct jump_poke_buffer) *
1600 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1601 if (!jump_poke_bufs)
1602 return -ENOMEM;
1603
1604 jump_poke_params = kmalloc(sizeof(struct text_poke_param) *
1605 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1606 if (!jump_poke_params) {
1607 kfree(jump_poke_bufs);
1608 jump_poke_bufs = NULL;
1609 return -ENOMEM;
1610 }
1611
1612 return 0;
1613}
1614#else /* !CONFIG_OPTPROBES */
1615static int __kprobes init_poke_params(void)
1616{
1617 return 0;
1618}
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001619#endif
1620
Rusty Lynch67729262005-07-05 18:54:50 -07001621int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001622{
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001623 return init_poke_params();
Rusty Lynchba8af122005-06-27 15:17:10 -07001624}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001625
1626int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1627{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001628 return 0;
1629}