2 * Kernel Probes (KProbes)
3 * arch/ia64/kernel/kprobes.c
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.
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.
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.
19 * Copyright (C) IBM Corporation, 2002, 2004
20 * Copyright (C) Intel Corporation, 2005
22 * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy
23 * <anil.s.keshavamurthy@intel.com> adapted from i386
26 #include <linux/kprobes.h>
27 #include <linux/ptrace.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
30 #include <linux/preempt.h>
31 #include <linux/moduleloader.h>
32 #include <linux/kdebug.h>
34 #include <asm/pgtable.h>
35 #include <asm/sections.h>
36 #include <asm/uaccess.h>
38 extern void jprobe_inst_return(void);
40 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
41 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
43 struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
45 enum instruction_type {A, I, M, F, B, L, X, u};
46 static enum instruction_type bundle_encoding[32][3] = {
81 /* Insert a long branch code */
82 static void __kprobes set_brl_inst(void *from, void *to)
84 s64 rel = ((s64) to - (s64) from) >> 4;
86 brl = (bundle_t *) ((u64) from & ~0xf);
87 brl->quad0.template = 0x05; /* [MLX](stop) */
88 brl->quad0.slot0 = NOP_M_INST; /* nop.m 0x0 */
89 brl->quad0.slot1_p0 = ((rel >> 20) & 0x7fffffffff) << 2;
90 brl->quad1.slot1_p1 = (((rel >> 20) & 0x7fffffffff) << 2) >> (64 - 46);
91 /* brl.cond.sptk.many.clr rel<<4 (qp=0) */
92 brl->quad1.slot2 = BRL_INST(rel >> 59, rel & 0xfffff);
96 * In this function we check to see if the instruction
97 * is IP relative instruction and update the kprobe
98 * inst flag accordingly
100 static void __kprobes update_kprobe_inst_flag(uint template, uint slot,
102 unsigned long kprobe_inst,
105 p->ainsn.inst_flag = 0;
106 p->ainsn.target_br_reg = 0;
107 p->ainsn.slot = slot;
109 /* Check for Break instruction
110 * Bits 37:40 Major opcode to be zero
111 * Bits 27:32 X6 to be zero
112 * Bits 32:35 X3 to be zero
114 if ((!major_opcode) && (!((kprobe_inst >> 27) & 0x1FF)) ) {
115 /* is a break instruction */
116 p->ainsn.inst_flag |= INST_FLAG_BREAK_INST;
120 if (bundle_encoding[template][slot] == B) {
121 switch (major_opcode) {
122 case INDIRECT_CALL_OPCODE:
123 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
124 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
126 case IP_RELATIVE_PREDICT_OPCODE:
127 case IP_RELATIVE_BRANCH_OPCODE:
128 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
130 case IP_RELATIVE_CALL_OPCODE:
131 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
132 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
133 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
136 } else if (bundle_encoding[template][slot] == X) {
137 switch (major_opcode) {
138 case LONG_CALL_OPCODE:
139 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
140 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
148 * In this function we check to see if the instruction
149 * (qp) cmpx.crel.ctype p1,p2=r2,r3
150 * on which we are inserting kprobe is cmp instruction
153 static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
155 unsigned long kprobe_inst)
160 if (!((bundle_encoding[template][slot] == I) ||
161 (bundle_encoding[template][slot] == M)))
164 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
165 (major_opcode == 0xE)))
168 cmp_inst.l = kprobe_inst;
169 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
170 /* Integer compare - Register Register (A6 type)*/
171 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
172 &&(cmp_inst.f.c == 1))
174 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
175 /* Integer compare - Immediate Register (A8 type)*/
176 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
184 * In this function we check to see if the instruction
185 * on which we are inserting kprobe is supported.
186 * Returns qp value if supported
187 * Returns -EINVAL if unsupported
189 static int __kprobes unsupported_inst(uint template, uint slot,
191 unsigned long kprobe_inst,
196 qp = kprobe_inst & 0x3f;
197 if (is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst)) {
198 if (slot == 1 && qp) {
199 printk(KERN_WARNING "Kprobes on cmp unc "
200 "instruction on slot 1 at <0x%lx> "
201 "is not supported\n", addr);
207 else if (bundle_encoding[template][slot] == I) {
208 if (major_opcode == 0) {
210 * Check for Integer speculation instruction
211 * - Bit 33-35 to be equal to 0x1
213 if (((kprobe_inst >> 33) & 0x7) == 1) {
215 "Kprobes on speculation inst at <0x%lx> not supported\n",
220 * IP relative mov instruction
221 * - Bit 27-35 to be equal to 0x30
223 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
225 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
231 else if ((major_opcode == 5) && !(kprobe_inst & (0xFUl << 33)) &&
232 (kprobe_inst & (0x1UL << 12))) {
233 /* test bit instructions, tbit,tnat,tf
234 * bit 33-36 to be equal to 0
235 * bit 12 to be equal to 1
237 if (slot == 1 && qp) {
238 printk(KERN_WARNING "Kprobes on test bit "
239 "instruction on slot at <0x%lx> "
240 "is not supported\n", addr);
246 else if (bundle_encoding[template][slot] == B) {
247 if (major_opcode == 7) {
248 /* IP-Relative Predict major code is 7 */
249 printk(KERN_WARNING "Kprobes on IP-Relative"
250 "Predict is not supported\n");
253 else if (major_opcode == 2) {
254 /* Indirect Predict, major code is 2
255 * bit 27-32 to be equal to 10 or 11
257 int x6=(kprobe_inst >> 27) & 0x3F;
258 if ((x6 == 0x10) || (x6 == 0x11)) {
259 printk(KERN_WARNING "Kprobes on "
260 "Indirect Predict is not supported\n");
265 /* kernel does not use float instruction, here for safety kprobe
266 * will judge whether it is fcmp/flass/float approximation instruction
268 else if (unlikely(bundle_encoding[template][slot] == F)) {
269 if ((major_opcode == 4 || major_opcode == 5) &&
270 (kprobe_inst & (0x1 << 12))) {
271 /* fcmp/fclass unc instruction */
272 if (slot == 1 && qp) {
273 printk(KERN_WARNING "Kprobes on fcmp/fclass "
274 "instruction on slot at <0x%lx> "
275 "is not supported\n", addr);
281 if ((major_opcode == 0 || major_opcode == 1) &&
282 (kprobe_inst & (0x1UL << 33))) {
283 /* float Approximation instruction */
284 if (slot == 1 && qp) {
285 printk(KERN_WARNING "Kprobes on float Approx "
286 "instr at <0x%lx> is not supported\n",
297 * In this function we override the bundle with
298 * the break instruction at the given slot.
300 static void __kprobes prepare_break_inst(uint template, uint slot,
302 unsigned long kprobe_inst,
306 unsigned long break_inst = BREAK_INST;
307 bundle_t *bundle = &p->opcode.bundle;
310 * Copy the original kprobe_inst qualifying predicate(qp)
311 * to the break instruction
317 bundle->quad0.slot0 = break_inst;
320 bundle->quad0.slot1_p0 = break_inst;
321 bundle->quad1.slot1_p1 = break_inst >> (64-46);
324 bundle->quad1.slot2 = break_inst;
329 * Update the instruction flag, so that we can
330 * emulate the instruction properly after we
331 * single step on original instruction
333 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
336 static void __kprobes get_kprobe_inst(bundle_t *bundle, uint slot,
337 unsigned long *kprobe_inst, uint *major_opcode)
339 unsigned long kprobe_inst_p0, kprobe_inst_p1;
340 unsigned int template;
342 template = bundle->quad0.template;
346 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
347 *kprobe_inst = bundle->quad0.slot0;
350 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
351 kprobe_inst_p0 = bundle->quad0.slot1_p0;
352 kprobe_inst_p1 = bundle->quad1.slot1_p1;
353 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
356 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
357 *kprobe_inst = bundle->quad1.slot2;
362 /* Returns non-zero if the addr is in the Interrupt Vector Table */
363 static int __kprobes in_ivt_functions(unsigned long addr)
365 return (addr >= (unsigned long)__start_ivt_text
366 && addr < (unsigned long)__end_ivt_text);
369 static int __kprobes valid_kprobe_addr(int template, int slot,
372 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
373 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
378 if (in_ivt_functions(addr)) {
379 printk(KERN_WARNING "Kprobes can't be inserted inside "
380 "IVT functions at 0x%lx\n", addr);
387 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
390 i = atomic_add_return(1, &kcb->prev_kprobe_index);
391 kcb->prev_kprobe[i-1].kp = kprobe_running();
392 kcb->prev_kprobe[i-1].status = kcb->kprobe_status;
395 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
398 i = atomic_read(&kcb->prev_kprobe_index);
399 __get_cpu_var(current_kprobe) = kcb->prev_kprobe[i-1].kp;
400 kcb->kprobe_status = kcb->prev_kprobe[i-1].status;
401 atomic_sub(1, &kcb->prev_kprobe_index);
404 static void __kprobes set_current_kprobe(struct kprobe *p,
405 struct kprobe_ctlblk *kcb)
407 __get_cpu_var(current_kprobe) = p;
410 static void kretprobe_trampoline(void)
415 * At this point the target function has been tricked into
416 * returning into our trampoline. Lookup the associated instance
418 * - call the handler function
419 * - cleanup by marking the instance as unused
420 * - long jump back to the original return address
422 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
424 struct kretprobe_instance *ri = NULL;
425 struct hlist_head *head, empty_rp;
426 struct hlist_node *node, *tmp;
427 unsigned long flags, orig_ret_address = 0;
428 unsigned long trampoline_address =
429 ((struct fnptr *)kretprobe_trampoline)->ip;
431 INIT_HLIST_HEAD(&empty_rp);
432 kretprobe_hash_lock(current, &head, &flags);
435 * It is possible to have multiple instances associated with a given
436 * task either because an multiple functions in the call path
437 * have a return probe installed on them, and/or more then one return
438 * return probe was registered for a target function.
440 * We can handle this because:
441 * - instances are always inserted at the head of the list
442 * - when multiple return probes are registered for the same
443 * function, the first instance's ret_addr will point to the
444 * real return address, and all the rest will point to
445 * kretprobe_trampoline
447 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
448 if (ri->task != current)
449 /* another task is sharing our hash bucket */
452 orig_ret_address = (unsigned long)ri->ret_addr;
453 if (orig_ret_address != trampoline_address)
455 * This is the real return address. Any other
456 * instances associated with this task are for
457 * other calls deeper on the call stack
462 regs->cr_iip = orig_ret_address;
464 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
465 if (ri->task != current)
466 /* another task is sharing our hash bucket */
469 if (ri->rp && ri->rp->handler)
470 ri->rp->handler(ri, regs);
472 orig_ret_address = (unsigned long)ri->ret_addr;
473 recycle_rp_inst(ri, &empty_rp);
475 if (orig_ret_address != trampoline_address)
477 * This is the real return address. Any other
478 * instances associated with this task are for
479 * other calls deeper on the call stack
484 kretprobe_assert(ri, orig_ret_address, trampoline_address);
486 reset_current_kprobe();
487 kretprobe_hash_unlock(current, &flags);
488 preempt_enable_no_resched();
490 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
491 hlist_del(&ri->hlist);
495 * By returning a non-zero value, we are telling
496 * kprobe_handler() that we don't want the post_handler
497 * to run (and have re-enabled preemption)
502 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
503 struct pt_regs *regs)
505 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
507 /* Replace the return addr with trampoline addr */
508 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
511 /* Check the instruction in the slot is break */
512 static int __kprobes __is_ia64_break_inst(bundle_t *bundle, uint slot)
514 unsigned int major_opcode;
515 unsigned int template = bundle->quad0.template;
516 unsigned long kprobe_inst;
518 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
519 if (slot == 1 && bundle_encoding[template][1] == L)
522 /* Get Kprobe probe instruction at given slot*/
523 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
525 /* For break instruction,
526 * Bits 37:40 Major opcode to be zero
527 * Bits 27:32 X6 to be zero
528 * Bits 32:35 X3 to be zero
530 if (major_opcode || ((kprobe_inst >> 27) & 0x1FF)) {
531 /* Not a break instruction */
535 /* Is a break instruction */
540 * In this function, we check whether the target bundle modifies IP or
541 * it triggers an exception. If so, it cannot be boostable.
543 static int __kprobes can_boost(bundle_t *bundle, uint slot,
544 unsigned long bundle_addr)
546 unsigned int template = bundle->quad0.template;
549 if (search_exception_tables(bundle_addr + slot) ||
550 __is_ia64_break_inst(bundle, slot))
551 return 0; /* exception may occur in this bundle*/
552 } while ((++slot) < 3);
554 if (template >= 0x10 /* including B unit */ ||
555 template == 0x04 /* including X unit */ ||
556 template == 0x06) /* undefined */
562 /* Prepare long jump bundle and disables other boosters if need */
563 static void __kprobes prepare_booster(struct kprobe *p)
565 unsigned long addr = (unsigned long)p->addr & ~0xFULL;
566 unsigned int slot = (unsigned long)p->addr & 0xf;
567 struct kprobe *other_kp;
569 if (can_boost(&p->ainsn.insn[0].bundle, slot, addr)) {
570 set_brl_inst(&p->ainsn.insn[1].bundle, (bundle_t *)addr + 1);
571 p->ainsn.inst_flag |= INST_FLAG_BOOSTABLE;
574 /* disables boosters in previous slots */
575 for (; addr < (unsigned long)p->addr; addr++) {
576 other_kp = get_kprobe((void *)addr);
578 other_kp->ainsn.inst_flag &= ~INST_FLAG_BOOSTABLE;
582 int __kprobes arch_prepare_kprobe(struct kprobe *p)
584 unsigned long addr = (unsigned long) p->addr;
585 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
586 unsigned long kprobe_inst=0;
587 unsigned int slot = addr & 0xf, template, major_opcode = 0;
591 bundle = &((kprobe_opcode_t *)kprobe_addr)->bundle;
592 template = bundle->quad0.template;
594 if(valid_kprobe_addr(template, slot, addr))
597 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
598 if (slot == 1 && bundle_encoding[template][1] == L)
601 /* Get kprobe_inst and major_opcode from the bundle */
602 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
604 qp = unsupported_inst(template, slot, major_opcode, kprobe_inst, addr);
608 p->ainsn.insn = get_insn_slot();
611 memcpy(&p->opcode, kprobe_addr, sizeof(kprobe_opcode_t));
612 memcpy(p->ainsn.insn, kprobe_addr, sizeof(kprobe_opcode_t));
614 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p, qp);
621 void __kprobes arch_arm_kprobe(struct kprobe *p)
623 unsigned long arm_addr;
624 bundle_t *src, *dest;
626 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
627 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
628 src = &p->opcode.bundle;
630 flush_icache_range((unsigned long)p->ainsn.insn,
631 (unsigned long)p->ainsn.insn +
632 sizeof(kprobe_opcode_t) * MAX_INSN_SIZE);
634 switch (p->ainsn.slot) {
636 dest->quad0.slot0 = src->quad0.slot0;
639 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
642 dest->quad1.slot2 = src->quad1.slot2;
645 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
648 void __kprobes arch_disarm_kprobe(struct kprobe *p)
650 unsigned long arm_addr;
651 bundle_t *src, *dest;
653 arm_addr = ((unsigned long)p->addr) & ~0xFUL;
654 dest = &((kprobe_opcode_t *)arm_addr)->bundle;
655 /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
656 src = &p->ainsn.insn->bundle;
657 switch (p->ainsn.slot) {
659 dest->quad0.slot0 = src->quad0.slot0;
662 dest->quad1.slot1_p1 = src->quad1.slot1_p1;
665 dest->quad1.slot2 = src->quad1.slot2;
668 flush_icache_range(arm_addr, arm_addr + sizeof(kprobe_opcode_t));
671 void __kprobes arch_remove_kprobe(struct kprobe *p)
673 mutex_lock(&kprobe_mutex);
674 free_insn_slot(p->ainsn.insn, p->ainsn.inst_flag & INST_FLAG_BOOSTABLE);
675 mutex_unlock(&kprobe_mutex);
678 * We are resuming execution after a single step fault, so the pt_regs
679 * structure reflects the register state after we executed the instruction
680 * located in the kprobe (p->ainsn.insn->bundle). We still need to adjust
681 * the ip to point back to the original stack address. To set the IP address
682 * to original stack address, handle the case where we need to fixup the
683 * relative IP address and/or fixup branch register.
685 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
687 unsigned long bundle_addr = (unsigned long) (&p->ainsn.insn->bundle);
688 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
689 unsigned long template;
690 int slot = ((unsigned long)p->addr & 0xf);
692 template = p->ainsn.insn->bundle.quad0.template;
694 if (slot == 1 && bundle_encoding[template][1] == L)
697 if (p->ainsn.inst_flag & ~INST_FLAG_BOOSTABLE) {
699 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
700 /* Fix relative IP address */
701 regs->cr_iip = (regs->cr_iip - bundle_addr) +
705 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
707 * Fix target branch register, software convention is
708 * to use either b0 or b6 or b7, so just checking
709 * only those registers
711 switch (p->ainsn.target_br_reg) {
713 if ((regs->b0 == bundle_addr) ||
714 (regs->b0 == bundle_addr + 0x10)) {
715 regs->b0 = (regs->b0 - bundle_addr) +
720 if ((regs->b6 == bundle_addr) ||
721 (regs->b6 == bundle_addr + 0x10)) {
722 regs->b6 = (regs->b6 - bundle_addr) +
727 if ((regs->b7 == bundle_addr) ||
728 (regs->b7 == bundle_addr + 0x10)) {
729 regs->b7 = (regs->b7 - bundle_addr) +
739 if (regs->cr_iip == bundle_addr + 0x10) {
740 regs->cr_iip = resume_addr + 0x10;
743 if (regs->cr_iip == bundle_addr) {
744 regs->cr_iip = resume_addr;
749 /* Turn off Single Step bit */
750 ia64_psr(regs)->ss = 0;
753 static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
755 unsigned long bundle_addr = (unsigned long) &p->ainsn.insn->bundle;
756 unsigned long slot = (unsigned long)p->addr & 0xf;
758 /* single step inline if break instruction */
759 if (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)
760 regs->cr_iip = (unsigned long)p->addr & ~0xFULL;
762 regs->cr_iip = bundle_addr & ~0xFULL;
767 ia64_psr(regs)->ri = slot;
769 /* turn on single stepping */
770 ia64_psr(regs)->ss = 1;
773 static int __kprobes is_ia64_break_inst(struct pt_regs *regs)
775 unsigned int slot = ia64_psr(regs)->ri;
776 unsigned long *kprobe_addr = (unsigned long *)regs->cr_iip;
779 memcpy(&bundle, kprobe_addr, sizeof(bundle_t));
781 return __is_ia64_break_inst(&bundle, slot);
784 static int __kprobes pre_kprobes_handler(struct die_args *args)
788 struct pt_regs *regs = args->regs;
789 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
790 struct kprobe_ctlblk *kcb;
793 * We don't want to be preempted for the entire
794 * duration of kprobe processing
797 kcb = get_kprobe_ctlblk();
799 /* Handle recursion cases */
800 if (kprobe_running()) {
801 p = get_kprobe(addr);
803 if ((kcb->kprobe_status == KPROBE_HIT_SS) &&
804 (p->ainsn.inst_flag == INST_FLAG_BREAK_INST)) {
805 ia64_psr(regs)->ss = 0;
808 /* We have reentered the pre_kprobe_handler(), since
809 * another probe was hit while within the handler.
810 * We here save the original kprobes variables and
811 * just single step on the instruction of the new probe
812 * without calling any user handlers.
814 save_previous_kprobe(kcb);
815 set_current_kprobe(p, kcb);
816 kprobes_inc_nmissed_count(p);
818 kcb->kprobe_status = KPROBE_REENTER;
820 } else if (args->err == __IA64_BREAK_JPROBE) {
822 * jprobe instrumented function just completed
824 p = __get_cpu_var(current_kprobe);
825 if (p->break_handler && p->break_handler(p, regs)) {
828 } else if (!is_ia64_break_inst(regs)) {
829 /* The breakpoint instruction was removed by
830 * another cpu right after we hit, no further
831 * handling of this interrupt is appropriate
841 p = get_kprobe(addr);
843 if (!is_ia64_break_inst(regs)) {
845 * The breakpoint instruction was removed right
846 * after we hit it. Another cpu has removed
847 * either a probepoint or a debugger breakpoint
848 * at this address. In either case, no further
849 * handling of this interrupt is appropriate.
855 /* Not one of our break, let kernel handle it */
859 set_current_kprobe(p, kcb);
860 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
862 if (p->pre_handler && p->pre_handler(p, regs))
864 * Our pre-handler is specifically requesting that we just
865 * do a return. This is used for both the jprobe pre-handler
866 * and the kretprobe trampoline
871 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
872 if (p->ainsn.inst_flag == INST_FLAG_BOOSTABLE && !p->post_handler) {
873 /* Boost up -- we can execute copied instructions directly */
874 ia64_psr(regs)->ri = p->ainsn.slot;
875 regs->cr_iip = (unsigned long)&p->ainsn.insn->bundle & ~0xFULL;
876 /* turn single stepping off */
877 ia64_psr(regs)->ss = 0;
879 reset_current_kprobe();
880 preempt_enable_no_resched();
885 kcb->kprobe_status = KPROBE_HIT_SS;
889 preempt_enable_no_resched();
893 static int __kprobes post_kprobes_handler(struct pt_regs *regs)
895 struct kprobe *cur = kprobe_running();
896 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
901 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
902 kcb->kprobe_status = KPROBE_HIT_SSDONE;
903 cur->post_handler(cur, regs, 0);
906 resume_execution(cur, regs);
908 /*Restore back the original saved kprobes variables and continue. */
909 if (kcb->kprobe_status == KPROBE_REENTER) {
910 restore_previous_kprobe(kcb);
913 reset_current_kprobe();
916 preempt_enable_no_resched();
920 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
922 struct kprobe *cur = kprobe_running();
923 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
926 switch(kcb->kprobe_status) {
930 * We are here because the instruction being single
931 * stepped caused a page fault. We reset the current
932 * kprobe and the instruction pointer points back to
933 * the probe address and allow the page fault handler
934 * to continue as a normal page fault.
936 regs->cr_iip = ((unsigned long)cur->addr) & ~0xFULL;
937 ia64_psr(regs)->ri = ((unsigned long)cur->addr) & 0xf;
938 if (kcb->kprobe_status == KPROBE_REENTER)
939 restore_previous_kprobe(kcb);
941 reset_current_kprobe();
942 preempt_enable_no_resched();
944 case KPROBE_HIT_ACTIVE:
945 case KPROBE_HIT_SSDONE:
947 * We increment the nmissed count for accounting,
948 * we can also use npre/npostfault count for accouting
949 * these specific fault cases.
951 kprobes_inc_nmissed_count(cur);
954 * We come here because instructions in the pre/post
955 * handler caused the page_fault, this could happen
956 * if handler tries to access user space by
957 * copy_from_user(), get_user() etc. Let the
958 * user-specified handler try to fix it first.
960 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
963 * In case the user-specified fault handler returned
964 * zero, try to fix up.
966 if (ia64_done_with_exception(regs))
970 * Let ia64_do_page_fault() fix it.
980 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
981 unsigned long val, void *data)
983 struct die_args *args = (struct die_args *)data;
984 int ret = NOTIFY_DONE;
986 if (args->regs && user_mode(args->regs))
991 /* err is break number from ia64_bad_break() */
992 if ((args->err >> 12) == (__IA64_BREAK_KPROBE >> 12)
993 || args->err == __IA64_BREAK_JPROBE
995 if (pre_kprobes_handler(args))
999 /* err is vector number from ia64_fault() */
1000 if (args->err == 36)
1001 if (post_kprobes_handler(args->regs))
1010 struct param_bsp_cfm {
1016 static void ia64_get_bsp_cfm(struct unw_frame_info *info, void *arg)
1019 struct param_bsp_cfm *lp = arg;
1022 unw_get_ip(info, &ip);
1026 unw_get_bsp(info, (unsigned long*)&lp->bsp);
1027 unw_get_cfm(info, (unsigned long*)&lp->cfm);
1030 } while (unw_unwind(info) >= 0);
1036 unsigned long arch_deref_entry_point(void *entry)
1038 return ((struct fnptr *)entry)->ip;
1041 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1043 struct jprobe *jp = container_of(p, struct jprobe, kp);
1044 unsigned long addr = arch_deref_entry_point(jp->entry);
1045 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1046 struct param_bsp_cfm pa;
1050 * Callee owns the argument space and could overwrite it, eg
1051 * tail call optimization. So to be absolutely safe
1052 * we save the argument space before transferring the control
1053 * to instrumented jprobe function which runs in
1054 * the process context
1056 pa.ip = regs->cr_iip;
1057 unw_init_running(ia64_get_bsp_cfm, &pa);
1058 bytes = (char *)ia64_rse_skip_regs(pa.bsp, pa.cfm & 0x3f)
1060 memcpy( kcb->jprobes_saved_stacked_regs,
1066 /* save architectural state */
1067 kcb->jprobe_saved_regs = *regs;
1069 /* after rfi, execute the jprobe instrumented function */
1070 regs->cr_iip = addr & ~0xFULL;
1071 ia64_psr(regs)->ri = addr & 0xf;
1072 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
1075 * fix the return address to our jprobe_inst_return() function
1076 * in the jprobes.S file
1078 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
1083 /* ia64 does not need this */
1084 void __kprobes jprobe_return(void)
1088 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1090 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1093 /* restoring architectural state */
1094 *regs = kcb->jprobe_saved_regs;
1096 /* restoring the original argument space */
1097 flush_register_stack();
1098 bytes = (char *)ia64_rse_skip_regs(kcb->bsp, kcb->cfm & 0x3f)
1101 kcb->jprobes_saved_stacked_regs,
1103 invalidate_stacked_regs();
1105 preempt_enable_no_resched();
1109 static struct kprobe trampoline_p = {
1110 .pre_handler = trampoline_probe_handler
1113 int __init arch_init_kprobes(void)
1116 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
1117 return register_kprobe(&trampoline_p);
1120 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1123 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip)