blob: 2d65cfa5e0b4bbdcd471d7d2b4db5e13542d5a26 [file] [log] [blame]
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +02001/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 */
5#include <linux/kallsyms.h>
6#include <linux/kprobes.h>
7#include <linux/uaccess.h>
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +02008#include <linux/hardirq.h>
9#include <linux/kdebug.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040010#include <linux/export.h>
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020011#include <linux/ptrace.h>
12#include <linux/kexec.h>
Ingo Molnarb8030902009-11-26 08:17:31 +010013#include <linux/sysfs.h>
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +020014#include <linux/bug.h>
15#include <linux/nmi.h>
16
17#include <asm/stacktrace.h>
18
Josh Poimboeufcb76c932016-09-14 21:07:42 -050019void stack_type_str(enum stack_type type, const char **begin, const char **end)
Steven Rostedt198d2082014-02-06 09:41:31 -050020{
Josh Poimboeufcb76c932016-09-14 21:07:42 -050021 switch (type) {
22 case STACK_TYPE_IRQ:
23 case STACK_TYPE_SOFTIRQ:
24 *begin = "IRQ";
25 *end = "EOI";
26 break;
27 default:
28 *begin = NULL;
29 *end = NULL;
30 }
Steven Rostedt198d2082014-02-06 09:41:31 -050031}
32
Josh Poimboeufcb76c932016-09-14 21:07:42 -050033static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
Steven Rostedt198d2082014-02-06 09:41:31 -050034{
Josh Poimboeufcb76c932016-09-14 21:07:42 -050035 unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack);
36 unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
Steven Rostedt198d2082014-02-06 09:41:31 -050037
Josh Poimboeuf5fe599e2016-09-14 21:07:43 -050038 /*
39 * This is a software stack, so 'end' can be a valid stack pointer.
40 * It just means the stack is empty.
41 */
42 if (stack < begin || stack > end)
Josh Poimboeufcb76c932016-09-14 21:07:42 -050043 return false;
44
45 info->type = STACK_TYPE_IRQ;
46 info->begin = begin;
47 info->end = end;
48
49 /*
50 * See irq_32.c -- the next stack pointer is stored at the beginning of
51 * the stack.
52 */
53 info->next_sp = (unsigned long *)*begin;
54
55 return true;
Steven Rostedt198d2082014-02-06 09:41:31 -050056}
57
Josh Poimboeufcb76c932016-09-14 21:07:42 -050058static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
Steven Rostedt198d2082014-02-06 09:41:31 -050059{
Josh Poimboeufcb76c932016-09-14 21:07:42 -050060 unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack);
61 unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
Steven Rostedt198d2082014-02-06 09:41:31 -050062
Josh Poimboeuf5fe599e2016-09-14 21:07:43 -050063 /*
64 * This is a software stack, so 'end' can be a valid stack pointer.
65 * It just means the stack is empty.
66 */
67 if (stack < begin || stack > end)
Josh Poimboeufcb76c932016-09-14 21:07:42 -050068 return false;
69
70 info->type = STACK_TYPE_SOFTIRQ;
71 info->begin = begin;
72 info->end = end;
73
74 /*
75 * The next stack pointer is stored at the beginning of the stack.
76 * See irq_32.c.
77 */
78 info->next_sp = (unsigned long *)*begin;
79
80 return true;
81}
82
83int get_stack_info(unsigned long *stack, struct task_struct *task,
84 struct stack_info *info, unsigned long *visit_mask)
85{
86 if (!stack)
87 goto unknown;
88
89 task = task ? : current;
90
91 if (in_task_stack(stack, task, info))
Josh Poimboeuffcd709e2016-09-14 21:07:44 -050092 goto recursion_check;
Josh Poimboeufcb76c932016-09-14 21:07:42 -050093
94 if (task != current)
95 goto unknown;
96
97 if (in_hardirq_stack(stack, info))
Josh Poimboeuffcd709e2016-09-14 21:07:44 -050098 goto recursion_check;
Josh Poimboeufcb76c932016-09-14 21:07:42 -050099
100 if (in_softirq_stack(stack, info))
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500101 goto recursion_check;
102
103 goto unknown;
104
105recursion_check:
106 /*
107 * Make sure we don't iterate through any given stack more than once.
108 * If it comes up a second time then there's something wrong going on:
109 * just break out and report an unknown stack type.
110 */
111 if (visit_mask) {
112 if (*visit_mask & (1UL << info->type))
113 goto unknown;
114 *visit_mask |= 1UL << info->type;
115 }
116
117 return 0;
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500118
119unknown:
120 info->type = STACK_TYPE_UNKNOWN;
121 return -EINVAL;
Steven Rostedt198d2082014-02-06 09:41:31 -0500122}
Frederic Weisbecker0406ca62009-07-01 21:02:09 +0200123
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900124void dump_trace(struct task_struct *task, struct pt_regs *regs,
125 unsigned long *stack, unsigned long bp,
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200126 const struct stacktrace_ops *ops, void *data)
127{
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500128 unsigned long visit_mask = 0;
Steven Rostedt7ee991f2008-12-02 23:50:04 -0500129 int graph = 0;
130
Josh Poimboeuf4b8afaf2016-08-24 11:50:17 -0500131 task = task ? : current;
132 stack = stack ? : get_stack_pointer(task, regs);
133 bp = bp ? : (unsigned long)get_frame_pointer(task, regs);
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900134
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200135 for (;;) {
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500136 const char *begin_str, *end_str;
137 struct stack_info info;
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200138
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500139 if (get_stack_info(stack, task, &info, &visit_mask))
Steven Rostedt0788aa62014-02-06 09:41:30 -0500140 break;
141
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500142 stack_type_str(info.type, &begin_str, &end_str);
143
144 if (begin_str && ops->stack(data, begin_str) < 0)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200145 break;
Steven Rostedt0788aa62014-02-06 09:41:30 -0500146
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500147 bp = ops->walk_stack(task, stack, bp, ops, data, &info, &graph);
148
149 if (end_str && ops->stack(data, end_str) < 0)
Alexander van Heukelum2ac53722008-10-04 23:12:43 +0200150 break;
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500151
152 stack = info.next_sp;
153
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200154 touch_nmi_watchdog();
155 }
156}
157EXPORT_SYMBOL(dump_trace);
158
Neil Horman878719e2008-10-23 10:40:06 -0400159void
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200160show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900161 unsigned long *sp, unsigned long bp, char *log_lvl)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200162{
163 unsigned long *stack;
164 int i;
165
Josh Poimboeuf4b8afaf2016-08-24 11:50:17 -0500166 sp = sp ? : get_stack_pointer(task, regs);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200167
168 stack = sp;
169 for (i = 0; i < kstack_depth_to_print; i++) {
170 if (kstack_end(stack))
171 break;
Adrien Schildknecht1fc7f612015-02-20 03:34:21 +0100172 if ((i % STACKSLOTS_PER_LINE) == 0) {
173 if (i != 0)
174 pr_cont("\n");
175 printk("%s %08lx", log_lvl, *stack++);
176 } else
177 pr_cont(" %08lx", *stack++);
Alexander van Heukelumca0a8162008-10-04 23:12:44 +0200178 touch_nmi_watchdog();
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200179 }
Joe Perchesc767a542012-05-21 19:50:07 -0700180 pr_cont("\n");
Namhyung Kime8e999cf2011-03-18 11:40:06 +0900181 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200182}
183
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200184
Jan Beulich57da8b92012-05-09 08:47:37 +0100185void show_regs(struct pt_regs *regs)
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200186{
187 int i;
188
Tejun Heoa43cb952013-04-30 15:27:17 -0700189 show_regs_print_info(KERN_EMERG);
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700190 __show_regs(regs, !user_mode(regs));
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200191
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200192 /*
193 * When in-kernel, we also print out the stack and code at the
194 * time of the fault..
195 */
Andy Lutomirskif39b6f02015-03-18 18:33:33 -0700196 if (!user_mode(regs)) {
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200197 unsigned int code_prologue = code_bytes * 43 / 64;
198 unsigned int code_len = code_bytes;
199 unsigned char c;
200 u8 *ip;
201
Joe Perchesc767a542012-05-21 19:50:07 -0700202 pr_emerg("Stack:\n");
Josh Poimboeuf5a8ff542016-08-24 11:50:18 -0500203 show_stack_log_lvl(NULL, regs, NULL, 0, KERN_EMERG);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200204
Joe Perchesc767a542012-05-21 19:50:07 -0700205 pr_emerg("Code:");
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200206
207 ip = (u8 *)regs->ip - code_prologue;
208 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
Alexander van Heukelum8a541662008-10-04 23:12:46 +0200209 /* try starting at IP */
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200210 ip = (u8 *)regs->ip;
211 code_len = code_len - code_prologue + 1;
212 }
213 for (i = 0; i < code_len; i++, ip++) {
214 if (ip < (u8 *)PAGE_OFFSET ||
215 probe_kernel_address(ip, c)) {
Joe Perchesc767a542012-05-21 19:50:07 -0700216 pr_cont(" Bad EIP value.");
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200217 break;
218 }
219 if (ip == (u8 *)regs->ip)
Joe Perchesc767a542012-05-21 19:50:07 -0700220 pr_cont(" <%02x>", c);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200221 else
Joe Perchesc767a542012-05-21 19:50:07 -0700222 pr_cont(" %02x", c);
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200223 }
224 }
Joe Perchesc767a542012-05-21 19:50:07 -0700225 pr_cont("\n");
Alexander van Heukelum2bc5f922008-09-30 13:12:14 +0200226}
227
228int is_valid_bugaddr(unsigned long ip)
229{
230 unsigned short ud2;
231
232 if (ip < PAGE_OFFSET)
233 return 0;
234 if (probe_kernel_address((unsigned short *)ip, ud2))
235 return 0;
236
237 return ud2 == 0x0b0f;
238}