2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/types.h>
10 #include <linux/kprobes.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/kdebug.h>
14 #include <linux/sched.h>
15 #include <linux/uaccess.h>
16 #include <asm/cacheflush.h>
17 #include <asm/current.h>
18 #include <asm/disasm.h>
20 #define MIN_STACK_SIZE(addr) min((unsigned long)MAX_STACK_SIZE, \
21 (unsigned long)current_thread_info() + THREAD_SIZE - (addr))
23 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
24 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
26 int __kprobes arch_prepare_kprobe(struct kprobe *p)
28 /* Attempt to probe at unaligned address */
29 if ((unsigned long)p->addr & 0x01)
32 /* Address should not be in exception handling code */
34 p->ainsn.is_short = is_short_instr((unsigned long)p->addr);
40 void __kprobes arch_arm_kprobe(struct kprobe *p)
42 *p->addr = UNIMP_S_INSTRUCTION;
44 flush_icache_range((unsigned long)p->addr,
45 (unsigned long)p->addr + sizeof(kprobe_opcode_t));
48 void __kprobes arch_disarm_kprobe(struct kprobe *p)
52 flush_icache_range((unsigned long)p->addr,
53 (unsigned long)p->addr + sizeof(kprobe_opcode_t));
56 void __kprobes arch_remove_kprobe(struct kprobe *p)
58 arch_disarm_kprobe(p);
60 /* Can we remove the kprobe in the middle of kprobe handling? */
61 if (p->ainsn.t1_addr) {
62 *(p->ainsn.t1_addr) = p->ainsn.t1_opcode;
64 flush_icache_range((unsigned long)p->ainsn.t1_addr,
65 (unsigned long)p->ainsn.t1_addr +
66 sizeof(kprobe_opcode_t));
68 p->ainsn.t1_addr = NULL;
71 if (p->ainsn.t2_addr) {
72 *(p->ainsn.t2_addr) = p->ainsn.t2_opcode;
74 flush_icache_range((unsigned long)p->ainsn.t2_addr,
75 (unsigned long)p->ainsn.t2_addr +
76 sizeof(kprobe_opcode_t));
78 p->ainsn.t2_addr = NULL;
82 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
84 kcb->prev_kprobe.kp = kprobe_running();
85 kcb->prev_kprobe.status = kcb->kprobe_status;
88 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
90 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
91 kcb->kprobe_status = kcb->prev_kprobe.status;
94 static inline void __kprobes set_current_kprobe(struct kprobe *p)
96 __this_cpu_write(current_kprobe, p);
99 static void __kprobes resume_execution(struct kprobe *p, unsigned long addr,
100 struct pt_regs *regs)
102 /* Remove the trap instructions inserted for single step and
103 * restore the original instructions
105 if (p->ainsn.t1_addr) {
106 *(p->ainsn.t1_addr) = p->ainsn.t1_opcode;
108 flush_icache_range((unsigned long)p->ainsn.t1_addr,
109 (unsigned long)p->ainsn.t1_addr +
110 sizeof(kprobe_opcode_t));
112 p->ainsn.t1_addr = NULL;
115 if (p->ainsn.t2_addr) {
116 *(p->ainsn.t2_addr) = p->ainsn.t2_opcode;
118 flush_icache_range((unsigned long)p->ainsn.t2_addr,
119 (unsigned long)p->ainsn.t2_addr +
120 sizeof(kprobe_opcode_t));
122 p->ainsn.t2_addr = NULL;
128 static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs)
130 unsigned long next_pc;
131 unsigned long tgt_if_br = 0;
135 /* Copy the opcode back to the kprobe location and execute the
136 * instruction. Because of this we will not be able to get into the
137 * same kprobe until this kprobe is done
139 *(p->addr) = p->opcode;
141 flush_icache_range((unsigned long)p->addr,
142 (unsigned long)p->addr + sizeof(kprobe_opcode_t));
144 /* Now we insert the trap at the next location after this instruction to
145 * single step. If it is a branch we insert the trap at possible branch
151 if (regs->status32 & 0x40) {
152 /* We are in a delay slot with the branch taken */
154 next_pc = bta & ~0x01;
156 if (!p->ainsn.is_short) {
160 /* Branch not taken */
163 /* next pc is taken from bta after executing the
164 * delay slot instruction
173 disasm_next_pc((unsigned long)p->addr, regs,
174 (struct callee_regs *) current->thread.callee_reg,
175 &next_pc, &tgt_if_br);
177 p->ainsn.t1_addr = (kprobe_opcode_t *) next_pc;
178 p->ainsn.t1_opcode = *(p->ainsn.t1_addr);
179 *(p->ainsn.t1_addr) = TRAP_S_2_INSTRUCTION;
181 flush_icache_range((unsigned long)p->ainsn.t1_addr,
182 (unsigned long)p->ainsn.t1_addr +
183 sizeof(kprobe_opcode_t));
186 p->ainsn.t2_addr = (kprobe_opcode_t *) tgt_if_br;
187 p->ainsn.t2_opcode = *(p->ainsn.t2_addr);
188 *(p->ainsn.t2_addr) = TRAP_S_2_INSTRUCTION;
190 flush_icache_range((unsigned long)p->ainsn.t2_addr,
191 (unsigned long)p->ainsn.t2_addr +
192 sizeof(kprobe_opcode_t));
196 int __kprobes arc_kprobe_handler(unsigned long addr, struct pt_regs *regs)
199 struct kprobe_ctlblk *kcb;
203 kcb = get_kprobe_ctlblk();
204 p = get_kprobe((unsigned long *)addr);
208 * We have reentered the kprobe_handler, since another kprobe
209 * was hit while within the handler, we save the original
210 * kprobes and single step on the instruction of the new probe
211 * without calling any user handlers to avoid recursive
214 if (kprobe_running()) {
215 save_previous_kprobe(kcb);
216 set_current_kprobe(p);
217 kprobes_inc_nmissed_count(p);
218 setup_singlestep(p, regs);
219 kcb->kprobe_status = KPROBE_REENTER;
223 set_current_kprobe(p);
224 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
226 /* If we have no pre-handler or it returned 0, we continue with
227 * normal processing. If we have a pre-handler and it returned
228 * non-zero - which means user handler setup registers to exit
229 * to another instruction, we must skip the single stepping.
231 if (!p->pre_handler || !p->pre_handler(p, regs)) {
232 setup_singlestep(p, regs);
233 kcb->kprobe_status = KPROBE_HIT_SS;
235 reset_current_kprobe();
236 preempt_enable_no_resched();
243 preempt_enable_no_resched();
247 static int __kprobes arc_post_kprobe_handler(unsigned long addr,
248 struct pt_regs *regs)
250 struct kprobe *cur = kprobe_running();
251 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
256 resume_execution(cur, addr, regs);
258 /* Rearm the kprobe */
259 arch_arm_kprobe(cur);
262 * When we return from trap instruction we go to the next instruction
263 * We restored the actual instruction in resume_exectuiont and we to
264 * return to the same address and execute it
268 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
269 kcb->kprobe_status = KPROBE_HIT_SSDONE;
270 cur->post_handler(cur, regs, 0);
273 if (kcb->kprobe_status == KPROBE_REENTER) {
274 restore_previous_kprobe(kcb);
278 reset_current_kprobe();
281 preempt_enable_no_resched();
286 * Fault can be for the instruction being single stepped or for the
287 * pre/post handlers in the module.
288 * This is applicable for applications like user probes, where we have the
289 * probe in user space and the handlers in the kernel
292 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)
294 struct kprobe *cur = kprobe_running();
295 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
297 switch (kcb->kprobe_status) {
301 * We are here because the instruction being single stepped
302 * caused the fault. We reset the current kprobe and allow the
303 * exception handler as if it is regular exception. In our
304 * case it doesn't matter because the system will be halted
306 resume_execution(cur, (unsigned long)cur->addr, regs);
308 if (kcb->kprobe_status == KPROBE_REENTER)
309 restore_previous_kprobe(kcb);
311 reset_current_kprobe();
313 preempt_enable_no_resched();
316 case KPROBE_HIT_ACTIVE:
317 case KPROBE_HIT_SSDONE:
319 * We are here because the instructions in the pre/post handler
323 /* We increment the nmissed count for accounting,
324 * we can also use npre/npostfault count for accounting
325 * these specific fault cases.
327 kprobes_inc_nmissed_count(cur);
330 * We come here because instructions in the pre/post
331 * handler caused the page_fault, this could happen
332 * if handler tries to access user space by
333 * copy_from_user(), get_user() etc. Let the
334 * user-specified handler try to fix it first.
336 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
340 * In case the user-specified fault handler returned zero,
343 if (fixup_exception(regs))
347 * fixup_exception() could not handle it,
348 * Let do_page_fault() fix it.
358 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
359 unsigned long val, void *data)
361 struct die_args *args = data;
362 unsigned long addr = args->err;
363 int ret = NOTIFY_DONE;
367 if (arc_kprobe_handler(addr, args->regs))
372 if (arc_post_kprobe_handler(addr, args->regs))
383 static void __used kretprobe_trampoline_holder(void)
385 __asm__ __volatile__(".global kretprobe_trampoline\n"
386 "kretprobe_trampoline:\n" "nop\n");
389 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
390 struct pt_regs *regs)
393 ri->ret_addr = (kprobe_opcode_t *) regs->blink;
395 /* Replace the return addr with trampoline addr */
396 regs->blink = (unsigned long)&kretprobe_trampoline;
399 static int __kprobes trampoline_probe_handler(struct kprobe *p,
400 struct pt_regs *regs)
402 struct kretprobe_instance *ri = NULL;
403 struct hlist_head *head, empty_rp;
404 struct hlist_node *tmp;
405 unsigned long flags, orig_ret_address = 0;
406 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
408 INIT_HLIST_HEAD(&empty_rp);
409 kretprobe_hash_lock(current, &head, &flags);
412 * It is possible to have multiple instances associated with a given
413 * task either because an multiple functions in the call path
414 * have a return probe installed on them, and/or more than one return
415 * return probe was registered for a target function.
417 * We can handle this because:
418 * - instances are always inserted at the head of the list
419 * - when multiple return probes are registered for the same
420 * function, the first instance's ret_addr will point to the
421 * real return address, and all the rest will point to
422 * kretprobe_trampoline
424 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
425 if (ri->task != current)
426 /* another task is sharing our hash bucket */
429 if (ri->rp && ri->rp->handler)
430 ri->rp->handler(ri, regs);
432 orig_ret_address = (unsigned long)ri->ret_addr;
433 recycle_rp_inst(ri, &empty_rp);
435 if (orig_ret_address != trampoline_address) {
437 * This is the real return address. Any other
438 * instances associated with this task are for
439 * other calls deeper on the call stack
445 kretprobe_assert(ri, orig_ret_address, trampoline_address);
446 regs->ret = orig_ret_address;
448 kretprobe_hash_unlock(current, &flags);
450 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
451 hlist_del(&ri->hlist);
455 /* By returning a non zero value, we are telling the kprobe handler
456 * that we don't want the post_handler to run
461 static struct kprobe trampoline_p = {
462 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
463 .pre_handler = trampoline_probe_handler
466 int __init arch_init_kprobes(void)
468 /* Registering the trampoline code for the kret probe */
469 return register_kprobe(&trampoline_p);
472 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
474 if (p->addr == (kprobe_opcode_t *) &kretprobe_trampoline)
480 void trap_is_kprobe(unsigned long address, struct pt_regs *regs)
482 notify_die(DIE_TRAP, "kprobe_trap", regs, address, 0, SIGTRAP);