]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/powerpc/kernel/kprobes.c
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 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
) = NULL
;
39 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
41 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
44 kprobe_opcode_t insn
= *p
->addr
;
46 if ((unsigned long)p
->addr
& 0x03) {
47 printk("Attempt to register kprobe at an unaligned address\n");
49 } else if (IS_MTMSRD(insn
) || IS_RFID(insn
)) {
50 printk("Cannot register a kprobe on rfid or mtmsrd\n");
54 /* insn must be on a special executable page on ppc64 */
56 p
->ainsn
.insn
= get_insn_slot();
62 memcpy(p
->ainsn
.insn
, p
->addr
, MAX_INSN_SIZE
* sizeof(kprobe_opcode_t
));
69 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
71 *p
->addr
= BREAKPOINT_INSTRUCTION
;
72 flush_icache_range((unsigned long) p
->addr
,
73 (unsigned long) p
->addr
+ sizeof(kprobe_opcode_t
));
76 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
79 flush_icache_range((unsigned long) p
->addr
,
80 (unsigned long) p
->addr
+ sizeof(kprobe_opcode_t
));
83 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
85 free_insn_slot(p
->ainsn
.insn
);
88 static inline void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
90 kprobe_opcode_t insn
= *p
->ainsn
.insn
;
94 /* single step inline if it is a trap variant */
96 regs
->nip
= (unsigned long)p
->addr
;
98 regs
->nip
= (unsigned long)p
->ainsn
.insn
;
101 static inline void save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
103 kcb
->prev_kprobe
.kp
= kprobe_running();
104 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
105 kcb
->prev_kprobe
.saved_msr
= kcb
->kprobe_saved_msr
;
108 static inline void restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
110 __get_cpu_var(current_kprobe
) = kcb
->prev_kprobe
.kp
;
111 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
112 kcb
->kprobe_saved_msr
= kcb
->prev_kprobe
.saved_msr
;
115 static inline void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
116 struct kprobe_ctlblk
*kcb
)
118 __get_cpu_var(current_kprobe
) = p
;
119 kcb
->kprobe_saved_msr
= regs
->msr
;
122 /* Called with kretprobe_lock held */
123 void __kprobes
arch_prepare_kretprobe(struct kretprobe
*rp
,
124 struct pt_regs
*regs
)
126 struct kretprobe_instance
*ri
;
128 if ((ri
= get_free_rp_inst(rp
)) != NULL
) {
131 ri
->ret_addr
= (kprobe_opcode_t
*)regs
->link
;
133 /* Replace the return addr with trampoline addr */
134 regs
->link
= (unsigned long)kretprobe_trampoline
;
141 static inline int kprobe_handler(struct pt_regs
*regs
)
145 unsigned int *addr
= (unsigned int *)regs
->nip
;
146 struct kprobe_ctlblk
*kcb
;
149 * We don't want to be preempted for the entire
150 * duration of kprobe processing
153 kcb
= get_kprobe_ctlblk();
155 /* Check we're not actually recursing */
156 if (kprobe_running()) {
157 p
= get_kprobe(addr
);
159 kprobe_opcode_t insn
= *p
->ainsn
.insn
;
160 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
162 regs
->msr
&= ~MSR_SE
;
163 regs
->msr
|= kcb
->kprobe_saved_msr
;
166 /* We have reentered the kprobe_handler(), since
167 * another probe was hit while within the handler.
168 * We here save the original kprobes variables and
169 * just single step on the instruction of the new probe
170 * without calling any user handlers.
172 save_previous_kprobe(kcb
);
173 set_current_kprobe(p
, regs
, kcb
);
174 kcb
->kprobe_saved_msr
= regs
->msr
;
175 kprobes_inc_nmissed_count(p
);
176 prepare_singlestep(p
, regs
);
177 kcb
->kprobe_status
= KPROBE_REENTER
;
180 p
= __get_cpu_var(current_kprobe
);
181 if (p
->break_handler
&& p
->break_handler(p
, regs
)) {
188 p
= get_kprobe(addr
);
190 if (*addr
!= BREAKPOINT_INSTRUCTION
) {
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
196 kprobe_opcode_t cur_insn
= *addr
;
197 if (is_trap(cur_insn
))
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.
208 /* Not one of ours: let kernel handle it */
212 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
213 set_current_kprobe(p
, regs
, kcb
);
214 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
215 /* handler has already set things up, so skip ss setup */
219 prepare_singlestep(p
, regs
);
220 kcb
->kprobe_status
= KPROBE_HIT_SS
;
224 preempt_enable_no_resched();
229 * Function return probe trampoline:
230 * - init_kprobes() establishes a probepoint here
231 * - When the probed function returns, this probe
232 * causes the handlers to fire
234 void kretprobe_trampoline_holder(void)
236 asm volatile(".global kretprobe_trampoline\n"
237 "kretprobe_trampoline:\n"
242 * Called when the probe at kretprobe trampoline is hit
244 int __kprobes
trampoline_probe_handler(struct kprobe
*p
, struct pt_regs
*regs
)
246 struct kretprobe_instance
*ri
= NULL
;
247 struct hlist_head
*head
;
248 struct hlist_node
*node
, *tmp
;
249 unsigned long flags
, orig_ret_address
= 0;
250 unsigned long trampoline_address
=(unsigned long)&kretprobe_trampoline
;
252 spin_lock_irqsave(&kretprobe_lock
, flags
);
253 head
= kretprobe_inst_table_head(current
);
256 * It is possible to have multiple instances associated with a given
257 * task either because an multiple functions in the call path
258 * have a return probe installed on them, and/or more then one return
259 * return probe was registered for a target function.
261 * We can handle this because:
262 * - instances are always inserted at the head of the list
263 * - when multiple return probes are registered for the same
264 * function, the first instance's ret_addr will point to the
265 * real return address, and all the rest will point to
266 * kretprobe_trampoline
268 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
269 if (ri
->task
!= current
)
270 /* another task is sharing our hash bucket */
273 if (ri
->rp
&& ri
->rp
->handler
)
274 ri
->rp
->handler(ri
, regs
);
276 orig_ret_address
= (unsigned long)ri
->ret_addr
;
279 if (orig_ret_address
!= trampoline_address
)
281 * This is the real return address. Any other
282 * instances associated with this task are for
283 * other calls deeper on the call stack
288 BUG_ON(!orig_ret_address
|| (orig_ret_address
== trampoline_address
));
289 regs
->nip
= orig_ret_address
;
291 reset_current_kprobe();
292 spin_unlock_irqrestore(&kretprobe_lock
, flags
);
293 preempt_enable_no_resched();
296 * By returning a non-zero value, we are telling
297 * kprobe_handler() that we don't want the post_handler
298 * to run (and have re-enabled preemption)
304 * Called after single-stepping. p->addr is the address of the
305 * instruction whose first byte has been replaced by the "breakpoint"
306 * instruction. To avoid the SMP problems that can occur when we
307 * temporarily put back the original opcode to single-step, we
308 * single-stepped a copy of the instruction. The address of this
309 * copy is p->ainsn.insn.
311 static void __kprobes
resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
314 unsigned int insn
= *p
->ainsn
.insn
;
316 regs
->nip
= (unsigned long)p
->addr
;
317 ret
= emulate_step(regs
, insn
);
319 regs
->nip
= (unsigned long)p
->addr
+ 4;
322 static inline int post_kprobe_handler(struct pt_regs
*regs
)
324 struct kprobe
*cur
= kprobe_running();
325 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
330 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
331 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
332 cur
->post_handler(cur
, regs
, 0);
335 resume_execution(cur
, regs
);
336 regs
->msr
|= kcb
->kprobe_saved_msr
;
338 /*Restore back the original saved kprobes variables and continue. */
339 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
340 restore_previous_kprobe(kcb
);
343 reset_current_kprobe();
345 preempt_enable_no_resched();
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.
352 if (regs
->msr
& MSR_SE
)
358 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
360 struct kprobe
*cur
= kprobe_running();
361 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
363 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
366 if (kcb
->kprobe_status
& KPROBE_HIT_SS
) {
367 resume_execution(cur
, regs
);
368 regs
->msr
&= ~MSR_SE
;
369 regs
->msr
|= kcb
->kprobe_saved_msr
;
371 reset_current_kprobe();
372 preempt_enable_no_resched();
378 * Wrapper routine to for handling exceptions.
380 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
381 unsigned long val
, void *data
)
383 struct die_args
*args
= (struct die_args
*)data
;
384 int ret
= NOTIFY_DONE
;
388 if (kprobe_handler(args
->regs
))
392 if (post_kprobe_handler(args
->regs
))
396 /* kprobe_running() needs smp_processor_id() */
398 if (kprobe_running() &&
399 kprobe_fault_handler(args
->regs
, args
->trapnr
))
409 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
411 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
412 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
414 memcpy(&kcb
->jprobe_saved_regs
, regs
, sizeof(struct pt_regs
));
416 /* setup return addr to the jprobe handler routine */
417 regs
->nip
= (unsigned long)(((func_descr_t
*)jp
->entry
)->entry
);
418 regs
->gpr
[2] = (unsigned long)(((func_descr_t
*)jp
->entry
)->toc
);
423 void __kprobes
jprobe_return(void)
425 asm volatile("trap" ::: "memory");
428 void __kprobes
jprobe_return_end(void)
432 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
434 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
437 * FIXME - we should ideally be validating that we got here 'cos
438 * of the "trap" in jprobe_return() above, before restoring the
441 memcpy(regs
, &kcb
->jprobe_saved_regs
, sizeof(struct pt_regs
));
442 preempt_enable_no_resched();
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
);