blob: 4ae95befd0eb584cbbe2408fd2e4f83f35e2826d [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>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070052
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010053#include <asm/cacheflush.h>
54#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080056#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020057#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040058#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053059#include <asm/debugreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061void jprobe_return_end(void);
62
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080063DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
64DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
H. Peter Anvin98272ed2009-10-12 14:14:10 -070066#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010067
68#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
69 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
70 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
71 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
72 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
73 << (row % 32))
74 /*
75 * Undefined/reserved opcodes, conditional jump, Opcode Extension
76 * Groups, and some special opcodes can not boost.
77 */
78static const u32 twobyte_is_boostable[256 / 32] = {
79 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
80 /* ---------------------------------------------- */
81 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
82 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
83 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
84 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
85 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
86 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
87 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
88 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
89 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
90 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
91 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
92 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
93 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
94 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
95 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
96 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
97 /* ----------------------------------------------- */
98 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
99};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100100#undef W
101
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700102struct kretprobe_blackpoint kretprobe_blacklist[] = {
103 {"__switch_to", }, /* This function switches only current task, but
104 doesn't switch kernel stack.*/
105 {NULL, NULL} /* Terminator */
106};
107const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
108
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100109/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100110static void __kprobes set_jmp_op(void *from, void *to)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100111{
112 struct __arch_jmp_op {
113 char op;
114 s32 raddr;
115 } __attribute__((packed)) * jop;
116 jop = (struct __arch_jmp_op *)from;
117 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
Masami Hiramatsud498f762010-02-25 08:33:49 -0500118 jop->op = RELATIVEJUMP_OPCODE;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100119}
120
121/*
Harvey Harrison99309272008-01-30 13:32:14 +0100122 * Check for the REX prefix which can only exist on X86_64
123 * X86_32 always returns 0
124 */
125static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
126{
127#ifdef CONFIG_X86_64
128 if ((*insn & 0xf0) == 0x40)
129 return 1;
130#endif
131 return 0;
132}
133
134/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100135 * Returns non-zero if opcode is boostable.
136 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100137 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100138static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100139{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100140 kprobe_opcode_t opcode;
141 kprobe_opcode_t *orig_opcodes = opcodes;
142
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530143 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400144 return 0; /* Page fault may occur on this address. */
145
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100146retry:
147 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
148 return 0;
149 opcode = *(opcodes++);
150
151 /* 2nd-byte opcode */
152 if (opcode == 0x0f) {
153 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
154 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100155 return test_bit(*opcodes,
156 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100157 }
158
159 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100160#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100161 case 0x40:
162 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100163#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100164 case 0x60:
165 if (0x63 < opcode && opcode < 0x67)
166 goto retry; /* prefixes */
167 /* can't boost Address-size override and bound */
168 return (opcode != 0x62 && opcode != 0x67);
169 case 0x70:
170 return 0; /* can't boost conditional jump */
171 case 0xc0:
172 /* can't boost software-interruptions */
173 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
174 case 0xd0:
175 /* can boost AA* and XLAT */
176 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
177 case 0xe0:
178 /* can boost in/out and absolute jmps */
179 return ((opcode & 0x04) || opcode == 0xea);
180 case 0xf0:
181 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
182 goto retry; /* lock/rep(ne) prefix */
183 /* clear and set flags are boostable */
184 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
185 default:
186 /* segment override prefixes are boostable */
187 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
188 goto retry; /* prefixes */
189 /* CS override prefix and call are not boostable */
190 return (opcode != 0x2e && opcode != 0x9a);
191 }
192}
193
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400194/* Recover the probed instruction at addr for further analysis. */
195static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
196{
197 struct kprobe *kp;
198 kp = get_kprobe((void *)addr);
199 if (!kp)
200 return -EINVAL;
201
202 /*
203 * Basically, kp->ainsn.insn has an original instruction.
204 * However, RIP-relative instruction can not do single-stepping
205 * at different place, fix_riprel() tweaks the displacement of
206 * that instruction. In that case, we can't recover the instruction
207 * from the kp->ainsn.insn.
208 *
209 * On the other hand, kp->opcode has a copy of the first byte of
210 * the probed instruction, which is overwritten by int3. And
211 * the instruction at kp->addr is not modified by kprobes except
212 * for the first byte, we can recover the original instruction
213 * from it and kp->opcode.
214 */
215 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
216 buf[0] = kp->opcode;
217 return 0;
218}
219
220/* Dummy buffers for kallsyms_lookup */
221static char __dummy_buf[KSYM_NAME_LEN];
222
223/* Check if paddr is at an instruction boundary */
224static int __kprobes can_probe(unsigned long paddr)
225{
226 int ret;
227 unsigned long addr, offset = 0;
228 struct insn insn;
229 kprobe_opcode_t buf[MAX_INSN_SIZE];
230
231 if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
232 return 0;
233
234 /* Decode instructions */
235 addr = paddr - offset;
236 while (addr < paddr) {
237 kernel_insn_init(&insn, (void *)addr);
238 insn_get_opcode(&insn);
239
240 /*
241 * Check if the instruction has been modified by another
242 * kprobe, in which case we replace the breakpoint by the
243 * original instruction in our buffer.
244 */
245 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
246 ret = recover_probed_instruction(buf, addr);
247 if (ret)
248 /*
249 * Another debugging subsystem might insert
250 * this breakpoint. In that case, we can't
251 * recover it.
252 */
253 return 0;
254 kernel_insn_init(&insn, buf);
255 }
256 insn_get_length(&insn);
257 addr += insn.length;
258 }
259
260 return (addr == paddr);
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100264 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
Andrew Morton86454192007-11-26 20:42:19 +0100266static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 switch (*insn) {
269 case 0xfa: /* cli */
270 case 0xfb: /* sti */
271 case 0xcf: /* iret/iretd */
272 case 0x9d: /* popf/popfd */
273 return 1;
274 }
Harvey Harrison99309272008-01-30 13:32:14 +0100275
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100276 /*
Harvey Harrison99309272008-01-30 13:32:14 +0100277 * on X86_64, 0x40-0x4f are REX prefixes so we need to look
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100278 * at the next byte instead.. but of course not recurse infinitely
279 */
Harvey Harrison99309272008-01-30 13:32:14 +0100280 if (is_REX_prefix(insn))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100281 return is_IF_modifier(++insn);
Harvey Harrison99309272008-01-30 13:32:14 +0100282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
286/*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100287 * Adjust the displacement if the instruction uses the %rip-relative
288 * addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100289 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100291 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100293static void __kprobes fix_riprel(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Harvey Harrison31f80e42008-01-30 13:32:16 +0100295#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400296 struct insn insn;
297 kernel_insn_init(&insn, p->ainsn.insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400299 if (insn_rip_relative(&insn)) {
300 s64 newdisp;
301 u8 *disp;
302 insn_get_displacement(&insn);
303 /*
304 * The copied instruction uses the %rip-relative addressing
305 * mode. Adjust the displacement for the difference between
306 * the original location of this instruction and the location
307 * of the copy that will actually be run. The tricky bit here
308 * is making sure that the sign extension happens correctly in
309 * this calculation, since we need a signed 32-bit result to
310 * be sign-extended to 64 bits when it's added to the %rip
311 * value and yield the same 64-bit result that the sign-
312 * extension of the original signed 32-bit displacement would
313 * have given.
314 */
315 newdisp = (u8 *) p->addr + (s64) insn.displacement.value -
316 (u8 *) p->ainsn.insn;
317 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
318 disp = (u8 *) p->ainsn.insn + insn_offset_displacement(&insn);
319 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100321#endif
Harvey Harrison31f80e42008-01-30 13:32:16 +0100322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800324static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100326 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Harvey Harrison31f80e42008-01-30 13:32:16 +0100327
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100328 fix_riprel(p);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100329
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100330 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100331 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100332 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100333 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100334
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700335 p->opcode = *p->addr;
336}
337
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100338int __kprobes arch_prepare_kprobe(struct kprobe *p)
339{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500340 if (alternatives_text_reserved(p->addr, p->addr))
341 return -EINVAL;
342
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400343 if (!can_probe((unsigned long)p->addr))
344 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100345 /* insn: must be on special executable page on x86. */
346 p->ainsn.insn = get_insn_slot();
347 if (!p->ainsn.insn)
348 return -ENOMEM;
349 arch_copy_kprobe(p);
350 return 0;
351}
352
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700353void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700354{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200355 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700356}
357
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700358void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700359{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200360 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800363void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800365 if (p->ainsn.insn) {
366 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
367 p->ainsn.insn = NULL;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700371static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700372{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800373 kcb->prev_kprobe.kp = kprobe_running();
374 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100375 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
376 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700377}
378
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700379static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700380{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800381 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
382 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100383 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
384 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700385}
386
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700387static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800388 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700389{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800390 __get_cpu_var(current_kprobe) = p;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100391 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100392 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700393 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100394 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700395}
396
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100397static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100398{
399 if (test_thread_flag(TIF_DEBUGCTLMSR))
Jan Beulich5b0e5082008-03-10 13:11:17 +0000400 update_debugctlmsr(0);
Roland McGrath1ecc7982008-01-30 13:30:54 +0100401}
402
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100403static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100404{
405 if (test_thread_flag(TIF_DEBUGCTLMSR))
Jan Beulich5b0e5082008-03-10 13:11:17 +0000406 update_debugctlmsr(current->thread.debugctlmsr);
Roland McGrath1ecc7982008-01-30 13:30:54 +0100407}
408
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700409void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700410 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700411{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100412 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700413
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700414 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100415
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700416 /* Replace the return addr with trampoline addr */
417 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700418}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100419
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100420static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500421 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100422{
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500423#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100424 if (p->ainsn.boostable == 1 && !p->post_handler) {
425 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500426 if (!reenter)
427 reset_current_kprobe();
428 /*
429 * Reentering boosted probe doesn't reset current_kprobe,
430 * nor set current_kprobe, because it doesn't use single
431 * stepping.
432 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100433 regs->ip = (unsigned long)p->ainsn.insn;
434 preempt_enable_no_resched();
435 return;
436 }
437#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500438 if (reenter) {
439 save_previous_kprobe(kcb);
440 set_current_kprobe(p, regs, kcb);
441 kcb->kprobe_status = KPROBE_REENTER;
442 } else
443 kcb->kprobe_status = KPROBE_HIT_SS;
444 /* Prepare real single stepping */
445 clear_btf();
446 regs->flags |= X86_EFLAGS_TF;
447 regs->flags &= ~X86_EFLAGS_IF;
448 /* single step inline if the instruction is an int3 */
449 if (p->opcode == BREAKPOINT_INSTRUCTION)
450 regs->ip = (unsigned long)p->addr;
451 else
452 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100453}
454
Harvey Harrison40102d42008-01-30 13:32:02 +0100455/*
456 * We have reentered the kprobe_handler(), since another probe was hit while
457 * within the handler. We save the original kprobes variables and just single
458 * step on the instruction of the new probe without calling any user handlers.
459 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100460static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
461 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100462{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100463 switch (kcb->kprobe_status) {
464 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100465 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100466 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500467 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100468 break;
469 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400470 /* A probe has been hit in the codepath leading up to, or just
471 * after, single-stepping of a probed instruction. This entire
472 * codepath should strictly reside in .kprobes.text section.
473 * Raise a BUG or we'll continue in an endless reentering loop
474 * and eventually a stack overflow.
475 */
476 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
477 p->addr);
478 dump_kprobe(p);
479 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100480 default:
481 /* impossible cases */
482 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100483 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100484 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100485
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100486 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100487}
Rusty Lynch73649da2005-06-23 00:09:23 -0700488
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100489/*
490 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200491 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100492 */
493static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100495 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100496 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800497 struct kprobe_ctlblk *kcb;
498
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100499 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100500 if (*addr != BREAKPOINT_INSTRUCTION) {
501 /*
502 * The breakpoint instruction was removed right
503 * after we hit it. Another cpu has removed
504 * either a probepoint or a debugger breakpoint
505 * at this address. In either case, no further
506 * handling of this interrupt is appropriate.
507 * Back up over the (now missing) int3 and run
508 * the original instruction.
509 */
510 regs->ip = (unsigned long)addr;
511 return 1;
512 }
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100513
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800514 /*
515 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100516 * duration of kprobe processing. We conditionally
517 * re-enable preemption at the end of this function,
518 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800519 */
520 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100522 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100523 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100524
Harvey Harrisonb9760152008-01-30 13:32:19 +0100525 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100526 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100527 if (reenter_kprobe(p, regs, kcb))
528 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100529 } else {
530 set_current_kprobe(p, regs, kcb);
531 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100534 * If we have no pre-handler or it returned 0, we
535 * continue with normal processing. If we have a
536 * pre-handler and it returned non-zero, it prepped
537 * for calling the break_handler below on re-entry
538 * for jprobe processing, so get out doing nothing
539 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100541 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500542 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100543 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100544 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100545 } else if (kprobe_running()) {
546 p = __get_cpu_var(current_kprobe);
547 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500548 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100549 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100551 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800553 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500557#ifdef CONFIG_X86_64
558#define SAVE_REGS_STRING \
559 /* Skip cs, ip, orig_ax. */ \
560 " subq $24, %rsp\n" \
561 " pushq %rdi\n" \
562 " pushq %rsi\n" \
563 " pushq %rdx\n" \
564 " pushq %rcx\n" \
565 " pushq %rax\n" \
566 " pushq %r8\n" \
567 " pushq %r9\n" \
568 " pushq %r10\n" \
569 " pushq %r11\n" \
570 " pushq %rbx\n" \
571 " pushq %rbp\n" \
572 " pushq %r12\n" \
573 " pushq %r13\n" \
574 " pushq %r14\n" \
575 " pushq %r15\n"
576#define RESTORE_REGS_STRING \
577 " popq %r15\n" \
578 " popq %r14\n" \
579 " popq %r13\n" \
580 " popq %r12\n" \
581 " popq %rbp\n" \
582 " popq %rbx\n" \
583 " popq %r11\n" \
584 " popq %r10\n" \
585 " popq %r9\n" \
586 " popq %r8\n" \
587 " popq %rax\n" \
588 " popq %rcx\n" \
589 " popq %rdx\n" \
590 " popq %rsi\n" \
591 " popq %rdi\n" \
592 /* Skip orig_ax, ip, cs */ \
593 " addq $24, %rsp\n"
594#else
595#define SAVE_REGS_STRING \
596 /* Skip cs, ip, orig_ax and gs. */ \
597 " subl $16, %esp\n" \
598 " pushl %fs\n" \
599 " pushl %ds\n" \
600 " pushl %es\n" \
601 " pushl %eax\n" \
602 " pushl %ebp\n" \
603 " pushl %edi\n" \
604 " pushl %esi\n" \
605 " pushl %edx\n" \
606 " pushl %ecx\n" \
607 " pushl %ebx\n"
608#define RESTORE_REGS_STRING \
609 " popl %ebx\n" \
610 " popl %ecx\n" \
611 " popl %edx\n" \
612 " popl %esi\n" \
613 " popl %edi\n" \
614 " popl %ebp\n" \
615 " popl %eax\n" \
616 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
617 " addl $24, %esp\n"
618#endif
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100621 * When a retprobed function returns, this code saves registers and
622 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700623 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800624static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100625{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100626 asm volatile (
627 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100628 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100629#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100630 /* We don't bother saving the ss register */
631 " pushq %rsp\n"
632 " pushfq\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500633 SAVE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100634 " movq %rsp, %rdi\n"
635 " call trampoline_handler\n"
636 /* Replace saved sp with true return address. */
637 " movq %rax, 152(%rsp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500638 RESTORE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100639 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100640#else
641 " pushf\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500642 SAVE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100643 " movl %esp, %eax\n"
644 " call trampoline_handler\n"
645 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400646 " movl 56(%esp), %edx\n"
647 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100648 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400649 " movl %eax, 56(%esp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500650 RESTORE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100651 " popf\n"
652#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100653 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100654}
Rusty Lynch73649da2005-06-23 00:09:23 -0700655
656/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100657 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700658 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800659static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700660{
bibo,mao62c27be2006-10-02 02:17:33 -0700661 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700662 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700663 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800664 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100665 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700666
bibo,mao99219a32006-10-02 02:17:35 -0700667 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700668 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100669 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100670#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100671 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100672#else
673 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400674 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100675#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100676 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100677 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700678
Rusty Lynchba8af122005-06-27 15:17:10 -0700679 /*
680 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100681 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200682 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700683 * return probe was registered for a target function.
684 *
685 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100686 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700687 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100688 * function, the (chronologically) first instance's ret_addr
689 * will be the real return address, and all the rest will
690 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700691 */
692 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700693 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700694 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700695 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700696
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100697 if (ri->rp && ri->rp->handler) {
698 __get_cpu_var(current_kprobe) = &ri->rp->kp;
699 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
Rusty Lynchba8af122005-06-27 15:17:10 -0700700 ri->rp->handler(ri, regs);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100701 __get_cpu_var(current_kprobe) = NULL;
702 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700703
Rusty Lynchba8af122005-06-27 15:17:10 -0700704 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700705 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700706
707 if (orig_ret_address != trampoline_address)
708 /*
709 * This is the real return address. Any other
710 * instances associated with this task are for
711 * other calls deeper on the call stack
712 */
713 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700714 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700715
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700716 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700717
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700718 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700719
bibo,mao99219a32006-10-02 02:17:35 -0700720 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
721 hlist_del(&ri->hlist);
722 kfree(ri);
723 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100724 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700725}
726
727/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * Called after single-stepping. p->addr is the address of the
729 * instruction whose first byte has been replaced by the "int 3"
730 * instruction. To avoid the SMP problems that can occur when we
731 * temporarily put back the original opcode to single-step, we
732 * single-stepped a copy of the instruction. The address of this
733 * copy is p->ainsn.insn.
734 *
735 * This function prepares to return from the post-single-step
736 * interrupt. We have to fix up the stack as follows:
737 *
738 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100739 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * it relative to the original instruction.
741 *
742 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100743 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *
745 * 2) If the single-stepped instruction was a call, the return address
746 * that is atop the stack is the address following the copied instruction.
747 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100748 *
749 * If this is the first time we've single-stepped the instruction at
750 * this probepoint, and the instruction is boostable, boost it: add a
751 * jump instruction after the copied instruction, that jumps to the next
752 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800754static void __kprobes resume_execution(struct kprobe *p,
755 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100757 unsigned long *tos = stack_addr(regs);
758 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
759 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kprobe_opcode_t *insn = p->ainsn.insn;
761
762 /*skip the REX prefix*/
Harvey Harrison99309272008-01-30 13:32:14 +0100763 if (is_REX_prefix(insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 insn++;
765
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100766 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100768 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100769 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100770 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100772 case 0xc2: /* iret/ret/lret */
773 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700774 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100775 case 0xcb:
776 case 0xcf:
777 case 0xea: /* jmp absolute -- ip is correct */
778 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100779 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100780 goto no_change;
781 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100782 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100784#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100785 case 0x9a: /* call absolute -- same as call absolute, indirect */
786 *tos = orig_ip + (*tos - copy_ip);
787 goto no_change;
788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700790 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100791 /*
792 * call absolute, indirect
793 * Fix return addr; ip is correct.
794 * But this is not boostable
795 */
796 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100797 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100798 } else if (((insn[1] & 0x31) == 0x20) ||
799 ((insn[1] & 0x31) == 0x21)) {
800 /*
801 * jmp near and far, absolute indirect
802 * ip is correct. And this is boostable
803 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100804 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100805 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 default:
808 break;
809 }
810
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100811 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100812 if ((regs->ip > copy_ip) &&
813 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100814 /*
815 * These instructions can be executed directly if it
816 * jumps back to correct address.
817 */
818 set_jmp_op((void *)regs->ip,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100819 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100820 p->ainsn.boostable = 1;
821 } else {
822 p->ainsn.boostable = -1;
823 }
824 }
825
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100826 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100827
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100828no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100829 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100832/*
833 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200834 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100835 */
836static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800838 struct kprobe *cur = kprobe_running();
839 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
840
841 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 0;
843
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500844 resume_execution(cur, regs, kcb);
845 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500846
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800847 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
848 kcb->kprobe_status = KPROBE_HIT_SSDONE;
849 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100852 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800853 if (kcb->kprobe_status == KPROBE_REENTER) {
854 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700855 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700856 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800857 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700858out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 preempt_enable_no_resched();
860
861 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100862 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * will have TF set, in which case, continue the remaining processing
864 * of do_debug, as if this is not a probe hit.
865 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100866 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868
869 return 1;
870}
871
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700872int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800874 struct kprobe *cur = kprobe_running();
875 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
876
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100877 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800878 case KPROBE_HIT_SS:
879 case KPROBE_REENTER:
880 /*
881 * We are here because the instruction being single
882 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100883 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800884 * and allow the page fault handler to continue as a
885 * normal page fault.
886 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100887 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100888 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800889 if (kcb->kprobe_status == KPROBE_REENTER)
890 restore_previous_kprobe(kcb);
891 else
892 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800894 break;
895 case KPROBE_HIT_ACTIVE:
896 case KPROBE_HIT_SSDONE:
897 /*
898 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100899 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800900 * these specific fault cases.
901 */
902 kprobes_inc_nmissed_count(cur);
903
904 /*
905 * We come here because instructions in the pre/post
906 * handler caused the page_fault, this could happen
907 * if handler tries to access user space by
908 * copy_from_user(), get_user() etc. Let the
909 * user-specified handler try to fix it first.
910 */
911 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
912 return 1;
913
914 /*
915 * In case the user-specified fault handler returned
916 * zero, try to fix up.
917 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100918 if (fixup_exception(regs))
919 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +0100920
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800921 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100922 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800923 * Let do_page_fault() fix it.
924 */
925 break;
926 default:
927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 return 0;
930}
931
932/*
933 * Wrapper routine for handling exceptions.
934 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700935int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
936 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100938 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800939 int ret = NOTIFY_DONE;
940
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100941 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -0800942 return ret;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 switch (val) {
945 case DIE_INT3:
946 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800947 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 break;
949 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +0530950 if (post_kprobe_handler(args->regs)) {
951 /*
952 * Reset the BS bit in dr6 (pointed by args->err) to
953 * denote completion of processing
954 */
955 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800956 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +0530957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
959 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +0100960 /*
961 * To be potentially processing a kprobe fault and to
962 * trust the result from kprobe_running(), we have
963 * be non-preemptible.
964 */
965 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800967 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 break;
969 default:
970 break;
971 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700975int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct jprobe *jp = container_of(p, struct jprobe, kp);
978 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800979 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800981 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100982 kcb->jprobe_saved_sp = stack_addr(regs);
983 addr = (unsigned long)(kcb->jprobe_saved_sp);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /*
986 * As Linus pointed out, gcc assumes that the callee
987 * owns the argument space and could overwrite it, e.g.
988 * tailcall optimization. So, to be absolutely safe
989 * we also save and restore enough stack bytes to cover
990 * the argument area.
991 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800992 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100993 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100994 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +0200995 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100996 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 1;
998}
999
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001000void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001002 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1003
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001004 asm volatile (
1005#ifdef CONFIG_X86_64
1006 " xchg %%rbx,%%rsp \n"
1007#else
1008 " xchgl %%ebx,%%esp \n"
1009#endif
1010 " int3 \n"
1011 " .globl jprobe_return_end\n"
1012 " jprobe_return_end: \n"
1013 " nop \n"::"b"
1014 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001017int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001019 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001020 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 struct jprobe *jp = container_of(p, struct jprobe, kp);
1022
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001023 if ((addr > (u8 *) jprobe_return) &&
1024 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001025 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001026 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001027 printk(KERN_ERR
1028 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001029 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001030 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001032 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 show_registers(regs);
1034 BUG();
1035 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001036 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001037 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1038 kcb->jprobes_stack,
1039 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001040 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 1;
1042 }
1043 return 0;
1044}
Rusty Lynchba8af122005-06-27 15:17:10 -07001045
Rusty Lynch67729262005-07-05 18:54:50 -07001046int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001047{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +01001048 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001049}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001050
1051int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1052{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001053 return 0;
1054}