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