^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) * Performance counter callchain support - powerpc architecture code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2009 Paul Mackerras, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/sigcontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/ucontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/vdso.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/pte-walk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "callchain.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Is sp valid as the address of the next kernel stack frame after prev_sp?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * The next frame may be in a different stack area but should not go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * back down in the same stack area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int valid_next_sp(unsigned long sp, unsigned long prev_sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (sp & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return 0; /* must be 16-byte aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (sp >= prev_sp + STACK_FRAME_MIN_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * sp could decrease when we jump off an interrupt stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * back to the regular process stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if ((sp & ~(THREAD_SIZE - 1)) != (prev_sp & ~(THREAD_SIZE - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long sp, next_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned long next_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned long lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) long level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned long *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) lr = regs->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) sp = regs->gpr[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) perf_callchain_store(entry, perf_instruction_pointer(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) fp = (unsigned long *) sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) next_sp = fp[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (next_sp == sp + STACK_INT_FRAME_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * This looks like an interrupt frame for an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * interrupt that occurred in the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) next_ip = regs->nip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) lr = regs->link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) perf_callchain_store_context(entry, PERF_CONTEXT_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) next_ip = lr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) next_ip = fp[STACK_FRAME_LR_SAVE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * We can't tell which of the first two addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * we get are valid, but we can filter out the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * obviously bogus ones here. We replace them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * with 0 rather than removing them entirely so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * that userspace can tell which is which.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if ((level == 1 && next_ip == lr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) (level <= 1 && !kernel_text_address(next_ip)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) next_ip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ++level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) perf_callchain_store(entry, next_ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!valid_next_sp(next_sp, sp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sp = next_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!is_32bit_task())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) perf_callchain_user_64(entry, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) perf_callchain_user_32(entry, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }