blob: bf0e18473677c9724a082c9a1dbffa797a433bbf [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/kprobes.h>
34#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/string.h>
36#include <linux/slab.h>
37#include <linux/preempt.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080038#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070039#include <linux/kdebug.h>
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080042#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020043#include <asm/alternative.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045void jprobe_return_end(void);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -080046static void __kprobes arch_copy_kprobe(struct kprobe *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080048DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
49DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Masami Hiramatsuf438d912007-10-16 01:27:49 -070051struct kretprobe_blackpoint kretprobe_blacklist[] = {
52 {"__switch_to", }, /* This function switches only current task, but
53 doesn't switch kernel stack.*/
54 {NULL, NULL} /* Terminator */
55};
56const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
57
Masami Hiramatsuaa470142008-01-30 13:31:21 +010058/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
59static __always_inline void set_jmp_op(void *from, void *to)
60{
61 struct __arch_jmp_op {
62 char op;
63 s32 raddr;
64 } __attribute__((packed)) * jop;
65 jop = (struct __arch_jmp_op *)from;
66 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
67 jop->op = RELATIVEJUMP_INSTRUCTION;
68}
69
70/*
71 * returns non-zero if opcode is boostable
72 * RIP relative instructions are adjusted at copying time
73 */
74static __always_inline int can_boost(kprobe_opcode_t *opcodes)
75{
76#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
77 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
78 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
79 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
80 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
81 << (row % 64))
82 /*
83 * Undefined/reserved opcodes, conditional jump, Opcode Extension
84 * Groups, and some special opcodes can not boost.
85 */
86 static const unsigned long twobyte_is_boostable[256 / 64] = {
87 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
88 /* ---------------------------------------------- */
89 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0)|/* 00 */
90 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 10 */
91 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 20 */
92 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),/* 30 */
93 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 40 */
94 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 50 */
95 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1)|/* 60 */
96 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),/* 70 */
97 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 80 */
98 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 90 */
99 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* a0 */
100 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),/* b0 */
101 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)|/* c0 */
102 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* d0 */
103 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* e0 */
104 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
105 /* ----------------------------------------------- */
106 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
107 };
108#undef W
109 kprobe_opcode_t opcode;
110 kprobe_opcode_t *orig_opcodes = opcodes;
111
112retry:
113 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
114 return 0;
115 opcode = *(opcodes++);
116
117 /* 2nd-byte opcode */
118 if (opcode == 0x0f) {
119 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
120 return 0;
121 return test_bit(*opcodes, twobyte_is_boostable);
122 }
123
124 switch (opcode & 0xf0) {
125 case 0x40:
126 goto retry; /* REX prefix is boostable */
127 case 0x60:
128 if (0x63 < opcode && opcode < 0x67)
129 goto retry; /* prefixes */
130 /* can't boost Address-size override and bound */
131 return (opcode != 0x62 && opcode != 0x67);
132 case 0x70:
133 return 0; /* can't boost conditional jump */
134 case 0xc0:
135 /* can't boost software-interruptions */
136 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
137 case 0xd0:
138 /* can boost AA* and XLAT */
139 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
140 case 0xe0:
141 /* can boost in/out and absolute jmps */
142 return ((opcode & 0x04) || opcode == 0xea);
143 case 0xf0:
144 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
145 goto retry; /* lock/rep(ne) prefix */
146 /* clear and set flags are boostable */
147 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
148 default:
149 /* segment override prefixes are boostable */
150 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
151 goto retry; /* prefixes */
152 /* CS override prefix and call are not boostable */
153 return (opcode != 0x2e && opcode != 0x9a);
154 }
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/*
158 * returns non-zero if opcode modifies the interrupt flag.
159 */
Andrew Morton86454192007-11-26 20:42:19 +0100160static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 switch (*insn) {
163 case 0xfa: /* cli */
164 case 0xfb: /* sti */
165 case 0xcf: /* iret/iretd */
166 case 0x9d: /* popf/popfd */
167 return 1;
168 }
169
170 if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
171 return 1;
172 return 0;
173}
174
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700175int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 /* insn: must be on special executable page on x86_64. */
Zhang, Yanmin2dd960d2005-09-30 11:59:20 -0700178 p->ainsn.insn = get_insn_slot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!p->ainsn.insn) {
180 return -ENOMEM;
181 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800182 arch_copy_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return 0;
184}
185
186/*
187 * Determine if the instruction uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100188 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * If not, return null.
190 */
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700191static s32 __kprobes *is_riprel(u8 *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
194 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
195 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
196 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
197 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
198 << (row % 64))
199 static const u64 onebyte_has_modrm[256 / 64] = {
200 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
201 /* ------------------------------- */
202 W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
203 W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
204 W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
205 W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
206 W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
207 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
208 W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
209 W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
210 W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
211 W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
212 W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
213 W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
214 W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
215 W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
216 W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
217 W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
218 /* ------------------------------- */
219 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
220 };
221 static const u64 twobyte_has_modrm[256 / 64] = {
222 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
223 /* ------------------------------- */
224 W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
225 W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
226 W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
227 W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
228 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
229 W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
230 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
231 W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
232 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
233 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
234 W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
235 W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
236 W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
237 W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
238 W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
239 W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
240 /* ------------------------------- */
241 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
242 };
243#undef W
244 int need_modrm;
245
246 /* Skip legacy instruction prefixes. */
247 while (1) {
248 switch (*insn) {
249 case 0x66:
250 case 0x67:
251 case 0x2e:
252 case 0x3e:
253 case 0x26:
254 case 0x64:
255 case 0x65:
256 case 0x36:
257 case 0xf0:
258 case 0xf3:
259 case 0xf2:
260 ++insn;
261 continue;
262 }
263 break;
264 }
265
266 /* Skip REX instruction prefix. */
267 if ((*insn & 0xf0) == 0x40)
268 ++insn;
269
270 if (*insn == 0x0f) { /* Two-byte opcode. */
271 ++insn;
272 need_modrm = test_bit(*insn, twobyte_has_modrm);
273 } else { /* One-byte opcode. */
274 need_modrm = test_bit(*insn, onebyte_has_modrm);
275 }
276
277 if (need_modrm) {
278 u8 modrm = *++insn;
279 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
280 /* Displacement follows ModRM byte. */
281 return (s32 *) ++insn;
282 }
283 }
284
285 /* No %rip-relative addressing mode here. */
286 return NULL;
287}
288
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800289static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 s32 *ripdisp;
292 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
293 ripdisp = is_riprel(p->ainsn.insn);
294 if (ripdisp) {
295 /*
296 * The copied instruction uses the %rip-relative
297 * addressing mode. Adjust the displacement for the
298 * difference between the original location of this
299 * instruction and the location of the copy that will
300 * actually be run. The tricky bit here is making sure
301 * that the sign extension happens correctly in this
302 * calculation, since we need a signed 32-bit result to
303 * be sign-extended to 64 bits when it's added to the
304 * %rip value and yield the same 64-bit result that the
305 * sign-extension of the original signed 32-bit
306 * displacement would have given.
307 */
308 s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
309 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
310 *ripdisp = disp;
311 }
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100312 if (can_boost(p->addr)) {
313 p->ainsn.boostable = 0;
314 } else {
315 p->ainsn.boostable = -1;
316 }
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700317 p->opcode = *p->addr;
318}
319
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700320void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700321{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200322 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700323}
324
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700325void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700326{
Andi Kleen19d36cc2007-07-22 11:12:31 +0200327 text_poke(p->addr, &p->opcode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Ananth N Mavinakayanahalli0498b632006-01-09 20:52:46 -0800330void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800332 mutex_lock(&kprobe_mutex);
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100333 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800334 mutex_unlock(&kprobe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700337static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700338{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800339 kcb->prev_kprobe.kp = kprobe_running();
340 kcb->prev_kprobe.status = kcb->kprobe_status;
341 kcb->prev_kprobe.old_rflags = kcb->kprobe_old_rflags;
342 kcb->prev_kprobe.saved_rflags = kcb->kprobe_saved_rflags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700343}
344
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700345static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700346{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800347 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
348 kcb->kprobe_status = kcb->prev_kprobe.status;
349 kcb->kprobe_old_rflags = kcb->prev_kprobe.old_rflags;
350 kcb->kprobe_saved_rflags = kcb->prev_kprobe.saved_rflags;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700351}
352
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700353static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800354 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700355{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800356 __get_cpu_var(current_kprobe) = p;
357 kcb->kprobe_saved_rflags = kcb->kprobe_old_rflags
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100358 = (regs->flags & (TF_MASK | IF_MASK));
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700359 if (is_IF_modifier(p->ainsn.insn))
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800360 kcb->kprobe_saved_rflags &= ~IF_MASK;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700361}
362
Roland McGrath1ecc7982008-01-30 13:30:54 +0100363static __always_inline void clear_btf(void)
364{
365 if (test_thread_flag(TIF_DEBUGCTLMSR))
366 wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);
367}
368
369static __always_inline void restore_btf(void)
370{
371 if (test_thread_flag(TIF_DEBUGCTLMSR))
372 wrmsrl(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr);
373}
374
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700375static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Roland McGrath1ecc7982008-01-30 13:30:54 +0100377 clear_btf();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100378 regs->flags |= TF_MASK;
379 regs->flags &= ~IF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*single step inline if the instruction is an int3*/
381 if (p->opcode == BREAKPOINT_INSTRUCTION)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100382 regs->ip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 else
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100384 regs->ip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800387/* Called with kretprobe_lock held */
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700388void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700389 struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700390{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100391 unsigned long *sara = (unsigned long *)regs->sp;
Rusty Lynch73649da2005-06-23 00:09:23 -0700392
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700393 ri->ret_addr = (kprobe_opcode_t *) *sara;
394 /* Replace the return addr with trampoline addr */
395 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700396}
397
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700398int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct kprobe *p;
401 int ret = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100402 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800403 struct kprobe_ctlblk *kcb;
404
405 /*
406 * We don't want to be preempted for the entire
407 * duration of kprobe processing
408 */
409 preempt_disable();
410 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Check we're not actually recursing */
413 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 p = get_kprobe(addr);
415 if (p) {
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800416 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700417 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100418 regs->flags &= ~TF_MASK;
419 regs->flags |= kcb->kprobe_saved_rflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto no_kprobe;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800421 } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700422 /* TODO: Provide re-entrancy from
423 * post_kprobes_handler() and avoid exception
424 * stack corruption while single-stepping on
425 * the instruction of the new probe.
426 */
427 arch_disarm_kprobe(p);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100428 regs->ip = (unsigned long)p->addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800429 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700430 ret = 1;
431 } else {
432 /* We have reentered the kprobe_handler(), since
433 * another probe was hit while within the
434 * handler. We here save the original kprobe
435 * variables and just single step on instruction
436 * of the new probe without calling any user
437 * handlers.
438 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800439 save_previous_kprobe(kcb);
440 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800441 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700442 prepare_singlestep(p, regs);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800443 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700444 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800447 if (*addr != BREAKPOINT_INSTRUCTION) {
448 /* The breakpoint instruction was removed by
449 * another cpu right after we hit, no further
450 * handling of this interrupt is appropriate
451 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100452 regs->ip = (unsigned long)addr;
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800453 ret = 1;
454 goto no_kprobe;
455 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800456 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (p->break_handler && p->break_handler(p, regs)) {
458 goto ss_probe;
459 }
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto no_kprobe;
462 }
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 p = get_kprobe(addr);
465 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (*addr != BREAKPOINT_INSTRUCTION) {
467 /*
468 * The breakpoint instruction was removed right
469 * after we hit it. Another cpu has removed
470 * either a probepoint or a debugger breakpoint
471 * at this address. In either case, no further
472 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700473 * Back up over the (now missing) int3 and run
474 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100476 regs->ip = (unsigned long)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 ret = 1;
478 }
479 /* Not one of ours: let kernel handle it */
480 goto no_kprobe;
481 }
482
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800483 set_current_kprobe(p, regs, kcb);
484 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (p->pre_handler && p->pre_handler(p, regs))
487 /* handler has already set things up, so skip ss setup */
488 return 1;
489
490ss_probe:
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100491#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
492 if (p->ainsn.boostable == 1 && !p->post_handler) {
493 /* Boost up -- we can execute copied instructions directly */
494 reset_current_kprobe();
495 regs->ip = (unsigned long)p->ainsn.insn;
496 preempt_enable_no_resched();
497 return 1;
498 }
499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 prepare_singlestep(p, regs);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800501 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 1;
503
504no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800505 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return ret;
507}
508
509/*
Rusty Lynch73649da2005-06-23 00:09:23 -0700510 * For function-return probes, init_kprobes() establishes a probepoint
511 * here. When a retprobed function returns, this probe is hit and
512 * trampoline_probe_handler() runs, calling the kretprobe's handler.
513 */
514 void kretprobe_trampoline_holder(void)
515 {
516 asm volatile ( ".global kretprobe_trampoline\n"
517 "kretprobe_trampoline: \n"
518 "nop\n");
519 }
520
521/*
522 * Called when we hit the probe point at kretprobe_trampoline
523 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700524int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700525{
bibo,mao62c27be2006-10-02 02:17:33 -0700526 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700527 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700528 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800529 unsigned long flags, orig_ret_address = 0;
Rusty Lynchba8af122005-06-27 15:17:10 -0700530 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700531
bibo,mao99219a32006-10-02 02:17:35 -0700532 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800533 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700534 head = kretprobe_inst_table_head(current);
Rusty Lynch73649da2005-06-23 00:09:23 -0700535
Rusty Lynchba8af122005-06-27 15:17:10 -0700536 /*
537 * It is possible to have multiple instances associated with a given
538 * task either because an multiple functions in the call path
539 * have a return probe installed on them, and/or more then one return
540 * return probe was registered for a target function.
541 *
542 * We can handle this because:
543 * - instances are always inserted at the head of the list
544 * - when multiple return probes are registered for the same
bibo,mao62c27be2006-10-02 02:17:33 -0700545 * function, the first instance's ret_addr will point to the
Rusty Lynchba8af122005-06-27 15:17:10 -0700546 * real return address, and all the rest will point to
547 * kretprobe_trampoline
548 */
549 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700550 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700551 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700552 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700553
Rusty Lynchba8af122005-06-27 15:17:10 -0700554 if (ri->rp && ri->rp->handler)
555 ri->rp->handler(ri, regs);
Rusty Lynch73649da2005-06-23 00:09:23 -0700556
Rusty Lynchba8af122005-06-27 15:17:10 -0700557 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700558 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700559
560 if (orig_ret_address != trampoline_address)
561 /*
562 * This is the real return address. Any other
563 * instances associated with this task are for
564 * other calls deeper on the call stack
565 */
566 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700567 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700568
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700569 kretprobe_assert(ri, orig_ret_address, trampoline_address);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100570 regs->ip = orig_ret_address;
Rusty Lynchba8af122005-06-27 15:17:10 -0700571
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800572 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800573 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700574 preempt_enable_no_resched();
575
bibo,mao99219a32006-10-02 02:17:35 -0700576 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
577 hlist_del(&ri->hlist);
578 kfree(ri);
579 }
bibo,mao62c27be2006-10-02 02:17:33 -0700580 /*
581 * By returning a non-zero value, we are telling
582 * kprobe_handler() that we don't want the post_handler
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800583 * to run (and have re-enabled preemption)
bibo,mao62c27be2006-10-02 02:17:33 -0700584 */
585 return 1;
Rusty Lynch73649da2005-06-23 00:09:23 -0700586}
587
588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * Called after single-stepping. p->addr is the address of the
590 * instruction whose first byte has been replaced by the "int 3"
591 * instruction. To avoid the SMP problems that can occur when we
592 * temporarily put back the original opcode to single-step, we
593 * single-stepped a copy of the instruction. The address of this
594 * copy is p->ainsn.insn.
595 *
596 * This function prepares to return from the post-single-step
597 * interrupt. We have to fix up the stack as follows:
598 *
599 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100600 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * it relative to the original instruction.
602 *
603 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100604 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
606 * 2) If the single-stepped instruction was a call, the return address
607 * that is atop the stack is the address following the copied instruction.
608 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100609 *
610 * If this is the first time we've single-stepped the instruction at
611 * this probepoint, and the instruction is boostable, boost it: add a
612 * jump instruction after the copied instruction, that jumps to the next
613 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800615static void __kprobes resume_execution(struct kprobe *p,
616 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100618 unsigned long *tos = (unsigned long *)regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unsigned long copy_rip = (unsigned long)p->ainsn.insn;
620 unsigned long orig_rip = (unsigned long)p->addr;
621 kprobe_opcode_t *insn = p->ainsn.insn;
622
623 /*skip the REX prefix*/
624 if (*insn >= 0x40 && *insn <= 0x4f)
625 insn++;
626
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100627 regs->flags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100629 case 0x9c: /* pushfl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800631 *tos |= kcb->kprobe_old_rflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100633 case 0xc2: /* iret/ret/lret */
634 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700635 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100636 case 0xcb:
637 case 0xcf:
638 case 0xea: /* jmp absolute -- ip is correct */
639 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100640 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100641 goto no_change;
642 case 0xe8: /* call relative - Fix return addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 *tos = orig_rip + (*tos - copy_rip);
644 break;
645 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700646 if ((insn[1] & 0x30) == 0x10) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* call absolute, indirect */
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100648 /* Fix return addr; ip is correct. */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100649 /* not boostable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *tos = orig_rip + (*tos - copy_rip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100651 goto no_change;
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700652 } else if (((insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
653 ((insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100654 /* ip is correct. And this is boostable */
655 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100656 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 default:
659 break;
660 }
661
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100662 if (p->ainsn.boostable == 0) {
663 if ((regs->ip > copy_rip) &&
664 (regs->ip - copy_rip) + 5 < MAX_INSN_SIZE) {
665 /*
666 * These instructions can be executed directly if it
667 * jumps back to correct address.
668 */
669 set_jmp_op((void *)regs->ip,
670 (void *)orig_rip + (regs->ip - copy_rip));
671 p->ainsn.boostable = 1;
672 } else {
673 p->ainsn.boostable = -1;
674 }
675 }
676
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100677 regs->ip = orig_rip + (regs->ip - copy_rip);
678
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100679no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100680 restore_btf();
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100681
682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700685int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800687 struct kprobe *cur = kprobe_running();
688 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
689
690 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return 0;
692
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800693 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
694 kcb->kprobe_status = KPROBE_HIT_SSDONE;
695 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800698 resume_execution(cur, regs, kcb);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100699 regs->flags |= kcb->kprobe_saved_rflags;
700 trace_hardirqs_fixup_flags(regs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700702 /* Restore the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800703 if (kcb->kprobe_status == KPROBE_REENTER) {
704 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700705 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700706 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800707 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700708out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 preempt_enable_no_resched();
710
711 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100712 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * will have TF set, in which case, continue the remaining processing
714 * of do_debug, as if this is not a probe hit.
715 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100716 if (regs->flags & TF_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
718
719 return 1;
720}
721
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700722int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800724 struct kprobe *cur = kprobe_running();
725 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800726 const struct exception_table_entry *fixup;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800727
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800728 switch(kcb->kprobe_status) {
729 case KPROBE_HIT_SS:
730 case KPROBE_REENTER:
731 /*
732 * We are here because the instruction being single
733 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100734 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800735 * and allow the page fault handler to continue as a
736 * normal page fault.
737 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100738 regs->ip = (unsigned long)cur->addr;
739 regs->flags |= kcb->kprobe_old_rflags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800740 if (kcb->kprobe_status == KPROBE_REENTER)
741 restore_previous_kprobe(kcb);
742 else
743 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800745 break;
746 case KPROBE_HIT_ACTIVE:
747 case KPROBE_HIT_SSDONE:
748 /*
749 * We increment the nmissed count for accounting,
750 * we can also use npre/npostfault count for accouting
751 * these specific fault cases.
752 */
753 kprobes_inc_nmissed_count(cur);
754
755 /*
756 * We come here because instructions in the pre/post
757 * handler caused the page_fault, this could happen
758 * if handler tries to access user space by
759 * copy_from_user(), get_user() etc. Let the
760 * user-specified handler try to fix it first.
761 */
762 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
763 return 1;
764
765 /*
766 * In case the user-specified fault handler returned
767 * zero, try to fix up.
768 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100769 fixup = search_exception_tables(regs->ip);
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800770 if (fixup) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100771 regs->ip = fixup->fixup;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800772 return 1;
773 }
774
775 /*
776 * fixup() could not handle it,
777 * Let do_page_fault() fix it.
778 */
779 break;
780 default:
781 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 return 0;
784}
785
786/*
787 * Wrapper routine for handling exceptions.
788 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700789int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
790 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800793 int ret = NOTIFY_DONE;
794
bibo,mao2326c772006-03-26 01:38:21 -0800795 if (args->regs && user_mode(args->regs))
796 return ret;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 switch (val) {
799 case DIE_INT3:
800 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800801 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803 case DIE_DEBUG:
804 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800805 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 break;
807 case DIE_GPF:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800808 /* kprobe_running() needs smp_processor_id() */
809 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (kprobe_running() &&
811 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800812 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800813 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 break;
815 default:
816 break;
817 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700821int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct jprobe *jp = container_of(p, struct jprobe, kp);
824 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800825 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800827 kcb->jprobe_saved_regs = *regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100828 kcb->jprobe_saved_rsp = (long *) regs->sp;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800829 addr = (unsigned long)(kcb->jprobe_saved_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /*
831 * As Linus pointed out, gcc assumes that the callee
832 * owns the argument space and could overwrite it, e.g.
833 * tailcall optimization. So, to be absolutely safe
834 * we also save and restore enough stack bytes to cover
835 * the argument area.
836 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800837 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
838 MIN_STACK_SIZE(addr));
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100839 regs->flags &= ~IF_MASK;
Peter Zijlstra58dfe882007-10-11 22:25:25 +0200840 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100841 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 1;
843}
844
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700845void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800847 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 asm volatile (" xchg %%rbx,%%rsp \n"
850 " int3 \n"
851 " .globl jprobe_return_end \n"
852 " jprobe_return_end: \n"
853 " nop \n"::"b"
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800854 (kcb->jprobe_saved_rsp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700857int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800859 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100860 u8 *addr = (u8 *) (regs->ip - 1);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800861 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct jprobe *jp = container_of(p, struct jprobe, kp);
863
864 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100865 if ((unsigned long *)regs->sp != kcb->jprobe_saved_rsp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +0100866 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100867 printk("current sp %p does not match saved sp %p\n",
868 (long *)regs->sp, kcb->jprobe_saved_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 printk("Saved registers for jprobe %p\n", jp);
870 show_registers(saved_regs);
871 printk("Current registers\n");
872 show_registers(regs);
873 BUG();
874 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800875 *regs = kcb->jprobe_saved_regs;
876 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800878 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 1;
880 }
881 return 0;
882}
Rusty Lynchba8af122005-06-27 15:17:10 -0700883
884static struct kprobe trampoline_p = {
885 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
886 .pre_handler = trampoline_probe_handler
887};
888
Rusty Lynch67729262005-07-05 18:54:50 -0700889int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -0700890{
891 return register_kprobe(&trampoline_p);
892}
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -0700893
894int __kprobes arch_trampoline_kprobe(struct kprobe *p)
895{
896 if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
897 return 1;
898
899 return 0;
900}