blob: f298eb2ff6c2968d69a2a3af6f7acf302d4c4c3f [file] [log] [blame]
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +08001/*
2 * Linux performance counter support for MIPS.
3 *
4 * Copyright (C) 2010 MIPS Technologies, Inc.
5 * Author: Deng-Cheng Zhu
6 *
7 * This code is based on the implementation for ARM, which is in turn
8 * based on the sparc64 perf event code and the x86 code. Performance
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +08009 * counter access is based on the MIPS Oprofile code. And the callchain
10 * support references the code of MIPS stacktrace.c.
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080017#include <linux/perf_event.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010018#include <linux/sched/task_stack.h>
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080019
Deng-Cheng Zhu14f70012010-10-12 19:37:22 +080020#include <asm/stacktrace.h>
Deng-Cheng Zhu3a9ab992010-10-12 19:37:24 +080021
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080022/* Callchain handling code. */
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080023
24/*
25 * Leave userspace callchain empty for now. When we find a way to trace
David Daneye5dcb582011-09-24 02:29:55 +020026 * the user stack callchains, we will add it here.
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080027 */
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080028
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -030029static void save_raw_perf_callchain(struct perf_callchain_entry_ctx *entry,
30 unsigned long reg29)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080031{
32 unsigned long *sp = (unsigned long *)reg29;
33 unsigned long addr;
34
35 while (!kstack_end(sp)) {
36 addr = *sp++;
37 if (__kernel_text_address(addr)) {
Deng-Cheng Zhu98f92f22011-01-21 16:19:20 +080038 perf_callchain_store(entry, addr);
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -030039 if (entry->nr >= entry->max_stack)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080040 break;
41 }
42 }
43}
44
Arnaldo Carvalho de Melocfbcf462016-04-28 12:30:53 -030045void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
46 struct pt_regs *regs)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080047{
48 unsigned long sp = regs->regs[29];
49#ifdef CONFIG_KALLSYMS
50 unsigned long ra = regs->regs[31];
51 unsigned long pc = regs->cp0_epc;
52
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080053 if (raw_show_trace || !__kernel_text_address(pc)) {
54 unsigned long stack_page =
55 (unsigned long)task_stack_page(current);
56 if (stack_page && sp >= stack_page &&
57 sp <= stack_page + THREAD_SIZE - 32)
58 save_raw_perf_callchain(entry, sp);
59 return;
60 }
61 do {
Deng-Cheng Zhu98f92f22011-01-21 16:19:20 +080062 perf_callchain_store(entry, pc);
Arnaldo Carvalho de Melo3b1fff02016-05-10 18:08:32 -030063 if (entry->nr >= entry->max_stack)
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080064 break;
65 pc = unwind_stack(current, &sp, pc, &ra);
66 } while (pc);
67#else
Deng-Cheng Zhu7e788d92010-10-12 19:37:23 +080068 save_raw_perf_callchain(entry, sp);
69#endif
70}