[PATCH] kprobe cleanup for VM_MASK judgement
[deliverable/linux.git] / arch / i386 / kernel / kprobes.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
31#include <linux/config.h>
32#include <linux/kprobes.h>
33#include <linux/ptrace.h>
1da177e4 34#include <linux/preempt.h>
7e1048b1 35#include <asm/cacheflush.h>
1da177e4
LT
36#include <asm/kdebug.h>
37#include <asm/desc.h>
b4026513 38#include <asm/uaccess.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 */
34c37e18 60static __always_inline int can_boost(kprobe_opcode_t opcode)
311ac88f
MH
61{
62 switch (opcode & 0xf0 ) {
63 case 0x70:
64 return 0; /* can't boost conditional jump */
65 case 0x90:
66 /* can't boost call and pushf */
67 return opcode != 0x9a && opcode != 0x9c;
68 case 0xc0:
69 /* can't boost undefined opcodes and soft-interruptions */
70 return (0xc1 < opcode && opcode < 0xc6) ||
71 (0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
72 case 0xd0:
73 /* can boost AA* and XLAT */
74 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
75 case 0xe0:
76 /* can boost in/out and (may be) jmps */
77 return (0xe3 < opcode && opcode != 0xe8);
78 case 0xf0:
79 /* clear and set flags can be boost */
80 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
81 default:
82 /* currently, can't boost 2 bytes opcodes */
83 return opcode != 0x0f;
84 }
85}
86
87
1da177e4
LT
88/*
89 * returns non-zero if opcode modifies the interrupt flag.
90 */
34c37e18 91static int __kprobes is_IF_modifier(kprobe_opcode_t opcode)
1da177e4
LT
92{
93 switch (opcode) {
94 case 0xfa: /* cli */
95 case 0xfb: /* sti */
96 case 0xcf: /* iret/iretd */
97 case 0x9d: /* popf/popfd */
98 return 1;
99 }
100 return 0;
101}
102
3d97ae5b 103int __kprobes arch_prepare_kprobe(struct kprobe *p)
1da177e4 104{
124d90be
PP
105 /* insn: must be on special executable page on i386. */
106 p->ainsn.insn = get_insn_slot();
107 if (!p->ainsn.insn)
108 return -ENOMEM;
109
1da177e4 110 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
7e1048b1 111 p->opcode = *p->addr;
311ac88f
MH
112 if (can_boost(p->opcode)) {
113 p->ainsn.boostable = 0;
114 } else {
115 p->ainsn.boostable = -1;
116 }
49a2a1b8 117 return 0;
1da177e4
LT
118}
119
3d97ae5b 120void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 121{
7e1048b1
RL
122 *p->addr = BREAKPOINT_INSTRUCTION;
123 flush_icache_range((unsigned long) p->addr,
124 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
1da177e4
LT
125}
126
3d97ae5b 127void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4
LT
128{
129 *p->addr = p->opcode;
7e1048b1
RL
130 flush_icache_range((unsigned long) p->addr,
131 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
132}
133
124d90be
PP
134void __kprobes arch_remove_kprobe(struct kprobe *p)
135{
7a7d1cf9 136 mutex_lock(&kprobe_mutex);
124d90be 137 free_insn_slot(p->ainsn.insn);
7a7d1cf9 138 mutex_unlock(&kprobe_mutex);
124d90be
PP
139}
140
34c37e18 141static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
417c8da6 142{
9a0e3a86
AM
143 kcb->prev_kprobe.kp = kprobe_running();
144 kcb->prev_kprobe.status = kcb->kprobe_status;
145 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
146 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
417c8da6
PP
147}
148
34c37e18 149static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
417c8da6 150{
9a0e3a86
AM
151 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
152 kcb->kprobe_status = kcb->prev_kprobe.status;
153 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
154 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
417c8da6
PP
155}
156
34c37e18 157static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
9a0e3a86 158 struct kprobe_ctlblk *kcb)
417c8da6 159{
9a0e3a86
AM
160 __get_cpu_var(current_kprobe) = p;
161 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
417c8da6
PP
162 = (regs->eflags & (TF_MASK | IF_MASK));
163 if (is_IF_modifier(p->opcode))
9a0e3a86 164 kcb->kprobe_saved_eflags &= ~IF_MASK;
417c8da6
PP
165}
166
34c37e18 167static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
168{
169 regs->eflags |= TF_MASK;
170 regs->eflags &= ~IF_MASK;
171 /*single step inline if the instruction is an int3*/
172 if (p->opcode == BREAKPOINT_INSTRUCTION)
173 regs->eip = (unsigned long)p->addr;
174 else
124d90be 175 regs->eip = (unsigned long)p->ainsn.insn;
1da177e4
LT
176}
177
991a51d8 178/* Called with kretprobe_lock held */
3d97ae5b
PP
179void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
180 struct pt_regs *regs)
b94cce92
HN
181{
182 unsigned long *sara = (unsigned long *)&regs->esp;
4bdbd37f
RL
183 struct kretprobe_instance *ri;
184
185 if ((ri = get_free_rp_inst(rp)) != NULL) {
186 ri->rp = rp;
187 ri->task = current;
188 ri->ret_addr = (kprobe_opcode_t *) *sara;
b94cce92 189
b94cce92
HN
190 /* Replace the return addr with trampoline addr */
191 *sara = (unsigned long) &kretprobe_trampoline;
b94cce92 192
4bdbd37f
RL
193 add_rp_inst(ri);
194 } else {
195 rp->nmissed++;
196 }
b94cce92
HN
197}
198
1da177e4
LT
199/*
200 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
201 * remain disabled thorough out this function.
202 */
3d97ae5b 203static int __kprobes kprobe_handler(struct pt_regs *regs)
1da177e4
LT
204{
205 struct kprobe *p;
206 int ret = 0;
2326c770 207 kprobe_opcode_t *addr;
d217d545 208 struct kprobe_ctlblk *kcb;
311ac88f
MH
209#ifdef CONFIG_PREEMPT
210 unsigned pre_preempt_count = preempt_count();
211#endif /* CONFIG_PREEMPT */
d217d545 212
2326c770 213 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
214
d217d545
AM
215 /*
216 * We don't want to be preempted for the entire
217 * duration of kprobe processing
218 */
219 preempt_disable();
220 kcb = get_kprobe_ctlblk();
1da177e4 221
1da177e4
LT
222 /* Check we're not actually recursing */
223 if (kprobe_running()) {
1da177e4
LT
224 p = get_kprobe(addr);
225 if (p) {
9a0e3a86 226 if (kcb->kprobe_status == KPROBE_HIT_SS &&
deac66ae 227 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
1da177e4 228 regs->eflags &= ~TF_MASK;
9a0e3a86 229 regs->eflags |= kcb->kprobe_saved_eflags;
1da177e4
LT
230 goto no_kprobe;
231 }
417c8da6
PP
232 /* We have reentered the kprobe_handler(), since
233 * another probe was hit while within the handler.
234 * We here save the original kprobes variables and
235 * just single step on the instruction of the new probe
236 * without calling any user handlers.
237 */
9a0e3a86
AM
238 save_previous_kprobe(kcb);
239 set_current_kprobe(p, regs, kcb);
bf8d5c52 240 kprobes_inc_nmissed_count(p);
417c8da6 241 prepare_singlestep(p, regs);
9a0e3a86 242 kcb->kprobe_status = KPROBE_REENTER;
417c8da6 243 return 1;
1da177e4 244 } else {
eb3a7292
KA
245 if (*addr != BREAKPOINT_INSTRUCTION) {
246 /* The breakpoint instruction was removed by
247 * another cpu right after we hit, no further
248 * handling of this interrupt is appropriate
249 */
250 regs->eip -= sizeof(kprobe_opcode_t);
251 ret = 1;
252 goto no_kprobe;
253 }
9a0e3a86 254 p = __get_cpu_var(current_kprobe);
1da177e4
LT
255 if (p->break_handler && p->break_handler(p, regs)) {
256 goto ss_probe;
257 }
258 }
1da177e4
LT
259 goto no_kprobe;
260 }
261
1da177e4
LT
262 p = get_kprobe(addr);
263 if (!p) {
1da177e4
LT
264 if (*addr != BREAKPOINT_INSTRUCTION) {
265 /*
266 * The breakpoint instruction was removed right
267 * after we hit it. Another cpu has removed
268 * either a probepoint or a debugger breakpoint
269 * at this address. In either case, no further
270 * handling of this interrupt is appropriate.
bce06494
JK
271 * Back up over the (now missing) int3 and run
272 * the original instruction.
1da177e4 273 */
bce06494 274 regs->eip -= sizeof(kprobe_opcode_t);
1da177e4
LT
275 ret = 1;
276 }
277 /* Not one of ours: let kernel handle it */
278 goto no_kprobe;
279 }
280
9a0e3a86
AM
281 set_current_kprobe(p, regs, kcb);
282 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1da177e4
LT
283
284 if (p->pre_handler && p->pre_handler(p, regs))
285 /* handler has already set things up, so skip ss setup */
286 return 1;
287
311ac88f
MH
288 if (p->ainsn.boostable == 1 &&
289#ifdef CONFIG_PREEMPT
290 !(pre_preempt_count) && /*
291 * This enables booster when the direct
292 * execution path aren't preempted.
293 */
294#endif /* CONFIG_PREEMPT */
295 !p->post_handler && !p->break_handler ) {
296 /* Boost up -- we can execute copied instructions directly */
297 reset_current_kprobe();
298 regs->eip = (unsigned long)p->ainsn.insn;
299 preempt_enable_no_resched();
300 return 1;
301 }
302
1da177e4
LT
303ss_probe:
304 prepare_singlestep(p, regs);
9a0e3a86 305 kcb->kprobe_status = KPROBE_HIT_SS;
1da177e4
LT
306 return 1;
307
308no_kprobe:
d217d545 309 preempt_enable_no_resched();
1da177e4
LT
310 return ret;
311}
312
b94cce92
HN
313/*
314 * For function-return probes, init_kprobes() establishes a probepoint
315 * here. When a retprobed function returns, this probe is hit and
316 * trampoline_probe_handler() runs, calling the kretprobe's handler.
317 */
c9becf58 318 void __kprobes kretprobe_trampoline_holder(void)
b94cce92 319 {
c9becf58 320 asm volatile ( ".global kretprobe_trampoline\n"
b94cce92 321 "kretprobe_trampoline: \n"
c9becf58
MH
322 " pushf\n"
323 /* skip cs, eip, orig_eax, es, ds */
324 " subl $20, %esp\n"
325 " pushl %eax\n"
326 " pushl %ebp\n"
327 " pushl %edi\n"
328 " pushl %esi\n"
329 " pushl %edx\n"
330 " pushl %ecx\n"
331 " pushl %ebx\n"
332 " movl %esp, %eax\n"
333 " call trampoline_handler\n"
334 /* move eflags to cs */
335 " movl 48(%esp), %edx\n"
336 " movl %edx, 44(%esp)\n"
337 /* save true return address on eflags */
338 " movl %eax, 48(%esp)\n"
339 " popl %ebx\n"
340 " popl %ecx\n"
341 " popl %edx\n"
342 " popl %esi\n"
343 " popl %edi\n"
344 " popl %ebp\n"
345 " popl %eax\n"
346 /* skip eip, orig_eax, es, ds */
347 " addl $16, %esp\n"
348 " popf\n"
349 " ret\n");
350}
b94cce92
HN
351
352/*
c9becf58 353 * Called from kretprobe_trampoline
b94cce92 354 */
c9becf58 355fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
b94cce92 356{
4bdbd37f
RL
357 struct kretprobe_instance *ri = NULL;
358 struct hlist_head *head;
359 struct hlist_node *node, *tmp;
991a51d8 360 unsigned long flags, orig_ret_address = 0;
4bdbd37f 361 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
b94cce92 362
991a51d8 363 spin_lock_irqsave(&kretprobe_lock, flags);
4bdbd37f 364 head = kretprobe_inst_table_head(current);
b94cce92 365
4bdbd37f
RL
366 /*
367 * It is possible to have multiple instances associated with a given
368 * task either because an multiple functions in the call path
369 * have a return probe installed on them, and/or more then one return
370 * return probe was registered for a target function.
371 *
372 * We can handle this because:
373 * - instances are always inserted at the head of the list
374 * - when multiple return probes are registered for the same
375 * function, the first instance's ret_addr will point to the
376 * real return address, and all the rest will point to
377 * kretprobe_trampoline
378 */
379 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
380 if (ri->task != current)
381 /* another task is sharing our hash bucket */
382 continue;
383
c9becf58
MH
384 if (ri->rp && ri->rp->handler){
385 __get_cpu_var(current_kprobe) = &ri->rp->kp;
4bdbd37f 386 ri->rp->handler(ri, regs);
c9becf58
MH
387 __get_cpu_var(current_kprobe) = NULL;
388 }
4bdbd37f
RL
389
390 orig_ret_address = (unsigned long)ri->ret_addr;
b94cce92 391 recycle_rp_inst(ri);
4bdbd37f
RL
392
393 if (orig_ret_address != trampoline_address)
394 /*
395 * This is the real return address. Any other
396 * instances associated with this task are for
397 * other calls deeper on the call stack
398 */
399 break;
b94cce92 400 }
4bdbd37f
RL
401
402 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
4bdbd37f 403
991a51d8 404 spin_unlock_irqrestore(&kretprobe_lock, flags);
4bdbd37f 405
c9becf58 406 return (void*)orig_ret_address;
b94cce92
HN
407}
408
1da177e4
LT
409/*
410 * Called after single-stepping. p->addr is the address of the
411 * instruction whose first byte has been replaced by the "int 3"
412 * instruction. To avoid the SMP problems that can occur when we
413 * temporarily put back the original opcode to single-step, we
414 * single-stepped a copy of the instruction. The address of this
415 * copy is p->ainsn.insn.
416 *
417 * This function prepares to return from the post-single-step
418 * interrupt. We have to fix up the stack as follows:
419 *
420 * 0) Except in the case of absolute or indirect jump or call instructions,
421 * the new eip is relative to the copied instruction. We need to make
422 * it relative to the original instruction.
423 *
424 * 1) If the single-stepped instruction was pushfl, then the TF and IF
425 * flags are set in the just-pushed eflags, and may need to be cleared.
426 *
427 * 2) If the single-stepped instruction was a call, the return address
428 * that is atop the stack is the address following the copied instruction.
429 * We need to make it the address following the original instruction.
311ac88f
MH
430 *
431 * This function also checks instruction size for preparing direct execution.
1da177e4 432 */
9a0e3a86
AM
433static void __kprobes resume_execution(struct kprobe *p,
434 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
1da177e4
LT
435{
436 unsigned long *tos = (unsigned long *)&regs->esp;
124d90be 437 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
1da177e4
LT
438 unsigned long orig_eip = (unsigned long)p->addr;
439
b50ea74c 440 regs->eflags &= ~TF_MASK;
1da177e4
LT
441 switch (p->ainsn.insn[0]) {
442 case 0x9c: /* pushfl */
443 *tos &= ~(TF_MASK | IF_MASK);
9a0e3a86 444 *tos |= kcb->kprobe_old_eflags;
1da177e4 445 break;
0b9e2cac
PP
446 case 0xc3: /* ret/lret */
447 case 0xcb:
448 case 0xc2:
449 case 0xca:
b50ea74c
MH
450 case 0xea: /* jmp absolute -- eip is correct */
451 /* eip is already adjusted, no more changes required */
311ac88f 452 p->ainsn.boostable = 1;
b50ea74c 453 goto no_change;
1da177e4
LT
454 case 0xe8: /* call relative - Fix return addr */
455 *tos = orig_eip + (*tos - copy_eip);
456 break;
457 case 0xff:
458 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
459 /* call absolute, indirect */
311ac88f
MH
460 /*
461 * Fix return addr; eip is correct.
462 * But this is not boostable
463 */
1da177e4 464 *tos = orig_eip + (*tos - copy_eip);
b50ea74c 465 goto no_change;
1da177e4
LT
466 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
467 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
311ac88f
MH
468 /* eip is correct. And this is boostable */
469 p->ainsn.boostable = 1;
b50ea74c 470 goto no_change;
1da177e4 471 }
1da177e4
LT
472 default:
473 break;
474 }
475
311ac88f
MH
476 if (p->ainsn.boostable == 0) {
477 if ((regs->eip > copy_eip) &&
478 (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
479 /*
480 * These instructions can be executed directly if it
481 * jumps back to correct address.
482 */
483 set_jmp_op((void *)regs->eip,
484 (void *)orig_eip + (regs->eip - copy_eip));
485 p->ainsn.boostable = 1;
486 } else {
487 p->ainsn.boostable = -1;
488 }
489 }
490
b50ea74c
MH
491 regs->eip = orig_eip + (regs->eip - copy_eip);
492
493no_change:
494 return;
1da177e4
LT
495}
496
497/*
498 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
991a51d8 499 * remain disabled thoroughout this function.
1da177e4 500 */
34c37e18 501static int __kprobes post_kprobe_handler(struct pt_regs *regs)
1da177e4 502{
9a0e3a86
AM
503 struct kprobe *cur = kprobe_running();
504 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
505
506 if (!cur)
1da177e4
LT
507 return 0;
508
9a0e3a86
AM
509 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
510 kcb->kprobe_status = KPROBE_HIT_SSDONE;
511 cur->post_handler(cur, regs, 0);
417c8da6 512 }
1da177e4 513
9a0e3a86
AM
514 resume_execution(cur, regs, kcb);
515 regs->eflags |= kcb->kprobe_saved_eflags;
1da177e4 516
417c8da6 517 /*Restore back the original saved kprobes variables and continue. */
9a0e3a86
AM
518 if (kcb->kprobe_status == KPROBE_REENTER) {
519 restore_previous_kprobe(kcb);
417c8da6
PP
520 goto out;
521 }
9a0e3a86 522 reset_current_kprobe();
417c8da6 523out:
1da177e4
LT
524 preempt_enable_no_resched();
525
526 /*
527 * if somebody else is singlestepping across a probe point, eflags
528 * will have TF set, in which case, continue the remaining processing
529 * of do_debug, as if this is not a probe hit.
530 */
531 if (regs->eflags & TF_MASK)
532 return 0;
533
534 return 1;
535}
536
34c37e18 537static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
1da177e4 538{
9a0e3a86
AM
539 struct kprobe *cur = kprobe_running();
540 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
541
b4026513
PP
542 switch(kcb->kprobe_status) {
543 case KPROBE_HIT_SS:
544 case KPROBE_REENTER:
545 /*
546 * We are here because the instruction being single
547 * stepped caused a page fault. We reset the current
548 * kprobe and the eip points back to the probe address
549 * and allow the page fault handler to continue as a
550 * normal page fault.
551 */
552 regs->eip = (unsigned long)cur->addr;
9a0e3a86 553 regs->eflags |= kcb->kprobe_old_eflags;
b4026513
PP
554 if (kcb->kprobe_status == KPROBE_REENTER)
555 restore_previous_kprobe(kcb);
556 else
557 reset_current_kprobe();
1da177e4 558 preempt_enable_no_resched();
b4026513
PP
559 break;
560 case KPROBE_HIT_ACTIVE:
561 case KPROBE_HIT_SSDONE:
562 /*
563 * We increment the nmissed count for accounting,
564 * we can also use npre/npostfault count for accouting
565 * these specific fault cases.
566 */
567 kprobes_inc_nmissed_count(cur);
568
569 /*
570 * We come here because instructions in the pre/post
571 * handler caused the page_fault, this could happen
572 * if handler tries to access user space by
573 * copy_from_user(), get_user() etc. Let the
574 * user-specified handler try to fix it first.
575 */
576 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
577 return 1;
578
579 /*
580 * In case the user-specified fault handler returned
581 * zero, try to fix up.
582 */
583 if (fixup_exception(regs))
584 return 1;
585
586 /*
587 * fixup_exception() could not handle it,
588 * Let do_page_fault() fix it.
589 */
590 break;
591 default:
592 break;
1da177e4
LT
593 }
594 return 0;
595}
596
597/*
598 * Wrapper routine to for handling exceptions.
599 */
3d97ae5b
PP
600int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
601 unsigned long val, void *data)
1da177e4
LT
602{
603 struct die_args *args = (struct die_args *)data;
66ff2d06
AM
604 int ret = NOTIFY_DONE;
605
2326c770 606 if (args->regs && user_mode(args->regs))
607 return ret;
608
1da177e4
LT
609 switch (val) {
610 case DIE_INT3:
611 if (kprobe_handler(args->regs))
66ff2d06 612 ret = NOTIFY_STOP;
1da177e4
LT
613 break;
614 case DIE_DEBUG:
615 if (post_kprobe_handler(args->regs))
66ff2d06 616 ret = NOTIFY_STOP;
1da177e4
LT
617 break;
618 case DIE_GPF:
1da177e4 619 case DIE_PAGE_FAULT:
d217d545
AM
620 /* kprobe_running() needs smp_processor_id() */
621 preempt_disable();
1da177e4
LT
622 if (kprobe_running() &&
623 kprobe_fault_handler(args->regs, args->trapnr))
66ff2d06 624 ret = NOTIFY_STOP;
d217d545 625 preempt_enable();
1da177e4
LT
626 break;
627 default:
628 break;
629 }
66ff2d06 630 return ret;
1da177e4
LT
631}
632
3d97ae5b 633int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
634{
635 struct jprobe *jp = container_of(p, struct jprobe, kp);
636 unsigned long addr;
9a0e3a86 637 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 638
9a0e3a86
AM
639 kcb->jprobe_saved_regs = *regs;
640 kcb->jprobe_saved_esp = &regs->esp;
641 addr = (unsigned long)(kcb->jprobe_saved_esp);
1da177e4
LT
642
643 /*
644 * TBD: As Linus pointed out, gcc assumes that the callee
645 * owns the argument space and could overwrite it, e.g.
646 * tailcall optimization. So, to be absolutely safe
647 * we also save and restore enough stack bytes to cover
648 * the argument area.
649 */
9a0e3a86
AM
650 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
651 MIN_STACK_SIZE(addr));
1da177e4
LT
652 regs->eflags &= ~IF_MASK;
653 regs->eip = (unsigned long)(jp->entry);
654 return 1;
655}
656
3d97ae5b 657void __kprobes jprobe_return(void)
1da177e4 658{
9a0e3a86
AM
659 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
660
1da177e4
LT
661 asm volatile (" xchgl %%ebx,%%esp \n"
662 " int3 \n"
663 " .globl jprobe_return_end \n"
664 " jprobe_return_end: \n"
665 " nop \n"::"b"
9a0e3a86 666 (kcb->jprobe_saved_esp):"memory");
1da177e4
LT
667}
668
3d97ae5b 669int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4 670{
9a0e3a86 671 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 672 u8 *addr = (u8 *) (regs->eip - 1);
9a0e3a86 673 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
1da177e4
LT
674 struct jprobe *jp = container_of(p, struct jprobe, kp);
675
676 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
9a0e3a86 677 if (&regs->esp != kcb->jprobe_saved_esp) {
1da177e4 678 struct pt_regs *saved_regs =
9a0e3a86
AM
679 container_of(kcb->jprobe_saved_esp,
680 struct pt_regs, esp);
1da177e4 681 printk("current esp %p does not match saved esp %p\n",
9a0e3a86 682 &regs->esp, kcb->jprobe_saved_esp);
1da177e4
LT
683 printk("Saved registers for jprobe %p\n", jp);
684 show_registers(saved_regs);
685 printk("Current registers\n");
686 show_registers(regs);
687 BUG();
688 }
9a0e3a86
AM
689 *regs = kcb->jprobe_saved_regs;
690 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
1da177e4 691 MIN_STACK_SIZE(stack_addr));
d217d545 692 preempt_enable_no_resched();
1da177e4
LT
693 return 1;
694 }
695 return 0;
696}
4bdbd37f 697
6772926b 698int __init arch_init_kprobes(void)
4bdbd37f 699{
c9becf58 700 return 0;
4bdbd37f 701}
This page took 0.166092 seconds and 5 git commands to generate.