blob: 3fe86d706a1493ad59cb655478bd9b41cfd11f4d [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
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010066#ifdef CONFIG_X86_64
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010067#define stack_addr(regs) ((unsigned long *)regs->sp)
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010068#else
69/*
70 * "&regs->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs
71 * don't save the ss and esp registers if the CPU is already in kernel
72 * mode when it traps. So for kprobes, regs->sp and regs->ss are not
73 * the [nonexistent] saved stack pointer and ss register, but rather
74 * the top 8 bytes of the pre-int3 stack. So &regs->sp happens to
75 * point to the top of the pre-int3 stack.
76 */
77#define stack_addr(regs) ((unsigned long *)&regs->sp)
78#endif
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010079
80#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
81 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
82 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
83 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
84 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
85 << (row % 32))
86 /*
87 * Undefined/reserved opcodes, conditional jump, Opcode Extension
88 * Groups, and some special opcodes can not boost.
89 */
90static const u32 twobyte_is_boostable[256 / 32] = {
91 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
92 /* ---------------------------------------------- */
93 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
94 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
95 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
96 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
97 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
98 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
99 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
100 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
101 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
102 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
103 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
104 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
105 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
106 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
107 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
108 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
109 /* ----------------------------------------------- */
110 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
111};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100112#undef W
113
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700114struct kretprobe_blackpoint kretprobe_blacklist[] = {
115 {"__switch_to", }, /* This function switches only current task, but
116 doesn't switch kernel stack.*/
117 {NULL, NULL} /* Terminator */
118};
119const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
120
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100121/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100122static void __kprobes set_jmp_op(void *from, void *to)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100123{
124 struct __arch_jmp_op {
125 char op;
126 s32 raddr;
127 } __attribute__((packed)) * jop;
128 jop = (struct __arch_jmp_op *)from;
129 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
130 jop->op = RELATIVEJUMP_INSTRUCTION;
131}
132
133/*
Harvey Harrison99309272008-01-30 13:32:14 +0100134 * Check for the REX prefix which can only exist on X86_64
135 * X86_32 always returns 0
136 */
137static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
138{
139#ifdef CONFIG_X86_64
140 if ((*insn & 0xf0) == 0x40)
141 return 1;
142#endif
143 return 0;
144}
145
146/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100147 * Returns non-zero if opcode is boostable.
148 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100149 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100150static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100151{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100152 kprobe_opcode_t opcode;
153 kprobe_opcode_t *orig_opcodes = opcodes;
154
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530155 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400156 return 0; /* Page fault may occur on this address. */
157
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100158retry:
159 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
160 return 0;
161 opcode = *(opcodes++);
162
163 /* 2nd-byte opcode */
164 if (opcode == 0x0f) {
165 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
166 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100167 return test_bit(*opcodes,
168 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100169 }
170
171 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100172#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100173 case 0x40:
174 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100175#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100176 case 0x60:
177 if (0x63 < opcode && opcode < 0x67)
178 goto retry; /* prefixes */
179 /* can't boost Address-size override and bound */
180 return (opcode != 0x62 && opcode != 0x67);
181 case 0x70:
182 return 0; /* can't boost conditional jump */
183 case 0xc0:
184 /* can't boost software-interruptions */
185 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
186 case 0xd0:
187 /* can boost AA* and XLAT */
188 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
189 case 0xe0:
190 /* can boost in/out and absolute jmps */
191 return ((opcode & 0x04) || opcode == 0xea);
192 case 0xf0:
193 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
194 goto retry; /* lock/rep(ne) prefix */
195 /* clear and set flags are boostable */
196 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
197 default:
198 /* segment override prefixes are boostable */
199 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
200 goto retry; /* prefixes */
201 /* CS override prefix and call are not boostable */
202 return (opcode != 0x2e && opcode != 0x9a);
203 }
204}
205
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400206/* Recover the probed instruction at addr for further analysis. */
207static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
208{
209 struct kprobe *kp;
210 kp = get_kprobe((void *)addr);
211 if (!kp)
212 return -EINVAL;
213
214 /*
215 * Basically, kp->ainsn.insn has an original instruction.
216 * However, RIP-relative instruction can not do single-stepping
217 * at different place, fix_riprel() tweaks the displacement of
218 * that instruction. In that case, we can't recover the instruction
219 * from the kp->ainsn.insn.
220 *
221 * On the other hand, kp->opcode has a copy of the first byte of
222 * the probed instruction, which is overwritten by int3. And
223 * the instruction at kp->addr is not modified by kprobes except
224 * for the first byte, we can recover the original instruction
225 * from it and kp->opcode.
226 */
227 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
228 buf[0] = kp->opcode;
229 return 0;
230}
231
232/* Dummy buffers for kallsyms_lookup */
233static char __dummy_buf[KSYM_NAME_LEN];
234
235/* Check if paddr is at an instruction boundary */
236static int __kprobes can_probe(unsigned long paddr)
237{
238 int ret;
239 unsigned long addr, offset = 0;
240 struct insn insn;
241 kprobe_opcode_t buf[MAX_INSN_SIZE];
242
243 if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
244 return 0;
245
246 /* Decode instructions */
247 addr = paddr - offset;
248 while (addr < paddr) {
249 kernel_insn_init(&insn, (void *)addr);
250 insn_get_opcode(&insn);
251
252 /*
253 * Check if the instruction has been modified by another
254 * kprobe, in which case we replace the breakpoint by the
255 * original instruction in our buffer.
256 */
257 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
258 ret = recover_probed_instruction(buf, addr);
259 if (ret)
260 /*
261 * Another debugging subsystem might insert
262 * this breakpoint. In that case, we can't
263 * recover it.
264 */
265 return 0;
266 kernel_insn_init(&insn, buf);
267 }
268 insn_get_length(&insn);
269 addr += insn.length;
270 }
271
272 return (addr == paddr);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100276 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Andrew Morton86454192007-11-26 20:42:19 +0100278static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 switch (*insn) {
281 case 0xfa: /* cli */
282 case 0xfb: /* sti */
283 case 0xcf: /* iret/iretd */
284 case 0x9d: /* popf/popfd */
285 return 1;
286 }
Harvey Harrison99309272008-01-30 13:32:14 +0100287
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100288 /*
Harvey Harrison99309272008-01-30 13:32:14 +0100289 * on X86_64, 0x40-0x4f are REX prefixes so we need to look
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100290 * at the next byte instead.. but of course not recurse infinitely
291 */
Harvey Harrison99309272008-01-30 13:32:14 +0100292 if (is_REX_prefix(insn))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100293 return is_IF_modifier(++insn);
Harvey Harrison99309272008-01-30 13:32:14 +0100294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
298/*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100299 * Adjust the displacement if the instruction uses the %rip-relative
300 * addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100301 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100303 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100305static void __kprobes fix_riprel(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Harvey Harrison31f80e42008-01-30 13:32:16 +0100307#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400308 struct insn insn;
309 kernel_insn_init(&insn, p->ainsn.insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400311 if (insn_rip_relative(&insn)) {
312 s64 newdisp;
313 u8 *disp;
314 insn_get_displacement(&insn);
315 /*
316 * The copied instruction uses the %rip-relative addressing
317 * mode. Adjust the displacement for the difference between
318 * the original location of this instruction and the location
319 * of the copy that will actually be run. The tricky bit here
320 * is making sure that the sign extension happens correctly in
321 * this calculation, since we need a signed 32-bit result to
322 * be sign-extended to 64 bits when it's added to the %rip
323 * value and yield the same 64-bit result that the sign-
324 * extension of the original signed 32-bit displacement would
325 * have given.
326 */
327 newdisp = (u8 *) p->addr + (s64) insn.displacement.value -
328 (u8 *) p->ainsn.insn;
329 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
330 disp = (u8 *) p->ainsn.insn + insn_offset_displacement(&insn);
331 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100333#endif
Harvey Harrison31f80e42008-01-30 13:32:16 +0100334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800336static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100338 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Harvey Harrison31f80e42008-01-30 13:32:16 +0100339
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100340 fix_riprel(p);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100341
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100342 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100343 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100344 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100345 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100346
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700347 p->opcode = *p->addr;
348}
349
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100350int __kprobes arch_prepare_kprobe(struct kprobe *p)
351{
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400352 if (!can_probe((unsigned long)p->addr))
353 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100354 /* insn: must be on special executable page on x86. */
355 p->ainsn.insn = get_insn_slot();
356 if (!p->ainsn.insn)
357 return -ENOMEM;
358 arch_copy_kprobe(p);
359 return 0;
360}
361
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700362void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700363{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200364 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700365}
366
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700367void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700368{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200369 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800372void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800374 if (p->ainsn.insn) {
375 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
376 p->ainsn.insn = NULL;
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700380static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700381{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800382 kcb->prev_kprobe.kp = kprobe_running();
383 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100384 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
385 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700386}
387
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700388static void __kprobes restore_previous_kprobe(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) = kcb->prev_kprobe.kp;
391 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100392 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
393 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700394}
395
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700396static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800397 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700398{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800399 __get_cpu_var(current_kprobe) = p;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100400 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100401 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700402 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100403 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700404}
405
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100406static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100407{
408 if (test_thread_flag(TIF_DEBUGCTLMSR))
Jan Beulich5b0e5082008-03-10 13:11:17 +0000409 update_debugctlmsr(0);
Roland McGrath1ecc7982008-01-30 13:30:54 +0100410}
411
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100412static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100413{
414 if (test_thread_flag(TIF_DEBUGCTLMSR))
Jan Beulich5b0e5082008-03-10 13:11:17 +0000415 update_debugctlmsr(current->thread.debugctlmsr);
Roland McGrath1ecc7982008-01-30 13:30:54 +0100416}
417
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700418static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Roland McGrath1ecc7982008-01-30 13:30:54 +0100420 clear_btf();
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100421 regs->flags |= X86_EFLAGS_TF;
422 regs->flags &= ~X86_EFLAGS_IF;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100423 /* single step inline if the instruction is an int3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (p->opcode == BREAKPOINT_INSTRUCTION)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100425 regs->ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100427 regs->ip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700430void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700431 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700432{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100433 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700434
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700435 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100436
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700437 /* Replace the return addr with trampoline addr */
438 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700439}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100440
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100441static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
442 struct kprobe_ctlblk *kcb)
443{
Masami Hiramatsu5a4ccaf2009-01-06 21:15:32 +0100444#if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100445 if (p->ainsn.boostable == 1 && !p->post_handler) {
446 /* Boost up -- we can execute copied instructions directly */
447 reset_current_kprobe();
448 regs->ip = (unsigned long)p->ainsn.insn;
449 preempt_enable_no_resched();
450 return;
451 }
452#endif
453 prepare_singlestep(p, regs);
454 kcb->kprobe_status = KPROBE_HIT_SS;
455}
456
Harvey Harrison40102d42008-01-30 13:32:02 +0100457/*
458 * We have reentered the kprobe_handler(), since another probe was hit while
459 * within the handler. We save the original kprobes variables and just single
460 * step on the instruction of the new probe without calling any user handlers.
461 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100462static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
463 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100464{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100465 switch (kcb->kprobe_status) {
466 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100467 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100468 save_previous_kprobe(kcb);
469 set_current_kprobe(p, regs, kcb);
470 kprobes_inc_nmissed_count(p);
471 prepare_singlestep(p, regs);
472 kcb->kprobe_status = KPROBE_REENTER;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100473 break;
474 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400475 /* A probe has been hit in the codepath leading up to, or just
476 * after, single-stepping of a probed instruction. This entire
477 * codepath should strictly reside in .kprobes.text section.
478 * Raise a BUG or we'll continue in an endless reentering loop
479 * and eventually a stack overflow.
480 */
481 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
482 p->addr);
483 dump_kprobe(p);
484 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100485 default:
486 /* impossible cases */
487 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100488 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100489 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100490
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100491 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100492}
Rusty Lynch73649da2005-06-23 00:09:23 -0700493
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100494/*
495 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
496 * remain disabled thorough out this function.
497 */
498static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100500 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100501 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800502 struct kprobe_ctlblk *kcb;
503
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100504 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100505 if (*addr != BREAKPOINT_INSTRUCTION) {
506 /*
507 * The breakpoint instruction was removed right
508 * after we hit it. Another cpu has removed
509 * either a probepoint or a debugger breakpoint
510 * at this address. In either case, no further
511 * handling of this interrupt is appropriate.
512 * Back up over the (now missing) int3 and run
513 * the original instruction.
514 */
515 regs->ip = (unsigned long)addr;
516 return 1;
517 }
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100518
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800519 /*
520 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100521 * duration of kprobe processing. We conditionally
522 * re-enable preemption at the end of this function,
523 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800524 */
525 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100527 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100528 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100529
Harvey Harrisonb9760152008-01-30 13:32:19 +0100530 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100531 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100532 if (reenter_kprobe(p, regs, kcb))
533 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100534 } else {
535 set_current_kprobe(p, regs, kcb);
536 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100539 * If we have no pre-handler or it returned 0, we
540 * continue with normal processing. If we have a
541 * pre-handler and it returned non-zero, it prepped
542 * for calling the break_handler below on re-entry
543 * for jprobe processing, so get out doing nothing
544 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100546 if (!p->pre_handler || !p->pre_handler(p, regs))
547 setup_singlestep(p, regs, kcb);
548 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100549 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100550 } else if (kprobe_running()) {
551 p = __get_cpu_var(current_kprobe);
552 if (p->break_handler && p->break_handler(p, regs)) {
553 setup_singlestep(p, regs, kcb);
554 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100556 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800558 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100563 * When a retprobed function returns, this code saves registers and
564 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700565 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800566static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100567{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100568 asm volatile (
569 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100570 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100571#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100572 /* We don't bother saving the ss register */
573 " pushq %rsp\n"
574 " pushfq\n"
575 /*
576 * Skip cs, ip, orig_ax.
577 * trampoline_handler() will plug in these values
578 */
579 " subq $24, %rsp\n"
580 " pushq %rdi\n"
581 " pushq %rsi\n"
582 " pushq %rdx\n"
583 " pushq %rcx\n"
584 " pushq %rax\n"
585 " pushq %r8\n"
586 " pushq %r9\n"
587 " pushq %r10\n"
588 " pushq %r11\n"
589 " pushq %rbx\n"
590 " pushq %rbp\n"
591 " pushq %r12\n"
592 " pushq %r13\n"
593 " pushq %r14\n"
594 " pushq %r15\n"
595 " movq %rsp, %rdi\n"
596 " call trampoline_handler\n"
597 /* Replace saved sp with true return address. */
598 " movq %rax, 152(%rsp)\n"
599 " popq %r15\n"
600 " popq %r14\n"
601 " popq %r13\n"
602 " popq %r12\n"
603 " popq %rbp\n"
604 " popq %rbx\n"
605 " popq %r11\n"
606 " popq %r10\n"
607 " popq %r9\n"
608 " popq %r8\n"
609 " popq %rax\n"
610 " popq %rcx\n"
611 " popq %rdx\n"
612 " popq %rsi\n"
613 " popq %rdi\n"
614 /* Skip orig_ax, ip, cs */
615 " addq $24, %rsp\n"
616 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100617#else
618 " pushf\n"
619 /*
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400620 * Skip cs, ip, orig_ax and gs.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100621 * trampoline_handler() will plug in these values
622 */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400623 " subl $16, %esp\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100624 " pushl %fs\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100625 " pushl %es\n"
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400626 " pushl %ds\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100627 " pushl %eax\n"
628 " pushl %ebp\n"
629 " pushl %edi\n"
630 " pushl %esi\n"
631 " pushl %edx\n"
632 " pushl %ecx\n"
633 " pushl %ebx\n"
634 " movl %esp, %eax\n"
635 " call trampoline_handler\n"
636 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400637 " movl 56(%esp), %edx\n"
638 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100639 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400640 " movl %eax, 56(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100641 " popl %ebx\n"
642 " popl %ecx\n"
643 " popl %edx\n"
644 " popl %esi\n"
645 " popl %edi\n"
646 " popl %ebp\n"
647 " popl %eax\n"
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400648 /* Skip ds, es, fs, gs, orig_ax and ip */
649 " addl $24, %esp\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100650 " popf\n"
651#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100652 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100653}
Rusty Lynch73649da2005-06-23 00:09:23 -0700654
655/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100656 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700657 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800658static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700659{
bibo,mao62c27be2006-10-02 02:17:33 -0700660 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700661 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700662 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800663 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100664 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700665
bibo,mao99219a32006-10-02 02:17:35 -0700666 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700667 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100668 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100669#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100670 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100671#else
672 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400673 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100674#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100675 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100676 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700677
Rusty Lynchba8af122005-06-27 15:17:10 -0700678 /*
679 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100680 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200681 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700682 * return probe was registered for a target function.
683 *
684 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100685 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700686 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100687 * function, the (chronologically) first instance's ret_addr
688 * will be the real return address, and all the rest will
689 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700690 */
691 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700692 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700693 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700694 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700695
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100696 if (ri->rp && ri->rp->handler) {
697 __get_cpu_var(current_kprobe) = &ri->rp->kp;
698 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
Rusty Lynchba8af122005-06-27 15:17:10 -0700699 ri->rp->handler(ri, regs);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100700 __get_cpu_var(current_kprobe) = NULL;
701 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700702
Rusty Lynchba8af122005-06-27 15:17:10 -0700703 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700704 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700705
706 if (orig_ret_address != trampoline_address)
707 /*
708 * This is the real return address. Any other
709 * instances associated with this task are for
710 * other calls deeper on the call stack
711 */
712 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700713 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700714
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700715 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700716
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700717 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700718
bibo,mao99219a32006-10-02 02:17:35 -0700719 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
720 hlist_del(&ri->hlist);
721 kfree(ri);
722 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100723 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700724}
725
726/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * Called after single-stepping. p->addr is the address of the
728 * instruction whose first byte has been replaced by the "int 3"
729 * instruction. To avoid the SMP problems that can occur when we
730 * temporarily put back the original opcode to single-step, we
731 * single-stepped a copy of the instruction. The address of this
732 * copy is p->ainsn.insn.
733 *
734 * This function prepares to return from the post-single-step
735 * interrupt. We have to fix up the stack as follows:
736 *
737 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100738 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * it relative to the original instruction.
740 *
741 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100742 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
744 * 2) If the single-stepped instruction was a call, the return address
745 * that is atop the stack is the address following the copied instruction.
746 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100747 *
748 * If this is the first time we've single-stepped the instruction at
749 * this probepoint, and the instruction is boostable, boost it: add a
750 * jump instruction after the copied instruction, that jumps to the next
751 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800753static void __kprobes resume_execution(struct kprobe *p,
754 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100756 unsigned long *tos = stack_addr(regs);
757 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
758 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 kprobe_opcode_t *insn = p->ainsn.insn;
760
761 /*skip the REX prefix*/
Harvey Harrison99309272008-01-30 13:32:14 +0100762 if (is_REX_prefix(insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 insn++;
764
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100765 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100767 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100768 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100769 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100771 case 0xc2: /* iret/ret/lret */
772 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700773 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100774 case 0xcb:
775 case 0xcf:
776 case 0xea: /* jmp absolute -- ip is correct */
777 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100778 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100779 goto no_change;
780 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100781 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100783#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100784 case 0x9a: /* call absolute -- same as call absolute, indirect */
785 *tos = orig_ip + (*tos - copy_ip);
786 goto no_change;
787#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700789 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100790 /*
791 * call absolute, indirect
792 * Fix return addr; ip is correct.
793 * But this is not boostable
794 */
795 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100796 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100797 } else if (((insn[1] & 0x31) == 0x20) ||
798 ((insn[1] & 0x31) == 0x21)) {
799 /*
800 * jmp near and far, absolute indirect
801 * ip is correct. And this is boostable
802 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100803 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100804 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 default:
807 break;
808 }
809
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100810 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100811 if ((regs->ip > copy_ip) &&
812 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100813 /*
814 * These instructions can be executed directly if it
815 * jumps back to correct address.
816 */
817 set_jmp_op((void *)regs->ip,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100818 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100819 p->ainsn.boostable = 1;
820 } else {
821 p->ainsn.boostable = -1;
822 }
823 }
824
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100825 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100826
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100827no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100828 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100831/*
832 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
833 * remain disabled thoroughout this function.
834 */
835static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800837 struct kprobe *cur = kprobe_running();
838 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
839
840 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500843 resume_execution(cur, regs, kcb);
844 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500845
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800846 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
847 kcb->kprobe_status = KPROBE_HIT_SSDONE;
848 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100851 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800852 if (kcb->kprobe_status == KPROBE_REENTER) {
853 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700854 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700855 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800856 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700857out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 preempt_enable_no_resched();
859
860 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100861 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * will have TF set, in which case, continue the remaining processing
863 * of do_debug, as if this is not a probe hit.
864 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100865 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return 0;
867
868 return 1;
869}
870
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700871int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800873 struct kprobe *cur = kprobe_running();
874 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
875
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100876 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800877 case KPROBE_HIT_SS:
878 case KPROBE_REENTER:
879 /*
880 * We are here because the instruction being single
881 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100882 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800883 * and allow the page fault handler to continue as a
884 * normal page fault.
885 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100886 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100887 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800888 if (kcb->kprobe_status == KPROBE_REENTER)
889 restore_previous_kprobe(kcb);
890 else
891 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800893 break;
894 case KPROBE_HIT_ACTIVE:
895 case KPROBE_HIT_SSDONE:
896 /*
897 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100898 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800899 * these specific fault cases.
900 */
901 kprobes_inc_nmissed_count(cur);
902
903 /*
904 * We come here because instructions in the pre/post
905 * handler caused the page_fault, this could happen
906 * if handler tries to access user space by
907 * copy_from_user(), get_user() etc. Let the
908 * user-specified handler try to fix it first.
909 */
910 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
911 return 1;
912
913 /*
914 * In case the user-specified fault handler returned
915 * zero, try to fix up.
916 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100917 if (fixup_exception(regs))
918 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +0100919
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800920 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100921 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800922 * Let do_page_fault() fix it.
923 */
924 break;
925 default:
926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 return 0;
929}
930
931/*
932 * Wrapper routine for handling exceptions.
933 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700934int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
935 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100937 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800938 int ret = NOTIFY_DONE;
939
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100940 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -0800941 return ret;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (val) {
944 case DIE_INT3:
945 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800946 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +0530949 if (post_kprobe_handler(args->regs)) {
950 /*
951 * Reset the BS bit in dr6 (pointed by args->err) to
952 * denote completion of processing
953 */
954 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800955 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +0530956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +0100959 /*
960 * To be potentially processing a kprobe fault and to
961 * trust the result from kprobe_running(), we have
962 * be non-preemptible.
963 */
964 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800966 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 default:
969 break;
970 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800971 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700974int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct jprobe *jp = container_of(p, struct jprobe, kp);
977 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800978 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800980 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100981 kcb->jprobe_saved_sp = stack_addr(regs);
982 addr = (unsigned long)(kcb->jprobe_saved_sp);
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * As Linus pointed out, gcc assumes that the callee
986 * owns the argument space and could overwrite it, e.g.
987 * tailcall optimization. So, to be absolutely safe
988 * we also save and restore enough stack bytes to cover
989 * the argument area.
990 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800991 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100992 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100993 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +0200994 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100995 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return 1;
997}
998
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700999void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001001 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1002
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001003 asm volatile (
1004#ifdef CONFIG_X86_64
1005 " xchg %%rbx,%%rsp \n"
1006#else
1007 " xchgl %%ebx,%%esp \n"
1008#endif
1009 " int3 \n"
1010 " .globl jprobe_return_end\n"
1011 " jprobe_return_end: \n"
1012 " nop \n"::"b"
1013 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001016int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001018 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001019 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 struct jprobe *jp = container_of(p, struct jprobe, kp);
1021
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001022 if ((addr > (u8 *) jprobe_return) &&
1023 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001024 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001025 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001026 printk(KERN_ERR
1027 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001028 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001029 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001031 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 show_registers(regs);
1033 BUG();
1034 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001035 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001036 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1037 kcb->jprobes_stack,
1038 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001039 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return 1;
1041 }
1042 return 0;
1043}
Rusty Lynchba8af122005-06-27 15:17:10 -07001044
Rusty Lynch67729262005-07-05 18:54:50 -07001045int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001046{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +01001047 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001048}
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -07001049
1050int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1051{
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -07001052 return 0;
1053}