Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzik...
[deliverable/linux.git] / arch / x86 / kernel / kprobes_32.c
CommitLineData
1da177e4
LT
1/*
2 * Kernel Probes (KProbes)
3 * arch/i386/kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
b94cce92
HN
26 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
27 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
28 * <prasanna@in.ibm.com> added function-return probes.
1da177e4
LT
29 */
30
1da177e4
LT
31#include <linux/kprobes.h>
32#include <linux/ptrace.h>
1da177e4 33#include <linux/preempt.h>
1eeb66a1 34#include <linux/kdebug.h>
7e1048b1 35#include <asm/cacheflush.h>
1da177e4 36#include <asm/desc.h>
b4026513 37#include <asm/uaccess.h>
19d36ccd 38#include <asm/alternative.h>
1da177e4 39
1da177e4
LT
40void jprobe_return_end(void);
41
9a0e3a86
AM
42DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
43DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
44
311ac88f 45/* insert a jmp code */
34c37e18 46static __always_inline void set_jmp_op(void *from, void *to)
311ac88f
MH
47{
48 struct __arch_jmp_op {
49 char op;
50 long raddr;
51 } __attribute__((packed)) *jop;
52 jop = (struct __arch_jmp_op *)from;
53 jop->raddr = (long)(to) - ((long)(from) + 5);
54 jop->op = RELATIVEJUMP_INSTRUCTION;
55}
56
57/*
58 * returns non-zero if opcodes can be boosted.
59 */
585deaca 60static __always_inline int can_boost(kprobe_opcode_t *opcodes)
311ac88f 61{
585deaca
MH
62#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
63 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
64 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
65 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
66 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
67 << (row % 32))
68 /*
69 * Undefined/reserved opcodes, conditional jump, Opcode Extension
70 * Groups, and some special opcodes can not be boost.
71 */
72 static const unsigned long twobyte_is_boostable[256 / 32] = {
73 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
74 /* ------------------------------- */
75 W(0x00, 0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0)| /* 00 */
76 W(0x10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 10 */
77 W(0x20, 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)| /* 20 */
78 W(0x30, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 30 */
79 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 40 */
80 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 50 */
81 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1)| /* 60 */
82 W(0x70, 0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1), /* 70 */
83 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 80 */
84 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), /* 90 */
85 W(0xa0, 1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1)| /* a0 */
86 W(0xb0, 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1), /* b0 */
87 W(0xc0, 1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1)| /* c0 */
88 W(0xd0, 0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1), /* d0 */
89 W(0xe0, 0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1)| /* e0 */
90 W(0xf0, 0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0) /* f0 */
91 /* ------------------------------- */
92 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
93 };
94#undef W
95 kprobe_opcode_t opcode;
96 kprobe_opcode_t *orig_opcodes = opcodes;
97retry:
98 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
99 return 0;
100 opcode = *(opcodes++);
101
102 /* 2nd-byte opcode */
103 if (opcode == 0x0f) {
104 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
105 return 0;
106 return test_bit(*opcodes, twobyte_is_boostable);
107 }
108
109 switch (opcode & 0xf0) {
110 case 0x60:
111 if (0x63 < opcode && opcode < 0x67)
112 goto retry; /* prefixes */
113 /* can't boost Address-size override and bound */
114 return (opcode != 0x62 && opcode != 0x67);
311ac88f
MH
115 case 0x70:
116 return 0; /* can't boost conditional jump */
311ac88f 117 case 0xc0:
585deaca
MH
118 /* can't boost software-interruptions */
119 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
311ac88f
MH
120 case 0xd0:
121 /* can boost AA* and XLAT */
122 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
123 case 0xe0:
585deaca
MH
124 /* can boost in/out and absolute jmps */
125 return ((opcode & 0x04) || opcode == 0xea);
311ac88f 126 case 0xf0:
585deaca
MH
127 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
128 goto retry; /* lock/rep(ne) prefix */
311ac88f
MH
129 /* clear and set flags can be boost */
130 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
131 default:
585deaca
MH
132 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
133 goto retry; /* prefixes */
134 /* can't boost CS override and call */
135 return (opcode != 0x2e && opcode != 0x9a);
311ac88f
MH
136 }
137}
138
1da177e4
LT
139/*
140 * returns non-zero if opcode modifies the interrupt flag.
141 */
34c37e18 142static int __kprobes is_IF_modifier(kprobe_opcode_t opcode)
1da177e4
LT
143{
144 switch (opcode) {
145 case 0xfa: /* cli */
146 case 0xfb: /* sti */
147 case 0xcf: /* iret/iretd */
148 case 0x9d: /* popf/popfd */
149 return 1;
150 }
151 return 0;
152}
153
3d97ae5b 154int __kprobes arch_prepare_kprobe(struct kprobe *p)
1da177e4 155{
124d90be
PP
156 /* insn: must be on special executable page on i386. */
157 p->ainsn.insn = get_insn_slot();
158 if (!p->ainsn.insn)
159 return -ENOMEM;
160
1da177e4 161 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
7e1048b1 162 p->opcode = *p->addr;
585deaca 163 if (can_boost(p->addr)) {
311ac88f
MH
164 p->ainsn.boostable = 0;
165 } else {
166 p->ainsn.boostable = -1;
167 }
49a2a1b8 168 return 0;
1da177e4
LT
169}
170
3d97ae5b 171void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 172{
19d36ccd 173 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
1da177e4
LT
174}
175
3d97ae5b 176void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4 177{
19d36ccd 178 text_poke(p->addr, &p->opcode, 1);
7e1048b1
RL
179}
180
124d90be
PP
181void __kprobes arch_remove_kprobe(struct kprobe *p)
182{
7a7d1cf9 183 mutex_lock(&kprobe_mutex);
b4c6c34a 184 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
7a7d1cf9 185 mutex_unlock(&kprobe_mutex);
124d90be
PP
186}
187
34c37e18 188static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
417c8da6 189{
9a0e3a86
AM
190 kcb->prev_kprobe.kp = kprobe_running();
191 kcb->prev_kprobe.status = kcb->kprobe_status;
192 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
193 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
417c8da6
PP
194}
195
34c37e18 196static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
417c8da6 197{
9a0e3a86
AM
198 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
199 kcb->kprobe_status = kcb->prev_kprobe.status;
200 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
201 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
417c8da6
PP
202}
203
34c37e18 204static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
9a0e3a86 205 struct kprobe_ctlblk *kcb)
417c8da6 206{
9a0e3a86
AM
207 __get_cpu_var(current_kprobe) = p;
208 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
417c8da6
PP
209 = (regs->eflags & (TF_MASK | IF_MASK));
210 if (is_IF_modifier(p->opcode))
9a0e3a86 211 kcb->kprobe_saved_eflags &= ~IF_MASK;
417c8da6
PP
212}
213
34c37e18 214static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
215{
216 regs->eflags |= TF_MASK;
217 regs->eflags &= ~IF_MASK;
218 /*single step inline if the instruction is an int3*/
219 if (p->opcode == BREAKPOINT_INSTRUCTION)
220 regs->eip = (unsigned long)p->addr;
221 else
124d90be 222 regs->eip = (unsigned long)p->ainsn.insn;
1da177e4
LT
223}
224
991a51d8 225/* Called with kretprobe_lock held */
4c4308cb 226void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
3d97ae5b 227 struct pt_regs *regs)
b94cce92
HN
228{
229 unsigned long *sara = (unsigned long *)&regs->esp;
4bdbd37f 230
4c4308cb 231 ri->ret_addr = (kprobe_opcode_t *) *sara;
62c27be0 232
4c4308cb
CH
233 /* Replace the return addr with trampoline addr */
234 *sara = (unsigned long) &kretprobe_trampoline;
b94cce92
HN
235}
236
1da177e4
LT
237/*
238 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
239 * remain disabled thorough out this function.
240 */
3d97ae5b 241static int __kprobes kprobe_handler(struct pt_regs *regs)
1da177e4
LT
242{
243 struct kprobe *p;
244 int ret = 0;
2326c770 245 kprobe_opcode_t *addr;
d217d545
AM
246 struct kprobe_ctlblk *kcb;
247
2326c770 248 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
249
d217d545
AM
250 /*
251 * We don't want to be preempted for the entire
252 * duration of kprobe processing
253 */
254 preempt_disable();
255 kcb = get_kprobe_ctlblk();
1da177e4 256
1da177e4
LT
257 /* Check we're not actually recursing */
258 if (kprobe_running()) {
1da177e4
LT
259 p = get_kprobe(addr);
260 if (p) {
9a0e3a86 261 if (kcb->kprobe_status == KPROBE_HIT_SS &&
deac66ae 262 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
1da177e4 263 regs->eflags &= ~TF_MASK;
9a0e3a86 264 regs->eflags |= kcb->kprobe_saved_eflags;
1da177e4
LT
265 goto no_kprobe;
266 }
417c8da6
PP
267 /* We have reentered the kprobe_handler(), since
268 * another probe was hit while within the handler.
269 * We here save the original kprobes variables and
270 * just single step on the instruction of the new probe
271 * without calling any user handlers.
272 */
9a0e3a86
AM
273 save_previous_kprobe(kcb);
274 set_current_kprobe(p, regs, kcb);
bf8d5c52 275 kprobes_inc_nmissed_count(p);
417c8da6 276 prepare_singlestep(p, regs);
9a0e3a86 277 kcb->kprobe_status = KPROBE_REENTER;
417c8da6 278 return 1;
1da177e4 279 } else {
eb3a7292
KA
280 if (*addr != BREAKPOINT_INSTRUCTION) {
281 /* The breakpoint instruction was removed by
282 * another cpu right after we hit, no further
283 * handling of this interrupt is appropriate
284 */
285 regs->eip -= sizeof(kprobe_opcode_t);
286 ret = 1;
287 goto no_kprobe;
288 }
9a0e3a86 289 p = __get_cpu_var(current_kprobe);
1da177e4
LT
290 if (p->break_handler && p->break_handler(p, regs)) {
291 goto ss_probe;
292 }
293 }
1da177e4
LT
294 goto no_kprobe;
295 }
296
1da177e4
LT
297 p = get_kprobe(addr);
298 if (!p) {
1da177e4
LT
299 if (*addr != BREAKPOINT_INSTRUCTION) {
300 /*
301 * The breakpoint instruction was removed right
302 * after we hit it. Another cpu has removed
303 * either a probepoint or a debugger breakpoint
304 * at this address. In either case, no further
305 * handling of this interrupt is appropriate.
bce06494
JK
306 * Back up over the (now missing) int3 and run
307 * the original instruction.
1da177e4 308 */
bce06494 309 regs->eip -= sizeof(kprobe_opcode_t);
1da177e4
LT
310 ret = 1;
311 }
312 /* Not one of ours: let kernel handle it */
313 goto no_kprobe;
314 }
315
9a0e3a86
AM
316 set_current_kprobe(p, regs, kcb);
317 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1da177e4
LT
318
319 if (p->pre_handler && p->pre_handler(p, regs))
320 /* handler has already set things up, so skip ss setup */
321 return 1;
322
36721656 323ss_probe:
b4c6c34a 324#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
c39df470 325 if (p->ainsn.boostable == 1 && !p->post_handler){
311ac88f
MH
326 /* Boost up -- we can execute copied instructions directly */
327 reset_current_kprobe();
328 regs->eip = (unsigned long)p->ainsn.insn;
329 preempt_enable_no_resched();
330 return 1;
331 }
c39df470 332#endif
1da177e4 333 prepare_singlestep(p, regs);
9a0e3a86 334 kcb->kprobe_status = KPROBE_HIT_SS;
1da177e4
LT
335 return 1;
336
337no_kprobe:
d217d545 338 preempt_enable_no_resched();
1da177e4
LT
339 return ret;
340}
341
b94cce92
HN
342/*
343 * For function-return probes, init_kprobes() establishes a probepoint
344 * here. When a retprobed function returns, this probe is hit and
345 * trampoline_probe_handler() runs, calling the kretprobe's handler.
346 */
c9becf58 347 void __kprobes kretprobe_trampoline_holder(void)
b94cce92 348 {
c9becf58 349 asm volatile ( ".global kretprobe_trampoline\n"
62c27be0 350 "kretprobe_trampoline: \n"
c9becf58 351 " pushf\n"
8bdc052e
MH
352 /* skip cs, eip, orig_eax */
353 " subl $12, %esp\n"
464d1a78 354 " pushl %fs\n"
8bdc052e
MH
355 " pushl %ds\n"
356 " pushl %es\n"
c9becf58
MH
357 " pushl %eax\n"
358 " pushl %ebp\n"
359 " pushl %edi\n"
360 " pushl %esi\n"
361 " pushl %edx\n"
362 " pushl %ecx\n"
363 " pushl %ebx\n"
364 " movl %esp, %eax\n"
365 " call trampoline_handler\n"
366 /* move eflags to cs */
8bdc052e
MH
367 " movl 52(%esp), %edx\n"
368 " movl %edx, 48(%esp)\n"
c9becf58 369 /* save true return address on eflags */
8bdc052e 370 " movl %eax, 52(%esp)\n"
c9becf58
MH
371 " popl %ebx\n"
372 " popl %ecx\n"
373 " popl %edx\n"
374 " popl %esi\n"
375 " popl %edi\n"
376 " popl %ebp\n"
377 " popl %eax\n"
464d1a78 378 /* skip eip, orig_eax, es, ds, fs */
8bdc052e 379 " addl $20, %esp\n"
c9becf58
MH
380 " popf\n"
381 " ret\n");
382}
b94cce92
HN
383
384/*
c9becf58 385 * Called from kretprobe_trampoline
b94cce92 386 */
c9becf58 387fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
b94cce92 388{
62c27be0 389 struct kretprobe_instance *ri = NULL;
99219a3f 390 struct hlist_head *head, empty_rp;
62c27be0 391 struct hlist_node *node, *tmp;
991a51d8 392 unsigned long flags, orig_ret_address = 0;
4bdbd37f 393 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
b94cce92 394
99219a3f 395 INIT_HLIST_HEAD(&empty_rp);
991a51d8 396 spin_lock_irqsave(&kretprobe_lock, flags);
62c27be0 397 head = kretprobe_inst_table_head(current);
8bdc052e 398 /* fixup registers */
ac3b6faf 399 regs->xcs = __KERNEL_CS | get_kernel_rpl();
8bdc052e
MH
400 regs->eip = trampoline_address;
401 regs->orig_eax = 0xffffffff;
b94cce92 402
4bdbd37f
RL
403 /*
404 * It is possible to have multiple instances associated with a given
405 * task either because an multiple functions in the call path
406 * have a return probe installed on them, and/or more then one return
407 * return probe was registered for a target function.
408 *
409 * We can handle this because:
410 * - instances are always inserted at the head of the list
411 * - when multiple return probes are registered for the same
62c27be0 412 * function, the first instance's ret_addr will point to the
4bdbd37f
RL
413 * real return address, and all the rest will point to
414 * kretprobe_trampoline
415 */
416 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
62c27be0 417 if (ri->task != current)
4bdbd37f 418 /* another task is sharing our hash bucket */
62c27be0 419 continue;
4bdbd37f 420
c9becf58
MH
421 if (ri->rp && ri->rp->handler){
422 __get_cpu_var(current_kprobe) = &ri->rp->kp;
8bdc052e 423 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
4bdbd37f 424 ri->rp->handler(ri, regs);
c9becf58
MH
425 __get_cpu_var(current_kprobe) = NULL;
426 }
4bdbd37f
RL
427
428 orig_ret_address = (unsigned long)ri->ret_addr;
99219a3f 429 recycle_rp_inst(ri, &empty_rp);
4bdbd37f
RL
430
431 if (orig_ret_address != trampoline_address)
432 /*
433 * This is the real return address. Any other
434 * instances associated with this task are for
435 * other calls deeper on the call stack
436 */
437 break;
b94cce92 438 }
4bdbd37f 439
0f95b7fc 440 kretprobe_assert(ri, orig_ret_address, trampoline_address);
991a51d8 441 spin_unlock_irqrestore(&kretprobe_lock, flags);
4bdbd37f 442
99219a3f 443 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
444 hlist_del(&ri->hlist);
445 kfree(ri);
446 }
c9becf58 447 return (void*)orig_ret_address;
b94cce92
HN
448}
449
1da177e4
LT
450/*
451 * Called after single-stepping. p->addr is the address of the
452 * instruction whose first byte has been replaced by the "int 3"
453 * instruction. To avoid the SMP problems that can occur when we
454 * temporarily put back the original opcode to single-step, we
455 * single-stepped a copy of the instruction. The address of this
456 * copy is p->ainsn.insn.
457 *
458 * This function prepares to return from the post-single-step
459 * interrupt. We have to fix up the stack as follows:
460 *
461 * 0) Except in the case of absolute or indirect jump or call instructions,
462 * the new eip is relative to the copied instruction. We need to make
463 * it relative to the original instruction.
464 *
465 * 1) If the single-stepped instruction was pushfl, then the TF and IF
466 * flags are set in the just-pushed eflags, and may need to be cleared.
467 *
468 * 2) If the single-stepped instruction was a call, the return address
469 * that is atop the stack is the address following the copied instruction.
470 * We need to make it the address following the original instruction.
311ac88f
MH
471 *
472 * This function also checks instruction size for preparing direct execution.
1da177e4 473 */
9a0e3a86
AM
474static void __kprobes resume_execution(struct kprobe *p,
475 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
1da177e4
LT
476{
477 unsigned long *tos = (unsigned long *)&regs->esp;
124d90be 478 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
1da177e4
LT
479 unsigned long orig_eip = (unsigned long)p->addr;
480
b50ea74c 481 regs->eflags &= ~TF_MASK;
1da177e4
LT
482 switch (p->ainsn.insn[0]) {
483 case 0x9c: /* pushfl */
484 *tos &= ~(TF_MASK | IF_MASK);
9a0e3a86 485 *tos |= kcb->kprobe_old_eflags;
1da177e4 486 break;
bcff5cd6
MH
487 case 0xc2: /* iret/ret/lret */
488 case 0xc3:
0b9e2cac 489 case 0xca:
bcff5cd6
MH
490 case 0xcb:
491 case 0xcf:
b50ea74c
MH
492 case 0xea: /* jmp absolute -- eip is correct */
493 /* eip is already adjusted, no more changes required */
311ac88f 494 p->ainsn.boostable = 1;
b50ea74c 495 goto no_change;
1da177e4
LT
496 case 0xe8: /* call relative - Fix return addr */
497 *tos = orig_eip + (*tos - copy_eip);
498 break;
bcff5cd6
MH
499 case 0x9a: /* call absolute -- same as call absolute, indirect */
500 *tos = orig_eip + (*tos - copy_eip);
501 goto no_change;
1da177e4
LT
502 case 0xff:
503 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
311ac88f 504 /*
bcff5cd6 505 * call absolute, indirect
311ac88f
MH
506 * Fix return addr; eip is correct.
507 * But this is not boostable
508 */
1da177e4 509 *tos = orig_eip + (*tos - copy_eip);
b50ea74c 510 goto no_change;
1da177e4
LT
511 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
512 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
311ac88f
MH
513 /* eip is correct. And this is boostable */
514 p->ainsn.boostable = 1;
b50ea74c 515 goto no_change;
1da177e4 516 }
1da177e4
LT
517 default:
518 break;
519 }
520
311ac88f
MH
521 if (p->ainsn.boostable == 0) {
522 if ((regs->eip > copy_eip) &&
523 (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
524 /*
525 * These instructions can be executed directly if it
526 * jumps back to correct address.
527 */
528 set_jmp_op((void *)regs->eip,
529 (void *)orig_eip + (regs->eip - copy_eip));
530 p->ainsn.boostable = 1;
531 } else {
532 p->ainsn.boostable = -1;
533 }
534 }
535
b50ea74c
MH
536 regs->eip = orig_eip + (regs->eip - copy_eip);
537
538no_change:
539 return;
1da177e4
LT
540}
541
542/*
543 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
991a51d8 544 * remain disabled thoroughout this function.
1da177e4 545 */
34c37e18 546static int __kprobes post_kprobe_handler(struct pt_regs *regs)
1da177e4 547{
9a0e3a86
AM
548 struct kprobe *cur = kprobe_running();
549 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
550
551 if (!cur)
1da177e4
LT
552 return 0;
553
9a0e3a86
AM
554 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
555 kcb->kprobe_status = KPROBE_HIT_SSDONE;
556 cur->post_handler(cur, regs, 0);
417c8da6 557 }
1da177e4 558
9a0e3a86
AM
559 resume_execution(cur, regs, kcb);
560 regs->eflags |= kcb->kprobe_saved_eflags;
1da177e4 561
417c8da6 562 /*Restore back the original saved kprobes variables and continue. */
9a0e3a86
AM
563 if (kcb->kprobe_status == KPROBE_REENTER) {
564 restore_previous_kprobe(kcb);
417c8da6
PP
565 goto out;
566 }
9a0e3a86 567 reset_current_kprobe();
417c8da6 568out:
1da177e4
LT
569 preempt_enable_no_resched();
570
571 /*
572 * if somebody else is singlestepping across a probe point, eflags
573 * will have TF set, in which case, continue the remaining processing
574 * of do_debug, as if this is not a probe hit.
575 */
576 if (regs->eflags & TF_MASK)
577 return 0;
578
579 return 1;
580}
581
34c37e18 582static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
1da177e4 583{
9a0e3a86
AM
584 struct kprobe *cur = kprobe_running();
585 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
586
b4026513
PP
587 switch(kcb->kprobe_status) {
588 case KPROBE_HIT_SS:
589 case KPROBE_REENTER:
590 /*
591 * We are here because the instruction being single
592 * stepped caused a page fault. We reset the current
593 * kprobe and the eip points back to the probe address
594 * and allow the page fault handler to continue as a
595 * normal page fault.
596 */
597 regs->eip = (unsigned long)cur->addr;
9a0e3a86 598 regs->eflags |= kcb->kprobe_old_eflags;
b4026513
PP
599 if (kcb->kprobe_status == KPROBE_REENTER)
600 restore_previous_kprobe(kcb);
601 else
602 reset_current_kprobe();
1da177e4 603 preempt_enable_no_resched();
b4026513
PP
604 break;
605 case KPROBE_HIT_ACTIVE:
606 case KPROBE_HIT_SSDONE:
607 /*
608 * We increment the nmissed count for accounting,
609 * we can also use npre/npostfault count for accouting
610 * these specific fault cases.
611 */
612 kprobes_inc_nmissed_count(cur);
613
614 /*
615 * We come here because instructions in the pre/post
616 * handler caused the page_fault, this could happen
617 * if handler tries to access user space by
618 * copy_from_user(), get_user() etc. Let the
619 * user-specified handler try to fix it first.
620 */
621 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
622 return 1;
623
624 /*
625 * In case the user-specified fault handler returned
626 * zero, try to fix up.
627 */
628 if (fixup_exception(regs))
629 return 1;
630
631 /*
632 * fixup_exception() could not handle it,
633 * Let do_page_fault() fix it.
634 */
635 break;
636 default:
637 break;
1da177e4
LT
638 }
639 return 0;
640}
641
642/*
643 * Wrapper routine to for handling exceptions.
644 */
3d97ae5b
PP
645int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
646 unsigned long val, void *data)
1da177e4
LT
647{
648 struct die_args *args = (struct die_args *)data;
66ff2d06
AM
649 int ret = NOTIFY_DONE;
650
64445416 651 if (args->regs && user_mode_vm(args->regs))
2326c770 652 return ret;
653
1da177e4
LT
654 switch (val) {
655 case DIE_INT3:
656 if (kprobe_handler(args->regs))
66ff2d06 657 ret = NOTIFY_STOP;
1da177e4
LT
658 break;
659 case DIE_DEBUG:
660 if (post_kprobe_handler(args->regs))
66ff2d06 661 ret = NOTIFY_STOP;
1da177e4
LT
662 break;
663 case DIE_GPF:
1da177e4 664 case DIE_PAGE_FAULT:
d217d545
AM
665 /* kprobe_running() needs smp_processor_id() */
666 preempt_disable();
1da177e4
LT
667 if (kprobe_running() &&
668 kprobe_fault_handler(args->regs, args->trapnr))
66ff2d06 669 ret = NOTIFY_STOP;
d217d545 670 preempt_enable();
1da177e4
LT
671 break;
672 default:
673 break;
674 }
66ff2d06 675 return ret;
1da177e4
LT
676}
677
3d97ae5b 678int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
679{
680 struct jprobe *jp = container_of(p, struct jprobe, kp);
681 unsigned long addr;
9a0e3a86 682 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 683
9a0e3a86
AM
684 kcb->jprobe_saved_regs = *regs;
685 kcb->jprobe_saved_esp = &regs->esp;
686 addr = (unsigned long)(kcb->jprobe_saved_esp);
1da177e4
LT
687
688 /*
689 * TBD: As Linus pointed out, gcc assumes that the callee
690 * owns the argument space and could overwrite it, e.g.
691 * tailcall optimization. So, to be absolutely safe
692 * we also save and restore enough stack bytes to cover
693 * the argument area.
694 */
9a0e3a86
AM
695 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
696 MIN_STACK_SIZE(addr));
1da177e4
LT
697 regs->eflags &= ~IF_MASK;
698 regs->eip = (unsigned long)(jp->entry);
699 return 1;
700}
701
3d97ae5b 702void __kprobes jprobe_return(void)
1da177e4 703{
9a0e3a86
AM
704 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
705
1da177e4
LT
706 asm volatile (" xchgl %%ebx,%%esp \n"
707 " int3 \n"
708 " .globl jprobe_return_end \n"
709 " jprobe_return_end: \n"
710 " nop \n"::"b"
9a0e3a86 711 (kcb->jprobe_saved_esp):"memory");
1da177e4
LT
712}
713
3d97ae5b 714int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4 715{
9a0e3a86 716 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 717 u8 *addr = (u8 *) (regs->eip - 1);
9a0e3a86 718 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
1da177e4
LT
719 struct jprobe *jp = container_of(p, struct jprobe, kp);
720
721 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
9a0e3a86 722 if (&regs->esp != kcb->jprobe_saved_esp) {
1da177e4 723 struct pt_regs *saved_regs =
9a0e3a86
AM
724 container_of(kcb->jprobe_saved_esp,
725 struct pt_regs, esp);
1da177e4 726 printk("current esp %p does not match saved esp %p\n",
9a0e3a86 727 &regs->esp, kcb->jprobe_saved_esp);
1da177e4
LT
728 printk("Saved registers for jprobe %p\n", jp);
729 show_registers(saved_regs);
730 printk("Current registers\n");
731 show_registers(regs);
732 BUG();
733 }
9a0e3a86
AM
734 *regs = kcb->jprobe_saved_regs;
735 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
1da177e4 736 MIN_STACK_SIZE(stack_addr));
d217d545 737 preempt_enable_no_resched();
1da177e4
LT
738 return 1;
739 }
740 return 0;
741}
4bdbd37f 742
bf8f6e5b
AM
743int __kprobes arch_trampoline_kprobe(struct kprobe *p)
744{
745 return 0;
746}
747
6772926b 748int __init arch_init_kprobes(void)
4bdbd37f 749{
c9becf58 750 return 0;
4bdbd37f 751}
This page took 0.345738 seconds and 5 git commands to generate.