[PATCH] hwmon: hwmon vs i2c, second round (05/11)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ia64 / kernel / kprobes.c
blob884f5cd27d8a656856a46342f616d4a198ec25c6
1 /*
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] = {
51 { M, I, I }, /* 00 */
52 { M, I, I }, /* 01 */
53 { M, I, I }, /* 02 */
54 { M, I, I }, /* 03 */
55 { M, L, X }, /* 04 */
56 { M, L, X }, /* 05 */
57 { u, u, u }, /* 06 */
58 { u, u, u }, /* 07 */
59 { M, M, I }, /* 08 */
60 { M, M, I }, /* 09 */
61 { M, M, I }, /* 0A */
62 { M, M, I }, /* 0B */
63 { M, F, I }, /* 0C */
64 { M, F, I }, /* 0D */
65 { M, M, F }, /* 0E */
66 { M, M, F }, /* 0F */
67 { M, I, B }, /* 10 */
68 { M, I, B }, /* 11 */
69 { M, B, B }, /* 12 */
70 { M, B, B }, /* 13 */
71 { u, u, u }, /* 14 */
72 { u, u, u }, /* 15 */
73 { B, B, B }, /* 16 */
74 { B, B, B }, /* 17 */
75 { M, M, B }, /* 18 */
76 { M, M, B }, /* 19 */
77 { u, u, u }, /* 1A */
78 { u, u, u }, /* 1B */
79 { M, F, B }, /* 1C */
80 { M, F, B }, /* 1D */
81 { u, u, u }, /* 1E */
82 { u, u, u }, /* 1F */
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);
101 break;
102 case IP_RELATIVE_PREDICT_OPCODE:
103 case IP_RELATIVE_BRANCH_OPCODE:
104 p->ainsn.inst_flag |= INST_FLAG_FIX_RELATIVE_IP_ADDR;
105 break;
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);
110 break;
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);
117 break;
120 return;
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) {
142 printk(KERN_WARNING
143 "Kprobes on speculation inst at <0x%lx> not supported\n",
144 addr);
145 return -EINVAL;
149 * IP relative mov instruction
150 * - Bit 27-35 to be equal to 0x30
152 if (((kprobe_inst >> 27) & 0x1FF) == 0x30) {
153 printk(KERN_WARNING
154 "Kprobes on \"mov r1=ip\" at <0x%lx> not supported\n",
155 addr);
156 return -EINVAL;
161 return 0;
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
169 * with ctype as unc.
171 static uint is_cmp_ctype_unc_inst(uint template, uint slot, uint major_opcode,
172 unsigned long kprobe_inst)
174 cmp_inst_t cmp_inst;
175 uint ctype_unc = 0;
177 if (!((bundle_encoding[template][slot] == I) ||
178 (bundle_encoding[template][slot] == M)))
179 goto out;
181 if (!((major_opcode == 0xC) || (major_opcode == 0xD) ||
182 (major_opcode == 0xE)))
183 goto out;
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))
190 ctype_unc = 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))
194 ctype_unc = 1;
196 out:
197 return ctype_unc;
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);
220 switch (slot) {
221 case 0:
222 bundle->quad0.slot0 = break_inst;
223 break;
224 case 1:
225 bundle->quad0.slot1_p0 = break_inst;
226 bundle->quad1.slot1_p1 = break_inst >> (64-46);
227 break;
228 case 2:
229 bundle->quad1.slot2 = break_inst;
230 break;
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;
249 switch (slot) {
250 case 0:
251 *major_opcode = (bundle->quad0.slot0 >> SLOT0_OPCODE_SHIFT);
252 *kprobe_inst = bundle->quad0.slot0;
253 break;
254 case 1:
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));
259 break;
260 case 2:
261 *major_opcode = (bundle->quad1.slot2 >> SLOT2_OPCODE_SHIFT);
262 *kprobe_inst = bundle->quad1.slot2;
263 break;
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 "
278 "at 0x%lx\n", addr);
279 return -EINVAL;
282 if (in_ivt_functions(addr)) {
283 printk(KERN_WARNING "Kprobes can't be inserted inside "
284 "IVT functions at 0x%lx\n", addr);
285 return -EINVAL;
288 if (slot == 1 && bundle_encoding[template][1] != L) {
289 printk(KERN_WARNING "Inserting kprobes on slot #1 "
290 "is not supported\n");
291 return -EINVAL;
294 return 0;
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)
311 current_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
321 * and then:
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 */
353 continue;
355 if (ri->rp && ri->rp->handler)
356 ri->rp->handler(ri, regs);
358 orig_ret_address = (unsigned long)ri->ret_addr;
359 recycle_rp_inst(ri);
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
367 break;
370 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
371 regs->cr_iip = orig_ret_address;
373 unlock_kprobes();
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.
381 return 1;
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) {
389 ri->rp = rp;
390 ri->task = current;
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;
396 add_rp_inst(ri);
397 } else {
398 rp->nmissed++;
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))
416 return -EINVAL;
418 /* Move to slot 2, if bundle is MLX type and kprobe slot is 1 */
419 if (slot == 1 && bundle_encoding[template][1] == L)
420 slot++;
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))
426 return -EINVAL;
428 prepare_break_inst(template, slot, major_opcode, kprobe_inst, p);
430 return 0;
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)
474 slot = 2;
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) {
490 case 0:
491 if ((regs->b0 == bundle_addr) ||
492 (regs->b0 == bundle_addr + 0x10)) {
493 regs->b0 = (regs->b0 - bundle_addr) +
494 resume_addr;
496 break;
497 case 6:
498 if ((regs->b6 == bundle_addr) ||
499 (regs->b6 == bundle_addr + 0x10)) {
500 regs->b6 = (regs->b6 - bundle_addr) +
501 resume_addr;
503 break;
504 case 7:
505 if ((regs->b7 == bundle_addr) ||
506 (regs->b7 == bundle_addr + 0x10)) {
507 regs->b7 = (regs->b7 - bundle_addr) +
508 resume_addr;
510 break;
511 } /* end switch */
513 goto turn_ss_off;
516 if (slot == 2) {
517 if (regs->cr_iip == bundle_addr + 0x10) {
518 regs->cr_iip = resume_addr + 0x10;
520 } else {
521 if (regs->cr_iip == bundle_addr) {
522 regs->cr_iip = resume_addr;
526 turn_ss_off:
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;
539 if (slot > 2)
540 slot = 0;
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)
550 struct kprobe *p;
551 int ret = 0;
552 struct pt_regs *regs = args->regs;
553 kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs);
555 preempt_disable();
557 /* Handle recursion cases */
558 if (kprobe_running()) {
559 p = get_kprobe(addr);
560 if (p) {
561 if (kprobe_status == KPROBE_HIT_SS) {
562 unlock_kprobes();
563 goto no_kprobe;
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);
573 p->nmissed++;
574 prepare_ss(p, regs);
575 kprobe_status = KPROBE_REENTER;
576 return 1;
577 } else if (args->err == __IA64_BREAK_JPROBE) {
579 * jprobe instrumented function just completed
581 p = current_kprobe;
582 if (p->break_handler && p->break_handler(p, regs)) {
583 goto ss_probe;
585 } else {
586 /* Not our break */
587 goto no_kprobe;
591 lock_kprobes();
592 p = get_kprobe(addr);
593 if (!p) {
594 unlock_kprobes();
595 goto no_kprobe;
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
607 return 1;
609 ss_probe:
610 prepare_ss(p, regs);
611 kprobe_status = KPROBE_HIT_SS;
612 return 1;
614 no_kprobe:
615 preempt_enable_no_resched();
616 return ret;
619 static int post_kprobes_handler(struct pt_regs *regs)
621 if (!kprobe_running())
622 return 0;
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();
634 goto out;
637 unlock_kprobes();
639 out:
640 preempt_enable_no_resched();
641 return 1;
644 static int kprobes_fault_handler(struct pt_regs *regs, int trapnr)
646 if (!kprobe_running())
647 return 0;
649 if (current_kprobe->fault_handler &&
650 current_kprobe->fault_handler(current_kprobe, regs, trapnr))
651 return 1;
653 if (kprobe_status & KPROBE_HIT_SS) {
654 resume_execution(current_kprobe, regs);
655 unlock_kprobes();
656 preempt_enable_no_resched();
659 return 0;
662 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
663 void *data)
665 struct die_args *args = (struct die_args *)data;
666 switch(val) {
667 case DIE_BREAK:
668 if (pre_kprobes_handler(args))
669 return NOTIFY_STOP;
670 break;
671 case DIE_SS:
672 if (post_kprobes_handler(args->regs))
673 return NOTIFY_STOP;
674 break;
675 case DIE_PAGE_FAULT:
676 if (kprobes_fault_handler(args->regs, args->trapnr))
677 return NOTIFY_STOP;
678 default:
679 break;
681 return NOTIFY_DONE;
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;
703 return 1;
706 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
708 *regs = jprobe_saved_regs;
709 return 1;
712 static struct kprobe trampoline_p = {
713 .pre_handler = trampoline_probe_handler
716 int __init arch_init_kprobes(void)
718 trampoline_p.addr =
719 (kprobe_opcode_t *)((struct fnptr *)kretprobe_trampoline)->ip;
720 return register_kprobe(&trampoline_p);