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/config.h>
27 #include <linux/kprobes.h>
28 #include <linux/ptrace.h>
29 #include <linux/spinlock.h>
30 #include <linux/string.h>
31 #include <linux/slab.h>
32 #include <linux/preempt.h>
33 #include <linux/moduleloader.h>
35 #include <asm/pgtable.h>
36 #include <asm/kdebug.h>
37 #include <asm/sections.h>
39 extern void jprobe_inst_return(void);
41 /* kprobe_status settings */
42 #define KPROBE_HIT_ACTIVE 0x00000001
43 #define KPROBE_HIT_SS 0x00000002
45 static struct kprobe
*current_kprobe
, *kprobe_prev
;
46 static unsigned long kprobe_status
, kprobe_status_prev
;
47 static struct pt_regs jprobe_saved_regs
;
49 enum instruction_type
{A
, I
, M
, F
, B
, L
, X
, u
};
50 static enum instruction_type bundle_encoding
[32][3] = {
86 * In this function we check to see if the instruction
87 * is IP relative instruction and update the kprobe
88 * inst flag accordingly
90 static void __kprobes
update_kprobe_inst_flag(uint
template, uint slot
,
92 unsigned long kprobe_inst
,
95 p
->ainsn
.inst_flag
= 0;
96 p
->ainsn
.target_br_reg
= 0;
98 /* Check for Break instruction
99 * Bits 37:40 Major opcode to be zero
100 * Bits 27:32 X6 to be zero
101 * Bits 32:35 X3 to be zero
103 if ((!major_opcode
) && (!((kprobe_inst
>> 27) & 0x1FF)) ) {
104 /* is a break instruction */
105 p
->ainsn
.inst_flag
|= INST_FLAG_BREAK_INST
;
109 if (bundle_encoding
[template][slot
] == B
) {
110 switch (major_opcode
) {
111 case INDIRECT_CALL_OPCODE
:
112 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_BRANCH_REG
;
113 p
->ainsn
.target_br_reg
= ((kprobe_inst
>> 6) & 0x7);
115 case IP_RELATIVE_PREDICT_OPCODE
:
116 case IP_RELATIVE_BRANCH_OPCODE
:
117 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_RELATIVE_IP_ADDR
;
119 case IP_RELATIVE_CALL_OPCODE
:
120 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_RELATIVE_IP_ADDR
;
121 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_BRANCH_REG
;
122 p
->ainsn
.target_br_reg
= ((kprobe_inst
>> 6) & 0x7);
125 } else if (bundle_encoding
[template][slot
] == X
) {
126 switch (major_opcode
) {
127 case LONG_CALL_OPCODE
:
128 p
->ainsn
.inst_flag
|= INST_FLAG_FIX_BRANCH_REG
;
129 p
->ainsn
.target_br_reg
= ((kprobe_inst
>> 6) & 0x7);
137 * In this function we check to see if the instruction
138 * on which we are inserting kprobe is supported.
139 * Returns 0 if supported
140 * Returns -EINVAL if unsupported
142 static int __kprobes
unsupported_inst(uint
template, uint slot
,
144 unsigned long kprobe_inst
,
147 unsigned long addr
= (unsigned long)p
->addr
;
149 if (bundle_encoding
[template][slot
] == I
) {
150 switch (major_opcode
) {
151 case 0x0: //I_UNIT_MISC_OPCODE:
153 * Check for Integer speculation instruction
154 * - Bit 33-35 to be equal to 0x1
156 if (((kprobe_inst
>> 33) & 0x7) == 1) {
158 "Kprobes on speculation inst at <0x%lx> not supported\n",
164 * IP relative mov instruction
165 * - Bit 27-35 to be equal to 0x30
167 if (((kprobe_inst
>> 27) & 0x1FF) == 0x30) {
169 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
181 * In this function we check to see if the instruction
182 * (qp) cmpx.crel.ctype p1,p2=r2,r3
183 * on which we are inserting kprobe is cmp instruction
186 static uint __kprobes
is_cmp_ctype_unc_inst(uint
template, uint slot
,
188 unsigned long kprobe_inst
)
193 if (!((bundle_encoding
[template][slot
] == I
) ||
194 (bundle_encoding
[template][slot
] == M
)))
197 if (!((major_opcode
== 0xC) || (major_opcode
== 0xD) ||
198 (major_opcode
== 0xE)))
201 cmp_inst
.l
= kprobe_inst
;
202 if ((cmp_inst
.f
.x2
== 0) || (cmp_inst
.f
.x2
== 1)) {
203 /* Integere compare - Register Register (A6 type)*/
204 if ((cmp_inst
.f
.tb
== 0) && (cmp_inst
.f
.ta
== 0)
205 &&(cmp_inst
.f
.c
== 1))
207 } else if ((cmp_inst
.f
.x2
== 2)||(cmp_inst
.f
.x2
== 3)) {
208 /* Integere compare - Immediate Register (A8 type)*/
209 if ((cmp_inst
.f
.ta
== 0) &&(cmp_inst
.f
.c
== 1))
217 * In this function we override the bundle with
218 * the break instruction at the given slot.
220 static void __kprobes
prepare_break_inst(uint
template, uint slot
,
222 unsigned long kprobe_inst
,
225 unsigned long break_inst
= BREAK_INST
;
226 bundle_t
*bundle
= &p
->ainsn
.insn
.bundle
;
229 * Copy the original kprobe_inst qualifying predicate(qp)
230 * to the break instruction iff !is_cmp_ctype_unc_inst
231 * because for cmp instruction with ctype equal to unc,
232 * which is a special instruction always needs to be
233 * executed regradless of qp
235 if (!is_cmp_ctype_unc_inst(template, slot
, major_opcode
, kprobe_inst
))
236 break_inst
|= (0x3f & kprobe_inst
);
240 bundle
->quad0
.slot0
= break_inst
;
243 bundle
->quad0
.slot1_p0
= break_inst
;
244 bundle
->quad1
.slot1_p1
= break_inst
>> (64-46);
247 bundle
->quad1
.slot2
= break_inst
;
252 * Update the instruction flag, so that we can
253 * emulate the instruction properly after we
254 * single step on original instruction
256 update_kprobe_inst_flag(template, slot
, major_opcode
, kprobe_inst
, p
);
259 static inline void get_kprobe_inst(bundle_t
*bundle
, uint slot
,
260 unsigned long *kprobe_inst
, uint
*major_opcode
)
262 unsigned long kprobe_inst_p0
, kprobe_inst_p1
;
263 unsigned int template;
265 template = bundle
->quad0
.template;
269 *major_opcode
= (bundle
->quad0
.slot0
>> SLOT0_OPCODE_SHIFT
);
270 *kprobe_inst
= bundle
->quad0
.slot0
;
273 *major_opcode
= (bundle
->quad1
.slot1_p1
>> SLOT1_p1_OPCODE_SHIFT
);
274 kprobe_inst_p0
= bundle
->quad0
.slot1_p0
;
275 kprobe_inst_p1
= bundle
->quad1
.slot1_p1
;
276 *kprobe_inst
= kprobe_inst_p0
| (kprobe_inst_p1
<< (64-46));
279 *major_opcode
= (bundle
->quad1
.slot2
>> SLOT2_OPCODE_SHIFT
);
280 *kprobe_inst
= bundle
->quad1
.slot2
;
285 /* Returns non-zero if the addr is in the Interrupt Vector Table */
286 static inline int in_ivt_functions(unsigned long addr
)
288 return (addr
>= (unsigned long)__start_ivt_text
289 && addr
< (unsigned long)__end_ivt_text
);
292 static int __kprobes
valid_kprobe_addr(int template, int slot
,
295 if ((slot
> 2) || ((bundle_encoding
[template][1] == L
) && slot
> 1)) {
296 printk(KERN_WARNING
"Attempting to insert unaligned kprobe "
301 if (in_ivt_functions(addr
)) {
302 printk(KERN_WARNING
"Kprobes can't be inserted inside "
303 "IVT functions at 0x%lx\n", addr
);
307 if (slot
== 1 && bundle_encoding
[template][1] != L
) {
308 printk(KERN_WARNING
"Inserting kprobes on slot #1 "
309 "is not supported\n");
316 static inline void save_previous_kprobe(void)
318 kprobe_prev
= current_kprobe
;
319 kprobe_status_prev
= kprobe_status
;
322 static inline void restore_previous_kprobe(void)
324 current_kprobe
= kprobe_prev
;
325 kprobe_status
= kprobe_status_prev
;
328 static inline void set_current_kprobe(struct kprobe
*p
)
333 static void kretprobe_trampoline(void)
338 * At this point the target function has been tricked into
339 * returning into our trampoline. Lookup the associated instance
341 * - call the handler function
342 * - cleanup by marking the instance as unused
343 * - long jump back to the original return address
345 int __kprobes
trampoline_probe_handler(struct kprobe
*p
, struct pt_regs
*regs
)
347 struct kretprobe_instance
*ri
= NULL
;
348 struct hlist_head
*head
;
349 struct hlist_node
*node
, *tmp
;
350 unsigned long orig_ret_address
= 0;
351 unsigned long trampoline_address
=
352 ((struct fnptr
*)kretprobe_trampoline
)->ip
;
354 head
= kretprobe_inst_table_head(current
);
357 * It is possible to have multiple instances associated with a given
358 * task either because an multiple functions in the call path
359 * have a return probe installed on them, and/or more then one return
360 * return probe was registered for a target function.
362 * We can handle this because:
363 * - instances are always inserted at the head of the list
364 * - when multiple return probes are registered for the same
365 * function, the first instance's ret_addr will point to the
366 * real return address, and all the rest will point to
367 * kretprobe_trampoline
369 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
370 if (ri
->task
!= current
)
371 /* another task is sharing our hash bucket */
374 if (ri
->rp
&& ri
->rp
->handler
)
375 ri
->rp
->handler(ri
, regs
);
377 orig_ret_address
= (unsigned long)ri
->ret_addr
;
380 if (orig_ret_address
!= trampoline_address
)
382 * This is the real return address. Any other
383 * instances associated with this task are for
384 * other calls deeper on the call stack
389 BUG_ON(!orig_ret_address
|| (orig_ret_address
== trampoline_address
));
390 regs
->cr_iip
= orig_ret_address
;
393 preempt_enable_no_resched();
396 * By returning a non-zero value, we are telling
397 * kprobe_handler() that we have handled unlocking
398 * and re-enabling preemption.
403 void __kprobes
arch_prepare_kretprobe(struct kretprobe
*rp
,
404 struct pt_regs
*regs
)
406 struct kretprobe_instance
*ri
;
408 if ((ri
= get_free_rp_inst(rp
)) != NULL
) {
411 ri
->ret_addr
= (kprobe_opcode_t
*)regs
->b0
;
413 /* Replace the return addr with trampoline addr */
414 regs
->b0
= ((struct fnptr
*)kretprobe_trampoline
)->ip
;
422 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
424 unsigned long addr
= (unsigned long) p
->addr
;
425 unsigned long *kprobe_addr
= (unsigned long *)(addr
& ~0xFULL
);
426 unsigned long kprobe_inst
=0;
427 unsigned int slot
= addr
& 0xf, template, major_opcode
= 0;
428 bundle_t
*bundle
= &p
->ainsn
.insn
.bundle
;
430 memcpy(&p
->opcode
.bundle
, kprobe_addr
, sizeof(bundle_t
));
431 memcpy(&p
->ainsn
.insn
.bundle
, kprobe_addr
, sizeof(bundle_t
));
433 template = bundle
->quad0
.template;
435 if(valid_kprobe_addr(template, slot
, addr
))
438 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
439 if (slot
== 1 && bundle_encoding
[template][1] == L
)
442 /* Get kprobe_inst and major_opcode from the bundle */
443 get_kprobe_inst(bundle
, slot
, &kprobe_inst
, &major_opcode
);
445 if (unsupported_inst(template, slot
, major_opcode
, kprobe_inst
, p
))
448 prepare_break_inst(template, slot
, major_opcode
, kprobe_inst
, p
);
453 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
455 unsigned long addr
= (unsigned long)p
->addr
;
456 unsigned long arm_addr
= addr
& ~0xFULL
;
458 memcpy((char *)arm_addr
, &p
->ainsn
.insn
.bundle
, sizeof(bundle_t
));
459 flush_icache_range(arm_addr
, arm_addr
+ sizeof(bundle_t
));
462 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
464 unsigned long addr
= (unsigned long)p
->addr
;
465 unsigned long arm_addr
= addr
& ~0xFULL
;
467 /* p->opcode contains the original unaltered bundle */
468 memcpy((char *) arm_addr
, (char *) &p
->opcode
.bundle
, sizeof(bundle_t
));
469 flush_icache_range(arm_addr
, arm_addr
+ sizeof(bundle_t
));
472 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
477 * We are resuming execution after a single step fault, so the pt_regs
478 * structure reflects the register state after we executed the instruction
479 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
480 * the ip to point back to the original stack address. To set the IP address
481 * to original stack address, handle the case where we need to fixup the
482 * relative IP address and/or fixup branch register.
484 static void __kprobes
resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
486 unsigned long bundle_addr
= ((unsigned long) (&p
->opcode
.bundle
)) & ~0xFULL
;
487 unsigned long resume_addr
= (unsigned long)p
->addr
& ~0xFULL
;
488 unsigned long template;
489 int slot
= ((unsigned long)p
->addr
& 0xf);
491 template = p
->opcode
.bundle
.quad0
.template;
493 if (slot
== 1 && bundle_encoding
[template][1] == L
)
496 if (p
->ainsn
.inst_flag
) {
498 if (p
->ainsn
.inst_flag
& INST_FLAG_FIX_RELATIVE_IP_ADDR
) {
499 /* Fix relative IP address */
500 regs
->cr_iip
= (regs
->cr_iip
- bundle_addr
) + resume_addr
;
503 if (p
->ainsn
.inst_flag
& INST_FLAG_FIX_BRANCH_REG
) {
505 * Fix target branch register, software convention is
506 * to use either b0 or b6 or b7, so just checking
507 * only those registers
509 switch (p
->ainsn
.target_br_reg
) {
511 if ((regs
->b0
== bundle_addr
) ||
512 (regs
->b0
== bundle_addr
+ 0x10)) {
513 regs
->b0
= (regs
->b0
- bundle_addr
) +
518 if ((regs
->b6
== bundle_addr
) ||
519 (regs
->b6
== bundle_addr
+ 0x10)) {
520 regs
->b6
= (regs
->b6
- bundle_addr
) +
525 if ((regs
->b7
== bundle_addr
) ||
526 (regs
->b7
== bundle_addr
+ 0x10)) {
527 regs
->b7
= (regs
->b7
- bundle_addr
) +
537 if (regs
->cr_iip
== bundle_addr
+ 0x10) {
538 regs
->cr_iip
= resume_addr
+ 0x10;
541 if (regs
->cr_iip
== bundle_addr
) {
542 regs
->cr_iip
= resume_addr
;
547 /* Turn off Single Step bit */
548 ia64_psr(regs
)->ss
= 0;
551 static void __kprobes
prepare_ss(struct kprobe
*p
, struct pt_regs
*regs
)
553 unsigned long bundle_addr
= (unsigned long) &p
->opcode
.bundle
;
554 unsigned long slot
= (unsigned long)p
->addr
& 0xf;
556 /* single step inline if break instruction */
557 if (p
->ainsn
.inst_flag
== INST_FLAG_BREAK_INST
)
558 regs
->cr_iip
= (unsigned long)p
->addr
& ~0xFULL
;
560 regs
->cr_iip
= bundle_addr
& ~0xFULL
;
565 ia64_psr(regs
)->ri
= slot
;
567 /* turn on single stepping */
568 ia64_psr(regs
)->ss
= 1;
571 static int __kprobes
is_ia64_break_inst(struct pt_regs
*regs
)
573 unsigned int slot
= ia64_psr(regs
)->ri
;
574 unsigned int template, major_opcode
;
575 unsigned long kprobe_inst
;
576 unsigned long *kprobe_addr
= (unsigned long *)regs
->cr_iip
;
579 memcpy(&bundle
, kprobe_addr
, sizeof(bundle_t
));
580 template = bundle
.quad0
.template;
582 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
583 if (slot
== 1 && bundle_encoding
[template][1] == L
)
586 /* Get Kprobe probe instruction at given slot*/
587 get_kprobe_inst(&bundle
, slot
, &kprobe_inst
, &major_opcode
);
589 /* For break instruction,
590 * Bits 37:40 Major opcode to be zero
591 * Bits 27:32 X6 to be zero
592 * Bits 32:35 X3 to be zero
594 if (major_opcode
|| ((kprobe_inst
>> 27) & 0x1FF) ) {
595 /* Not a break instruction */
599 /* Is a break instruction */
603 static int __kprobes
pre_kprobes_handler(struct die_args
*args
)
607 struct pt_regs
*regs
= args
->regs
;
608 kprobe_opcode_t
*addr
= (kprobe_opcode_t
*)instruction_pointer(regs
);
612 /* Handle recursion cases */
613 if (kprobe_running()) {
614 p
= get_kprobe(addr
);
616 if ( (kprobe_status
== KPROBE_HIT_SS
) &&
617 (p
->ainsn
.inst_flag
== INST_FLAG_BREAK_INST
)) {
618 ia64_psr(regs
)->ss
= 0;
622 /* We have reentered the pre_kprobe_handler(), since
623 * another probe was hit while within the handler.
624 * We here save the original kprobes variables and
625 * just single step on the instruction of the new probe
626 * without calling any user handlers.
628 save_previous_kprobe();
629 set_current_kprobe(p
);
632 kprobe_status
= KPROBE_REENTER
;
634 } else if (args
->err
== __IA64_BREAK_JPROBE
) {
636 * jprobe instrumented function just completed
639 if (p
->break_handler
&& p
->break_handler(p
, regs
)) {
649 p
= get_kprobe(addr
);
652 if (!is_ia64_break_inst(regs
)) {
654 * The breakpoint instruction was removed right
655 * after we hit it. Another cpu has removed
656 * either a probepoint or a debugger breakpoint
657 * at this address. In either case, no further
658 * handling of this interrupt is appropriate.
664 /* Not one of our break, let kernel handle it */
668 kprobe_status
= KPROBE_HIT_ACTIVE
;
669 set_current_kprobe(p
);
671 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
673 * Our pre-handler is specifically requesting that we just
674 * do a return. This is used for both the jprobe pre-handler
675 * and the kretprobe trampoline
681 kprobe_status
= KPROBE_HIT_SS
;
685 preempt_enable_no_resched();
689 static int __kprobes
post_kprobes_handler(struct pt_regs
*regs
)
691 if (!kprobe_running())
694 if ((kprobe_status
!= KPROBE_REENTER
) && current_kprobe
->post_handler
) {
695 kprobe_status
= KPROBE_HIT_SSDONE
;
696 current_kprobe
->post_handler(current_kprobe
, regs
, 0);
699 resume_execution(current_kprobe
, regs
);
701 /*Restore back the original saved kprobes variables and continue. */
702 if (kprobe_status
== KPROBE_REENTER
) {
703 restore_previous_kprobe();
710 preempt_enable_no_resched();
714 static int __kprobes
kprobes_fault_handler(struct pt_regs
*regs
, int trapnr
)
716 if (!kprobe_running())
719 if (current_kprobe
->fault_handler
&&
720 current_kprobe
->fault_handler(current_kprobe
, regs
, trapnr
))
723 if (kprobe_status
& KPROBE_HIT_SS
) {
724 resume_execution(current_kprobe
, regs
);
726 preempt_enable_no_resched();
732 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
733 unsigned long val
, void *data
)
735 struct die_args
*args
= (struct die_args
*)data
;
738 if (pre_kprobes_handler(args
))
742 if (post_kprobes_handler(args
->regs
))
746 if (kprobes_fault_handler(args
->regs
, args
->trapnr
))
754 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
756 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
757 unsigned long addr
= ((struct fnptr
*)(jp
->entry
))->ip
;
759 /* save architectural state */
760 jprobe_saved_regs
= *regs
;
762 /* after rfi, execute the jprobe instrumented function */
763 regs
->cr_iip
= addr
& ~0xFULL
;
764 ia64_psr(regs
)->ri
= addr
& 0xf;
765 regs
->r1
= ((struct fnptr
*)(jp
->entry
))->gp
;
768 * fix the return address to our jprobe_inst_return() function
769 * in the jprobes.S file
771 regs
->b0
= ((struct fnptr
*)(jprobe_inst_return
))->ip
;
776 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
778 *regs
= jprobe_saved_regs
;
782 static struct kprobe trampoline_p
= {
783 .pre_handler
= trampoline_probe_handler
786 int __init
arch_init_kprobes(void)
789 (kprobe_opcode_t
*)((struct fnptr
*)kretprobe_trampoline
)->ip
;
790 return register_kprobe(&trampoline_p
);