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_ENTRY
) {
41 * On 64-bit, we have a generic entry stack that we
42 * use for all the kernel entry points, including
45 return "ENTRY_TRAMPOLINE";
48 if (type
>= STACK_TYPE_EXCEPTION
&& type
<= STACK_TYPE_EXCEPTION_LAST
)
49 return exception_stack_names
[type
- STACK_TYPE_EXCEPTION
];
54 static bool in_exception_stack(unsigned long *stack
, struct stack_info
*info
)
56 unsigned long *begin
, *end
;
60 BUILD_BUG_ON(N_EXCEPTION_STACKS
!= 4);
62 for (k
= 0; k
< N_EXCEPTION_STACKS
; k
++) {
63 end
= (unsigned long *)raw_cpu_ptr(&orig_ist
)->ist
[k
];
64 begin
= end
- (exception_stack_sizes
[k
] / sizeof(long));
65 regs
= (struct pt_regs
*)end
- 1;
67 if (stack
< begin
|| stack
>= end
)
70 info
->type
= STACK_TYPE_EXCEPTION
+ k
;
73 info
->next_sp
= (unsigned long *)regs
->sp
;
81 static bool in_irq_stack(unsigned long *stack
, struct stack_info
*info
)
83 unsigned long *end
= (unsigned long *)this_cpu_read(irq_stack_ptr
);
84 unsigned long *begin
= end
- (IRQ_STACK_SIZE
/ sizeof(long));
87 * This is a software stack, so 'end' can be a valid stack pointer.
88 * It just means the stack is empty.
90 if (stack
< begin
|| stack
> end
)
93 info
->type
= STACK_TYPE_IRQ
;
98 * The next stack pointer is the first thing pushed by the entry code
99 * after switching to the irq stack.
101 info
->next_sp
= (unsigned long *)*(end
- 1);
106 int get_stack_info(unsigned long *stack
, struct task_struct
*task
,
107 struct stack_info
*info
, unsigned long *visit_mask
)
112 task
= task
? : current
;
114 if (in_task_stack(stack
, task
, info
))
115 goto recursion_check
;
120 if (in_exception_stack(stack
, info
))
121 goto recursion_check
;
123 if (in_irq_stack(stack
, info
))
124 goto recursion_check
;
126 if (in_entry_stack(stack
, info
))
127 goto recursion_check
;
133 * Make sure we don't iterate through any given stack more than once.
134 * If it comes up a second time then there's something wrong going on:
135 * just break out and report an unknown stack type.
138 if (*visit_mask
& (1UL << info
->type
)) {
139 printk_deferred_once(KERN_WARNING
"WARNING: stack recursion on stack type %d\n", info
->type
);
142 *visit_mask
|= 1UL << info
->type
;
148 info
->type
= STACK_TYPE_UNKNOWN
;
152 void show_regs(struct pt_regs
*regs
)
156 show_regs_print_info(KERN_DEFAULT
);
157 __show_regs(regs
, 1);
160 * When in-kernel, we also print out the stack and code at the
161 * time of the fault..
163 if (!user_mode(regs
)) {
164 unsigned int code_prologue
= code_bytes
* 43 / 64;
165 unsigned int code_len
= code_bytes
;
169 show_trace_log_lvl(current
, regs
, NULL
, KERN_DEFAULT
);
171 printk(KERN_DEFAULT
"Code: ");
173 ip
= (u8
*)regs
->ip
- code_prologue
;
174 if (ip
< (u8
*)PAGE_OFFSET
|| probe_kernel_address(ip
, c
)) {
175 /* try starting at IP */
177 code_len
= code_len
- code_prologue
+ 1;
179 for (i
= 0; i
< code_len
; i
++, ip
++) {
180 if (ip
< (u8
*)PAGE_OFFSET
||
181 probe_kernel_address(ip
, c
)) {
182 pr_cont(" Bad RIP value.");
185 if (ip
== (u8
*)regs
->ip
)
186 pr_cont("<%02x> ", c
);