blob: bc93b1dd9a01a5c4d009b1e2a01402f16649a01b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/pgtable.h>
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -080044#include <asm/uaccess.h>
Andi Kleen19d36cc2007-07-22 11:12:31 +020045#include <asm/alternative.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047void jprobe_return_end(void);
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -080048static void __kprobes arch_copy_kprobe(struct kprobe *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -080050DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
51DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Masami Hiramatsuf438d912007-10-16 01:27:49 -070053struct kretprobe_blackpoint kretprobe_blacklist[] = {
54 {"__switch_to", }, /* This function switches only current task, but
55 doesn't switch kernel stack.*/
56 {NULL, NULL} /* Terminator */
57};
58const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
59
Masami Hiramatsuaa470142008-01-30 13:31:21 +010060/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
61static __always_inline void set_jmp_op(void *from, void *to)
62{
63 struct __arch_jmp_op {
64 char op;
65 s32 raddr;
66 } __attribute__((packed)) * jop;
67 jop = (struct __arch_jmp_op *)from;
68 jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
69 jop->op = RELATIVEJUMP_INSTRUCTION;
70}
71
72/*
73 * returns non-zero if opcode is boostable
74 * RIP relative instructions are adjusted at copying time
75 */
76static __always_inline int can_boost(kprobe_opcode_t *opcodes)
77{
78#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
79 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
80 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
81 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
82 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
83 << (row % 64))
84 /*
85 * Undefined/reserved opcodes, conditional jump, Opcode Extension
86 * Groups, and some special opcodes can not boost.
87 */
88 static const unsigned long twobyte_is_boostable[256 / 64] = {
89 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90 /* ---------------------------------------------- */
91 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0)|/* 00 */
92 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 10 */
93 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 20 */
94 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),/* 30 */
95 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 40 */
96 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 50 */
97 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1)|/* 60 */
98 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1),/* 70 */
99 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)|/* 80 */
100 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)|/* 90 */
101 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* a0 */
102 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1),/* b0 */
103 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)|/* c0 */
104 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* d0 */
105 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1)|/* e0 */
106 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
107 /* ----------------------------------------------- */
108 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
109 };
110#undef W
111 kprobe_opcode_t opcode;
112 kprobe_opcode_t *orig_opcodes = opcodes;
113
114retry:
115 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
116 return 0;
117 opcode = *(opcodes++);
118
119 /* 2nd-byte opcode */
120 if (opcode == 0x0f) {
121 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
122 return 0;
123 return test_bit(*opcodes, twobyte_is_boostable);
124 }
125
126 switch (opcode & 0xf0) {
127 case 0x40:
128 goto retry; /* REX prefix is boostable */
129 case 0x60:
130 if (0x63 < opcode && opcode < 0x67)
131 goto retry; /* prefixes */
132 /* can't boost Address-size override and bound */
133 return (opcode != 0x62 && opcode != 0x67);
134 case 0x70:
135 return 0; /* can't boost conditional jump */
136 case 0xc0:
137 /* can't boost software-interruptions */
138 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
139 case 0xd0:
140 /* can boost AA* and XLAT */
141 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
142 case 0xe0:
143 /* can boost in/out and absolute jmps */
144 return ((opcode & 0x04) || opcode == 0xea);
145 case 0xf0:
146 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
147 goto retry; /* lock/rep(ne) prefix */
148 /* clear and set flags are boostable */
149 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
150 default:
151 /* segment override prefixes are boostable */
152 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
153 goto retry; /* prefixes */
154 /* CS override prefix and call are not boostable */
155 return (opcode != 0x2e && opcode != 0x9a);
156 }
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * returns non-zero if opcode modifies the interrupt flag.
161 */
Andrew Morton86454192007-11-26 20:42:19 +0100162static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 switch (*insn) {
165 case 0xfa: /* cli */
166 case 0xfb: /* sti */
167 case 0xcf: /* iret/iretd */
168 case 0x9d: /* popf/popfd */
169 return 1;
170 }
171
172 if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
173 return 1;
174 return 0;
175}
176
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700177int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 /* insn: must be on special executable page on x86_64. */
Zhang, Yanmin2dd960d2005-09-30 11:59:20 -0700180 p->ainsn.insn = get_insn_slot();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!p->ainsn.insn) {
182 return -ENOMEM;
183 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800184 arch_copy_kprobe(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
186}
187
188/*
189 * Determine if the instruction uses the %rip-relative addressing mode.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100190 * If it does, Return the address of the 32-bit displacement word.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * If not, return null.
192 */
Prasanna S Panchamukhi3b602112006-04-18 22:22:00 -0700193static s32 __kprobes *is_riprel(u8 *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
196 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
197 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
198 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
199 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
200 << (row % 64))
201 static const u64 onebyte_has_modrm[256 / 64] = {
202 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
203 /* ------------------------------- */
204 W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
205 W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
206 W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
207 W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
208 W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
209 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
210 W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
211 W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
212 W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
213 W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
214 W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
215 W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
216 W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
217 W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
218 W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
219 W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
220 /* ------------------------------- */
221 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
222 };
223 static const u64 twobyte_has_modrm[256 / 64] = {
224 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
225 /* ------------------------------- */
226 W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
227 W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
228 W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
229 W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
230 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
231 W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
232 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
233 W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
234 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
235 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
236 W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
237 W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
238 W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
239 W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
240 W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
241 W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
242 /* ------------------------------- */
243 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
244 };
245#undef W
246 int need_modrm;
247
248 /* Skip legacy instruction prefixes. */
249 while (1) {
250 switch (*insn) {
251 case 0x66:
252 case 0x67:
253 case 0x2e:
254 case 0x3e:
255 case 0x26:
256 case 0x64:
257 case 0x65:
258 case 0x36:
259 case 0xf0:
260 case 0xf3:
261 case 0xf2:
262 ++insn;
263 continue;
264 }
265 break;
266 }
267
268 /* Skip REX instruction prefix. */
269 if ((*insn & 0xf0) == 0x40)
270 ++insn;
271
272 if (*insn == 0x0f) { /* Two-byte opcode. */
273 ++insn;
274 need_modrm = test_bit(*insn, twobyte_has_modrm);
275 } else { /* One-byte opcode. */
276 need_modrm = test_bit(*insn, onebyte_has_modrm);
277 }
278
279 if (need_modrm) {
280 u8 modrm = *++insn;
281 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
282 /* Displacement follows ModRM byte. */
283 return (s32 *) ++insn;
284 }
285 }
286
287 /* No %rip-relative addressing mode here. */
288 return NULL;
289}
290
Keshavamurthy Anil Sf709b122006-01-09 20:52:44 -0800291static void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 s32 *ripdisp;
294 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
295 ripdisp = is_riprel(p->ainsn.insn);
296 if (ripdisp) {
297 /*
298 * The copied instruction uses the %rip-relative
299 * addressing mode. Adjust the displacement for the
300 * difference between the original location of this
301 * instruction and the location of the copy that will
302 * actually be run. The tricky bit here is making sure
303 * that the sign extension happens correctly in this
304 * calculation, since we need a signed 32-bit result to
305 * be sign-extended to 64 bits when it's added to the
306 * %rip value and yield the same 64-bit result that the
307 * sign-extension of the original signed 32-bit
308 * displacement would have given.
309 */
310 s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
311 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
312 *ripdisp = disp;
313 }
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100314 if (can_boost(p->addr)) {
315 p->ainsn.boostable = 0;
316 } else {
317 p->ainsn.boostable = -1;
318 }
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700319 p->opcode = *p->addr;
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;
343 kcb->prev_kprobe.old_rflags = kcb->kprobe_old_rflags;
344 kcb->prev_kprobe.saved_rflags = kcb->kprobe_saved_rflags;
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;
351 kcb->kprobe_old_rflags = kcb->prev_kprobe.old_rflags;
352 kcb->kprobe_saved_rflags = kcb->prev_kprobe.saved_rflags;
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;
359 kcb->kprobe_saved_rflags = kcb->kprobe_old_rflags
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))
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800362 kcb->kprobe_saved_rflags &= ~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{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100393 unsigned long *sara = (unsigned long *)regs->sp;
Rusty Lynch73649da2005-06-23 00:09:23 -0700394
Christoph Hellwig4c4308c2007-05-08 00:34:14 -0700395 ri->ret_addr = (kprobe_opcode_t *) *sara;
396 /* Replace the return addr with trampoline addr */
397 *sara = (unsigned long) &kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700398}
399
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700400int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct kprobe *p;
403 int ret = 0;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100404 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800405 struct kprobe_ctlblk *kcb;
406
407 /*
408 * We don't want to be preempted for the entire
409 * duration of kprobe processing
410 */
411 preempt_disable();
412 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* Check we're not actually recursing */
415 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 p = get_kprobe(addr);
417 if (p) {
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800418 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700419 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100420 regs->flags &= ~TF_MASK;
421 regs->flags |= kcb->kprobe_saved_rflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto no_kprobe;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800423 } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700424 /* TODO: Provide re-entrancy from
425 * post_kprobes_handler() and avoid exception
426 * stack corruption while single-stepping on
427 * the instruction of the new probe.
428 */
429 arch_disarm_kprobe(p);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100430 regs->ip = (unsigned long)p->addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800431 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700432 ret = 1;
433 } else {
434 /* We have reentered the kprobe_handler(), since
435 * another probe was hit while within the
436 * handler. We here save the original kprobe
437 * variables and just single step on instruction
438 * of the new probe without calling any user
439 * handlers.
440 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800441 save_previous_kprobe(kcb);
442 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800443 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700444 prepare_singlestep(p, regs);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800445 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700446 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800449 if (*addr != BREAKPOINT_INSTRUCTION) {
450 /* The breakpoint instruction was removed by
451 * another cpu right after we hit, no further
452 * handling of this interrupt is appropriate
453 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100454 regs->ip = (unsigned long)addr;
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800455 ret = 1;
456 goto no_kprobe;
457 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800458 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (p->break_handler && p->break_handler(p, regs)) {
460 goto ss_probe;
461 }
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 goto no_kprobe;
464 }
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 p = get_kprobe(addr);
467 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (*addr != BREAKPOINT_INSTRUCTION) {
469 /*
470 * The breakpoint instruction was removed right
471 * after we hit it. Another cpu has removed
472 * either a probepoint or a debugger breakpoint
473 * at this address. In either case, no further
474 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700475 * Back up over the (now missing) int3 and run
476 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100478 regs->ip = (unsigned long)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ret = 1;
480 }
481 /* Not one of ours: let kernel handle it */
482 goto no_kprobe;
483 }
484
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800485 set_current_kprobe(p, regs, kcb);
486 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (p->pre_handler && p->pre_handler(p, regs))
489 /* handler has already set things up, so skip ss setup */
490 return 1;
491
492ss_probe:
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100493#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
494 if (p->ainsn.boostable == 1 && !p->post_handler) {
495 /* Boost up -- we can execute copied instructions directly */
496 reset_current_kprobe();
497 regs->ip = (unsigned long)p->ainsn.insn;
498 preempt_enable_no_resched();
499 return 1;
500 }
501#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 prepare_singlestep(p, regs);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800503 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 1;
505
506no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800507 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return ret;
509}
510
511/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100512 * When a retprobed function returns, this code saves registers and
513 * calls trampoline_handler() runs, which calls the kretprobe's handler.
Rusty Lynch73649da2005-06-23 00:09:23 -0700514 */
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100515 void __kprobes kretprobe_trampoline_holder(void)
Rusty Lynch73649da2005-06-23 00:09:23 -0700516 {
517 asm volatile ( ".global kretprobe_trampoline\n"
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100518 "kretprobe_trampoline: \n"
519 /* We don't bother saving the ss register */
520 " pushq %rsp\n"
521 " pushfq\n"
522 /*
523 * Skip cs, ip, orig_ax.
524 * trampoline_handler() will plug in these values
525 */
526 " subq $24, %rsp\n"
527 " pushq %rdi\n"
528 " pushq %rsi\n"
529 " pushq %rdx\n"
530 " pushq %rcx\n"
531 " pushq %rax\n"
532 " pushq %r8\n"
533 " pushq %r9\n"
534 " pushq %r10\n"
535 " pushq %r11\n"
536 " pushq %rbx\n"
537 " pushq %rbp\n"
538 " pushq %r12\n"
539 " pushq %r13\n"
540 " pushq %r14\n"
541 " pushq %r15\n"
542 " movq %rsp, %rdi\n"
543 " call trampoline_handler\n"
544 /* Replace saved sp with true return address. */
545 " movq %rax, 152(%rsp)\n"
546 " popq %r15\n"
547 " popq %r14\n"
548 " popq %r13\n"
549 " popq %r12\n"
550 " popq %rbp\n"
551 " popq %rbx\n"
552 " popq %r11\n"
553 " popq %r10\n"
554 " popq %r9\n"
555 " popq %r8\n"
556 " popq %rax\n"
557 " popq %rcx\n"
558 " popq %rdx\n"
559 " popq %rsi\n"
560 " popq %rdi\n"
561 /* Skip orig_ax, ip, cs */
562 " addq $24, %rsp\n"
563 " popfq\n"
564 " ret\n");
Rusty Lynch73649da2005-06-23 00:09:23 -0700565 }
566
567/*
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100568 * Called from kretprobe_trampoline
Rusty Lynch73649da2005-06-23 00:09:23 -0700569 */
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100570fastcall void * __kprobes trampoline_handler(struct pt_regs *regs)
Rusty Lynch73649da2005-06-23 00:09:23 -0700571{
bibo,mao62c27be2006-10-02 02:17:33 -0700572 struct kretprobe_instance *ri = NULL;
bibo,mao99219a32006-10-02 02:17:35 -0700573 struct hlist_head *head, empty_rp;
bibo,mao62c27be2006-10-02 02:17:33 -0700574 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800575 unsigned long flags, orig_ret_address = 0;
Rusty Lynchba8af122005-06-27 15:17:10 -0700576 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Rusty Lynch73649da2005-06-23 00:09:23 -0700577
bibo,mao99219a32006-10-02 02:17:35 -0700578 INIT_HLIST_HEAD(&empty_rp);
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800579 spin_lock_irqsave(&kretprobe_lock, flags);
bibo,mao62c27be2006-10-02 02:17:33 -0700580 head = kretprobe_inst_table_head(current);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100581 /* fixup rt_regs */
582 regs->cs = __KERNEL_CS;
583 regs->ip = trampoline_address;
584 regs->orig_ax = 0xffffffffffffffff;
Rusty Lynch73649da2005-06-23 00:09:23 -0700585
Rusty Lynchba8af122005-06-27 15:17:10 -0700586 /*
587 * It is possible to have multiple instances associated with a given
588 * task either because an multiple functions in the call path
589 * have a return probe installed on them, and/or more then one return
590 * return probe was registered for a target function.
591 *
592 * We can handle this because:
593 * - instances are always inserted at the head of the list
594 * - when multiple return probes are registered for the same
bibo,mao62c27be2006-10-02 02:17:33 -0700595 * function, the first instance's ret_addr will point to the
Rusty Lynchba8af122005-06-27 15:17:10 -0700596 * real return address, and all the rest will point to
597 * kretprobe_trampoline
598 */
599 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
bibo,mao62c27be2006-10-02 02:17:33 -0700600 if (ri->task != current)
Rusty Lynchba8af122005-06-27 15:17:10 -0700601 /* another task is sharing our hash bucket */
bibo,mao62c27be2006-10-02 02:17:33 -0700602 continue;
Rusty Lynch73649da2005-06-23 00:09:23 -0700603
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100604 if (ri->rp && ri->rp->handler) {
605 __get_cpu_var(current_kprobe) = &ri->rp->kp;
606 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
Rusty Lynchba8af122005-06-27 15:17:10 -0700607 ri->rp->handler(ri, regs);
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100608 __get_cpu_var(current_kprobe) = NULL;
609 }
Rusty Lynch73649da2005-06-23 00:09:23 -0700610
Rusty Lynchba8af122005-06-27 15:17:10 -0700611 orig_ret_address = (unsigned long)ri->ret_addr;
bibo,mao99219a32006-10-02 02:17:35 -0700612 recycle_rp_inst(ri, &empty_rp);
Rusty Lynchba8af122005-06-27 15:17:10 -0700613
614 if (orig_ret_address != trampoline_address)
615 /*
616 * This is the real return address. Any other
617 * instances associated with this task are for
618 * other calls deeper on the call stack
619 */
620 break;
Rusty Lynch73649da2005-06-23 00:09:23 -0700621 }
Rusty Lynchba8af122005-06-27 15:17:10 -0700622
Ananth N Mavinakayanahalli0f95b7f2007-05-08 00:28:27 -0700623 kretprobe_assert(ri, orig_ret_address, trampoline_address);
Rusty Lynchba8af122005-06-27 15:17:10 -0700624
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800625 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynchba8af122005-06-27 15:17:10 -0700626
bibo,mao99219a32006-10-02 02:17:35 -0700627 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
628 hlist_del(&ri->hlist);
629 kfree(ri);
630 }
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100631 return (void *)orig_ret_address;
Rusty Lynch73649da2005-06-23 00:09:23 -0700632}
633
634/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * Called after single-stepping. p->addr is the address of the
636 * instruction whose first byte has been replaced by the "int 3"
637 * instruction. To avoid the SMP problems that can occur when we
638 * temporarily put back the original opcode to single-step, we
639 * single-stepped a copy of the instruction. The address of this
640 * copy is p->ainsn.insn.
641 *
642 * This function prepares to return from the post-single-step
643 * interrupt. We have to fix up the stack as follows:
644 *
645 * 0) Except in the case of absolute or indirect jump or call instructions,
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100646 * the new ip is relative to the copied instruction. We need to make
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * it relative to the original instruction.
648 *
649 * 1) If the single-stepped instruction was pushfl, then the TF and IF
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100650 * flags are set in the just-pushed flags, and may need to be cleared.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *
652 * 2) If the single-stepped instruction was a call, the return address
653 * that is atop the stack is the address following the copied instruction.
654 * We need to make it the address following the original instruction.
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100655 *
656 * If this is the first time we've single-stepped the instruction at
657 * this probepoint, and the instruction is boostable, boost it: add a
658 * jump instruction after the copied instruction, that jumps to the next
659 * instruction after the probepoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800661static void __kprobes resume_execution(struct kprobe *p,
662 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100664 unsigned long *tos = (unsigned long *)regs->sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned long copy_rip = (unsigned long)p->ainsn.insn;
666 unsigned long orig_rip = (unsigned long)p->addr;
667 kprobe_opcode_t *insn = p->ainsn.insn;
668
669 /*skip the REX prefix*/
670 if (*insn >= 0x40 && *insn <= 0x4f)
671 insn++;
672
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100673 regs->flags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 switch (*insn) {
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100675 case 0x9c: /* pushfl */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800677 *tos |= kcb->kprobe_old_rflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100679 case 0xc2: /* iret/ret/lret */
680 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700681 case 0xca:
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100682 case 0xcb:
683 case 0xcf:
684 case 0xea: /* jmp absolute -- ip is correct */
685 /* ip is already adjusted, no more changes required */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100686 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100687 goto no_change;
688 case 0xe8: /* call relative - Fix return addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *tos = orig_rip + (*tos - copy_rip);
690 break;
691 case 0xff:
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700692 if ((insn[1] & 0x30) == 0x10) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* call absolute, indirect */
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100694 /* Fix return addr; ip is correct. */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100695 /* not boostable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *tos = orig_rip + (*tos - copy_rip);
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100697 goto no_change;
Satoshi Oshimadc49e342006-05-20 15:00:21 -0700698 } else if (((insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
699 ((insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100700 /* ip is correct. And this is boostable */
701 p->ainsn.boostable = 1;
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100702 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 default:
705 break;
706 }
707
Masami Hiramatsuaa470142008-01-30 13:31:21 +0100708 if (p->ainsn.boostable == 0) {
709 if ((regs->ip > copy_rip) &&
710 (regs->ip - copy_rip) + 5 < MAX_INSN_SIZE) {
711 /*
712 * These instructions can be executed directly if it
713 * jumps back to correct address.
714 */
715 set_jmp_op((void *)regs->ip,
716 (void *)orig_rip + (regs->ip - copy_rip));
717 p->ainsn.boostable = 1;
718 } else {
719 p->ainsn.boostable = -1;
720 }
721 }
722
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100723 regs->ip = orig_rip + (regs->ip - copy_rip);
724
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100725no_change:
Roland McGrath1ecc7982008-01-30 13:30:54 +0100726 restore_btf();
Masami Hiramatsu0b0122f2007-12-18 18:05:58 +0100727
728 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700731int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800733 struct kprobe *cur = kprobe_running();
734 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
735
736 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return 0;
738
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800739 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
740 kcb->kprobe_status = KPROBE_HIT_SSDONE;
741 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800744 resume_execution(cur, regs, kcb);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100745 regs->flags |= kcb->kprobe_saved_rflags;
746 trace_hardirqs_fixup_flags(regs->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700748 /* Restore the original saved kprobes variables and continue. */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800749 if (kcb->kprobe_status == KPROBE_REENTER) {
750 restore_previous_kprobe(kcb);
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700751 goto out;
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700752 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800753 reset_current_kprobe();
Prasanna S Panchamukhiaa3d7e32005-06-23 00:09:37 -0700754out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 preempt_enable_no_resched();
756
757 /*
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100758 * if somebody else is singlestepping across a probe point, flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * will have TF set, in which case, continue the remaining processing
760 * of do_debug, as if this is not a probe hit.
761 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100762 if (regs->flags & TF_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764
765 return 1;
766}
767
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700768int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800770 struct kprobe *cur = kprobe_running();
771 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800772 const struct exception_table_entry *fixup;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800773
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800774 switch(kcb->kprobe_status) {
775 case KPROBE_HIT_SS:
776 case KPROBE_REENTER:
777 /*
778 * We are here because the instruction being single
779 * stepped caused a page fault. We reset the current
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100780 * kprobe and the ip points back to the probe address
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800781 * and allow the page fault handler to continue as a
782 * normal page fault.
783 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100784 regs->ip = (unsigned long)cur->addr;
785 regs->flags |= kcb->kprobe_old_rflags;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800786 if (kcb->kprobe_status == KPROBE_REENTER)
787 restore_previous_kprobe(kcb);
788 else
789 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 preempt_enable_no_resched();
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800791 break;
792 case KPROBE_HIT_ACTIVE:
793 case KPROBE_HIT_SSDONE:
794 /*
795 * We increment the nmissed count for accounting,
796 * we can also use npre/npostfault count for accouting
797 * these specific fault cases.
798 */
799 kprobes_inc_nmissed_count(cur);
800
801 /*
802 * We come here because instructions in the pre/post
803 * handler caused the page_fault, this could happen
804 * if handler tries to access user space by
805 * copy_from_user(), get_user() etc. Let the
806 * user-specified handler try to fix it first.
807 */
808 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
809 return 1;
810
811 /*
812 * In case the user-specified fault handler returned
813 * zero, try to fix up.
814 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100815 fixup = search_exception_tables(regs->ip);
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800816 if (fixup) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100817 regs->ip = fixup->fixup;
Prasanna S Panchamukhic28f8962006-03-26 01:38:23 -0800818 return 1;
819 }
820
821 /*
822 * fixup() could not handle it,
823 * Let do_page_fault() fix it.
824 */
825 break;
826 default:
827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 return 0;
830}
831
832/*
833 * Wrapper routine for handling exceptions.
834 */
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700835int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
836 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800839 int ret = NOTIFY_DONE;
840
bibo,mao2326c772006-03-26 01:38:21 -0800841 if (args->regs && user_mode(args->regs))
842 return ret;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 switch (val) {
845 case DIE_INT3:
846 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800847 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 break;
849 case DIE_DEBUG:
850 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800851 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 break;
853 case DIE_GPF:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800854 /* kprobe_running() needs smp_processor_id() */
855 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (kprobe_running() &&
857 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800858 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800859 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 break;
861 default:
862 break;
863 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700867int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct jprobe *jp = container_of(p, struct jprobe, kp);
870 unsigned long addr;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800871 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800873 kcb->jprobe_saved_regs = *regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100874 kcb->jprobe_saved_rsp = (long *) regs->sp;
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800875 addr = (unsigned long)(kcb->jprobe_saved_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /*
877 * As Linus pointed out, gcc assumes that the callee
878 * owns the argument space and could overwrite it, e.g.
879 * tailcall optimization. So, to be absolutely safe
880 * we also save and restore enough stack bytes to cover
881 * the argument area.
882 */
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800883 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
884 MIN_STACK_SIZE(addr));
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100885 regs->flags &= ~IF_MASK;
Peter Zijlstra58dfe882007-10-11 22:25:25 +0200886 trace_hardirqs_off();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100887 regs->ip = (unsigned long)(jp->entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 1;
889}
890
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700891void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800893 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 asm volatile (" xchg %%rbx,%%rsp \n"
896 " int3 \n"
897 " .globl jprobe_return_end \n"
898 " jprobe_return_end: \n"
899 " nop \n"::"b"
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800900 (kcb->jprobe_saved_rsp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -0700903int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800905 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100906 u8 *addr = (u8 *) (regs->ip - 1);
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800907 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct jprobe *jp = container_of(p, struct jprobe, kp);
909
910 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100911 if ((unsigned long *)regs->sp != kcb->jprobe_saved_rsp) {
Masami Hiramatsu29b6cd72007-12-18 18:05:58 +0100912 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100913 printk("current sp %p does not match saved sp %p\n",
914 (long *)regs->sp, kcb->jprobe_saved_rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 printk("Saved registers for jprobe %p\n", jp);
916 show_registers(saved_regs);
917 printk("Current registers\n");
918 show_registers(regs);
919 BUG();
920 }
Ananth N Mavinakayanahallie7a510f2005-11-07 01:00:12 -0800921 *regs = kcb->jprobe_saved_regs;
922 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800924 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return 1;
926 }
927 return 0;
928}
Rusty Lynchba8af122005-06-27 15:17:10 -0700929
Rusty Lynch67729262005-07-05 18:54:50 -0700930int __init arch_init_kprobes(void)
Rusty Lynchba8af122005-06-27 15:17:10 -0700931{
Masami Hiramatsuda07ab02008-01-30 13:31:21 +0100932 return 0;
Rusty Lynchba8af122005-06-27 15:17:10 -0700933}
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -0700934
935int __kprobes arch_trampoline_kprobe(struct kprobe *p)
936{
Ananth N Mavinakayanahallibf8f6e52007-05-08 00:34:16 -0700937 return 0;
938}