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 if (bundle_encoding[template][slot] == B) {
99 switch (major_opcode) {
100 case INDIRECT_CALL_OPCODE:
101 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
102 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
104 case IP_RELATIVE_PREDICT_OPCODE:
105 case IP_RELATIVE_BRANCH_OPCODE:
106 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
108 case IP_RELATIVE_CALL_OPCODE:
109 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
110 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
111 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
114 } else if (bundle_encoding[template][slot] == X) {
115 switch (major_opcode) {
116 case LONG_CALL_OPCODE:
117 p->ainsn.inst_flag |= INST_FLAG_FIX_BRANCH_REG;
118 p->ainsn.target_br_reg = ((kprobe_inst >> 6) & 0x7);
126 * In this function we check to see if the instruction
127 * on which we are inserting kprobe is supported.
128 * Returns 0 if supported
129 * Returns -EINVAL if unsupported
131 static int __kprobes unsupported_inst(uint template, uint slot,
133 unsigned long kprobe_inst,
136 unsigned long addr = (unsigned long)p->addr;
138 if (bundle_encoding[template][slot] == I) {
139 switch (major_opcode) {
140 case 0x0: //I_UNIT_MISC_OPCODE:
142 * Check for Integer speculation instruction
143 * - Bit 33-35 to be equal to 0x1
145 if (((kprobe_inst >> 33) & 0x7) == 1) {
147 "Kprobes on speculation inst at <0x%lx> not supported\n",
153 * IP relative mov instruction
154 * - Bit 27-35 to be equal to 0x30
156 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
158 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
170 * In this function we check to see if the instruction
171 * (qp) cmpx.crel.ctype p1,p2=r2,r3
172 * on which we are inserting kprobe is cmp instruction
175 static uint __kprobes is_cmp_ctype_unc_inst(uint template, uint slot,
177 unsigned long kprobe_inst)
182 if (!((bundle_encoding[template][slot] == I) ||
183 (bundle_encoding[template][slot] == M)))
186 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
187 (major_opcode == 0xE)))
190 cmp_inst.l = kprobe_inst;
191 if ((cmp_inst.f.x2 == 0) || (cmp_inst.f.x2 == 1)) {
192 /* Integere compare - Register Register (A6 type)*/
193 if ((cmp_inst.f.tb == 0) && (cmp_inst.f.ta == 0)
194 &&(cmp_inst.f.c == 1))
196 } else if ((cmp_inst.f.x2 == 2)||(cmp_inst.f.x2 == 3)) {
197 /* Integere compare - Immediate Register (A8 type)*/
198 if ((cmp_inst.f.ta == 0) &&(cmp_inst.f.c == 1))
206 * In this function we override the bundle with
207 * the break instruction at the given slot.
209 static void __kprobes prepare_break_inst(uint template, uint slot,
211 unsigned long kprobe_inst,
214 unsigned long break_inst = BREAK_INST;
215 bundle_t *bundle = &p->ainsn.insn.bundle;
218 * Copy the original kprobe_inst qualifying predicate(qp)
219 * to the break instruction iff !is_cmp_ctype_unc_inst
220 * because for cmp instruction with ctype equal to unc,
221 * which is a special instruction always needs to be
222 * executed regradless of qp
224 if (!is_cmp_ctype_unc_inst(template, slot, major_opcode, kprobe_inst))
225 break_inst |= (0x3f & kprobe_inst);
229 bundle->quad0.slot0 = break_inst;
232 bundle->quad0.slot1_p0 = break_inst;
233 bundle->quad1.slot1_p1 = break_inst >> (64-46);
236 bundle->quad1.slot2 = break_inst;
241 * Update the instruction flag, so that we can
242 * emulate the instruction properly after we
243 * single step on original instruction
245 update_kprobe_inst_flag(template, slot, major_opcode, kprobe_inst, p);
248 static inline void get_kprobe_inst(bundle_t *bundle, uint slot,
249 unsigned long *kprobe_inst, uint *major_opcode)
251 unsigned long kprobe_inst_p0, kprobe_inst_p1;
252 unsigned int template;
254 template = bundle->quad0.template;
258 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
259 *kprobe_inst = bundle->quad0.slot0;
262 *major_opcode = (bundle->quad1.slot1_p1 >> SLOT1_p1_OPCODE_SHIFT);
263 kprobe_inst_p0 = bundle->quad0.slot1_p0;
264 kprobe_inst_p1 = bundle->quad1.slot1_p1;
265 *kprobe_inst = kprobe_inst_p0 | (kprobe_inst_p1 << (64-46));
268 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
269 *kprobe_inst = bundle->quad1.slot2;
274 /* Returns non-zero if the addr is in the Interrupt Vector Table */
275 static inline int in_ivt_functions(unsigned long addr)
277 return (addr >= (unsigned long)__start_ivt_text
278 && addr < (unsigned long)__end_ivt_text);
281 static int __kprobes valid_kprobe_addr(int template, int slot,
284 if ((slot > 2) || ((bundle_encoding[template][1] == L) && slot > 1)) {
285 printk(KERN_WARNING "Attempting to insert unaligned kprobe "
290 if (in_ivt_functions(addr)) {
291 printk(KERN_WARNING "Kprobes can't be inserted inside "
292 "IVT functions at 0x%lx\n", addr);
296 if (slot == 1 && bundle_encoding[template][1] != L) {
297 printk(KERN_WARNING "Inserting kprobes on slot #1 "
298 "is not supported\n");
305 static inline void save_previous_kprobe(void)
307 kprobe_prev = current_kprobe;
308 kprobe_status_prev = kprobe_status;
311 static inline void restore_previous_kprobe(void)
313 current_kprobe = kprobe_prev;
314 kprobe_status = kprobe_status_prev;
317 static inline void set_current_kprobe(struct kprobe *p)
322 static void kretprobe_trampoline(void)
327 * At this point the target function has been tricked into
328 * returning into our trampoline. Lookup the associated instance
330 * - call the handler function
331 * - cleanup by marking the instance as unused
332 * - long jump back to the original return address
334 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
336 struct kretprobe_instance *ri = NULL;
337 struct hlist_head *head;
338 struct hlist_node *node, *tmp;
339 unsigned long orig_ret_address = 0;
340 unsigned long trampoline_address =
341 ((struct fnptr *)kretprobe_trampoline)->ip;
343 head = kretprobe_inst_table_head(current);
346 * It is possible to have multiple instances associated with a given
347 * task either because an multiple functions in the call path
348 * have a return probe installed on them, and/or more then one return
349 * return probe was registered for a target function.
351 * We can handle this because:
352 * - instances are always inserted at the head of the list
353 * - when multiple return probes are registered for the same
354 * function, the first instance's ret_addr will point to the
355 * real return address, and all the rest will point to
356 * kretprobe_trampoline
358 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
359 if (ri->task != current)
360 /* another task is sharing our hash bucket */
363 if (ri->rp && ri->rp->handler)
364 ri->rp->handler(ri, regs);
366 orig_ret_address = (unsigned long)ri->ret_addr;
369 if (orig_ret_address != trampoline_address)
371 * This is the real return address. Any other
372 * instances associated with this task are for
373 * other calls deeper on the call stack
378 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
379 regs->cr_iip = orig_ret_address;
382 preempt_enable_no_resched();
385 * By returning a non-zero value, we are telling
386 * kprobe_handler() that we have handled unlocking
387 * and re-enabling preemption.
392 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
393 struct pt_regs *regs)
395 struct kretprobe_instance *ri;
397 if ((ri = get_free_rp_inst(rp)) != NULL) {
400 ri->ret_addr = (kprobe_opcode_t *)regs->b0;
402 /* Replace the return addr with trampoline addr */
403 regs->b0 = ((struct fnptr *)kretprobe_trampoline)->ip;
411 int __kprobes arch_prepare_kprobe(struct kprobe *p)
413 unsigned long addr = (unsigned long) p->addr;
414 unsigned long *kprobe_addr = (unsigned long *)(addr & ~0xFULL);
415 unsigned long kprobe_inst=0;
416 unsigned int slot = addr & 0xf, template, major_opcode = 0;
417 bundle_t *bundle = &p->ainsn.insn.bundle;
419 memcpy(&p->opcode.bundle, kprobe_addr, sizeof(bundle_t));
420 memcpy(&p->ainsn.insn.bundle, kprobe_addr, sizeof(bundle_t));
422 template = bundle->quad0.template;
424 if(valid_kprobe_addr(template, slot, addr))
427 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
428 if (slot == 1 && bundle_encoding[template][1] == L)
431 /* Get kprobe_inst and major_opcode from the bundle */
432 get_kprobe_inst(bundle, slot, &kprobe_inst, &major_opcode);
434 if (unsupported_inst(template, slot, major_opcode, kprobe_inst, p))
437 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
442 void __kprobes arch_arm_kprobe(struct kprobe *p)
444 unsigned long addr = (unsigned long)p->addr;
445 unsigned long arm_addr = addr & ~0xFULL;
447 memcpy((char *)arm_addr, &p->ainsn.insn.bundle, sizeof(bundle_t));
448 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
451 void __kprobes arch_disarm_kprobe(struct kprobe *p)
453 unsigned long addr = (unsigned long)p->addr;
454 unsigned long arm_addr = addr & ~0xFULL;
456 /* p->opcode contains the original unaltered bundle */
457 memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t));
458 flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t));
461 void __kprobes arch_remove_kprobe(struct kprobe *p)
466 * We are resuming execution after a single step fault, so the pt_regs
467 * structure reflects the register state after we executed the instruction
468 * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust
469 * the ip to point back to the original stack address. To set the IP address
470 * to original stack address, handle the case where we need to fixup the
471 * relative IP address and/or fixup branch register.
473 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
475 unsigned long bundle_addr = ((unsigned long) (&p->opcode.bundle)) & ~0xFULL;
476 unsigned long resume_addr = (unsigned long)p->addr & ~0xFULL;
477 unsigned long template;
478 int slot = ((unsigned long)p->addr & 0xf);
480 template = p->opcode.bundle.quad0.template;
482 if (slot == 1 && bundle_encoding[template][1] == L)
485 if (p->ainsn.inst_flag) {
487 if (p->ainsn.inst_flag & INST_FLAG_FIX_RELATIVE_IP_ADDR) {
488 /* Fix relative IP address */
489 regs->cr_iip = (regs->cr_iip - bundle_addr) + resume_addr;
492 if (p->ainsn.inst_flag & INST_FLAG_FIX_BRANCH_REG) {
494 * Fix target branch register, software convention is
495 * to use either b0 or b6 or b7, so just checking
496 * only those registers
498 switch (p->ainsn.target_br_reg) {
500 if ((regs->b0 == bundle_addr) ||
501 (regs->b0 == bundle_addr + 0x10)) {
502 regs->b0 = (regs->b0 - bundle_addr) +
507 if ((regs->b6 == bundle_addr) ||
508 (regs->b6 == bundle_addr + 0x10)) {
509 regs->b6 = (regs->b6 - bundle_addr) +
514 if ((regs->b7 == bundle_addr) ||
515 (regs->b7 == bundle_addr + 0x10)) {
516 regs->b7 = (regs->b7 - bundle_addr) +
526 if (regs->cr_iip == bundle_addr + 0x10) {
527 regs->cr_iip = resume_addr + 0x10;
530 if (regs->cr_iip == bundle_addr) {
531 regs->cr_iip = resume_addr;
536 /* Turn off Single Step bit */
537 ia64_psr(regs)->ss = 0;
540 static void __kprobes prepare_ss(struct kprobe *p, struct pt_regs *regs)
542 unsigned long bundle_addr = (unsigned long) &p->opcode.bundle;
543 unsigned long slot = (unsigned long)p->addr & 0xf;
545 /* Update instruction pointer (IIP) and slot number (IPSR.ri) */
546 regs->cr_iip = bundle_addr & ~0xFULL;
551 ia64_psr(regs)->ri = slot;
553 /* turn on single stepping */
554 ia64_psr(regs)->ss = 1;
557 static int __kprobes pre_kprobes_handler(struct die_args *args)
561 struct pt_regs *regs = args->regs;
562 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
566 /* Handle recursion cases */
567 if (kprobe_running()) {
568 p = get_kprobe(addr);
570 if (kprobe_status == KPROBE_HIT_SS) {
574 /* We have reentered the pre_kprobe_handler(), since
575 * another probe was hit while within the handler.
576 * We here save the original kprobes variables and
577 * just single step on the instruction of the new probe
578 * without calling any user handlers.
580 save_previous_kprobe();
581 set_current_kprobe(p);
584 kprobe_status = KPROBE_REENTER;
586 } else if (args->err == __IA64_BREAK_JPROBE) {
588 * jprobe instrumented function just completed
591 if (p->break_handler && p->break_handler(p, regs)) {
601 p = get_kprobe(addr);
607 kprobe_status = KPROBE_HIT_ACTIVE;
608 set_current_kprobe(p);
610 if (p->pre_handler && p->pre_handler(p, regs))
612 * Our pre-handler is specifically requesting that we just
613 * do a return. This is used for both the jprobe pre-handler
614 * and the kretprobe trampoline
620 kprobe_status = KPROBE_HIT_SS;
624 preempt_enable_no_resched();
628 static int __kprobes post_kprobes_handler(struct pt_regs *regs)
630 if (!kprobe_running())
633 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
634 kprobe_status = KPROBE_HIT_SSDONE;
635 current_kprobe->post_handler(current_kprobe, regs, 0);
638 resume_execution(current_kprobe, regs);
640 /*Restore back the original saved kprobes variables and continue. */
641 if (kprobe_status == KPROBE_REENTER) {
642 restore_previous_kprobe();
649 preempt_enable_no_resched();
653 static int __kprobes kprobes_fault_handler(struct pt_regs *regs, int trapnr)
655 if (!kprobe_running())
658 if (current_kprobe->fault_handler &&
659 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
662 if (kprobe_status & KPROBE_HIT_SS) {
663 resume_execution(current_kprobe, regs);
665 preempt_enable_no_resched();
671 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
672 unsigned long val, void *data)
674 struct die_args *args = (struct die_args *)data;
677 if (pre_kprobes_handler(args))
681 if (post_kprobes_handler(args->regs))
685 if (kprobes_fault_handler(args->regs, args->trapnr))
693 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
695 struct jprobe *jp = container_of(p, struct jprobe, kp);
696 unsigned long addr = ((struct fnptr *)(jp->entry))->ip;
698 /* save architectural state */
699 jprobe_saved_regs = *regs;
701 /* after rfi, execute the jprobe instrumented function */
702 regs->cr_iip = addr & ~0xFULL;
703 ia64_psr(regs)->ri = addr & 0xf;
704 regs->r1 = ((struct fnptr *)(jp->entry))->gp;
707 * fix the return address to our jprobe_inst_return() function
708 * in the jprobes.S file
710 regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip;
715 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
717 *regs = jprobe_saved_regs;
721 static struct kprobe trampoline_p = {
722 .pre_handler = trampoline_probe_handler
725 int __init arch_init_kprobes(void)
728 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
729 return register_kprobe(&trampoline_p);