2 * arch/arm/kernel/kprobes.c
6 * Abhishek Sagar <sagar.abhishek@gmail.com>
7 * Copyright (C) 2006, 2007 Motorola Inc.
9 * Nicolas Pitre <nico@marvell.com>
10 * Copyright (C) 2007 Marvell Ltd.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/kernel.h>
23 #include <linux/kprobes.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/stop_machine.h>
27 #include <linux/sched/debug.h>
28 #include <linux/stringify.h>
29 #include <asm/traps.h>
30 #include <asm/opcodes.h>
31 #include <asm/cacheflush.h>
32 #include <linux/percpu.h>
33 #include <linux/bug.h>
34 #include <asm/patch.h>
35 #include <asm/sections.h>
37 #include "../decode-arm.h"
38 #include "../decode-thumb.h"
41 #define MIN_STACK_SIZE(addr) \
42 min((unsigned long)MAX_STACK_SIZE, \
43 (unsigned long)current_thread_info() + THREAD_START_SP - (addr))
45 #define flush_insns(addr, size) \
46 flush_icache_range((unsigned long)(addr), \
47 (unsigned long)(addr) + \
50 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
51 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
54 int __kprobes arch_prepare_kprobe(struct kprobe *p)
57 kprobe_opcode_t tmp_insn[MAX_INSN_SIZE];
58 unsigned long addr = (unsigned long)p->addr;
60 kprobe_decode_insn_t *decode_insn;
61 const union decode_action *actions;
63 const struct decode_checker **checkers;
65 #ifdef CONFIG_THUMB2_KERNEL
67 addr &= ~1; /* Bit 0 would normally be set to indicate Thumb code */
68 insn = __mem_to_opcode_thumb16(((u16 *)addr)[0]);
69 if (is_wide_instruction(insn)) {
70 u16 inst2 = __mem_to_opcode_thumb16(((u16 *)addr)[1]);
71 insn = __opcode_thumb32_compose(insn, inst2);
72 decode_insn = thumb32_probes_decode_insn;
73 actions = kprobes_t32_actions;
74 checkers = kprobes_t32_checkers;
76 decode_insn = thumb16_probes_decode_insn;
77 actions = kprobes_t16_actions;
78 checkers = kprobes_t16_checkers;
80 #else /* !CONFIG_THUMB2_KERNEL */
84 insn = __mem_to_opcode_arm(*p->addr);
85 decode_insn = arm_probes_decode_insn;
86 actions = kprobes_arm_actions;
87 checkers = kprobes_arm_checkers;
91 p->ainsn.insn = tmp_insn;
93 switch ((*decode_insn)(insn, &p->ainsn, true, actions, checkers)) {
94 case INSN_REJECTED: /* not supported */
97 case INSN_GOOD: /* instruction uses slot */
98 p->ainsn.insn = get_insn_slot();
101 for (is = 0; is < MAX_INSN_SIZE; ++is)
102 p->ainsn.insn[is] = tmp_insn[is];
103 flush_insns(p->ainsn.insn,
104 sizeof(p->ainsn.insn[0]) * MAX_INSN_SIZE);
105 p->ainsn.insn_fn = (probes_insn_fn_t *)
106 ((uintptr_t)p->ainsn.insn | thumb);
109 case INSN_GOOD_NO_SLOT: /* instruction doesn't need insn slot */
110 p->ainsn.insn = NULL;
115 * Never instrument insn like 'str r0, [sp, +/-r1]'. Also, insn likes
116 * 'str r0, [sp, #-68]' should also be prohibited.
119 if ((p->ainsn.stack_space < 0) ||
120 (p->ainsn.stack_space > MAX_STACK_SIZE))
126 void __kprobes arch_arm_kprobe(struct kprobe *p)
131 if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) {
132 /* Remove any Thumb flag */
133 addr = (void *)((uintptr_t)p->addr & ~1);
135 if (is_wide_instruction(p->opcode))
136 brkp = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION;
138 brkp = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION;
140 kprobe_opcode_t insn = p->opcode;
143 brkp = KPROBE_ARM_BREAKPOINT_INSTRUCTION;
145 if (insn >= 0xe0000000)
146 brkp |= 0xe0000000; /* Unconditional instruction */
148 brkp |= insn & 0xf0000000; /* Copy condition from insn */
151 patch_text(addr, brkp);
155 * The actual disarming is done here on each CPU and synchronized using
156 * stop_machine. This synchronization is necessary on SMP to avoid removing
157 * a probe between the moment the 'Undefined Instruction' exception is raised
158 * and the moment the exception handler reads the faulting instruction from
159 * memory. It is also needed to atomically set the two half-words of a 32-bit
167 static int __kprobes_remove_breakpoint(void *data)
169 struct patch *p = data;
170 __patch_text(p->addr, p->insn);
174 void __kprobes kprobes_remove_breakpoint(void *addr, unsigned int insn)
180 stop_machine_cpuslocked(__kprobes_remove_breakpoint, &p,
184 void __kprobes arch_disarm_kprobe(struct kprobe *p)
186 kprobes_remove_breakpoint((void *)((uintptr_t)p->addr & ~1),
190 void __kprobes arch_remove_kprobe(struct kprobe *p)
193 free_insn_slot(p->ainsn.insn, 0);
194 p->ainsn.insn = NULL;
198 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
200 kcb->prev_kprobe.kp = kprobe_running();
201 kcb->prev_kprobe.status = kcb->kprobe_status;
204 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
206 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
207 kcb->kprobe_status = kcb->prev_kprobe.status;
210 static void __kprobes set_current_kprobe(struct kprobe *p)
212 __this_cpu_write(current_kprobe, p);
215 static void __kprobes
216 singlestep_skip(struct kprobe *p, struct pt_regs *regs)
218 #ifdef CONFIG_THUMB2_KERNEL
219 regs->ARM_cpsr = it_advance(regs->ARM_cpsr);
220 if (is_wide_instruction(p->opcode))
229 static inline void __kprobes
230 singlestep(struct kprobe *p, struct pt_regs *regs, struct kprobe_ctlblk *kcb)
232 p->ainsn.insn_singlestep(p->opcode, &p->ainsn, regs);
236 * Called with IRQs disabled. IRQs must remain disabled from that point
237 * all the way until processing this kprobe is complete. The current
238 * kprobes implementation cannot process more than one nested level of
239 * kprobe, and that level is reserved for user kprobe handlers, so we can't
240 * risk encountering a new kprobe in an interrupt handler.
242 void __kprobes kprobe_handler(struct pt_regs *regs)
244 struct kprobe *p, *cur;
245 struct kprobe_ctlblk *kcb;
247 kcb = get_kprobe_ctlblk();
248 cur = kprobe_running();
250 #ifdef CONFIG_THUMB2_KERNEL
252 * First look for a probe which was registered using an address with
253 * bit 0 set, this is the usual situation for pointers to Thumb code.
254 * If not found, fallback to looking for one with bit 0 clear.
256 p = get_kprobe((kprobe_opcode_t *)(regs->ARM_pc | 1));
258 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
260 #else /* ! CONFIG_THUMB2_KERNEL */
261 p = get_kprobe((kprobe_opcode_t *)regs->ARM_pc);
265 if (!p->ainsn.insn_check_cc(regs->ARM_cpsr)) {
267 * Probe hit but conditional execution check failed,
268 * so just skip the instruction and continue as if
269 * nothing had happened.
270 * In this case, we can skip recursing check too.
272 singlestep_skip(p, regs);
274 /* Kprobe is pending, so we're recursing. */
275 switch (kcb->kprobe_status) {
276 case KPROBE_HIT_ACTIVE:
277 case KPROBE_HIT_SSDONE:
279 /* A pre- or post-handler probe got us here. */
280 kprobes_inc_nmissed_count(p);
281 save_previous_kprobe(kcb);
282 set_current_kprobe(p);
283 kcb->kprobe_status = KPROBE_REENTER;
284 singlestep(p, regs, kcb);
285 restore_previous_kprobe(kcb);
288 /* A nested probe was hit in FIQ, it is a BUG */
289 pr_warn("Unrecoverable kprobe detected.\n");
293 /* impossible cases */
297 /* Probe hit and conditional execution check ok. */
298 set_current_kprobe(p);
299 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
302 * If we have no pre-handler or it returned 0, we
303 * continue with normal processing. If we have a
304 * pre-handler and it returned non-zero, it will
305 * modify the execution path and no need to single
306 * stepping. Let's just reset current kprobe and exit.
308 if (!p->pre_handler || !p->pre_handler(p, regs)) {
309 kcb->kprobe_status = KPROBE_HIT_SS;
310 singlestep(p, regs, kcb);
311 if (p->post_handler) {
312 kcb->kprobe_status = KPROBE_HIT_SSDONE;
313 p->post_handler(p, regs, 0);
316 reset_current_kprobe();
320 * The probe was removed and a race is in progress.
321 * There is nothing we can do about it. Let's restart
322 * the instruction. By the time we can restart, the
323 * real instruction will be there.
328 static int __kprobes kprobe_trap_handler(struct pt_regs *regs, unsigned int instr)
331 local_irq_save(flags);
332 kprobe_handler(regs);
333 local_irq_restore(flags);
337 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
339 struct kprobe *cur = kprobe_running();
340 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
342 switch (kcb->kprobe_status) {
346 * We are here because the instruction being single
347 * stepped caused a page fault. We reset the current
348 * kprobe and the PC to point back to the probe address
349 * and allow the page fault handler to continue as a
352 regs->ARM_pc = (long)cur->addr;
353 if (kcb->kprobe_status == KPROBE_REENTER) {
354 restore_previous_kprobe(kcb);
356 reset_current_kprobe();
360 case KPROBE_HIT_ACTIVE:
361 case KPROBE_HIT_SSDONE:
363 * We increment the nmissed count for accounting,
364 * we can also use npre/npostfault count for accounting
365 * these specific fault cases.
367 kprobes_inc_nmissed_count(cur);
370 * We come here because instructions in the pre/post
371 * handler caused the page_fault, this could happen
372 * if handler tries to access user space by
373 * copy_from_user(), get_user() etc. Let the
374 * user-specified handler try to fix it.
376 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
387 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
388 unsigned long val, void *data)
391 * notify_die() is currently never called on ARM,
392 * so this callback is currently empty.
398 * When a retprobed function returns, trampoline_handler() is called,
399 * calling the kretprobe's handler. We construct a struct pt_regs to
400 * give a view of registers r0-r11 to the user return-handler. This is
401 * not a complete pt_regs structure, but that should be plenty sufficient
402 * for kretprobe handlers which should normally be interested in r0 only
405 void __naked __kprobes kretprobe_trampoline(void)
407 __asm__ __volatile__ (
408 "stmdb sp!, {r0 - r11} \n\t"
410 "bl trampoline_handler \n\t"
412 "ldmia sp!, {r0 - r11} \n\t"
413 #ifdef CONFIG_THUMB2_KERNEL
421 /* Called from kretprobe_trampoline */
422 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
424 struct kretprobe_instance *ri = NULL;
425 struct hlist_head *head, empty_rp;
426 struct hlist_node *tmp;
427 unsigned long flags, orig_ret_address = 0;
428 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
429 kprobe_opcode_t *correct_ret_addr = NULL;
431 INIT_HLIST_HEAD(&empty_rp);
432 kretprobe_hash_lock(current, &head, &flags);
435 * It is possible to have multiple instances associated with a given
436 * task either because multiple functions in the call path have
437 * a return probe installed on them, and/or more than one return
438 * probe was registered for a target function.
440 * We can handle this because:
441 * - instances are always inserted at the head of the list
442 * - when multiple return probes are registered for the same
443 * function, the first instance's ret_addr will point to the
444 * real return address, and all the rest will point to
445 * kretprobe_trampoline
447 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
448 if (ri->task != current)
449 /* another task is sharing our hash bucket */
452 orig_ret_address = (unsigned long)ri->ret_addr;
454 if (orig_ret_address != trampoline_address)
456 * This is the real return address. Any other
457 * instances associated with this task are for
458 * other calls deeper on the call stack
463 kretprobe_assert(ri, orig_ret_address, trampoline_address);
465 correct_ret_addr = ri->ret_addr;
466 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
467 if (ri->task != current)
468 /* another task is sharing our hash bucket */
471 orig_ret_address = (unsigned long)ri->ret_addr;
472 if (ri->rp && ri->rp->handler) {
473 __this_cpu_write(current_kprobe, &ri->rp->kp);
474 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
475 ri->ret_addr = correct_ret_addr;
476 ri->rp->handler(ri, regs);
477 __this_cpu_write(current_kprobe, NULL);
480 recycle_rp_inst(ri, &empty_rp);
482 if (orig_ret_address != trampoline_address)
484 * This is the real return address. Any other
485 * instances associated with this task are for
486 * other calls deeper on the call stack
491 kretprobe_hash_unlock(current, &flags);
493 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
494 hlist_del(&ri->hlist);
498 return (void *)orig_ret_address;
501 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
502 struct pt_regs *regs)
504 ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr;
506 /* Replace the return addr with trampoline addr. */
507 regs->ARM_lr = (unsigned long)&kretprobe_trampoline;
510 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
515 #ifdef CONFIG_THUMB2_KERNEL
517 static struct undef_hook kprobes_thumb16_break_hook = {
518 .instr_mask = 0xffff,
519 .instr_val = KPROBE_THUMB16_BREAKPOINT_INSTRUCTION,
520 .cpsr_mask = MODE_MASK,
521 .cpsr_val = SVC_MODE,
522 .fn = kprobe_trap_handler,
525 static struct undef_hook kprobes_thumb32_break_hook = {
526 .instr_mask = 0xffffffff,
527 .instr_val = KPROBE_THUMB32_BREAKPOINT_INSTRUCTION,
528 .cpsr_mask = MODE_MASK,
529 .cpsr_val = SVC_MODE,
530 .fn = kprobe_trap_handler,
533 #else /* !CONFIG_THUMB2_KERNEL */
535 static struct undef_hook kprobes_arm_break_hook = {
536 .instr_mask = 0x0fffffff,
537 .instr_val = KPROBE_ARM_BREAKPOINT_INSTRUCTION,
538 .cpsr_mask = MODE_MASK,
539 .cpsr_val = SVC_MODE,
540 .fn = kprobe_trap_handler,
543 #endif /* !CONFIG_THUMB2_KERNEL */
545 int __init arch_init_kprobes(void)
547 arm_probes_decode_init();
548 #ifdef CONFIG_THUMB2_KERNEL
549 register_undef_hook(&kprobes_thumb16_break_hook);
550 register_undef_hook(&kprobes_thumb32_break_hook);
552 register_undef_hook(&kprobes_arm_break_hook);
557 bool arch_within_kprobe_blacklist(unsigned long addr)
559 void *a = (void *)addr;
561 return __in_irqentry_text(addr) ||
562 in_entry_text(addr) ||
563 in_idmap_text(addr) ||
564 memory_contains(__kprobes_text_start, __kprobes_text_end, a, 1);