blob: b5b1848c5336e505a5a147f0497865056f34b307 [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>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070051
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010052#include <asm/cacheflush.h>
53#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080055#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020056#include <asm/alternative.h>
K.Prasad62edab92009-06-01 23:47:06 +053057#include <asm/debugreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059void jprobe_return_end(void);
60
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080061DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
62DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010064#ifdef CONFIG_X86_64
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010065#define stack_addr(regs) ((unsigned long *)regs->sp)
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010066#else
67/*
68 * "&regs->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs
69 * don't save the ss and esp registers if the CPU is already in kernel
70 * mode when it traps. So for kprobes, regs->sp and regs->ss are not
71 * the [nonexistent] saved stack pointer and ss register, but rather
72 * the top 8 bytes of the pre-int3 stack. So &regs->sp happens to
73 * point to the top of the pre-int3 stack.
74 */
75#define stack_addr(regs) ((unsigned long *)&regs->sp)
76#endif
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010077
78#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
79 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
80 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
81 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
82 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
83 << (row % 32))
84 /*
85 * Undefined/reserved opcodes, conditional jump, Opcode Extension
86 * Groups, and some special opcodes can not boost.
87 */
88static const u32 twobyte_is_boostable[256 / 32] = {
89 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90 /* ---------------------------------------------- */
91 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
92 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
93 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
94 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
95 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
96 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
97 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
98 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
99 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
100 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
101 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
102 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
103 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
104 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
105 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
106 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
107 /* ----------------------------------------------- */
108 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
109};
110static const u32 onebyte_has_modrm[256 / 32] = {
111 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
112 /* ----------------------------------------------- */
113 W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */
114 W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */
115 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */
116 W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */
117 W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
118 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
119 W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */
120 W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */
121 W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
122 W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */
123 W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */
124 W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */
125 W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */
126 W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
127 W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */
128 W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) /* f0 */
129 /* ----------------------------------------------- */
130 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
131};
132static const u32 twobyte_has_modrm[256 / 32] = {
133 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
134 /* ----------------------------------------------- */
135 W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */
136 W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */
137 W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */
138 W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */
139 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */
140 W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */
141 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */
142 W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */
143 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */
144 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */
145 W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */
146 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */
147 W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */
148 W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */
149 W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */
150 W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* ff */
151 /* ----------------------------------------------- */
152 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
153};
154#undef W
155
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700156struct kretprobe_blackpoint kretprobe_blacklist[] = {
157 {"__switch_to", }, /* This function switches only current task, but
158 doesn't switch kernel stack.*/
159 {NULL, NULL} /* Terminator */
160};
161const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
162
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100163/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100164static void __kprobes set_jmp_op(void *from, void *to)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100165{
166 struct __arch_jmp_op {
167 char op;
168 s32 raddr;
169 } __attribute__((packed)) * jop;
170 jop = (struct __arch_jmp_op *)from;
171 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
172 jop->op = RELATIVEJUMP_INSTRUCTION;
173}
174
175/*
Harvey Harrison99309272008-01-30 13:32:14 +0100176 * Check for the REX prefix which can only exist on X86_64
177 * X86_32 always returns 0
178 */
179static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
180{
181#ifdef CONFIG_X86_64
182 if ((*insn & 0xf0) == 0x40)
183 return 1;
184#endif
185 return 0;
186}
187
188/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100189 * Returns non-zero if opcode is boostable.
190 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100191 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100192static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100193{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100194 kprobe_opcode_t opcode;
195 kprobe_opcode_t *orig_opcodes = opcodes;
196
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530197 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400198 return 0; /* Page fault may occur on this address. */
199
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100200retry:
201 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
202 return 0;
203 opcode = *(opcodes++);
204
205 /* 2nd-byte opcode */
206 if (opcode == 0x0f) {
207 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
208 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100209 return test_bit(*opcodes,
210 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100211 }
212
213 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100214#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100215 case 0x40:
216 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100217#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100218 case 0x60:
219 if (0x63 < opcode && opcode < 0x67)
220 goto retry; /* prefixes */
221 /* can't boost Address-size override and bound */
222 return (opcode != 0x62 && opcode != 0x67);
223 case 0x70:
224 return 0; /* can't boost conditional jump */
225 case 0xc0:
226 /* can't boost software-interruptions */
227 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
228 case 0xd0:
229 /* can boost AA* and XLAT */
230 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
231 case 0xe0:
232 /* can boost in/out and absolute jmps */
233 return ((opcode & 0x04) || opcode == 0xea);
234 case 0xf0:
235 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
236 goto retry; /* lock/rep(ne) prefix */
237 /* clear and set flags are boostable */
238 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
239 default:
240 /* segment override prefixes are boostable */
241 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
242 goto retry; /* prefixes */
243 /* CS override prefix and call are not boostable */
244 return (opcode != 0x2e && opcode != 0x9a);
245 }
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100249 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 */
Andrew Morton86454192007-11-26 20:42:19 +0100251static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 switch (*insn) {
254 case 0xfa: /* cli */
255 case 0xfb: /* sti */
256 case 0xcf: /* iret/iretd */
257 case 0x9d: /* popf/popfd */
258 return 1;
259 }
Harvey Harrison99309272008-01-30 13:32:14 +0100260
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100261 /*
Harvey Harrison99309272008-01-30 13:32:14 +0100262 * on X86_64, 0x40-0x4f are REX prefixes so we need to look
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100263 * at the next byte instead.. but of course not recurse infinitely
264 */
Harvey Harrison99309272008-01-30 13:32:14 +0100265 if (is_REX_prefix(insn))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100266 return is_IF_modifier(++insn);
Harvey Harrison99309272008-01-30 13:32:14 +0100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269}
270
271/*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100272 * Adjust the displacement if the instruction uses the %rip-relative
273 * addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100274 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100276 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100278static void __kprobes fix_riprel(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Harvey Harrison31f80e42008-01-30 13:32:16 +0100280#ifdef CONFIG_X86_64
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100281 u8 *insn = p->ainsn.insn;
282 s64 disp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int need_modrm;
284
285 /* Skip legacy instruction prefixes. */
286 while (1) {
287 switch (*insn) {
288 case 0x66:
289 case 0x67:
290 case 0x2e:
291 case 0x3e:
292 case 0x26:
293 case 0x64:
294 case 0x65:
295 case 0x36:
296 case 0xf0:
297 case 0xf3:
298 case 0xf2:
299 ++insn;
300 continue;
301 }
302 break;
303 }
304
305 /* Skip REX instruction prefix. */
Harvey Harrison99309272008-01-30 13:32:14 +0100306 if (is_REX_prefix(insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ++insn;
308
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100309 if (*insn == 0x0f) {
310 /* Two-byte opcode. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 ++insn;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100312 need_modrm = test_bit(*insn,
313 (unsigned long *)twobyte_has_modrm);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100314 } else
315 /* One-byte opcode. */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100316 need_modrm = test_bit(*insn,
317 (unsigned long *)onebyte_has_modrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (need_modrm) {
320 u8 modrm = *++insn;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100321 if ((modrm & 0xc7) == 0x05) {
322 /* %rip+disp32 addressing mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* Displacement follows ModRM byte. */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100324 ++insn;
325 /*
326 * The copied instruction uses the %rip-relative
327 * addressing mode. Adjust the displacement for the
328 * difference between the original location of this
329 * instruction and the location of the copy that will
330 * actually be run. The tricky bit here is making sure
331 * that the sign extension happens correctly in this
332 * calculation, since we need a signed 32-bit result to
333 * be sign-extended to 64 bits when it's added to the
334 * %rip value and yield the same 64-bit result that the
335 * sign-extension of the original signed 32-bit
336 * displacement would have given.
337 */
338 disp = (u8 *) p->addr + *((s32 *) insn) -
339 (u8 *) p->ainsn.insn;
340 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
341 *(s32 *)insn = (s32) disp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100344#endif
Harvey Harrison31f80e42008-01-30 13:32:16 +0100345}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800347static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100349 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Harvey Harrison31f80e42008-01-30 13:32:16 +0100350
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100351 fix_riprel(p);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100352
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100353 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100354 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100355 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100356 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100357
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700358 p->opcode = *p->addr;
359}
360
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100361int __kprobes arch_prepare_kprobe(struct kprobe *p)
362{
363 /* insn: must be on special executable page on x86. */
364 p->ainsn.insn = get_insn_slot();
365 if (!p->ainsn.insn)
366 return -ENOMEM;
367 arch_copy_kprobe(p);
368 return 0;
369}
370
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700371void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700372{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200373 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700374}
375
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700376void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700377{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200378 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800381void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800383 if (p->ainsn.insn) {
384 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
385 p->ainsn.insn = NULL;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700389static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700390{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800391 kcb->prev_kprobe.kp = kprobe_running();
392 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100393 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
394 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700395}
396
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700397static void __kprobes restore_previous_kprobe(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) = kcb->prev_kprobe.kp;
400 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100401 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
402 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700403}
404
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700405static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800406 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700407{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800408 __get_cpu_var(current_kprobe) = p;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100409 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100410 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700411 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100412 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700413}
414
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100415static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100416{
417 if (test_thread_flag(TIF_DEBUGCTLMSR))
Jan Beulich5b0e5082008-03-10 13:11:17 +0000418 update_debugctlmsr(0);
Roland McGrath1ecc7982008-01-30 13:30:54 +0100419}
420
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100421static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100422{
423 if (test_thread_flag(TIF_DEBUGCTLMSR))
Jan Beulich5b0e5082008-03-10 13:11:17 +0000424 update_debugctlmsr(current->thread.debugctlmsr);
Roland McGrath1ecc7982008-01-30 13:30:54 +0100425}
426
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700427static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Roland McGrath1ecc7982008-01-30 13:30:54 +0100429 clear_btf();
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100430 regs->flags |= X86_EFLAGS_TF;
431 regs->flags &= ~X86_EFLAGS_IF;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100432 /* single step inline if the instruction is an int3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (p->opcode == BREAKPOINT_INSTRUCTION)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100434 regs->ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100436 regs->ip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700439void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700440 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700441{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100442 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700443
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700444 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100445
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700446 /* Replace the return addr with trampoline addr */
447 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700448}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100449
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100450static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
451 struct kprobe_ctlblk *kcb)
452{
Masami Hiramatsu5a4ccaf2009-01-06 21:15:32 +0100453#if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100454 if (p->ainsn.boostable == 1 && !p->post_handler) {
455 /* Boost up -- we can execute copied instructions directly */
456 reset_current_kprobe();
457 regs->ip = (unsigned long)p->ainsn.insn;
458 preempt_enable_no_resched();
459 return;
460 }
461#endif
462 prepare_singlestep(p, regs);
463 kcb->kprobe_status = KPROBE_HIT_SS;
464}
465
Harvey Harrison40102d42008-01-30 13:32:02 +0100466/*
467 * We have reentered the kprobe_handler(), since another probe was hit while
468 * within the handler. We save the original kprobes variables and just single
469 * step on the instruction of the new probe without calling any user handlers.
470 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100471static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
472 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100473{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100474 switch (kcb->kprobe_status) {
475 case KPROBE_HIT_SSDONE:
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100476#ifdef CONFIG_X86_64
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100477 /* TODO: Provide re-entrancy from post_kprobes_handler() and
478 * avoid exception stack corruption while single-stepping on
479 * the instruction of the new probe.
480 */
481 arch_disarm_kprobe(p);
482 regs->ip = (unsigned long)p->addr;
483 reset_current_kprobe();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100484 preempt_enable_no_resched();
485 break;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100486#endif
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100487 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100488 save_previous_kprobe(kcb);
489 set_current_kprobe(p, regs, kcb);
490 kprobes_inc_nmissed_count(p);
491 prepare_singlestep(p, regs);
492 kcb->kprobe_status = KPROBE_REENTER;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100493 break;
494 case KPROBE_HIT_SS:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100495 if (p == kprobe_running()) {
gorcunov@gmail.coma5c15d42008-03-28 17:56:56 +0300496 regs->flags &= ~X86_EFLAGS_TF;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100497 regs->flags |= kcb->kprobe_saved_flags;
498 return 0;
499 } else {
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100500 /* A probe has been hit in the codepath leading up
501 * to, or just after, single-stepping of a probed
502 * instruction. This entire codepath should strictly
503 * reside in .kprobes.text section. Raise a warning
504 * to highlight this peculiar case.
505 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100506 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100507 default:
508 /* impossible cases */
509 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100510 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100511 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100512
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100513 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100514}
Rusty Lynch73649da2005-06-23 00:09:23 -0700515
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100516/*
517 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
518 * remain disabled thorough out this function.
519 */
520static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100522 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100523 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800524 struct kprobe_ctlblk *kcb;
525
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100526 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100527 if (*addr != BREAKPOINT_INSTRUCTION) {
528 /*
529 * The breakpoint instruction was removed right
530 * after we hit it. Another cpu has removed
531 * either a probepoint or a debugger breakpoint
532 * at this address. In either case, no further
533 * handling of this interrupt is appropriate.
534 * Back up over the (now missing) int3 and run
535 * the original instruction.
536 */
537 regs->ip = (unsigned long)addr;
538 return 1;
539 }
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100540
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800541 /*
542 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100543 * duration of kprobe processing. We conditionally
544 * re-enable preemption at the end of this function,
545 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800546 */
547 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100549 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100550 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100551
Harvey Harrisonb9760152008-01-30 13:32:19 +0100552 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100553 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100554 if (reenter_kprobe(p, regs, kcb))
555 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100556 } else {
557 set_current_kprobe(p, regs, kcb);
558 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100561 * If we have no pre-handler or it returned 0, we
562 * continue with normal processing. If we have a
563 * pre-handler and it returned non-zero, it prepped
564 * for calling the break_handler below on re-entry
565 * for jprobe processing, so get out doing nothing
566 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100568 if (!p->pre_handler || !p->pre_handler(p, regs))
569 setup_singlestep(p, regs, kcb);
570 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100571 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100572 } else if (kprobe_running()) {
573 p = __get_cpu_var(current_kprobe);
574 if (p->break_handler && p->break_handler(p, regs)) {
575 setup_singlestep(p, regs, kcb);
576 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100578 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800580 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100585 * When a retprobed function returns, this code saves registers and
586 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700587 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800588static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100589{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100590 asm volatile (
591 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100592 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100593#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100594 /* We don't bother saving the ss register */
595 " pushq %rsp\n"
596 " pushfq\n"
597 /*
598 * Skip cs, ip, orig_ax.
599 * trampoline_handler() will plug in these values
600 */
601 " subq $24, %rsp\n"
602 " pushq %rdi\n"
603 " pushq %rsi\n"
604 " pushq %rdx\n"
605 " pushq %rcx\n"
606 " pushq %rax\n"
607 " pushq %r8\n"
608 " pushq %r9\n"
609 " pushq %r10\n"
610 " pushq %r11\n"
611 " pushq %rbx\n"
612 " pushq %rbp\n"
613 " pushq %r12\n"
614 " pushq %r13\n"
615 " pushq %r14\n"
616 " pushq %r15\n"
617 " movq %rsp, %rdi\n"
618 " call trampoline_handler\n"
619 /* Replace saved sp with true return address. */
620 " movq %rax, 152(%rsp)\n"
621 " popq %r15\n"
622 " popq %r14\n"
623 " popq %r13\n"
624 " popq %r12\n"
625 " popq %rbp\n"
626 " popq %rbx\n"
627 " popq %r11\n"
628 " popq %r10\n"
629 " popq %r9\n"
630 " popq %r8\n"
631 " popq %rax\n"
632 " popq %rcx\n"
633 " popq %rdx\n"
634 " popq %rsi\n"
635 " popq %rdi\n"
636 /* Skip orig_ax, ip, cs */
637 " addq $24, %rsp\n"
638 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100639#else
640 " pushf\n"
641 /*
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400642 * Skip cs, ip, orig_ax and gs.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100643 * trampoline_handler() will plug in these values
644 */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400645 " subl $16, %esp\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100646 " pushl %fs\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100647 " pushl %es\n"
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400648 " pushl %ds\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100649 " pushl %eax\n"
650 " pushl %ebp\n"
651 " pushl %edi\n"
652 " pushl %esi\n"
653 " pushl %edx\n"
654 " pushl %ecx\n"
655 " pushl %ebx\n"
656 " movl %esp, %eax\n"
657 " call trampoline_handler\n"
658 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400659 " movl 56(%esp), %edx\n"
660 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100661 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400662 " movl %eax, 56(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100663 " popl %ebx\n"
664 " popl %ecx\n"
665 " popl %edx\n"
666 " popl %esi\n"
667 " popl %edi\n"
668 " popl %ebp\n"
669 " popl %eax\n"
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400670 /* Skip ds, es, fs, gs, orig_ax and ip */
671 " addl $24, %esp\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100672 " popf\n"
673#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100674 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100675}
Rusty Lynch73649da2005-06-23 00:09:23 -0700676
677/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100678 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700679 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800680static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700681{
bibo,mao62c27be2006-10-02 02:17:33 -0700682 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700683 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700684 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800685 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100686 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700687
bibo,mao99219a32006-10-02 02:17:35 -0700688 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700689 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100690 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100691#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100692 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100693#else
694 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400695 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100696#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100697 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100698 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700699
Rusty Lynchba8af122005-06-27 15:17:10 -0700700 /*
701 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100702 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200703 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700704 * return probe was registered for a target function.
705 *
706 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100707 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700708 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100709 * function, the (chronologically) first instance's ret_addr
710 * will be the real return address, and all the rest will
711 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700712 */
713 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700714 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700715 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700716 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700717
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100718 if (ri->rp && ri->rp->handler) {
719 __get_cpu_var(current_kprobe) = &ri->rp->kp;
720 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
Rusty Lynchba8af122005-06-27 15:17:10 -0700721 ri->rp->handler(ri, regs);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100722 __get_cpu_var(current_kprobe) = NULL;
723 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700724
Rusty Lynchba8af122005-06-27 15:17:10 -0700725 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700726 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700727
728 if (orig_ret_address != trampoline_address)
729 /*
730 * This is the real return address. Any other
731 * instances associated with this task are for
732 * other calls deeper on the call stack
733 */
734 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700735 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700736
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700737 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700738
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700739 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700740
bibo,mao99219a32006-10-02 02:17:35 -0700741 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
742 hlist_del(&ri->hlist);
743 kfree(ri);
744 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100745 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700746}
747
748/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * Called after single-stepping. p->addr is the address of the
750 * instruction whose first byte has been replaced by the "int 3"
751 * instruction. To avoid the SMP problems that can occur when we
752 * temporarily put back the original opcode to single-step, we
753 * single-stepped a copy of the instruction. The address of this
754 * copy is p->ainsn.insn.
755 *
756 * This function prepares to return from the post-single-step
757 * interrupt. We have to fix up the stack as follows:
758 *
759 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100760 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * it relative to the original instruction.
762 *
763 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100764 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * 2) If the single-stepped instruction was a call, the return address
767 * that is atop the stack is the address following the copied instruction.
768 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100769 *
770 * If this is the first time we've single-stepped the instruction at
771 * this probepoint, and the instruction is boostable, boost it: add a
772 * jump instruction after the copied instruction, that jumps to the next
773 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800775static void __kprobes resume_execution(struct kprobe *p,
776 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100778 unsigned long *tos = stack_addr(regs);
779 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
780 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 kprobe_opcode_t *insn = p->ainsn.insn;
782
783 /*skip the REX prefix*/
Harvey Harrison99309272008-01-30 13:32:14 +0100784 if (is_REX_prefix(insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 insn++;
786
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100787 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100789 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100790 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100791 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100793 case 0xc2: /* iret/ret/lret */
794 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700795 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100796 case 0xcb:
797 case 0xcf:
798 case 0xea: /* jmp absolute -- ip is correct */
799 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100800 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100801 goto no_change;
802 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100803 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100805#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100806 case 0x9a: /* call absolute -- same as call absolute, indirect */
807 *tos = orig_ip + (*tos - copy_ip);
808 goto no_change;
809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700811 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100812 /*
813 * call absolute, indirect
814 * Fix return addr; ip is correct.
815 * But this is not boostable
816 */
817 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100818 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100819 } else if (((insn[1] & 0x31) == 0x20) ||
820 ((insn[1] & 0x31) == 0x21)) {
821 /*
822 * jmp near and far, absolute indirect
823 * ip is correct. And this is boostable
824 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100825 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100826 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 default:
829 break;
830 }
831
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100832 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100833 if ((regs->ip > copy_ip) &&
834 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100835 /*
836 * These instructions can be executed directly if it
837 * jumps back to correct address.
838 */
839 set_jmp_op((void *)regs->ip,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100840 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100841 p->ainsn.boostable = 1;
842 } else {
843 p->ainsn.boostable = -1;
844 }
845 }
846
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100847 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100848
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100849no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100850 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100853/*
854 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
855 * remain disabled thoroughout this function.
856 */
857static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800859 struct kprobe *cur = kprobe_running();
860 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
861
862 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
864
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500865 resume_execution(cur, regs, kcb);
866 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500867
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800868 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
869 kcb->kprobe_status = KPROBE_HIT_SSDONE;
870 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100873 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800874 if (kcb->kprobe_status == KPROBE_REENTER) {
875 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700876 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700877 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800878 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 preempt_enable_no_resched();
881
882 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100883 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * will have TF set, in which case, continue the remaining processing
885 * of do_debug, as if this is not a probe hit.
886 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100887 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889
890 return 1;
891}
892
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700893int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800895 struct kprobe *cur = kprobe_running();
896 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
897
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100898 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800899 case KPROBE_HIT_SS:
900 case KPROBE_REENTER:
901 /*
902 * We are here because the instruction being single
903 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100904 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800905 * and allow the page fault handler to continue as a
906 * normal page fault.
907 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100908 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100909 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800910 if (kcb->kprobe_status == KPROBE_REENTER)
911 restore_previous_kprobe(kcb);
912 else
913 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800915 break;
916 case KPROBE_HIT_ACTIVE:
917 case KPROBE_HIT_SSDONE:
918 /*
919 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100920 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800921 * these specific fault cases.
922 */
923 kprobes_inc_nmissed_count(cur);
924
925 /*
926 * We come here because instructions in the pre/post
927 * handler caused the page_fault, this could happen
928 * if handler tries to access user space by
929 * copy_from_user(), get_user() etc. Let the
930 * user-specified handler try to fix it first.
931 */
932 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
933 return 1;
934
935 /*
936 * In case the user-specified fault handler returned
937 * zero, try to fix up.
938 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100939 if (fixup_exception(regs))
940 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +0100941
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800942 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100943 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800944 * Let do_page_fault() fix it.
945 */
946 break;
947 default:
948 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 return 0;
951}
952
953/*
954 * Wrapper routine for handling exceptions.
955 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700956int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
957 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100959 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800960 int ret = NOTIFY_DONE;
961
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100962 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -0800963 return ret;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 switch (val) {
966 case DIE_INT3:
967 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800968 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +0530971 if (post_kprobe_handler(args->regs)) {
972 /*
973 * Reset the BS bit in dr6 (pointed by args->err) to
974 * denote completion of processing
975 */
976 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800977 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +0530978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +0100981 /*
982 * To be potentially processing a kprobe fault and to
983 * trust the result from kprobe_running(), we have
984 * be non-preemptible.
985 */
986 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800988 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
990 default:
991 break;
992 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800993 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700996int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct jprobe *jp = container_of(p, struct jprobe, kp);
999 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001000 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001002 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001003 kcb->jprobe_saved_sp = stack_addr(regs);
1004 addr = (unsigned long)(kcb->jprobe_saved_sp);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
1007 * As Linus pointed out, gcc assumes that the callee
1008 * owns the argument space and could overwrite it, e.g.
1009 * tailcall optimization. So, to be absolutely safe
1010 * we also save and restore enough stack bytes to cover
1011 * the argument area.
1012 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001013 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001014 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001015 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001016 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001017 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 1;
1019}
1020
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001021void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001023 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1024
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001025 asm volatile (
1026#ifdef CONFIG_X86_64
1027 " xchg %%rbx,%%rsp \n"
1028#else
1029 " xchgl %%ebx,%%esp \n"
1030#endif
1031 " int3 \n"
1032 " .globl jprobe_return_end\n"
1033 " jprobe_return_end: \n"
1034 " nop \n"::"b"
1035 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001038int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001040 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001041 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct jprobe *jp = container_of(p, struct jprobe, kp);
1043
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001044 if ((addr > (u8 *) jprobe_return) &&
1045 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001046 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001047 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001048 printk(KERN_ERR
1049 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001050 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001051 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001053 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 show_registers(regs);
1055 BUG();
1056 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001057 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001058 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1059 kcb->jprobes_stack,
1060 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001061 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 1;
1063 }
1064 return 0;
1065}
Rusty Lynchba8af122005-06-27 15:17:10 -07001066
Rusty Lynch67729262005-07-05 18:54:50 -07001067int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001068{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +01001069 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001070}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001071
1072int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1073{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001074 return 0;
1075}