]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/kernel/dumpstack_64.c
x86/process: Allow runtime control of Speculative Store Bypass
[mirror_ubuntu-artful-kernel.git] / arch / x86 / kernel / dumpstack_64.c
CommitLineData
6fcbede3
AH
1/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4 */
b17b0153 5#include <linux/sched/debug.h>
6fcbede3
AH
6#include <linux/kallsyms.h>
7#include <linux/kprobes.h>
8#include <linux/uaccess.h>
6fcbede3
AH
9#include <linux/hardirq.h>
10#include <linux/kdebug.h>
186f4360 11#include <linux/export.h>
6fcbede3
AH
12#include <linux/ptrace.h>
13#include <linux/kexec.h>
b8030906 14#include <linux/sysfs.h>
6fcbede3
AH
15#include <linux/bug.h>
16#include <linux/nmi.h>
17
18#include <asm/stacktrace.h>
19
9c003907
JP
20static 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",
25};
6fcbede3 26
9c003907
JP
27static unsigned long exception_stack_sizes[N_EXCEPTION_STACKS] = {
28 [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
29 [DEBUG_STACK - 1] = DEBUG_STKSZ
b8030906 30};
0406ca6d 31
3d02a9c4 32const char *stack_type_name(enum stack_type type)
0406ca6d 33{
cb76c939
JP
34 BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
35
3d02a9c4
JP
36 if (type == STACK_TYPE_IRQ)
37 return "IRQ";
38
e0437c47
DH
39 if (type == STACK_TYPE_ENTRY) {
40 /*
41 * On 64-bit, we have a generic entry stack that we
42 * use for all the kernel entry points, including
43 * SYSENTER.
44 */
45 return "ENTRY_TRAMPOLINE";
46 }
72e90cc5 47
3d02a9c4
JP
48 if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
49 return exception_stack_names[type - STACK_TYPE_EXCEPTION];
50
51 return NULL;
cb76c939
JP
52}
53
fcd709ef 54static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
cb76c939
JP
55{
56 unsigned long *begin, *end;
57 struct pt_regs *regs;
6fcbede3
AH
58 unsigned k;
59
9c003907
JP
60 BUILD_BUG_ON(N_EXCEPTION_STACKS != 4);
61
6fcbede3 62 for (k = 0; k < N_EXCEPTION_STACKS; k++) {
cb76c939
JP
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;
9c003907
JP
66
67 if (stack < begin || stack >= end)
6fcbede3 68 continue;
9c003907 69
cb76c939
JP
70 info->type = STACK_TYPE_EXCEPTION + k;
71 info->begin = begin;
72 info->end = end;
73 info->next_sp = (unsigned long *)regs->sp;
74
75 return true;
6fcbede3 76 }
9c003907 77
cb76c939 78 return false;
6fcbede3
AH
79}
80
cb76c939 81static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
af2d8289 82{
cb76c939
JP
83 unsigned long *end = (unsigned long *)this_cpu_read(irq_stack_ptr);
84 unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
af2d8289 85
5fe599e0
JP
86 /*
87 * This is a software stack, so 'end' can be a valid stack pointer.
88 * It just means the stack is empty.
89 */
90 if (stack < begin || stack > end)
cb76c939 91 return false;
2223f6f6 92
cb76c939
JP
93 info->type = STACK_TYPE_IRQ;
94 info->begin = begin;
95 info->end = end;
96
97 /*
98 * The next stack pointer is the first thing pushed by the entry code
99 * after switching to the irq stack.
100 */
101 info->next_sp = (unsigned long *)*(end - 1);
102
103 return true;
104}
105
106int get_stack_info(unsigned long *stack, struct task_struct *task,
107 struct stack_info *info, unsigned long *visit_mask)
2223f6f6 108{
cb76c939
JP
109 if (!stack)
110 goto unknown;
111
112 task = task ? : current;
2223f6f6 113
cb76c939 114 if (in_task_stack(stack, task, info))
fcd709ef 115 goto recursion_check;
2223f6f6 116
cb76c939
JP
117 if (task != current)
118 goto unknown;
2223f6f6 119
fcd709ef
JP
120 if (in_exception_stack(stack, info))
121 goto recursion_check;
2223f6f6 122
cb76c939 123 if (in_irq_stack(stack, info))
fcd709ef
JP
124 goto recursion_check;
125
e0437c47 126 if (in_entry_stack(stack, info))
72e90cc5
AL
127 goto recursion_check;
128
fcd709ef
JP
129 goto unknown;
130
131recursion_check:
132 /*
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.
136 */
137 if (visit_mask) {
0d2b8579
JP
138 if (*visit_mask & (1UL << info->type)) {
139 printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
fcd709ef 140 goto unknown;
0d2b8579 141 }
fcd709ef
JP
142 *visit_mask |= 1UL << info->type;
143 }
2223f6f6 144
cb76c939 145 return 0;
2223f6f6 146
cb76c939
JP
147unknown:
148 info->type = STACK_TYPE_UNKNOWN;
149 return -EINVAL;
2223f6f6
SR
150}
151
57da8b96 152void show_regs(struct pt_regs *regs)
6fcbede3
AH
153{
154 int i;
6fcbede3 155
a43cb95d 156 show_regs_print_info(KERN_DEFAULT);
6fcbede3 157 __show_regs(regs, 1);
6fcbede3
AH
158
159 /*
160 * When in-kernel, we also print out the stack and code at the
161 * time of the fault..
162 */
163 if (!user_mode(regs)) {
164 unsigned int code_prologue = code_bytes * 43 / 64;
165 unsigned int code_len = code_bytes;
166 unsigned char c;
167 u8 *ip;
168
0ee1dd9f 169 show_trace_log_lvl(current, regs, NULL, KERN_DEFAULT);
6fcbede3 170
b0f4c4b3 171 printk(KERN_DEFAULT "Code: ");
6fcbede3
AH
172
173 ip = (u8 *)regs->ip - code_prologue;
174 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
8a541665 175 /* try starting at IP */
6fcbede3
AH
176 ip = (u8 *)regs->ip;
177 code_len = code_len - code_prologue + 1;
178 }
179 for (i = 0; i < code_len; i++, ip++) {
180 if (ip < (u8 *)PAGE_OFFSET ||
181 probe_kernel_address(ip, c)) {
c767a54b 182 pr_cont(" Bad RIP value.");
6fcbede3
AH
183 break;
184 }
185 if (ip == (u8 *)regs->ip)
c767a54b 186 pr_cont("<%02x> ", c);
6fcbede3 187 else
c767a54b 188 pr_cont("%02x ", c);
6fcbede3
AH
189 }
190 }
c767a54b 191 pr_cont("\n");
6fcbede3 192}