2 * arch/arm64/kernel/probes/kprobes.c
4 * Kprobes support for ARM64
6 * Copyright (C) 2013 Linaro Limited.
7 * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/kprobes.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/stop_machine.h>
24 #include <linux/stringify.h>
25 #include <asm/traps.h>
26 #include <asm/ptrace.h>
27 #include <asm/cacheflush.h>
28 #include <asm/debug-monitors.h>
29 #include <asm/system_misc.h>
31 #include <asm/uaccess.h>
33 #include <asm-generic/sections.h>
35 #include "decode-insn.h"
37 #define MIN_STACK_SIZE(addr) min((unsigned long)MAX_STACK_SIZE, \
38 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
40 void jprobe_return_break(void);
42 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
43 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
46 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
48 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
50 /* prepare insn slot */
51 p->ainsn.insn[0] = cpu_to_le32(p->opcode);
53 flush_icache_range((uintptr_t) (p->ainsn.insn),
54 (uintptr_t) (p->ainsn.insn) +
55 MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
58 * Needs restoring of return address after stepping xol.
60 p->ainsn.restore = (unsigned long) p->addr +
61 sizeof(kprobe_opcode_t);
64 static void __kprobes arch_prepare_simulate(struct kprobe *p)
66 /* This instructions is not executed xol. No need to adjust the PC */
70 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
72 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
75 p->ainsn.handler((u32)p->opcode, (long)p->addr, regs);
77 /* single step simulated, now go for post processing */
78 post_kprobe_handler(kcb, regs);
81 int __kprobes arch_prepare_kprobe(struct kprobe *p)
83 unsigned long probe_addr = (unsigned long)p->addr;
84 extern char __start_rodata[];
85 extern char __end_rodata[];
90 /* copy instruction */
91 p->opcode = le32_to_cpu(*p->addr);
93 if (in_exception_text(probe_addr))
95 if (probe_addr >= (unsigned long) __start_rodata &&
96 probe_addr <= (unsigned long) __end_rodata)
99 /* decode instruction */
100 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
101 case INSN_REJECTED: /* insn not supported */
104 case INSN_GOOD_NO_SLOT: /* insn need simulation */
105 p->ainsn.insn = NULL;
108 case INSN_GOOD: /* instruction uses slot */
109 p->ainsn.insn = get_insn_slot();
115 /* prepare the instruction */
117 arch_prepare_ss_slot(p);
119 arch_prepare_simulate(p);
124 static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode)
129 addrs[0] = (void *)addr;
130 insns[0] = (u32)opcode;
132 return aarch64_insn_patch_text(addrs, insns, 1);
135 /* arm kprobe: install breakpoint in text */
136 void __kprobes arch_arm_kprobe(struct kprobe *p)
138 patch_text(p->addr, BRK64_OPCODE_KPROBES);
141 /* disarm kprobe: remove breakpoint from text */
142 void __kprobes arch_disarm_kprobe(struct kprobe *p)
144 patch_text(p->addr, p->opcode);
147 void __kprobes arch_remove_kprobe(struct kprobe *p)
150 free_insn_slot(p->ainsn.insn, 0);
151 p->ainsn.insn = NULL;
155 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
157 kcb->prev_kprobe.kp = kprobe_running();
158 kcb->prev_kprobe.status = kcb->kprobe_status;
161 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
163 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
164 kcb->kprobe_status = kcb->prev_kprobe.status;
167 static void __kprobes set_current_kprobe(struct kprobe *p)
169 __this_cpu_write(current_kprobe, p);
173 * The D-flag (Debug mask) is set (masked) upon debug exception entry.
174 * Kprobes needs to clear (unmask) D-flag -ONLY- in case of recursive
175 * probe i.e. when probe hit from kprobe handler context upon
176 * executing the pre/post handlers. In this case we return with
177 * D-flag clear so that single-stepping can be carried-out.
179 * Leave D-flag set in all other cases.
181 static void __kprobes
182 spsr_set_debug_flag(struct pt_regs *regs, int mask)
184 unsigned long spsr = regs->pstate;
195 * Interrupts need to be disabled before single-step mode is set, and not
196 * reenabled until after single-step mode ends.
197 * Without disabling interrupt on local CPU, there is a chance of
198 * interrupt occurrence in the period of exception return and start of
199 * out-of-line single-step, that result in wrongly single stepping
200 * into the interrupt handler.
202 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
203 struct pt_regs *regs)
205 kcb->saved_irqflag = regs->pstate;
206 regs->pstate |= PSR_I_BIT;
209 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
210 struct pt_regs *regs)
212 if (kcb->saved_irqflag & PSR_I_BIT)
213 regs->pstate |= PSR_I_BIT;
215 regs->pstate &= ~PSR_I_BIT;
218 static void __kprobes
219 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
221 kcb->ss_ctx.ss_pending = true;
222 kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
225 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
227 kcb->ss_ctx.ss_pending = false;
228 kcb->ss_ctx.match_addr = 0;
231 static void __kprobes setup_singlestep(struct kprobe *p,
232 struct pt_regs *regs,
233 struct kprobe_ctlblk *kcb, int reenter)
238 save_previous_kprobe(kcb);
239 set_current_kprobe(p);
240 kcb->kprobe_status = KPROBE_REENTER;
242 kcb->kprobe_status = KPROBE_HIT_SS;
247 /* prepare for single stepping */
248 slot = (unsigned long)p->ainsn.insn;
250 set_ss_context(kcb, slot); /* mark pending ss */
252 if (kcb->kprobe_status == KPROBE_REENTER)
253 spsr_set_debug_flag(regs, 0);
255 /* IRQs and single stepping do not mix well. */
256 kprobes_save_local_irqflag(kcb, regs);
257 kernel_enable_single_step(regs);
258 instruction_pointer_set(regs, slot);
260 /* insn simulation */
261 arch_simulate_insn(p, regs);
265 static int __kprobes reenter_kprobe(struct kprobe *p,
266 struct pt_regs *regs,
267 struct kprobe_ctlblk *kcb)
269 switch (kcb->kprobe_status) {
270 case KPROBE_HIT_SSDONE:
271 case KPROBE_HIT_ACTIVE:
272 kprobes_inc_nmissed_count(p);
273 setup_singlestep(p, regs, kcb, 1);
277 pr_warn("Unrecoverable kprobe detected at %p.\n", p->addr);
289 static void __kprobes
290 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
292 struct kprobe *cur = kprobe_running();
297 /* return addr restore if non-branching insn */
298 if (cur->ainsn.restore != 0)
299 instruction_pointer_set(regs, cur->ainsn.restore);
301 /* restore back original saved kprobe variables and continue */
302 if (kcb->kprobe_status == KPROBE_REENTER) {
303 restore_previous_kprobe(kcb);
306 /* call post handler */
307 kcb->kprobe_status = KPROBE_HIT_SSDONE;
308 if (cur->post_handler) {
309 /* post_handler can hit breakpoint and single step
310 * again, so we enable D-flag for recursive exception.
312 cur->post_handler(cur, regs, 0);
315 reset_current_kprobe();
318 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
320 struct kprobe *cur = kprobe_running();
321 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
323 switch (kcb->kprobe_status) {
327 * We are here because the instruction being single
328 * stepped caused a page fault. We reset the current
329 * kprobe and the ip points back to the probe address
330 * and allow the page fault handler to continue as a
333 instruction_pointer_set(regs, (unsigned long) cur->addr);
334 if (!instruction_pointer(regs))
337 kernel_disable_single_step();
338 if (kcb->kprobe_status == KPROBE_REENTER)
339 spsr_set_debug_flag(regs, 1);
341 if (kcb->kprobe_status == KPROBE_REENTER)
342 restore_previous_kprobe(kcb);
344 reset_current_kprobe();
347 case KPROBE_HIT_ACTIVE:
348 case KPROBE_HIT_SSDONE:
350 * We increment the nmissed count for accounting,
351 * we can also use npre/npostfault count for accounting
352 * these specific fault cases.
354 kprobes_inc_nmissed_count(cur);
357 * We come here because instructions in the pre/post
358 * handler caused the page_fault, this could happen
359 * if handler tries to access user space by
360 * copy_from_user(), get_user() etc. Let the
361 * user-specified handler try to fix it first.
363 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
367 * In case the user-specified fault handler returned
368 * zero, try to fix up.
370 if (fixup_exception(regs))
376 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
377 unsigned long val, void *data)
382 static void __kprobes kprobe_handler(struct pt_regs *regs)
384 struct kprobe *p, *cur_kprobe;
385 struct kprobe_ctlblk *kcb;
386 unsigned long addr = instruction_pointer(regs);
388 kcb = get_kprobe_ctlblk();
389 cur_kprobe = kprobe_running();
391 p = get_kprobe((kprobe_opcode_t *) addr);
395 if (reenter_kprobe(p, regs, kcb))
399 set_current_kprobe(p);
400 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
403 * If we have no pre-handler or it returned 0, we
404 * continue with normal processing. If we have a
405 * pre-handler and it returned non-zero, it prepped
406 * for calling the break_handler below on re-entry,
407 * so get out doing nothing more here.
409 * pre_handler can hit a breakpoint and can step thru
410 * before return, keep PSTATE D-flag enabled until
411 * pre_handler return back.
413 if (!p->pre_handler || !p->pre_handler(p, regs)) {
414 setup_singlestep(p, regs, kcb, 0);
418 } else if ((le32_to_cpu(*(kprobe_opcode_t *) addr) ==
419 BRK64_OPCODE_KPROBES) && cur_kprobe) {
420 /* We probably hit a jprobe. Call its break handler. */
421 if (cur_kprobe->break_handler &&
422 cur_kprobe->break_handler(cur_kprobe, regs)) {
423 setup_singlestep(cur_kprobe, regs, kcb, 0);
428 * The breakpoint instruction was removed right
429 * after we hit it. Another cpu has removed
430 * either a probepoint or a debugger breakpoint
431 * at this address. In either case, no further
432 * handling of this interrupt is appropriate.
433 * Return back to original instruction, and continue.
438 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
440 if ((kcb->ss_ctx.ss_pending)
441 && (kcb->ss_ctx.match_addr == addr)) {
442 clear_ss_context(kcb); /* clear pending ss */
443 return DBG_HOOK_HANDLED;
445 /* not ours, kprobes should ignore it */
446 return DBG_HOOK_ERROR;
450 kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr)
452 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
455 /* return error if this is not our step */
456 retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
458 if (retval == DBG_HOOK_HANDLED) {
459 kprobes_restore_local_irqflag(kcb, regs);
460 kernel_disable_single_step();
462 if (kcb->kprobe_status == KPROBE_REENTER)
463 spsr_set_debug_flag(regs, 1);
465 post_kprobe_handler(kcb, regs);
472 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
474 kprobe_handler(regs);
475 return DBG_HOOK_HANDLED;
478 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
480 struct jprobe *jp = container_of(p, struct jprobe, kp);
481 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
482 long stack_ptr = kernel_stack_pointer(regs);
484 kcb->jprobe_saved_regs = *regs;
486 * As Linus pointed out, gcc assumes that the callee
487 * owns the argument space and could overwrite it, e.g.
488 * tailcall optimization. So, to be absolutely safe
489 * we also save and restore enough stack bytes to cover
492 memcpy(kcb->jprobes_stack, (void *)stack_ptr,
493 MIN_STACK_SIZE(stack_ptr));
495 instruction_pointer_set(regs, (unsigned long) jp->entry);
497 pause_graph_tracing();
501 void __kprobes jprobe_return(void)
503 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
506 * Jprobe handler return by entering break exception,
507 * encoded same as kprobe, but with following conditions
508 * -a magic number in x0 to identify from rest of other kprobes.
509 * -restore stack addr to original saved pt_regs
511 asm volatile ("ldr x0, [%0]\n\t"
513 ".globl jprobe_return_break\n\t"
514 "jprobe_return_break:\n\t"
517 : "r"(&kcb->jprobe_saved_regs.sp),
518 "I"(BRK64_ESR_KPROBES)
522 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
524 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
525 long stack_addr = kcb->jprobe_saved_regs.sp;
526 long orig_sp = kernel_stack_pointer(regs);
527 struct jprobe *jp = container_of(p, struct jprobe, kp);
529 if (instruction_pointer(regs) != (u64) jprobe_return_break)
532 if (orig_sp != stack_addr) {
533 struct pt_regs *saved_regs =
534 (struct pt_regs *)kcb->jprobe_saved_regs.sp;
535 pr_err("current sp %lx does not match saved sp %lx\n",
536 orig_sp, stack_addr);
537 pr_err("Saved registers for jprobe %p\n", jp);
538 show_regs(saved_regs);
539 pr_err("Current registers\n");
543 unpause_graph_tracing();
544 *regs = kcb->jprobe_saved_regs;
545 memcpy((void *)stack_addr, kcb->jprobes_stack,
546 MIN_STACK_SIZE(stack_addr));
547 preempt_enable_no_resched();
551 bool arch_within_kprobe_blacklist(unsigned long addr)
553 extern char __idmap_text_start[], __idmap_text_end[];
555 if ((addr >= (unsigned long)__kprobes_text_start &&
556 addr < (unsigned long)__kprobes_text_end) ||
557 (addr >= (unsigned long)__entry_text_start &&
558 addr < (unsigned long)__entry_text_end) ||
559 (addr >= (unsigned long)__idmap_text_start &&
560 addr < (unsigned long)__idmap_text_end) ||
561 !!search_exception_tables(addr))
568 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
573 int __init arch_init_kprobes(void)