blob: 76dadf6d396bf4a8ead435df348f36376126b910 [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
Guenter Roeckaf2bdf82020-04-13 09:12:34 -070017register unsigned long sp_in_global __asm__("sp");
18
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;
25
26 if (regs) {
Christoph Hellwig6ab77af2019-04-15 11:14:40 +020027 fp = frame_pointer(regs);
28 sp = user_stack_pointer(regs);
29 pc = instruction_pointer(regs);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070030 } else if (task == NULL || task == current) {
Palmer Dabbelt52e7c522020-02-27 11:07:28 -080031 const register unsigned long current_sp = sp_in_global;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070032 fp = (unsigned long)__builtin_frame_address(0);
33 sp = current_sp;
34 pc = (unsigned long)walk_stackframe;
35 } 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
Kefeng Wang5cb00802020-11-13 14:42:23 +080046 if (unlikely(!__kernel_text_address(pc) || !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;
57 fp = frame->fp;
Alan Kaob785ec12018-02-13 13:13:21 +080058 pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
59 (unsigned long *)(fp - 8));
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070060 }
61}
62
63#else /* !CONFIG_FRAME_POINTER */
64
Kefeng Wang0502bee2020-05-11 10:19:53 +080065void notrace walk_stackframe(struct task_struct *task,
Kefeng Wang9dd97062020-11-13 14:42:22 +080066 struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070067{
68 unsigned long sp, pc;
69 unsigned long *ksp;
70
71 if (regs) {
Christoph Hellwig6ab77af2019-04-15 11:14:40 +020072 sp = user_stack_pointer(regs);
73 pc = instruction_pointer(regs);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070074 } else if (task == NULL || task == current) {
Palmer Dabbelt52e7c522020-02-27 11:07:28 -080075 sp = sp_in_global;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070076 pc = (unsigned long)walk_stackframe;
77 } else {
78 /* task blocked in __switch_to */
79 sp = task->thread.sp;
80 pc = task->thread.ra;
81 }
82
83 if (unlikely(sp & 0x7))
84 return;
85
86 ksp = (unsigned long *)sp;
87 while (!kstack_end(ksp)) {
Kefeng Wang5cb00802020-11-13 14:42:23 +080088 if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070089 break;
90 pc = (*ksp++) - 0x4;
91 }
92}
93
94#endif /* CONFIG_FRAME_POINTER */
95
Kefeng Wang5cb00802020-11-13 14:42:23 +080096static bool print_trace_address(void *arg, unsigned long pc)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -070097{
Dmitry Safonov0b3d4362020-06-08 21:31:17 -070098 const char *loglvl = arg;
99
100 print_ip_sym(loglvl, pc);
Kefeng Wang5cb00802020-11-13 14:42:23 +0800101 return true;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700102}
103
Kefeng Wang091b9452021-01-11 20:40:12 +0800104void dump_backtrace(struct pt_regs *regs, struct task_struct *task,
105 const char *loglvl)
106{
107 pr_cont("%sCall Trace:\n", loglvl);
108 walk_stackframe(task, regs, print_trace_address, (void *)loglvl);
109}
110
Dmitry Safonov9cb8f062020-06-08 21:32:29 -0700111void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
Dmitry Safonov0b3d4362020-06-08 21:31:17 -0700112{
Kefeng Wang091b9452021-01-11 20:40:12 +0800113 dump_backtrace(NULL, task, loglvl);
Dmitry Safonov0b3d4362020-06-08 21:31:17 -0700114}
115
Kefeng Wang9dd97062020-11-13 14:42:22 +0800116static bool save_wchan(void *arg, unsigned long pc)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700117{
118 if (!in_sched_functions(pc)) {
119 unsigned long *p = arg;
120 *p = pc;
Kefeng Wang5cb00802020-11-13 14:42:23 +0800121 return false;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700122 }
Kefeng Wang5cb00802020-11-13 14:42:23 +0800123 return true;
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700124}
125
126unsigned long get_wchan(struct task_struct *task)
127{
128 unsigned long pc = 0;
129
130 if (likely(task && task != current && task->state != TASK_RUNNING))
131 walk_stackframe(task, NULL, save_wchan, &pc);
132 return pc;
133}
134
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700135#ifdef CONFIG_STACKTRACE
136
Kefeng Wang5cb00802020-11-13 14:42:23 +0800137void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
138 struct task_struct *task, struct pt_regs *regs)
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700139{
Kefeng Wang5cb00802020-11-13 14:42:23 +0800140 walk_stackframe(task, regs, consume_entry, cookie);
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700141}
142
Palmer Dabbelt5d8544e2017-07-10 18:03:19 -0700143#endif /* CONFIG_STACKTRACE */