]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6fcbede3 AH |
2 | /* |
3 | * Copyright (C) 1991, 1992 Linus Torvalds | |
4 | * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs | |
5 | */ | |
b17b0153 | 6 | #include <linux/sched/debug.h> |
6fcbede3 AH |
7 | #include <linux/kallsyms.h> |
8 | #include <linux/kprobes.h> | |
9 | #include <linux/uaccess.h> | |
6fcbede3 AH |
10 | #include <linux/hardirq.h> |
11 | #include <linux/kdebug.h> | |
186f4360 | 12 | #include <linux/export.h> |
6fcbede3 AH |
13 | #include <linux/ptrace.h> |
14 | #include <linux/kexec.h> | |
b8030906 | 15 | #include <linux/sysfs.h> |
6fcbede3 AH |
16 | #include <linux/bug.h> |
17 | #include <linux/nmi.h> | |
18 | ||
19 | #include <asm/stacktrace.h> | |
20 | ||
9c003907 JP |
21 | static char *exception_stack_names[N_EXCEPTION_STACKS] = { |
22 | [ DOUBLEFAULT_STACK-1 ] = "#DF", | |
23 | [ NMI_STACK-1 ] = "NMI", | |
24 | [ DEBUG_STACK-1 ] = "#DB", | |
25 | [ MCE_STACK-1 ] = "#MC", | |
26 | }; | |
6fcbede3 | 27 | |
9c003907 JP |
28 | static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = { |
29 | [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ, | |
30 | [DEBUG_STACK - 1] = DEBUG_STKSZ | |
b8030906 | 31 | }; |
0406ca6d | 32 | |
3d02a9c4 | 33 | const char *stack_type_name(enum stack_type type) |
0406ca6d | 34 | { |
cb76c939 JP |
35 | BUILD_BUG_ON(N_EXCEPTION_STACKS != 4); |
36 | ||
3d02a9c4 JP |
37 | if (type == STACK_TYPE_IRQ) |
38 | return "IRQ"; | |
39 | ||
40 | if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST) | |
41 | return exception_stack_names[type - STACK_TYPE_EXCEPTION]; | |
42 | ||
43 | return NULL; | |
cb76c939 JP |
44 | } |
45 | ||
fcd709ef | 46 | static bool in_exception_stack(unsigned long *stack, struct stack_info *info) |
cb76c939 JP |
47 | { |
48 | unsigned long *begin, *end; | |
49 | struct pt_regs *regs; | |
6fcbede3 AH |
50 | unsigned k; |
51 | ||
9c003907 JP |
52 | BUILD_BUG_ON(N_EXCEPTION_STACKS != 4); |
53 | ||
6fcbede3 | 54 | for (k = 0; k < N_EXCEPTION_STACKS; k++) { |
cb76c939 JP |
55 | end = (unsigned long *)raw_cpu_ptr(&orig_ist)->ist[k]; |
56 | begin = end - (exception_stack_sizes[k] / sizeof(long)); | |
57 | regs = (struct pt_regs *)end - 1; | |
9c003907 | 58 | |
5a3cf869 | 59 | if (stack <= begin || stack >= end) |
6fcbede3 | 60 | continue; |
9c003907 | 61 | |
cb76c939 JP |
62 | info->type = STACK_TYPE_EXCEPTION + k; |
63 | info->begin = begin; | |
64 | info->end = end; | |
65 | info->next_sp = (unsigned long *)regs->sp; | |
66 | ||
67 | return true; | |
6fcbede3 | 68 | } |
9c003907 | 69 | |
cb76c939 | 70 | return false; |
6fcbede3 AH |
71 | } |
72 | ||
cb76c939 | 73 | static bool in_irq_stack(unsigned long *stack, struct stack_info *info) |
af2d8289 | 74 | { |
cb76c939 JP |
75 | unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr); |
76 | unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long)); | |
af2d8289 | 77 | |
5fe599e0 JP |
78 | /* |
79 | * This is a software stack, so 'end' can be a valid stack pointer. | |
80 | * It just means the stack is empty. | |
81 | */ | |
5a3cf869 | 82 | if (stack <= begin || stack > end) |
cb76c939 | 83 | return false; |
2223f6f6 | 84 | |
cb76c939 JP |
85 | info->type = STACK_TYPE_IRQ; |
86 | info->begin = begin; | |
87 | info->end = end; | |
88 | ||
89 | /* | |
90 | * The next stack pointer is the first thing pushed by the entry code | |
91 | * after switching to the irq stack. | |
92 | */ | |
93 | info->next_sp = (unsigned long *)*(end - 1); | |
94 | ||
95 | return true; | |
96 | } | |
97 | ||
98 | int get_stack_info(unsigned long *stack, struct task_struct *task, | |
99 | struct stack_info *info, unsigned long *visit_mask) | |
2223f6f6 | 100 | { |
cb76c939 JP |
101 | if (!stack) |
102 | goto unknown; | |
103 | ||
104 | task = task ? : current; | |
2223f6f6 | 105 | |
cb76c939 | 106 | if (in_task_stack(stack, task, info)) |
fcd709ef | 107 | goto recursion_check; |
2223f6f6 | 108 | |
cb76c939 JP |
109 | if (task != current) |
110 | goto unknown; | |
2223f6f6 | 111 | |
fcd709ef JP |
112 | if (in_exception_stack(stack, info)) |
113 | goto recursion_check; | |
2223f6f6 | 114 | |
cb76c939 | 115 | if (in_irq_stack(stack, info)) |
fcd709ef JP |
116 | goto recursion_check; |
117 | ||
118 | goto unknown; | |
119 | ||
120 | recursion_check: | |
121 | /* | |
122 | * Make sure we don't iterate through any given stack more than once. | |
123 | * If it comes up a second time then there's something wrong going on: | |
124 | * just break out and report an unknown stack type. | |
125 | */ | |
126 | if (visit_mask) { | |
0d2b8579 JP |
127 | if (*visit_mask & (1UL << info->type)) { |
128 | printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type); | |
fcd709ef | 129 | goto unknown; |
0d2b8579 | 130 | } |
fcd709ef JP |
131 | *visit_mask |= 1UL << info->type; |
132 | } | |
2223f6f6 | 133 | |
cb76c939 | 134 | return 0; |
2223f6f6 | 135 | |
cb76c939 JP |
136 | unknown: |
137 | info->type = STACK_TYPE_UNKNOWN; | |
138 | return -EINVAL; | |
2223f6f6 SR |
139 | } |
140 | ||
57da8b96 | 141 | void show_regs(struct pt_regs *regs) |
6fcbede3 AH |
142 | { |
143 | int i; | |
6fcbede3 | 144 | |
a43cb95d | 145 | show_regs_print_info(KERN_DEFAULT); |
6fcbede3 | 146 | __show_regs(regs, 1); |
6fcbede3 AH |
147 | |
148 | /* | |
149 | * When in-kernel, we also print out the stack and code at the | |
150 | * time of the fault.. | |
151 | */ | |
152 | if (!user_mode(regs)) { | |
153 | unsigned int code_prologue = code_bytes * 43 / 64; | |
154 | unsigned int code_len = code_bytes; | |
155 | unsigned char c; | |
156 | u8 *ip; | |
157 | ||
0ee1dd9f | 158 | show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT); |
6fcbede3 | 159 | |
b0f4c4b3 | 160 | printk(KERN_DEFAULT "Code: "); |
6fcbede3 AH |
161 | |
162 | ip = (u8 *)regs->ip - code_prologue; | |
163 | if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) { | |
8a541665 | 164 | /* try starting at IP */ |
6fcbede3 AH |
165 | ip = (u8 *)regs->ip; |
166 | code_len = code_len - code_prologue + 1; | |
167 | } | |
168 | for (i = 0; i < code_len; i++, ip++) { | |
169 | if (ip < (u8 *)PAGE_OFFSET || | |
170 | probe_kernel_address(ip, c)) { | |
c767a54b | 171 | pr_cont(" Bad RIP value."); |
6fcbede3 AH |
172 | break; |
173 | } | |
174 | if (ip == (u8 *)regs->ip) | |
c767a54b | 175 | pr_cont("<%02x> ", c); |
6fcbede3 | 176 | else |
c767a54b | 177 | pr_cont("%02x ", c); |
6fcbede3 AH |
178 | } |
179 | } | |
c767a54b | 180 | pr_cont("\n"); |
6fcbede3 | 181 | } |