18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Performance counter callchain support - powerpc architecture code 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright © 2009 Paul Mackerras, IBM Corporation. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/kernel.h> 88c2ecf20Sopenharmony_ci#include <linux/sched.h> 98c2ecf20Sopenharmony_ci#include <linux/perf_event.h> 108c2ecf20Sopenharmony_ci#include <linux/percpu.h> 118c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 128c2ecf20Sopenharmony_ci#include <linux/mm.h> 138c2ecf20Sopenharmony_ci#include <asm/ptrace.h> 148c2ecf20Sopenharmony_ci#include <asm/sigcontext.h> 158c2ecf20Sopenharmony_ci#include <asm/ucontext.h> 168c2ecf20Sopenharmony_ci#include <asm/vdso.h> 178c2ecf20Sopenharmony_ci#include <asm/pte-walk.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include "callchain.h" 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * Is sp valid as the address of the next kernel stack frame after prev_sp? 238c2ecf20Sopenharmony_ci * The next frame may be in a different stack area but should not go 248c2ecf20Sopenharmony_ci * back down in the same stack area. 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_cistatic int valid_next_sp(unsigned long sp, unsigned long prev_sp) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci if (sp & 0xf) 298c2ecf20Sopenharmony_ci return 0; /* must be 16-byte aligned */ 308c2ecf20Sopenharmony_ci if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD)) 318c2ecf20Sopenharmony_ci return 0; 328c2ecf20Sopenharmony_ci if (sp >= prev_sp + STACK_FRAME_MIN_SIZE) 338c2ecf20Sopenharmony_ci return 1; 348c2ecf20Sopenharmony_ci /* 358c2ecf20Sopenharmony_ci * sp could decrease when we jump off an interrupt stack 368c2ecf20Sopenharmony_ci * back to the regular process stack. 378c2ecf20Sopenharmony_ci */ 388c2ecf20Sopenharmony_ci if ((sp & ~(THREAD_SIZE - 1)) != (prev_sp & ~(THREAD_SIZE - 1))) 398c2ecf20Sopenharmony_ci return 1; 408c2ecf20Sopenharmony_ci return 0; 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_civoid 448c2ecf20Sopenharmony_ciperf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci unsigned long sp, next_sp; 478c2ecf20Sopenharmony_ci unsigned long next_ip; 488c2ecf20Sopenharmony_ci unsigned long lr; 498c2ecf20Sopenharmony_ci long level = 0; 508c2ecf20Sopenharmony_ci unsigned long *fp; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci lr = regs->link; 538c2ecf20Sopenharmony_ci sp = regs->gpr[1]; 548c2ecf20Sopenharmony_ci perf_callchain_store(entry, perf_instruction_pointer(regs)); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD)) 578c2ecf20Sopenharmony_ci return; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci for (;;) { 608c2ecf20Sopenharmony_ci fp = (unsigned long *) sp; 618c2ecf20Sopenharmony_ci next_sp = fp[0]; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (next_sp == sp + STACK_INT_FRAME_SIZE && 648c2ecf20Sopenharmony_ci validate_sp(sp, current, STACK_INT_FRAME_SIZE) && 658c2ecf20Sopenharmony_ci fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) { 668c2ecf20Sopenharmony_ci /* 678c2ecf20Sopenharmony_ci * This looks like an interrupt frame for an 688c2ecf20Sopenharmony_ci * interrupt that occurred in the kernel 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_ci regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD); 718c2ecf20Sopenharmony_ci next_ip = regs->nip; 728c2ecf20Sopenharmony_ci lr = regs->link; 738c2ecf20Sopenharmony_ci level = 0; 748c2ecf20Sopenharmony_ci perf_callchain_store_context(entry, PERF_CONTEXT_KERNEL); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci } else { 778c2ecf20Sopenharmony_ci if (level == 0) 788c2ecf20Sopenharmony_ci next_ip = lr; 798c2ecf20Sopenharmony_ci else 808c2ecf20Sopenharmony_ci next_ip = fp[STACK_FRAME_LR_SAVE]; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci /* 838c2ecf20Sopenharmony_ci * We can't tell which of the first two addresses 848c2ecf20Sopenharmony_ci * we get are valid, but we can filter out the 858c2ecf20Sopenharmony_ci * obviously bogus ones here. We replace them 868c2ecf20Sopenharmony_ci * with 0 rather than removing them entirely so 878c2ecf20Sopenharmony_ci * that userspace can tell which is which. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ci if ((level == 1 && next_ip == lr) || 908c2ecf20Sopenharmony_ci (level <= 1 && !kernel_text_address(next_ip))) 918c2ecf20Sopenharmony_ci next_ip = 0; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci ++level; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci perf_callchain_store(entry, next_ip); 978c2ecf20Sopenharmony_ci if (!valid_next_sp(next_sp, sp)) 988c2ecf20Sopenharmony_ci return; 998c2ecf20Sopenharmony_ci sp = next_sp; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_civoid 1048c2ecf20Sopenharmony_ciperf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci if (!is_32bit_task()) 1078c2ecf20Sopenharmony_ci perf_callchain_user_64(entry, regs); 1088c2ecf20Sopenharmony_ci else 1098c2ecf20Sopenharmony_ci perf_callchain_user_32(entry, regs); 1108c2ecf20Sopenharmony_ci} 111