^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) * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kdebug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/disasm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define MIN_STACK_SIZE(addr) min((unsigned long)MAX_STACK_SIZE, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) (unsigned long)current_thread_info() + THREAD_SIZE - (addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int __kprobes arch_prepare_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Attempt to probe at unaligned address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if ((unsigned long)p->addr & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* Address should not be in exception handling code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) p->ainsn.is_short = is_short_instr((unsigned long)p->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) p->opcode = *p->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) void __kprobes arch_arm_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *p->addr = UNIMP_S_INSTRUCTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) flush_icache_range((unsigned long)p->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) (unsigned long)p->addr + sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void __kprobes arch_disarm_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *p->addr = p->opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) flush_icache_range((unsigned long)p->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) (unsigned long)p->addr + sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void __kprobes arch_remove_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) arch_disarm_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Can we remove the kprobe in the middle of kprobe handling? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (p->ainsn.t1_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *(p->ainsn.t1_addr) = p->ainsn.t1_opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) flush_icache_range((unsigned long)p->ainsn.t1_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) (unsigned long)p->ainsn.t1_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) p->ainsn.t1_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (p->ainsn.t2_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *(p->ainsn.t2_addr) = p->ainsn.t2_opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) flush_icache_range((unsigned long)p->ainsn.t2_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) (unsigned long)p->ainsn.t2_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) p->ainsn.t2_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kcb->prev_kprobe.kp = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) kcb->prev_kprobe.status = kcb->kprobe_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) kcb->kprobe_status = kcb->prev_kprobe.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static inline void __kprobes set_current_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __this_cpu_write(current_kprobe, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void __kprobes resume_execution(struct kprobe *p, unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Remove the trap instructions inserted for single step and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * restore the original instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (p->ainsn.t1_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *(p->ainsn.t1_addr) = p->ainsn.t1_opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) flush_icache_range((unsigned long)p->ainsn.t1_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) (unsigned long)p->ainsn.t1_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) p->ainsn.t1_addr = NULL;
^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) if (p->ainsn.t2_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *(p->ainsn.t2_addr) = p->ainsn.t2_opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) flush_icache_range((unsigned long)p->ainsn.t2_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) (unsigned long)p->ainsn.t2_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) p->ainsn.t2_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned long next_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) unsigned long tgt_if_br = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int is_branch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned long bta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Copy the opcode back to the kprobe location and execute the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * instruction. Because of this we will not be able to get into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * same kprobe until this kprobe is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *(p->addr) = p->opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) flush_icache_range((unsigned long)p->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) (unsigned long)p->addr + sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* Now we insert the trap at the next location after this instruction to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * single step. If it is a branch we insert the trap at possible branch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * targets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) bta = regs->bta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (regs->status32 & 0x40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* We are in a delay slot with the branch taken */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) next_pc = bta & ~0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!p->ainsn.is_short) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (bta & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) regs->blink += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Branch not taken */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) next_pc += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* next pc is taken from bta after executing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * delay slot instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) regs->bta += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) is_branch = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) is_branch =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) disasm_next_pc((unsigned long)p->addr, regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) (struct callee_regs *) current->thread.callee_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) &next_pc, &tgt_if_br);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) p->ainsn.t1_addr = (kprobe_opcode_t *) next_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) p->ainsn.t1_opcode = *(p->ainsn.t1_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *(p->ainsn.t1_addr) = TRAP_S_2_INSTRUCTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) flush_icache_range((unsigned long)p->ainsn.t1_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) (unsigned long)p->ainsn.t1_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (is_branch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) p->ainsn.t2_addr = (kprobe_opcode_t *) tgt_if_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) p->ainsn.t2_opcode = *(p->ainsn.t2_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *(p->ainsn.t2_addr) = TRAP_S_2_INSTRUCTION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) flush_icache_range((unsigned long)p->ainsn.t2_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) (unsigned long)p->ainsn.t2_addr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) sizeof(kprobe_opcode_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^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) int __kprobes arc_kprobe_handler(unsigned long addr, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct kprobe *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct kprobe_ctlblk *kcb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) p = get_kprobe((unsigned long *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * We have reentered the kprobe_handler, since another kprobe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * was hit while within the handler, we save the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * kprobes and single step on the instruction of the new probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * without calling any user handlers to avoid recursive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * kprobes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (kprobe_running()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) save_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) set_current_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) kprobes_inc_nmissed_count(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) setup_singlestep(p, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) kcb->kprobe_status = KPROBE_REENTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return 1;
^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) set_current_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kcb->kprobe_status = KPROBE_HIT_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* If we have no pre-handler or it returned 0, we continue with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * normal processing. If we have a pre-handler and it returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * non-zero - which means user handler setup registers to exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * to another instruction, we must skip the single stepping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!p->pre_handler || !p->pre_handler(p, regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) setup_singlestep(p, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) kcb->kprobe_status = KPROBE_HIT_SS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) preempt_enable_no_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* no_kprobe: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) preempt_enable_no_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int __kprobes arc_post_kprobe_handler(unsigned long addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct kprobe *cur = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^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 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) resume_execution(cur, addr, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* Rearm the kprobe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) arch_arm_kprobe(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * When we return from trap instruction we go to the next instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * We restored the actual instruction in resume_exectuiont and we to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * return to the same address and execute it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) regs->ret = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) kcb->kprobe_status = KPROBE_HIT_SSDONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) cur->post_handler(cur, regs, 0);
^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) if (kcb->kprobe_status == KPROBE_REENTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) restore_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) preempt_enable_no_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * Fault can be for the instruction being single stepped or for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * pre/post handlers in the module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * This is applicable for applications like user probes, where we have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * probe in user space and the handlers in the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct kprobe *cur = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) switch (kcb->kprobe_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) case KPROBE_HIT_SS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) case KPROBE_REENTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * We are here because the instruction being single stepped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * caused the fault. We reset the current kprobe and allow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * exception handler as if it is regular exception. In our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * case it doesn't matter because the system will be halted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) resume_execution(cur, (unsigned long)cur->addr, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (kcb->kprobe_status == KPROBE_REENTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) restore_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) preempt_enable_no_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) case KPROBE_HIT_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) case KPROBE_HIT_SSDONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * We are here because the instructions in the pre/post handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * caused the fault.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* We increment the nmissed count for accounting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * we can also use npre/npostfault count for accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * these specific fault cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) kprobes_inc_nmissed_count(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * We come here because instructions in the pre/post
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * handler caused the page_fault, this could happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * if handler tries to access user space by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * copy_from_user(), get_user() etc. Let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * user-specified handler try to fix it first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * In case the user-specified fault handler returned zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * try to fix up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (fixup_exception(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * fixup_exception() could not handle it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Let do_page_fault() fix it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) unsigned long val, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct die_args *args = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) unsigned long addr = args->err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) int ret = NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) case DIE_IERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (arc_kprobe_handler(addr, args->regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return NOTIFY_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) case DIE_TRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (arc_post_kprobe_handler(addr, args->regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return NOTIFY_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static void __used kretprobe_trampoline_holder(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) __asm__ __volatile__(".global kretprobe_trampoline\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) "kretprobe_trampoline:\n" "nop\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) ri->ret_addr = (kprobe_opcode_t *) regs->blink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ri->fp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* Replace the return addr with trampoline addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) regs->blink = (unsigned long)&kretprobe_trampoline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int __kprobes trampoline_probe_handler(struct kprobe *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) regs->ret = __kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* By returning a non zero value, we are telling the kprobe handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * that we don't want the post_handler to run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static struct kprobe trampoline_p = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .pre_handler = trampoline_probe_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int __init arch_init_kprobes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /* Registering the trampoline code for the kret probe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return register_kprobe(&trampoline_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) int __kprobes arch_trampoline_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (p->addr == (kprobe_opcode_t *) &kretprobe_trampoline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) void trap_is_kprobe(unsigned long address, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) notify_die(DIE_TRAP, "kprobe_trap", regs, address, 0, SIGTRAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }