blob: ca6d450bee7e3d4bf7182c78e625bdc36e354da5 [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 Hiramatsu46484682012-03-05 22:32:16 +0900364static int __kprobes __copy_instruction(u8 *dest, u8 *src)
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
Masami Hiramatsu46484682012-03-05 22:32:16 +0900369 kernel_insn_init(&insn, (void *)recover_probed_instruction(buf, (unsigned long)src));
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500370 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900371 /* Another subsystem puts a breakpoint, failed to recover */
Masami Hiramatsu46484682012-03-05 22:32:16 +0900372 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +0900373 return 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500374 memcpy(dest, insn.kaddr, insn.length);
375
376#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400377 if (insn_rip_relative(&insn)) {
378 s64 newdisp;
379 u8 *disp;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500380 kernel_insn_init(&insn, dest);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400381 insn_get_displacement(&insn);
382 /*
383 * The copied instruction uses the %rip-relative addressing
384 * mode. Adjust the displacement for the difference between
385 * the original location of this instruction and the location
386 * of the copy that will actually be run. The tricky bit here
387 * is making sure that the sign extension happens correctly in
388 * this calculation, since we need a signed 32-bit result to
389 * be sign-extended to 64 bits when it's added to the %rip
390 * value and yield the same 64-bit result that the sign-
391 * extension of the original signed 32-bit displacement would
392 * have given.
393 */
Masami Hiramatsu46484682012-03-05 22:32:16 +0900394 newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400395 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500396 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400397 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100399#endif
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500400 return insn.length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800403static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Masami Hiramatsu46484682012-03-05 22:32:16 +0900405 /* Copy an instruction with recovering if other optprobe modifies it.*/
406 __copy_instruction(p->ainsn.insn, p->addr);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100407
Masami Hiramatsu46484682012-03-05 22:32:16 +0900408 /*
409 * __copy_instruction can modify the displacement of the instruction,
410 * but it doesn't affect boostable check.
411 */
412 if (can_boost(p->ainsn.insn))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100413 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100414 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100415 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100416
Masami Hiramatsu46484682012-03-05 22:32:16 +0900417 /* Also, displacement change doesn't affect the first byte */
418 p->opcode = p->ainsn.insn[0];
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700419}
420
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100421int __kprobes arch_prepare_kprobe(struct kprobe *p)
422{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500423 if (alternatives_text_reserved(p->addr, p->addr))
424 return -EINVAL;
425
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400426 if (!can_probe((unsigned long)p->addr))
427 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100428 /* insn: must be on special executable page on x86. */
429 p->ainsn.insn = get_insn_slot();
430 if (!p->ainsn.insn)
431 return -ENOMEM;
432 arch_copy_kprobe(p);
433 return 0;
434}
435
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700436void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700437{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200438 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700439}
440
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700441void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700442{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200443 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800446void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800448 if (p->ainsn.insn) {
449 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
450 p->ainsn.insn = NULL;
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700454static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700455{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800456 kcb->prev_kprobe.kp = kprobe_running();
457 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100458 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
459 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700460}
461
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700462static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700463{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600464 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800465 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100466 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
467 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700468}
469
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700470static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800471 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700472{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600473 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100474 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100475 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700476 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100477 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700478}
479
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100480static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100481{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100482 if (test_thread_flag(TIF_BLOCKSTEP)) {
483 unsigned long debugctl = get_debugctlmsr();
484
485 debugctl &= ~DEBUGCTLMSR_BTF;
486 update_debugctlmsr(debugctl);
487 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100488}
489
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100490static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100491{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100492 if (test_thread_flag(TIF_BLOCKSTEP)) {
493 unsigned long debugctl = get_debugctlmsr();
494
495 debugctl |= DEBUGCTLMSR_BTF;
496 update_debugctlmsr(debugctl);
497 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100498}
499
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700500void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700501 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700502{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100503 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700504
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700505 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100506
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700507 /* Replace the return addr with trampoline addr */
508 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700509}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100510
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500511#ifdef CONFIG_OPTPROBES
512static int __kprobes setup_detour_execution(struct kprobe *p,
513 struct pt_regs *regs,
514 int reenter);
515#else
516#define setup_detour_execution(p, regs, reenter) (0)
517#endif
518
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100519static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500520 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100521{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500522 if (setup_detour_execution(p, regs, reenter))
523 return;
524
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500525#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100526 if (p->ainsn.boostable == 1 && !p->post_handler) {
527 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500528 if (!reenter)
529 reset_current_kprobe();
530 /*
531 * Reentering boosted probe doesn't reset current_kprobe,
532 * nor set current_kprobe, because it doesn't use single
533 * stepping.
534 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100535 regs->ip = (unsigned long)p->ainsn.insn;
536 preempt_enable_no_resched();
537 return;
538 }
539#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500540 if (reenter) {
541 save_previous_kprobe(kcb);
542 set_current_kprobe(p, regs, kcb);
543 kcb->kprobe_status = KPROBE_REENTER;
544 } else
545 kcb->kprobe_status = KPROBE_HIT_SS;
546 /* Prepare real single stepping */
547 clear_btf();
548 regs->flags |= X86_EFLAGS_TF;
549 regs->flags &= ~X86_EFLAGS_IF;
550 /* single step inline if the instruction is an int3 */
551 if (p->opcode == BREAKPOINT_INSTRUCTION)
552 regs->ip = (unsigned long)p->addr;
553 else
554 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100555}
556
Harvey Harrison40102d42008-01-30 13:32:02 +0100557/*
558 * We have reentered the kprobe_handler(), since another probe was hit while
559 * within the handler. We save the original kprobes variables and just single
560 * step on the instruction of the new probe without calling any user handlers.
561 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100562static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
563 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100564{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100565 switch (kcb->kprobe_status) {
566 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100567 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100568 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500569 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100570 break;
571 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400572 /* A probe has been hit in the codepath leading up to, or just
573 * after, single-stepping of a probed instruction. This entire
574 * codepath should strictly reside in .kprobes.text section.
575 * Raise a BUG or we'll continue in an endless reentering loop
576 * and eventually a stack overflow.
577 */
578 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
579 p->addr);
580 dump_kprobe(p);
581 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100582 default:
583 /* impossible cases */
584 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100585 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100586 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100587
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100588 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100589}
Rusty Lynch73649da2005-06-23 00:09:23 -0700590
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100591/*
592 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200593 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100594 */
595static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100597 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100598 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800599 struct kprobe_ctlblk *kcb;
600
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100601 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800602 /*
603 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100604 * duration of kprobe processing. We conditionally
605 * re-enable preemption at the end of this function,
606 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800607 */
608 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100610 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100611 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100612
Harvey Harrisonb9760152008-01-30 13:32:19 +0100613 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100614 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100615 if (reenter_kprobe(p, regs, kcb))
616 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100617 } else {
618 set_current_kprobe(p, regs, kcb);
619 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100622 * If we have no pre-handler or it returned 0, we
623 * continue with normal processing. If we have a
624 * pre-handler and it returned non-zero, it prepped
625 * for calling the break_handler below on re-entry
626 * for jprobe processing, so get out doing nothing
627 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100629 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500630 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100631 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100632 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400633 } else if (*addr != BREAKPOINT_INSTRUCTION) {
634 /*
635 * The breakpoint instruction was removed right
636 * after we hit it. Another cpu has removed
637 * either a probepoint or a debugger breakpoint
638 * at this address. In either case, no further
639 * handling of this interrupt is appropriate.
640 * Back up over the (now missing) int3 and run
641 * the original instruction.
642 */
643 regs->ip = (unsigned long)addr;
644 preempt_enable_no_resched();
645 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100646 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600647 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100648 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500649 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100650 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100652 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800654 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500658#ifdef CONFIG_X86_64
659#define SAVE_REGS_STRING \
660 /* Skip cs, ip, orig_ax. */ \
661 " subq $24, %rsp\n" \
662 " pushq %rdi\n" \
663 " pushq %rsi\n" \
664 " pushq %rdx\n" \
665 " pushq %rcx\n" \
666 " pushq %rax\n" \
667 " pushq %r8\n" \
668 " pushq %r9\n" \
669 " pushq %r10\n" \
670 " pushq %r11\n" \
671 " pushq %rbx\n" \
672 " pushq %rbp\n" \
673 " pushq %r12\n" \
674 " pushq %r13\n" \
675 " pushq %r14\n" \
676 " pushq %r15\n"
677#define RESTORE_REGS_STRING \
678 " popq %r15\n" \
679 " popq %r14\n" \
680 " popq %r13\n" \
681 " popq %r12\n" \
682 " popq %rbp\n" \
683 " popq %rbx\n" \
684 " popq %r11\n" \
685 " popq %r10\n" \
686 " popq %r9\n" \
687 " popq %r8\n" \
688 " popq %rax\n" \
689 " popq %rcx\n" \
690 " popq %rdx\n" \
691 " popq %rsi\n" \
692 " popq %rdi\n" \
693 /* Skip orig_ax, ip, cs */ \
694 " addq $24, %rsp\n"
695#else
696#define SAVE_REGS_STRING \
697 /* Skip cs, ip, orig_ax and gs. */ \
698 " subl $16, %esp\n" \
699 " pushl %fs\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500700 " pushl %es\n" \
Roland McGratha1974792010-07-16 18:17:12 -0700701 " pushl %ds\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500702 " pushl %eax\n" \
703 " pushl %ebp\n" \
704 " pushl %edi\n" \
705 " pushl %esi\n" \
706 " pushl %edx\n" \
707 " pushl %ecx\n" \
708 " pushl %ebx\n"
709#define RESTORE_REGS_STRING \
710 " popl %ebx\n" \
711 " popl %ecx\n" \
712 " popl %edx\n" \
713 " popl %esi\n" \
714 " popl %edi\n" \
715 " popl %ebp\n" \
716 " popl %eax\n" \
717 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
718 " addl $24, %esp\n"
719#endif
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100722 * When a retprobed function returns, this code saves registers and
723 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700724 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800725static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100726{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100727 asm volatile (
728 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100729 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100730#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100731 /* We don't bother saving the ss register */
732 " pushq %rsp\n"
733 " pushfq\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500734 SAVE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100735 " movq %rsp, %rdi\n"
736 " call trampoline_handler\n"
737 /* Replace saved sp with true return address. */
738 " movq %rax, 152(%rsp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500739 RESTORE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100740 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100741#else
742 " pushf\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500743 SAVE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100744 " movl %esp, %eax\n"
745 " call trampoline_handler\n"
746 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400747 " movl 56(%esp), %edx\n"
748 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100749 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400750 " movl %eax, 56(%esp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500751 RESTORE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100752 " popf\n"
753#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100754 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100755}
Rusty Lynch73649da2005-06-23 00:09:23 -0700756
757/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100758 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700759 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800760static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700761{
bibo,mao62c27be2006-10-02 02:17:33 -0700762 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700763 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700764 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800765 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100766 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900767 kprobe_opcode_t *correct_ret_addr = NULL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700768
bibo,mao99219a32006-10-02 02:17:35 -0700769 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700770 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100771 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100772#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100773 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100774#else
775 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400776 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100777#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100778 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100779 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700780
Rusty Lynchba8af122005-06-27 15:17:10 -0700781 /*
782 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100783 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200784 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700785 * return probe was registered for a target function.
786 *
787 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100788 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700789 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100790 * function, the (chronologically) first instance's ret_addr
791 * will be the real return address, and all the rest will
792 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700793 */
794 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700795 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700796 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700797 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700798
Rusty Lynchba8af122005-06-27 15:17:10 -0700799 orig_ret_address = (unsigned long)ri->ret_addr;
Rusty Lynchba8af122005-06-27 15:17:10 -0700800
801 if (orig_ret_address != trampoline_address)
802 /*
803 * This is the real return address. Any other
804 * instances associated with this task are for
805 * other calls deeper on the call stack
806 */
807 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700808 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700809
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700810 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700811
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900812 correct_ret_addr = ri->ret_addr;
813 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
814 if (ri->task != current)
815 /* another task is sharing our hash bucket */
816 continue;
817
818 orig_ret_address = (unsigned long)ri->ret_addr;
819 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600820 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900821 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
822 ri->ret_addr = correct_ret_addr;
823 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600824 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900825 }
826
827 recycle_rp_inst(ri, &empty_rp);
828
829 if (orig_ret_address != trampoline_address)
830 /*
831 * This is the real return address. Any other
832 * instances associated with this task are for
833 * other calls deeper on the call stack
834 */
835 break;
836 }
837
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700838 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700839
bibo,mao99219a32006-10-02 02:17:35 -0700840 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
841 hlist_del(&ri->hlist);
842 kfree(ri);
843 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100844 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700845}
846
847/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * Called after single-stepping. p->addr is the address of the
849 * instruction whose first byte has been replaced by the "int 3"
850 * instruction. To avoid the SMP problems that can occur when we
851 * temporarily put back the original opcode to single-step, we
852 * single-stepped a copy of the instruction. The address of this
853 * copy is p->ainsn.insn.
854 *
855 * This function prepares to return from the post-single-step
856 * interrupt. We have to fix up the stack as follows:
857 *
858 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100859 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * it relative to the original instruction.
861 *
862 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100863 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
865 * 2) If the single-stepped instruction was a call, the return address
866 * that is atop the stack is the address following the copied instruction.
867 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100868 *
869 * If this is the first time we've single-stepped the instruction at
870 * this probepoint, and the instruction is boostable, boost it: add a
871 * jump instruction after the copied instruction, that jumps to the next
872 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800874static void __kprobes resume_execution(struct kprobe *p,
875 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100877 unsigned long *tos = stack_addr(regs);
878 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
879 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 kprobe_opcode_t *insn = p->ainsn.insn;
881
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900882 /* Skip prefixes */
883 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100885 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100887 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100888 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100889 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100891 case 0xc2: /* iret/ret/lret */
892 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700893 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100894 case 0xcb:
895 case 0xcf:
896 case 0xea: /* jmp absolute -- ip is correct */
897 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100898 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100899 goto no_change;
900 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100901 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100903#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100904 case 0x9a: /* call absolute -- same as call absolute, indirect */
905 *tos = orig_ip + (*tos - copy_ip);
906 goto no_change;
907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700909 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100910 /*
911 * call absolute, indirect
912 * Fix return addr; ip is correct.
913 * But this is not boostable
914 */
915 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100916 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100917 } else if (((insn[1] & 0x31) == 0x20) ||
918 ((insn[1] & 0x31) == 0x21)) {
919 /*
920 * jmp near and far, absolute indirect
921 * ip is correct. And this is boostable
922 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100923 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100924 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 default:
927 break;
928 }
929
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100930 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100931 if ((regs->ip > copy_ip) &&
932 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100933 /*
934 * These instructions can be executed directly if it
935 * jumps back to correct address.
936 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500937 synthesize_reljump((void *)regs->ip,
938 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100939 p->ainsn.boostable = 1;
940 } else {
941 p->ainsn.boostable = -1;
942 }
943 }
944
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100945 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100946
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100947no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100948 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100951/*
952 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200953 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100954 */
955static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800957 struct kprobe *cur = kprobe_running();
958 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
959
960 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500963 resume_execution(cur, regs, kcb);
964 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500965
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800966 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
967 kcb->kprobe_status = KPROBE_HIT_SSDONE;
968 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100971 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800972 if (kcb->kprobe_status == KPROBE_REENTER) {
973 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700974 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700975 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800976 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700977out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 preempt_enable_no_resched();
979
980 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100981 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * will have TF set, in which case, continue the remaining processing
983 * of do_debug, as if this is not a probe hit.
984 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100985 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987
988 return 1;
989}
990
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700991int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800993 struct kprobe *cur = kprobe_running();
994 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
995
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100996 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800997 case KPROBE_HIT_SS:
998 case KPROBE_REENTER:
999 /*
1000 * We are here because the instruction being single
1001 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001002 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001003 * and allow the page fault handler to continue as a
1004 * normal page fault.
1005 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001006 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001007 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001008 if (kcb->kprobe_status == KPROBE_REENTER)
1009 restore_previous_kprobe(kcb);
1010 else
1011 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001013 break;
1014 case KPROBE_HIT_ACTIVE:
1015 case KPROBE_HIT_SSDONE:
1016 /*
1017 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001018 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001019 * these specific fault cases.
1020 */
1021 kprobes_inc_nmissed_count(cur);
1022
1023 /*
1024 * We come here because instructions in the pre/post
1025 * handler caused the page_fault, this could happen
1026 * if handler tries to access user space by
1027 * copy_from_user(), get_user() etc. Let the
1028 * user-specified handler try to fix it first.
1029 */
1030 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1031 return 1;
1032
1033 /*
1034 * In case the user-specified fault handler returned
1035 * zero, try to fix up.
1036 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001037 if (fixup_exception(regs))
1038 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +01001039
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001040 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001041 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -08001042 * Let do_page_fault() fix it.
1043 */
1044 break;
1045 default:
1046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 return 0;
1049}
1050
1051/*
1052 * Wrapper routine for handling exceptions.
1053 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001054int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
1055 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Jan Engelhardtade1af72008-01-30 13:33:23 +01001057 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001058 int ret = NOTIFY_DONE;
1059
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001060 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001061 return ret;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 switch (val) {
1064 case DIE_INT3:
1065 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001066 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 break;
1068 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +05301069 if (post_kprobe_handler(args->regs)) {
1070 /*
1071 * Reset the BS bit in dr6 (pointed by args->err) to
1072 * denote completion of processing
1073 */
1074 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001075 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +05301076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 break;
1078 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001079 /*
1080 * To be potentially processing a kprobe fault and to
1081 * trust the result from kprobe_running(), we have
1082 * be non-preemptible.
1083 */
1084 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001086 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 break;
1088 default:
1089 break;
1090 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001094int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct jprobe *jp = container_of(p, struct jprobe, kp);
1097 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001098 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001100 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001101 kcb->jprobe_saved_sp = stack_addr(regs);
1102 addr = (unsigned long)(kcb->jprobe_saved_sp);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * As Linus pointed out, gcc assumes that the callee
1106 * owns the argument space and could overwrite it, e.g.
1107 * tailcall optimization. So, to be absolutely safe
1108 * we also save and restore enough stack bytes to cover
1109 * the argument area.
1110 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001111 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001112 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001113 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001114 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001115 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 1;
1117}
1118
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001119void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001121 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1122
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001123 asm volatile (
1124#ifdef CONFIG_X86_64
1125 " xchg %%rbx,%%rsp \n"
1126#else
1127 " xchgl %%ebx,%%esp \n"
1128#endif
1129 " int3 \n"
1130 " .globl jprobe_return_end\n"
1131 " jprobe_return_end: \n"
1132 " nop \n"::"b"
1133 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001136int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001138 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001139 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct jprobe *jp = container_of(p, struct jprobe, kp);
1141
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001142 if ((addr > (u8 *) jprobe_return) &&
1143 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001144 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001145 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001146 printk(KERN_ERR
1147 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001148 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001149 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001151 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 show_registers(regs);
1153 BUG();
1154 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001155 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001156 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1157 kcb->jprobes_stack,
1158 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001159 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 1;
1161 }
1162 return 0;
1163}
Rusty Lynchba8af122005-06-27 15:17:10 -07001164
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001165
1166#ifdef CONFIG_OPTPROBES
1167
1168/* Insert a call instruction at address 'from', which calls address 'to'.*/
1169static void __kprobes synthesize_relcall(void *from, void *to)
1170{
1171 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1172}
1173
1174/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1175static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1176 unsigned long val)
1177{
1178#ifdef CONFIG_X86_64
1179 *addr++ = 0x48;
1180 *addr++ = 0xbf;
1181#else
1182 *addr++ = 0xb8;
1183#endif
1184 *(unsigned long *)addr = val;
1185}
1186
Namhyung Kim6376b222010-09-15 10:04:28 +09001187static void __used __kprobes kprobes_optinsn_template_holder(void)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001188{
1189 asm volatile (
1190 ".global optprobe_template_entry\n"
1191 "optprobe_template_entry: \n"
1192#ifdef CONFIG_X86_64
1193 /* We don't bother saving the ss register */
1194 " pushq %rsp\n"
1195 " pushfq\n"
1196 SAVE_REGS_STRING
1197 " movq %rsp, %rsi\n"
1198 ".global optprobe_template_val\n"
1199 "optprobe_template_val: \n"
1200 ASM_NOP5
1201 ASM_NOP5
1202 ".global optprobe_template_call\n"
1203 "optprobe_template_call: \n"
1204 ASM_NOP5
1205 /* Move flags to rsp */
1206 " movq 144(%rsp), %rdx\n"
1207 " movq %rdx, 152(%rsp)\n"
1208 RESTORE_REGS_STRING
1209 /* Skip flags entry */
1210 " addq $8, %rsp\n"
1211 " popfq\n"
1212#else /* CONFIG_X86_32 */
1213 " pushf\n"
1214 SAVE_REGS_STRING
1215 " movl %esp, %edx\n"
1216 ".global optprobe_template_val\n"
1217 "optprobe_template_val: \n"
1218 ASM_NOP5
1219 ".global optprobe_template_call\n"
1220 "optprobe_template_call: \n"
1221 ASM_NOP5
1222 RESTORE_REGS_STRING
1223 " addl $4, %esp\n" /* skip cs */
1224 " popf\n"
1225#endif
1226 ".global optprobe_template_end\n"
1227 "optprobe_template_end: \n");
1228}
1229
1230#define TMPL_MOVE_IDX \
1231 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1232#define TMPL_CALL_IDX \
1233 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1234#define TMPL_END_IDX \
1235 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1236
1237#define INT3_SIZE sizeof(kprobe_opcode_t)
1238
1239/* Optimized kprobe call back function: called from optinsn */
1240static void __kprobes optimized_callback(struct optimized_kprobe *op,
1241 struct pt_regs *regs)
1242{
1243 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001244 unsigned long flags;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001245
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001246 /* This is possible if op is under delayed unoptimizing */
1247 if (kprobe_disabled(&op->kp))
1248 return;
1249
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001250 local_irq_save(flags);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001251 if (kprobe_running()) {
1252 kprobes_inc_nmissed_count(&op->kp);
1253 } else {
1254 /* Save skipped registers */
1255#ifdef CONFIG_X86_64
1256 regs->cs = __KERNEL_CS;
1257#else
1258 regs->cs = __KERNEL_CS | get_kernel_rpl();
1259 regs->gs = 0;
1260#endif
1261 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1262 regs->orig_ax = ~0UL;
1263
Christoph Lameterb76834b2010-12-06 11:16:25 -06001264 __this_cpu_write(current_kprobe, &op->kp);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001265 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1266 opt_pre_handler(&op->kp, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -06001267 __this_cpu_write(current_kprobe, NULL);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001268 }
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001269 local_irq_restore(flags);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001270}
1271
1272static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1273{
1274 int len = 0, ret;
1275
1276 while (len < RELATIVEJUMP_SIZE) {
Masami Hiramatsu46484682012-03-05 22:32:16 +09001277 ret = __copy_instruction(dest + len, src + len);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001278 if (!ret || !can_boost(dest + len))
1279 return -EINVAL;
1280 len += ret;
1281 }
1282 /* Check whether the address range is reserved */
1283 if (ftrace_text_reserved(src, src + len - 1) ||
Jason Baron4c3ef6d2010-09-17 11:09:08 -04001284 alternatives_text_reserved(src, src + len - 1) ||
1285 jump_label_text_reserved(src, src + len - 1))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001286 return -EBUSY;
1287
1288 return len;
1289}
1290
1291/* Check whether insn is indirect jump */
1292static int __kprobes insn_is_indirect_jump(struct insn *insn)
1293{
1294 return ((insn->opcode.bytes[0] == 0xff &&
1295 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1296 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1297}
1298
1299/* Check whether insn jumps into specified address range */
1300static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1301{
1302 unsigned long target = 0;
1303
1304 switch (insn->opcode.bytes[0]) {
1305 case 0xe0: /* loopne */
1306 case 0xe1: /* loope */
1307 case 0xe2: /* loop */
1308 case 0xe3: /* jcxz */
1309 case 0xe9: /* near relative jump */
1310 case 0xeb: /* short relative jump */
1311 break;
1312 case 0x0f:
1313 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1314 break;
1315 return 0;
1316 default:
1317 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1318 break;
1319 return 0;
1320 }
1321 target = (unsigned long)insn->next_byte + insn->immediate.value;
1322
1323 return (start <= target && target <= start + len);
1324}
1325
1326/* Decode whole function to ensure any instructions don't jump into target */
1327static int __kprobes can_optimize(unsigned long paddr)
1328{
Masami Hiramatsu46484682012-03-05 22:32:16 +09001329 unsigned long addr, size = 0, offset = 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001330 struct insn insn;
1331 kprobe_opcode_t buf[MAX_INSN_SIZE];
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001332
1333 /* Lookup symbol including addr */
Namhyung Kim6abded72010-09-15 10:04:29 +09001334 if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001335 return 0;
1336
Jiri Olsa2a8247a2011-02-21 15:25:13 +01001337 /*
1338 * Do not optimize in the entry code due to the unstable
1339 * stack handling.
1340 */
1341 if ((paddr >= (unsigned long )__entry_text_start) &&
1342 (paddr < (unsigned long )__entry_text_end))
1343 return 0;
1344
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001345 /* Check there is enough space for a relative jump. */
1346 if (size - offset < RELATIVEJUMP_SIZE)
1347 return 0;
1348
1349 /* Decode instructions */
1350 addr = paddr - offset;
1351 while (addr < paddr - offset + size) { /* Decode until function end */
1352 if (search_exception_tables(addr))
1353 /*
1354 * Since some fixup code will jumps into this function,
1355 * we can't optimize kprobe in this function.
1356 */
1357 return 0;
Masami Hiramatsu46484682012-03-05 22:32:16 +09001358 kernel_insn_init(&insn, (void *)recover_probed_instruction(buf, addr));
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001359 insn_get_length(&insn);
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +09001360 /* Another subsystem puts a breakpoint */
1361 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
1362 return 0;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001363 /* Recover address */
1364 insn.kaddr = (void *)addr;
1365 insn.next_byte = (void *)(addr + insn.length);
1366 /* Check any instructions don't jump into target */
1367 if (insn_is_indirect_jump(&insn) ||
1368 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1369 RELATIVE_ADDR_SIZE))
1370 return 0;
1371 addr += insn.length;
1372 }
1373
1374 return 1;
1375}
1376
1377/* Check optimized_kprobe can actually be optimized. */
1378int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1379{
1380 int i;
1381 struct kprobe *p;
1382
1383 for (i = 1; i < op->optinsn.size; i++) {
1384 p = get_kprobe(op->kp.addr + i);
1385 if (p && !kprobe_disabled(p))
1386 return -EEXIST;
1387 }
1388
1389 return 0;
1390}
1391
1392/* Check the addr is within the optimized instructions. */
1393int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1394 unsigned long addr)
1395{
1396 return ((unsigned long)op->kp.addr <= addr &&
1397 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1398}
1399
1400/* Free optimized instruction slot */
1401static __kprobes
1402void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1403{
1404 if (op->optinsn.insn) {
1405 free_optinsn_slot(op->optinsn.insn, dirty);
1406 op->optinsn.insn = NULL;
1407 op->optinsn.size = 0;
1408 }
1409}
1410
1411void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1412{
1413 __arch_remove_optimized_kprobe(op, 1);
1414}
1415
1416/*
1417 * Copy replacing target instructions
1418 * Target instructions MUST be relocatable (checked inside)
Masami Hiramatsu86b4ce32012-03-05 22:32:09 +09001419 * This is called when new aggr(opt)probe is allocated or reused.
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001420 */
1421int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1422{
1423 u8 *buf;
1424 int ret;
1425 long rel;
1426
1427 if (!can_optimize((unsigned long)op->kp.addr))
1428 return -EILSEQ;
1429
1430 op->optinsn.insn = get_optinsn_slot();
1431 if (!op->optinsn.insn)
1432 return -ENOMEM;
1433
1434 /*
1435 * Verify if the address gap is in 2GB range, because this uses
1436 * a relative jump.
1437 */
1438 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1439 if (abs(rel) > 0x7fffffff)
1440 return -ERANGE;
1441
1442 buf = (u8 *)op->optinsn.insn;
1443
1444 /* Copy instructions into the out-of-line buffer */
1445 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1446 if (ret < 0) {
1447 __arch_remove_optimized_kprobe(op, 0);
1448 return ret;
1449 }
1450 op->optinsn.size = ret;
1451
1452 /* Copy arch-dep-instance from template */
1453 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1454
1455 /* Set probe information */
1456 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1457
1458 /* Set probe function call */
1459 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1460
1461 /* Set returning jmp instruction at the tail of out-of-line buffer */
1462 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1463 (u8 *)op->kp.addr + op->optinsn.size);
1464
1465 flush_icache_range((unsigned long) buf,
1466 (unsigned long) buf + TMPL_END_IDX +
1467 op->optinsn.size + RELATIVEJUMP_SIZE);
1468 return 0;
1469}
1470
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001471#define MAX_OPTIMIZE_PROBES 256
1472static struct text_poke_param *jump_poke_params;
1473static struct jump_poke_buffer {
1474 u8 buf[RELATIVEJUMP_SIZE];
1475} *jump_poke_bufs;
1476
1477static void __kprobes setup_optimize_kprobe(struct text_poke_param *tprm,
1478 u8 *insn_buf,
1479 struct optimized_kprobe *op)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001480{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001481 s32 rel = (s32)((long)op->optinsn.insn -
1482 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1483
1484 /* Backup instructions which will be replaced by jump address */
1485 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1486 RELATIVE_ADDR_SIZE);
1487
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001488 insn_buf[0] = RELATIVEJUMP_OPCODE;
1489 *(s32 *)(&insn_buf[1]) = rel;
1490
1491 tprm->addr = op->kp.addr;
1492 tprm->opcode = insn_buf;
1493 tprm->len = RELATIVEJUMP_SIZE;
1494}
1495
1496/*
1497 * Replace breakpoints (int3) with relative jumps.
1498 * Caller must call with locking kprobe_mutex and text_mutex.
1499 */
1500void __kprobes arch_optimize_kprobes(struct list_head *oplist)
1501{
1502 struct optimized_kprobe *op, *tmp;
1503 int c = 0;
1504
1505 list_for_each_entry_safe(op, tmp, oplist, list) {
1506 WARN_ON(kprobe_disabled(&op->kp));
1507 /* Setup param */
1508 setup_optimize_kprobe(&jump_poke_params[c],
1509 jump_poke_bufs[c].buf, op);
1510 list_del_init(&op->list);
1511 if (++c >= MAX_OPTIMIZE_PROBES)
1512 break;
1513 }
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001514
1515 /*
1516 * text_poke_smp doesn't support NMI/MCE code modifying.
1517 * However, since kprobes itself also doesn't support NMI/MCE
1518 * code probing, it's not a problem.
1519 */
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001520 text_poke_smp_batch(jump_poke_params, c);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001521}
1522
Masami Hiramatsuf984ba42010-12-03 18:54:34 +09001523static void __kprobes setup_unoptimize_kprobe(struct text_poke_param *tprm,
1524 u8 *insn_buf,
1525 struct optimized_kprobe *op)
1526{
1527 /* Set int3 to first byte for kprobes */
1528 insn_buf[0] = BREAKPOINT_INSTRUCTION;
1529 memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1530
1531 tprm->addr = op->kp.addr;
1532 tprm->opcode = insn_buf;
1533 tprm->len = RELATIVEJUMP_SIZE;
1534}
1535
1536/*
1537 * Recover original instructions and breakpoints from relative jumps.
1538 * Caller must call with locking kprobe_mutex.
1539 */
1540extern void arch_unoptimize_kprobes(struct list_head *oplist,
1541 struct list_head *done_list)
1542{
1543 struct optimized_kprobe *op, *tmp;
1544 int c = 0;
1545
1546 list_for_each_entry_safe(op, tmp, oplist, list) {
1547 /* Setup param */
1548 setup_unoptimize_kprobe(&jump_poke_params[c],
1549 jump_poke_bufs[c].buf, op);
1550 list_move(&op->list, done_list);
1551 if (++c >= MAX_OPTIMIZE_PROBES)
1552 break;
1553 }
1554
1555 /*
1556 * text_poke_smp doesn't support NMI/MCE code modifying.
1557 * However, since kprobes itself also doesn't support NMI/MCE
1558 * code probing, it's not a problem.
1559 */
1560 text_poke_smp_batch(jump_poke_params, c);
1561}
1562
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001563/* Replace a relative jump with a breakpoint (int3). */
1564void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1565{
1566 u8 buf[RELATIVEJUMP_SIZE];
1567
1568 /* Set int3 to first byte for kprobes */
1569 buf[0] = BREAKPOINT_INSTRUCTION;
1570 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1571 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1572}
1573
1574static int __kprobes setup_detour_execution(struct kprobe *p,
1575 struct pt_regs *regs,
1576 int reenter)
1577{
1578 struct optimized_kprobe *op;
1579
1580 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1581 /* This kprobe is really able to run optimized path. */
1582 op = container_of(p, struct optimized_kprobe, kp);
1583 /* Detour through copied instructions */
1584 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1585 if (!reenter)
1586 reset_current_kprobe();
1587 preempt_enable_no_resched();
1588 return 1;
1589 }
1590 return 0;
1591}
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001592
1593static int __kprobes init_poke_params(void)
1594{
1595 /* Allocate code buffer and parameter array */
1596 jump_poke_bufs = kmalloc(sizeof(struct jump_poke_buffer) *
1597 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1598 if (!jump_poke_bufs)
1599 return -ENOMEM;
1600
1601 jump_poke_params = kmalloc(sizeof(struct text_poke_param) *
1602 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1603 if (!jump_poke_params) {
1604 kfree(jump_poke_bufs);
1605 jump_poke_bufs = NULL;
1606 return -ENOMEM;
1607 }
1608
1609 return 0;
1610}
1611#else /* !CONFIG_OPTPROBES */
1612static int __kprobes init_poke_params(void)
1613{
1614 return 0;
1615}
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001616#endif
1617
Rusty Lynch67729262005-07-05 18:54:50 -07001618int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001619{
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001620 return init_poke_params();
Rusty Lynchba8af122005-06-27 15:17:10 -07001621}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001622
1623int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1624{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001625 return 0;
1626}