[PATCH] kprobes: Temporary disarming of reentrant probe for ppc64
[linux-2.6.git] / arch / ppc64 / kernel / kprobes.c
1 /*
2  *  Kernel Probes (KProbes)
3  *  arch/ppc64/kernel/kprobes.c
4  *
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.
9  *
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.
14  *
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.
18  *
19  * Copyright (C) IBM Corporation, 2002, 2004
20  *
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
28  */
29
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>
38
39 static struct kprobe *current_kprobe;
40 static unsigned long kprobe_status, kprobe_saved_msr;
41 static struct kprobe *kprobe_prev;
42 static unsigned long kprobe_status_prev, kprobe_saved_msr_prev;
43 static struct pt_regs jprobe_saved_regs;
44
45 int arch_prepare_kprobe(struct kprobe *p)
46 {
47         int ret = 0;
48         kprobe_opcode_t insn = *p->addr;
49
50         if ((unsigned long)p->addr & 0x03) {
51                 printk("Attempt to register kprobe at an unaligned address\n");
52                 ret = -EINVAL;
53         } else if (IS_MTMSRD(insn) || IS_RFID(insn)) {
54                 printk("Cannot register a kprobe on rfid or mtmsrd\n");
55                 ret = -EINVAL;
56         }
57         return ret;
58 }
59
60 void arch_copy_kprobe(struct kprobe *p)
61 {
62         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
63         p->opcode = *p->addr;
64 }
65
66 void arch_arm_kprobe(struct kprobe *p)
67 {
68         *p->addr = BREAKPOINT_INSTRUCTION;
69         flush_icache_range((unsigned long) p->addr,
70                            (unsigned long) p->addr + sizeof(kprobe_opcode_t));
71 }
72
73 void arch_disarm_kprobe(struct kprobe *p)
74 {
75         *p->addr = p->opcode;
76         flush_icache_range((unsigned long) p->addr,
77                            (unsigned long) p->addr + sizeof(kprobe_opcode_t));
78 }
79
80 void arch_remove_kprobe(struct kprobe *p)
81 {
82 }
83
84 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
85 {
86         regs->msr |= MSR_SE;
87         /*single step inline if it a breakpoint instruction*/
88         if (p->opcode == BREAKPOINT_INSTRUCTION)
89                 regs->nip = (unsigned long)p->addr;
90         else
91                 regs->nip = (unsigned long)&p->ainsn.insn;
92 }
93
94 static inline void save_previous_kprobe(void)
95 {
96         kprobe_prev = current_kprobe;
97         kprobe_status_prev = kprobe_status;
98         kprobe_saved_msr_prev = kprobe_saved_msr;
99 }
100
101 static inline void restore_previous_kprobe(void)
102 {
103         current_kprobe = kprobe_prev;
104         kprobe_status = kprobe_status_prev;
105         kprobe_saved_msr = kprobe_saved_msr_prev;
106 }
107
108 static inline int kprobe_handler(struct pt_regs *regs)
109 {
110         struct kprobe *p;
111         int ret = 0;
112         unsigned int *addr = (unsigned int *)regs->nip;
113
114         /* Check we're not actually recursing */
115         if (kprobe_running()) {
116                 /* We *are* holding lock here, so this is safe.
117                    Disarm the probe we just hit, and ignore it. */
118                 p = get_kprobe(addr);
119                 if (p) {
120                         if (kprobe_status == KPROBE_HIT_SS) {
121                                 regs->msr &= ~MSR_SE;
122                                 regs->msr |= kprobe_saved_msr;
123                                 unlock_kprobes();
124                                 goto no_kprobe;
125                         }
126                         /* We have reentered the kprobe_handler(), since
127                          * another probe was hit while within the handler.
128                          * We here save the original kprobes variables and
129                          * just single step on the instruction of the new probe
130                          * without calling any user handlers.
131                          */
132                         save_previous_kprobe();
133                         current_kprobe = p;
134                         kprobe_saved_msr = regs->msr;
135                         p->nmissed++;
136                         prepare_singlestep(p, regs);
137                         kprobe_status = KPROBE_REENTER;
138                         return 1;
139                 } else {
140                         p = current_kprobe;
141                         if (p->break_handler && p->break_handler(p, regs)) {
142                                 goto ss_probe;
143                         }
144                 }
145                 /* If it's not ours, can't be delete race, (we hold lock). */
146                 goto no_kprobe;
147         }
148
149         lock_kprobes();
150         p = get_kprobe(addr);
151         if (!p) {
152                 unlock_kprobes();
153                 if (*addr != BREAKPOINT_INSTRUCTION) {
154                         /*
155                          * PowerPC has multiple variants of the "trap"
156                          * instruction. If the current instruction is a
157                          * trap variant, it could belong to someone else
158                          */
159                         kprobe_opcode_t cur_insn = *addr;
160                         if (IS_TW(cur_insn) || IS_TD(cur_insn) ||
161                                         IS_TWI(cur_insn) || IS_TDI(cur_insn))
162                                 goto no_kprobe;
163                         /*
164                          * The breakpoint instruction was removed right
165                          * after we hit it.  Another cpu has removed
166                          * either a probepoint or a debugger breakpoint
167                          * at this address.  In either case, no further
168                          * handling of this interrupt is appropriate.
169                          */
170                         ret = 1;
171                 }
172                 /* Not one of ours: let kernel handle it */
173                 goto no_kprobe;
174         }
175
176         kprobe_status = KPROBE_HIT_ACTIVE;
177         current_kprobe = p;
178         kprobe_saved_msr = regs->msr;
179         if (p->pre_handler && p->pre_handler(p, regs))
180                 /* handler has already set things up, so skip ss setup */
181                 return 1;
182
183 ss_probe:
184         prepare_singlestep(p, regs);
185         kprobe_status = KPROBE_HIT_SS;
186         /*
187          * This preempt_disable() matches the preempt_enable_no_resched()
188          * in post_kprobe_handler().
189          */
190         preempt_disable();
191         return 1;
192
193 no_kprobe:
194         return ret;
195 }
196
197 /*
198  * Called after single-stepping.  p->addr is the address of the
199  * instruction whose first byte has been replaced by the "breakpoint"
200  * instruction.  To avoid the SMP problems that can occur when we
201  * temporarily put back the original opcode to single-step, we
202  * single-stepped a copy of the instruction.  The address of this
203  * copy is p->ainsn.insn.
204  */
205 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
206 {
207         int ret;
208
209         regs->nip = (unsigned long)p->addr;
210         ret = emulate_step(regs, p->ainsn.insn[0]);
211         if (ret == 0)
212                 regs->nip = (unsigned long)p->addr + 4;
213 }
214
215 static inline int post_kprobe_handler(struct pt_regs *regs)
216 {
217         if (!kprobe_running())
218                 return 0;
219
220         if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
221                 kprobe_status = KPROBE_HIT_SSDONE;
222                 current_kprobe->post_handler(current_kprobe, regs, 0);
223         }
224
225         resume_execution(current_kprobe, regs);
226         regs->msr |= kprobe_saved_msr;
227
228         /*Restore back the original saved kprobes variables and continue. */
229         if (kprobe_status == KPROBE_REENTER) {
230                 restore_previous_kprobe();
231                 goto out;
232         }
233         unlock_kprobes();
234 out:
235         preempt_enable_no_resched();
236
237         /*
238          * if somebody else is singlestepping across a probe point, msr
239          * will have SE set, in which case, continue the remaining processing
240          * of do_debug, as if this is not a probe hit.
241          */
242         if (regs->msr & MSR_SE)
243                 return 0;
244
245         return 1;
246 }
247
248 /* Interrupts disabled, kprobe_lock held. */
249 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
250 {
251         if (current_kprobe->fault_handler
252             && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
253                 return 1;
254
255         if (kprobe_status & KPROBE_HIT_SS) {
256                 resume_execution(current_kprobe, regs);
257                 regs->msr &= ~MSR_SE;
258                 regs->msr |= kprobe_saved_msr;
259
260                 unlock_kprobes();
261                 preempt_enable_no_resched();
262         }
263         return 0;
264 }
265
266 /*
267  * Wrapper routine to for handling exceptions.
268  */
269 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
270                              void *data)
271 {
272         struct die_args *args = (struct die_args *)data;
273         int ret = NOTIFY_DONE;
274
275         /*
276          * Interrupts are not disabled here.  We need to disable
277          * preemption, because kprobe_running() uses smp_processor_id().
278          */
279         preempt_disable();
280         switch (val) {
281         case DIE_BPT:
282                 if (kprobe_handler(args->regs))
283                         ret = NOTIFY_STOP;
284                 break;
285         case DIE_SSTEP:
286                 if (post_kprobe_handler(args->regs))
287                         ret = NOTIFY_STOP;
288                 break;
289         case DIE_GPF:
290         case DIE_PAGE_FAULT:
291                 if (kprobe_running() &&
292                     kprobe_fault_handler(args->regs, args->trapnr))
293                         ret = NOTIFY_STOP;
294                 break;
295         default:
296                 break;
297         }
298         preempt_enable();
299         return ret;
300 }
301
302 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
303 {
304         struct jprobe *jp = container_of(p, struct jprobe, kp);
305
306         memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
307
308         /* setup return addr to the jprobe handler routine */
309         regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
310         regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
311
312         return 1;
313 }
314
315 void jprobe_return(void)
316 {
317         asm volatile("trap" ::: "memory");
318 }
319
320 void jprobe_return_end(void)
321 {
322 };
323
324 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
325 {
326         /*
327          * FIXME - we should ideally be validating that we got here 'cos
328          * of the "trap" in jprobe_return() above, before restoring the
329          * saved regs...
330          */
331         memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));
332         return 1;
333 }