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 update_kprobe_inst_flag(uint template, uint slot, uint major_opcode,
91 unsigned long kprobe_inst, struct kprobe *p)
93 p->ainsn.inst_flag = 0;
94 p->ainsn.target_br_reg = 0;
96 if (bundle_encoding[template][slot] == B) {
97 switch (major_opcode) {
98 case INDIRECT_CALL_OPCODE:
99 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
100 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
102 case IP_RELATIVE_PREDICT_OPCODE:
103 case IP_RELATIVE_BRANCH_OPCODE:
104 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
106 case IP_RELATIVE_CALL_OPCODE:
107 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
108 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
109 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
112 } else if (bundle_encoding[template][slot] == X) {
113 switch (major_opcode) {
114 case LONG_CALL_OPCODE:
115 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
116 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
124 * In this function we check to see if the instruction
125 * on which we are inserting kprobe is supported.
126 * Returns 0 if supported
127 * Returns -EINVAL if unsupported
129 static int unsupported_inst(uint template, uint slot, uint major_opcode,
130 unsigned long kprobe_inst, struct kprobe *p)
132 unsigned long addr = (unsigned long)p->addr;
134 if (bundle_encoding[template][slot] == I) {
135 switch (major_opcode) {
136 case 0x0: //I_UNIT_MISC_OPCODE:
138 * Check for Integer speculation instruction
139 * - Bit 33-35 to be equal to 0x1
141 if (((kprobe_inst >> 33) & 0x7) == 1) {
143 "Kprobes on speculation inst at <0x%lx> not supported\n",
149 * IP relative mov instruction
150 * - Bit 27-35 to be equal to 0x30
152 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
154 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
166 * In this function we check to see if the instruction
167 * (qp) cmpx.crel.ctype p1,p2=r2,r3
168 * on which we are inserting kprobe is cmp instruction
171 static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
172 unsigned long kprobe_inst)
177 if (!((bundle_encoding[template][slot] == I) ||
178 (bundle_encoding[template][slot] == M)))
181 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
182 (major_opcode == 0xE)))
185 cmp_inst.l = kprobe_inst;
186 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
187 /* Integere compare - Register Register (A6 type)*/
188 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
189 &&(cmp_inst.f.c == 1))
191 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
192 /* Integere compare - Immediate Register (A8 type)*/
193 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
201 * In this function we override the bundle with
202 * the break instruction at the given slot.
204 static void prepare_break_inst(uint template, uint slot, uint major_opcode,
205 unsigned long kprobe_inst, struct kprobe *p)
207 unsigned long break_inst = BREAK_INST;
208 bundle_t *bundle = &p->ainsn.insn.bundle;
211 * Copy the original kprobe_inst qualifying predicate(qp)
212 * to the break instruction iff !is_cmp_ctype_unc_inst
213 * because for cmp instruction with ctype equal to unc,
214 * which is a special instruction always needs to be
215 * executed regradless of qp
217 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
218 break_inst |= (0x3f & kprobe_inst);
222 bundle->quad0.slot0 = break_inst;
225 bundle->quad0.slot1_p0 = break_inst;
226 bundle->quad1.slot1_p1 = break_inst >> (64-46);
229 bundle->quad1.slot2 = break_inst;
234 * Update the instruction flag, so that we can
235 * emulate the instruction properly after we
236 * single step on original instruction
238 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
241 static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
242 unsigned long *kprobe_inst, uint *major_opcode)
244 unsigned long kprobe_inst_p0, kprobe_inst_p1;
245 unsigned int template;
247 template = bundle->quad0.template;
251 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
252 *kprobe_inst = bundle->quad0.slot0;
255 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
256 kprobe_inst_p0 = bundle->quad0.slot1_p0;
257 kprobe_inst_p1 = bundle->quad1.slot1_p1;
258 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
261 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
262 *kprobe_inst = bundle->quad1.slot2;
267 /* Returns non-zero if the addr is in the Interrupt Vector Table */
268 static inline int in_ivt_functions(unsigned long addr)
270 return (addr >= (unsigned long)__start_ivt_text
271 && addr < (unsigned long)__end_ivt_text);
274 static int valid_kprobe_addr(int template, int slot, unsigned long addr)
276 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
277 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
282 if (in_ivt_functions(addr)) {
283 printk(KERN_WARNING "Kprobes can't be inserted inside "
284 "IVT functions at 0x%lx\n", addr);
288 if (slot == 1 && bundle_encoding[template][1] != L) {
289 printk(KERN_WARNING "Inserting kprobes on slot #1 "
290 "is not supported\n");
297 static inline void save_previous_kprobe(void)
299 kprobe_prev = current_kprobe;
300 kprobe_status_prev = kprobe_status;
303 static inline void restore_previous_kprobe(void)
305 current_kprobe = kprobe_prev;
306 kprobe_status = kprobe_status_prev;
309 static inline void set_current_kprobe(struct kprobe *p)
314 static void kretprobe_trampoline(void)
319 * At this point the target function has been tricked into
320 * returning into our trampoline. Lookup the associated instance
322 * - call the handler function
323 * - cleanup by marking the instance as unused
324 * - long jump back to the original return address
326 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
328 struct kretprobe_instance *ri = NULL;
329 struct hlist_head *head;
330 struct hlist_node *node, *tmp;
331 unsigned long orig_ret_address = 0;
332 unsigned long trampoline_address =
333 ((struct fnptr *)kretprobe_trampoline)->ip;
335 head = kretprobe_inst_table_head(current);
338 * It is possible to have multiple instances associated with a given
339 * task either because an multiple functions in the call path
340 * have a return probe installed on them, and/or more then one return
341 * return probe was registered for a target function.
343 * We can handle this because:
344 * - instances are always inserted at the head of the list
345 * - when multiple return probes are registered for the same
346 * function, the first instance's ret_addr will point to the
347 * real return address, and all the rest will point to
348 * kretprobe_trampoline
350 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
351 if (ri->task != current)
352 /* another task is sharing our hash bucket */
355 if (ri->rp && ri->rp->handler)
356 ri->rp->handler(ri, regs);
358 orig_ret_address = (unsigned long)ri->ret_addr;
361 if (orig_ret_address != trampoline_address)
363 * This is the real return address. Any other
364 * instances associated with this task are for
365 * other calls deeper on the call stack
370 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
371 regs->cr_iip = orig_ret_address;
374 preempt_enable_no_resched();
377 * By returning a non-zero value, we are telling
378 * kprobe_handler() that we have handled unlocking
379 * and re-enabling preemption.
384 void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
386 struct kretprobe_instance *ri;
388 if ((ri = get_free_rp_inst(rp)) != NULL) {
391 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
393 /* Replace the return addr with trampoline addr */
394 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
402 int arch_prepare_kprobe(struct kprobe *p)
404 unsigned long addr = (unsigned long) p->addr;
405 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
406 unsigned long kprobe_inst=0;
407 unsigned int slot = addr & 0xf, template, major_opcode = 0;
408 bundle_t *bundle = &p->ainsn.insn.bundle;
410 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
411 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
413 template = bundle->quad0.template;
415 if(valid_kprobe_addr(template, slot, addr))
418 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
419 if (slot == 1 && bundle_encoding[template][1] == L)
422 /* Get kprobe_inst and major_opcode from the bundle */
423 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
425 if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
428 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
433 void arch_arm_kprobe(struct kprobe *p)
435 unsigned long addr = (unsigned long)p->addr;
436 unsigned long arm_addr = addr & ~0xFULL;
438 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
439 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
442 void arch_disarm_kprobe(struct kprobe *p)
444 unsigned long addr = (unsigned long)p->addr;
445 unsigned long arm_addr = addr & ~0xFULL;
447 /* p->opcode contains the original unaltered bundle */
448 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
449 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
452 void arch_remove_kprobe(struct kprobe *p)
457 * We are resuming execution after a single step fault, so the pt_regs
458 * structure reflects the register state after we executed the instruction
459 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
460 * the ip to point back to the original stack address. To set the IP address
461 * to original stack address, handle the case where we need to fixup the
462 * relative IP address and/or fixup branch register.
464 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
466 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
467 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
468 unsigned long template;
469 int slot = ((unsigned long)p->addr & 0xf);
471 template = p->opcode.bundle.quad0.template;
473 if (slot == 1 && bundle_encoding[template][1] == L)
476 if (p->ainsn.inst_flag) {
478 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
479 /* Fix relative IP address */
480 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
483 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
485 * Fix target branch register, software convention is
486 * to use either b0 or b6 or b7, so just checking
487 * only those registers
489 switch (p->ainsn.target_br_reg) {
491 if ((regs->b0 == bundle_addr) ||
492 (regs->b0 == bundle_addr + 0x10)) {
493 regs->b0 = (regs->b0 - bundle_addr) +
498 if ((regs->b6 == bundle_addr) ||
499 (regs->b6 == bundle_addr + 0x10)) {
500 regs->b6 = (regs->b6 - bundle_addr) +
505 if ((regs->b7 == bundle_addr) ||
506 (regs->b7 == bundle_addr + 0x10)) {
507 regs->b7 = (regs->b7 - bundle_addr) +
517 if (regs->cr_iip == bundle_addr + 0x10) {
518 regs->cr_iip = resume_addr + 0x10;
521 if (regs->cr_iip == bundle_addr) {
522 regs->cr_iip = resume_addr;
527 /* Turn off Single Step bit */
528 ia64_psr(regs)->ss = 0;
531 static void prepare_ss(struct kprobe *p, struct pt_regs *regs)
533 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
534 unsigned long slot = (unsigned long)p->addr & 0xf;
536 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
537 regs->cr_iip = bundle_addr & ~0xFULL;
542 ia64_psr(regs)->ri = slot;
544 /* turn on single stepping */
545 ia64_psr(regs)->ss = 1;
548 static int pre_kprobes_handler(struct die_args *args)
552 struct pt_regs *regs = args->regs;
553 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
557 /* Handle recursion cases */
558 if (kprobe_running()) {
559 p = get_kprobe(addr);
561 if (kprobe_status == KPROBE_HIT_SS) {
565 /* We have reentered the pre_kprobe_handler(), since
566 * another probe was hit while within the handler.
567 * We here save the original kprobes variables and
568 * just single step on the instruction of the new probe
569 * without calling any user handlers.
571 save_previous_kprobe();
572 set_current_kprobe(p);
575 kprobe_status = KPROBE_REENTER;
577 } else if (args->err == __IA64_BREAK_JPROBE) {
579 * jprobe instrumented function just completed
582 if (p->break_handler && p->break_handler(p, regs)) {
592 p = get_kprobe(addr);
598 kprobe_status = KPROBE_HIT_ACTIVE;
599 set_current_kprobe(p);
601 if (p->pre_handler && p->pre_handler(p, regs))
603 * Our pre-handler is specifically requesting that we just
604 * do a return. This is used for both the jprobe pre-handler
605 * and the kretprobe trampoline
611 kprobe_status = KPROBE_HIT_SS;
615 preempt_enable_no_resched();
619 static int post_kprobes_handler(struct pt_regs *regs)
621 if (!kprobe_running())
624 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
625 kprobe_status = KPROBE_HIT_SSDONE;
626 current_kprobe->post_handler(current_kprobe, regs, 0);
629 resume_execution(current_kprobe, regs);
631 /*Restore back the original saved kprobes variables and continue. */
632 if (kprobe_status == KPROBE_REENTER) {
633 restore_previous_kprobe();
640 preempt_enable_no_resched();
644 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
646 if (!kprobe_running())
649 if (current_kprobe->fault_handler &&
650 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
653 if (kprobe_status & KPROBE_HIT_SS) {
654 resume_execution(current_kprobe, regs);
656 preempt_enable_no_resched();
662 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
665 struct die_args *args = (struct die_args *)data;
668 if (pre_kprobes_handler(args))
672 if (post_kprobes_handler(args->regs))
676 if (kprobes_fault_handler(args->regs, args->trapnr))
684 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
686 struct jprobe *jp = container_of(p, struct jprobe, kp);
687 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
689 /* save architectural state */
690 jprobe_saved_regs = *regs;
692 /* after rfi, execute the jprobe instrumented function */
693 regs->cr_iip = addr & ~0xFULL;
694 ia64_psr(regs)->ri = addr & 0xf;
695 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
698 * fix the return address to our jprobe_inst_return() function
699 * in the jprobes.S file
701 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
706 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
708 *regs = jprobe_saved_regs;
712 static struct kprobe trampoline_p = {
713 .pre_handler = trampoline_probe_handler
716 int __init arch_init_kprobes(void)
719 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
720 return register_kprobe(&trampoline_p);