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/preempt.h>
34 #include <asm/cacheflush.h>
35 #include <asm/kdebug.h>
36 #include <asm/sstep.h>
38 static DECLARE_MUTEX(kprobe_mutex
);
39 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
) = NULL
;
40 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
42 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
45 kprobe_opcode_t insn
= *p
->addr
;
47 if ((unsigned long)p
->addr
& 0x03) {
48 printk("Attempt to register kprobe at an unaligned address\n");
50 } else if (IS_MTMSRD(insn
) || IS_RFID(insn
)) {
51 printk("Cannot register a kprobe on rfid or mtmsrd\n");
55 /* insn must be on a special executable page on ppc64 */
58 p
->ainsn
.insn
= get_insn_slot();
66 void __kprobes
arch_copy_kprobe(struct kprobe
*p
)
68 memcpy(p
->ainsn
.insn
, p
->addr
, MAX_INSN_SIZE
* sizeof(kprobe_opcode_t
));
72 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
74 *p
->addr
= BREAKPOINT_INSTRUCTION
;
75 flush_icache_range((unsigned long) p
->addr
,
76 (unsigned long) p
->addr
+ sizeof(kprobe_opcode_t
));
79 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
82 flush_icache_range((unsigned long) p
->addr
,
83 (unsigned long) p
->addr
+ sizeof(kprobe_opcode_t
));
86 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
89 free_insn_slot(p
->ainsn
.insn
);
93 static inline void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
95 kprobe_opcode_t insn
= *p
->ainsn
.insn
;
99 /* single step inline if it is a trap variant */
101 regs
->nip
= (unsigned long)p
->addr
;
103 regs
->nip
= (unsigned long)p
->ainsn
.insn
;
106 static inline void save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
108 kcb
->prev_kprobe
.kp
= kprobe_running();
109 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
110 kcb
->prev_kprobe
.saved_msr
= kcb
->kprobe_saved_msr
;
113 static inline void restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
115 __get_cpu_var(current_kprobe
) = kcb
->prev_kprobe
.kp
;
116 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
117 kcb
->kprobe_saved_msr
= kcb
->prev_kprobe
.saved_msr
;
120 static inline void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
121 struct kprobe_ctlblk
*kcb
)
123 __get_cpu_var(current_kprobe
) = p
;
124 kcb
->kprobe_saved_msr
= regs
->msr
;
127 /* Called with kretprobe_lock held */
128 void __kprobes
arch_prepare_kretprobe(struct kretprobe
*rp
,
129 struct pt_regs
*regs
)
131 struct kretprobe_instance
*ri
;
133 if ((ri
= get_free_rp_inst(rp
)) != NULL
) {
136 ri
->ret_addr
= (kprobe_opcode_t
*)regs
->link
;
138 /* Replace the return addr with trampoline addr */
139 regs
->link
= (unsigned long)kretprobe_trampoline
;
146 static inline int kprobe_handler(struct pt_regs
*regs
)
150 unsigned int *addr
= (unsigned int *)regs
->nip
;
151 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
153 /* Check we're not actually recursing */
154 if (kprobe_running()) {
155 p
= get_kprobe(addr
);
157 kprobe_opcode_t insn
= *p
->ainsn
.insn
;
158 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
160 regs
->msr
&= ~MSR_SE
;
161 regs
->msr
|= kcb
->kprobe_saved_msr
;
164 /* We have reentered the kprobe_handler(), since
165 * another probe was hit while within the handler.
166 * We here save the original kprobes variables and
167 * just single step on the instruction of the new probe
168 * without calling any user handlers.
170 save_previous_kprobe(kcb
);
171 set_current_kprobe(p
, regs
, kcb
);
172 kcb
->kprobe_saved_msr
= regs
->msr
;
174 prepare_singlestep(p
, regs
);
175 kcb
->kprobe_status
= KPROBE_REENTER
;
178 p
= __get_cpu_var(current_kprobe
);
179 if (p
->break_handler
&& p
->break_handler(p
, regs
)) {
186 p
= get_kprobe(addr
);
188 if (*addr
!= BREAKPOINT_INSTRUCTION
) {
190 * PowerPC has multiple variants of the "trap"
191 * instruction. If the current instruction is a
192 * trap variant, it could belong to someone else
194 kprobe_opcode_t cur_insn
= *addr
;
195 if (is_trap(cur_insn
))
198 * The breakpoint instruction was removed right
199 * after we hit it. Another cpu has removed
200 * either a probepoint or a debugger breakpoint
201 * at this address. In either case, no further
202 * handling of this interrupt is appropriate.
206 /* Not one of ours: let kernel handle it */
211 * This preempt_disable() matches the preempt_enable_no_resched()
212 * in post_kprobe_handler().
215 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
216 set_current_kprobe(p
, regs
, kcb
);
217 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
218 /* handler has already set things up, so skip ss setup */
222 prepare_singlestep(p
, regs
);
223 kcb
->kprobe_status
= KPROBE_HIT_SS
;
231 * Function return probe trampoline:
232 * - init_kprobes() establishes a probepoint here
233 * - When the probed function returns, this probe
234 * causes the handlers to fire
236 void kretprobe_trampoline_holder(void)
238 asm volatile(".global kretprobe_trampoline\n"
239 "kretprobe_trampoline:\n"
244 * Called when the probe at kretprobe trampoline is hit
246 int __kprobes
trampoline_probe_handler(struct kprobe
*p
, struct pt_regs
*regs
)
248 struct kretprobe_instance
*ri
= NULL
;
249 struct hlist_head
*head
;
250 struct hlist_node
*node
, *tmp
;
251 unsigned long flags
, orig_ret_address
= 0;
252 unsigned long trampoline_address
=(unsigned long)&kretprobe_trampoline
;
254 spin_lock_irqsave(&kretprobe_lock
, flags
);
255 head
= kretprobe_inst_table_head(current
);
258 * It is possible to have multiple instances associated with a given
259 * task either because an multiple functions in the call path
260 * have a return probe installed on them, and/or more then one return
261 * return probe was registered for a target function.
263 * We can handle this because:
264 * - instances are always inserted at the head of the list
265 * - when multiple return probes are registered for the same
266 * function, the first instance's ret_addr will point to the
267 * real return address, and all the rest will point to
268 * kretprobe_trampoline
270 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
271 if (ri
->task
!= current
)
272 /* another task is sharing our hash bucket */
275 if (ri
->rp
&& ri
->rp
->handler
)
276 ri
->rp
->handler(ri
, regs
);
278 orig_ret_address
= (unsigned long)ri
->ret_addr
;
281 if (orig_ret_address
!= trampoline_address
)
283 * This is the real return address. Any other
284 * instances associated with this task are for
285 * other calls deeper on the call stack
290 BUG_ON(!orig_ret_address
|| (orig_ret_address
== trampoline_address
));
291 regs
->nip
= orig_ret_address
;
293 reset_current_kprobe();
294 spin_unlock_irqrestore(&kretprobe_lock
, flags
);
295 preempt_enable_no_resched();
298 * By returning a non-zero value, we are telling
299 * kprobe_handler() that we have handled unlocking
300 * and re-enabling preemption.
306 * Called after single-stepping. p->addr is the address of the
307 * instruction whose first byte has been replaced by the "breakpoint"
308 * instruction. To avoid the SMP problems that can occur when we
309 * temporarily put back the original opcode to single-step, we
310 * single-stepped a copy of the instruction. The address of this
311 * copy is p->ainsn.insn.
313 static void __kprobes
resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
316 unsigned int insn
= *p
->ainsn
.insn
;
318 regs
->nip
= (unsigned long)p
->addr
;
319 ret
= emulate_step(regs
, insn
);
321 regs
->nip
= (unsigned long)p
->addr
+ 4;
324 static inline int post_kprobe_handler(struct pt_regs
*regs
)
326 struct kprobe
*cur
= kprobe_running();
327 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
332 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
333 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
334 cur
->post_handler(cur
, regs
, 0);
337 resume_execution(cur
, regs
);
338 regs
->msr
|= kcb
->kprobe_saved_msr
;
340 /*Restore back the original saved kprobes variables and continue. */
341 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
342 restore_previous_kprobe(kcb
);
345 reset_current_kprobe();
347 preempt_enable_no_resched();
350 * if somebody else is singlestepping across a probe point, msr
351 * will have SE set, in which case, continue the remaining processing
352 * of do_debug, as if this is not a probe hit.
354 if (regs
->msr
& MSR_SE
)
360 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
362 struct kprobe
*cur
= kprobe_running();
363 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
365 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
368 if (kcb
->kprobe_status
& KPROBE_HIT_SS
) {
369 resume_execution(cur
, regs
);
370 regs
->msr
&= ~MSR_SE
;
371 regs
->msr
|= kcb
->kprobe_saved_msr
;
373 reset_current_kprobe();
374 preempt_enable_no_resched();
380 * Wrapper routine to for handling exceptions.
382 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
383 unsigned long val
, void *data
)
385 struct die_args
*args
= (struct die_args
*)data
;
386 int ret
= NOTIFY_DONE
;
391 if (kprobe_handler(args
->regs
))
395 if (post_kprobe_handler(args
->regs
))
399 if (kprobe_running() &&
400 kprobe_fault_handler(args
->regs
, args
->trapnr
))
410 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
412 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
413 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
415 memcpy(&kcb
->jprobe_saved_regs
, regs
, sizeof(struct pt_regs
));
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
);
424 void __kprobes
jprobe_return(void)
426 asm volatile("trap" ::: "memory");
429 void __kprobes
jprobe_return_end(void)
433 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
435 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
438 * FIXME - we should ideally be validating that we got here 'cos
439 * of the "trap" in jprobe_return() above, before restoring the
442 memcpy(regs
, &kcb
->jprobe_saved_regs
, sizeof(struct pt_regs
));
446 static struct kprobe trampoline_p
= {
447 .addr
= (kprobe_opcode_t
*) &kretprobe_trampoline
,
448 .pre_handler
= trampoline_probe_handler
451 int __init
arch_init_kprobes(void)
453 return register_kprobe(&trampoline_p
);