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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  3)  * Dynamic Ftrace based Kprobes Optimization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  5)  * Copyright (C) Hitachi Ltd., 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  6)  * Copyright 2016 Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  7)  *		  IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  9) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* Ftrace callback handler for kprobes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) 			   struct ftrace_ops *ops, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) 	struct kprobe *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) 	struct kprobe_ctlblk *kcb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) 	p = get_kprobe((kprobe_opcode_t *)nip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) 	if (unlikely(!p) || kprobe_disabled(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 	kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) 	if (kprobe_running()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) 		kprobes_inc_nmissed_count(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) 		 * On powerpc, NIP is *before* this instruction for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) 		 * pre handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) 		regs->nip -= MCOUNT_INSN_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 		__this_cpu_write(current_kprobe, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 		if (!p->pre_handler || !p->pre_handler(p, regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 			 * Emulate singlestep (and also recover regs->nip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) 			 * as if there is a nop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) 			regs->nip += MCOUNT_INSN_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 			if (unlikely(p->post_handler)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 				kcb->kprobe_status = KPROBE_HIT_SSDONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 				p->post_handler(p, regs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 		 * If pre_handler returns !0, it changes regs->nip. We have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) 		 * skip emulating post_handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) 		__this_cpu_write(current_kprobe, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) NOKPROBE_SYMBOL(kprobe_ftrace_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int arch_prepare_kprobe_ftrace(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) 	p->ainsn.insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 	p->ainsn.boostable = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }