[NETLINK]: w1_int.c: fix default netlink group
[linux-2.6/verdex.git] / arch / ppc64 / kernel / kprobes.c
bloba3d519518fb8dfe41f2bcf7450bd2d5546bc3987
1 /*
2 * Kernel Probes (KProbes)
3 * arch/ppc64/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
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
27 * for PPC64
30 #include <linux/config.h>
31 #include <linux/kprobes.h>
32 #include <linux/ptrace.h>
33 #include <linux/spinlock.h>
34 #include <linux/preempt.h>
35 #include <asm/cacheflush.h>
36 #include <asm/kdebug.h>
37 #include <asm/sstep.h>
39 static DECLARE_MUTEX(kprobe_mutex);
41 static struct kprobe *current_kprobe;
42 static unsigned long kprobe_status, kprobe_saved_msr;
43 static struct kprobe *kprobe_prev;
44 static unsigned long kprobe_status_prev, kprobe_saved_msr_prev;
45 static struct pt_regs jprobe_saved_regs;
47 int arch_prepare_kprobe(struct kprobe *p)
49 int ret = 0;
50 kprobe_opcode_t insn = *p->addr;
52 if ((unsigned long)p->addr & 0x03) {
53 printk("Attempt to register kprobe at an unaligned address\n");
54 ret = -EINVAL;
55 } else if (IS_MTMSRD(insn) || IS_RFID(insn)) {
56 printk("Cannot register a kprobe on rfid or mtmsrd\n");
57 ret = -EINVAL;
60 /* insn must be on a special executable page on ppc64 */
61 if (!ret) {
62 up(&kprobe_mutex);
63 p->ainsn.insn = get_insn_slot();
64 down(&kprobe_mutex);
65 if (!p->ainsn.insn)
66 ret = -ENOMEM;
68 return ret;
71 void arch_copy_kprobe(struct kprobe *p)
73 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
74 p->opcode = *p->addr;
77 void arch_arm_kprobe(struct kprobe *p)
79 *p->addr = BREAKPOINT_INSTRUCTION;
80 flush_icache_range((unsigned long) p->addr,
81 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
84 void arch_disarm_kprobe(struct kprobe *p)
86 *p->addr = p->opcode;
87 flush_icache_range((unsigned long) p->addr,
88 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
91 void arch_remove_kprobe(struct kprobe *p)
93 up(&kprobe_mutex);
94 free_insn_slot(p->ainsn.insn);
95 down(&kprobe_mutex);
98 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
100 kprobe_opcode_t insn = *p->ainsn.insn;
102 regs->msr |= MSR_SE;
104 /* single step inline if it is a trap variant */
105 if (IS_TW(insn) || IS_TD(insn) || IS_TWI(insn) || IS_TDI(insn))
106 regs->nip = (unsigned long)p->addr;
107 else
108 regs->nip = (unsigned long)p->ainsn.insn;
111 static inline void save_previous_kprobe(void)
113 kprobe_prev = current_kprobe;
114 kprobe_status_prev = kprobe_status;
115 kprobe_saved_msr_prev = kprobe_saved_msr;
118 static inline void restore_previous_kprobe(void)
120 current_kprobe = kprobe_prev;
121 kprobe_status = kprobe_status_prev;
122 kprobe_saved_msr = kprobe_saved_msr_prev;
125 void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
127 struct kretprobe_instance *ri;
129 if ((ri = get_free_rp_inst(rp)) != NULL) {
130 ri->rp = rp;
131 ri->task = current;
132 ri->ret_addr = (kprobe_opcode_t *)regs->link;
134 /* Replace the return addr with trampoline addr */
135 regs->link = (unsigned long)kretprobe_trampoline;
136 add_rp_inst(ri);
137 } else {
138 rp->nmissed++;
142 static inline int kprobe_handler(struct pt_regs *regs)
144 struct kprobe *p;
145 int ret = 0;
146 unsigned int *addr = (unsigned int *)regs->nip;
148 /* Check we're not actually recursing */
149 if (kprobe_running()) {
150 /* We *are* holding lock here, so this is safe.
151 Disarm the probe we just hit, and ignore it. */
152 p = get_kprobe(addr);
153 if (p) {
154 if (kprobe_status == KPROBE_HIT_SS) {
155 regs->msr &= ~MSR_SE;
156 regs->msr |= kprobe_saved_msr;
157 unlock_kprobes();
158 goto no_kprobe;
160 /* We have reentered the kprobe_handler(), since
161 * another probe was hit while within the handler.
162 * We here save the original kprobes variables and
163 * just single step on the instruction of the new probe
164 * without calling any user handlers.
166 save_previous_kprobe();
167 current_kprobe = p;
168 kprobe_saved_msr = regs->msr;
169 p->nmissed++;
170 prepare_singlestep(p, regs);
171 kprobe_status = KPROBE_REENTER;
172 return 1;
173 } else {
174 p = current_kprobe;
175 if (p->break_handler && p->break_handler(p, regs)) {
176 goto ss_probe;
179 /* If it's not ours, can't be delete race, (we hold lock). */
180 goto no_kprobe;
183 lock_kprobes();
184 p = get_kprobe(addr);
185 if (!p) {
186 unlock_kprobes();
187 if (*addr != BREAKPOINT_INSTRUCTION) {
189 * PowerPC has multiple variants of the "trap"
190 * instruction. If the current instruction is a
191 * trap variant, it could belong to someone else
193 kprobe_opcode_t cur_insn = *addr;
194 if (IS_TW(cur_insn) || IS_TD(cur_insn) ||
195 IS_TWI(cur_insn) || IS_TDI(cur_insn))
196 goto no_kprobe;
198 * The breakpoint instruction was removed right
199 * after we hit it. Another cpu has removed
200 * either a probepoint or a debugger breakpoint
201 * at this address. In either case, no further
202 * handling of this interrupt is appropriate.
204 ret = 1;
206 /* Not one of ours: let kernel handle it */
207 goto no_kprobe;
210 kprobe_status = KPROBE_HIT_ACTIVE;
211 current_kprobe = p;
212 kprobe_saved_msr = regs->msr;
213 if (p->pre_handler && p->pre_handler(p, regs))
214 /* handler has already set things up, so skip ss setup */
215 return 1;
217 ss_probe:
218 prepare_singlestep(p, regs);
219 kprobe_status = KPROBE_HIT_SS;
221 * This preempt_disable() matches the preempt_enable_no_resched()
222 * in post_kprobe_handler().
224 preempt_disable();
225 return 1;
227 no_kprobe:
228 return ret;
232 * Function return probe trampoline:
233 * - init_kprobes() establishes a probepoint here
234 * - When the probed function returns, this probe
235 * causes the handlers to fire
237 void kretprobe_trampoline_holder(void)
239 asm volatile(".global kretprobe_trampoline\n"
240 "kretprobe_trampoline:\n"
241 "nop\n");
245 * Called when the probe at kretprobe trampoline is hit
247 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
249 struct kretprobe_instance *ri = NULL;
250 struct hlist_head *head;
251 struct hlist_node *node, *tmp;
252 unsigned long orig_ret_address = 0;
253 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
255 head = kretprobe_inst_table_head(current);
258 * It is possible to have multiple instances associated with a given
259 * task either because an multiple functions in the call path
260 * have a return probe installed on them, and/or more then one return
261 * return probe was registered for a target function.
263 * We can handle this because:
264 * - instances are always inserted at the head of the list
265 * - when multiple return probes are registered for the same
266 * function, the first instance's ret_addr will point to the
267 * real return address, and all the rest will point to
268 * kretprobe_trampoline
270 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
271 if (ri->task != current)
272 /* another task is sharing our hash bucket */
273 continue;
275 if (ri->rp && ri->rp->handler)
276 ri->rp->handler(ri, regs);
278 orig_ret_address = (unsigned long)ri->ret_addr;
279 recycle_rp_inst(ri);
281 if (orig_ret_address != trampoline_address)
283 * This is the real return address. Any other
284 * instances associated with this task are for
285 * other calls deeper on the call stack
287 break;
290 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
291 regs->nip = orig_ret_address;
293 unlock_kprobes();
296 * By returning a non-zero value, we are telling
297 * kprobe_handler() that we have handled unlocking
298 * and re-enabling preemption.
300 return 1;
304 * Called after single-stepping. p->addr is the address of the
305 * instruction whose first byte has been replaced by the "breakpoint"
306 * instruction. To avoid the SMP problems that can occur when we
307 * temporarily put back the original opcode to single-step, we
308 * single-stepped a copy of the instruction. The address of this
309 * copy is p->ainsn.insn.
311 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
313 int ret;
314 unsigned int insn = *p->ainsn.insn;
316 regs->nip = (unsigned long)p->addr;
317 ret = emulate_step(regs, insn);
318 if (ret == 0)
319 regs->nip = (unsigned long)p->addr + 4;
322 static inline int post_kprobe_handler(struct pt_regs *regs)
324 if (!kprobe_running())
325 return 0;
327 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
328 kprobe_status = KPROBE_HIT_SSDONE;
329 current_kprobe->post_handler(current_kprobe, regs, 0);
332 resume_execution(current_kprobe, regs);
333 regs->msr |= kprobe_saved_msr;
335 /*Restore back the original saved kprobes variables and continue. */
336 if (kprobe_status == KPROBE_REENTER) {
337 restore_previous_kprobe();
338 goto out;
340 unlock_kprobes();
341 out:
342 preempt_enable_no_resched();
345 * if somebody else is singlestepping across a probe point, msr
346 * will have SE set, in which case, continue the remaining processing
347 * of do_debug, as if this is not a probe hit.
349 if (regs->msr & MSR_SE)
350 return 0;
352 return 1;
355 /* Interrupts disabled, kprobe_lock held. */
356 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
358 if (current_kprobe->fault_handler
359 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
360 return 1;
362 if (kprobe_status & KPROBE_HIT_SS) {
363 resume_execution(current_kprobe, regs);
364 regs->msr &= ~MSR_SE;
365 regs->msr |= kprobe_saved_msr;
367 unlock_kprobes();
368 preempt_enable_no_resched();
370 return 0;
374 * Wrapper routine to for handling exceptions.
376 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
377 void *data)
379 struct die_args *args = (struct die_args *)data;
380 int ret = NOTIFY_DONE;
383 * Interrupts are not disabled here. We need to disable
384 * preemption, because kprobe_running() uses smp_processor_id().
386 preempt_disable();
387 switch (val) {
388 case DIE_BPT:
389 if (kprobe_handler(args->regs))
390 ret = NOTIFY_STOP;
391 break;
392 case DIE_SSTEP:
393 if (post_kprobe_handler(args->regs))
394 ret = NOTIFY_STOP;
395 break;
396 case DIE_GPF:
397 case DIE_PAGE_FAULT:
398 if (kprobe_running() &&
399 kprobe_fault_handler(args->regs, args->trapnr))
400 ret = NOTIFY_STOP;
401 break;
402 default:
403 break;
405 preempt_enable();
406 return ret;
409 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
411 struct jprobe *jp = container_of(p, struct jprobe, kp);
413 memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
415 /* setup return addr to the jprobe handler routine */
416 regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
417 regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
419 return 1;
422 void jprobe_return(void)
424 asm volatile("trap" ::: "memory");
427 void jprobe_return_end(void)
431 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
434 * FIXME - we should ideally be validating that we got here 'cos
435 * of the "trap" in jprobe_return() above, before restoring the
436 * saved regs...
438 memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));
439 return 1;
442 static struct kprobe trampoline_p = {
443 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
444 .pre_handler = trampoline_probe_handler
447 int __init arch_init_kprobes(void)
449 return register_kprobe(&trampoline_p);