^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) int arch_check_ftrace_location(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) if (ftrace_location((unsigned long)p->addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) p->flags |= KPROBE_FLAG_FTRACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* Ftrace callback handler for kprobes -- called under preepmt disabed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct ftrace_ops *ops, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) bool lr_saver = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct kprobe *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct kprobe_ctlblk *kcb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /* Preempt is disabled by ftrace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) p = get_kprobe((kprobe_opcode_t *)ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) p = get_kprobe((kprobe_opcode_t *)(ip - MCOUNT_INSN_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (unlikely(!p) || kprobe_disabled(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) lr_saver = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) kcb = get_kprobe_ctlblk();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (kprobe_running()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) kprobes_inc_nmissed_count(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long orig_ip = instruction_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (lr_saver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ip -= MCOUNT_INSN_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) instruction_pointer_set(regs, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __this_cpu_write(current_kprobe, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) kcb->kprobe_status = KPROBE_HIT_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!p->pre_handler || !p->pre_handler(p, regs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Emulate singlestep (and also recover regs->pc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * as if there is a nop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) instruction_pointer_set(regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) (unsigned long)p->addr + MCOUNT_INSN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (unlikely(p->post_handler)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) kcb->kprobe_status = KPROBE_HIT_SSDONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) p->post_handler(p, regs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) instruction_pointer_set(regs, orig_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * If pre_handler returns !0, it changes regs->pc. We have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * skip emulating post_handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) __this_cpu_write(current_kprobe, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) NOKPROBE_SYMBOL(kprobe_ftrace_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int arch_prepare_kprobe_ftrace(struct kprobe *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) p->ainsn.api.insn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }