blob: f2f56c0967b66e5723b293d41946084281fba223 [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.
78 */
79static const u32 twobyte_is_boostable[256 / 32] = {
80 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
81 /* ---------------------------------------------- */
82 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
83 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
84 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
85 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
86 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
87 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
88 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
89 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
90 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
91 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
92 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
93 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
94 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
95 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
96 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
97 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
98 /* ----------------------------------------------- */
99 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
100};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100101#undef W
102
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700103struct kretprobe_blackpoint kretprobe_blacklist[] = {
104 {"__switch_to", }, /* This function switches only current task, but
105 doesn't switch kernel stack.*/
106 {NULL, NULL} /* Terminator */
107};
108const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
109
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500110static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100111{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500112 struct __arch_relative_insn {
113 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100114 s32 raddr;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500115 } __attribute__((packed)) *insn;
116
117 insn = (struct __arch_relative_insn *)from;
118 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
119 insn->op = op;
120}
121
122/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
123static void __kprobes synthesize_reljump(void *from, void *to)
124{
125 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100126}
127
128/*
Harvey Harrison99309272008-01-30 13:32:14 +0100129 * Check for the REX prefix which can only exist on X86_64
130 * X86_32 always returns 0
131 */
132static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
133{
134#ifdef CONFIG_X86_64
135 if ((*insn & 0xf0) == 0x40)
136 return 1;
137#endif
138 return 0;
139}
140
141/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100142 * Returns non-zero if opcode is boostable.
143 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100144 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100145static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100146{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100147 kprobe_opcode_t opcode;
148 kprobe_opcode_t *orig_opcodes = opcodes;
149
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530150 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400151 return 0; /* Page fault may occur on this address. */
152
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100153retry:
154 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
155 return 0;
156 opcode = *(opcodes++);
157
158 /* 2nd-byte opcode */
159 if (opcode == 0x0f) {
160 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
161 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100162 return test_bit(*opcodes,
163 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100164 }
165
166 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100167#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100168 case 0x40:
169 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100170#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100171 case 0x60:
172 if (0x63 < opcode && opcode < 0x67)
173 goto retry; /* prefixes */
174 /* can't boost Address-size override and bound */
175 return (opcode != 0x62 && opcode != 0x67);
176 case 0x70:
177 return 0; /* can't boost conditional jump */
178 case 0xc0:
179 /* can't boost software-interruptions */
180 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
181 case 0xd0:
182 /* can boost AA* and XLAT */
183 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
184 case 0xe0:
185 /* can boost in/out and absolute jmps */
186 return ((opcode & 0x04) || opcode == 0xea);
187 case 0xf0:
188 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
189 goto retry; /* lock/rep(ne) prefix */
190 /* clear and set flags are boostable */
191 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
192 default:
193 /* segment override prefixes are boostable */
194 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
195 goto retry; /* prefixes */
196 /* CS override prefix and call are not boostable */
197 return (opcode != 0x2e && opcode != 0x9a);
198 }
199}
200
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400201/* Recover the probed instruction at addr for further analysis. */
202static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
203{
204 struct kprobe *kp;
205 kp = get_kprobe((void *)addr);
206 if (!kp)
207 return -EINVAL;
208
209 /*
210 * Basically, kp->ainsn.insn has an original instruction.
211 * However, RIP-relative instruction can not do single-stepping
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500212 * at different place, __copy_instruction() tweaks the displacement of
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400213 * that instruction. In that case, we can't recover the instruction
214 * from the kp->ainsn.insn.
215 *
216 * On the other hand, kp->opcode has a copy of the first byte of
217 * the probed instruction, which is overwritten by int3. And
218 * the instruction at kp->addr is not modified by kprobes except
219 * for the first byte, we can recover the original instruction
220 * from it and kp->opcode.
221 */
222 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
223 buf[0] = kp->opcode;
224 return 0;
225}
226
227/* Dummy buffers for kallsyms_lookup */
228static char __dummy_buf[KSYM_NAME_LEN];
229
230/* Check if paddr is at an instruction boundary */
231static int __kprobes can_probe(unsigned long paddr)
232{
233 int ret;
234 unsigned long addr, offset = 0;
235 struct insn insn;
236 kprobe_opcode_t buf[MAX_INSN_SIZE];
237
238 if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
239 return 0;
240
241 /* Decode instructions */
242 addr = paddr - offset;
243 while (addr < paddr) {
244 kernel_insn_init(&insn, (void *)addr);
245 insn_get_opcode(&insn);
246
247 /*
248 * Check if the instruction has been modified by another
249 * kprobe, in which case we replace the breakpoint by the
250 * original instruction in our buffer.
251 */
252 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
253 ret = recover_probed_instruction(buf, addr);
254 if (ret)
255 /*
256 * Another debugging subsystem might insert
257 * this breakpoint. In that case, we can't
258 * recover it.
259 */
260 return 0;
261 kernel_insn_init(&insn, buf);
262 }
263 insn_get_length(&insn);
264 addr += insn.length;
265 }
266
267 return (addr == paddr);
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100271 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Andrew Morton86454192007-11-26 20:42:19 +0100273static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 switch (*insn) {
276 case 0xfa: /* cli */
277 case 0xfb: /* sti */
278 case 0xcf: /* iret/iretd */
279 case 0x9d: /* popf/popfd */
280 return 1;
281 }
Harvey Harrison99309272008-01-30 13:32:14 +0100282
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100283 /*
Harvey Harrison99309272008-01-30 13:32:14 +0100284 * on X86_64, 0x40-0x4f are REX prefixes so we need to look
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100285 * at the next byte instead.. but of course not recurse infinitely
286 */
Harvey Harrison99309272008-01-30 13:32:14 +0100287 if (is_REX_prefix(insn))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100288 return is_IF_modifier(++insn);
Harvey Harrison99309272008-01-30 13:32:14 +0100289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
291}
292
293/*
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500294 * Copy an instruction and adjust the displacement if the instruction
295 * uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100296 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100298 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500300static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400302 struct insn insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500303 int ret;
304 kprobe_opcode_t buf[MAX_INSN_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500306 kernel_insn_init(&insn, src);
307 if (recover) {
308 insn_get_opcode(&insn);
309 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
310 ret = recover_probed_instruction(buf,
311 (unsigned long)src);
312 if (ret)
313 return 0;
314 kernel_insn_init(&insn, buf);
315 }
316 }
317 insn_get_length(&insn);
318 memcpy(dest, insn.kaddr, insn.length);
319
320#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400321 if (insn_rip_relative(&insn)) {
322 s64 newdisp;
323 u8 *disp;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500324 kernel_insn_init(&insn, dest);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400325 insn_get_displacement(&insn);
326 /*
327 * The copied instruction uses the %rip-relative addressing
328 * mode. Adjust the displacement for the difference between
329 * the original location of this instruction and the location
330 * of the copy that will actually be run. The tricky bit here
331 * is making sure that the sign extension happens correctly in
332 * this calculation, since we need a signed 32-bit result to
333 * be sign-extended to 64 bits when it's added to the %rip
334 * value and yield the same 64-bit result that the sign-
335 * extension of the original signed 32-bit displacement would
336 * have given.
337 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500338 newdisp = (u8 *) src + (s64) insn.displacement.value -
339 (u8 *) dest;
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400340 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500341 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400342 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100344#endif
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500345 return insn.length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800348static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500350 /*
351 * Copy an instruction without recovering int3, because it will be
352 * put by another subsystem.
353 */
354 __copy_instruction(p->ainsn.insn, p->addr, 0);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100355
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100356 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100357 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100358 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100359 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100360
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700361 p->opcode = *p->addr;
362}
363
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100364int __kprobes arch_prepare_kprobe(struct kprobe *p)
365{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500366 if (alternatives_text_reserved(p->addr, p->addr))
367 return -EINVAL;
368
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400369 if (!can_probe((unsigned long)p->addr))
370 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100371 /* insn: must be on special executable page on x86. */
372 p->ainsn.insn = get_insn_slot();
373 if (!p->ainsn.insn)
374 return -ENOMEM;
375 arch_copy_kprobe(p);
376 return 0;
377}
378
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700379void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700380{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200381 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700382}
383
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700384void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700385{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200386 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800389void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800391 if (p->ainsn.insn) {
392 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
393 p->ainsn.insn = NULL;
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700397static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700398{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800399 kcb->prev_kprobe.kp = kprobe_running();
400 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100401 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
402 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700403}
404
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700405static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700406{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800407 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
408 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100409 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
410 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700411}
412
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700413static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800414 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700415{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800416 __get_cpu_var(current_kprobe) = p;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100417 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100418 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700419 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100420 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700421}
422
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100423static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100424{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100425 if (test_thread_flag(TIF_BLOCKSTEP)) {
426 unsigned long debugctl = get_debugctlmsr();
427
428 debugctl &= ~DEBUGCTLMSR_BTF;
429 update_debugctlmsr(debugctl);
430 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100431}
432
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100433static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100434{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100435 if (test_thread_flag(TIF_BLOCKSTEP)) {
436 unsigned long debugctl = get_debugctlmsr();
437
438 debugctl |= DEBUGCTLMSR_BTF;
439 update_debugctlmsr(debugctl);
440 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100441}
442
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700443void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700444 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700445{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100446 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700447
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700448 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100449
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700450 /* Replace the return addr with trampoline addr */
451 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700452}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100453
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500454#ifdef CONFIG_OPTPROBES
455static int __kprobes setup_detour_execution(struct kprobe *p,
456 struct pt_regs *regs,
457 int reenter);
458#else
459#define setup_detour_execution(p, regs, reenter) (0)
460#endif
461
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100462static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500463 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100464{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500465 if (setup_detour_execution(p, regs, reenter))
466 return;
467
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500468#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100469 if (p->ainsn.boostable == 1 && !p->post_handler) {
470 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500471 if (!reenter)
472 reset_current_kprobe();
473 /*
474 * Reentering boosted probe doesn't reset current_kprobe,
475 * nor set current_kprobe, because it doesn't use single
476 * stepping.
477 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100478 regs->ip = (unsigned long)p->ainsn.insn;
479 preempt_enable_no_resched();
480 return;
481 }
482#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500483 if (reenter) {
484 save_previous_kprobe(kcb);
485 set_current_kprobe(p, regs, kcb);
486 kcb->kprobe_status = KPROBE_REENTER;
487 } else
488 kcb->kprobe_status = KPROBE_HIT_SS;
489 /* Prepare real single stepping */
490 clear_btf();
491 regs->flags |= X86_EFLAGS_TF;
492 regs->flags &= ~X86_EFLAGS_IF;
493 /* single step inline if the instruction is an int3 */
494 if (p->opcode == BREAKPOINT_INSTRUCTION)
495 regs->ip = (unsigned long)p->addr;
496 else
497 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100498}
499
Harvey Harrison40102d42008-01-30 13:32:02 +0100500/*
501 * We have reentered the kprobe_handler(), since another probe was hit while
502 * within the handler. We save the original kprobes variables and just single
503 * step on the instruction of the new probe without calling any user handlers.
504 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100505static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
506 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100507{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100508 switch (kcb->kprobe_status) {
509 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100510 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100511 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500512 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100513 break;
514 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400515 /* A probe has been hit in the codepath leading up to, or just
516 * after, single-stepping of a probed instruction. This entire
517 * codepath should strictly reside in .kprobes.text section.
518 * Raise a BUG or we'll continue in an endless reentering loop
519 * and eventually a stack overflow.
520 */
521 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
522 p->addr);
523 dump_kprobe(p);
524 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100525 default:
526 /* impossible cases */
527 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100528 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100529 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100530
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100531 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100532}
Rusty Lynch73649da2005-06-23 00:09:23 -0700533
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100534/*
535 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200536 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100537 */
538static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100540 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100541 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800542 struct kprobe_ctlblk *kcb;
543
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100544 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100545 if (*addr != BREAKPOINT_INSTRUCTION) {
546 /*
547 * The breakpoint instruction was removed right
548 * after we hit it. Another cpu has removed
549 * either a probepoint or a debugger breakpoint
550 * at this address. In either case, no further
551 * handling of this interrupt is appropriate.
552 * Back up over the (now missing) int3 and run
553 * the original instruction.
554 */
555 regs->ip = (unsigned long)addr;
556 return 1;
557 }
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100558
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800559 /*
560 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100561 * duration of kprobe processing. We conditionally
562 * re-enable preemption at the end of this function,
563 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800564 */
565 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100567 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100568 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100569
Harvey Harrisonb9760152008-01-30 13:32:19 +0100570 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100571 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100572 if (reenter_kprobe(p, regs, kcb))
573 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100574 } else {
575 set_current_kprobe(p, regs, kcb);
576 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100579 * If we have no pre-handler or it returned 0, we
580 * continue with normal processing. If we have a
581 * pre-handler and it returned non-zero, it prepped
582 * for calling the break_handler below on re-entry
583 * for jprobe processing, so get out doing nothing
584 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100586 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500587 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100588 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100589 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100590 } else if (kprobe_running()) {
591 p = __get_cpu_var(current_kprobe);
592 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500593 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100594 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100596 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800598 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500602#ifdef CONFIG_X86_64
603#define SAVE_REGS_STRING \
604 /* Skip cs, ip, orig_ax. */ \
605 " subq $24, %rsp\n" \
606 " pushq %rdi\n" \
607 " pushq %rsi\n" \
608 " pushq %rdx\n" \
609 " pushq %rcx\n" \
610 " pushq %rax\n" \
611 " pushq %r8\n" \
612 " pushq %r9\n" \
613 " pushq %r10\n" \
614 " pushq %r11\n" \
615 " pushq %rbx\n" \
616 " pushq %rbp\n" \
617 " pushq %r12\n" \
618 " pushq %r13\n" \
619 " pushq %r14\n" \
620 " pushq %r15\n"
621#define RESTORE_REGS_STRING \
622 " popq %r15\n" \
623 " popq %r14\n" \
624 " popq %r13\n" \
625 " popq %r12\n" \
626 " popq %rbp\n" \
627 " popq %rbx\n" \
628 " popq %r11\n" \
629 " popq %r10\n" \
630 " popq %r9\n" \
631 " popq %r8\n" \
632 " popq %rax\n" \
633 " popq %rcx\n" \
634 " popq %rdx\n" \
635 " popq %rsi\n" \
636 " popq %rdi\n" \
637 /* Skip orig_ax, ip, cs */ \
638 " addq $24, %rsp\n"
639#else
640#define SAVE_REGS_STRING \
641 /* Skip cs, ip, orig_ax and gs. */ \
642 " subl $16, %esp\n" \
643 " pushl %fs\n" \
644 " pushl %ds\n" \
645 " pushl %es\n" \
646 " pushl %eax\n" \
647 " pushl %ebp\n" \
648 " pushl %edi\n" \
649 " pushl %esi\n" \
650 " pushl %edx\n" \
651 " pushl %ecx\n" \
652 " pushl %ebx\n"
653#define RESTORE_REGS_STRING \
654 " popl %ebx\n" \
655 " popl %ecx\n" \
656 " popl %edx\n" \
657 " popl %esi\n" \
658 " popl %edi\n" \
659 " popl %ebp\n" \
660 " popl %eax\n" \
661 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
662 " addl $24, %esp\n"
663#endif
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100666 * When a retprobed function returns, this code saves registers and
667 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700668 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800669static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100670{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100671 asm volatile (
672 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100673 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100674#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100675 /* We don't bother saving the ss register */
676 " pushq %rsp\n"
677 " pushfq\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500678 SAVE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100679 " movq %rsp, %rdi\n"
680 " call trampoline_handler\n"
681 /* Replace saved sp with true return address. */
682 " movq %rax, 152(%rsp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500683 RESTORE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100684 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100685#else
686 " pushf\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500687 SAVE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100688 " movl %esp, %eax\n"
689 " call trampoline_handler\n"
690 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400691 " movl 56(%esp), %edx\n"
692 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100693 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400694 " movl %eax, 56(%esp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500695 RESTORE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100696 " popf\n"
697#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100698 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100699}
Rusty Lynch73649da2005-06-23 00:09:23 -0700700
701/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100702 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700703 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800704static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700705{
bibo,mao62c27be2006-10-02 02:17:33 -0700706 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700707 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700708 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800709 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100710 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700711
bibo,mao99219a32006-10-02 02:17:35 -0700712 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700713 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100714 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100715#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100716 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100717#else
718 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400719 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100720#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100721 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100722 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700723
Rusty Lynchba8af122005-06-27 15:17:10 -0700724 /*
725 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100726 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200727 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700728 * return probe was registered for a target function.
729 *
730 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100731 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700732 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100733 * function, the (chronologically) first instance's ret_addr
734 * will be the real return address, and all the rest will
735 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700736 */
737 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700738 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700739 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700740 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700741
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100742 if (ri->rp && ri->rp->handler) {
743 __get_cpu_var(current_kprobe) = &ri->rp->kp;
744 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
Rusty Lynchba8af122005-06-27 15:17:10 -0700745 ri->rp->handler(ri, regs);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100746 __get_cpu_var(current_kprobe) = NULL;
747 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700748
Rusty Lynchba8af122005-06-27 15:17:10 -0700749 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700750 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700751
752 if (orig_ret_address != trampoline_address)
753 /*
754 * This is the real return address. Any other
755 * instances associated with this task are for
756 * other calls deeper on the call stack
757 */
758 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700759 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700760
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700761 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700762
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700763 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700764
bibo,mao99219a32006-10-02 02:17:35 -0700765 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
766 hlist_del(&ri->hlist);
767 kfree(ri);
768 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100769 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700770}
771
772/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * Called after single-stepping. p->addr is the address of the
774 * instruction whose first byte has been replaced by the "int 3"
775 * instruction. To avoid the SMP problems that can occur when we
776 * temporarily put back the original opcode to single-step, we
777 * single-stepped a copy of the instruction. The address of this
778 * copy is p->ainsn.insn.
779 *
780 * This function prepares to return from the post-single-step
781 * interrupt. We have to fix up the stack as follows:
782 *
783 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100784 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * it relative to the original instruction.
786 *
787 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100788 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 *
790 * 2) If the single-stepped instruction was a call, the return address
791 * that is atop the stack is the address following the copied instruction.
792 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100793 *
794 * If this is the first time we've single-stepped the instruction at
795 * this probepoint, and the instruction is boostable, boost it: add a
796 * jump instruction after the copied instruction, that jumps to the next
797 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800799static void __kprobes resume_execution(struct kprobe *p,
800 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100802 unsigned long *tos = stack_addr(regs);
803 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
804 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 kprobe_opcode_t *insn = p->ainsn.insn;
806
807 /*skip the REX prefix*/
Harvey Harrison99309272008-01-30 13:32:14 +0100808 if (is_REX_prefix(insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 insn++;
810
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100811 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100813 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100814 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100815 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100817 case 0xc2: /* iret/ret/lret */
818 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700819 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100820 case 0xcb:
821 case 0xcf:
822 case 0xea: /* jmp absolute -- ip is correct */
823 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100824 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100825 goto no_change;
826 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100827 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100829#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100830 case 0x9a: /* call absolute -- same as call absolute, indirect */
831 *tos = orig_ip + (*tos - copy_ip);
832 goto no_change;
833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700835 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100836 /*
837 * call absolute, indirect
838 * Fix return addr; ip is correct.
839 * But this is not boostable
840 */
841 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100842 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100843 } else if (((insn[1] & 0x31) == 0x20) ||
844 ((insn[1] & 0x31) == 0x21)) {
845 /*
846 * jmp near and far, absolute indirect
847 * ip is correct. And this is boostable
848 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100849 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100850 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 default:
853 break;
854 }
855
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100856 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100857 if ((regs->ip > copy_ip) &&
858 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100859 /*
860 * These instructions can be executed directly if it
861 * jumps back to correct address.
862 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500863 synthesize_reljump((void *)regs->ip,
864 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100865 p->ainsn.boostable = 1;
866 } else {
867 p->ainsn.boostable = -1;
868 }
869 }
870
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100871 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100872
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100873no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100874 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100877/*
878 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200879 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100880 */
881static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800883 struct kprobe *cur = kprobe_running();
884 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
885
886 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500889 resume_execution(cur, regs, kcb);
890 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500891
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800892 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
893 kcb->kprobe_status = KPROBE_HIT_SSDONE;
894 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100897 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800898 if (kcb->kprobe_status == KPROBE_REENTER) {
899 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700900 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700901 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800902 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700903out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 preempt_enable_no_resched();
905
906 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100907 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * will have TF set, in which case, continue the remaining processing
909 * of do_debug, as if this is not a probe hit.
910 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100911 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913
914 return 1;
915}
916
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700917int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800919 struct kprobe *cur = kprobe_running();
920 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
921
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100922 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800923 case KPROBE_HIT_SS:
924 case KPROBE_REENTER:
925 /*
926 * We are here because the instruction being single
927 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100928 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800929 * and allow the page fault handler to continue as a
930 * normal page fault.
931 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100932 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100933 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800934 if (kcb->kprobe_status == KPROBE_REENTER)
935 restore_previous_kprobe(kcb);
936 else
937 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800939 break;
940 case KPROBE_HIT_ACTIVE:
941 case KPROBE_HIT_SSDONE:
942 /*
943 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100944 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800945 * these specific fault cases.
946 */
947 kprobes_inc_nmissed_count(cur);
948
949 /*
950 * We come here because instructions in the pre/post
951 * handler caused the page_fault, this could happen
952 * if handler tries to access user space by
953 * copy_from_user(), get_user() etc. Let the
954 * user-specified handler try to fix it first.
955 */
956 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
957 return 1;
958
959 /*
960 * In case the user-specified fault handler returned
961 * zero, try to fix up.
962 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100963 if (fixup_exception(regs))
964 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +0100965
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800966 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100967 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800968 * Let do_page_fault() fix it.
969 */
970 break;
971 default:
972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
974 return 0;
975}
976
977/*
978 * Wrapper routine for handling exceptions.
979 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700980int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
981 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100983 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800984 int ret = NOTIFY_DONE;
985
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100986 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -0800987 return ret;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 switch (val) {
990 case DIE_INT3:
991 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800992 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 break;
994 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +0530995 if (post_kprobe_handler(args->regs)) {
996 /*
997 * Reset the BS bit in dr6 (pointed by args->err) to
998 * denote completion of processing
999 */
1000 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001001 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +05301002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001005 /*
1006 * To be potentially processing a kprobe fault and to
1007 * trust the result from kprobe_running(), we have
1008 * be non-preemptible.
1009 */
1010 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001012 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014 default:
1015 break;
1016 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001017 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001020int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 struct jprobe *jp = container_of(p, struct jprobe, kp);
1023 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001024 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001026 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001027 kcb->jprobe_saved_sp = stack_addr(regs);
1028 addr = (unsigned long)(kcb->jprobe_saved_sp);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * As Linus pointed out, gcc assumes that the callee
1032 * owns the argument space and could overwrite it, e.g.
1033 * tailcall optimization. So, to be absolutely safe
1034 * we also save and restore enough stack bytes to cover
1035 * the argument area.
1036 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001037 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001038 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001039 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001040 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001041 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return 1;
1043}
1044
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001045void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001047 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1048
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001049 asm volatile (
1050#ifdef CONFIG_X86_64
1051 " xchg %%rbx,%%rsp \n"
1052#else
1053 " xchgl %%ebx,%%esp \n"
1054#endif
1055 " int3 \n"
1056 " .globl jprobe_return_end\n"
1057 " jprobe_return_end: \n"
1058 " nop \n"::"b"
1059 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001062int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001064 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001065 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct jprobe *jp = container_of(p, struct jprobe, kp);
1067
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001068 if ((addr > (u8 *) jprobe_return) &&
1069 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001070 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001071 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001072 printk(KERN_ERR
1073 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001074 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001075 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001077 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 show_registers(regs);
1079 BUG();
1080 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001081 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001082 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1083 kcb->jprobes_stack,
1084 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001085 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 1;
1087 }
1088 return 0;
1089}
Rusty Lynchba8af122005-06-27 15:17:10 -07001090
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001091
1092#ifdef CONFIG_OPTPROBES
1093
1094/* Insert a call instruction at address 'from', which calls address 'to'.*/
1095static void __kprobes synthesize_relcall(void *from, void *to)
1096{
1097 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1098}
1099
1100/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1101static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1102 unsigned long val)
1103{
1104#ifdef CONFIG_X86_64
1105 *addr++ = 0x48;
1106 *addr++ = 0xbf;
1107#else
1108 *addr++ = 0xb8;
1109#endif
1110 *(unsigned long *)addr = val;
1111}
1112
1113void __kprobes kprobes_optinsn_template_holder(void)
1114{
1115 asm volatile (
1116 ".global optprobe_template_entry\n"
1117 "optprobe_template_entry: \n"
1118#ifdef CONFIG_X86_64
1119 /* We don't bother saving the ss register */
1120 " pushq %rsp\n"
1121 " pushfq\n"
1122 SAVE_REGS_STRING
1123 " movq %rsp, %rsi\n"
1124 ".global optprobe_template_val\n"
1125 "optprobe_template_val: \n"
1126 ASM_NOP5
1127 ASM_NOP5
1128 ".global optprobe_template_call\n"
1129 "optprobe_template_call: \n"
1130 ASM_NOP5
1131 /* Move flags to rsp */
1132 " movq 144(%rsp), %rdx\n"
1133 " movq %rdx, 152(%rsp)\n"
1134 RESTORE_REGS_STRING
1135 /* Skip flags entry */
1136 " addq $8, %rsp\n"
1137 " popfq\n"
1138#else /* CONFIG_X86_32 */
1139 " pushf\n"
1140 SAVE_REGS_STRING
1141 " movl %esp, %edx\n"
1142 ".global optprobe_template_val\n"
1143 "optprobe_template_val: \n"
1144 ASM_NOP5
1145 ".global optprobe_template_call\n"
1146 "optprobe_template_call: \n"
1147 ASM_NOP5
1148 RESTORE_REGS_STRING
1149 " addl $4, %esp\n" /* skip cs */
1150 " popf\n"
1151#endif
1152 ".global optprobe_template_end\n"
1153 "optprobe_template_end: \n");
1154}
1155
1156#define TMPL_MOVE_IDX \
1157 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1158#define TMPL_CALL_IDX \
1159 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1160#define TMPL_END_IDX \
1161 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1162
1163#define INT3_SIZE sizeof(kprobe_opcode_t)
1164
1165/* Optimized kprobe call back function: called from optinsn */
1166static void __kprobes optimized_callback(struct optimized_kprobe *op,
1167 struct pt_regs *regs)
1168{
1169 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1170
1171 preempt_disable();
1172 if (kprobe_running()) {
1173 kprobes_inc_nmissed_count(&op->kp);
1174 } else {
1175 /* Save skipped registers */
1176#ifdef CONFIG_X86_64
1177 regs->cs = __KERNEL_CS;
1178#else
1179 regs->cs = __KERNEL_CS | get_kernel_rpl();
1180 regs->gs = 0;
1181#endif
1182 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1183 regs->orig_ax = ~0UL;
1184
1185 __get_cpu_var(current_kprobe) = &op->kp;
1186 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1187 opt_pre_handler(&op->kp, regs);
1188 __get_cpu_var(current_kprobe) = NULL;
1189 }
1190 preempt_enable_no_resched();
1191}
1192
1193static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1194{
1195 int len = 0, ret;
1196
1197 while (len < RELATIVEJUMP_SIZE) {
1198 ret = __copy_instruction(dest + len, src + len, 1);
1199 if (!ret || !can_boost(dest + len))
1200 return -EINVAL;
1201 len += ret;
1202 }
1203 /* Check whether the address range is reserved */
1204 if (ftrace_text_reserved(src, src + len - 1) ||
1205 alternatives_text_reserved(src, src + len - 1))
1206 return -EBUSY;
1207
1208 return len;
1209}
1210
1211/* Check whether insn is indirect jump */
1212static int __kprobes insn_is_indirect_jump(struct insn *insn)
1213{
1214 return ((insn->opcode.bytes[0] == 0xff &&
1215 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1216 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1217}
1218
1219/* Check whether insn jumps into specified address range */
1220static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1221{
1222 unsigned long target = 0;
1223
1224 switch (insn->opcode.bytes[0]) {
1225 case 0xe0: /* loopne */
1226 case 0xe1: /* loope */
1227 case 0xe2: /* loop */
1228 case 0xe3: /* jcxz */
1229 case 0xe9: /* near relative jump */
1230 case 0xeb: /* short relative jump */
1231 break;
1232 case 0x0f:
1233 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1234 break;
1235 return 0;
1236 default:
1237 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1238 break;
1239 return 0;
1240 }
1241 target = (unsigned long)insn->next_byte + insn->immediate.value;
1242
1243 return (start <= target && target <= start + len);
1244}
1245
1246/* Decode whole function to ensure any instructions don't jump into target */
1247static int __kprobes can_optimize(unsigned long paddr)
1248{
1249 int ret;
1250 unsigned long addr, size = 0, offset = 0;
1251 struct insn insn;
1252 kprobe_opcode_t buf[MAX_INSN_SIZE];
1253 /* Dummy buffers for lookup_symbol_attrs */
1254 static char __dummy_buf[KSYM_NAME_LEN];
1255
1256 /* Lookup symbol including addr */
1257 if (!kallsyms_lookup(paddr, &size, &offset, NULL, __dummy_buf))
1258 return 0;
1259
1260 /* Check there is enough space for a relative jump. */
1261 if (size - offset < RELATIVEJUMP_SIZE)
1262 return 0;
1263
1264 /* Decode instructions */
1265 addr = paddr - offset;
1266 while (addr < paddr - offset + size) { /* Decode until function end */
1267 if (search_exception_tables(addr))
1268 /*
1269 * Since some fixup code will jumps into this function,
1270 * we can't optimize kprobe in this function.
1271 */
1272 return 0;
1273 kernel_insn_init(&insn, (void *)addr);
1274 insn_get_opcode(&insn);
1275 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
1276 ret = recover_probed_instruction(buf, addr);
1277 if (ret)
1278 return 0;
1279 kernel_insn_init(&insn, buf);
1280 }
1281 insn_get_length(&insn);
1282 /* Recover address */
1283 insn.kaddr = (void *)addr;
1284 insn.next_byte = (void *)(addr + insn.length);
1285 /* Check any instructions don't jump into target */
1286 if (insn_is_indirect_jump(&insn) ||
1287 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1288 RELATIVE_ADDR_SIZE))
1289 return 0;
1290 addr += insn.length;
1291 }
1292
1293 return 1;
1294}
1295
1296/* Check optimized_kprobe can actually be optimized. */
1297int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1298{
1299 int i;
1300 struct kprobe *p;
1301
1302 for (i = 1; i < op->optinsn.size; i++) {
1303 p = get_kprobe(op->kp.addr + i);
1304 if (p && !kprobe_disabled(p))
1305 return -EEXIST;
1306 }
1307
1308 return 0;
1309}
1310
1311/* Check the addr is within the optimized instructions. */
1312int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1313 unsigned long addr)
1314{
1315 return ((unsigned long)op->kp.addr <= addr &&
1316 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1317}
1318
1319/* Free optimized instruction slot */
1320static __kprobes
1321void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1322{
1323 if (op->optinsn.insn) {
1324 free_optinsn_slot(op->optinsn.insn, dirty);
1325 op->optinsn.insn = NULL;
1326 op->optinsn.size = 0;
1327 }
1328}
1329
1330void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1331{
1332 __arch_remove_optimized_kprobe(op, 1);
1333}
1334
1335/*
1336 * Copy replacing target instructions
1337 * Target instructions MUST be relocatable (checked inside)
1338 */
1339int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1340{
1341 u8 *buf;
1342 int ret;
1343 long rel;
1344
1345 if (!can_optimize((unsigned long)op->kp.addr))
1346 return -EILSEQ;
1347
1348 op->optinsn.insn = get_optinsn_slot();
1349 if (!op->optinsn.insn)
1350 return -ENOMEM;
1351
1352 /*
1353 * Verify if the address gap is in 2GB range, because this uses
1354 * a relative jump.
1355 */
1356 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1357 if (abs(rel) > 0x7fffffff)
1358 return -ERANGE;
1359
1360 buf = (u8 *)op->optinsn.insn;
1361
1362 /* Copy instructions into the out-of-line buffer */
1363 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1364 if (ret < 0) {
1365 __arch_remove_optimized_kprobe(op, 0);
1366 return ret;
1367 }
1368 op->optinsn.size = ret;
1369
1370 /* Copy arch-dep-instance from template */
1371 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1372
1373 /* Set probe information */
1374 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1375
1376 /* Set probe function call */
1377 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1378
1379 /* Set returning jmp instruction at the tail of out-of-line buffer */
1380 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1381 (u8 *)op->kp.addr + op->optinsn.size);
1382
1383 flush_icache_range((unsigned long) buf,
1384 (unsigned long) buf + TMPL_END_IDX +
1385 op->optinsn.size + RELATIVEJUMP_SIZE);
1386 return 0;
1387}
1388
1389/* Replace a breakpoint (int3) with a relative jump. */
1390int __kprobes arch_optimize_kprobe(struct optimized_kprobe *op)
1391{
1392 unsigned char jmp_code[RELATIVEJUMP_SIZE];
1393 s32 rel = (s32)((long)op->optinsn.insn -
1394 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1395
1396 /* Backup instructions which will be replaced by jump address */
1397 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1398 RELATIVE_ADDR_SIZE);
1399
1400 jmp_code[0] = RELATIVEJUMP_OPCODE;
1401 *(s32 *)(&jmp_code[1]) = rel;
1402
1403 /*
1404 * text_poke_smp doesn't support NMI/MCE code modifying.
1405 * However, since kprobes itself also doesn't support NMI/MCE
1406 * code probing, it's not a problem.
1407 */
1408 text_poke_smp(op->kp.addr, jmp_code, RELATIVEJUMP_SIZE);
1409 return 0;
1410}
1411
1412/* Replace a relative jump with a breakpoint (int3). */
1413void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1414{
1415 u8 buf[RELATIVEJUMP_SIZE];
1416
1417 /* Set int3 to first byte for kprobes */
1418 buf[0] = BREAKPOINT_INSTRUCTION;
1419 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1420 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1421}
1422
1423static int __kprobes setup_detour_execution(struct kprobe *p,
1424 struct pt_regs *regs,
1425 int reenter)
1426{
1427 struct optimized_kprobe *op;
1428
1429 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1430 /* This kprobe is really able to run optimized path. */
1431 op = container_of(p, struct optimized_kprobe, kp);
1432 /* Detour through copied instructions */
1433 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1434 if (!reenter)
1435 reset_current_kprobe();
1436 preempt_enable_no_resched();
1437 return 1;
1438 }
1439 return 0;
1440}
1441#endif
1442
Rusty Lynch67729262005-07-05 18:54:50 -07001443int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001444{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +01001445 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001446}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001447
1448int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1449{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001450 return 0;
1451}