2 * Kernel Probes (KProbes)
4 * Copyright (C) 2005-2006 Atmel Corporation
6 * Based on arch/ppc64/kernel/kprobes.c
7 * Copyright (C) IBM Corporation, 2002, 2004
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/kprobes.h>
15 #include <linux/ptrace.h>
17 #include <asm/cacheflush.h>
18 #include <asm/kdebug.h>
21 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
);
22 static unsigned long kprobe_status
;
23 static struct pt_regs jprobe_saved_regs
;
25 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
29 if ((unsigned long)p
->addr
& 0x01) {
30 printk("Attempt to register kprobe at an unaligned address\n");
34 /* XXX: Might be a good idea to check if p->addr is a valid
35 * kernel address as well... */
38 pr_debug("copy kprobe at %p\n", p
->addr
);
39 memcpy(p
->ainsn
.insn
, p
->addr
, MAX_INSN_SIZE
* sizeof(kprobe_opcode_t
));
46 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
48 pr_debug("arming kprobe at %p\n", p
->addr
);
49 *p
->addr
= BREAKPOINT_INSTRUCTION
;
50 flush_icache_range((unsigned long)p
->addr
,
51 (unsigned long)p
->addr
+ sizeof(kprobe_opcode_t
));
54 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
56 pr_debug("disarming kprobe at %p\n", p
->addr
);
58 flush_icache_range((unsigned long)p
->addr
,
59 (unsigned long)p
->addr
+ sizeof(kprobe_opcode_t
));
62 static void __kprobes
prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
66 pr_debug("preparing to singlestep over %p (PC=%08lx)\n",
69 BUG_ON(!(sysreg_read(SR
) & SYSREG_BIT(SR_D
)));
71 dc
= __mfdr(DBGREG_DC
);
73 __mtdr(DBGREG_DC
, dc
);
76 * We must run the instruction from its original location
77 * since it may actually reference PC.
79 * TODO: Do the instruction replacement directly in icache.
82 flush_icache_range((unsigned long)p
->addr
,
83 (unsigned long)p
->addr
+ sizeof(kprobe_opcode_t
));
86 static void __kprobes
resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
90 pr_debug("resuming execution at PC=%08lx\n", regs
->pc
);
92 dc
= __mfdr(DBGREG_DC
);
94 __mtdr(DBGREG_DC
, dc
);
96 *p
->addr
= BREAKPOINT_INSTRUCTION
;
97 flush_icache_range((unsigned long)p
->addr
,
98 (unsigned long)p
->addr
+ sizeof(kprobe_opcode_t
));
101 static void __kprobes
set_current_kprobe(struct kprobe
*p
)
103 __get_cpu_var(current_kprobe
) = p
;
106 static int __kprobes
kprobe_handler(struct pt_regs
*regs
)
109 void *addr
= (void *)regs
->pc
;
112 pr_debug("kprobe_handler: kprobe_running=%d\n",
116 * We don't want to be preempted for the entire
117 * duration of kprobe processing
121 /* Check that we're not recursing */
122 if (kprobe_running()) {
123 p
= get_kprobe(addr
);
125 if (kprobe_status
== KPROBE_HIT_SS
) {
126 printk("FIXME: kprobe hit while single-stepping!\n");
130 printk("FIXME: kprobe hit while handling another kprobe\n");
133 p
= kprobe_running();
134 if (p
->break_handler
&& p
->break_handler(p
, regs
))
137 /* If it's not ours, can't be delete race, (we hold lock). */
141 p
= get_kprobe(addr
);
145 kprobe_status
= KPROBE_HIT_ACTIVE
;
146 set_current_kprobe(p
);
147 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
148 /* handler has already set things up, so skip ss setup */
152 prepare_singlestep(p
, regs
);
153 kprobe_status
= KPROBE_HIT_SS
;
160 static int __kprobes
post_kprobe_handler(struct pt_regs
*regs
)
162 struct kprobe
*cur
= kprobe_running();
164 pr_debug("post_kprobe_handler, cur=%p\n", cur
);
169 if (cur
->post_handler
) {
170 kprobe_status
= KPROBE_HIT_SSDONE
;
171 cur
->post_handler(cur
, regs
, 0);
174 resume_execution(cur
, regs
);
175 reset_current_kprobe();
176 preempt_enable_no_resched();
181 static int __kprobes
kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
183 struct kprobe
*cur
= kprobe_running();
185 pr_debug("kprobe_fault_handler: trapnr=%d\n", trapnr
);
187 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
190 if (kprobe_status
& KPROBE_HIT_SS
) {
191 resume_execution(cur
, regs
);
192 preempt_enable_no_resched();
198 * Wrapper routine to for handling exceptions.
200 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
201 unsigned long val
, void *data
)
203 struct die_args
*args
= (struct die_args
*)data
;
204 int ret
= NOTIFY_DONE
;
206 pr_debug("kprobe_exceptions_notify: val=%lu, data=%p\n",
211 if (kprobe_handler(args
->regs
))
215 if (post_kprobe_handler(args
->regs
))
220 && kprobe_fault_handler(args
->regs
, args
->trapnr
))
230 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
232 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
234 memcpy(&jprobe_saved_regs
, regs
, sizeof(struct pt_regs
));
237 * TODO: We should probably save some of the stack here as
238 * well, since gcc may pass arguments on the stack for certain
239 * functions (lots of arguments, large aggregates, varargs)
242 /* setup return addr to the jprobe handler routine */
243 regs
->pc
= (unsigned long)jp
->entry
;
247 void __kprobes
jprobe_return(void)
249 asm volatile("breakpoint" ::: "memory");
252 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
255 * FIXME - we should ideally be validating that we got here 'cos
256 * of the "trap" in jprobe_return() above, before restoring the
259 memcpy(regs
, &jprobe_saved_regs
, sizeof(struct pt_regs
));
263 int __init
arch_init_kprobes(void)
265 printk("KPROBES: Enabling monitor mode (MM|DBE)...\n");
266 __mtdr(DBGREG_DC
, DC_MM
| DC_DBE
);
268 /* TODO: Register kretprobe trampoline */