2 * Kernel Probes (KProbes)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2002, 2006
20 * s390 port, used ppc64 as template. Mike Grundy <grundym@us.ibm.com>
23 #include <linux/kprobes.h>
24 #include <linux/ptrace.h>
25 #include <linux/preempt.h>
26 #include <linux/stop_machine.h>
27 #include <linux/kdebug.h>
28 #include <linux/uaccess.h>
29 #include <asm/cacheflush.h>
30 #include <asm/sections.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/hardirq.h>
35 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
36 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
38 struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
40 int __kprobes arch_prepare_kprobe(struct kprobe *p)
42 /* Make sure the probe isn't going on a difficult instruction */
43 if (is_prohibited_opcode((kprobe_opcode_t *) p->addr))
46 if ((unsigned long)p->addr & 0x01)
49 /* Use the get_insn_slot() facility for correctness */
50 if (!(p->ainsn.insn = get_insn_slot()))
53 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
55 get_instruction_type(&p->ainsn);
60 int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
62 switch (*(__u8 *) instruction) {
63 case 0x0c: /* bassm */
67 case 0xac: /* stnsm */
68 case 0xad: /* stosm */
71 switch (*(__u16 *) instruction) {
73 case 0xb25a: /* bsa */
74 case 0xb240: /* bakr */
75 case 0xb258: /* bsg */
78 case 0xb98d: /* epsw */
84 void __kprobes get_instruction_type(struct arch_specific_insn *ainsn)
86 /* default fixup method */
87 ainsn->fixup = FIXUP_PSW_NORMAL;
90 ainsn->reg = (*ainsn->insn & 0xf0) >> 4;
92 /* save the instruction length (pop 5-5) in bytes */
93 switch (*(__u8 *) (ainsn->insn) >> 6) {
106 switch (*(__u8 *) ainsn->insn) {
107 case 0x05: /* balr */
108 case 0x0d: /* basr */
109 ainsn->fixup = FIXUP_RETURN_REGISTER;
110 /* if r2 = 0, no branch will be taken */
111 if ((*ainsn->insn & 0x0f) == 0)
112 ainsn->fixup |= FIXUP_BRANCH_NOT_TAKEN;
114 case 0x06: /* bctr */
116 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
120 ainsn->fixup = FIXUP_RETURN_REGISTER;
125 case 0x87: /* bxle */
126 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
128 case 0x82: /* lpsw */
129 ainsn->fixup = FIXUP_NOT_REQUIRED;
131 case 0xb2: /* lpswe */
132 if (*(((__u8 *) ainsn->insn) + 1) == 0xb2) {
133 ainsn->fixup = FIXUP_NOT_REQUIRED;
136 case 0xa7: /* bras */
137 if ((*ainsn->insn & 0x0f) == 0x05) {
138 ainsn->fixup |= FIXUP_RETURN_REGISTER;
142 if ((*ainsn->insn & 0x0f) == 0x00 /* larl */
143 || (*ainsn->insn & 0x0f) == 0x05) /* brasl */
144 ainsn->fixup |= FIXUP_RETURN_REGISTER;
147 if (*(((__u8 *) ainsn->insn) + 5 ) == 0x44 || /* bxhg */
148 *(((__u8 *) ainsn->insn) + 5) == 0x45) {/* bxleg */
149 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
152 case 0xe3: /* bctg */
153 if (*(((__u8 *) ainsn->insn) + 5) == 0x46) {
154 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
160 struct ins_replace_args {
161 kprobe_opcode_t *ptr;
162 kprobe_opcode_t opcode;
165 static int __kprobes swap_instruction(void *aref)
167 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
168 unsigned long status = kcb->kprobe_status;
169 struct ins_replace_args *args = aref;
171 kcb->kprobe_status = KPROBE_SWAP_INST;
172 probe_kernel_write(args->ptr, &args->opcode, sizeof(args->opcode));
173 kcb->kprobe_status = status;
177 void __kprobes arch_arm_kprobe(struct kprobe *p)
179 struct ins_replace_args args;
182 args.opcode = BREAKPOINT_INSTRUCTION;
183 stop_machine(swap_instruction, &args, NULL);
186 void __kprobes arch_disarm_kprobe(struct kprobe *p)
188 struct ins_replace_args args;
191 args.opcode = p->opcode;
192 stop_machine(swap_instruction, &args, NULL);
195 void __kprobes arch_remove_kprobe(struct kprobe *p)
198 free_insn_slot(p->ainsn.insn, 0);
199 p->ainsn.insn = NULL;
203 static void __kprobes enable_singlestep(struct kprobe_ctlblk *kcb,
204 struct pt_regs *regs,
207 per_cr_bits kprobe_per_regs[1];
209 /* Set up the per control reg info, will pass to lctl */
210 memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
211 kprobe_per_regs[0].em_instruction_fetch = 1;
212 kprobe_per_regs[0].starting_addr = ip;
213 kprobe_per_regs[0].ending_addr = ip;
215 /* Save control regs and psw mask */
216 __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
217 kcb->kprobe_saved_imask = regs->psw.mask &
218 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT);
220 /* Set PER control regs, turns on single step for the given address */
221 __ctl_load(kprobe_per_regs, 9, 11);
222 regs->psw.mask |= PSW_MASK_PER;
223 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
224 regs->psw.addr = ip | PSW_ADDR_AMODE;
227 static void __kprobes disable_singlestep(struct kprobe_ctlblk *kcb,
228 struct pt_regs *regs,
231 /* Restore control regs and psw mask, set new psw address */
232 __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
233 regs->psw.mask &= ~PSW_MASK_PER;
234 regs->psw.mask |= kcb->kprobe_saved_imask;
235 regs->psw.addr = ip | PSW_ADDR_AMODE;
239 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
241 kcb->prev_kprobe.kp = kprobe_running();
242 kcb->prev_kprobe.status = kcb->kprobe_status;
245 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
247 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
248 kcb->kprobe_status = kcb->prev_kprobe.status;
251 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
252 struct kprobe_ctlblk *kcb)
254 __get_cpu_var(current_kprobe) = p;
257 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
258 struct pt_regs *regs)
260 ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
262 /* Replace the return addr with trampoline addr */
263 regs->gprs[14] = (unsigned long)&kretprobe_trampoline;
266 static int __kprobes kprobe_handler(struct pt_regs *regs)
270 unsigned long *addr = (unsigned long *)
271 ((regs->psw.addr & PSW_ADDR_INSN) - 2);
272 struct kprobe_ctlblk *kcb;
275 * We don't want to be preempted for the entire
276 * duration of kprobe processing
279 kcb = get_kprobe_ctlblk();
281 /* Check we're not actually recursing */
282 if (kprobe_running()) {
283 p = get_kprobe(addr);
285 /* We have reentered the kprobe_handler(), since
286 * another probe was hit while within the handler.
287 * We here save the original kprobes variables and
288 * just single step on the instruction of the new probe
289 * without calling any user handlers.
291 save_previous_kprobe(kcb);
292 set_current_kprobe(p, regs, kcb);
293 kprobes_inc_nmissed_count(p);
294 enable_singlestep(kcb, regs,
295 (unsigned long) p->ainsn.insn);
296 kcb->kprobe_status = KPROBE_REENTER;
299 p = __get_cpu_var(current_kprobe);
300 if (p->break_handler && p->break_handler(p, regs)) {
307 p = get_kprobe(addr);
310 * No kprobe at this address. The fault has not been
311 * caused by a kprobe breakpoint. The race of breakpoint
312 * vs. kprobe remove does not exist because on s390 we
313 * use stop_machine to arm/disarm the breakpoints.
317 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
318 set_current_kprobe(p, regs, kcb);
319 if (p->pre_handler && p->pre_handler(p, regs))
320 /* handler has already set things up, so skip ss setup */
324 enable_singlestep(kcb, regs, (unsigned long) p->ainsn.insn);
325 kcb->kprobe_status = KPROBE_HIT_SS;
329 preempt_enable_no_resched();
334 * Function return probe trampoline:
335 * - init_kprobes() establishes a probepoint here
336 * - When the probed function returns, this probe
337 * causes the handlers to fire
339 static void __used kretprobe_trampoline_holder(void)
341 asm volatile(".global kretprobe_trampoline\n"
342 "kretprobe_trampoline: bcr 0,0\n");
346 * Called when the probe at kretprobe trampoline is hit
348 static int __kprobes trampoline_probe_handler(struct kprobe *p,
349 struct pt_regs *regs)
351 struct kretprobe_instance *ri = NULL;
352 struct hlist_head *head, empty_rp;
353 struct hlist_node *node, *tmp;
354 unsigned long flags, orig_ret_address = 0;
355 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
356 kprobe_opcode_t *correct_ret_addr = NULL;
358 INIT_HLIST_HEAD(&empty_rp);
359 kretprobe_hash_lock(current, &head, &flags);
362 * It is possible to have multiple instances associated with a given
363 * task either because an multiple functions in the call path
364 * have a return probe installed on them, and/or more than one return
365 * return probe was registered for a target function.
367 * We can handle this because:
368 * - instances are always inserted at the head of the list
369 * - when multiple return probes are registered for the same
370 * function, the first instance's ret_addr will point to the
371 * real return address, and all the rest will point to
372 * kretprobe_trampoline
374 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
375 if (ri->task != current)
376 /* another task is sharing our hash bucket */
379 orig_ret_address = (unsigned long)ri->ret_addr;
381 if (orig_ret_address != trampoline_address)
383 * This is the real return address. Any other
384 * instances associated with this task are for
385 * other calls deeper on the call stack
390 kretprobe_assert(ri, orig_ret_address, trampoline_address);
392 correct_ret_addr = ri->ret_addr;
393 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
394 if (ri->task != current)
395 /* another task is sharing our hash bucket */
398 orig_ret_address = (unsigned long)ri->ret_addr;
400 if (ri->rp && ri->rp->handler) {
401 ri->ret_addr = correct_ret_addr;
402 ri->rp->handler(ri, regs);
405 recycle_rp_inst(ri, &empty_rp);
407 if (orig_ret_address != trampoline_address) {
409 * This is the real return address. Any other
410 * instances associated with this task are for
411 * other calls deeper on the call stack
417 regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
419 reset_current_kprobe();
420 kretprobe_hash_unlock(current, &flags);
421 preempt_enable_no_resched();
423 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
424 hlist_del(&ri->hlist);
428 * By returning a non-zero value, we are telling
429 * kprobe_handler() that we don't want the post_handler
430 * to run (and have re-enabled preemption)
436 * Called after single-stepping. p->addr is the address of the
437 * instruction whose first byte has been replaced by the "breakpoint"
438 * instruction. To avoid the SMP problems that can occur when we
439 * temporarily put back the original opcode to single-step, we
440 * single-stepped a copy of the instruction. The address of this
441 * copy is p->ainsn.insn.
443 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
445 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
446 unsigned long ip = regs->psw.addr & PSW_ADDR_INSN;
448 if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
449 ip += (unsigned long) p->addr - (unsigned long) p->ainsn.insn;
451 if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN)
452 if (ip - (unsigned long) p->ainsn.insn == p->ainsn.ilen)
453 ip = (unsigned long) p->addr + p->ainsn.ilen;
455 if (p->ainsn.fixup & FIXUP_RETURN_REGISTER)
456 regs->gprs[p->ainsn.reg] += (unsigned long) p->addr -
457 (unsigned long) p->ainsn.insn;
459 disable_singlestep(kcb, regs, ip);
462 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
464 struct kprobe *cur = kprobe_running();
465 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
470 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
471 kcb->kprobe_status = KPROBE_HIT_SSDONE;
472 cur->post_handler(cur, regs, 0);
475 resume_execution(cur, regs);
477 /*Restore back the original saved kprobes variables and continue. */
478 if (kcb->kprobe_status == KPROBE_REENTER) {
479 restore_previous_kprobe(kcb);
482 reset_current_kprobe();
484 preempt_enable_no_resched();
487 * if somebody else is singlestepping across a probe point, psw mask
488 * will have PER set, in which case, continue the remaining processing
489 * of do_single_step, as if this is not a probe hit.
491 if (regs->psw.mask & PSW_MASK_PER) {
498 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, int trapnr)
500 struct kprobe *cur = kprobe_running();
501 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
502 const struct exception_table_entry *entry;
504 switch(kcb->kprobe_status) {
505 case KPROBE_SWAP_INST:
506 /* We are here because the instruction replacement failed */
511 * We are here because the instruction being single
512 * stepped caused a page fault. We reset the current
513 * kprobe and the nip points back to the probe address
514 * and allow the page fault handler to continue as a
517 disable_singlestep(kcb, regs, (unsigned long) cur->addr);
518 if (kcb->kprobe_status == KPROBE_REENTER)
519 restore_previous_kprobe(kcb);
521 reset_current_kprobe();
523 preempt_enable_no_resched();
525 case KPROBE_HIT_ACTIVE:
526 case KPROBE_HIT_SSDONE:
528 * We increment the nmissed count for accounting,
529 * we can also use npre/npostfault count for accouting
530 * these specific fault cases.
532 kprobes_inc_nmissed_count(cur);
535 * We come here because instructions in the pre/post
536 * handler caused the page_fault, this could happen
537 * if handler tries to access user space by
538 * copy_from_user(), get_user() etc. Let the
539 * user-specified handler try to fix it first.
541 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
545 * In case the user-specified fault handler returned
546 * zero, try to fix up.
548 entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
550 regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
555 * fixup_exception() could not handle it,
556 * Let do_page_fault() fix it.
565 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
569 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
571 ret = kprobe_trap_handler(regs, trapnr);
572 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
573 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
578 * Wrapper routine to for handling exceptions.
580 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
581 unsigned long val, void *data)
583 struct die_args *args = (struct die_args *)data;
584 struct pt_regs *regs = args->regs;
585 int ret = NOTIFY_DONE;
587 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
592 if (kprobe_handler(args->regs))
596 if (post_kprobe_handler(args->regs))
600 if (!preemptible() && kprobe_running() &&
601 kprobe_trap_handler(args->regs, args->trapnr))
608 if (regs->psw.mask & (PSW_MASK_IO | PSW_MASK_EXT))
609 local_irq_restore(regs->psw.mask & ~PSW_MASK_PER);
614 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
616 struct jprobe *jp = container_of(p, struct jprobe, kp);
618 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
620 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
622 /* setup return addr to the jprobe handler routine */
623 regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE;
624 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT);
626 /* r14 is the function return address */
627 kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
628 /* r15 is the stack pointer */
629 kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15];
630 addr = (unsigned long)kcb->jprobe_saved_r15;
632 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
633 MIN_STACK_SIZE(addr));
637 void __kprobes jprobe_return(void)
639 asm volatile(".word 0x0002");
642 void __kprobes jprobe_return_end(void)
644 asm volatile("bcr 0,0");
647 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
649 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
650 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15);
652 /* Put the regs back */
653 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
654 /* put the stack back */
655 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
656 MIN_STACK_SIZE(stack_addr));
657 preempt_enable_no_resched();
661 static struct kprobe trampoline_p = {
662 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
663 .pre_handler = trampoline_probe_handler
666 int __init arch_init_kprobes(void)
668 return register_kprobe(&trampoline_p);
671 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
673 if (p->addr == (kprobe_opcode_t *) & kretprobe_trampoline)