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
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
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/kdebug.h>
36 #include <asm/sstep.h>
38 /* kprobe_status settings */
39 #define KPROBE_HIT_ACTIVE 0x00000001
40 #define KPROBE_HIT_SS 0x00000002
42 static struct kprobe *current_kprobe;
43 static unsigned long kprobe_status, kprobe_saved_msr;
44 static struct pt_regs jprobe_saved_regs;
46 int arch_prepare_kprobe(struct kprobe *p)
48 kprobe_opcode_t insn = *p->addr;
50 if (IS_MTMSRD(insn) || IS_RFID(insn))
51 /* cannot put bp on RFID/MTMSRD */
56 void arch_copy_kprobe(struct kprobe *p)
58 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
61 void arch_remove_kprobe(struct kprobe *p)
65 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
68 regs->nip = (unsigned long)p->addr;
71 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
74 /*single step inline if it a breakpoint instruction*/
75 if (p->opcode == BREAKPOINT_INSTRUCTION)
76 regs->nip = (unsigned long)p->addr;
78 regs->nip = (unsigned long)&p->ainsn.insn;
81 static inline int kprobe_handler(struct pt_regs *regs)
85 unsigned int *addr = (unsigned int *)regs->nip;
87 /* Check we're not actually recursing */
88 if (kprobe_running()) {
89 /* We *are* holding lock here, so this is safe.
90 Disarm the probe we just hit, and ignore it. */
93 if (kprobe_status == KPROBE_HIT_SS) {
95 regs->msr |= kprobe_saved_msr;
99 disarm_kprobe(p, regs);
103 if (p->break_handler && p->break_handler(p, regs)) {
107 /* If it's not ours, can't be delete race, (we hold lock). */
112 p = get_kprobe(addr);
115 if (*addr != BREAKPOINT_INSTRUCTION) {
117 * PowerPC has multiple variants of the "trap"
118 * instruction. If the current instruction is a
119 * trap variant, it could belong to someone else
121 kprobe_opcode_t cur_insn = *addr;
122 if (IS_TW(cur_insn) || IS_TD(cur_insn) ||
123 IS_TWI(cur_insn) || IS_TDI(cur_insn))
126 * The breakpoint instruction was removed right
127 * after we hit it. Another cpu has removed
128 * either a probepoint or a debugger breakpoint
129 * at this address. In either case, no further
130 * handling of this interrupt is appropriate.
134 /* Not one of ours: let kernel handle it */
138 kprobe_status = KPROBE_HIT_ACTIVE;
140 kprobe_saved_msr = regs->msr;
141 if (p->pre_handler && p->pre_handler(p, regs))
142 /* handler has already set things up, so skip ss setup */
146 prepare_singlestep(p, regs);
147 kprobe_status = KPROBE_HIT_SS;
149 * This preempt_disable() matches the preempt_enable_no_resched()
150 * in post_kprobe_handler().
160 * Called after single-stepping. p->addr is the address of the
161 * instruction whose first byte has been replaced by the "breakpoint"
162 * instruction. To avoid the SMP problems that can occur when we
163 * temporarily put back the original opcode to single-step, we
164 * single-stepped a copy of the instruction. The address of this
165 * copy is p->ainsn.insn.
167 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
171 regs->nip = (unsigned long)p->addr;
172 ret = emulate_step(regs, p->ainsn.insn[0]);
174 regs->nip = (unsigned long)p->addr + 4;
176 regs->msr &= ~MSR_SE;
179 static inline int post_kprobe_handler(struct pt_regs *regs)
181 if (!kprobe_running())
184 if (current_kprobe->post_handler)
185 current_kprobe->post_handler(current_kprobe, regs, 0);
187 resume_execution(current_kprobe, regs);
188 regs->msr |= kprobe_saved_msr;
191 preempt_enable_no_resched();
194 * if somebody else is singlestepping across a probe point, msr
195 * will have SE set, in which case, continue the remaining processing
196 * of do_debug, as if this is not a probe hit.
198 if (regs->msr & MSR_SE)
204 /* Interrupts disabled, kprobe_lock held. */
205 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
207 if (current_kprobe->fault_handler
208 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
211 if (kprobe_status & KPROBE_HIT_SS) {
212 resume_execution(current_kprobe, regs);
213 regs->msr |= kprobe_saved_msr;
216 preempt_enable_no_resched();
222 * Wrapper routine to for handling exceptions.
224 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
227 struct die_args *args = (struct die_args *)data;
228 int ret = NOTIFY_DONE;
231 * Interrupts are not disabled here. We need to disable
232 * preemption, because kprobe_running() uses smp_processor_id().
237 if (kprobe_handler(args->regs))
241 if (post_kprobe_handler(args->regs))
246 if (kprobe_running() &&
247 kprobe_fault_handler(args->regs, args->trapnr))
257 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
259 struct jprobe *jp = container_of(p, struct jprobe, kp);
261 memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
263 /* setup return addr to the jprobe handler routine */
264 regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
265 regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
270 void jprobe_return(void)
272 asm volatile("trap" ::: "memory");
275 void jprobe_return_end(void)
279 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
282 * FIXME - we should ideally be validating that we got here 'cos
283 * of the "trap" in jprobe_return() above, before restoring the
286 memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));