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] = {
82 * In this function we check to see if the instruction
83 * is IP relative instruction and update the kprobe
84 * inst flag accordingly
86 static void __kprobes
update_kprobe_inst_flag(uint
template, uint slot
,
88 unsigned long kprobe_inst
,
91 p
->ainsn
.inst_flag
= 0;
92 p
->ainsn
.target_br_reg
= 0;
95 /* Check for Break instruction
96 * Bits 37:40 Major opcode to be zero
97 * Bits 27:32 X6 to be zero
98 * Bits 32:35 X3 to be zero
100 if ((!major_opcode
) && (!((kprobe_inst
>> 27) & 0x1FF)) ) {
101 /* is a break instruction */
102 p
->ainsn
.inst_flag
|= INST_FLAG_BREAK_INST
;
106 if (bundle_encoding
[template][slot
] == B
) {
107 switch (major_opcode
) {
108 case INDIRECT_CALL_OPCODE
:
109 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_BRANCH_REG
;
110 p
->ainsn
.target_br_reg
= ((kprobe_inst
>> 6) & 0x7);
112 case IP_RELATIVE_PREDICT_OPCODE
:
113 case IP_RELATIVE_BRANCH_OPCODE
:
114 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_RELATIVE_IP_ADDR
;
116 case IP_RELATIVE_CALL_OPCODE
:
117 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_RELATIVE_IP_ADDR
;
118 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_BRANCH_REG
;
119 p
->ainsn
.target_br_reg
= ((kprobe_inst
>> 6) & 0x7);
122 } else if (bundle_encoding
[template][slot
] == X
) {
123 switch (major_opcode
) {
124 case LONG_CALL_OPCODE
:
125 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_BRANCH_REG
;
126 p
->ainsn
.target_br_reg
= ((kprobe_inst
>> 6) & 0x7);
134 * In this function we check to see if the instruction
135 * (qp) cmpx.crel.ctype p1,p2=r2,r3
136 * on which we are inserting kprobe is cmp instruction
139 static uint __kprobes
is_cmp_ctype_unc_inst(uint
template, uint slot
,
141 unsigned long kprobe_inst
)
146 if (!((bundle_encoding
[template][slot
] == I
) ||
147 (bundle_encoding
[template][slot
] == M
)))
150 if (!((major_opcode
== 0xC) || (major_opcode
== 0xD) ||
151 (major_opcode
== 0xE)))
154 cmp_inst
.l
= kprobe_inst
;
155 if ((cmp_inst
.f
.x2
== 0) || (cmp_inst
.f
.x2
== 1)) {
156 /* Integer compare - Register Register (A6 type)*/
157 if ((cmp_inst
.f
.tb
== 0) && (cmp_inst
.f
.ta
== 0)
158 &&(cmp_inst
.f
.c
== 1))
160 } else if ((cmp_inst
.f
.x2
== 2)||(cmp_inst
.f
.x2
== 3)) {
161 /* Integer compare - Immediate Register (A8 type)*/
162 if ((cmp_inst
.f
.ta
== 0) &&(cmp_inst
.f
.c
== 1))
170 * In this function we check to see if the instruction
171 * on which we are inserting kprobe is supported.
172 * Returns qp value if supported
173 * Returns -EINVAL if unsupported
175 static int __kprobes
unsupported_inst(uint
template, uint slot
,
177 unsigned long kprobe_inst
,
182 qp
= kprobe_inst
& 0x3f;
183 if (is_cmp_ctype_unc_inst(template, slot
, major_opcode
, kprobe_inst
)) {
184 if (slot
== 1 && qp
) {
185 printk(KERN_WARNING
"Kprobes on cmp unc "
186 "instruction on slot 1 at <0x%lx> "
187 "is not supported\n", addr
);
193 else if (bundle_encoding
[template][slot
] == I
) {
194 if (major_opcode
== 0) {
196 * Check for Integer speculation instruction
197 * - Bit 33-35 to be equal to 0x1
199 if (((kprobe_inst
>> 33) & 0x7) == 1) {
201 "Kprobes on speculation inst at <0x%lx> not supported\n",
206 * IP relative mov instruction
207 * - Bit 27-35 to be equal to 0x30
209 if (((kprobe_inst
>> 27) & 0x1FF) == 0x30) {
211 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
217 else if ((major_opcode
== 5) && !(kprobe_inst
& (0xFUl
<< 33)) &&
218 (kprobe_inst
& (0x1UL
<< 12))) {
219 /* test bit instructions, tbit,tnat,tf
220 * bit 33-36 to be equal to 0
221 * bit 12 to be equal to 1
223 if (slot
== 1 && qp
) {
224 printk(KERN_WARNING
"Kprobes on test bit "
225 "instruction on slot at <0x%lx> "
226 "is not supported\n", addr
);
232 else if (bundle_encoding
[template][slot
] == B
) {
233 if (major_opcode
== 7) {
234 /* IP-Relative Predict major code is 7 */
235 printk(KERN_WARNING
"Kprobes on IP-Relative"
236 "Predict is not supported\n");
239 else if (major_opcode
== 2) {
240 /* Indirect Predict, major code is 2
241 * bit 27-32 to be equal to 10 or 11
243 int x6
=(kprobe_inst
>> 27) & 0x3F;
244 if ((x6
== 0x10) || (x6
== 0x11)) {
245 printk(KERN_WARNING
"Kprobes on "
246 "Indirect Predict is not supported\n");
251 /* kernel does not use float instruction, here for safety kprobe
252 * will judge whether it is fcmp/flass/float approximation instruction
254 else if (unlikely(bundle_encoding
[template][slot
] == F
)) {
255 if ((major_opcode
== 4 || major_opcode
== 5) &&
256 (kprobe_inst
& (0x1 << 12))) {
257 /* fcmp/fclass unc instruction */
258 if (slot
== 1 && qp
) {
259 printk(KERN_WARNING
"Kprobes on fcmp/fclass "
260 "instruction on slot at <0x%lx> "
261 "is not supported\n", addr
);
267 if ((major_opcode
== 0 || major_opcode
== 1) &&
268 (kprobe_inst
& (0x1UL
<< 33))) {
269 /* float Approximation instruction */
270 if (slot
== 1 && qp
) {
271 printk(KERN_WARNING
"Kprobes on float Approx "
272 "instr at <0x%lx> is not supported\n",
283 * In this function we override the bundle with
284 * the break instruction at the given slot.
286 static void __kprobes
prepare_break_inst(uint
template, uint slot
,
288 unsigned long kprobe_inst
,
292 unsigned long break_inst
= BREAK_INST
;
293 bundle_t
*bundle
= &p
->opcode
.bundle
;
296 * Copy the original kprobe_inst qualifying predicate(qp)
297 * to the break instruction
303 bundle
->quad0
.slot0
= break_inst
;
306 bundle
->quad0
.slot1_p0
= break_inst
;
307 bundle
->quad1
.slot1_p1
= break_inst
>> (64-46);
310 bundle
->quad1
.slot2
= break_inst
;
315 * Update the instruction flag, so that we can
316 * emulate the instruction properly after we
317 * single step on original instruction
319 update_kprobe_inst_flag(template, slot
, major_opcode
, kprobe_inst
, p
);
322 static void __kprobes
get_kprobe_inst(bundle_t
*bundle
, uint slot
,
323 unsigned long *kprobe_inst
, uint
*major_opcode
)
325 unsigned long kprobe_inst_p0
, kprobe_inst_p1
;
326 unsigned int template;
328 template = bundle
->quad0
.template;
332 *major_opcode
= (bundle
->quad0
.slot0
>> SLOT0_OPCODE_SHIFT
);
333 *kprobe_inst
= bundle
->quad0
.slot0
;
336 *major_opcode
= (bundle
->quad1
.slot1_p1
>> SLOT1_p1_OPCODE_SHIFT
);
337 kprobe_inst_p0
= bundle
->quad0
.slot1_p0
;
338 kprobe_inst_p1
= bundle
->quad1
.slot1_p1
;
339 *kprobe_inst
= kprobe_inst_p0
| (kprobe_inst_p1
<< (64-46));
342 *major_opcode
= (bundle
->quad1
.slot2
>> SLOT2_OPCODE_SHIFT
);
343 *kprobe_inst
= bundle
->quad1
.slot2
;
348 /* Returns non-zero if the addr is in the Interrupt Vector Table */
349 static int __kprobes
in_ivt_functions(unsigned long addr
)
351 return (addr
>= (unsigned long)__start_ivt_text
352 && addr
< (unsigned long)__end_ivt_text
);
355 static int __kprobes
valid_kprobe_addr(int template, int slot
,
358 if ((slot
> 2) || ((bundle_encoding
[template][1] == L
) && slot
> 1)) {
359 printk(KERN_WARNING
"Attempting to insert unaligned kprobe "
364 if (in_ivt_functions(addr
)) {
365 printk(KERN_WARNING
"Kprobes can't be inserted inside "
366 "IVT functions at 0x%lx\n", addr
);
373 static void __kprobes
save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
376 i
= atomic_add_return(1, &kcb
->prev_kprobe_index
);
377 kcb
->prev_kprobe
[i
-1].kp
= kprobe_running();
378 kcb
->prev_kprobe
[i
-1].status
= kcb
->kprobe_status
;
381 static void __kprobes
restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
384 i
= atomic_sub_return(1, &kcb
->prev_kprobe_index
);
385 __get_cpu_var(current_kprobe
) = kcb
->prev_kprobe
[i
].kp
;
386 kcb
->kprobe_status
= kcb
->prev_kprobe
[i
].status
;
389 static void __kprobes
set_current_kprobe(struct kprobe
*p
,
390 struct kprobe_ctlblk
*kcb
)
392 __get_cpu_var(current_kprobe
) = p
;
395 static void kretprobe_trampoline(void)
400 * At this point the target function has been tricked into
401 * returning into our trampoline. Lookup the associated instance
403 * - call the handler function
404 * - cleanup by marking the instance as unused
405 * - long jump back to the original return address
407 int __kprobes
trampoline_probe_handler(struct kprobe
*p
, struct pt_regs
*regs
)
409 struct kretprobe_instance
*ri
= NULL
;
410 struct hlist_head
*head
, empty_rp
;
411 struct hlist_node
*node
, *tmp
;
412 unsigned long flags
, orig_ret_address
= 0;
413 unsigned long trampoline_address
=
414 ((struct fnptr
*)kretprobe_trampoline
)->ip
;
416 INIT_HLIST_HEAD(&empty_rp
);
417 spin_lock_irqsave(&kretprobe_lock
, flags
);
418 head
= kretprobe_inst_table_head(current
);
421 * It is possible to have multiple instances associated with a given
422 * task either because an multiple functions in the call path
423 * have a return probe installed on them, and/or more then one return
424 * return probe was registered for a target function.
426 * We can handle this because:
427 * - instances are always inserted at the head of the list
428 * - when multiple return probes are registered for the same
429 * function, the first instance's ret_addr will point to the
430 * real return address, and all the rest will point to
431 * kretprobe_trampoline
433 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
434 if (ri
->task
!= current
)
435 /* another task is sharing our hash bucket */
438 orig_ret_address
= (unsigned long)ri
->ret_addr
;
439 if (orig_ret_address
!= trampoline_address
)
441 * This is the real return address. Any other
442 * instances associated with this task are for
443 * other calls deeper on the call stack
448 regs
->cr_iip
= orig_ret_address
;
450 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
451 if (ri
->task
!= current
)
452 /* another task is sharing our hash bucket */
455 if (ri
->rp
&& ri
->rp
->handler
)
456 ri
->rp
->handler(ri
, regs
);
458 orig_ret_address
= (unsigned long)ri
->ret_addr
;
459 recycle_rp_inst(ri
, &empty_rp
);
461 if (orig_ret_address
!= trampoline_address
)
463 * This is the real return address. Any other
464 * instances associated with this task are for
465 * other calls deeper on the call stack
470 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
472 reset_current_kprobe();
473 spin_unlock_irqrestore(&kretprobe_lock
, flags
);
474 preempt_enable_no_resched();
476 hlist_for_each_entry_safe(ri
, node
, tmp
, &empty_rp
, hlist
) {
477 hlist_del(&ri
->hlist
);
481 * By returning a non-zero value, we are telling
482 * kprobe_handler() that we don't want the post_handler
483 * to run (and have re-enabled preemption)
488 /* Called with kretprobe_lock held */
489 void __kprobes
arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
490 struct pt_regs
*regs
)
492 ri
->ret_addr
= (kprobe_opcode_t
*)regs
->b0
;
494 /* Replace the return addr with trampoline addr */
495 regs
->b0
= ((struct fnptr
*)kretprobe_trampoline
)->ip
;
498 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
500 unsigned long addr
= (unsigned long) p
->addr
;
501 unsigned long *kprobe_addr
= (unsigned long *)(addr
& ~0xFULL
);
502 unsigned long kprobe_inst
=0;
503 unsigned int slot
= addr
& 0xf, template, major_opcode
= 0;
507 bundle
= &((kprobe_opcode_t
*)kprobe_addr
)->bundle
;
508 template = bundle
->quad0
.template;
510 if(valid_kprobe_addr(template, slot
, addr
))
513 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
514 if (slot
== 1 && bundle_encoding
[template][1] == L
)
517 /* Get kprobe_inst and major_opcode from the bundle */
518 get_kprobe_inst(bundle
, slot
, &kprobe_inst
, &major_opcode
);
520 qp
= unsupported_inst(template, slot
, major_opcode
, kprobe_inst
, addr
);
524 p
->ainsn
.insn
= get_insn_slot();
527 memcpy(&p
->opcode
, kprobe_addr
, sizeof(kprobe_opcode_t
));
528 memcpy(p
->ainsn
.insn
, kprobe_addr
, sizeof(kprobe_opcode_t
));
530 prepare_break_inst(template, slot
, major_opcode
, kprobe_inst
, p
, qp
);
535 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
537 unsigned long arm_addr
;
538 bundle_t
*src
, *dest
;
540 arm_addr
= ((unsigned long)p
->addr
) & ~0xFUL
;
541 dest
= &((kprobe_opcode_t
*)arm_addr
)->bundle
;
542 src
= &p
->opcode
.bundle
;
544 flush_icache_range((unsigned long)p
->ainsn
.insn
,
545 (unsigned long)p
->ainsn
.insn
+ sizeof(kprobe_opcode_t
));
546 switch (p
->ainsn
.slot
) {
548 dest
->quad0
.slot0
= src
->quad0
.slot0
;
551 dest
->quad1
.slot1_p1
= src
->quad1
.slot1_p1
;
554 dest
->quad1
.slot2
= src
->quad1
.slot2
;
557 flush_icache_range(arm_addr
, arm_addr
+ sizeof(kprobe_opcode_t
));
560 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
562 unsigned long arm_addr
;
563 bundle_t
*src
, *dest
;
565 arm_addr
= ((unsigned long)p
->addr
) & ~0xFUL
;
566 dest
= &((kprobe_opcode_t
*)arm_addr
)->bundle
;
567 /* p->ainsn.insn contains the original unaltered kprobe_opcode_t */
568 src
= &p
->ainsn
.insn
->bundle
;
569 switch (p
->ainsn
.slot
) {
571 dest
->quad0
.slot0
= src
->quad0
.slot0
;
574 dest
->quad1
.slot1_p1
= src
->quad1
.slot1_p1
;
577 dest
->quad1
.slot2
= src
->quad1
.slot2
;
580 flush_icache_range(arm_addr
, arm_addr
+ sizeof(kprobe_opcode_t
));
583 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
585 mutex_lock(&kprobe_mutex
);
586 free_insn_slot(p
->ainsn
.insn
, 0);
587 mutex_unlock(&kprobe_mutex
);
590 * We are resuming execution after a single step fault, so the pt_regs
591 * structure reflects the register state after we executed the instruction
592 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
593 * the ip to point back to the original stack address. To set the IP address
594 * to original stack address, handle the case where we need to fixup the
595 * relative IP address and/or fixup branch register.
597 static void __kprobes
resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
599 unsigned long bundle_addr
= (unsigned long) (&p
->ainsn
.insn
->bundle
);
600 unsigned long resume_addr
= (unsigned long)p
->addr
& ~0xFULL
;
601 unsigned long template;
602 int slot
= ((unsigned long)p
->addr
& 0xf);
604 template = p
->ainsn
.insn
->bundle
.quad0
.template;
606 if (slot
== 1 && bundle_encoding
[template][1] == L
)
609 if (p
->ainsn
.inst_flag
) {
611 if (p
->ainsn
.inst_flag
& INST_FLAG_FIX_RELATIVE_IP_ADDR
) {
612 /* Fix relative IP address */
613 regs
->cr_iip
= (regs
->cr_iip
- bundle_addr
) +
617 if (p
->ainsn
.inst_flag
& INST_FLAG_FIX_BRANCH_REG
) {
619 * Fix target branch register, software convention is
620 * to use either b0 or b6 or b7, so just checking
621 * only those registers
623 switch (p
->ainsn
.target_br_reg
) {
625 if ((regs
->b0
== bundle_addr
) ||
626 (regs
->b0
== bundle_addr
+ 0x10)) {
627 regs
->b0
= (regs
->b0
- bundle_addr
) +
632 if ((regs
->b6
== bundle_addr
) ||
633 (regs
->b6
== bundle_addr
+ 0x10)) {
634 regs
->b6
= (regs
->b6
- bundle_addr
) +
639 if ((regs
->b7
== bundle_addr
) ||
640 (regs
->b7
== bundle_addr
+ 0x10)) {
641 regs
->b7
= (regs
->b7
- bundle_addr
) +
651 if (regs
->cr_iip
== bundle_addr
+ 0x10) {
652 regs
->cr_iip
= resume_addr
+ 0x10;
655 if (regs
->cr_iip
== bundle_addr
) {
656 regs
->cr_iip
= resume_addr
;
661 /* Turn off Single Step bit */
662 ia64_psr(regs
)->ss
= 0;
665 static void __kprobes
prepare_ss(struct kprobe
*p
, struct pt_regs
*regs
)
667 unsigned long bundle_addr
= (unsigned long) &p
->ainsn
.insn
->bundle
;
668 unsigned long slot
= (unsigned long)p
->addr
& 0xf;
670 /* single step inline if break instruction */
671 if (p
->ainsn
.inst_flag
== INST_FLAG_BREAK_INST
)
672 regs
->cr_iip
= (unsigned long)p
->addr
& ~0xFULL
;
674 regs
->cr_iip
= bundle_addr
& ~0xFULL
;
679 ia64_psr(regs
)->ri
= slot
;
681 /* turn on single stepping */
682 ia64_psr(regs
)->ss
= 1;
685 static int __kprobes
is_ia64_break_inst(struct pt_regs
*regs
)
687 unsigned int slot
= ia64_psr(regs
)->ri
;
688 unsigned int template, major_opcode
;
689 unsigned long kprobe_inst
;
690 unsigned long *kprobe_addr
= (unsigned long *)regs
->cr_iip
;
693 memcpy(&bundle
, kprobe_addr
, sizeof(bundle_t
));
694 template = bundle
.quad0
.template;
696 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
697 if (slot
== 1 && bundle_encoding
[template][1] == L
)
700 /* Get Kprobe probe instruction at given slot*/
701 get_kprobe_inst(&bundle
, slot
, &kprobe_inst
, &major_opcode
);
703 /* For break instruction,
704 * Bits 37:40 Major opcode to be zero
705 * Bits 27:32 X6 to be zero
706 * Bits 32:35 X3 to be zero
708 if (major_opcode
|| ((kprobe_inst
>> 27) & 0x1FF) ) {
709 /* Not a break instruction */
713 /* Is a break instruction */
717 static int __kprobes
pre_kprobes_handler(struct die_args
*args
)
721 struct pt_regs
*regs
= args
->regs
;
722 kprobe_opcode_t
*addr
= (kprobe_opcode_t
*)instruction_pointer(regs
);
723 struct kprobe_ctlblk
*kcb
;
726 * We don't want to be preempted for the entire
727 * duration of kprobe processing
730 kcb
= get_kprobe_ctlblk();
732 /* Handle recursion cases */
733 if (kprobe_running()) {
734 p
= get_kprobe(addr
);
736 if ((kcb
->kprobe_status
== KPROBE_HIT_SS
) &&
737 (p
->ainsn
.inst_flag
== INST_FLAG_BREAK_INST
)) {
738 ia64_psr(regs
)->ss
= 0;
741 /* We have reentered the pre_kprobe_handler(), since
742 * another probe was hit while within the handler.
743 * We here save the original kprobes variables and
744 * just single step on the instruction of the new probe
745 * without calling any user handlers.
747 save_previous_kprobe(kcb
);
748 set_current_kprobe(p
, kcb
);
749 kprobes_inc_nmissed_count(p
);
751 kcb
->kprobe_status
= KPROBE_REENTER
;
753 } else if (args
->err
== __IA64_BREAK_JPROBE
) {
755 * jprobe instrumented function just completed
757 p
= __get_cpu_var(current_kprobe
);
758 if (p
->break_handler
&& p
->break_handler(p
, regs
)) {
761 } else if (!is_ia64_break_inst(regs
)) {
762 /* The breakpoint instruction was removed by
763 * another cpu right after we hit, no further
764 * handling of this interrupt is appropriate
774 p
= get_kprobe(addr
);
776 if (!is_ia64_break_inst(regs
)) {
778 * The breakpoint instruction was removed right
779 * after we hit it. Another cpu has removed
780 * either a probepoint or a debugger breakpoint
781 * at this address. In either case, no further
782 * handling of this interrupt is appropriate.
788 /* Not one of our break, let kernel handle it */
792 set_current_kprobe(p
, kcb
);
793 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
795 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
797 * Our pre-handler is specifically requesting that we just
798 * do a return. This is used for both the jprobe pre-handler
799 * and the kretprobe trampoline
805 kcb
->kprobe_status
= KPROBE_HIT_SS
;
809 preempt_enable_no_resched();
813 static int __kprobes
post_kprobes_handler(struct pt_regs
*regs
)
815 struct kprobe
*cur
= kprobe_running();
816 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
821 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
822 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
823 cur
->post_handler(cur
, regs
, 0);
826 resume_execution(cur
, regs
);
828 /*Restore back the original saved kprobes variables and continue. */
829 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
830 restore_previous_kprobe(kcb
);
833 reset_current_kprobe();
836 preempt_enable_no_resched();
840 int __kprobes
kprobes_fault_handler(struct pt_regs
*regs
, int trapnr
)
842 struct kprobe
*cur
= kprobe_running();
843 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
846 switch(kcb
->kprobe_status
) {
850 * We are here because the instruction being single
851 * stepped caused a page fault. We reset the current
852 * kprobe and the instruction pointer points back to
853 * the probe address and allow the page fault handler
854 * to continue as a normal page fault.
856 regs
->cr_iip
= ((unsigned long)cur
->addr
) & ~0xFULL
;
857 ia64_psr(regs
)->ri
= ((unsigned long)cur
->addr
) & 0xf;
858 if (kcb
->kprobe_status
== KPROBE_REENTER
)
859 restore_previous_kprobe(kcb
);
861 reset_current_kprobe();
862 preempt_enable_no_resched();
864 case KPROBE_HIT_ACTIVE
:
865 case KPROBE_HIT_SSDONE
:
867 * We increment the nmissed count for accounting,
868 * we can also use npre/npostfault count for accouting
869 * these specific fault cases.
871 kprobes_inc_nmissed_count(cur
);
874 * We come here because instructions in the pre/post
875 * handler caused the page_fault, this could happen
876 * if handler tries to access user space by
877 * copy_from_user(), get_user() etc. Let the
878 * user-specified handler try to fix it first.
880 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
883 * In case the user-specified fault handler returned
884 * zero, try to fix up.
886 if (ia64_done_with_exception(regs
))
890 * Let ia64_do_page_fault() fix it.
900 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
901 unsigned long val
, void *data
)
903 struct die_args
*args
= (struct die_args
*)data
;
904 int ret
= NOTIFY_DONE
;
906 if (args
->regs
&& user_mode(args
->regs
))
911 /* err is break number from ia64_bad_break() */
912 if ((args
->err
>> 12) == (__IA64_BREAK_KPROBE
>> 12)
913 || args
->err
== __IA64_BREAK_JPROBE
915 if (pre_kprobes_handler(args
))
919 /* err is vector number from ia64_fault() */
921 if (post_kprobes_handler(args
->regs
))
930 struct param_bsp_cfm
{
936 static void ia64_get_bsp_cfm(struct unw_frame_info
*info
, void *arg
)
939 struct param_bsp_cfm
*lp
= arg
;
942 unw_get_ip(info
, &ip
);
946 unw_get_bsp(info
, (unsigned long*)&lp
->bsp
);
947 unw_get_cfm(info
, (unsigned long*)&lp
->cfm
);
950 } while (unw_unwind(info
) >= 0);
956 unsigned long arch_deref_entry_point(void *entry
)
958 return ((struct fnptr
*)entry
)->ip
;
961 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
963 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
964 unsigned long addr
= arch_deref_entry_point(jp
->entry
);
965 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
966 struct param_bsp_cfm pa
;
970 * Callee owns the argument space and could overwrite it, eg
971 * tail call optimization. So to be absolutely safe
972 * we save the argument space before transferring the control
973 * to instrumented jprobe function which runs in
974 * the process context
976 pa
.ip
= regs
->cr_iip
;
977 unw_init_running(ia64_get_bsp_cfm
, &pa
);
978 bytes
= (char *)ia64_rse_skip_regs(pa
.bsp
, pa
.cfm
& 0x3f)
980 memcpy( kcb
->jprobes_saved_stacked_regs
,
986 /* save architectural state */
987 kcb
->jprobe_saved_regs
= *regs
;
989 /* after rfi, execute the jprobe instrumented function */
990 regs
->cr_iip
= addr
& ~0xFULL
;
991 ia64_psr(regs
)->ri
= addr
& 0xf;
992 regs
->r1
= ((struct fnptr
*)(jp
->entry
))->gp
;
995 * fix the return address to our jprobe_inst_return() function
996 * in the jprobes.S file
998 regs
->b0
= ((struct fnptr
*)(jprobe_inst_return
))->ip
;
1003 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
1005 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
1008 /* restoring architectural state */
1009 *regs
= kcb
->jprobe_saved_regs
;
1011 /* restoring the original argument space */
1012 flush_register_stack();
1013 bytes
= (char *)ia64_rse_skip_regs(kcb
->bsp
, kcb
->cfm
& 0x3f)
1016 kcb
->jprobes_saved_stacked_regs
,
1018 invalidate_stacked_regs();
1020 preempt_enable_no_resched();
1024 static struct kprobe trampoline_p
= {
1025 .pre_handler
= trampoline_probe_handler
1028 int __init
arch_init_kprobes(void)
1031 (kprobe_opcode_t
*)((struct fnptr
*)kretprobe_trampoline
)->ip
;
1032 return register_kprobe(&trampoline_p
);
1035 int __kprobes
arch_trampoline_kprobe(struct kprobe
*p
)
1038 (kprobe_opcode_t
*)((struct fnptr
*)kretprobe_trampoline
)->ip
)