blob: 2d7763749b1b6daa73e39174fbf6cd04917b8f5a [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.
25 * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64
27 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
Rusty Lynch73649da2005-06-23 00:09:23 -070029 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
30 * Added function return probes functionality
Masami Hiramatsuda07ab02008-01-30 13:31:21 +010031 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
32 * and kretprobe-booster for x86-64
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kprobes.h>
36#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/preempt.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080040#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070041#include <linux/kdebug.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070042
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010043#include <asm/cacheflush.h>
44#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080046#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020047#include <asm/alternative.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049void jprobe_return_end(void);
50
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080051DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
52DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +010054#define stack_addr(regs) ((unsigned long *)regs->sp)
55
56#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
57 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
58 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
59 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
60 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
61 << (row % 32))
62 /*
63 * Undefined/reserved opcodes, conditional jump, Opcode Extension
64 * Groups, and some special opcodes can not boost.
65 */
66static const u32 twobyte_is_boostable[256 / 32] = {
67 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
68 /* ---------------------------------------------- */
69 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
70 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
71 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
72 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
73 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
74 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
75 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
76 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
77 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
78 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
79 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
80 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
81 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
82 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
83 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
84 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
85 /* ----------------------------------------------- */
86 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
87};
88static const u32 onebyte_has_modrm[256 / 32] = {
89 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90 /* ----------------------------------------------- */
91 W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */
92 W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */
93 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */
94 W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */
95 W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
96 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
97 W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */
98 W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */
99 W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
100 W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */
101 W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */
102 W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */
103 W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */
104 W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
105 W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */
106 W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) /* f0 */
107 /* ----------------------------------------------- */
108 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
109};
110static const u32 twobyte_has_modrm[256 / 32] = {
111 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
112 /* ----------------------------------------------- */
113 W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */
114 W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */
115 W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */
116 W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */
117 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */
118 W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */
119 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */
120 W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */
121 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */
122 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */
123 W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */
124 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */
125 W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */
126 W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */
127 W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */
128 W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* ff */
129 /* ----------------------------------------------- */
130 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
131};
132#undef W
133
Masami Hiramatsuf438d912007-10-16 01:27:49 -0700134struct kretprobe_blackpoint kretprobe_blacklist[] = {
135 {"__switch_to", }, /* This function switches only current task, but
136 doesn't switch kernel stack.*/
137 {NULL, NULL} /* Terminator */
138};
139const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
140
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100141/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
142static __always_inline void set_jmp_op(void *from, void *to)
143{
144 struct __arch_jmp_op {
145 char op;
146 s32 raddr;
147 } __attribute__((packed)) * jop;
148 jop = (struct __arch_jmp_op *)from;
149 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
150 jop->op = RELATIVEJUMP_INSTRUCTION;
151}
152
153/*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100154 * returns non-zero if opcode is boostable.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100155 * RIP relative instructions are adjusted at copying time
156 */
157static __always_inline int can_boost(kprobe_opcode_t *opcodes)
158{
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100159 kprobe_opcode_t opcode;
160 kprobe_opcode_t *orig_opcodes = opcodes;
161
162retry:
163 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
164 return 0;
165 opcode = *(opcodes++);
166
167 /* 2nd-byte opcode */
168 if (opcode == 0x0f) {
169 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
170 return 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100171 return test_bit(*opcodes,
172 (unsigned long *)twobyte_is_boostable);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100173 }
174
175 switch (opcode & 0xf0) {
176 case 0x40:
177 goto retry; /* REX prefix is boostable */
178 case 0x60:
179 if (0x63 < opcode && opcode < 0x67)
180 goto retry; /* prefixes */
181 /* can't boost Address-size override and bound */
182 return (opcode != 0x62 && opcode != 0x67);
183 case 0x70:
184 return 0; /* can't boost conditional jump */
185 case 0xc0:
186 /* can't boost software-interruptions */
187 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
188 case 0xd0:
189 /* can boost AA* and XLAT */
190 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
191 case 0xe0:
192 /* can boost in/out and absolute jmps */
193 return ((opcode & 0x04) || opcode == 0xea);
194 case 0xf0:
195 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
196 goto retry; /* lock/rep(ne) prefix */
197 /* clear and set flags are boostable */
198 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
199 default:
200 /* segment override prefixes are boostable */
201 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
202 goto retry; /* prefixes */
203 /* CS override prefix and call are not boostable */
204 return (opcode != 0x2e && opcode != 0x9a);
205 }
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
209 * returns non-zero if opcode modifies the interrupt flag.
210 */
Andrew Morton86454192007-11-26 20:42:19 +0100211static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 switch (*insn) {
214 case 0xfa: /* cli */
215 case 0xfb: /* sti */
216 case 0xcf: /* iret/iretd */
217 case 0x9d: /* popf/popfd */
218 return 1;
219 }
220
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100221 /*
222 * on 64 bit x86, 0x40-0x4f are prefixes so we need to look
223 * at the next byte instead.. but of course not recurse infinitely
224 */
225 if (*insn >= 0x40 && *insn <= 0x4f)
226 return is_IF_modifier(++insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228}
229
230/*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100231 * Adjust the displacement if the instruction uses the %rip-relative
232 * addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100233 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * If not, return null.
235 */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100236static void __kprobes fix_riprel(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100238 u8 *insn = p->ainsn.insn;
239 s64 disp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int need_modrm;
241
242 /* Skip legacy instruction prefixes. */
243 while (1) {
244 switch (*insn) {
245 case 0x66:
246 case 0x67:
247 case 0x2e:
248 case 0x3e:
249 case 0x26:
250 case 0x64:
251 case 0x65:
252 case 0x36:
253 case 0xf0:
254 case 0xf3:
255 case 0xf2:
256 ++insn;
257 continue;
258 }
259 break;
260 }
261
262 /* Skip REX instruction prefix. */
263 if ((*insn & 0xf0) == 0x40)
264 ++insn;
265
266 if (*insn == 0x0f) { /* Two-byte opcode. */
267 ++insn;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100268 need_modrm = test_bit(*insn,
269 (unsigned long *)twobyte_has_modrm);
270 } else /* One-byte opcode. */
271 need_modrm = test_bit(*insn,
272 (unsigned long *)onebyte_has_modrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (need_modrm) {
275 u8 modrm = *++insn;
276 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
277 /* Displacement follows ModRM byte. */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100278 ++insn;
279 /*
280 * The copied instruction uses the %rip-relative
281 * addressing mode. Adjust the displacement for the
282 * difference between the original location of this
283 * instruction and the location of the copy that will
284 * actually be run. The tricky bit here is making sure
285 * that the sign extension happens correctly in this
286 * calculation, since we need a signed 32-bit result to
287 * be sign-extended to 64 bits when it's added to the
288 * %rip value and yield the same 64-bit result that the
289 * sign-extension of the original signed 32-bit
290 * displacement would have given.
291 */
292 disp = (u8 *) p->addr + *((s32 *) insn) -
293 (u8 *) p->ainsn.insn;
294 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
295 *(s32 *)insn = (s32) disp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800300static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100302 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
303 fix_riprel(p);
304 if (can_boost(p->addr))
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100305 p->ainsn.boostable = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100306 else
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100307 p->ainsn.boostable = -1;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100308
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700309 p->opcode = *p->addr;
310}
311
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100312int __kprobes arch_prepare_kprobe(struct kprobe *p)
313{
314 /* insn: must be on special executable page on x86. */
315 p->ainsn.insn = get_insn_slot();
316 if (!p->ainsn.insn)
317 return -ENOMEM;
318 arch_copy_kprobe(p);
319 return 0;
320}
321
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700322void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700323{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200324 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700325}
326
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700327void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700328{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200329 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800332void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800334 mutex_lock(&kprobe_mutex);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100335 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800336 mutex_unlock(&kprobe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700339static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700340{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800341 kcb->prev_kprobe.kp = kprobe_running();
342 kcb->prev_kprobe.status = kcb->kprobe_status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100343 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
344 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700345}
346
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700347static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700348{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800349 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
350 kcb->kprobe_status = kcb->prev_kprobe.status;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100351 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
352 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700353}
354
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700355static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800356 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700357{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800358 __get_cpu_var(current_kprobe) = p;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100359 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100360 = (regs->flags & (TF_MASK | IF_MASK));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700361 if (is_IF_modifier(p->ainsn.insn))
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100362 kcb->kprobe_saved_flags &= ~IF_MASK;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700363}
364
Roland McGrath1ecc7982008-01-30 13:30:54 +0100365static __always_inline void clear_btf(void)
366{
367 if (test_thread_flag(TIF_DEBUGCTLMSR))
368 wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);
369}
370
371static __always_inline void restore_btf(void)
372{
373 if (test_thread_flag(TIF_DEBUGCTLMSR))
374 wrmsrl(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr);
375}
376
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700377static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Roland McGrath1ecc7982008-01-30 13:30:54 +0100379 clear_btf();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100380 regs->flags |= TF_MASK;
381 regs->flags &= ~IF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*single step inline if the instruction is an int3*/
383 if (p->opcode == BREAKPOINT_INSTRUCTION)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100384 regs->ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100386 regs->ip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800389/* Called with kretprobe_lock held */
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700390void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700391 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700392{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100393 unsigned long *sara = stack_addr(regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700394
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700395 ri->ret_addr = (kprobe_opcode_t *) *sara;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100396
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700397 /* Replace the return addr with trampoline addr */
398 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700399}
400
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100401/*
402 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
403 * remain disabled thorough out this function.
404 */
405static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct kprobe *p;
408 int ret = 0;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100409 kprobe_opcode_t *addr;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800410 struct kprobe_ctlblk *kcb;
411
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100412 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
413
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800414 /*
415 * We don't want to be preempted for the entire
416 * duration of kprobe processing
417 */
418 preempt_disable();
419 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Check we're not actually recursing */
422 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 p = get_kprobe(addr);
424 if (p) {
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800425 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700426 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100427 regs->flags &= ~TF_MASK;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100428 regs->flags |= kcb->kprobe_saved_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto no_kprobe;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800430 } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700431 /* TODO: Provide re-entrancy from
432 * post_kprobes_handler() and avoid exception
433 * stack corruption while single-stepping on
434 * the instruction of the new probe.
435 */
436 arch_disarm_kprobe(p);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100437 regs->ip = (unsigned long)p->addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800438 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700439 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100441 /* We have reentered the kprobe_handler(), since
442 * another probe was hit while within the handler.
443 * We here save the original kprobes variables and
444 * just single step on the instruction of the new probe
445 * without calling any user handlers.
446 */
447 save_previous_kprobe(kcb);
448 set_current_kprobe(p, regs, kcb);
449 kprobes_inc_nmissed_count(p);
450 prepare_singlestep(p, regs);
451 kcb->kprobe_status = KPROBE_REENTER;
452 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800454 if (*addr != BREAKPOINT_INSTRUCTION) {
455 /* The breakpoint instruction was removed by
456 * another cpu right after we hit, no further
457 * handling of this interrupt is appropriate
458 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100459 regs->ip = (unsigned long)addr;
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800460 ret = 1;
461 goto no_kprobe;
462 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800463 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (p->break_handler && p->break_handler(p, regs)) {
465 goto ss_probe;
466 }
467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto no_kprobe;
469 }
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 p = get_kprobe(addr);
472 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (*addr != BREAKPOINT_INSTRUCTION) {
474 /*
475 * The breakpoint instruction was removed right
476 * after we hit it. Another cpu has removed
477 * either a probepoint or a debugger breakpoint
478 * at this address. In either case, no further
479 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700480 * Back up over the (now missing) int3 and run
481 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100483 regs->ip = (unsigned long)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ret = 1;
485 }
486 /* Not one of ours: let kernel handle it */
487 goto no_kprobe;
488 }
489
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800490 set_current_kprobe(p, regs, kcb);
491 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (p->pre_handler && p->pre_handler(p, regs))
494 /* handler has already set things up, so skip ss setup */
495 return 1;
496
497ss_probe:
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100498#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
499 if (p->ainsn.boostable == 1 && !p->post_handler) {
500 /* Boost up -- we can execute copied instructions directly */
501 reset_current_kprobe();
502 regs->ip = (unsigned long)p->ainsn.insn;
503 preempt_enable_no_resched();
504 return 1;
505 }
506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 prepare_singlestep(p, regs);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800508 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 1;
510
511no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800512 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return ret;
514}
515
516/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100517 * When a retprobed function returns, this code saves registers and
518 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700519 */
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100520 void __kprobes kretprobe_trampoline_holder(void)
Rusty Lynch73649da2005-06-23 00:09:23 -0700521 {
522 asm volatile ( ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100523 "kretprobe_trampoline: \n"
524 /* We don't bother saving the ss register */
525 " pushq %rsp\n"
526 " pushfq\n"
527 /*
528 * Skip cs, ip, orig_ax.
529 * trampoline_handler() will plug in these values
530 */
531 " subq $24, %rsp\n"
532 " pushq %rdi\n"
533 " pushq %rsi\n"
534 " pushq %rdx\n"
535 " pushq %rcx\n"
536 " pushq %rax\n"
537 " pushq %r8\n"
538 " pushq %r9\n"
539 " pushq %r10\n"
540 " pushq %r11\n"
541 " pushq %rbx\n"
542 " pushq %rbp\n"
543 " pushq %r12\n"
544 " pushq %r13\n"
545 " pushq %r14\n"
546 " pushq %r15\n"
547 " movq %rsp, %rdi\n"
548 " call trampoline_handler\n"
549 /* Replace saved sp with true return address. */
550 " movq %rax, 152(%rsp)\n"
551 " popq %r15\n"
552 " popq %r14\n"
553 " popq %r13\n"
554 " popq %r12\n"
555 " popq %rbp\n"
556 " popq %rbx\n"
557 " popq %r11\n"
558 " popq %r10\n"
559 " popq %r9\n"
560 " popq %r8\n"
561 " popq %rax\n"
562 " popq %rcx\n"
563 " popq %rdx\n"
564 " popq %rsi\n"
565 " popq %rdi\n"
566 /* Skip orig_ax, ip, cs */
567 " addq $24, %rsp\n"
568 " popfq\n"
569 " ret\n");
Rusty Lynch73649da2005-06-23 00:09:23 -0700570 }
571
572/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100573 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700574 */
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100575fastcall void * __kprobes trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700576{
bibo,mao62c27be2006-10-02 02:17:33 -0700577 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700578 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700579 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800580 unsigned long flags, orig_ret_address = 0;
Rusty Lynchba8af122005-06-27 15:17:10 -0700581 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700582
bibo,mao99219a32006-10-02 02:17:35 -0700583 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800584 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700585 head = kretprobe_inst_table_head(current);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100586 /* fixup registers */
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100587 regs->cs = __KERNEL_CS;
588 regs->ip = trampoline_address;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100589 regs->orig_ax = ~0UL;
Rusty Lynch73649da2005-06-23 00:09:23 -0700590
Rusty Lynchba8af122005-06-27 15:17:10 -0700591 /*
592 * It is possible to have multiple instances associated with a given
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100593 * task either because multiple functions in the call path have
594 * return probes installed on them, and/or more then one
Rusty Lynchba8af122005-06-27 15:17:10 -0700595 * return probe was registered for a target function.
596 *
597 * We can handle this because:
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100598 * - instances are always pushed into the head of the list
Rusty Lynchba8af122005-06-27 15:17:10 -0700599 * - when multiple return probes are registered for the same
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100600 * function, the (chronologically) first instance's ret_addr
601 * will be the real return address, and all the rest will
602 * point to kretprobe_trampoline.
Rusty Lynchba8af122005-06-27 15:17:10 -0700603 */
604 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700605 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700606 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700607 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700608
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100609 if (ri->rp && ri->rp->handler) {
610 __get_cpu_var(current_kprobe) = &ri->rp->kp;
611 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
Rusty Lynchba8af122005-06-27 15:17:10 -0700612 ri->rp->handler(ri, regs);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100613 __get_cpu_var(current_kprobe) = NULL;
614 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700615
Rusty Lynchba8af122005-06-27 15:17:10 -0700616 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700617 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700618
619 if (orig_ret_address != trampoline_address)
620 /*
621 * This is the real return address. Any other
622 * instances associated with this task are for
623 * other calls deeper on the call stack
624 */
625 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700626 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700627
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700628 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700629
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800630 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700631
bibo,mao99219a32006-10-02 02:17:35 -0700632 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
633 hlist_del(&ri->hlist);
634 kfree(ri);
635 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100636 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700637}
638
639/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Called after single-stepping. p->addr is the address of the
641 * instruction whose first byte has been replaced by the "int 3"
642 * instruction. To avoid the SMP problems that can occur when we
643 * temporarily put back the original opcode to single-step, we
644 * single-stepped a copy of the instruction. The address of this
645 * copy is p->ainsn.insn.
646 *
647 * This function prepares to return from the post-single-step
648 * interrupt. We have to fix up the stack as follows:
649 *
650 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100651 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * it relative to the original instruction.
653 *
654 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100655 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *
657 * 2) If the single-stepped instruction was a call, the return address
658 * that is atop the stack is the address following the copied instruction.
659 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100660 *
661 * If this is the first time we've single-stepped the instruction at
662 * this probepoint, and the instruction is boostable, boost it: add a
663 * jump instruction after the copied instruction, that jumps to the next
664 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800666static void __kprobes resume_execution(struct kprobe *p,
667 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100669 unsigned long *tos = stack_addr(regs);
670 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
671 unsigned long orig_ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 kprobe_opcode_t *insn = p->ainsn.insn;
673
674 /*skip the REX prefix*/
675 if (*insn >= 0x40 && *insn <= 0x4f)
676 insn++;
677
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100678 regs->flags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100680 case 0x9c: /* pushfl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 *tos &= ~(TF_MASK | IF_MASK);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100682 *tos |= kcb->kprobe_old_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100684 case 0xc2: /* iret/ret/lret */
685 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700686 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100687 case 0xcb:
688 case 0xcf:
689 case 0xea: /* jmp absolute -- ip is correct */
690 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100691 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100692 goto no_change;
693 case 0xe8: /* call relative - Fix return addr */
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100694 *tos = orig_ip + (*tos - copy_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700697 if ((insn[1] & 0x30) == 0x10) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100698 /*
699 * call absolute, indirect
700 * Fix return addr; ip is correct.
701 * But this is not boostable
702 */
703 *tos = orig_ip + (*tos - copy_ip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100704 goto no_change;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100705 } else if (((insn[1] & 0x31) == 0x20) ||
706 ((insn[1] & 0x31) == 0x21)) {
707 /*
708 * jmp near and far, absolute indirect
709 * ip is correct. And this is boostable
710 */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100711 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100712 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 default:
715 break;
716 }
717
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100718 if (p->ainsn.boostable == 0) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100719 if ((regs->ip > copy_ip) &&
720 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100721 /*
722 * These instructions can be executed directly if it
723 * jumps back to correct address.
724 */
725 set_jmp_op((void *)regs->ip,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100726 (void *)orig_ip + (regs->ip - copy_ip));
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100727 p->ainsn.boostable = 1;
728 } else {
729 p->ainsn.boostable = -1;
730 }
731 }
732
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100733 regs->ip += orig_ip - copy_ip;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100734
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100735no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100736 restore_btf();
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100737
738 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100741/*
742 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
743 * remain disabled thoroughout this function.
744 */
745static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800747 struct kprobe *cur = kprobe_running();
748 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
749
750 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return 0;
752
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800753 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
754 kcb->kprobe_status = KPROBE_HIT_SSDONE;
755 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800758 resume_execution(cur, regs, kcb);
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100759 regs->flags |= kcb->kprobe_saved_flags;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100760 trace_hardirqs_fixup_flags(regs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100762 /* Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800763 if (kcb->kprobe_status == KPROBE_REENTER) {
764 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700765 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700766 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800767 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700768out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 preempt_enable_no_resched();
770
771 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100772 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * will have TF set, in which case, continue the remaining processing
774 * of do_debug, as if this is not a probe hit.
775 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100776 if (regs->flags & TF_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778
779 return 1;
780}
781
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700782int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800784 struct kprobe *cur = kprobe_running();
785 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800786 const struct exception_table_entry *fixup;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800787
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800788 switch(kcb->kprobe_status) {
789 case KPROBE_HIT_SS:
790 case KPROBE_REENTER:
791 /*
792 * We are here because the instruction being single
793 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100794 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800795 * and allow the page fault handler to continue as a
796 * normal page fault.
797 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100798 regs->ip = (unsigned long)cur->addr;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100799 regs->flags |= kcb->kprobe_old_flags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800800 if (kcb->kprobe_status == KPROBE_REENTER)
801 restore_previous_kprobe(kcb);
802 else
803 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800805 break;
806 case KPROBE_HIT_ACTIVE:
807 case KPROBE_HIT_SSDONE:
808 /*
809 * We increment the nmissed count for accounting,
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100810 * we can also use npre/npostfault count for accounting
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800811 * these specific fault cases.
812 */
813 kprobes_inc_nmissed_count(cur);
814
815 /*
816 * We come here because instructions in the pre/post
817 * handler caused the page_fault, this could happen
818 * if handler tries to access user space by
819 * copy_from_user(), get_user() etc. Let the
820 * user-specified handler try to fix it first.
821 */
822 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
823 return 1;
824
825 /*
826 * In case the user-specified fault handler returned
827 * zero, try to fix up.
828 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100829 fixup = search_exception_tables(regs->ip);
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800830 if (fixup) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100831 regs->ip = fixup->fixup;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800832 return 1;
833 }
834
835 /*
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100836 * fixup routine could not handle it,
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800837 * Let do_page_fault() fix it.
838 */
839 break;
840 default:
841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 return 0;
844}
845
846/*
847 * Wrapper routine for handling exceptions.
848 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700849int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
850 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800853 int ret = NOTIFY_DONE;
854
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100855 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -0800856 return ret;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 switch (val) {
859 case DIE_INT3:
860 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800861 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 case DIE_DEBUG:
864 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800865 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
867 case DIE_GPF:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800868 /* kprobe_running() needs smp_processor_id() */
869 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (kprobe_running() &&
871 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800872 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800873 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 break;
875 default:
876 break;
877 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800878 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700881int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct jprobe *jp = container_of(p, struct jprobe, kp);
884 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800885 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800887 kcb->jprobe_saved_regs = *regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100888 kcb->jprobe_saved_sp = stack_addr(regs);
889 addr = (unsigned long)(kcb->jprobe_saved_sp);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * As Linus pointed out, gcc assumes that the callee
893 * owns the argument space and could overwrite it, e.g.
894 * tailcall optimization. So, to be absolutely safe
895 * we also save and restore enough stack bytes to cover
896 * the argument area.
897 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800898 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
899 MIN_STACK_SIZE(addr));
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100900 regs->flags &= ~IF_MASK;
Peter Zijlstra58dfe882007-10-11 22:25:25 +0200901 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100902 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 1;
904}
905
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700906void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800908 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 asm volatile (" xchg %%rbx,%%rsp \n"
911 " int3 \n"
912 " .globl jprobe_return_end \n"
913 " jprobe_return_end: \n"
914 " nop \n"::"b"
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100915 (kcb->jprobe_saved_sp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700918int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800920 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100921 u8 *addr = (u8 *) (regs->ip - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct jprobe *jp = container_of(p, struct jprobe, kp);
923
924 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100925 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +0100926 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100927 printk("current sp %p does not match saved sp %p\n",
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100928 stack_addr(regs), kcb->jprobe_saved_sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 printk("Saved registers for jprobe %p\n", jp);
930 show_registers(saved_regs);
931 printk("Current registers\n");
932 show_registers(regs);
933 BUG();
934 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800935 *regs = kcb->jprobe_saved_regs;
Masami Hiramatsu8533bbe2008-01-30 13:31:21 +0100936 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
937 kcb->jprobes_stack,
938 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800939 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 1;
941 }
942 return 0;
943}
Rusty Lynchba8af122005-06-27 15:17:10 -0700944
Rusty Lynch67729262005-07-05 18:54:50 -0700945int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -0700946{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100947 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -0700948}
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -0700949
950int __kprobes arch_trampoline_kprobe(struct kprobe *p)
951{
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -0700952 return 0;
953}