blob: 794bc95134cd5f45f85345f6a006a9d45442fcb3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2002, 2004
19 *
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010025 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010029 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
30 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
31 * <prasanna@in.ibm.com> added function-return probes.
32 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
33 * Added function return probes functionality
34 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
35 * kprobe-booster and kretprobe-booster for i386.
Masami Hiramatsuda07ab02008-01-30 13:31:21 +010036 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
37 * and kretprobe-booster for x86-64
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010038 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
39 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40 * unified x86 kprobes code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kprobes.h>
44#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/string.h>
46#include <linux/slab.h>
Quentin Barnesb506a9d2008-01-30 13:32:32 +010047#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/preempt.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080049#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070050#include <linux/kdebug.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040051#include <linux/kallsyms.h>
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -050052#include <linux/ftrace.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070053
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010054#include <asm/cacheflush.h>
55#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080057#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020058#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040059#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053060#include <asm/debugreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062void jprobe_return_end(void);
63
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080064DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
65DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
H. Peter Anvin98272ed2009-10-12 14:14:10 -070067#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010068
69#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
70 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
71 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
72 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
73 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
74 << (row % 32))
75 /*
76 * Undefined/reserved opcodes, conditional jump, Opcode Extension
77 * Groups, and some special opcodes can not boost.
Josh Stone2f3f49d2011-10-24 10:15:51 -070078 * This is non-const to keep gcc from statically optimizing it out, as
79 * variable_test_bit makes gcc think only *(unsigned long*) is used.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010080 */
Josh Stone2f3f49d2011-10-24 10:15:51 -070081static u32 twobyte_is_boostable[256 / 32] = {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010082 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
83 /* ---------------------------------------------- */
84 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
85 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
86 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
87 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
88 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
89 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
90 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
91 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
92 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
93 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
94 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
95 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
96 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
97 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
98 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
99 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
100 /* ----------------------------------------------- */
101 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
102};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100103#undef W
104
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700105struct kretprobe_blackpoint kretprobe_blacklist[] = {
106 {"__switch_to", }, /* This function switches only current task, but
107 doesn't switch kernel stack.*/
108 {NULL, NULL} /* Terminator */
109};
110const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
111
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500112static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100113{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500114 struct __arch_relative_insn {
115 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100116 s32 raddr;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500117 } __attribute__((packed)) *insn;
118
119 insn = (struct __arch_relative_insn *)from;
120 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
121 insn->op = op;
122}
123
124/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
125static void __kprobes synthesize_reljump(void *from, void *to)
126{
127 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100128}
129
130/*
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900131 * Skip the prefixes of the instruction.
Harvey Harrison99309272008-01-30 13:32:14 +0100132 */
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900133static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
Harvey Harrison99309272008-01-30 13:32:14 +0100134{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900135 insn_attr_t attr;
136
137 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
138 while (inat_is_legacy_prefix(attr)) {
139 insn++;
140 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
141 }
Harvey Harrison99309272008-01-30 13:32:14 +0100142#ifdef CONFIG_X86_64
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900143 if (inat_is_rex_prefix(attr))
144 insn++;
Harvey Harrison99309272008-01-30 13:32:14 +0100145#endif
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900146 return insn;
Harvey Harrison99309272008-01-30 13:32:14 +0100147}
148
149/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100150 * Returns non-zero if opcode is boostable.
151 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100152 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100153static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100154{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100155 kprobe_opcode_t opcode;
156 kprobe_opcode_t *orig_opcodes = opcodes;
157
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530158 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400159 return 0; /* Page fault may occur on this address. */
160
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100161retry:
162 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
163 return 0;
164 opcode = *(opcodes++);
165
166 /* 2nd-byte opcode */
167 if (opcode == 0x0f) {
168 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
169 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100170 return test_bit(*opcodes,
171 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100172 }
173
174 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100175#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100176 case 0x40:
177 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100178#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100179 case 0x60:
180 if (0x63 < opcode && opcode < 0x67)
181 goto retry; /* prefixes */
182 /* can't boost Address-size override and bound */
183 return (opcode != 0x62 && opcode != 0x67);
184 case 0x70:
185 return 0; /* can't boost conditional jump */
186 case 0xc0:
187 /* can't boost software-interruptions */
188 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
189 case 0xd0:
190 /* can boost AA* and XLAT */
191 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
192 case 0xe0:
193 /* can boost in/out and absolute jmps */
194 return ((opcode & 0x04) || opcode == 0xea);
195 case 0xf0:
196 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
197 goto retry; /* lock/rep(ne) prefix */
198 /* clear and set flags are boostable */
199 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
200 default:
201 /* segment override prefixes are boostable */
202 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
203 goto retry; /* prefixes */
204 /* CS override prefix and call are not boostable */
205 return (opcode != 0x2e && opcode != 0x9a);
206 }
207}
208
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400209/* Recover the probed instruction at addr for further analysis. */
210static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
211{
212 struct kprobe *kp;
213 kp = get_kprobe((void *)addr);
214 if (!kp)
215 return -EINVAL;
216
217 /*
218 * Basically, kp->ainsn.insn has an original instruction.
219 * However, RIP-relative instruction can not do single-stepping
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500220 * at different place, __copy_instruction() tweaks the displacement of
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400221 * that instruction. In that case, we can't recover the instruction
222 * from the kp->ainsn.insn.
223 *
224 * On the other hand, kp->opcode has a copy of the first byte of
225 * the probed instruction, which is overwritten by int3. And
226 * the instruction at kp->addr is not modified by kprobes except
227 * for the first byte, we can recover the original instruction
228 * from it and kp->opcode.
229 */
230 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
231 buf[0] = kp->opcode;
232 return 0;
233}
234
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400235/* 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
Namhyung Kim6abded72010-09-15 10:04:29 +0900243 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400244 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{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900280 /* Skip prefixes */
281 insn = skip_prefixes(insn);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 switch (*insn) {
284 case 0xfa: /* cli */
285 case 0xfb: /* sti */
286 case 0xcf: /* iret/iretd */
287 case 0x9d: /* popf/popfd */
288 return 1;
289 }
Harvey Harrison99309272008-01-30 13:32:14 +0100290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
293
294/*
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500295 * Copy an instruction and adjust the displacement if the instruction
296 * uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100297 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100299 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500301static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400303 struct insn insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500304 int ret;
305 kprobe_opcode_t buf[MAX_INSN_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500307 kernel_insn_init(&insn, src);
308 if (recover) {
309 insn_get_opcode(&insn);
310 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
311 ret = recover_probed_instruction(buf,
312 (unsigned long)src);
313 if (ret)
314 return 0;
315 kernel_insn_init(&insn, buf);
316 }
317 }
318 insn_get_length(&insn);
319 memcpy(dest, insn.kaddr, insn.length);
320
321#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400322 if (insn_rip_relative(&insn)) {
323 s64 newdisp;
324 u8 *disp;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500325 kernel_insn_init(&insn, dest);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400326 insn_get_displacement(&insn);
327 /*
328 * The copied instruction uses the %rip-relative addressing
329 * mode. Adjust the displacement for the difference between
330 * the original location of this instruction and the location
331 * of the copy that will actually be run. The tricky bit here
332 * is making sure that the sign extension happens correctly in
333 * this calculation, since we need a signed 32-bit result to
334 * be sign-extended to 64 bits when it's added to the %rip
335 * value and yield the same 64-bit result that the sign-
336 * extension of the original signed 32-bit displacement would
337 * have given.
338 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500339 newdisp = (u8 *) src + (s64) insn.displacement.value -
340 (u8 *) dest;
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400341 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500342 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400343 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100345#endif
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500346 return insn.length;
Harvey Harrison31f80e42008-01-30 13:32:16 +0100347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800349static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500351 /*
352 * Copy an instruction without recovering int3, because it will be
353 * put by another subsystem.
354 */
355 __copy_instruction(p->ainsn.insn, p->addr, 0);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100356
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100357 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100358 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100359 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100360 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100361
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700362 p->opcode = *p->addr;
363}
364
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100365int __kprobes arch_prepare_kprobe(struct kprobe *p)
366{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500367 if (alternatives_text_reserved(p->addr, p->addr))
368 return -EINVAL;
369
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400370 if (!can_probe((unsigned long)p->addr))
371 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100372 /* insn: must be on special executable page on x86. */
373 p->ainsn.insn = get_insn_slot();
374 if (!p->ainsn.insn)
375 return -ENOMEM;
376 arch_copy_kprobe(p);
377 return 0;
378}
379
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700380void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700381{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200382 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700383}
384
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700385void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700386{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200387 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800390void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800392 if (p->ainsn.insn) {
393 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
394 p->ainsn.insn = NULL;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700398static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700399{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800400 kcb->prev_kprobe.kp = kprobe_running();
401 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100402 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
403 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700404}
405
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700406static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700407{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600408 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800409 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100410 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
411 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700412}
413
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700414static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800415 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700416{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600417 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100418 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100419 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700420 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100421 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700422}
423
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100424static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100425{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100426 if (test_thread_flag(TIF_BLOCKSTEP)) {
427 unsigned long debugctl = get_debugctlmsr();
428
429 debugctl &= ~DEBUGCTLMSR_BTF;
430 update_debugctlmsr(debugctl);
431 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100432}
433
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100434static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100435{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100436 if (test_thread_flag(TIF_BLOCKSTEP)) {
437 unsigned long debugctl = get_debugctlmsr();
438
439 debugctl |= DEBUGCTLMSR_BTF;
440 update_debugctlmsr(debugctl);
441 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100442}
443
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700444void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700445 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700446{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100447 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700448
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700449 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100450
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700451 /* Replace the return addr with trampoline addr */
452 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700453}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100454
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500455#ifdef CONFIG_OPTPROBES
456static int __kprobes setup_detour_execution(struct kprobe *p,
457 struct pt_regs *regs,
458 int reenter);
459#else
460#define setup_detour_execution(p, regs, reenter) (0)
461#endif
462
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100463static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500464 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100465{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500466 if (setup_detour_execution(p, regs, reenter))
467 return;
468
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500469#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100470 if (p->ainsn.boostable == 1 && !p->post_handler) {
471 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500472 if (!reenter)
473 reset_current_kprobe();
474 /*
475 * Reentering boosted probe doesn't reset current_kprobe,
476 * nor set current_kprobe, because it doesn't use single
477 * stepping.
478 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100479 regs->ip = (unsigned long)p->ainsn.insn;
480 preempt_enable_no_resched();
481 return;
482 }
483#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500484 if (reenter) {
485 save_previous_kprobe(kcb);
486 set_current_kprobe(p, regs, kcb);
487 kcb->kprobe_status = KPROBE_REENTER;
488 } else
489 kcb->kprobe_status = KPROBE_HIT_SS;
490 /* Prepare real single stepping */
491 clear_btf();
492 regs->flags |= X86_EFLAGS_TF;
493 regs->flags &= ~X86_EFLAGS_IF;
494 /* single step inline if the instruction is an int3 */
495 if (p->opcode == BREAKPOINT_INSTRUCTION)
496 regs->ip = (unsigned long)p->addr;
497 else
498 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100499}
500
Harvey Harrison40102d42008-01-30 13:32:02 +0100501/*
502 * We have reentered the kprobe_handler(), since another probe was hit while
503 * within the handler. We save the original kprobes variables and just single
504 * step on the instruction of the new probe without calling any user handlers.
505 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100506static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
507 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100508{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100509 switch (kcb->kprobe_status) {
510 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100511 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100512 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500513 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100514 break;
515 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400516 /* A probe has been hit in the codepath leading up to, or just
517 * after, single-stepping of a probed instruction. This entire
518 * codepath should strictly reside in .kprobes.text section.
519 * Raise a BUG or we'll continue in an endless reentering loop
520 * and eventually a stack overflow.
521 */
522 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
523 p->addr);
524 dump_kprobe(p);
525 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100526 default:
527 /* impossible cases */
528 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100529 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100530 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100531
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100532 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100533}
Rusty Lynch73649da2005-06-23 00:09:23 -0700534
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100535/*
536 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200537 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100538 */
539static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100541 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100542 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800543 struct kprobe_ctlblk *kcb;
544
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100545 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800546 /*
547 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100548 * duration of kprobe processing. We conditionally
549 * re-enable preemption at the end of this function,
550 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800551 */
552 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100554 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100555 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100556
Harvey Harrisonb9760152008-01-30 13:32:19 +0100557 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100558 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100559 if (reenter_kprobe(p, regs, kcb))
560 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100561 } else {
562 set_current_kprobe(p, regs, kcb);
563 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100566 * If we have no pre-handler or it returned 0, we
567 * continue with normal processing. If we have a
568 * pre-handler and it returned non-zero, it prepped
569 * for calling the break_handler below on re-entry
570 * for jprobe processing, so get out doing nothing
571 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100573 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500574 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100575 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100576 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400577 } else if (*addr != BREAKPOINT_INSTRUCTION) {
578 /*
579 * The breakpoint instruction was removed right
580 * after we hit it. Another cpu has removed
581 * either a probepoint or a debugger breakpoint
582 * at this address. In either case, no further
583 * handling of this interrupt is appropriate.
584 * Back up over the (now missing) int3 and run
585 * the original instruction.
586 */
587 regs->ip = (unsigned long)addr;
588 preempt_enable_no_resched();
589 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100590 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600591 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100592 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500593 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100594 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100596 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800598 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500602#ifdef CONFIG_X86_64
603#define SAVE_REGS_STRING \
604 /* Skip cs, ip, orig_ax. */ \
605 " subq $24, %rsp\n" \
606 " pushq %rdi\n" \
607 " pushq %rsi\n" \
608 " pushq %rdx\n" \
609 " pushq %rcx\n" \
610 " pushq %rax\n" \
611 " pushq %r8\n" \
612 " pushq %r9\n" \
613 " pushq %r10\n" \
614 " pushq %r11\n" \
615 " pushq %rbx\n" \
616 " pushq %rbp\n" \
617 " pushq %r12\n" \
618 " pushq %r13\n" \
619 " pushq %r14\n" \
620 " pushq %r15\n"
621#define RESTORE_REGS_STRING \
622 " popq %r15\n" \
623 " popq %r14\n" \
624 " popq %r13\n" \
625 " popq %r12\n" \
626 " popq %rbp\n" \
627 " popq %rbx\n" \
628 " popq %r11\n" \
629 " popq %r10\n" \
630 " popq %r9\n" \
631 " popq %r8\n" \
632 " popq %rax\n" \
633 " popq %rcx\n" \
634 " popq %rdx\n" \
635 " popq %rsi\n" \
636 " popq %rdi\n" \
637 /* Skip orig_ax, ip, cs */ \
638 " addq $24, %rsp\n"
639#else
640#define SAVE_REGS_STRING \
641 /* Skip cs, ip, orig_ax and gs. */ \
642 " subl $16, %esp\n" \
643 " pushl %fs\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500644 " pushl %es\n" \
Roland McGratha1974792010-07-16 18:17:12 -0700645 " pushl %ds\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500646 " pushl %eax\n" \
647 " pushl %ebp\n" \
648 " pushl %edi\n" \
649 " pushl %esi\n" \
650 " pushl %edx\n" \
651 " pushl %ecx\n" \
652 " pushl %ebx\n"
653#define RESTORE_REGS_STRING \
654 " popl %ebx\n" \
655 " popl %ecx\n" \
656 " popl %edx\n" \
657 " popl %esi\n" \
658 " popl %edi\n" \
659 " popl %ebp\n" \
660 " popl %eax\n" \
661 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
662 " addl $24, %esp\n"
663#endif
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100666 * When a retprobed function returns, this code saves registers and
667 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700668 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800669static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100670{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100671 asm volatile (
672 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100673 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100674#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100675 /* We don't bother saving the ss register */
676 " pushq %rsp\n"
677 " pushfq\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500678 SAVE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100679 " movq %rsp, %rdi\n"
680 " call trampoline_handler\n"
681 /* Replace saved sp with true return address. */
682 " movq %rax, 152(%rsp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500683 RESTORE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100684 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100685#else
686 " pushf\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500687 SAVE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100688 " movl %esp, %eax\n"
689 " call trampoline_handler\n"
690 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400691 " movl 56(%esp), %edx\n"
692 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100693 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400694 " movl %eax, 56(%esp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500695 RESTORE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100696 " popf\n"
697#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100698 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100699}
Rusty Lynch73649da2005-06-23 00:09:23 -0700700
701/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100702 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700703 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800704static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700705{
bibo,mao62c27be2006-10-02 02:17:33 -0700706 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700707 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700708 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800709 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100710 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900711 kprobe_opcode_t *correct_ret_addr = NULL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700712
bibo,mao99219a32006-10-02 02:17:35 -0700713 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700714 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100715 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100716#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100717 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100718#else
719 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400720 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100721#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100722 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100723 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700724
Rusty Lynchba8af122005-06-27 15:17:10 -0700725 /*
726 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100727 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200728 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700729 * return probe was registered for a target function.
730 *
731 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100732 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700733 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100734 * function, the (chronologically) first instance's ret_addr
735 * will be the real return address, and all the rest will
736 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700737 */
738 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700739 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700740 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700741 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700742
Rusty Lynchba8af122005-06-27 15:17:10 -0700743 orig_ret_address = (unsigned long)ri->ret_addr;
Rusty Lynchba8af122005-06-27 15:17:10 -0700744
745 if (orig_ret_address != trampoline_address)
746 /*
747 * This is the real return address. Any other
748 * instances associated with this task are for
749 * other calls deeper on the call stack
750 */
751 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700752 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700753
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700754 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700755
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900756 correct_ret_addr = ri->ret_addr;
757 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
758 if (ri->task != current)
759 /* another task is sharing our hash bucket */
760 continue;
761
762 orig_ret_address = (unsigned long)ri->ret_addr;
763 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600764 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900765 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
766 ri->ret_addr = correct_ret_addr;
767 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600768 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900769 }
770
771 recycle_rp_inst(ri, &empty_rp);
772
773 if (orig_ret_address != trampoline_address)
774 /*
775 * This is the real return address. Any other
776 * instances associated with this task are for
777 * other calls deeper on the call stack
778 */
779 break;
780 }
781
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700782 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700783
bibo,mao99219a32006-10-02 02:17:35 -0700784 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
785 hlist_del(&ri->hlist);
786 kfree(ri);
787 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100788 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700789}
790
791/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * Called after single-stepping. p->addr is the address of the
793 * instruction whose first byte has been replaced by the "int 3"
794 * instruction. To avoid the SMP problems that can occur when we
795 * temporarily put back the original opcode to single-step, we
796 * single-stepped a copy of the instruction. The address of this
797 * copy is p->ainsn.insn.
798 *
799 * This function prepares to return from the post-single-step
800 * interrupt. We have to fix up the stack as follows:
801 *
802 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100803 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * it relative to the original instruction.
805 *
806 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100807 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
809 * 2) If the single-stepped instruction was a call, the return address
810 * that is atop the stack is the address following the copied instruction.
811 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100812 *
813 * If this is the first time we've single-stepped the instruction at
814 * this probepoint, and the instruction is boostable, boost it: add a
815 * jump instruction after the copied instruction, that jumps to the next
816 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800818static void __kprobes resume_execution(struct kprobe *p,
819 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100821 unsigned long *tos = stack_addr(regs);
822 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
823 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kprobe_opcode_t *insn = p->ainsn.insn;
825
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900826 /* Skip prefixes */
827 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100829 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100831 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100832 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100833 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100835 case 0xc2: /* iret/ret/lret */
836 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700837 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100838 case 0xcb:
839 case 0xcf:
840 case 0xea: /* jmp absolute -- ip is correct */
841 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100842 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100843 goto no_change;
844 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100845 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100847#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100848 case 0x9a: /* call absolute -- same as call absolute, indirect */
849 *tos = orig_ip + (*tos - copy_ip);
850 goto no_change;
851#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700853 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100854 /*
855 * call absolute, indirect
856 * Fix return addr; ip is correct.
857 * But this is not boostable
858 */
859 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100860 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100861 } else if (((insn[1] & 0x31) == 0x20) ||
862 ((insn[1] & 0x31) == 0x21)) {
863 /*
864 * jmp near and far, absolute indirect
865 * ip is correct. And this is boostable
866 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100867 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100868 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 default:
871 break;
872 }
873
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100874 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100875 if ((regs->ip > copy_ip) &&
876 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100877 /*
878 * These instructions can be executed directly if it
879 * jumps back to correct address.
880 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500881 synthesize_reljump((void *)regs->ip,
882 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100883 p->ainsn.boostable = 1;
884 } else {
885 p->ainsn.boostable = -1;
886 }
887 }
888
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100889 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100890
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100891no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100892 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100895/*
896 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200897 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100898 */
899static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800901 struct kprobe *cur = kprobe_running();
902 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
903
904 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
906
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500907 resume_execution(cur, regs, kcb);
908 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500909
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800910 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
911 kcb->kprobe_status = KPROBE_HIT_SSDONE;
912 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100915 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800916 if (kcb->kprobe_status == KPROBE_REENTER) {
917 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700918 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700919 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800920 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700921out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 preempt_enable_no_resched();
923
924 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100925 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * will have TF set, in which case, continue the remaining processing
927 * of do_debug, as if this is not a probe hit.
928 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100929 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931
932 return 1;
933}
934
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700935int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800937 struct kprobe *cur = kprobe_running();
938 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
939
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100940 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800941 case KPROBE_HIT_SS:
942 case KPROBE_REENTER:
943 /*
944 * We are here because the instruction being single
945 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100946 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800947 * and allow the page fault handler to continue as a
948 * normal page fault.
949 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100950 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100951 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800952 if (kcb->kprobe_status == KPROBE_REENTER)
953 restore_previous_kprobe(kcb);
954 else
955 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800957 break;
958 case KPROBE_HIT_ACTIVE:
959 case KPROBE_HIT_SSDONE:
960 /*
961 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100962 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800963 * these specific fault cases.
964 */
965 kprobes_inc_nmissed_count(cur);
966
967 /*
968 * We come here because instructions in the pre/post
969 * handler caused the page_fault, this could happen
970 * if handler tries to access user space by
971 * copy_from_user(), get_user() etc. Let the
972 * user-specified handler try to fix it first.
973 */
974 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
975 return 1;
976
977 /*
978 * In case the user-specified fault handler returned
979 * zero, try to fix up.
980 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100981 if (fixup_exception(regs))
982 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +0100983
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800984 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100985 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800986 * Let do_page_fault() fix it.
987 */
988 break;
989 default:
990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992 return 0;
993}
994
995/*
996 * Wrapper routine for handling exceptions.
997 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700998int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
999 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jan Engelhardtade1af72008-01-30 13:33:23 +01001001 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001002 int ret = NOTIFY_DONE;
1003
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001004 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001005 return ret;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 switch (val) {
1008 case DIE_INT3:
1009 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001010 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
1012 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +05301013 if (post_kprobe_handler(args->regs)) {
1014 /*
1015 * Reset the BS bit in dr6 (pointed by args->err) to
1016 * denote completion of processing
1017 */
1018 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001019 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +05301020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
1022 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001023 /*
1024 * To be potentially processing a kprobe fault and to
1025 * trust the result from kprobe_running(), we have
1026 * be non-preemptible.
1027 */
1028 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001030 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 default:
1033 break;
1034 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001035 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001038int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct jprobe *jp = container_of(p, struct jprobe, kp);
1041 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001042 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001044 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001045 kcb->jprobe_saved_sp = stack_addr(regs);
1046 addr = (unsigned long)(kcb->jprobe_saved_sp);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * As Linus pointed out, gcc assumes that the callee
1050 * owns the argument space and could overwrite it, e.g.
1051 * tailcall optimization. So, to be absolutely safe
1052 * we also save and restore enough stack bytes to cover
1053 * the argument area.
1054 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001055 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001056 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001057 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001058 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001059 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 1;
1061}
1062
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001063void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001065 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1066
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001067 asm volatile (
1068#ifdef CONFIG_X86_64
1069 " xchg %%rbx,%%rsp \n"
1070#else
1071 " xchgl %%ebx,%%esp \n"
1072#endif
1073 " int3 \n"
1074 " .globl jprobe_return_end\n"
1075 " jprobe_return_end: \n"
1076 " nop \n"::"b"
1077 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001080int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001082 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001083 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct jprobe *jp = container_of(p, struct jprobe, kp);
1085
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001086 if ((addr > (u8 *) jprobe_return) &&
1087 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001088 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001089 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001090 printk(KERN_ERR
1091 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001092 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001093 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001095 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 show_registers(regs);
1097 BUG();
1098 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001099 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001100 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1101 kcb->jprobes_stack,
1102 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001103 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 1;
1105 }
1106 return 0;
1107}
Rusty Lynchba8af122005-06-27 15:17:10 -07001108
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001109
1110#ifdef CONFIG_OPTPROBES
1111
1112/* Insert a call instruction at address 'from', which calls address 'to'.*/
1113static void __kprobes synthesize_relcall(void *from, void *to)
1114{
1115 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1116}
1117
1118/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1119static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1120 unsigned long val)
1121{
1122#ifdef CONFIG_X86_64
1123 *addr++ = 0x48;
1124 *addr++ = 0xbf;
1125#else
1126 *addr++ = 0xb8;
1127#endif
1128 *(unsigned long *)addr = val;
1129}
1130
Namhyung Kim6376b222010-09-15 10:04:28 +09001131static void __used __kprobes kprobes_optinsn_template_holder(void)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001132{
1133 asm volatile (
1134 ".global optprobe_template_entry\n"
1135 "optprobe_template_entry: \n"
1136#ifdef CONFIG_X86_64
1137 /* We don't bother saving the ss register */
1138 " pushq %rsp\n"
1139 " pushfq\n"
1140 SAVE_REGS_STRING
1141 " movq %rsp, %rsi\n"
1142 ".global optprobe_template_val\n"
1143 "optprobe_template_val: \n"
1144 ASM_NOP5
1145 ASM_NOP5
1146 ".global optprobe_template_call\n"
1147 "optprobe_template_call: \n"
1148 ASM_NOP5
1149 /* Move flags to rsp */
1150 " movq 144(%rsp), %rdx\n"
1151 " movq %rdx, 152(%rsp)\n"
1152 RESTORE_REGS_STRING
1153 /* Skip flags entry */
1154 " addq $8, %rsp\n"
1155 " popfq\n"
1156#else /* CONFIG_X86_32 */
1157 " pushf\n"
1158 SAVE_REGS_STRING
1159 " movl %esp, %edx\n"
1160 ".global optprobe_template_val\n"
1161 "optprobe_template_val: \n"
1162 ASM_NOP5
1163 ".global optprobe_template_call\n"
1164 "optprobe_template_call: \n"
1165 ASM_NOP5
1166 RESTORE_REGS_STRING
1167 " addl $4, %esp\n" /* skip cs */
1168 " popf\n"
1169#endif
1170 ".global optprobe_template_end\n"
1171 "optprobe_template_end: \n");
1172}
1173
1174#define TMPL_MOVE_IDX \
1175 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1176#define TMPL_CALL_IDX \
1177 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1178#define TMPL_END_IDX \
1179 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1180
1181#define INT3_SIZE sizeof(kprobe_opcode_t)
1182
1183/* Optimized kprobe call back function: called from optinsn */
1184static void __kprobes optimized_callback(struct optimized_kprobe *op,
1185 struct pt_regs *regs)
1186{
1187 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001188 unsigned long flags;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001189
Masami Hiramatsu6274de42010-12-03 18:54:09 +09001190 /* This is possible if op is under delayed unoptimizing */
1191 if (kprobe_disabled(&op->kp))
1192 return;
1193
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001194 local_irq_save(flags);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001195 if (kprobe_running()) {
1196 kprobes_inc_nmissed_count(&op->kp);
1197 } else {
1198 /* Save skipped registers */
1199#ifdef CONFIG_X86_64
1200 regs->cs = __KERNEL_CS;
1201#else
1202 regs->cs = __KERNEL_CS | get_kernel_rpl();
1203 regs->gs = 0;
1204#endif
1205 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1206 regs->orig_ax = ~0UL;
1207
Christoph Lameterb76834b2010-12-06 11:16:25 -06001208 __this_cpu_write(current_kprobe, &op->kp);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001209 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1210 opt_pre_handler(&op->kp, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -06001211 __this_cpu_write(current_kprobe, NULL);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001212 }
Jiri Olsa9bbeacf2011-05-11 13:06:13 +02001213 local_irq_restore(flags);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001214}
1215
1216static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1217{
1218 int len = 0, ret;
1219
1220 while (len < RELATIVEJUMP_SIZE) {
1221 ret = __copy_instruction(dest + len, src + len, 1);
1222 if (!ret || !can_boost(dest + len))
1223 return -EINVAL;
1224 len += ret;
1225 }
1226 /* Check whether the address range is reserved */
1227 if (ftrace_text_reserved(src, src + len - 1) ||
Jason Baron4c3ef6d2010-09-17 11:09:08 -04001228 alternatives_text_reserved(src, src + len - 1) ||
1229 jump_label_text_reserved(src, src + len - 1))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001230 return -EBUSY;
1231
1232 return len;
1233}
1234
1235/* Check whether insn is indirect jump */
1236static int __kprobes insn_is_indirect_jump(struct insn *insn)
1237{
1238 return ((insn->opcode.bytes[0] == 0xff &&
1239 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1240 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1241}
1242
1243/* Check whether insn jumps into specified address range */
1244static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1245{
1246 unsigned long target = 0;
1247
1248 switch (insn->opcode.bytes[0]) {
1249 case 0xe0: /* loopne */
1250 case 0xe1: /* loope */
1251 case 0xe2: /* loop */
1252 case 0xe3: /* jcxz */
1253 case 0xe9: /* near relative jump */
1254 case 0xeb: /* short relative jump */
1255 break;
1256 case 0x0f:
1257 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1258 break;
1259 return 0;
1260 default:
1261 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1262 break;
1263 return 0;
1264 }
1265 target = (unsigned long)insn->next_byte + insn->immediate.value;
1266
1267 return (start <= target && target <= start + len);
1268}
1269
1270/* Decode whole function to ensure any instructions don't jump into target */
1271static int __kprobes can_optimize(unsigned long paddr)
1272{
1273 int ret;
1274 unsigned long addr, size = 0, offset = 0;
1275 struct insn insn;
1276 kprobe_opcode_t buf[MAX_INSN_SIZE];
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001277
1278 /* Lookup symbol including addr */
Namhyung Kim6abded72010-09-15 10:04:29 +09001279 if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001280 return 0;
1281
Jiri Olsa2a8247a2011-02-21 15:25:13 +01001282 /*
1283 * Do not optimize in the entry code due to the unstable
1284 * stack handling.
1285 */
1286 if ((paddr >= (unsigned long )__entry_text_start) &&
1287 (paddr < (unsigned long )__entry_text_end))
1288 return 0;
1289
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001290 /* Check there is enough space for a relative jump. */
1291 if (size - offset < RELATIVEJUMP_SIZE)
1292 return 0;
1293
1294 /* Decode instructions */
1295 addr = paddr - offset;
1296 while (addr < paddr - offset + size) { /* Decode until function end */
1297 if (search_exception_tables(addr))
1298 /*
1299 * Since some fixup code will jumps into this function,
1300 * we can't optimize kprobe in this function.
1301 */
1302 return 0;
1303 kernel_insn_init(&insn, (void *)addr);
1304 insn_get_opcode(&insn);
1305 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
1306 ret = recover_probed_instruction(buf, addr);
1307 if (ret)
1308 return 0;
1309 kernel_insn_init(&insn, buf);
1310 }
1311 insn_get_length(&insn);
1312 /* Recover address */
1313 insn.kaddr = (void *)addr;
1314 insn.next_byte = (void *)(addr + insn.length);
1315 /* Check any instructions don't jump into target */
1316 if (insn_is_indirect_jump(&insn) ||
1317 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1318 RELATIVE_ADDR_SIZE))
1319 return 0;
1320 addr += insn.length;
1321 }
1322
1323 return 1;
1324}
1325
1326/* Check optimized_kprobe can actually be optimized. */
1327int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1328{
1329 int i;
1330 struct kprobe *p;
1331
1332 for (i = 1; i < op->optinsn.size; i++) {
1333 p = get_kprobe(op->kp.addr + i);
1334 if (p && !kprobe_disabled(p))
1335 return -EEXIST;
1336 }
1337
1338 return 0;
1339}
1340
1341/* Check the addr is within the optimized instructions. */
1342int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1343 unsigned long addr)
1344{
1345 return ((unsigned long)op->kp.addr <= addr &&
1346 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1347}
1348
1349/* Free optimized instruction slot */
1350static __kprobes
1351void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1352{
1353 if (op->optinsn.insn) {
1354 free_optinsn_slot(op->optinsn.insn, dirty);
1355 op->optinsn.insn = NULL;
1356 op->optinsn.size = 0;
1357 }
1358}
1359
1360void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1361{
1362 __arch_remove_optimized_kprobe(op, 1);
1363}
1364
1365/*
1366 * Copy replacing target instructions
1367 * Target instructions MUST be relocatable (checked inside)
1368 */
1369int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1370{
1371 u8 *buf;
1372 int ret;
1373 long rel;
1374
1375 if (!can_optimize((unsigned long)op->kp.addr))
1376 return -EILSEQ;
1377
1378 op->optinsn.insn = get_optinsn_slot();
1379 if (!op->optinsn.insn)
1380 return -ENOMEM;
1381
1382 /*
1383 * Verify if the address gap is in 2GB range, because this uses
1384 * a relative jump.
1385 */
1386 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1387 if (abs(rel) > 0x7fffffff)
1388 return -ERANGE;
1389
1390 buf = (u8 *)op->optinsn.insn;
1391
1392 /* Copy instructions into the out-of-line buffer */
1393 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1394 if (ret < 0) {
1395 __arch_remove_optimized_kprobe(op, 0);
1396 return ret;
1397 }
1398 op->optinsn.size = ret;
1399
1400 /* Copy arch-dep-instance from template */
1401 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1402
1403 /* Set probe information */
1404 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1405
1406 /* Set probe function call */
1407 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1408
1409 /* Set returning jmp instruction at the tail of out-of-line buffer */
1410 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1411 (u8 *)op->kp.addr + op->optinsn.size);
1412
1413 flush_icache_range((unsigned long) buf,
1414 (unsigned long) buf + TMPL_END_IDX +
1415 op->optinsn.size + RELATIVEJUMP_SIZE);
1416 return 0;
1417}
1418
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001419#define MAX_OPTIMIZE_PROBES 256
1420static struct text_poke_param *jump_poke_params;
1421static struct jump_poke_buffer {
1422 u8 buf[RELATIVEJUMP_SIZE];
1423} *jump_poke_bufs;
1424
1425static void __kprobes setup_optimize_kprobe(struct text_poke_param *tprm,
1426 u8 *insn_buf,
1427 struct optimized_kprobe *op)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001428{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001429 s32 rel = (s32)((long)op->optinsn.insn -
1430 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1431
1432 /* Backup instructions which will be replaced by jump address */
1433 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1434 RELATIVE_ADDR_SIZE);
1435
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001436 insn_buf[0] = RELATIVEJUMP_OPCODE;
1437 *(s32 *)(&insn_buf[1]) = rel;
1438
1439 tprm->addr = op->kp.addr;
1440 tprm->opcode = insn_buf;
1441 tprm->len = RELATIVEJUMP_SIZE;
1442}
1443
1444/*
1445 * Replace breakpoints (int3) with relative jumps.
1446 * Caller must call with locking kprobe_mutex and text_mutex.
1447 */
1448void __kprobes arch_optimize_kprobes(struct list_head *oplist)
1449{
1450 struct optimized_kprobe *op, *tmp;
1451 int c = 0;
1452
1453 list_for_each_entry_safe(op, tmp, oplist, list) {
1454 WARN_ON(kprobe_disabled(&op->kp));
1455 /* Setup param */
1456 setup_optimize_kprobe(&jump_poke_params[c],
1457 jump_poke_bufs[c].buf, op);
1458 list_del_init(&op->list);
1459 if (++c >= MAX_OPTIMIZE_PROBES)
1460 break;
1461 }
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001462
1463 /*
1464 * text_poke_smp doesn't support NMI/MCE code modifying.
1465 * However, since kprobes itself also doesn't support NMI/MCE
1466 * code probing, it's not a problem.
1467 */
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001468 text_poke_smp_batch(jump_poke_params, c);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001469}
1470
Masami Hiramatsuf984ba42010-12-03 18:54:34 +09001471static void __kprobes setup_unoptimize_kprobe(struct text_poke_param *tprm,
1472 u8 *insn_buf,
1473 struct optimized_kprobe *op)
1474{
1475 /* Set int3 to first byte for kprobes */
1476 insn_buf[0] = BREAKPOINT_INSTRUCTION;
1477 memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1478
1479 tprm->addr = op->kp.addr;
1480 tprm->opcode = insn_buf;
1481 tprm->len = RELATIVEJUMP_SIZE;
1482}
1483
1484/*
1485 * Recover original instructions and breakpoints from relative jumps.
1486 * Caller must call with locking kprobe_mutex.
1487 */
1488extern void arch_unoptimize_kprobes(struct list_head *oplist,
1489 struct list_head *done_list)
1490{
1491 struct optimized_kprobe *op, *tmp;
1492 int c = 0;
1493
1494 list_for_each_entry_safe(op, tmp, oplist, list) {
1495 /* Setup param */
1496 setup_unoptimize_kprobe(&jump_poke_params[c],
1497 jump_poke_bufs[c].buf, op);
1498 list_move(&op->list, done_list);
1499 if (++c >= MAX_OPTIMIZE_PROBES)
1500 break;
1501 }
1502
1503 /*
1504 * text_poke_smp doesn't support NMI/MCE code modifying.
1505 * However, since kprobes itself also doesn't support NMI/MCE
1506 * code probing, it's not a problem.
1507 */
1508 text_poke_smp_batch(jump_poke_params, c);
1509}
1510
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001511/* Replace a relative jump with a breakpoint (int3). */
1512void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1513{
1514 u8 buf[RELATIVEJUMP_SIZE];
1515
1516 /* Set int3 to first byte for kprobes */
1517 buf[0] = BREAKPOINT_INSTRUCTION;
1518 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1519 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1520}
1521
1522static int __kprobes setup_detour_execution(struct kprobe *p,
1523 struct pt_regs *regs,
1524 int reenter)
1525{
1526 struct optimized_kprobe *op;
1527
1528 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1529 /* This kprobe is really able to run optimized path. */
1530 op = container_of(p, struct optimized_kprobe, kp);
1531 /* Detour through copied instructions */
1532 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1533 if (!reenter)
1534 reset_current_kprobe();
1535 preempt_enable_no_resched();
1536 return 1;
1537 }
1538 return 0;
1539}
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001540
1541static int __kprobes init_poke_params(void)
1542{
1543 /* Allocate code buffer and parameter array */
1544 jump_poke_bufs = kmalloc(sizeof(struct jump_poke_buffer) *
1545 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1546 if (!jump_poke_bufs)
1547 return -ENOMEM;
1548
1549 jump_poke_params = kmalloc(sizeof(struct text_poke_param) *
1550 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1551 if (!jump_poke_params) {
1552 kfree(jump_poke_bufs);
1553 jump_poke_bufs = NULL;
1554 return -ENOMEM;
1555 }
1556
1557 return 0;
1558}
1559#else /* !CONFIG_OPTPROBES */
1560static int __kprobes init_poke_params(void)
1561{
1562 return 0;
1563}
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001564#endif
1565
Rusty Lynch67729262005-07-05 18:54:50 -07001566int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001567{
Masami Hiramatsucd7ebe22010-12-03 18:54:28 +09001568 return init_poke_params();
Rusty Lynchba8af122005-06-27 15:17:10 -07001569}
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -07001570
1571int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1572{
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -07001573 return 0;
1574}