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 <asm/cacheflush.h>
28 #include <asm/kdebug.h>
29 #include <asm/sections.h>
30 #include <asm/uaccess.h>
31 #include <linux/module.h>
33 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
34 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
36 int __kprobes arch_prepare_kprobe(struct kprobe *p)
38 /* Make sure the probe isn't going on a difficult instruction */
39 if (is_prohibited_opcode((kprobe_opcode_t *) p->addr))
42 if ((unsigned long)p->addr & 0x01) {
43 printk("Attempt to register kprobe at an unaligned address\n");
47 /* Use the get_insn_slot() facility for correctness */
48 if (!(p->ainsn.insn = get_insn_slot()))
51 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
53 get_instruction_type(&p->ainsn);
58 int __kprobes is_prohibited_opcode(kprobe_opcode_t *instruction)
60 switch (*(__u8 *) instruction) {
61 case 0x0c: /* bassm */
67 switch (*(__u16 *) instruction) {
69 case 0xb25a: /* bsa */
70 case 0xb240: /* bakr */
71 case 0xb258: /* bsg */
79 void __kprobes get_instruction_type(struct arch_specific_insn *ainsn)
81 /* default fixup method */
82 ainsn->fixup = FIXUP_PSW_NORMAL;
85 ainsn->reg = (*ainsn->insn & 0xf0) >> 4;
87 /* save the instruction length (pop 5-5) in bytes */
88 switch (*(__u8 *) (ainsn->insn) >> 4) {
101 switch (*(__u8 *) ainsn->insn) {
102 case 0x05: /* balr */
103 case 0x0d: /* basr */
104 ainsn->fixup = FIXUP_RETURN_REGISTER;
105 /* if r2 = 0, no branch will be taken */
106 if ((*ainsn->insn & 0x0f) == 0)
107 ainsn->fixup |= FIXUP_BRANCH_NOT_TAKEN;
109 case 0x06: /* bctr */
111 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
115 ainsn->fixup = FIXUP_RETURN_REGISTER;
120 case 0x87: /* bxle */
121 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
123 case 0x82: /* lpsw */
124 ainsn->fixup = FIXUP_NOT_REQUIRED;
126 case 0xb2: /* lpswe */
127 if (*(((__u8 *) ainsn->insn) + 1) == 0xb2) {
128 ainsn->fixup = FIXUP_NOT_REQUIRED;
131 case 0xa7: /* bras */
132 if ((*ainsn->insn & 0x0f) == 0x05) {
133 ainsn->fixup |= FIXUP_RETURN_REGISTER;
137 if ((*ainsn->insn & 0x0f) == 0x00 /* larl */
138 || (*ainsn->insn & 0x0f) == 0x05) /* brasl */
139 ainsn->fixup |= FIXUP_RETURN_REGISTER;
142 if (*(((__u8 *) ainsn->insn) + 5 ) == 0x44 || /* bxhg */
143 *(((__u8 *) ainsn->insn) + 5) == 0x45) {/* bxleg */
144 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
147 case 0xe3: /* bctg */
148 if (*(((__u8 *) ainsn->insn) + 5) == 0x46) {
149 ainsn->fixup = FIXUP_BRANCH_NOT_TAKEN;
155 static int __kprobes swap_instruction(void *aref)
157 struct ins_replace_args *args = aref;
163 * Text segment is read-only, hence we use stura to bypass dynamic
164 * address translation to exchange the instruction. Since stura
165 * always operates on four bytes, but we only want to exchange two
166 * bytes do some calculations to get things right. In addition we
167 * shall not cross any page boundaries (vmalloc area!) when writing
168 * the new instruction.
170 addr = (u32 *)((unsigned long)args->ptr & -4UL);
171 if ((unsigned long)args->ptr & 2)
172 instr = ((*addr) & 0xffff0000) | args->new;
174 instr = ((*addr) & 0x0000ffff) | args->new << 16;
183 : "a" (addr), "d" (instr)
189 void __kprobes arch_arm_kprobe(struct kprobe *p)
191 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
192 unsigned long status = kcb->kprobe_status;
193 struct ins_replace_args args;
196 args.old = p->opcode;
197 args.new = BREAKPOINT_INSTRUCTION;
199 kcb->kprobe_status = KPROBE_SWAP_INST;
200 stop_machine_run(swap_instruction, &args, NR_CPUS);
201 kcb->kprobe_status = status;
204 void __kprobes arch_disarm_kprobe(struct kprobe *p)
206 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
207 unsigned long status = kcb->kprobe_status;
208 struct ins_replace_args args;
211 args.old = BREAKPOINT_INSTRUCTION;
212 args.new = p->opcode;
214 kcb->kprobe_status = KPROBE_SWAP_INST;
215 stop_machine_run(swap_instruction, &args, NR_CPUS);
216 kcb->kprobe_status = status;
219 void __kprobes arch_remove_kprobe(struct kprobe *p)
221 mutex_lock(&kprobe_mutex);
222 free_insn_slot(p->ainsn.insn, 0);
223 mutex_unlock(&kprobe_mutex);
226 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
228 per_cr_bits kprobe_per_regs[1];
230 memset(kprobe_per_regs, 0, sizeof(per_cr_bits));
231 regs->psw.addr = (unsigned long)p->ainsn.insn | PSW_ADDR_AMODE;
233 /* Set up the per control reg info, will pass to lctl */
234 kprobe_per_regs[0].em_instruction_fetch = 1;
235 kprobe_per_regs[0].starting_addr = (unsigned long)p->ainsn.insn;
236 kprobe_per_regs[0].ending_addr = (unsigned long)p->ainsn.insn + 1;
238 /* Set the PER control regs, turns on single step for this address */
239 __ctl_load(kprobe_per_regs, 9, 11);
240 regs->psw.mask |= PSW_MASK_PER;
241 regs->psw.mask &= ~(PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
244 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
246 kcb->prev_kprobe.kp = kprobe_running();
247 kcb->prev_kprobe.status = kcb->kprobe_status;
248 kcb->prev_kprobe.kprobe_saved_imask = kcb->kprobe_saved_imask;
249 memcpy(kcb->prev_kprobe.kprobe_saved_ctl, kcb->kprobe_saved_ctl,
250 sizeof(kcb->kprobe_saved_ctl));
253 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
255 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
256 kcb->kprobe_status = kcb->prev_kprobe.status;
257 kcb->kprobe_saved_imask = kcb->prev_kprobe.kprobe_saved_imask;
258 memcpy(kcb->kprobe_saved_ctl, kcb->prev_kprobe.kprobe_saved_ctl,
259 sizeof(kcb->kprobe_saved_ctl));
262 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
263 struct kprobe_ctlblk *kcb)
265 __get_cpu_var(current_kprobe) = p;
266 /* Save the interrupt and per flags */
267 kcb->kprobe_saved_imask = regs->psw.mask &
268 (PSW_MASK_PER | PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK);
269 /* Save the control regs that govern PER */
270 __ctl_store(kcb->kprobe_saved_ctl, 9, 11);
273 /* Called with kretprobe_lock held */
274 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
275 struct pt_regs *regs)
277 struct kretprobe_instance *ri;
279 if ((ri = get_free_rp_inst(rp)) != NULL) {
282 ri->ret_addr = (kprobe_opcode_t *) regs->gprs[14];
284 /* Replace the return addr with trampoline addr */
285 regs->gprs[14] = (unsigned long)&kretprobe_trampoline;
293 static int __kprobes kprobe_handler(struct pt_regs *regs)
297 unsigned long *addr = (unsigned long *)
298 ((regs->psw.addr & PSW_ADDR_INSN) - 2);
299 struct kprobe_ctlblk *kcb;
302 * We don't want to be preempted for the entire
303 * duration of kprobe processing
306 kcb = get_kprobe_ctlblk();
308 /* Check we're not actually recursing */
309 if (kprobe_running()) {
310 p = get_kprobe(addr);
312 if (kcb->kprobe_status == KPROBE_HIT_SS &&
313 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
314 regs->psw.mask &= ~PSW_MASK_PER;
315 regs->psw.mask |= kcb->kprobe_saved_imask;
318 /* We have reentered the kprobe_handler(), since
319 * another probe was hit while within the handler.
320 * We here save the original kprobes variables and
321 * just single step on the instruction of the new probe
322 * without calling any user handlers.
324 save_previous_kprobe(kcb);
325 set_current_kprobe(p, regs, kcb);
326 kprobes_inc_nmissed_count(p);
327 prepare_singlestep(p, regs);
328 kcb->kprobe_status = KPROBE_REENTER;
331 p = __get_cpu_var(current_kprobe);
332 if (p->break_handler && p->break_handler(p, regs)) {
339 p = get_kprobe(addr);
342 * No kprobe at this address. The fault has not been
343 * caused by a kprobe breakpoint. The race of breakpoint
344 * vs. kprobe remove does not exist because on s390 we
345 * use stop_machine_run to arm/disarm the breakpoints.
349 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
350 set_current_kprobe(p, regs, kcb);
351 if (p->pre_handler && p->pre_handler(p, regs))
352 /* handler has already set things up, so skip ss setup */
356 prepare_singlestep(p, regs);
357 kcb->kprobe_status = KPROBE_HIT_SS;
361 preempt_enable_no_resched();
366 * Function return probe trampoline:
367 * - init_kprobes() establishes a probepoint here
368 * - When the probed function returns, this probe
369 * causes the handlers to fire
371 void kretprobe_trampoline_holder(void)
373 asm volatile(".global kretprobe_trampoline\n"
374 "kretprobe_trampoline: bcr 0,0\n");
378 * Called when the probe at kretprobe trampoline is hit
380 static int __kprobes trampoline_probe_handler(struct kprobe *p,
381 struct pt_regs *regs)
383 struct kretprobe_instance *ri = NULL;
384 struct hlist_head *head, empty_rp;
385 struct hlist_node *node, *tmp;
386 unsigned long flags, orig_ret_address = 0;
387 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
389 INIT_HLIST_HEAD(&empty_rp);
390 spin_lock_irqsave(&kretprobe_lock, flags);
391 head = kretprobe_inst_table_head(current);
394 * It is possible to have multiple instances associated with a given
395 * task either because an multiple functions in the call path
396 * have a return probe installed on them, and/or more then one return
397 * return probe was registered for a target function.
399 * We can handle this because:
400 * - instances are always inserted at the head of the list
401 * - when multiple return probes are registered for the same
402 * function, the first instance's ret_addr will point to the
403 * real return address, and all the rest will point to
404 * kretprobe_trampoline
406 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
407 if (ri->task != current)
408 /* another task is sharing our hash bucket */
411 if (ri->rp && ri->rp->handler)
412 ri->rp->handler(ri, regs);
414 orig_ret_address = (unsigned long)ri->ret_addr;
415 recycle_rp_inst(ri, &empty_rp);
417 if (orig_ret_address != trampoline_address) {
419 * This is the real return address. Any other
420 * instances associated with this task are for
421 * other calls deeper on the call stack
426 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
427 regs->psw.addr = orig_ret_address | PSW_ADDR_AMODE;
429 reset_current_kprobe();
430 spin_unlock_irqrestore(&kretprobe_lock, flags);
431 preempt_enable_no_resched();
433 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
434 hlist_del(&ri->hlist);
438 * By returning a non-zero value, we are telling
439 * kprobe_handler() that we don't want the post_handler
440 * to run (and have re-enabled preemption)
446 * Called after single-stepping. p->addr is the address of the
447 * instruction whose first byte has been replaced by the "breakpoint"
448 * instruction. To avoid the SMP problems that can occur when we
449 * temporarily put back the original opcode to single-step, we
450 * single-stepped a copy of the instruction. The address of this
451 * copy is p->ainsn.insn.
453 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
455 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
457 regs->psw.addr &= PSW_ADDR_INSN;
459 if (p->ainsn.fixup & FIXUP_PSW_NORMAL)
460 regs->psw.addr = (unsigned long)p->addr +
461 ((unsigned long)regs->psw.addr -
462 (unsigned long)p->ainsn.insn);
464 if (p->ainsn.fixup & FIXUP_BRANCH_NOT_TAKEN)
465 if ((unsigned long)regs->psw.addr -
466 (unsigned long)p->ainsn.insn == p->ainsn.ilen)
467 regs->psw.addr = (unsigned long)p->addr + p->ainsn.ilen;
469 if (p->ainsn.fixup & FIXUP_RETURN_REGISTER)
470 regs->gprs[p->ainsn.reg] = ((unsigned long)p->addr +
471 (regs->gprs[p->ainsn.reg] -
472 (unsigned long)p->ainsn.insn))
475 regs->psw.addr |= PSW_ADDR_AMODE;
476 /* turn off PER mode */
477 regs->psw.mask &= ~PSW_MASK_PER;
478 /* Restore the original per control regs */
479 __ctl_load(kcb->kprobe_saved_ctl, 9, 11);
480 regs->psw.mask |= kcb->kprobe_saved_imask;
483 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
485 struct kprobe *cur = kprobe_running();
486 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
491 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
492 kcb->kprobe_status = KPROBE_HIT_SSDONE;
493 cur->post_handler(cur, regs, 0);
496 resume_execution(cur, regs);
498 /*Restore back the original saved kprobes variables and continue. */
499 if (kcb->kprobe_status == KPROBE_REENTER) {
500 restore_previous_kprobe(kcb);
503 reset_current_kprobe();
505 preempt_enable_no_resched();
508 * if somebody else is singlestepping across a probe point, psw mask
509 * will have PER set, in which case, continue the remaining processing
510 * of do_single_step, as if this is not a probe hit.
512 if (regs->psw.mask & PSW_MASK_PER) {
519 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
521 struct kprobe *cur = kprobe_running();
522 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
523 const struct exception_table_entry *entry;
525 switch(kcb->kprobe_status) {
526 case KPROBE_SWAP_INST:
527 /* We are here because the instruction replacement failed */
532 * We are here because the instruction being single
533 * stepped caused a page fault. We reset the current
534 * kprobe and the nip points back to the probe address
535 * and allow the page fault handler to continue as a
538 regs->psw.addr = (unsigned long)cur->addr | PSW_ADDR_AMODE;
539 regs->psw.mask &= ~PSW_MASK_PER;
540 regs->psw.mask |= kcb->kprobe_saved_imask;
541 if (kcb->kprobe_status == KPROBE_REENTER)
542 restore_previous_kprobe(kcb);
544 reset_current_kprobe();
545 preempt_enable_no_resched();
547 case KPROBE_HIT_ACTIVE:
548 case KPROBE_HIT_SSDONE:
550 * We increment the nmissed count for accounting,
551 * we can also use npre/npostfault count for accouting
552 * these specific fault cases.
554 kprobes_inc_nmissed_count(cur);
557 * We come here because instructions in the pre/post
558 * handler caused the page_fault, this could happen
559 * if handler tries to access user space by
560 * copy_from_user(), get_user() etc. Let the
561 * user-specified handler try to fix it first.
563 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
567 * In case the user-specified fault handler returned
568 * zero, try to fix up.
570 entry = search_exception_tables(regs->psw.addr & PSW_ADDR_INSN);
572 regs->psw.addr = entry->fixup | PSW_ADDR_AMODE;
577 * fixup_exception() could not handle it,
578 * Let do_page_fault() fix it.
588 * Wrapper routine to for handling exceptions.
590 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
591 unsigned long val, void *data)
593 struct die_args *args = (struct die_args *)data;
594 int ret = NOTIFY_DONE;
598 if (kprobe_handler(args->regs))
602 if (post_kprobe_handler(args->regs))
606 /* kprobe_running() needs smp_processor_id() */
608 if (kprobe_running() &&
609 kprobe_fault_handler(args->regs, args->trapnr))
619 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
621 struct jprobe *jp = container_of(p, struct jprobe, kp);
623 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
625 memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs));
627 /* setup return addr to the jprobe handler routine */
628 regs->psw.addr = (unsigned long)(jp->entry) | PSW_ADDR_AMODE;
630 /* r14 is the function return address */
631 kcb->jprobe_saved_r14 = (unsigned long)regs->gprs[14];
632 /* r15 is the stack pointer */
633 kcb->jprobe_saved_r15 = (unsigned long)regs->gprs[15];
634 addr = (unsigned long)kcb->jprobe_saved_r15;
636 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *) addr,
637 MIN_STACK_SIZE(addr));
641 void __kprobes jprobe_return(void)
643 asm volatile(".word 0x0002");
646 void __kprobes jprobe_return_end(void)
648 asm volatile("bcr 0,0");
651 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
653 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
654 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_r15);
656 /* Put the regs back */
657 memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs));
658 /* put the stack back */
659 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
660 MIN_STACK_SIZE(stack_addr));
661 preempt_enable_no_resched();
665 static struct kprobe trampoline_p = {
666 .addr = (kprobe_opcode_t *) & kretprobe_trampoline,
667 .pre_handler = trampoline_probe_handler
670 int __init arch_init_kprobes(void)
672 return register_kprobe(&trampoline_p);