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)
125 page
= vmalloc_exec(PAGE_SIZE
);
127 set_memory_ro((unsigned long)page
, 1);
128 set_vm_flush_reset_perms(page
);
134 /* arm kprobe: install breakpoint in text */
135 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
137 patch_text(p
->addr
, BRK64_OPCODE_KPROBES
);
140 /* disarm kprobe: remove breakpoint from text */
141 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
143 patch_text(p
->addr
, p
->opcode
);
146 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
148 if (p
->ainsn
.api
.insn
) {
149 free_insn_slot(p
->ainsn
.api
.insn
, 0);
150 p
->ainsn
.api
.insn
= NULL
;
154 static void __kprobes
save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
156 kcb
->prev_kprobe
.kp
= kprobe_running();
157 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
160 static void __kprobes
restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
162 __this_cpu_write(current_kprobe
, kcb
->prev_kprobe
.kp
);
163 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
166 static void __kprobes
set_current_kprobe(struct kprobe
*p
)
168 __this_cpu_write(current_kprobe
, p
);
172 * Interrupts need to be disabled before single-step mode is set, and not
173 * reenabled until after single-step mode ends.
174 * Without disabling interrupt on local CPU, there is a chance of
175 * interrupt occurrence in the period of exception return and start of
176 * out-of-line single-step, that result in wrongly single stepping
177 * into the interrupt handler.
179 static void __kprobes
kprobes_save_local_irqflag(struct kprobe_ctlblk
*kcb
,
180 struct pt_regs
*regs
)
182 kcb
->saved_irqflag
= regs
->pstate
& DAIF_MASK
;
183 regs
->pstate
|= PSR_I_BIT
;
184 /* Unmask PSTATE.D for enabling software step exceptions. */
185 regs
->pstate
&= ~PSR_D_BIT
;
188 static void __kprobes
kprobes_restore_local_irqflag(struct kprobe_ctlblk
*kcb
,
189 struct pt_regs
*regs
)
191 regs
->pstate
&= ~DAIF_MASK
;
192 regs
->pstate
|= kcb
->saved_irqflag
;
195 static void __kprobes
196 set_ss_context(struct kprobe_ctlblk
*kcb
, unsigned long addr
)
198 kcb
->ss_ctx
.ss_pending
= true;
199 kcb
->ss_ctx
.match_addr
= addr
+ sizeof(kprobe_opcode_t
);
202 static void __kprobes
clear_ss_context(struct kprobe_ctlblk
*kcb
)
204 kcb
->ss_ctx
.ss_pending
= false;
205 kcb
->ss_ctx
.match_addr
= 0;
208 static void __kprobes
setup_singlestep(struct kprobe
*p
,
209 struct pt_regs
*regs
,
210 struct kprobe_ctlblk
*kcb
, int reenter
)
215 save_previous_kprobe(kcb
);
216 set_current_kprobe(p
);
217 kcb
->kprobe_status
= KPROBE_REENTER
;
219 kcb
->kprobe_status
= KPROBE_HIT_SS
;
223 if (p
->ainsn
.api
.insn
) {
224 /* prepare for single stepping */
225 slot
= (unsigned long)p
->ainsn
.api
.insn
;
227 set_ss_context(kcb
, slot
); /* mark pending ss */
229 /* IRQs and single stepping do not mix well. */
230 kprobes_save_local_irqflag(kcb
, regs
);
231 kernel_enable_single_step(regs
);
232 instruction_pointer_set(regs
, slot
);
234 /* insn simulation */
235 arch_simulate_insn(p
, regs
);
239 static int __kprobes
reenter_kprobe(struct kprobe
*p
,
240 struct pt_regs
*regs
,
241 struct kprobe_ctlblk
*kcb
)
243 switch (kcb
->kprobe_status
) {
244 case KPROBE_HIT_SSDONE
:
245 case KPROBE_HIT_ACTIVE
:
246 kprobes_inc_nmissed_count(p
);
247 setup_singlestep(p
, regs
, kcb
, 1);
251 pr_warn("Unrecoverable kprobe detected.\n");
263 static void __kprobes
264 post_kprobe_handler(struct kprobe_ctlblk
*kcb
, struct pt_regs
*regs
)
266 struct kprobe
*cur
= kprobe_running();
271 /* return addr restore if non-branching insn */
272 if (cur
->ainsn
.api
.restore
!= 0)
273 instruction_pointer_set(regs
, cur
->ainsn
.api
.restore
);
275 /* restore back original saved kprobe variables and continue */
276 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
277 restore_previous_kprobe(kcb
);
280 /* call post handler */
281 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
282 if (cur
->post_handler
) {
283 /* post_handler can hit breakpoint and single step
284 * again, so we enable D-flag for recursive exception.
286 cur
->post_handler(cur
, regs
, 0);
289 reset_current_kprobe();
292 int __kprobes
kprobe_fault_handler(struct pt_regs
*regs
, unsigned int fsr
)
294 struct kprobe
*cur
= kprobe_running();
295 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
297 switch (kcb
->kprobe_status
) {
301 * We are here because the instruction being single
302 * stepped caused a page fault. We reset the current
303 * kprobe and the ip points back to the probe address
304 * and allow the page fault handler to continue as a
307 instruction_pointer_set(regs
, (unsigned long) cur
->addr
);
308 if (!instruction_pointer(regs
))
311 kernel_disable_single_step();
313 if (kcb
->kprobe_status
== KPROBE_REENTER
)
314 restore_previous_kprobe(kcb
);
316 reset_current_kprobe();
319 case KPROBE_HIT_ACTIVE
:
320 case KPROBE_HIT_SSDONE
:
322 * We increment the nmissed count for accounting,
323 * we can also use npre/npostfault count for accounting
324 * these specific fault cases.
326 kprobes_inc_nmissed_count(cur
);
329 * We come here because instructions in the pre/post
330 * handler caused the page_fault, this could happen
331 * if handler tries to access user space by
332 * copy_from_user(), get_user() etc. Let the
333 * user-specified handler try to fix it first.
335 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, fsr
))
339 * In case the user-specified fault handler returned
340 * zero, try to fix up.
342 if (fixup_exception(regs
))
348 static void __kprobes
kprobe_handler(struct pt_regs
*regs
)
350 struct kprobe
*p
, *cur_kprobe
;
351 struct kprobe_ctlblk
*kcb
;
352 unsigned long addr
= instruction_pointer(regs
);
354 kcb
= get_kprobe_ctlblk();
355 cur_kprobe
= kprobe_running();
357 p
= get_kprobe((kprobe_opcode_t
*) addr
);
361 if (reenter_kprobe(p
, regs
, kcb
))
365 set_current_kprobe(p
);
366 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
369 * If we have no pre-handler or it returned 0, we
370 * continue with normal processing. If we have a
371 * pre-handler and it returned non-zero, it will
372 * modify the execution path and no need to single
373 * stepping. Let's just reset current kprobe and exit.
375 * pre_handler can hit a breakpoint and can step thru
376 * before return, keep PSTATE D-flag enabled until
377 * pre_handler return back.
379 if (!p
->pre_handler
|| !p
->pre_handler(p
, regs
)) {
380 setup_singlestep(p
, regs
, kcb
, 0);
382 reset_current_kprobe();
386 * The breakpoint instruction was removed right
387 * after we hit it. Another cpu has removed
388 * either a probepoint or a debugger breakpoint
389 * at this address. In either case, no further
390 * handling of this interrupt is appropriate.
391 * Return back to original instruction, and continue.
396 kprobe_ss_hit(struct kprobe_ctlblk
*kcb
, unsigned long addr
)
398 if ((kcb
->ss_ctx
.ss_pending
)
399 && (kcb
->ss_ctx
.match_addr
== addr
)) {
400 clear_ss_context(kcb
); /* clear pending ss */
401 return DBG_HOOK_HANDLED
;
403 /* not ours, kprobes should ignore it */
404 return DBG_HOOK_ERROR
;
408 kprobe_single_step_handler(struct pt_regs
*regs
, unsigned int esr
)
410 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
413 /* return error if this is not our step */
414 retval
= kprobe_ss_hit(kcb
, instruction_pointer(regs
));
416 if (retval
== DBG_HOOK_HANDLED
) {
417 kprobes_restore_local_irqflag(kcb
, regs
);
418 kernel_disable_single_step();
420 post_kprobe_handler(kcb
, regs
);
426 static struct step_hook kprobes_step_hook
= {
427 .fn
= kprobe_single_step_handler
,
431 kprobe_breakpoint_handler(struct pt_regs
*regs
, unsigned int esr
)
433 kprobe_handler(regs
);
434 return DBG_HOOK_HANDLED
;
437 static struct break_hook kprobes_break_hook
= {
438 .imm
= KPROBES_BRK_IMM
,
439 .fn
= kprobe_breakpoint_handler
,
443 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
444 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
446 int __init
arch_populate_kprobe_blacklist(void)
450 ret
= kprobe_add_area_blacklist((unsigned long)__entry_text_start
,
451 (unsigned long)__entry_text_end
);
454 ret
= kprobe_add_area_blacklist((unsigned long)__irqentry_text_start
,
455 (unsigned long)__irqentry_text_end
);
458 ret
= kprobe_add_area_blacklist((unsigned long)__idmap_text_start
,
459 (unsigned long)__idmap_text_end
);
462 ret
= kprobe_add_area_blacklist((unsigned long)__hyp_text_start
,
463 (unsigned long)__hyp_text_end
);
464 if (ret
|| is_kernel_in_hyp_mode())
466 ret
= kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start
,
467 (unsigned long)__hyp_idmap_text_end
);
471 void __kprobes __used
*trampoline_probe_handler(struct pt_regs
*regs
)
473 struct kretprobe_instance
*ri
= NULL
;
474 struct hlist_head
*head
, empty_rp
;
475 struct hlist_node
*tmp
;
476 unsigned long flags
, orig_ret_address
= 0;
477 unsigned long trampoline_address
=
478 (unsigned long)&kretprobe_trampoline
;
479 kprobe_opcode_t
*correct_ret_addr
= NULL
;
481 INIT_HLIST_HEAD(&empty_rp
);
482 kretprobe_hash_lock(current
, &head
, &flags
);
485 * It is possible to have multiple instances associated with a given
486 * task either because multiple functions in the call path have
487 * return probes installed on them, and/or more than one
488 * return probe was registered for a target function.
490 * We can handle this because:
491 * - instances are always pushed into the head of the list
492 * - when multiple return probes are registered for the same
493 * function, the (chronologically) first instance's ret_addr
494 * will be the real return address, and all the rest will
495 * point to kretprobe_trampoline.
497 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
498 if (ri
->task
!= current
)
499 /* another task is sharing our hash bucket */
502 orig_ret_address
= (unsigned long)ri
->ret_addr
;
504 if (orig_ret_address
!= trampoline_address
)
506 * This is the real return address. Any other
507 * instances associated with this task are for
508 * other calls deeper on the call stack
513 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
515 correct_ret_addr
= ri
->ret_addr
;
516 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
517 if (ri
->task
!= current
)
518 /* another task is sharing our hash bucket */
521 orig_ret_address
= (unsigned long)ri
->ret_addr
;
522 if (ri
->rp
&& ri
->rp
->handler
) {
523 __this_cpu_write(current_kprobe
, &ri
->rp
->kp
);
524 get_kprobe_ctlblk()->kprobe_status
= KPROBE_HIT_ACTIVE
;
525 ri
->ret_addr
= correct_ret_addr
;
526 ri
->rp
->handler(ri
, regs
);
527 __this_cpu_write(current_kprobe
, NULL
);
530 recycle_rp_inst(ri
, &empty_rp
);
532 if (orig_ret_address
!= trampoline_address
)
534 * This is the real return address. Any other
535 * instances associated with this task are for
536 * other calls deeper on the call stack
541 kretprobe_hash_unlock(current
, &flags
);
543 hlist_for_each_entry_safe(ri
, tmp
, &empty_rp
, hlist
) {
544 hlist_del(&ri
->hlist
);
547 return (void *)orig_ret_address
;
550 void __kprobes
arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
551 struct pt_regs
*regs
)
553 ri
->ret_addr
= (kprobe_opcode_t
*)regs
->regs
[30];
555 /* replace return addr (x30) with trampoline */
556 regs
->regs
[30] = (long)&kretprobe_trampoline
;
559 int __kprobes
arch_trampoline_kprobe(struct kprobe
*p
)
564 int __init
arch_init_kprobes(void)
566 register_kernel_break_hook(&kprobes_break_hook
);
567 register_kernel_step_hook(&kprobes_step_hook
);