Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/extable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/stop_machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "decode-insn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static void __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct csky_insn_patch {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	kprobe_opcode_t	*addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	u32		opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	atomic_t	cpu_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int __kprobes patch_text_cb(void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct csky_insn_patch *param = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	unsigned int addr = (unsigned int)param->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (atomic_inc_return(&param->cpu_count) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		*(u16 *) addr = cpu_to_le16(param->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		dcache_wb_range(addr, addr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		atomic_inc(&param->cpu_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		while (atomic_read(&param->cpu_count) <= num_online_cpus())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 			cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	icache_inv_range(addr, addr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return 0;
^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) static int __kprobes patch_text(kprobe_opcode_t *addr, u32 opcode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct csky_insn_patch param = { addr, opcode, ATOMIC_INIT(0) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return stop_machine_cpuslocked(patch_text_cb, &param, cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	unsigned long offset = is_insn32(p->opcode) ? 4 : 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	p->ainsn.api.restore = (unsigned long)p->addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	patch_text(p->ainsn.api.insn, p->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static void __kprobes arch_prepare_simulate(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	p->ainsn.api.restore = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (p->ainsn.api.handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	post_kprobe_handler(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) int __kprobes arch_prepare_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unsigned long probe_addr = (unsigned long)p->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (probe_addr & 0x1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		pr_warn("Address not aligned.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -EINVAL;
^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) 	/* copy instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	p->opcode = le32_to_cpu(*p->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* decode instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	switch (csky_probe_decode_insn(p->addr, &p->ainsn.api)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	case INSN_REJECTED:	/* insn not supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	case INSN_GOOD_NO_SLOT:	/* insn need simulation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		p->ainsn.api.insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	case INSN_GOOD:	/* instruction uses slot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		p->ainsn.api.insn = get_insn_slot();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (!p->ainsn.api.insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	/* prepare the instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (p->ainsn.api.insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		arch_prepare_ss_slot(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		arch_prepare_simulate(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* install breakpoint in text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void __kprobes arch_arm_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	patch_text(p->addr, USR_BKPT);
^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) /* remove breakpoint from text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) void __kprobes arch_disarm_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	patch_text(p->addr, p->opcode);
^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) void __kprobes arch_remove_kprobe(struct kprobe *p)
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	kcb->prev_kprobe.kp = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	kcb->prev_kprobe.status = kcb->kprobe_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	kcb->kprobe_status = kcb->prev_kprobe.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void __kprobes set_current_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	__this_cpu_write(current_kprobe, p);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * Interrupts need to be disabled before single-step mode is set, and not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * reenabled until after single-step mode ends.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * Without disabling interrupt on local CPU, there is a chance of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * interrupt occurrence in the period of exception return and  start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * out-of-line single-step, that result in wrongly single stepping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * into the interrupt handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 						struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	kcb->saved_sr = regs->sr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	regs->sr &= ~BIT(6);
^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) static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 						struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	regs->sr = kcb->saved_sr;
^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) static void __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	unsigned long offset = is_insn32(p->opcode) ? 4 : 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	kcb->ss_ctx.ss_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	kcb->ss_ctx.match_addr = addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	kcb->ss_ctx.ss_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	kcb->ss_ctx.match_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) #define TRACE_MODE_SI		BIT(14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #define TRACE_MODE_MASK		~(0x3 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #define TRACE_MODE_RUN		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void __kprobes setup_singlestep(struct kprobe *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				       struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				       struct kprobe_ctlblk *kcb, int reenter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	unsigned long slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (reenter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		save_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		set_current_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		kcb->kprobe_status = KPROBE_REENTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		kcb->kprobe_status = KPROBE_HIT_SS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (p->ainsn.api.insn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		/* prepare for single stepping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		slot = (unsigned long)p->ainsn.api.insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		set_ss_context(kcb, slot, p);	/* mark pending ss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		/* IRQs and single stepping do not mix well. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		kprobes_save_local_irqflag(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		instruction_pointer_set(regs, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		/* insn simulation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		arch_simulate_insn(p, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int __kprobes reenter_kprobe(struct kprobe *p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				    struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				    struct kprobe_ctlblk *kcb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	switch (kcb->kprobe_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	case KPROBE_HIT_SSDONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	case KPROBE_HIT_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		kprobes_inc_nmissed_count(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		setup_singlestep(p, regs, kcb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	case KPROBE_HIT_SS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	case KPROBE_REENTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		pr_warn("Unrecoverable kprobe detected.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		dump_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static void __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct kprobe *cur = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (!cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	/* return addr restore if non-branching insn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (cur->ainsn.api.restore != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		regs->pc = cur->ainsn.api.restore;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	/* restore back original saved kprobe variables and continue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (kcb->kprobe_status == KPROBE_REENTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		restore_previous_kprobe(kcb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	/* call post handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	kcb->kprobe_status = KPROBE_HIT_SSDONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (cur->post_handler)	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		/* post_handler can hit breakpoint and single step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * again, so we enable D-flag for recursive exception.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		cur->post_handler(cur, regs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	}
^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 trapnr)
^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) 		regs->pc = (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) 			reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	case KPROBE_HIT_ACTIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	case KPROBE_HIT_SSDONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		 * We increment the nmissed count for accounting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		 * we can also use npre/npostfault count for accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		 * these specific fault cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		kprobes_inc_nmissed_count(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		 * We come here because instructions in the pre/post
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		 * handler caused the page_fault, this could happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 * if handler tries to access user space by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		 * copy_from_user(), get_user() etc. Let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		 * user-specified handler try to fix it first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		 * In case the user-specified fault handler returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		 * zero, try to fix up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (fixup_exception(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) kprobe_breakpoint_handler(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct kprobe *p, *cur_kprobe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct kprobe_ctlblk *kcb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	unsigned long addr = instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	cur_kprobe = kprobe_running();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	p = get_kprobe((kprobe_opcode_t *) addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (cur_kprobe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			if (reenter_kprobe(p, regs, kcb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			/* Probe hit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			set_current_kprobe(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			 * If we have no pre-handler or it returned 0, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			 * continue with normal processing.  If we have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 			 * pre-handler and it returned non-zero, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			 * modify the execution path and no need to single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			 * stepping. Let's just reset current kprobe and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			 * pre_handler can hit a breakpoint and can step thru
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			 * before return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			if (!p->pre_handler || !p->pre_handler(p, regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				setup_singlestep(p, regs, kcb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				reset_current_kprobe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	 * The breakpoint instruction was removed right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	 * after we hit it.  Another cpu has removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * either a probepoint or a debugger breakpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * at this address.  In either case, no further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 * handling of this interrupt is appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	 * Return back to original instruction, and continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int __kprobes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) kprobe_single_step_handler(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if ((kcb->ss_ctx.ss_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	    && (kcb->ss_ctx.match_addr == instruction_pointer(regs))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		clear_ss_context(kcb);	/* clear pending ss */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		kprobes_restore_local_irqflag(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		post_kprobe_handler(kcb, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int __init arch_populate_kprobe_blacklist(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 					(unsigned long)__irqentry_text_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
^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) void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 				      struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	ri->ret_addr = (kprobe_opcode_t *)regs->lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	ri->fp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	regs->lr = (unsigned long) &kretprobe_trampoline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int __kprobes arch_trampoline_kprobe(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) int __init arch_init_kprobes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }