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