blob: 6071ee99f5cbb975beed8c8100d657b485468966 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Rusty Lynch7e1048b2005-06-23 00:09:25 -070035#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/kdebug.h>
37#include <asm/sstep.h>
38
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070039static DECLARE_MUTEX(kprobe_mutex);
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static struct kprobe *current_kprobe;
42static unsigned long kprobe_status, kprobe_saved_msr;
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -070043static struct kprobe *kprobe_prev;
44static unsigned long kprobe_status_prev, kprobe_saved_msr_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static struct pt_regs jprobe_saved_regs;
46
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -070047int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Ananth N Mavinakayanahalli63224d1e82005-06-08 15:49:41 -070049 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 kprobe_opcode_t insn = *p->addr;
51
Ananth N Mavinakayanahalli63224d1e82005-06-08 15:49:41 -070052 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 }
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070059
60 /* insn must be on a special executable page on ppc64 */
61 if (!ret) {
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070062 down(&kprobe_mutex);
Ananth N Mavinakayanahalli2d8ab6a2005-10-01 13:14:17 -040063 p->ainsn.insn = get_insn_slot();
64 up(&kprobe_mutex);
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070065 if (!p->ainsn.insn)
66 ret = -ENOMEM;
67 }
Ananth N Mavinakayanahalli63224d1e82005-06-08 15:49:41 -070068 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -070071void __kprobes arch_copy_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -070074 p->opcode = *p->addr;
75}
76
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -070077void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070078{
79 *p->addr = BREAKPOINT_INSTRUCTION;
80 flush_icache_range((unsigned long) p->addr,
81 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
82}
83
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -070084void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070085{
86 *p->addr = p->opcode;
87 flush_icache_range((unsigned long) p->addr,
88 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -070091void __kprobes arch_remove_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -070093 down(&kprobe_mutex);
Ananth N Mavinakayanahalli2d8ab6a2005-10-01 13:14:17 -040094 free_insn_slot(p->ainsn.insn);
95 up(&kprobe_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
99{
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700100 kprobe_opcode_t insn = *p->ainsn.insn;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 regs->msr |= MSR_SE;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700103
104 /* single step inline if it is a trap variant */
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700105 if (is_trap(insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 regs->nip = (unsigned long)p->addr;
107 else
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700108 regs->nip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700111static 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
118static 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
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700125void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
126 struct pt_regs *regs)
Rusty Lynch97f79432005-06-27 15:17:15 -0700127{
128 struct kretprobe_instance *ri;
129
130 if ((ri = get_free_rp_inst(rp)) != NULL) {
131 ri->rp = rp;
132 ri->task = current;
133 ri->ret_addr = (kprobe_opcode_t *)regs->link;
134
135 /* Replace the return addr with trampoline addr */
136 regs->link = (unsigned long)kretprobe_trampoline;
137 add_rp_inst(ri);
138 } else {
139 rp->nmissed++;
140 }
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static inline int kprobe_handler(struct pt_regs *regs)
144{
145 struct kprobe *p;
146 int ret = 0;
147 unsigned int *addr = (unsigned int *)regs->nip;
148
149 /* Check we're not actually recursing */
150 if (kprobe_running()) {
151 /* We *are* holding lock here, so this is safe.
152 Disarm the probe we just hit, and ignore it. */
153 p = get_kprobe(addr);
154 if (p) {
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700155 kprobe_opcode_t insn = *p->ainsn.insn;
156 if (kprobe_status == KPROBE_HIT_SS &&
157 is_trap(insn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 regs->msr &= ~MSR_SE;
159 regs->msr |= kprobe_saved_msr;
160 unlock_kprobes();
161 goto no_kprobe;
162 }
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700163 /* We have reentered the kprobe_handler(), since
164 * another probe was hit while within the handler.
165 * We here save the original kprobes variables and
166 * just single step on the instruction of the new probe
167 * without calling any user handlers.
168 */
169 save_previous_kprobe();
170 current_kprobe = p;
171 kprobe_saved_msr = regs->msr;
172 p->nmissed++;
173 prepare_singlestep(p, regs);
174 kprobe_status = KPROBE_REENTER;
175 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 } else {
177 p = current_kprobe;
178 if (p->break_handler && p->break_handler(p, regs)) {
179 goto ss_probe;
180 }
181 }
182 /* If it's not ours, can't be delete race, (we hold lock). */
183 goto no_kprobe;
184 }
185
186 lock_kprobes();
187 p = get_kprobe(addr);
188 if (!p) {
189 unlock_kprobes();
190 if (*addr != BREAKPOINT_INSTRUCTION) {
191 /*
192 * PowerPC has multiple variants of the "trap"
193 * instruction. If the current instruction is a
194 * trap variant, it could belong to someone else
195 */
196 kprobe_opcode_t cur_insn = *addr;
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700197 if (is_trap(cur_insn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto no_kprobe;
199 /*
200 * The breakpoint instruction was removed right
201 * after we hit it. Another cpu has removed
202 * either a probepoint or a debugger breakpoint
203 * at this address. In either case, no further
204 * handling of this interrupt is appropriate.
205 */
206 ret = 1;
207 }
208 /* Not one of ours: let kernel handle it */
209 goto no_kprobe;
210 }
211
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800212 /*
213 * This preempt_disable() matches the preempt_enable_no_resched()
214 * in post_kprobe_handler().
215 */
216 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 kprobe_status = KPROBE_HIT_ACTIVE;
218 current_kprobe = p;
219 kprobe_saved_msr = regs->msr;
220 if (p->pre_handler && p->pre_handler(p, regs))
221 /* handler has already set things up, so skip ss setup */
222 return 1;
223
224ss_probe:
225 prepare_singlestep(p, regs);
226 kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 1;
228
229no_kprobe:
230 return ret;
231}
232
233/*
Rusty Lynch97f79432005-06-27 15:17:15 -0700234 * Function return probe trampoline:
235 * - init_kprobes() establishes a probepoint here
236 * - When the probed function returns, this probe
237 * causes the handlers to fire
238 */
239void kretprobe_trampoline_holder(void)
240{
241 asm volatile(".global kretprobe_trampoline\n"
242 "kretprobe_trampoline:\n"
243 "nop\n");
244}
245
246/*
247 * Called when the probe at kretprobe trampoline is hit
248 */
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700249int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Rusty Lynch97f79432005-06-27 15:17:15 -0700250{
251 struct kretprobe_instance *ri = NULL;
252 struct hlist_head *head;
253 struct hlist_node *node, *tmp;
254 unsigned long orig_ret_address = 0;
255 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
256
257 head = kretprobe_inst_table_head(current);
258
259 /*
260 * It is possible to have multiple instances associated with a given
261 * task either because an multiple functions in the call path
262 * have a return probe installed on them, and/or more then one return
263 * return probe was registered for a target function.
264 *
265 * We can handle this because:
266 * - instances are always inserted at the head of the list
267 * - when multiple return probes are registered for the same
268 * function, the first instance's ret_addr will point to the
269 * real return address, and all the rest will point to
270 * kretprobe_trampoline
271 */
272 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
273 if (ri->task != current)
274 /* another task is sharing our hash bucket */
275 continue;
276
277 if (ri->rp && ri->rp->handler)
278 ri->rp->handler(ri, regs);
279
280 orig_ret_address = (unsigned long)ri->ret_addr;
281 recycle_rp_inst(ri);
282
283 if (orig_ret_address != trampoline_address)
284 /*
285 * This is the real return address. Any other
286 * instances associated with this task are for
287 * other calls deeper on the call stack
288 */
289 break;
290 }
291
292 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
293 regs->nip = orig_ret_address;
294
295 unlock_kprobes();
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800296 preempt_enable_no_resched();
Rusty Lynch97f79432005-06-27 15:17:15 -0700297
298 /*
299 * By returning a non-zero value, we are telling
300 * kprobe_handler() that we have handled unlocking
301 * and re-enabling preemption.
302 */
303 return 1;
304}
305
306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * Called after single-stepping. p->addr is the address of the
308 * instruction whose first byte has been replaced by the "breakpoint"
309 * instruction. To avoid the SMP problems that can occur when we
310 * temporarily put back the original opcode to single-step, we
311 * single-stepped a copy of the instruction. The address of this
312 * copy is p->ainsn.insn.
313 */
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700314static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 int ret;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700317 unsigned int insn = *p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 regs->nip = (unsigned long)p->addr;
Ananth N Mavinakayanahalli9ec4b1f2005-06-27 15:17:01 -0700320 ret = emulate_step(regs, insn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (ret == 0)
322 regs->nip = (unsigned long)p->addr + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325static inline int post_kprobe_handler(struct pt_regs *regs)
326{
327 if (!kprobe_running())
328 return 0;
329
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700330 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
331 kprobe_status = KPROBE_HIT_SSDONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 current_kprobe->post_handler(current_kprobe, regs, 0);
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 resume_execution(current_kprobe, regs);
336 regs->msr |= kprobe_saved_msr;
337
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700338 /*Restore back the original saved kprobes variables and continue. */
339 if (kprobe_status == KPROBE_REENTER) {
340 restore_previous_kprobe();
341 goto out;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unlock_kprobes();
Prasanna S Panchamukhi42cc2062005-06-23 00:09:38 -0700344out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 preempt_enable_no_resched();
346
347 /*
348 * if somebody else is singlestepping across a probe point, msr
349 * will have SE set, in which case, continue the remaining processing
350 * of do_debug, as if this is not a probe hit.
351 */
352 if (regs->msr & MSR_SE)
353 return 0;
354
355 return 1;
356}
357
358/* Interrupts disabled, kprobe_lock held. */
359static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
360{
361 if (current_kprobe->fault_handler
362 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
363 return 1;
364
365 if (kprobe_status & KPROBE_HIT_SS) {
366 resume_execution(current_kprobe, regs);
Ananth N Mavinakayanahallif829fd22005-06-08 15:50:00 -0700367 regs->msr &= ~MSR_SE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 regs->msr |= kprobe_saved_msr;
369
370 unlock_kprobes();
371 preempt_enable_no_resched();
372 }
373 return 0;
374}
375
376/*
377 * Wrapper routine to for handling exceptions.
378 */
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700379int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
380 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct die_args *args = (struct die_args *)data;
383 int ret = NOTIFY_DONE;
384
385 /*
386 * Interrupts are not disabled here. We need to disable
387 * preemption, because kprobe_running() uses smp_processor_id().
388 */
389 preempt_disable();
390 switch (val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 case DIE_BPT:
392 if (kprobe_handler(args->regs))
393 ret = NOTIFY_STOP;
394 break;
395 case DIE_SSTEP:
396 if (post_kprobe_handler(args->regs))
397 ret = NOTIFY_STOP;
398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 case DIE_PAGE_FAULT:
400 if (kprobe_running() &&
401 kprobe_fault_handler(args->regs, args->trapnr))
402 ret = NOTIFY_STOP;
403 break;
404 default:
405 break;
406 }
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700407 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return ret;
409}
410
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700411int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct jprobe *jp = container_of(p, struct jprobe, kp);
414
415 memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs));
416
417 /* setup return addr to the jprobe handler routine */
418 regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry);
419 regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc);
420
421 return 1;
422}
423
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700424void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 asm volatile("trap" ::: "memory");
427}
428
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700429void __kprobes jprobe_return_end(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431};
432
Prasanna S Panchamukhibb144a82005-09-06 15:19:29 -0700433int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 /*
436 * FIXME - we should ideally be validating that we got here 'cos
437 * of the "trap" in jprobe_return() above, before restoring the
438 * saved regs...
439 */
440 memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs));
441 return 1;
442}
Rusty Lynch97f79432005-06-27 15:17:15 -0700443
444static struct kprobe trampoline_p = {
445 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
446 .pre_handler = trampoline_probe_handler
447};
448
Rusty Lynch67729262005-07-05 18:54:50 -0700449int __init arch_init_kprobes(void)
Rusty Lynch97f79432005-06-27 15:17:15 -0700450{
451 return register_kprobe(&trampoline_p);
452}