1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm64/kernel/probes/kprobes.c
5 * Kprobes support for ARM64
7 * Copyright (C) 2013 Linaro Limited.
8 * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
11 #define pr_fmt(fmt) "kprobes: " fmt
13 #include <linux/extable.h>
14 #include <linux/kasan.h>
15 #include <linux/kernel.h>
16 #include <linux/kprobes.h>
17 #include <linux/sched/debug.h>
18 #include <linux/set_memory.h>
19 #include <linux/slab.h>
20 #include <linux/stop_machine.h>
21 #include <linux/stringify.h>
22 #include <linux/uaccess.h>
23 #include <linux/vmalloc.h>
25 #include <asm/cacheflush.h>
26 #include <asm/daifflags.h>
27 #include <asm/debug-monitors.h>
30 #include <asm/patching.h>
31 #include <asm/ptrace.h>
32 #include <asm/sections.h>
33 #include <asm/system_misc.h>
34 #include <asm/traps.h>
36 #include "decode-insn.h"
38 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
39 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
42 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
44 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
46 kprobe_opcode_t *addr = p->ainsn.api.insn;
47 void *addrs[] = {addr, addr + 1};
48 u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
50 /* prepare insn slot */
51 aarch64_insn_patch_text(addrs, insns, 2);
53 flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));
56 * Needs restoring of return address after stepping xol.
58 p->ainsn.api.restore = (unsigned long) p->addr +
59 sizeof(kprobe_opcode_t);
62 static void __kprobes arch_prepare_simulate(struct kprobe *p)
64 /* This instructions is not executed xol. No need to adjust the PC */
65 p->ainsn.api.restore = 0;
68 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
70 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
72 if (p->ainsn.api.handler)
73 p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
75 /* single step simulated, now go for post processing */
76 post_kprobe_handler(p, kcb, regs);
79 int __kprobes arch_prepare_kprobe(struct kprobe *p)
81 unsigned long probe_addr = (unsigned long)p->addr;
86 /* copy instruction */
87 p->opcode = le32_to_cpu(*p->addr);
89 if (search_exception_tables(probe_addr))
92 /* decode instruction */
93 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
94 case INSN_REJECTED: /* insn not supported */
97 case INSN_GOOD_NO_SLOT: /* insn need simulation */
98 p->ainsn.api.insn = NULL;
101 case INSN_GOOD: /* instruction uses slot */
102 p->ainsn.api.insn = get_insn_slot();
103 if (!p->ainsn.api.insn)
108 /* prepare the instruction */
109 if (p->ainsn.api.insn)
110 arch_prepare_ss_slot(p);
112 arch_prepare_simulate(p);
117 void *alloc_insn_page(void)
119 return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
120 GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS,
121 NUMA_NO_NODE, __builtin_return_address(0));
124 /* arm kprobe: install breakpoint in text */
125 void __kprobes arch_arm_kprobe(struct kprobe *p)
127 void *addr = p->addr;
128 u32 insn = BRK64_OPCODE_KPROBES;
130 aarch64_insn_patch_text(&addr, &insn, 1);
133 /* disarm kprobe: remove breakpoint from text */
134 void __kprobes arch_disarm_kprobe(struct kprobe *p)
136 void *addr = p->addr;
138 aarch64_insn_patch_text(&addr, &p->opcode, 1);
141 void __kprobes arch_remove_kprobe(struct kprobe *p)
143 if (p->ainsn.api.insn) {
144 free_insn_slot(p->ainsn.api.insn, 0);
145 p->ainsn.api.insn = NULL;
149 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
151 kcb->prev_kprobe.kp = kprobe_running();
152 kcb->prev_kprobe.status = kcb->kprobe_status;
155 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
157 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
158 kcb->kprobe_status = kcb->prev_kprobe.status;
161 static void __kprobes set_current_kprobe(struct kprobe *p)
163 __this_cpu_write(current_kprobe, p);
167 * Mask all of DAIF while executing the instruction out-of-line, to keep things
168 * simple and avoid nesting exceptions. Interrupts do have to be disabled since
169 * the kprobe state is per-CPU and doesn't get migrated.
171 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
172 struct pt_regs *regs)
174 kcb->saved_irqflag = regs->pstate & DAIF_MASK;
175 regs->pstate |= DAIF_MASK;
178 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
179 struct pt_regs *regs)
181 regs->pstate &= ~DAIF_MASK;
182 regs->pstate |= kcb->saved_irqflag;
185 static void __kprobes setup_singlestep(struct kprobe *p,
186 struct pt_regs *regs,
187 struct kprobe_ctlblk *kcb, int reenter)
192 save_previous_kprobe(kcb);
193 set_current_kprobe(p);
194 kcb->kprobe_status = KPROBE_REENTER;
196 kcb->kprobe_status = KPROBE_HIT_SS;
200 if (p->ainsn.api.insn) {
201 /* prepare for single stepping */
202 slot = (unsigned long)p->ainsn.api.insn;
204 kprobes_save_local_irqflag(kcb, regs);
205 instruction_pointer_set(regs, slot);
207 /* insn simulation */
208 arch_simulate_insn(p, regs);
212 static int __kprobes reenter_kprobe(struct kprobe *p,
213 struct pt_regs *regs,
214 struct kprobe_ctlblk *kcb)
216 switch (kcb->kprobe_status) {
217 case KPROBE_HIT_SSDONE:
218 case KPROBE_HIT_ACTIVE:
219 kprobes_inc_nmissed_count(p);
220 setup_singlestep(p, regs, kcb, 1);
224 pr_warn("Failed to recover from reentered kprobes.\n");
236 static void __kprobes
237 post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
239 /* return addr restore if non-branching insn */
240 if (cur->ainsn.api.restore != 0)
241 instruction_pointer_set(regs, cur->ainsn.api.restore);
243 /* restore back original saved kprobe variables and continue */
244 if (kcb->kprobe_status == KPROBE_REENTER) {
245 restore_previous_kprobe(kcb);
248 /* call post handler */
249 kcb->kprobe_status = KPROBE_HIT_SSDONE;
250 if (cur->post_handler)
251 cur->post_handler(cur, regs, 0);
253 reset_current_kprobe();
256 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
258 struct kprobe *cur = kprobe_running();
259 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
261 switch (kcb->kprobe_status) {
265 * We are here because the instruction being single
266 * stepped caused a page fault. We reset the current
267 * kprobe and the ip points back to the probe address
268 * and allow the page fault handler to continue as a
271 instruction_pointer_set(regs, (unsigned long) cur->addr);
272 BUG_ON(!instruction_pointer(regs));
274 if (kcb->kprobe_status == KPROBE_REENTER) {
275 restore_previous_kprobe(kcb);
277 kprobes_restore_local_irqflag(kcb, regs);
278 reset_current_kprobe();
282 case KPROBE_HIT_ACTIVE:
283 case KPROBE_HIT_SSDONE:
285 * In case the user-specified fault handler returned
286 * zero, try to fix up.
288 if (fixup_exception(regs))
294 static void __kprobes kprobe_handler(struct pt_regs *regs)
296 struct kprobe *p, *cur_kprobe;
297 struct kprobe_ctlblk *kcb;
298 unsigned long addr = instruction_pointer(regs);
300 kcb = get_kprobe_ctlblk();
301 cur_kprobe = kprobe_running();
303 p = get_kprobe((kprobe_opcode_t *) addr);
307 if (reenter_kprobe(p, regs, kcb))
311 set_current_kprobe(p);
312 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
315 * If we have no pre-handler or it returned 0, we
316 * continue with normal processing. If we have a
317 * pre-handler and it returned non-zero, it will
318 * modify the execution path and no need to single
319 * stepping. Let's just reset current kprobe and exit.
321 if (!p->pre_handler || !p->pre_handler(p, regs)) {
322 setup_singlestep(p, regs, kcb, 0);
324 reset_current_kprobe();
328 * The breakpoint instruction was removed right
329 * after we hit it. Another cpu has removed
330 * either a probepoint or a debugger breakpoint
331 * at this address. In either case, no further
332 * handling of this interrupt is appropriate.
333 * Return back to original instruction, and continue.
338 kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr)
340 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
341 unsigned long addr = instruction_pointer(regs);
342 struct kprobe *cur = kprobe_running();
344 if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
345 ((unsigned long)&cur->ainsn.api.insn[1] == addr)) {
346 kprobes_restore_local_irqflag(kcb, regs);
347 post_kprobe_handler(cur, kcb, regs);
349 return DBG_HOOK_HANDLED;
352 /* not ours, kprobes should ignore it */
353 return DBG_HOOK_ERROR;
356 static struct break_hook kprobes_break_ss_hook = {
357 .imm = KPROBES_BRK_SS_IMM,
358 .fn = kprobe_breakpoint_ss_handler,
362 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned long esr)
364 kprobe_handler(regs);
365 return DBG_HOOK_HANDLED;
368 static struct break_hook kprobes_break_hook = {
369 .imm = KPROBES_BRK_IMM,
370 .fn = kprobe_breakpoint_handler,
374 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
375 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
377 int __init arch_populate_kprobe_blacklist(void)
381 ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
382 (unsigned long)__entry_text_end);
385 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
386 (unsigned long)__irqentry_text_end);
389 ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
390 (unsigned long)__idmap_text_end);
393 ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
394 (unsigned long)__hyp_text_end);
395 if (ret || is_kernel_in_hyp_mode())
397 ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
398 (unsigned long)__hyp_idmap_text_end);
402 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
404 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline,
405 (void *)kernel_stack_pointer(regs));
408 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
409 struct pt_regs *regs)
411 ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
412 ri->fp = (void *)kernel_stack_pointer(regs);
414 /* replace return addr (x30) with trampoline */
415 regs->regs[30] = (long)&kretprobe_trampoline;
418 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
423 int __init arch_init_kprobes(void)
425 register_kernel_break_hook(&kprobes_break_hook);
426 register_kernel_break_hook(&kprobes_break_ss_hook);