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>
10 #include <linux/extable.h>
11 #include <linux/kasan.h>
12 #include <linux/kernel.h>
13 #include <linux/kprobes.h>
14 #include <linux/sched/debug.h>
15 #include <linux/set_memory.h>
16 #include <linux/slab.h>
17 #include <linux/stop_machine.h>
18 #include <linux/stringify.h>
19 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
22 #include <asm/cacheflush.h>
23 #include <asm/daifflags.h>
24 #include <asm/debug-monitors.h>
27 #include <asm/patching.h>
28 #include <asm/ptrace.h>
29 #include <asm/sections.h>
30 #include <asm/system_misc.h>
31 #include <asm/traps.h>
33 #include "decode-insn.h"
35 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
36 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
39 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
41 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
43 kprobe_opcode_t *addr = p->ainsn.api.insn;
44 void *addrs[] = {addr, addr + 1};
45 u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
47 /* prepare insn slot */
48 aarch64_insn_patch_text(addrs, insns, 2);
50 flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));
53 * Needs restoring of return address after stepping xol.
55 p->ainsn.api.restore = (unsigned long) p->addr +
56 sizeof(kprobe_opcode_t);
59 static void __kprobes arch_prepare_simulate(struct kprobe *p)
61 /* This instructions is not executed xol. No need to adjust the PC */
62 p->ainsn.api.restore = 0;
65 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
67 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
69 if (p->ainsn.api.handler)
70 p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
72 /* single step simulated, now go for post processing */
73 post_kprobe_handler(p, kcb, regs);
76 int __kprobes arch_prepare_kprobe(struct kprobe *p)
78 unsigned long probe_addr = (unsigned long)p->addr;
83 /* copy instruction */
84 p->opcode = le32_to_cpu(*p->addr);
86 if (search_exception_tables(probe_addr))
89 /* decode instruction */
90 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
91 case INSN_REJECTED: /* insn not supported */
94 case INSN_GOOD_NO_SLOT: /* insn need simulation */
95 p->ainsn.api.insn = NULL;
98 case INSN_GOOD: /* instruction uses slot */
99 p->ainsn.api.insn = get_insn_slot();
100 if (!p->ainsn.api.insn)
105 /* prepare the instruction */
106 if (p->ainsn.api.insn)
107 arch_prepare_ss_slot(p);
109 arch_prepare_simulate(p);
114 void *alloc_insn_page(void)
116 return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
117 GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS,
118 NUMA_NO_NODE, __builtin_return_address(0));
121 /* arm kprobe: install breakpoint in text */
122 void __kprobes arch_arm_kprobe(struct kprobe *p)
124 void *addr = p->addr;
125 u32 insn = BRK64_OPCODE_KPROBES;
127 aarch64_insn_patch_text(&addr, &insn, 1);
130 /* disarm kprobe: remove breakpoint from text */
131 void __kprobes arch_disarm_kprobe(struct kprobe *p)
133 void *addr = p->addr;
135 aarch64_insn_patch_text(&addr, &p->opcode, 1);
138 void __kprobes arch_remove_kprobe(struct kprobe *p)
140 if (p->ainsn.api.insn) {
141 free_insn_slot(p->ainsn.api.insn, 0);
142 p->ainsn.api.insn = NULL;
146 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
148 kcb->prev_kprobe.kp = kprobe_running();
149 kcb->prev_kprobe.status = kcb->kprobe_status;
152 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
154 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
155 kcb->kprobe_status = kcb->prev_kprobe.status;
158 static void __kprobes set_current_kprobe(struct kprobe *p)
160 __this_cpu_write(current_kprobe, p);
164 * Mask all of DAIF while executing the instruction out-of-line, to keep things
165 * simple and avoid nesting exceptions. Interrupts do have to be disabled since
166 * the kprobe state is per-CPU and doesn't get migrated.
168 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
169 struct pt_regs *regs)
171 kcb->saved_irqflag = regs->pstate & DAIF_MASK;
172 regs->pstate |= DAIF_MASK;
175 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
176 struct pt_regs *regs)
178 regs->pstate &= ~DAIF_MASK;
179 regs->pstate |= kcb->saved_irqflag;
182 static void __kprobes setup_singlestep(struct kprobe *p,
183 struct pt_regs *regs,
184 struct kprobe_ctlblk *kcb, int reenter)
189 save_previous_kprobe(kcb);
190 set_current_kprobe(p);
191 kcb->kprobe_status = KPROBE_REENTER;
193 kcb->kprobe_status = KPROBE_HIT_SS;
197 if (p->ainsn.api.insn) {
198 /* prepare for single stepping */
199 slot = (unsigned long)p->ainsn.api.insn;
201 kprobes_save_local_irqflag(kcb, regs);
202 instruction_pointer_set(regs, slot);
204 /* insn simulation */
205 arch_simulate_insn(p, regs);
209 static int __kprobes reenter_kprobe(struct kprobe *p,
210 struct pt_regs *regs,
211 struct kprobe_ctlblk *kcb)
213 switch (kcb->kprobe_status) {
214 case KPROBE_HIT_SSDONE:
215 case KPROBE_HIT_ACTIVE:
216 kprobes_inc_nmissed_count(p);
217 setup_singlestep(p, regs, kcb, 1);
221 pr_warn("Unrecoverable kprobe detected.\n");
233 static void __kprobes
234 post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
236 /* return addr restore if non-branching insn */
237 if (cur->ainsn.api.restore != 0)
238 instruction_pointer_set(regs, cur->ainsn.api.restore);
240 /* restore back original saved kprobe variables and continue */
241 if (kcb->kprobe_status == KPROBE_REENTER) {
242 restore_previous_kprobe(kcb);
245 /* call post handler */
246 kcb->kprobe_status = KPROBE_HIT_SSDONE;
247 if (cur->post_handler)
248 cur->post_handler(cur, regs, 0);
250 reset_current_kprobe();
253 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
255 struct kprobe *cur = kprobe_running();
256 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
258 switch (kcb->kprobe_status) {
262 * We are here because the instruction being single
263 * stepped caused a page fault. We reset the current
264 * kprobe and the ip points back to the probe address
265 * and allow the page fault handler to continue as a
268 instruction_pointer_set(regs, (unsigned long) cur->addr);
269 BUG_ON(!instruction_pointer(regs));
271 if (kcb->kprobe_status == KPROBE_REENTER) {
272 restore_previous_kprobe(kcb);
274 kprobes_restore_local_irqflag(kcb, regs);
275 reset_current_kprobe();
279 case KPROBE_HIT_ACTIVE:
280 case KPROBE_HIT_SSDONE:
282 * In case the user-specified fault handler returned
283 * zero, try to fix up.
285 if (fixup_exception(regs))
291 static void __kprobes kprobe_handler(struct pt_regs *regs)
293 struct kprobe *p, *cur_kprobe;
294 struct kprobe_ctlblk *kcb;
295 unsigned long addr = instruction_pointer(regs);
297 kcb = get_kprobe_ctlblk();
298 cur_kprobe = kprobe_running();
300 p = get_kprobe((kprobe_opcode_t *) addr);
304 if (reenter_kprobe(p, regs, kcb))
308 set_current_kprobe(p);
309 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
312 * If we have no pre-handler or it returned 0, we
313 * continue with normal processing. If we have a
314 * pre-handler and it returned non-zero, it will
315 * modify the execution path and no need to single
316 * stepping. Let's just reset current kprobe and exit.
318 if (!p->pre_handler || !p->pre_handler(p, regs)) {
319 setup_singlestep(p, regs, kcb, 0);
321 reset_current_kprobe();
325 * The breakpoint instruction was removed right
326 * after we hit it. Another cpu has removed
327 * either a probepoint or a debugger breakpoint
328 * at this address. In either case, no further
329 * handling of this interrupt is appropriate.
330 * Return back to original instruction, and continue.
335 kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned int esr)
337 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
338 unsigned long addr = instruction_pointer(regs);
339 struct kprobe *cur = kprobe_running();
341 if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
342 ((unsigned long)&cur->ainsn.api.insn[1] == addr)) {
343 kprobes_restore_local_irqflag(kcb, regs);
344 post_kprobe_handler(cur, kcb, regs);
346 return DBG_HOOK_HANDLED;
349 /* not ours, kprobes should ignore it */
350 return DBG_HOOK_ERROR;
353 static struct break_hook kprobes_break_ss_hook = {
354 .imm = KPROBES_BRK_SS_IMM,
355 .fn = kprobe_breakpoint_ss_handler,
359 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
361 kprobe_handler(regs);
362 return DBG_HOOK_HANDLED;
365 static struct break_hook kprobes_break_hook = {
366 .imm = KPROBES_BRK_IMM,
367 .fn = kprobe_breakpoint_handler,
371 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
372 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
374 int __init arch_populate_kprobe_blacklist(void)
378 ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
379 (unsigned long)__entry_text_end);
382 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
383 (unsigned long)__irqentry_text_end);
386 ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
387 (unsigned long)__idmap_text_end);
390 ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
391 (unsigned long)__hyp_text_end);
392 if (ret || is_kernel_in_hyp_mode())
394 ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
395 (unsigned long)__hyp_idmap_text_end);
399 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
401 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline,
402 (void *)kernel_stack_pointer(regs));
405 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
406 struct pt_regs *regs)
408 ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
409 ri->fp = (void *)kernel_stack_pointer(regs);
411 /* replace return addr (x30) with trampoline */
412 regs->regs[30] = (long)&kretprobe_trampoline;
415 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
420 int __init arch_init_kprobes(void)
422 register_kernel_break_hook(&kprobes_break_hook);
423 register_kernel_break_hook(&kprobes_break_ss_hook);