^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * arch/arm64/kernel/probes/kprobes.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Kprobes support for ARM64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2013 Linaro Limited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/extable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/stop_machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/set_memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/stringify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/traps.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/debug-monitors.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/daifflags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/system_misc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/insn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "decode-insn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kprobe_opcode_t *addr = p->ainsn.api.insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void *addrs[] = {addr, addr + 1};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* prepare insn slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) aarch64_insn_patch_text(addrs, insns, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Needs restoring of return address after stepping xol.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) p->ainsn.api.restore = (unsigned long) p->addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) sizeof(kprobe_opcode_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void __kprobes arch_prepare_simulate(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* This instructions is not executed xol. No need to adjust the PC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) p->ainsn.api.restore = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (p->ainsn.api.handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* single step simulated, now go for post processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) post_kprobe_handler(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int __kprobes arch_prepare_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned long probe_addr = (unsigned long)p->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (probe_addr & 0x3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* copy instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) p->opcode = le32_to_cpu(*p->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (search_exception_tables(probe_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* decode instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case INSN_REJECTED: /* insn not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) case INSN_GOOD_NO_SLOT: /* insn need simulation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) p->ainsn.api.insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case INSN_GOOD: /* instruction uses slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) p->ainsn.api.insn = get_insn_slot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!p->ainsn.api.insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* prepare the instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (p->ainsn.api.insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) arch_prepare_ss_slot(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) arch_prepare_simulate(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) void *alloc_insn_page(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) NUMA_NO_NODE, __builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* arm kprobe: install breakpoint in text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) void __kprobes arch_arm_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void *addr = p->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u32 insn = BRK64_OPCODE_KPROBES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) aarch64_insn_patch_text(&addr, &insn, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* disarm kprobe: remove breakpoint from text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) void __kprobes arch_disarm_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void *addr = p->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) aarch64_insn_patch_text(&addr, &p->opcode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void __kprobes arch_remove_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (p->ainsn.api.insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) free_insn_slot(p->ainsn.api.insn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) p->ainsn.api.insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) kcb->prev_kprobe.kp = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) kcb->prev_kprobe.status = kcb->kprobe_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kcb->kprobe_status = kcb->prev_kprobe.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static void __kprobes set_current_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) __this_cpu_write(current_kprobe, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * Mask all of DAIF while executing the instruction out-of-line, to keep things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * simple and avoid nesting exceptions. Interrupts do have to be disabled since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * the kprobe state is per-CPU and doesn't get migrated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) kcb->saved_irqflag = regs->pstate & DAIF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) regs->pstate |= DAIF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) regs->pstate &= ~DAIF_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) regs->pstate |= kcb->saved_irqflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) kcb->ss_ctx.ss_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kcb->ss_ctx.match_addr = addr + sizeof(kprobe_opcode_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) kcb->ss_ctx.ss_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) kcb->ss_ctx.match_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static void __kprobes setup_singlestep(struct kprobe *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct kprobe_ctlblk *kcb, int reenter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) unsigned long slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (reenter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) save_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) set_current_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) kcb->kprobe_status = KPROBE_REENTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) kcb->kprobe_status = KPROBE_HIT_SS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (p->ainsn.api.insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* prepare for single stepping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) slot = (unsigned long)p->ainsn.api.insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) set_ss_context(kcb, slot); /* mark pending ss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) kprobes_save_local_irqflag(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) instruction_pointer_set(regs, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* insn simulation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) arch_simulate_insn(p, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static int __kprobes reenter_kprobe(struct kprobe *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) switch (kcb->kprobe_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) case KPROBE_HIT_SSDONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) case KPROBE_HIT_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) kprobes_inc_nmissed_count(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) setup_singlestep(p, regs, kcb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case KPROBE_HIT_SS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) case KPROBE_REENTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) pr_warn("Unrecoverable kprobe detected.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dump_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct kprobe *cur = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* return addr restore if non-branching insn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (cur->ainsn.api.restore != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) instruction_pointer_set(regs, cur->ainsn.api.restore);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* restore back original saved kprobe variables and continue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (kcb->kprobe_status == KPROBE_REENTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) restore_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* call post handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) kcb->kprobe_status = KPROBE_HIT_SSDONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (cur->post_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) cur->post_handler(cur, regs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct kprobe *cur = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) switch (kcb->kprobe_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case KPROBE_HIT_SS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case KPROBE_REENTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * We are here because the instruction being single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * stepped caused a page fault. We reset the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * kprobe and the ip points back to the probe address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * and allow the page fault handler to continue as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * normal page fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) instruction_pointer_set(regs, (unsigned long) cur->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!instruction_pointer(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (kcb->kprobe_status == KPROBE_REENTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) restore_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) kprobes_restore_local_irqflag(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) case KPROBE_HIT_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) case KPROBE_HIT_SSDONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * We increment the nmissed count for accounting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * we can also use npre/npostfault count for accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * these specific fault cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) kprobes_inc_nmissed_count(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * We come here because instructions in the pre/post
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * handler caused the page_fault, this could happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * if handler tries to access user space by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * copy_from_user(), get_user() etc. Let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * user-specified handler try to fix it first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (cur->fault_handler && cur->fault_handler(cur, regs, fsr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * In case the user-specified fault handler returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * zero, try to fix up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (fixup_exception(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static void __kprobes kprobe_handler(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct kprobe *p, *cur_kprobe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct kprobe_ctlblk *kcb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) unsigned long addr = instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) cur_kprobe = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) p = get_kprobe((kprobe_opcode_t *) addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (cur_kprobe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (reenter_kprobe(p, regs, kcb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* Probe hit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) set_current_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) kcb->kprobe_status = KPROBE_HIT_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * If we have no pre-handler or it returned 0, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * continue with normal processing. If we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * pre-handler and it returned non-zero, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * modify the execution path and no need to single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * stepping. Let's just reset current kprobe and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!p->pre_handler || !p->pre_handler(p, regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) setup_singlestep(p, regs, kcb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * The breakpoint instruction was removed right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * after we hit it. Another cpu has removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * either a probepoint or a debugger breakpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * at this address. In either case, no further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * handling of this interrupt is appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * Return back to original instruction, and continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static int __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) kprobe_ss_hit(struct kprobe_ctlblk *kcb, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if ((kcb->ss_ctx.ss_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) && (kcb->ss_ctx.match_addr == addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) clear_ss_context(kcb); /* clear pending ss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return DBG_HOOK_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* not ours, kprobes should ignore it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return DBG_HOOK_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static int __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned int esr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /* return error if this is not our step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) retval = kprobe_ss_hit(kcb, instruction_pointer(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (retval == DBG_HOOK_HANDLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) kprobes_restore_local_irqflag(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) post_kprobe_handler(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static struct break_hook kprobes_break_ss_hook = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .imm = KPROBES_BRK_SS_IMM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .fn = kprobe_breakpoint_ss_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static int __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) kprobe_handler(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return DBG_HOOK_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static struct break_hook kprobes_break_hook = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .imm = KPROBES_BRK_IMM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .fn = kprobe_breakpoint_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) int __init arch_populate_kprobe_blacklist(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) (unsigned long)__entry_text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) (unsigned long)__irqentry_text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) (unsigned long)__idmap_text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) (unsigned long)__hyp_text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (ret || is_kernel_in_hyp_mode())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) (unsigned long)__hyp_idmap_text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) (void *)kernel_stack_pointer(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) ri->fp = (void *)kernel_stack_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /* replace return addr (x30) with trampoline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) regs->regs[30] = (long)&kretprobe_trampoline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) int __kprobes arch_trampoline_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) int __init arch_init_kprobes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) register_kernel_break_hook(&kprobes_break_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) register_kernel_break_hook(&kprobes_break_ss_hook);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }