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/kasan.h>
11 #include <linux/kernel.h>
12 #include <linux/kprobes.h>
13 #include <linux/extable.h>
14 #include <linux/slab.h>
15 #include <linux/stop_machine.h>
16 #include <linux/sched/debug.h>
17 #include <linux/set_memory.h>
18 #include <linux/stringify.h>
19 #include <linux/vmalloc.h>
20 #include <asm/traps.h>
21 #include <asm/ptrace.h>
22 #include <asm/cacheflush.h>
23 #include <asm/debug-monitors.h>
24 #include <asm/daifflags.h>
25 #include <asm/system_misc.h>
27 #include <linux/uaccess.h>
29 #include <asm/sections.h>
31 #include "decode-insn.h"
33 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
34 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
37 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
39 static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode)
47 return aarch64_insn_patch_text(addrs, insns, 1);
50 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
52 /* prepare insn slot */
53 patch_text(p->ainsn.api.insn, p->opcode);
55 flush_icache_range((uintptr_t) (p->ainsn.api.insn),
56 (uintptr_t) (p->ainsn.api.insn) +
57 MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
60 * Needs restoring of return address after stepping xol.
62 p->ainsn.api.restore = (unsigned long) p->addr +
63 sizeof(kprobe_opcode_t);
66 static void __kprobes arch_prepare_simulate(struct kprobe *p)
68 /* This instructions is not executed xol. No need to adjust the PC */
69 p->ainsn.api.restore = 0;
72 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
74 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
76 if (p->ainsn.api.handler)
77 p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
79 /* single step simulated, now go for post processing */
80 post_kprobe_handler(kcb, regs);
83 int __kprobes arch_prepare_kprobe(struct kprobe *p)
85 unsigned long probe_addr = (unsigned long)p->addr;
90 /* copy instruction */
91 p->opcode = le32_to_cpu(*p->addr);
93 if (search_exception_tables(probe_addr))
96 /* decode instruction */
97 switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
98 case INSN_REJECTED: /* insn not supported */
101 case INSN_GOOD_NO_SLOT: /* insn need simulation */
102 p->ainsn.api.insn = NULL;
105 case INSN_GOOD: /* instruction uses slot */
106 p->ainsn.api.insn = get_insn_slot();
107 if (!p->ainsn.api.insn)
112 /* prepare the instruction */
113 if (p->ainsn.api.insn)
114 arch_prepare_ss_slot(p);
116 arch_prepare_simulate(p);
121 void *alloc_insn_page(void)
123 return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
124 GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS,
125 NUMA_NO_NODE, __builtin_return_address(0));
128 /* arm kprobe: install breakpoint in text */
129 void __kprobes arch_arm_kprobe(struct kprobe *p)
131 patch_text(p->addr, BRK64_OPCODE_KPROBES);
134 /* disarm kprobe: remove breakpoint from text */
135 void __kprobes arch_disarm_kprobe(struct kprobe *p)
137 patch_text(p->addr, p->opcode);
140 void __kprobes arch_remove_kprobe(struct kprobe *p)
142 if (p->ainsn.api.insn) {
143 free_insn_slot(p->ainsn.api.insn, 0);
144 p->ainsn.api.insn = NULL;
148 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
150 kcb->prev_kprobe.kp = kprobe_running();
151 kcb->prev_kprobe.status = kcb->kprobe_status;
154 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
156 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
157 kcb->kprobe_status = kcb->prev_kprobe.status;
160 static void __kprobes set_current_kprobe(struct kprobe *p)
162 __this_cpu_write(current_kprobe, p);
166 * Interrupts need to be disabled before single-step mode is set, and not
167 * reenabled until after single-step mode ends.
168 * Without disabling interrupt on local CPU, there is a chance of
169 * interrupt occurrence in the period of exception return and start of
170 * out-of-line single-step, that result in wrongly single stepping
171 * into the interrupt handler.
173 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
174 struct pt_regs *regs)
176 kcb->saved_irqflag = regs->pstate & DAIF_MASK;
177 regs->pstate |= PSR_I_BIT;
178 /* Unmask PSTATE.D for enabling software step exceptions. */
179 regs->pstate &= ~PSR_D_BIT;
182 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
183 struct pt_regs *regs)
185 regs->pstate &= ~DAIF_MASK;
186 regs->pstate |= kcb->saved_irqflag;
189 static void __kprobes
190 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
192 kcb->ss_ctx.ss_pending = true;
193 kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
196 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
198 kcb->ss_ctx.ss_pending = false;
199 kcb->ss_ctx.match_addr = 0;
202 static void __kprobes setup_singlestep(struct kprobe *p,
203 struct pt_regs *regs,
204 struct kprobe_ctlblk *kcb, int reenter)
209 save_previous_kprobe(kcb);
210 set_current_kprobe(p);
211 kcb->kprobe_status = KPROBE_REENTER;
213 kcb->kprobe_status = KPROBE_HIT_SS;
217 if (p->ainsn.api.insn) {
218 /* prepare for single stepping */
219 slot = (unsigned long)p->ainsn.api.insn;
221 set_ss_context(kcb, slot); /* mark pending ss */
223 /* IRQs and single stepping do not mix well. */
224 kprobes_save_local_irqflag(kcb, regs);
225 kernel_enable_single_step(regs);
226 instruction_pointer_set(regs, slot);
228 /* insn simulation */
229 arch_simulate_insn(p, regs);
233 static int __kprobes reenter_kprobe(struct kprobe *p,
234 struct pt_regs *regs,
235 struct kprobe_ctlblk *kcb)
237 switch (kcb->kprobe_status) {
238 case KPROBE_HIT_SSDONE:
239 case KPROBE_HIT_ACTIVE:
240 kprobes_inc_nmissed_count(p);
241 setup_singlestep(p, regs, kcb, 1);
245 pr_warn("Unrecoverable kprobe detected.\n");
257 static void __kprobes
258 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
260 struct kprobe *cur = kprobe_running();
265 /* return addr restore if non-branching insn */
266 if (cur->ainsn.api.restore != 0)
267 instruction_pointer_set(regs, cur->ainsn.api.restore);
269 /* restore back original saved kprobe variables and continue */
270 if (kcb->kprobe_status == KPROBE_REENTER) {
271 restore_previous_kprobe(kcb);
274 /* call post handler */
275 kcb->kprobe_status = KPROBE_HIT_SSDONE;
276 if (cur->post_handler) {
277 /* post_handler can hit breakpoint and single step
278 * again, so we enable D-flag for recursive exception.
280 cur->post_handler(cur, regs, 0);
283 reset_current_kprobe();
286 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
288 struct kprobe *cur = kprobe_running();
289 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
291 switch (kcb->kprobe_status) {
295 * We are here because the instruction being single
296 * stepped caused a page fault. We reset the current
297 * kprobe and the ip points back to the probe address
298 * and allow the page fault handler to continue as a
301 instruction_pointer_set(regs, (unsigned long) cur->addr);
302 if (!instruction_pointer(regs))
305 kernel_disable_single_step();
307 if (kcb->kprobe_status == KPROBE_REENTER)
308 restore_previous_kprobe(kcb);
310 reset_current_kprobe();
313 case KPROBE_HIT_ACTIVE:
314 case KPROBE_HIT_SSDONE:
316 * We increment the nmissed count for accounting,
317 * we can also use npre/npostfault count for accounting
318 * these specific fault cases.
320 kprobes_inc_nmissed_count(cur);
323 * We come here because instructions in the pre/post
324 * handler caused the page_fault, this could happen
325 * if handler tries to access user space by
326 * copy_from_user(), get_user() etc. Let the
327 * user-specified handler try to fix it first.
329 if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
333 * In case the user-specified fault handler returned
334 * zero, try to fix up.
336 if (fixup_exception(regs))
342 static void __kprobes kprobe_handler(struct pt_regs *regs)
344 struct kprobe *p, *cur_kprobe;
345 struct kprobe_ctlblk *kcb;
346 unsigned long addr = instruction_pointer(regs);
348 kcb = get_kprobe_ctlblk();
349 cur_kprobe = kprobe_running();
351 p = get_kprobe((kprobe_opcode_t *) addr);
355 if (reenter_kprobe(p, regs, kcb))
359 set_current_kprobe(p);
360 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
363 * If we have no pre-handler or it returned 0, we
364 * continue with normal processing. If we have a
365 * pre-handler and it returned non-zero, it will
366 * modify the execution path and no need to single
367 * stepping. Let's just reset current kprobe and exit.
369 * pre_handler can hit a breakpoint and can step thru
370 * before return, keep PSTATE D-flag enabled until
371 * pre_handler return back.
373 if (!p->pre_handler || !p->pre_handler(p, regs)) {
374 setup_singlestep(p, regs, kcb, 0);
376 reset_current_kprobe();
380 * The breakpoint instruction was removed right
381 * after we hit it. Another cpu has removed
382 * either a probepoint or a debugger breakpoint
383 * at this address. In either case, no further
384 * handling of this interrupt is appropriate.
385 * Return back to original instruction, and continue.
390 kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
392 if ((kcb->ss_ctx.ss_pending)
393 && (kcb->ss_ctx.match_addr == addr)) {
394 clear_ss_context(kcb); /* clear pending ss */
395 return DBG_HOOK_HANDLED;
397 /* not ours, kprobes should ignore it */
398 return DBG_HOOK_ERROR;
402 kprobe_single_step_handler(struct pt_regs *regs, unsigned int esr)
404 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
407 /* return error if this is not our step */
408 retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
410 if (retval == DBG_HOOK_HANDLED) {
411 kprobes_restore_local_irqflag(kcb, regs);
412 kernel_disable_single_step();
414 post_kprobe_handler(kcb, regs);
420 static struct step_hook kprobes_step_hook = {
421 .fn = kprobe_single_step_handler,
425 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
427 kprobe_handler(regs);
428 return DBG_HOOK_HANDLED;
431 static struct break_hook kprobes_break_hook = {
432 .imm = KPROBES_BRK_IMM,
433 .fn = kprobe_breakpoint_handler,
437 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
438 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
440 int __init arch_populate_kprobe_blacklist(void)
444 ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
445 (unsigned long)__entry_text_end);
448 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
449 (unsigned long)__irqentry_text_end);
452 ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
453 (unsigned long)__idmap_text_end);
456 ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
457 (unsigned long)__hyp_text_end);
458 if (ret || is_kernel_in_hyp_mode())
460 ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
461 (unsigned long)__hyp_idmap_text_end);
465 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
467 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline,
468 (void *)kernel_stack_pointer(regs));
471 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
472 struct pt_regs *regs)
474 ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
475 ri->fp = (void *)kernel_stack_pointer(regs);
477 /* replace return addr (x30) with trampoline */
478 regs->regs[30] = (long)&kretprobe_trampoline;
481 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
486 int __init arch_init_kprobes(void)
488 register_kernel_break_hook(&kprobes_break_hook);
489 register_kernel_step_hook(&kprobes_step_hook);