blob: 5d7a9c03903b5057059af712cd8fe86ac498348a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +08002/*
3 * Linux performance counter support for MIPS.
4 *
5 * Copyright (C) 2010 MIPS Technologies, Inc.
6 * Author: Deng-Cheng Zhu
7 *
8 * This code is based on the implementation for ARM, which is in turn
9 * based on the sparc64 perf event code and the x86 code. Performance
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080010 * counter access is based on the MIPS Oprofile code. And the callchain
11 * support references the code of MIPS stacktrace.c.
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080012 */
13
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080014#include <linux/perf_event.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010015#include <linux/sched/task_stack.h>
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080016
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080017#include <asm/stacktrace.h>
Deng-Cheng Zhu3a9ab992010-10-12 19:37:24 +080018
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080019/* Callchain handling code. */
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080020
21/*
22 * Leave userspace callchain empty for now. When we find a way to trace
David Daneye5dcb582011-09-24 02:29:55 +020023 * the user stack callchains, we will add it here.
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080024 */
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080025
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -030026static void save_raw_perf_callchain(struct perf_callchain_entry_ctx *entry,
27 unsigned long reg29)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080028{
29 unsigned long *sp = (unsigned long *)reg29;
30 unsigned long addr;
31
32 while (!kstack_end(sp)) {
33 addr = *sp++;
34 if (__kernel_text_address(addr)) {
Deng-Cheng Zhu98f92f22011-01-21 16:19:20 +080035 perf_callchain_store(entry, addr);
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -030036 if (entry->nr >= entry->max_stack)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080037 break;
38 }
39 }
40}
41
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -030042void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
43 struct pt_regs *regs)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080044{
45 unsigned long sp = regs->regs[29];
46#ifdef CONFIG_KALLSYMS
47 unsigned long ra = regs->regs[31];
48 unsigned long pc = regs->cp0_epc;
49
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080050 if (raw_show_trace || !__kernel_text_address(pc)) {
51 unsigned long stack_page =
52 (unsigned long)task_stack_page(current);
53 if (stack_page && sp >= stack_page &&
54 sp <= stack_page + THREAD_SIZE - 32)
55 save_raw_perf_callchain(entry, sp);
56 return;
57 }
58 do {
Deng-Cheng Zhu98f92f22011-01-21 16:19:20 +080059 perf_callchain_store(entry, pc);
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -030060 if (entry->nr >= entry->max_stack)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080061 break;
62 pc = unwind_stack(current, &sp, pc, &ra);
63 } while (pc);
64#else
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080065 save_raw_perf_callchain(entry, sp);
66#endif
67}