blob: 572ecc88ca40784f143a2eca02645e23e0cbb0b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) IBM Corporation, 2002, 2004
19 *
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010025 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010029 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
30 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
31 * <prasanna@in.ibm.com> added function-return probes.
32 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
33 * Added function return probes functionality
34 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
35 * kprobe-booster and kretprobe-booster for i386.
Masami Hiramatsuda07ab02008-01-30 13:31:21 +010036 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
37 * and kretprobe-booster for x86-64
Masami Hiramatsud6be29b2008-01-30 13:31:21 +010038 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
39 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40 * unified x86 kprobes code.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kprobes.h>
44#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/string.h>
46#include <linux/slab.h>
Quentin Barnesb506a9d2008-01-30 13:32:32 +010047#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/preempt.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080049#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070050#include <linux/kdebug.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040051#include <linux/kallsyms.h>
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -050052#include <linux/ftrace.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070053
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010054#include <asm/cacheflush.h>
55#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080057#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020058#include <asm/alternative.h>
Masami Hiramatsub46b3d72009-08-13 16:34:28 -040059#include <asm/insn.h>
K.Prasad62edab92009-06-01 23:47:06 +053060#include <asm/debugreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062void jprobe_return_end(void);
63
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080064DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
65DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
H. Peter Anvin98272ed2009-10-12 14:14:10 -070067#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010068
69#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
70 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
71 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
72 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
73 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
74 << (row % 32))
75 /*
76 * Undefined/reserved opcodes, conditional jump, Opcode Extension
77 * Groups, and some special opcodes can not boost.
78 */
79static const u32 twobyte_is_boostable[256 / 32] = {
80 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
81 /* ---------------------------------------------- */
82 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
83 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
84 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
85 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
86 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
87 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
88 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
89 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
90 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
91 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
92 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
93 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
94 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
95 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
96 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
97 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
98 /* ----------------------------------------------- */
99 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
100};
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100101#undef W
102
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700103struct kretprobe_blackpoint kretprobe_blacklist[] = {
104 {"__switch_to", }, /* This function switches only current task, but
105 doesn't switch kernel stack.*/
106 {NULL, NULL} /* Terminator */
107};
108const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
109
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500110static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100111{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500112 struct __arch_relative_insn {
113 u8 op;
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100114 s32 raddr;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500115 } __attribute__((packed)) *insn;
116
117 insn = (struct __arch_relative_insn *)from;
118 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
119 insn->op = op;
120}
121
122/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
123static void __kprobes synthesize_reljump(void *from, void *to)
124{
125 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100126}
127
128/*
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900129 * Skip the prefixes of the instruction.
Harvey Harrison99309272008-01-30 13:32:14 +0100130 */
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900131static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
Harvey Harrison99309272008-01-30 13:32:14 +0100132{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900133 insn_attr_t attr;
134
135 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
136 while (inat_is_legacy_prefix(attr)) {
137 insn++;
138 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
139 }
Harvey Harrison99309272008-01-30 13:32:14 +0100140#ifdef CONFIG_X86_64
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900141 if (inat_is_rex_prefix(attr))
142 insn++;
Harvey Harrison99309272008-01-30 13:32:14 +0100143#endif
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900144 return insn;
Harvey Harrison99309272008-01-30 13:32:14 +0100145}
146
147/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100148 * Returns non-zero if opcode is boostable.
149 * RIP relative instructions are adjusted at copying time in 64 bits mode
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100150 */
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100151static int __kprobes can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100152{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100153 kprobe_opcode_t opcode;
154 kprobe_opcode_t *orig_opcodes = opcodes;
155
Jaswinder Singh Rajputcde5edb2009-03-18 17:37:45 +0530156 if (search_exception_tables((unsigned long)opcodes))
Masami Hiramatsu30390882009-03-16 18:57:22 -0400157 return 0; /* Page fault may occur on this address. */
158
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100159retry:
160 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
161 return 0;
162 opcode = *(opcodes++);
163
164 /* 2nd-byte opcode */
165 if (opcode == 0x0f) {
166 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
167 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100168 return test_bit(*opcodes,
169 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100170 }
171
172 switch (opcode & 0xf0) {
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100173#ifdef CONFIG_X86_64
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100174 case 0x40:
175 goto retry; /* REX prefix is boostable */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100176#endif
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100177 case 0x60:
178 if (0x63 < opcode && opcode < 0x67)
179 goto retry; /* prefixes */
180 /* can't boost Address-size override and bound */
181 return (opcode != 0x62 && opcode != 0x67);
182 case 0x70:
183 return 0; /* can't boost conditional jump */
184 case 0xc0:
185 /* can't boost software-interruptions */
186 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
187 case 0xd0:
188 /* can boost AA* and XLAT */
189 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
190 case 0xe0:
191 /* can boost in/out and absolute jmps */
192 return ((opcode & 0x04) || opcode == 0xea);
193 case 0xf0:
194 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
195 goto retry; /* lock/rep(ne) prefix */
196 /* clear and set flags are boostable */
197 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
198 default:
199 /* segment override prefixes are boostable */
200 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
201 goto retry; /* prefixes */
202 /* CS override prefix and call are not boostable */
203 return (opcode != 0x2e && opcode != 0x9a);
204 }
205}
206
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400207/* Recover the probed instruction at addr for further analysis. */
208static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
209{
210 struct kprobe *kp;
211 kp = get_kprobe((void *)addr);
212 if (!kp)
213 return -EINVAL;
214
215 /*
216 * Basically, kp->ainsn.insn has an original instruction.
217 * However, RIP-relative instruction can not do single-stepping
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500218 * at different place, __copy_instruction() tweaks the displacement of
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400219 * that instruction. In that case, we can't recover the instruction
220 * from the kp->ainsn.insn.
221 *
222 * On the other hand, kp->opcode has a copy of the first byte of
223 * the probed instruction, which is overwritten by int3. And
224 * the instruction at kp->addr is not modified by kprobes except
225 * for the first byte, we can recover the original instruction
226 * from it and kp->opcode.
227 */
228 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
229 buf[0] = kp->opcode;
230 return 0;
231}
232
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400233/* Check if paddr is at an instruction boundary */
234static int __kprobes can_probe(unsigned long paddr)
235{
236 int ret;
237 unsigned long addr, offset = 0;
238 struct insn insn;
239 kprobe_opcode_t buf[MAX_INSN_SIZE];
240
Namhyung Kim6abded72010-09-15 10:04:29 +0900241 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400242 return 0;
243
244 /* Decode instructions */
245 addr = paddr - offset;
246 while (addr < paddr) {
247 kernel_insn_init(&insn, (void *)addr);
248 insn_get_opcode(&insn);
249
250 /*
251 * Check if the instruction has been modified by another
252 * kprobe, in which case we replace the breakpoint by the
253 * original instruction in our buffer.
254 */
255 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
256 ret = recover_probed_instruction(buf, addr);
257 if (ret)
258 /*
259 * Another debugging subsystem might insert
260 * this breakpoint. In that case, we can't
261 * recover it.
262 */
263 return 0;
264 kernel_insn_init(&insn, buf);
265 }
266 insn_get_length(&insn);
267 addr += insn.length;
268 }
269
270 return (addr == paddr);
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/*
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100274 * Returns non-zero if opcode modifies the interrupt flag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
Andrew Morton86454192007-11-26 20:42:19 +0100276static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900278 /* Skip prefixes */
279 insn = skip_prefixes(insn);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 switch (*insn) {
282 case 0xfa: /* cli */
283 case 0xfb: /* sti */
284 case 0xcf: /* iret/iretd */
285 case 0x9d: /* popf/popfd */
286 return 1;
287 }
Harvey Harrison99309272008-01-30 13:32:14 +0100288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290}
291
292/*
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500293 * Copy an instruction and adjust the displacement if the instruction
294 * uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100295 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * If not, return null.
Harvey Harrison31f80e42008-01-30 13:32:16 +0100297 * Only applicable to 64-bit x86.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500299static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400301 struct insn insn;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500302 int ret;
303 kprobe_opcode_t buf[MAX_INSN_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500305 kernel_insn_init(&insn, src);
306 if (recover) {
307 insn_get_opcode(&insn);
308 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
309 ret = recover_probed_instruction(buf,
310 (unsigned long)src);
311 if (ret)
312 return 0;
313 kernel_insn_init(&insn, buf);
314 }
315 }
316 insn_get_length(&insn);
317 memcpy(dest, insn.kaddr, insn.length);
318
319#ifdef CONFIG_X86_64
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400320 if (insn_rip_relative(&insn)) {
321 s64 newdisp;
322 u8 *disp;
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500323 kernel_insn_init(&insn, dest);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400324 insn_get_displacement(&insn);
325 /*
326 * The copied instruction uses the %rip-relative addressing
327 * mode. Adjust the displacement for the difference between
328 * the original location of this instruction and the location
329 * of the copy that will actually be run. The tricky bit here
330 * is making sure that the sign extension happens correctly in
331 * this calculation, since we need a signed 32-bit result to
332 * be sign-extended to 64 bits when it's added to the %rip
333 * value and yield the same 64-bit result that the sign-
334 * extension of the original signed 32-bit displacement would
335 * have given.
336 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500337 newdisp = (u8 *) src + (s64) insn.displacement.value -
338 (u8 *) dest;
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400339 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500340 disp = (u8 *) dest + insn_offset_displacement(&insn);
Masami Hiramatsu89ae4652009-08-13 16:34:36 -0400341 *(s32 *) disp = (s32) newdisp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100343#endif
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500344 return insn.length;
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 Hiramatsuc0f7ac32010-02-25 08:34:46 -0500349 /*
350 * Copy an instruction without recovering int3, because it will be
351 * put by another subsystem.
352 */
353 __copy_instruction(p->ainsn.insn, p->addr, 0);
Harvey Harrison31f80e42008-01-30 13:32:16 +0100354
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100355 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100356 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100357 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100358 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100359
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700360 p->opcode = *p->addr;
361}
362
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100363int __kprobes arch_prepare_kprobe(struct kprobe *p)
364{
Masami Hiramatsu4554dbc2010-02-02 16:49:18 -0500365 if (alternatives_text_reserved(p->addr, p->addr))
366 return -EINVAL;
367
Masami Hiramatsub46b3d72009-08-13 16:34:28 -0400368 if (!can_probe((unsigned long)p->addr))
369 return -EILSEQ;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100370 /* insn: must be on special executable page on x86. */
371 p->ainsn.insn = get_insn_slot();
372 if (!p->ainsn.insn)
373 return -ENOMEM;
374 arch_copy_kprobe(p);
375 return 0;
376}
377
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700378void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700379{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200380 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700381}
382
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700383void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700384{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200385 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800388void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Masami Hiramatsu12941562009-01-06 14:41:50 -0800390 if (p->ainsn.insn) {
391 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
392 p->ainsn.insn = NULL;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700396static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700397{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800398 kcb->prev_kprobe.kp = kprobe_running();
399 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100400 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
401 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700402}
403
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700404static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700405{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600406 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800407 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100408 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
409 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700410}
411
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700412static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800413 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700414{
Christoph Lameterb76834b2010-12-06 11:16:25 -0600415 __this_cpu_write(current_kprobe, p);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100416 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100417 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700418 if (is_IF_modifier(p->ainsn.insn))
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100419 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700420}
421
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100422static void __kprobes clear_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100423{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100424 if (test_thread_flag(TIF_BLOCKSTEP)) {
425 unsigned long debugctl = get_debugctlmsr();
426
427 debugctl &= ~DEBUGCTLMSR_BTF;
428 update_debugctlmsr(debugctl);
429 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100430}
431
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100432static void __kprobes restore_btf(void)
Roland McGrath1ecc7982008-01-30 13:30:54 +0100433{
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100434 if (test_thread_flag(TIF_BLOCKSTEP)) {
435 unsigned long debugctl = get_debugctlmsr();
436
437 debugctl |= DEBUGCTLMSR_BTF;
438 update_debugctlmsr(debugctl);
439 }
Roland McGrath1ecc7982008-01-30 13:30:54 +0100440}
441
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700442void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700443 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700444{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100445 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700446
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700447 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100448
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700449 /* Replace the return addr with trampoline addr */
450 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700451}
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100452
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500453#ifdef CONFIG_OPTPROBES
454static int __kprobes setup_detour_execution(struct kprobe *p,
455 struct pt_regs *regs,
456 int reenter);
457#else
458#define setup_detour_execution(p, regs, reenter) (0)
459#endif
460
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100461static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500462 struct kprobe_ctlblk *kcb, int reenter)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100463{
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500464 if (setup_detour_execution(p, regs, reenter))
465 return;
466
Masami Hiramatsu615d0eb2010-02-02 16:49:04 -0500467#if !defined(CONFIG_PREEMPT)
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100468 if (p->ainsn.boostable == 1 && !p->post_handler) {
469 /* Boost up -- we can execute copied instructions directly */
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500470 if (!reenter)
471 reset_current_kprobe();
472 /*
473 * Reentering boosted probe doesn't reset current_kprobe,
474 * nor set current_kprobe, because it doesn't use single
475 * stepping.
476 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100477 regs->ip = (unsigned long)p->ainsn.insn;
478 preempt_enable_no_resched();
479 return;
480 }
481#endif
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500482 if (reenter) {
483 save_previous_kprobe(kcb);
484 set_current_kprobe(p, regs, kcb);
485 kcb->kprobe_status = KPROBE_REENTER;
486 } else
487 kcb->kprobe_status = KPROBE_HIT_SS;
488 /* Prepare real single stepping */
489 clear_btf();
490 regs->flags |= X86_EFLAGS_TF;
491 regs->flags &= ~X86_EFLAGS_IF;
492 /* single step inline if the instruction is an int3 */
493 if (p->opcode == BREAKPOINT_INSTRUCTION)
494 regs->ip = (unsigned long)p->addr;
495 else
496 regs->ip = (unsigned long)p->ainsn.insn;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100497}
498
Harvey Harrison40102d42008-01-30 13:32:02 +0100499/*
500 * We have reentered the kprobe_handler(), since another probe was hit while
501 * within the handler. We save the original kprobes variables and just single
502 * step on the instruction of the new probe without calling any user handlers.
503 */
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100504static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
505 struct kprobe_ctlblk *kcb)
Harvey Harrison40102d42008-01-30 13:32:02 +0100506{
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100507 switch (kcb->kprobe_status) {
508 case KPROBE_HIT_SSDONE:
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100509 case KPROBE_HIT_ACTIVE:
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100510 kprobes_inc_nmissed_count(p);
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500511 setup_singlestep(p, regs, kcb, 1);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100512 break;
513 case KPROBE_HIT_SS:
Masami Hiramatsue9afe9e2009-08-27 13:22:58 -0400514 /* A probe has been hit in the codepath leading up to, or just
515 * after, single-stepping of a probed instruction. This entire
516 * codepath should strictly reside in .kprobes.text section.
517 * Raise a BUG or we'll continue in an endless reentering loop
518 * and eventually a stack overflow.
519 */
520 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
521 p->addr);
522 dump_kprobe(p);
523 BUG();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100524 default:
525 /* impossible cases */
526 WARN_ON(1);
Abhishek Sagarfb8830e2008-01-30 13:33:13 +0100527 return 0;
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100528 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100529
Masami Hiramatsu59e87cd2008-01-30 13:32:02 +0100530 return 1;
Harvey Harrison40102d42008-01-30 13:32:02 +0100531}
Rusty Lynch73649da2005-06-23 00:09:23 -0700532
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100533/*
534 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200535 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100536 */
537static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100539 kprobe_opcode_t *addr;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100540 struct kprobe *p;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800541 struct kprobe_ctlblk *kcb;
542
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100543 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800544 /*
545 * We don't want to be preempted for the entire
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100546 * duration of kprobe processing. We conditionally
547 * re-enable preemption at the end of this function,
548 * and also in reenter_kprobe() and setup_singlestep().
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800549 */
550 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100552 kcb = get_kprobe_ctlblk();
Harvey Harrisonb9760152008-01-30 13:32:19 +0100553 p = get_kprobe(addr);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100554
Harvey Harrisonb9760152008-01-30 13:32:19 +0100555 if (p) {
Harvey Harrisonb9760152008-01-30 13:32:19 +0100556 if (kprobe_running()) {
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100557 if (reenter_kprobe(p, regs, kcb))
558 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100559 } else {
560 set_current_kprobe(p, regs, kcb);
561 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100564 * If we have no pre-handler or it returned 0, we
565 * continue with normal processing. If we have a
566 * pre-handler and it returned non-zero, it prepped
567 * for calling the break_handler below on re-entry
568 * for jprobe processing, so get out doing nothing
569 * more here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 */
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100571 if (!p->pre_handler || !p->pre_handler(p, regs))
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500572 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100573 return 1;
Harvey Harrisonb9760152008-01-30 13:32:19 +0100574 }
Masami Hiramatsu829e9242010-04-27 18:33:49 -0400575 } else if (*addr != BREAKPOINT_INSTRUCTION) {
576 /*
577 * The breakpoint instruction was removed right
578 * after we hit it. Another cpu has removed
579 * either a probepoint or a debugger breakpoint
580 * at this address. In either case, no further
581 * handling of this interrupt is appropriate.
582 * Back up over the (now missing) int3 and run
583 * the original instruction.
584 */
585 regs->ip = (unsigned long)addr;
586 preempt_enable_no_resched();
587 return 1;
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100588 } else if (kprobe_running()) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600589 p = __this_cpu_read(current_kprobe);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100590 if (p->break_handler && p->break_handler(p, regs)) {
Masami Hiramatsu0f94eb62010-02-25 08:34:23 -0500591 setup_singlestep(p, regs, kcb, 0);
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100592 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100594 } /* else: not a kprobe fault; let the kernel handle it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800596 preempt_enable_no_resched();
Abhishek Sagarf315dec2008-01-30 13:32:50 +0100597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500600#ifdef CONFIG_X86_64
601#define SAVE_REGS_STRING \
602 /* Skip cs, ip, orig_ax. */ \
603 " subq $24, %rsp\n" \
604 " pushq %rdi\n" \
605 " pushq %rsi\n" \
606 " pushq %rdx\n" \
607 " pushq %rcx\n" \
608 " pushq %rax\n" \
609 " pushq %r8\n" \
610 " pushq %r9\n" \
611 " pushq %r10\n" \
612 " pushq %r11\n" \
613 " pushq %rbx\n" \
614 " pushq %rbp\n" \
615 " pushq %r12\n" \
616 " pushq %r13\n" \
617 " pushq %r14\n" \
618 " pushq %r15\n"
619#define RESTORE_REGS_STRING \
620 " popq %r15\n" \
621 " popq %r14\n" \
622 " popq %r13\n" \
623 " popq %r12\n" \
624 " popq %rbp\n" \
625 " popq %rbx\n" \
626 " popq %r11\n" \
627 " popq %r10\n" \
628 " popq %r9\n" \
629 " popq %r8\n" \
630 " popq %rax\n" \
631 " popq %rcx\n" \
632 " popq %rdx\n" \
633 " popq %rsi\n" \
634 " popq %rdi\n" \
635 /* Skip orig_ax, ip, cs */ \
636 " addq $24, %rsp\n"
637#else
638#define SAVE_REGS_STRING \
639 /* Skip cs, ip, orig_ax and gs. */ \
640 " subl $16, %esp\n" \
641 " pushl %fs\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500642 " pushl %es\n" \
Roland McGratha1974792010-07-16 18:17:12 -0700643 " pushl %ds\n" \
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500644 " pushl %eax\n" \
645 " pushl %ebp\n" \
646 " pushl %edi\n" \
647 " pushl %esi\n" \
648 " pushl %edx\n" \
649 " pushl %ecx\n" \
650 " pushl %ebx\n"
651#define RESTORE_REGS_STRING \
652 " popl %ebx\n" \
653 " popl %ecx\n" \
654 " popl %edx\n" \
655 " popl %esi\n" \
656 " popl %edi\n" \
657 " popl %ebp\n" \
658 " popl %eax\n" \
659 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
660 " addl $24, %esp\n"
661#endif
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100664 * When a retprobed function returns, this code saves registers and
665 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700666 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800667static void __used __kprobes kretprobe_trampoline_holder(void)
Harvey Harrison10175792008-01-30 13:33:01 +0100668{
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100669 asm volatile (
670 ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100671 "kretprobe_trampoline: \n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100672#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100673 /* We don't bother saving the ss register */
674 " pushq %rsp\n"
675 " pushfq\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500676 SAVE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100677 " movq %rsp, %rdi\n"
678 " call trampoline_handler\n"
679 /* Replace saved sp with true return address. */
680 " movq %rax, 152(%rsp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500681 RESTORE_REGS_STRING
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100682 " popfq\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100683#else
684 " pushf\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500685 SAVE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100686 " movl %esp, %eax\n"
687 " call trampoline_handler\n"
688 /* Move flags to cs */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400689 " movl 56(%esp), %edx\n"
690 " movl %edx, 52(%esp)\n"
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100691 /* Replace saved flags with true return address. */
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400692 " movl %eax, 56(%esp)\n"
Masami Hiramatsuf007ea22010-02-25 08:34:30 -0500693 RESTORE_REGS_STRING
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100694 " popf\n"
695#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100696 " ret\n");
Harvey Harrison10175792008-01-30 13:33:01 +0100697}
Rusty Lynch73649da2005-06-23 00:09:23 -0700698
699/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100700 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700701 */
Harvey Harrisonf1452d42008-02-14 15:23:53 -0800702static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700703{
bibo,mao62c27be2006-10-02 02:17:33 -0700704 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700705 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700706 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800707 unsigned long flags, orig_ret_address = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100708 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900709 kprobe_opcode_t *correct_ret_addr = NULL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700710
bibo,mao99219a32006-10-02 02:17:35 -0700711 INIT_HLIST_HEAD(&empty_rp);
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700712 kretprobe_hash_lock(current, &head, &flags);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100713 /* fixup registers */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100714#ifdef CONFIG_X86_64
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100715 regs->cs = __KERNEL_CS;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100716#else
717 regs->cs = __KERNEL_CS | get_kernel_rpl();
Masami Hiramatsufee039a2009-03-23 10:14:52 -0400718 regs->gs = 0;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100719#endif
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100720 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100721 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700722
Rusty Lynchba8af122005-06-27 15:17:10 -0700723 /*
724 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100725 * task either because multiple functions in the call path have
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200726 * return probes installed on them, and/or more than one
Rusty Lynchba8af122005-06-27 15:17:10 -0700727 * return probe was registered for a target function.
728 *
729 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100730 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700731 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100732 * function, the (chronologically) first instance's ret_addr
733 * will be the real return address, and all the rest will
734 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700735 */
736 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700737 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700738 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700739 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700740
Rusty Lynchba8af122005-06-27 15:17:10 -0700741 orig_ret_address = (unsigned long)ri->ret_addr;
Rusty Lynchba8af122005-06-27 15:17:10 -0700742
743 if (orig_ret_address != trampoline_address)
744 /*
745 * This is the real return address. Any other
746 * instances associated with this task are for
747 * other calls deeper on the call stack
748 */
749 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700750 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700751
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700752 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700753
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900754 correct_ret_addr = ri->ret_addr;
755 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
756 if (ri->task != current)
757 /* another task is sharing our hash bucket */
758 continue;
759
760 orig_ret_address = (unsigned long)ri->ret_addr;
761 if (ri->rp && ri->rp->handler) {
Christoph Lameterb76834b2010-12-06 11:16:25 -0600762 __this_cpu_write(current_kprobe, &ri->rp->kp);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900763 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
764 ri->ret_addr = correct_ret_addr;
765 ri->rp->handler(ri, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -0600766 __this_cpu_write(current_kprobe, NULL);
KUMANO Syuhei737480a2010-08-15 15:18:04 +0900767 }
768
769 recycle_rp_inst(ri, &empty_rp);
770
771 if (orig_ret_address != trampoline_address)
772 /*
773 * This is the real return address. Any other
774 * instances associated with this task are for
775 * other calls deeper on the call stack
776 */
777 break;
778 }
779
Srinivasa D Sef53d9c2008-07-25 01:46:04 -0700780 kretprobe_hash_unlock(current, &flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700781
bibo,mao99219a32006-10-02 02:17:35 -0700782 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
783 hlist_del(&ri->hlist);
784 kfree(ri);
785 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100786 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700787}
788
789/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * Called after single-stepping. p->addr is the address of the
791 * instruction whose first byte has been replaced by the "int 3"
792 * instruction. To avoid the SMP problems that can occur when we
793 * temporarily put back the original opcode to single-step, we
794 * single-stepped a copy of the instruction. The address of this
795 * copy is p->ainsn.insn.
796 *
797 * This function prepares to return from the post-single-step
798 * interrupt. We have to fix up the stack as follows:
799 *
800 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100801 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * it relative to the original instruction.
803 *
804 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100805 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
807 * 2) If the single-stepped instruction was a call, the return address
808 * that is atop the stack is the address following the copied instruction.
809 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100810 *
811 * If this is the first time we've single-stepped the instruction at
812 * this probepoint, and the instruction is boostable, boost it: add a
813 * jump instruction after the copied instruction, that jumps to the next
814 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800816static void __kprobes resume_execution(struct kprobe *p,
817 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100819 unsigned long *tos = stack_addr(regs);
820 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
821 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 kprobe_opcode_t *insn = p->ainsn.insn;
823
Masami Hiramatsu567a9fd2010-06-29 14:53:50 +0900824 /* Skip prefixes */
825 insn = skip_prefixes(insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100827 regs->flags &= ~X86_EFLAGS_TF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100829 case 0x9c: /* pushfl */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100830 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100831 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100833 case 0xc2: /* iret/ret/lret */
834 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700835 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100836 case 0xcb:
837 case 0xcf:
838 case 0xea: /* jmp absolute -- ip is correct */
839 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100840 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100841 goto no_change;
842 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100843 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 break;
Harvey Harrisone7b5e112008-01-30 13:31:43 +0100845#ifdef CONFIG_X86_32
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100846 case 0x9a: /* call absolute -- same as call absolute, indirect */
847 *tos = orig_ip + (*tos - copy_ip);
848 goto no_change;
849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700851 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100852 /*
853 * call absolute, indirect
854 * Fix return addr; ip is correct.
855 * But this is not boostable
856 */
857 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100858 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100859 } else if (((insn[1] & 0x31) == 0x20) ||
860 ((insn[1] & 0x31) == 0x21)) {
861 /*
862 * jmp near and far, absolute indirect
863 * ip is correct. And this is boostable
864 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100865 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100866 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 default:
869 break;
870 }
871
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100872 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100873 if ((regs->ip > copy_ip) &&
874 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100875 /*
876 * These instructions can be executed directly if it
877 * jumps back to correct address.
878 */
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -0500879 synthesize_reljump((void *)regs->ip,
880 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100881 p->ainsn.boostable = 1;
882 } else {
883 p->ainsn.boostable = -1;
884 }
885 }
886
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100887 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100888
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100889no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100890 restore_btf();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100893/*
894 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200895 * remain disabled throughout this function.
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100896 */
897static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800899 struct kprobe *cur = kprobe_running();
900 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
901
902 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500905 resume_execution(cur, regs, kcb);
906 regs->flags |= kcb->kprobe_saved_flags;
Yakov Lerneracb5b8a2008-03-16 03:21:21 -0500907
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800908 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
909 kcb->kprobe_status = KPROBE_HIT_SSDONE;
910 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100913 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800914 if (kcb->kprobe_status == KPROBE_REENTER) {
915 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700916 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700917 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800918 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700919out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 preempt_enable_no_resched();
921
922 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100923 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * will have TF set, in which case, continue the remaining processing
925 * of do_debug, as if this is not a probe hit.
926 */
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100927 if (regs->flags & X86_EFLAGS_TF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929
930 return 1;
931}
932
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700933int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800935 struct kprobe *cur = kprobe_running();
936 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
937
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100938 switch (kcb->kprobe_status) {
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800939 case KPROBE_HIT_SS:
940 case KPROBE_REENTER:
941 /*
942 * We are here because the instruction being single
943 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100944 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800945 * and allow the page fault handler to continue as a
946 * normal page fault.
947 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100948 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100949 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800950 if (kcb->kprobe_status == KPROBE_REENTER)
951 restore_previous_kprobe(kcb);
952 else
953 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800955 break;
956 case KPROBE_HIT_ACTIVE:
957 case KPROBE_HIT_SSDONE:
958 /*
959 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100960 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800961 * these specific fault cases.
962 */
963 kprobes_inc_nmissed_count(cur);
964
965 /*
966 * We come here because instructions in the pre/post
967 * handler caused the page_fault, this could happen
968 * if handler tries to access user space by
969 * copy_from_user(), get_user() etc. Let the
970 * user-specified handler try to fix it first.
971 */
972 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
973 return 1;
974
975 /*
976 * In case the user-specified fault handler returned
977 * zero, try to fix up.
978 */
Masami Hiramatsud6be29b2008-01-30 13:31:21 +0100979 if (fixup_exception(regs))
980 return 1;
Harvey Harrison6d485832008-01-30 13:31:41 +0100981
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800982 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100983 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800984 * Let do_page_fault() fix it.
985 */
986 break;
987 default:
988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 return 0;
991}
992
993/*
994 * Wrapper routine for handling exceptions.
995 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700996int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
997 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Jan Engelhardtade1af72008-01-30 13:33:23 +0100999 struct die_args *args = data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001000 int ret = NOTIFY_DONE;
1001
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001002 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -08001003 return ret;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 switch (val) {
1006 case DIE_INT3:
1007 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001008 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 break;
1010 case DIE_DEBUG:
K.Prasad62edab92009-06-01 23:47:06 +05301011 if (post_kprobe_handler(args->regs)) {
1012 /*
1013 * Reset the BS bit in dr6 (pointed by args->err) to
1014 * denote completion of processing
1015 */
1016 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001017 ret = NOTIFY_STOP;
K.Prasad62edab92009-06-01 23:47:06 +05301018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
1020 case DIE_GPF:
Quentin Barnesb506a9d2008-01-30 13:32:32 +01001021 /*
1022 * To be potentially processing a kprobe fault and to
1023 * trust the result from kprobe_running(), we have
1024 * be non-preemptible.
1025 */
1026 if (!preemptible() && kprobe_running() &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001028 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030 default:
1031 break;
1032 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -08001033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001036int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 struct jprobe *jp = container_of(p, struct jprobe, kp);
1039 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001040 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001042 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001043 kcb->jprobe_saved_sp = stack_addr(regs);
1044 addr = (unsigned long)(kcb->jprobe_saved_sp);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /*
1047 * As Linus pointed out, gcc assumes that the callee
1048 * owns the argument space and could overwrite it, e.g.
1049 * tailcall optimization. So, to be absolutely safe
1050 * we also save and restore enough stack bytes to cover
1051 * the argument area.
1052 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001053 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001054 MIN_STACK_SIZE(addr));
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001055 regs->flags &= ~X86_EFLAGS_IF;
Peter Zijlstra58dfe882007-10-11 22:25:25 +02001056 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001057 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 1;
1059}
1060
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001061void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001063 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1064
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001065 asm volatile (
1066#ifdef CONFIG_X86_64
1067 " xchg %%rbx,%%rsp \n"
1068#else
1069 " xchgl %%ebx,%%esp \n"
1070#endif
1071 " int3 \n"
1072 " .globl jprobe_return_end\n"
1073 " jprobe_return_end: \n"
1074 " nop \n"::"b"
1075 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -07001078int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001080 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001081 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct jprobe *jp = container_of(p, struct jprobe, kp);
1083
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001084 if ((addr > (u8 *) jprobe_return) &&
1085 (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001086 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +01001087 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001088 printk(KERN_ERR
1089 "current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001090 stack_addr(regs), kcb->jprobe_saved_sp);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001091 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 show_registers(saved_regs);
Masami Hiramatsud6be29b2008-01-30 13:31:21 +01001093 printk(KERN_ERR "Current registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 show_registers(regs);
1095 BUG();
1096 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -08001097 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +01001098 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1099 kcb->jprobes_stack,
1100 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -08001101 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 1;
1103 }
1104 return 0;
1105}
Rusty Lynchba8af122005-06-27 15:17:10 -07001106
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001107
1108#ifdef CONFIG_OPTPROBES
1109
1110/* Insert a call instruction at address 'from', which calls address 'to'.*/
1111static void __kprobes synthesize_relcall(void *from, void *to)
1112{
1113 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1114}
1115
1116/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1117static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1118 unsigned long val)
1119{
1120#ifdef CONFIG_X86_64
1121 *addr++ = 0x48;
1122 *addr++ = 0xbf;
1123#else
1124 *addr++ = 0xb8;
1125#endif
1126 *(unsigned long *)addr = val;
1127}
1128
Namhyung Kim6376b222010-09-15 10:04:28 +09001129static void __used __kprobes kprobes_optinsn_template_holder(void)
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001130{
1131 asm volatile (
1132 ".global optprobe_template_entry\n"
1133 "optprobe_template_entry: \n"
1134#ifdef CONFIG_X86_64
1135 /* We don't bother saving the ss register */
1136 " pushq %rsp\n"
1137 " pushfq\n"
1138 SAVE_REGS_STRING
1139 " movq %rsp, %rsi\n"
1140 ".global optprobe_template_val\n"
1141 "optprobe_template_val: \n"
1142 ASM_NOP5
1143 ASM_NOP5
1144 ".global optprobe_template_call\n"
1145 "optprobe_template_call: \n"
1146 ASM_NOP5
1147 /* Move flags to rsp */
1148 " movq 144(%rsp), %rdx\n"
1149 " movq %rdx, 152(%rsp)\n"
1150 RESTORE_REGS_STRING
1151 /* Skip flags entry */
1152 " addq $8, %rsp\n"
1153 " popfq\n"
1154#else /* CONFIG_X86_32 */
1155 " pushf\n"
1156 SAVE_REGS_STRING
1157 " movl %esp, %edx\n"
1158 ".global optprobe_template_val\n"
1159 "optprobe_template_val: \n"
1160 ASM_NOP5
1161 ".global optprobe_template_call\n"
1162 "optprobe_template_call: \n"
1163 ASM_NOP5
1164 RESTORE_REGS_STRING
1165 " addl $4, %esp\n" /* skip cs */
1166 " popf\n"
1167#endif
1168 ".global optprobe_template_end\n"
1169 "optprobe_template_end: \n");
1170}
1171
1172#define TMPL_MOVE_IDX \
1173 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1174#define TMPL_CALL_IDX \
1175 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1176#define TMPL_END_IDX \
1177 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1178
1179#define INT3_SIZE sizeof(kprobe_opcode_t)
1180
1181/* Optimized kprobe call back function: called from optinsn */
1182static void __kprobes optimized_callback(struct optimized_kprobe *op,
1183 struct pt_regs *regs)
1184{
1185 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1186
1187 preempt_disable();
1188 if (kprobe_running()) {
1189 kprobes_inc_nmissed_count(&op->kp);
1190 } else {
1191 /* Save skipped registers */
1192#ifdef CONFIG_X86_64
1193 regs->cs = __KERNEL_CS;
1194#else
1195 regs->cs = __KERNEL_CS | get_kernel_rpl();
1196 regs->gs = 0;
1197#endif
1198 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1199 regs->orig_ax = ~0UL;
1200
Christoph Lameterb76834b2010-12-06 11:16:25 -06001201 __this_cpu_write(current_kprobe, &op->kp);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001202 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1203 opt_pre_handler(&op->kp, regs);
Christoph Lameterb76834b2010-12-06 11:16:25 -06001204 __this_cpu_write(current_kprobe, NULL);
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001205 }
1206 preempt_enable_no_resched();
1207}
1208
1209static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1210{
1211 int len = 0, ret;
1212
1213 while (len < RELATIVEJUMP_SIZE) {
1214 ret = __copy_instruction(dest + len, src + len, 1);
1215 if (!ret || !can_boost(dest + len))
1216 return -EINVAL;
1217 len += ret;
1218 }
1219 /* Check whether the address range is reserved */
1220 if (ftrace_text_reserved(src, src + len - 1) ||
Jason Baron4c3ef6d2010-09-17 11:09:08 -04001221 alternatives_text_reserved(src, src + len - 1) ||
1222 jump_label_text_reserved(src, src + len - 1))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001223 return -EBUSY;
1224
1225 return len;
1226}
1227
1228/* Check whether insn is indirect jump */
1229static int __kprobes insn_is_indirect_jump(struct insn *insn)
1230{
1231 return ((insn->opcode.bytes[0] == 0xff &&
1232 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1233 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1234}
1235
1236/* Check whether insn jumps into specified address range */
1237static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1238{
1239 unsigned long target = 0;
1240
1241 switch (insn->opcode.bytes[0]) {
1242 case 0xe0: /* loopne */
1243 case 0xe1: /* loope */
1244 case 0xe2: /* loop */
1245 case 0xe3: /* jcxz */
1246 case 0xe9: /* near relative jump */
1247 case 0xeb: /* short relative jump */
1248 break;
1249 case 0x0f:
1250 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1251 break;
1252 return 0;
1253 default:
1254 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1255 break;
1256 return 0;
1257 }
1258 target = (unsigned long)insn->next_byte + insn->immediate.value;
1259
1260 return (start <= target && target <= start + len);
1261}
1262
1263/* Decode whole function to ensure any instructions don't jump into target */
1264static int __kprobes can_optimize(unsigned long paddr)
1265{
1266 int ret;
1267 unsigned long addr, size = 0, offset = 0;
1268 struct insn insn;
1269 kprobe_opcode_t buf[MAX_INSN_SIZE];
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001270
1271 /* Lookup symbol including addr */
Namhyung Kim6abded72010-09-15 10:04:29 +09001272 if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
Masami Hiramatsuc0f7ac32010-02-25 08:34:46 -05001273 return 0;
1274
1275 /* Check there is enough space for a relative jump. */
1276 if (size - offset < RELATIVEJUMP_SIZE)
1277 return 0;
1278
1279 /* Decode instructions */
1280 addr = paddr - offset;
1281 while (addr < paddr - offset + size) { /* Decode until function end */
1282 if (search_exception_tables(addr))
1283 /*
1284 * Since some fixup code will jumps into this function,
1285 * we can't optimize kprobe in this function.
1286 */
1287 return 0;
1288 kernel_insn_init(&insn, (void *)addr);
1289 insn_get_opcode(&insn);
1290 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
1291 ret = recover_probed_instruction(buf, addr);
1292 if (ret)
1293 return 0;
1294 kernel_insn_init(&insn, buf);
1295 }
1296 insn_get_length(&insn);
1297 /* Recover address */
1298 insn.kaddr = (void *)addr;
1299 insn.next_byte = (void *)(addr + insn.length);
1300 /* Check any instructions don't jump into target */
1301 if (insn_is_indirect_jump(&insn) ||
1302 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1303 RELATIVE_ADDR_SIZE))
1304 return 0;
1305 addr += insn.length;
1306 }
1307
1308 return 1;
1309}
1310
1311/* Check optimized_kprobe can actually be optimized. */
1312int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1313{
1314 int i;
1315 struct kprobe *p;
1316
1317 for (i = 1; i < op->optinsn.size; i++) {
1318 p = get_kprobe(op->kp.addr + i);
1319 if (p && !kprobe_disabled(p))
1320 return -EEXIST;
1321 }
1322
1323 return 0;
1324}
1325
1326/* Check the addr is within the optimized instructions. */
1327int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1328 unsigned long addr)
1329{
1330 return ((unsigned long)op->kp.addr <= addr &&
1331 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1332}
1333
1334/* Free optimized instruction slot */
1335static __kprobes
1336void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1337{
1338 if (op->optinsn.insn) {
1339 free_optinsn_slot(op->optinsn.insn, dirty);
1340 op->optinsn.insn = NULL;
1341 op->optinsn.size = 0;
1342 }
1343}
1344
1345void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1346{
1347 __arch_remove_optimized_kprobe(op, 1);
1348}
1349
1350/*
1351 * Copy replacing target instructions
1352 * Target instructions MUST be relocatable (checked inside)
1353 */
1354int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1355{
1356 u8 *buf;
1357 int ret;
1358 long rel;
1359
1360 if (!can_optimize((unsigned long)op->kp.addr))
1361 return -EILSEQ;
1362
1363 op->optinsn.insn = get_optinsn_slot();
1364 if (!op->optinsn.insn)
1365 return -ENOMEM;
1366
1367 /*
1368 * Verify if the address gap is in 2GB range, because this uses
1369 * a relative jump.
1370 */
1371 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1372 if (abs(rel) > 0x7fffffff)
1373 return -ERANGE;
1374
1375 buf = (u8 *)op->optinsn.insn;
1376
1377 /* Copy instructions into the out-of-line buffer */
1378 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1379 if (ret < 0) {
1380 __arch_remove_optimized_kprobe(op, 0);
1381 return ret;
1382 }
1383 op->optinsn.size = ret;
1384
1385 /* Copy arch-dep-instance from template */
1386 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1387
1388 /* Set probe information */
1389 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1390
1391 /* Set probe function call */
1392 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1393
1394 /* Set returning jmp instruction at the tail of out-of-line buffer */
1395 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1396 (u8 *)op->kp.addr + op->optinsn.size);
1397
1398 flush_icache_range((unsigned long) buf,
1399 (unsigned long) buf + TMPL_END_IDX +
1400 op->optinsn.size + RELATIVEJUMP_SIZE);
1401 return 0;
1402}
1403
1404/* Replace a breakpoint (int3) with a relative jump. */
1405int __kprobes arch_optimize_kprobe(struct optimized_kprobe *op)
1406{
1407 unsigned char jmp_code[RELATIVEJUMP_SIZE];
1408 s32 rel = (s32)((long)op->optinsn.insn -
1409 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1410
1411 /* Backup instructions which will be replaced by jump address */
1412 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1413 RELATIVE_ADDR_SIZE);
1414
1415 jmp_code[0] = RELATIVEJUMP_OPCODE;
1416 *(s32 *)(&jmp_code[1]) = rel;
1417
1418 /*
1419 * text_poke_smp doesn't support NMI/MCE code modifying.
1420 * However, since kprobes itself also doesn't support NMI/MCE
1421 * code probing, it's not a problem.
1422 */
1423 text_poke_smp(op->kp.addr, jmp_code, RELATIVEJUMP_SIZE);
1424 return 0;
1425}
1426
1427/* Replace a relative jump with a breakpoint (int3). */
1428void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1429{
1430 u8 buf[RELATIVEJUMP_SIZE];
1431
1432 /* Set int3 to first byte for kprobes */
1433 buf[0] = BREAKPOINT_INSTRUCTION;
1434 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1435 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1436}
1437
1438static int __kprobes setup_detour_execution(struct kprobe *p,
1439 struct pt_regs *regs,
1440 int reenter)
1441{
1442 struct optimized_kprobe *op;
1443
1444 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1445 /* This kprobe is really able to run optimized path. */
1446 op = container_of(p, struct optimized_kprobe, kp);
1447 /* Detour through copied instructions */
1448 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1449 if (!reenter)
1450 reset_current_kprobe();
1451 preempt_enable_no_resched();
1452 return 1;
1453 }
1454 return 0;
1455}
1456#endif
1457
Rusty Lynch67729262005-07-05 18:54:50 -07001458int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -07001459{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +01001460 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -07001461}
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001462
1463int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1464{
Ananth N Mavinakayanahallibf8f6e5b2007-05-08 00:34:16 -07001465 return 0;
1466}