Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Performance counter callchain support - powerpc architecture code
0004  *
0005  * Copyright © 2009 Paul Mackerras, IBM Corporation.
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/sched.h>
0009 #include <linux/perf_event.h>
0010 #include <linux/percpu.h>
0011 #include <linux/uaccess.h>
0012 #include <linux/mm.h>
0013 #include <asm/ptrace.h>
0014 #include <asm/sigcontext.h>
0015 #include <asm/ucontext.h>
0016 #include <asm/vdso.h>
0017 #include <asm/pte-walk.h>
0018 
0019 #include "callchain.h"
0020 
0021 /*
0022  * Is sp valid as the address of the next kernel stack frame after prev_sp?
0023  * The next frame may be in a different stack area but should not go
0024  * back down in the same stack area.
0025  */
0026 static int valid_next_sp(unsigned long sp, unsigned long prev_sp)
0027 {
0028     if (sp & 0xf)
0029         return 0;       /* must be 16-byte aligned */
0030     if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
0031         return 0;
0032     if (sp >= prev_sp + STACK_FRAME_MIN_SIZE)
0033         return 1;
0034     /*
0035      * sp could decrease when we jump off an interrupt stack
0036      * back to the regular process stack.
0037      */
0038     if ((sp & ~(THREAD_SIZE - 1)) != (prev_sp & ~(THREAD_SIZE - 1)))
0039         return 1;
0040     return 0;
0041 }
0042 
0043 void __no_sanitize_address
0044 perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
0045 {
0046     unsigned long sp, next_sp;
0047     unsigned long next_ip;
0048     unsigned long lr;
0049     long level = 0;
0050     unsigned long *fp;
0051 
0052     lr = regs->link;
0053     sp = regs->gpr[1];
0054     perf_callchain_store(entry, perf_instruction_pointer(regs));
0055 
0056     if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD))
0057         return;
0058 
0059     for (;;) {
0060         fp = (unsigned long *) sp;
0061         next_sp = fp[0];
0062 
0063         if (next_sp == sp + STACK_INT_FRAME_SIZE &&
0064             fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
0065             /*
0066              * This looks like an interrupt frame for an
0067              * interrupt that occurred in the kernel
0068              */
0069             regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD);
0070             next_ip = regs->nip;
0071             lr = regs->link;
0072             level = 0;
0073             perf_callchain_store_context(entry, PERF_CONTEXT_KERNEL);
0074 
0075         } else {
0076             if (level == 0)
0077                 next_ip = lr;
0078             else
0079                 next_ip = fp[STACK_FRAME_LR_SAVE];
0080 
0081             /*
0082              * We can't tell which of the first two addresses
0083              * we get are valid, but we can filter out the
0084              * obviously bogus ones here.  We replace them
0085              * with 0 rather than removing them entirely so
0086              * that userspace can tell which is which.
0087              */
0088             if ((level == 1 && next_ip == lr) ||
0089                 (level <= 1 && !kernel_text_address(next_ip)))
0090                 next_ip = 0;
0091 
0092             ++level;
0093         }
0094 
0095         perf_callchain_store(entry, next_ip);
0096         if (!valid_next_sp(next_sp, sp))
0097             return;
0098         sp = next_sp;
0099     }
0100 }
0101 
0102 void
0103 perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
0104 {
0105     if (!is_32bit_task())
0106         perf_callchain_user_64(entry, regs);
0107     else
0108         perf_callchain_user_32(entry, regs);
0109 }