- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.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/kdebug.h>
36 #include <asm/sstep.h>
37
38 /* kprobe_status settings */
39 #define KPROBE_HIT_ACTIVE       0x00000001
40 #define KPROBE_HIT_SS           0x00000002
41
42 static struct kprobe *current_kprobe;
43 static unsigned long kprobe_status, kprobe_saved_msr;
44 static struct pt_regs jprobe_saved_regs;
45
46 int arch_prepare_kprobe(struct kprobe *p)
47 {
48         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
49         if (IS_MTMSRD(p->ainsn.insn[0]) || IS_RFID(p->ainsn.insn[0]))
50                 /* cannot put bp on RFID/MTMSRD */
51                 return 1;
52         return 0;
53 }
54
55 void arch_remove_kprobe(struct kprobe *p)
56 {
57 }
58
59 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
60 {
61         *p->addr = p->opcode;
62         regs->nip = (unsigned long)p->addr;
63 }
64
65 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
66 {
67         regs->msr |= MSR_SE;
68         regs->nip = (unsigned long)&p->ainsn.insn;
69 }
70
71 static inline int kprobe_handler(struct pt_regs *regs)
72 {
73         struct kprobe *p;
74         int ret = 0;
75         unsigned int *addr = (unsigned int *)regs->nip;
76
77         /* We're in an interrupt, but this is clear and BUG()-safe. */
78         preempt_disable();
79
80         /* Check we're not actually recursing */
81         if (kprobe_running()) {
82                 /* We *are* holding lock here, so this is safe.
83                    Disarm the probe we just hit, and ignore it. */
84                 p = get_kprobe(addr);
85                 if (p) {
86                         disarm_kprobe(p, regs);
87                         ret = 1;
88                 } else {
89                         p = current_kprobe;
90                         if (p->break_handler && p->break_handler(p, regs)) {
91                                 goto ss_probe;
92                         }
93                 }
94                 /* If it's not ours, can't be delete race, (we hold lock). */
95                 goto no_kprobe;
96         }
97
98         lock_kprobes();
99         p = get_kprobe(addr);
100         if (!p) {
101                 unlock_kprobes();
102 #if 0
103                 if (*addr != BREAKPOINT_INSTRUCTION) {
104                         /*
105                          * The breakpoint instruction was removed right
106                          * after we hit it.  Another cpu has removed
107                          * either a probepoint or a debugger breakpoint
108                          * at this address.  In either case, no further
109                          * handling of this interrupt is appropriate.
110                          */
111                         ret = 1;
112                 }
113 #endif
114                 /* Not one of ours: let kernel handle it */
115                 goto no_kprobe;
116         }
117
118         kprobe_status = KPROBE_HIT_ACTIVE;
119         current_kprobe = p;
120         kprobe_saved_msr = regs->msr;
121         if (p->pre_handler(p, regs)) {
122                 /* handler has already set things up, so skip ss setup */
123                 return 1;
124         }
125
126 ss_probe:
127         prepare_singlestep(p, regs);
128         kprobe_status = KPROBE_HIT_SS;
129         return 1;
130
131 no_kprobe:
132         preempt_enable_no_resched();
133         return ret;
134 }
135
136 /*
137  * Called after single-stepping.  p->addr is the address of the
138  * instruction whose first byte has been replaced by the "breakpoint"
139  * instruction.  To avoid the SMP problems that can occur when we
140  * temporarily put back the original opcode to single-step, we
141  * single-stepped a copy of the instruction.  The address of this
142  * copy is p->ainsn.insn.
143  */
144 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
145 {
146         int ret;
147
148         regs->nip = (unsigned long)p->addr;
149         ret = emulate_step(regs, p->ainsn.insn[0]);
150         if (ret == 0)
151                 regs->nip = (unsigned long)p->addr + 4;
152
153         regs->msr &= ~MSR_SE;
154 }
155
156 static inline int post_kprobe_handler(struct pt_regs *regs)
157 {
158         if (!kprobe_running())
159                 return 0;
160
161         if (current_kprobe->post_handler)
162                 current_kprobe->post_handler(current_kprobe, regs, 0);
163
164         resume_execution(current_kprobe, regs);
165         regs->msr |= kprobe_saved_msr;
166
167         unlock_kprobes();
168         preempt_enable_no_resched();
169
170         /*
171          * if somebody else is singlestepping across a probe point, msr
172          * will have SE set, in which case, continue the remaining processing
173          * of do_debug, as if this is not a probe hit.
174          */
175         if (regs->msr & MSR_SE)
176                 return 0;
177
178         return 1;
179 }
180
181 /* Interrupts disabled, kprobe_lock held. */
182 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
183 {
184         if (current_kprobe->fault_handler
185             && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
186                 return 1;
187
188         if (kprobe_status & KPROBE_HIT_SS) {
189                 resume_execution(current_kprobe, regs);
190                 regs->msr |= kprobe_saved_msr;
191
192                 unlock_kprobes();
193                 preempt_enable_no_resched();
194         }
195         return 0;
196 }
197
198 /*
199  * Wrapper routine to for handling exceptions.
200  */
201 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
202                              void *data)
203 {
204         struct die_args *args = (struct die_args *)data;
205         switch (val) {
206         case DIE_IABR_MATCH:
207         case DIE_DABR_MATCH:
208         case DIE_BPT:
209                 if (kprobe_handler(args->regs))
210                         return NOTIFY_STOP;
211                 break;
212         case DIE_SSTEP:
213                 if (post_kprobe_handler(args->regs))
214                         return NOTIFY_STOP;
215                 break;
216         case DIE_GPF:
217         case DIE_PAGE_FAULT:
218                 if (kprobe_running() &&
219                     kprobe_fault_handler(args->regs, args->trapnr))
220                         return NOTIFY_STOP;
221                 break;
222         default:
223                 break;
224         }
225         return NOTIFY_DONE;
226 }
227
228 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
229 {
230         struct jprobe *jp = container_of(p, struct jprobe, kp);
231
232         memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
233
234         /* setup return addr to the jprobe handler routine */
235         regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
236         regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
237
238         return 1;
239 }
240
241 void jprobe_return(void)
242 {
243         preempt_enable_no_resched();
244         asm volatile("trap" ::: "memory");
245 }
246
247 void jprobe_return_end(void)
248 {
249 };
250
251 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
252 {
253         /*
254          * FIXME - we should ideally be validating that we got here 'cos
255          * of the "trap" in jprobe_return() above, before restoring the
256          * saved regs...
257          */
258         memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));
259         return 1;
260 }