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);
45 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
47 /* prepare insn slot */
48 p->ainsn.insn[0] = cpu_to_le32(p->opcode);
50 flush_icache_range((uintptr_t) (p->ainsn.insn),
51 (uintptr_t) (p->ainsn.insn) +
52 MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
55 * Needs restoring of return address after stepping xol.
57 p->ainsn.restore = (unsigned long) p->addr +
58 sizeof(kprobe_opcode_t);
61 int __kprobes arch_prepare_kprobe(struct kprobe *p)
63 unsigned long probe_addr = (unsigned long)p->addr;
64 extern char __start_rodata[];
65 extern char __end_rodata[];
70 /* copy instruction */
71 p->opcode = le32_to_cpu(*p->addr);
73 if (in_exception_text(probe_addr))
75 if (probe_addr >= (unsigned long) __start_rodata &&
76 probe_addr <= (unsigned long) __end_rodata)
79 /* decode instruction */
80 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
81 case INSN_REJECTED: /* insn not supported */
84 case INSN_GOOD: /* instruction uses slot */
85 p->ainsn.insn = get_insn_slot();
91 /* prepare the instruction */
92 arch_prepare_ss_slot(p);
97 static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode)
102 addrs[0] = (void *)addr;
103 insns[0] = (u32)opcode;
105 return aarch64_insn_patch_text(addrs, insns, 1);
108 /* arm kprobe: install breakpoint in text */
109 void __kprobes arch_arm_kprobe(struct kprobe *p)
111 patch_text(p->addr, BRK64_OPCODE_KPROBES);
114 /* disarm kprobe: remove breakpoint from text */
115 void __kprobes arch_disarm_kprobe(struct kprobe *p)
117 patch_text(p->addr, p->opcode);
120 void __kprobes arch_remove_kprobe(struct kprobe *p)
123 free_insn_slot(p->ainsn.insn, 0);
124 p->ainsn.insn = NULL;
128 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
130 kcb->prev_kprobe.kp = kprobe_running();
131 kcb->prev_kprobe.status = kcb->kprobe_status;
134 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
136 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
137 kcb->kprobe_status = kcb->prev_kprobe.status;
140 static void __kprobes set_current_kprobe(struct kprobe *p)
142 __this_cpu_write(current_kprobe, p);
146 * The D-flag (Debug mask) is set (masked) upon debug exception entry.
147 * Kprobes needs to clear (unmask) D-flag -ONLY- in case of recursive
148 * probe i.e. when probe hit from kprobe handler context upon
149 * executing the pre/post handlers. In this case we return with
150 * D-flag clear so that single-stepping can be carried-out.
152 * Leave D-flag set in all other cases.
154 static void __kprobes
155 spsr_set_debug_flag(struct pt_regs *regs, int mask)
157 unsigned long spsr = regs->pstate;
168 * Interrupts need to be disabled before single-step mode is set, and not
169 * reenabled until after single-step mode ends.
170 * Without disabling interrupt on local CPU, there is a chance of
171 * interrupt occurrence in the period of exception return and start of
172 * out-of-line single-step, that result in wrongly single stepping
173 * into the interrupt handler.
175 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
176 struct pt_regs *regs)
178 kcb->saved_irqflag = regs->pstate;
179 regs->pstate |= PSR_I_BIT;
182 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
183 struct pt_regs *regs)
185 if (kcb->saved_irqflag & PSR_I_BIT)
186 regs->pstate |= PSR_I_BIT;
188 regs->pstate &= ~PSR_I_BIT;
191 static void __kprobes
192 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
194 kcb->ss_ctx.ss_pending = true;
195 kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
198 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
200 kcb->ss_ctx.ss_pending = false;
201 kcb->ss_ctx.match_addr = 0;
204 static void __kprobes setup_singlestep(struct kprobe *p,
205 struct pt_regs *regs,
206 struct kprobe_ctlblk *kcb, int reenter)
211 save_previous_kprobe(kcb);
212 set_current_kprobe(p);
213 kcb->kprobe_status = KPROBE_REENTER;
215 kcb->kprobe_status = KPROBE_HIT_SS;
218 BUG_ON(!p->ainsn.insn);
220 /* prepare for single stepping */
221 slot = (unsigned long)p->ainsn.insn;
223 set_ss_context(kcb, slot); /* mark pending ss */
225 if (kcb->kprobe_status == KPROBE_REENTER)
226 spsr_set_debug_flag(regs, 0);
228 /* IRQs and single stepping do not mix well. */
229 kprobes_save_local_irqflag(kcb, regs);
230 kernel_enable_single_step(regs);
231 instruction_pointer_set(regs, slot);
234 static int __kprobes reenter_kprobe(struct kprobe *p,
235 struct pt_regs *regs,
236 struct kprobe_ctlblk *kcb)
238 switch (kcb->kprobe_status) {
239 case KPROBE_HIT_SSDONE:
240 case KPROBE_HIT_ACTIVE:
241 kprobes_inc_nmissed_count(p);
242 setup_singlestep(p, regs, kcb, 1);
246 pr_warn("Unrecoverable kprobe detected at %p.\n", p->addr);
258 static void __kprobes
259 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
261 struct kprobe *cur = kprobe_running();
266 /* return addr restore if non-branching insn */
267 if (cur->ainsn.restore != 0)
268 instruction_pointer_set(regs, cur->ainsn.restore);
270 /* restore back original saved kprobe variables and continue */
271 if (kcb->kprobe_status == KPROBE_REENTER) {
272 restore_previous_kprobe(kcb);
275 /* call post handler */
276 kcb->kprobe_status = KPROBE_HIT_SSDONE;
277 if (cur->post_handler) {
278 /* post_handler can hit breakpoint and single step
279 * again, so we enable D-flag for recursive exception.
281 cur->post_handler(cur, regs, 0);
284 reset_current_kprobe();
287 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
289 struct kprobe *cur = kprobe_running();
290 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
292 switch (kcb->kprobe_status) {
296 * We are here because the instruction being single
297 * stepped caused a page fault. We reset the current
298 * kprobe and the ip points back to the probe address
299 * and allow the page fault handler to continue as a
302 instruction_pointer_set(regs, (unsigned long) cur->addr);
303 if (!instruction_pointer(regs))
306 kernel_disable_single_step();
307 if (kcb->kprobe_status == KPROBE_REENTER)
308 spsr_set_debug_flag(regs, 1);
310 if (kcb->kprobe_status == KPROBE_REENTER)
311 restore_previous_kprobe(kcb);
313 reset_current_kprobe();
316 case KPROBE_HIT_ACTIVE:
317 case KPROBE_HIT_SSDONE:
319 * We increment the nmissed count for accounting,
320 * we can also use npre/npostfault count for accounting
321 * these specific fault cases.
323 kprobes_inc_nmissed_count(cur);
326 * We come here because instructions in the pre/post
327 * handler caused the page_fault, this could happen
328 * if handler tries to access user space by
329 * copy_from_user(), get_user() etc. Let the
330 * user-specified handler try to fix it first.
332 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
336 * In case the user-specified fault handler returned
337 * zero, try to fix up.
339 if (fixup_exception(regs))
345 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
346 unsigned long val, void *data)
351 static void __kprobes kprobe_handler(struct pt_regs *regs)
353 struct kprobe *p, *cur_kprobe;
354 struct kprobe_ctlblk *kcb;
355 unsigned long addr = instruction_pointer(regs);
357 kcb = get_kprobe_ctlblk();
358 cur_kprobe = kprobe_running();
360 p = get_kprobe((kprobe_opcode_t *) addr);
364 if (reenter_kprobe(p, regs, kcb))
368 set_current_kprobe(p);
369 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
372 * If we have no pre-handler or it returned 0, we
373 * continue with normal processing. If we have a
374 * pre-handler and it returned non-zero, it prepped
375 * for calling the break_handler below on re-entry,
376 * so get out doing nothing more here.
378 * pre_handler can hit a breakpoint and can step thru
379 * before return, keep PSTATE D-flag enabled until
380 * pre_handler return back.
382 if (!p->pre_handler || !p->pre_handler(p, regs)) {
383 setup_singlestep(p, regs, kcb, 0);
387 } else if ((le32_to_cpu(*(kprobe_opcode_t *) addr) ==
388 BRK64_OPCODE_KPROBES) && cur_kprobe) {
389 /* We probably hit a jprobe. Call its break handler. */
390 if (cur_kprobe->break_handler &&
391 cur_kprobe->break_handler(cur_kprobe, regs)) {
392 setup_singlestep(cur_kprobe, regs, kcb, 0);
397 * The breakpoint instruction was removed right
398 * after we hit it. Another cpu has removed
399 * either a probepoint or a debugger breakpoint
400 * at this address. In either case, no further
401 * handling of this interrupt is appropriate.
402 * Return back to original instruction, and continue.
407 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
409 if ((kcb->ss_ctx.ss_pending)
410 && (kcb->ss_ctx.match_addr == addr)) {
411 clear_ss_context(kcb); /* clear pending ss */
412 return DBG_HOOK_HANDLED;
414 /* not ours, kprobes should ignore it */
415 return DBG_HOOK_ERROR;
419 kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr)
421 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
424 /* return error if this is not our step */
425 retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
427 if (retval == DBG_HOOK_HANDLED) {
428 kprobes_restore_local_irqflag(kcb, regs);
429 kernel_disable_single_step();
431 if (kcb->kprobe_status == KPROBE_REENTER)
432 spsr_set_debug_flag(regs, 1);
434 post_kprobe_handler(kcb, regs);
441 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
443 kprobe_handler(regs);
444 return DBG_HOOK_HANDLED;
447 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
449 struct jprobe *jp = container_of(p, struct jprobe, kp);
450 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
451 long stack_ptr = kernel_stack_pointer(regs);
453 kcb->jprobe_saved_regs = *regs;
455 * As Linus pointed out, gcc assumes that the callee
456 * owns the argument space and could overwrite it, e.g.
457 * tailcall optimization. So, to be absolutely safe
458 * we also save and restore enough stack bytes to cover
461 memcpy(kcb->jprobes_stack, (void *)stack_ptr,
462 MIN_STACK_SIZE(stack_ptr));
464 instruction_pointer_set(regs, (unsigned long) jp->entry);
466 pause_graph_tracing();
470 void __kprobes jprobe_return(void)
472 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
475 * Jprobe handler return by entering break exception,
476 * encoded same as kprobe, but with following conditions
477 * -a magic number in x0 to identify from rest of other kprobes.
478 * -restore stack addr to original saved pt_regs
480 asm volatile ("ldr x0, [%0]\n\t"
482 ".globl jprobe_return_break\n\t"
483 "jprobe_return_break:\n\t"
486 : "r"(&kcb->jprobe_saved_regs.sp),
487 "I"(BRK64_ESR_KPROBES)
491 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
493 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
494 long stack_addr = kcb->jprobe_saved_regs.sp;
495 long orig_sp = kernel_stack_pointer(regs);
496 struct jprobe *jp = container_of(p, struct jprobe, kp);
498 if (instruction_pointer(regs) != (u64) jprobe_return_break)
501 if (orig_sp != stack_addr) {
502 struct pt_regs *saved_regs =
503 (struct pt_regs *)kcb->jprobe_saved_regs.sp;
504 pr_err("current sp %lx does not match saved sp %lx\n",
505 orig_sp, stack_addr);
506 pr_err("Saved registers for jprobe %p\n", jp);
507 show_regs(saved_regs);
508 pr_err("Current registers\n");
512 unpause_graph_tracing();
513 *regs = kcb->jprobe_saved_regs;
514 memcpy((void *)stack_addr, kcb->jprobes_stack,
515 MIN_STACK_SIZE(stack_addr));
516 preempt_enable_no_resched();
520 bool arch_within_kprobe_blacklist(unsigned long addr)
522 extern char __idmap_text_start[], __idmap_text_end[];
524 if ((addr >= (unsigned long)__kprobes_text_start &&
525 addr < (unsigned long)__kprobes_text_end) ||
526 (addr >= (unsigned long)__entry_text_start &&
527 addr < (unsigned long)__entry_text_end) ||
528 (addr >= (unsigned long)__idmap_text_start &&
529 addr < (unsigned long)__idmap_text_end) ||
530 !!search_exception_tables(addr))
537 int __init arch_init_kprobes(void)