blob: 14d2b53ec322d86005d2b94ffa92c21e179d29df [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -07002/*
3 * Copyright (C) 2008 ARM Limited
4 * Copyright (C) 2014 Regents of the University of California
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -07005 */
6
7#include <linux/export.h>
8#include <linux/kallsyms.h>
9#include <linux/sched.h>
10#include <linux/sched/debug.h>
11#include <linux/sched/task_stack.h>
12#include <linux/stacktrace.h>
Alan Kaob785ec12018-02-13 13:13:21 +080013#include <linux/ftrace.h>
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070014
Kefeng Wang99c168f2020-11-13 14:42:21 +080015#include <asm/stacktrace.h>
16
Kefeng Wang23c10752021-03-17 23:08:38 +080017register unsigned long sp_in_global __asm__("sp");
Guenter Roeckaf2bdf82020-04-13 09:12:34 -070018
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070019#ifdef CONFIG_FRAME_POINTER
20
Mao Handbeb90b2019-08-29 14:57:00 +080021void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
Kefeng Wang5cb00802020-11-13 14:42:23 +080022 bool (*fn)(void *, unsigned long), void *arg)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070023{
24 unsigned long fp, sp, pc;
Changbin Du6a00ef42022-01-17 23:44:33 +080025 int level = 0;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070026
27 if (regs) {
Christoph Hellwig6ab77af2019-04-15 11:14:40 +020028 fp = frame_pointer(regs);
29 sp = user_stack_pointer(regs);
30 pc = instruction_pointer(regs);
Jisheng Zhang78d9d802021-07-16 21:40:51 +080031 } else if (task == NULL || task == current) {
Changbin Du6a00ef42022-01-17 23:44:33 +080032 fp = (unsigned long)__builtin_frame_address(0);
33 sp = sp_in_global;
34 pc = (unsigned long)walk_stackframe;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070035 } else {
36 /* task blocked in __switch_to */
37 fp = task->thread.s[0];
38 sp = task->thread.sp;
39 pc = task->thread.ra;
40 }
41
42 for (;;) {
43 unsigned long low, high;
44 struct stackframe *frame;
45
Changbin Du6a00ef42022-01-17 23:44:33 +080046 if (unlikely(!__kernel_text_address(pc) || (level++ >= 1 && !fn(arg, pc))))
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070047 break;
48
49 /* Validate frame pointer */
50 low = sp + sizeof(struct stackframe);
51 high = ALIGN(sp, THREAD_SIZE);
52 if (unlikely(fp < low || fp > high || fp & 0x7))
53 break;
54 /* Unwind stack frame */
55 frame = (struct stackframe *)fp - 1;
56 sp = fp;
Chen Huangf766f772021-01-11 20:40:14 +080057 if (regs && (regs->epc == pc) && (frame->fp & 0x7)) {
58 fp = frame->ra;
59 pc = regs->ra;
60 } else {
61 fp = frame->fp;
62 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
63 (unsigned long *)(fp - 8));
64 }
65
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070066 }
67}
68
69#else /* !CONFIG_FRAME_POINTER */
70
Kefeng Wang0502bee2020-05-11 10:19:53 +080071void notrace walk_stackframe(struct task_struct *task,
Kefeng Wang9dd97062020-11-13 14:42:22 +080072 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070073{
74 unsigned long sp, pc;
75 unsigned long *ksp;
76
77 if (regs) {
Christoph Hellwig6ab77af2019-04-15 11:14:40 +020078 sp = user_stack_pointer(regs);
79 pc = instruction_pointer(regs);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070080 } else if (task == NULL || task == current) {
Palmer Dabbelt52e7c522020-02-27 11:07:28 -080081 sp = sp_in_global;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070082 pc = (unsigned long)walk_stackframe;
83 } else {
84 /* task blocked in __switch_to */
85 sp = task->thread.sp;
86 pc = task->thread.ra;
87 }
88
89 if (unlikely(sp & 0x7))
90 return;
91
92 ksp = (unsigned long *)sp;
93 while (!kstack_end(ksp)) {
Kefeng Wang5cb00802020-11-13 14:42:23 +080094 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070095 break;
96 pc = (*ksp++) - 0x4;
97 }
98}
99
100#endif /* CONFIG_FRAME_POINTER */
101
Kefeng Wang5cb00802020-11-13 14:42:23 +0800102static bool print_trace_address(void *arg, unsigned long pc)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700103{
Dmitry Safonov0b3d4362020-06-08 21:31:17 -0700104 const char *loglvl = arg;
105
106 print_ip_sym(loglvl, pc);
Kefeng Wang5cb00802020-11-13 14:42:23 +0800107 return true;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700108}
109
Chen Huangeac2f302021-04-29 07:03:48 +0000110noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
Kefeng Wang091b9452021-01-11 20:40:12 +0800111 const char *loglvl)
112{
Kefeng Wang091b9452021-01-11 20:40:12 +0800113 walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
114}
115
Dmitry Safonov9cb8f062020-06-08 21:32:29 -0700116void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
Dmitry Safonov0b3d4362020-06-08 21:31:17 -0700117{
Chen Huangeac2f302021-04-29 07:03:48 +0000118 pr_cont("%sCall Trace:\n", loglvl);
Kefeng Wang091b9452021-01-11 20:40:12 +0800119 dump_backtrace(NULL, task, loglvl);
Dmitry Safonov0b3d4362020-06-08 21:31:17 -0700120}
121
Kefeng Wang9dd97062020-11-13 14:42:22 +0800122static bool save_wchan(void *arg, unsigned long pc)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700123{
124 if (!in_sched_functions(pc)) {
125 unsigned long *p = arg;
126 *p = pc;
Kefeng Wang5cb00802020-11-13 14:42:23 +0800127 return false;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700128 }
Kefeng Wang5cb00802020-11-13 14:42:23 +0800129 return true;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700130}
131
Kees Cook42a20f82021-09-29 15:02:14 -0700132unsigned long __get_wchan(struct task_struct *task)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700133{
134 unsigned long pc = 0;
135
Kees Cook42a20f82021-09-29 15:02:14 -0700136 if (!try_get_task_stack(task))
137 return 0;
138 walk_stackframe(task, NULL, save_wchan, &pc);
139 put_task_stack(task);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700140 return pc;
141}
142
Chen Huangeac2f302021-04-29 07:03:48 +0000143noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
Kefeng Wang5cb00802020-11-13 14:42:23 +0800144 struct task_struct *task, struct pt_regs *regs)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700145{
Kefeng Wang5cb00802020-11-13 14:42:23 +0800146 walk_stackframe(task, regs, consume_entry, cookie);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700147}