2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 #include <linux/sched/debug.h>
6 #include <linux/kallsyms.h>
7 #include <linux/kprobes.h>
8 #include <linux/uaccess.h>
9 #include <linux/hardirq.h>
10 #include <linux/kdebug.h>
11 #include <linux/export.h>
12 #include <linux/ptrace.h>
13 #include <linux/kexec.h>
14 #include <linux/sysfs.h>
15 #include <linux/bug.h>
16 #include <linux/nmi.h>
18 #include <asm/stacktrace.h>
20 static char *exception_stack_names[N_EXCEPTION_STACKS] = {
21 [ DOUBLEFAULT_STACK-1 ] = "#DF",
22 [ NMI_STACK-1 ] = "NMI",
23 [ DEBUG_STACK-1 ] = "#DB",
24 [ MCE_STACK-1 ] = "#MC",
27 static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
28 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
29 [DEBUG_STACK - 1] = DEBUG_STKSZ
32 const char *stack_type_name(enum stack_type type)
34 BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
36 if (type == STACK_TYPE_IRQ)
39 if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
40 return exception_stack_names[type - STACK_TYPE_EXCEPTION];
45 static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
47 unsigned long *begin, *end;
51 BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
53 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
54 end = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k];
55 begin = end - (exception_stack_sizes[k] / sizeof(long));
56 regs = (struct pt_regs *)end - 1;
58 if (stack < begin || stack >= end)
61 info->type = STACK_TYPE_EXCEPTION + k;
64 info->next_sp = (unsigned long *)regs->sp;
72 static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
74 unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr);
75 unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
78 * This is a software stack, so 'end' can be a valid stack pointer.
79 * It just means the stack is empty.
81 if (stack < begin || stack > end)
84 info->type = STACK_TYPE_IRQ;
89 * The next stack pointer is the first thing pushed by the entry code
90 * after switching to the irq stack.
92 info->next_sp = (unsigned long *)*(end - 1);
97 int get_stack_info(unsigned long *stack, struct task_struct *task,
98 struct stack_info *info, unsigned long *visit_mask)
103 task = task ? : current;
105 if (in_task_stack(stack, task, info))
106 goto recursion_check;
111 if (in_exception_stack(stack, info))
112 goto recursion_check;
114 if (in_irq_stack(stack, info))
115 goto recursion_check;
121 * Make sure we don't iterate through any given stack more than once.
122 * If it comes up a second time then there's something wrong going on:
123 * just break out and report an unknown stack type.
126 if (*visit_mask & (1UL << info->type)) {
127 printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
130 *visit_mask |= 1UL << info->type;
136 info->type = STACK_TYPE_UNKNOWN;
140 void show_regs(struct pt_regs *regs)
144 show_regs_print_info(KERN_DEFAULT);
145 __show_regs(regs, 1);
148 * When in-kernel, we also print out the stack and code at the
149 * time of the fault..
151 if (!user_mode(regs)) {
152 unsigned int code_prologue = code_bytes * 43 / 64;
153 unsigned int code_len = code_bytes;
157 show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
159 printk(KERN_DEFAULT "Code: ");
161 ip = (u8 *)regs->ip - code_prologue;
162 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
163 /* try starting at IP */
165 code_len = code_len - code_prologue + 1;
167 for (i = 0; i < code_len; i++, ip++) {
168 if (ip < (u8 *)PAGE_OFFSET ||
169 probe_kernel_address(ip, c)) {
170 pr_cont(" Bad RIP value.");
173 if (ip == (u8 *)regs->ip)
174 pr_cont("<%02x> ", c);