blob: 753b8cfe8b8a212d7423ec1622e3699431a3b6f2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +02002/*
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5 */
Ingo Molnarb17b0152017-02-08 18:51:35 +01006#include <linux/sched/debug.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +02007#include <linux/kallsyms.h>
8#include <linux/kprobes.h>
9#include <linux/uaccess.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020010#include <linux/hardirq.h>
11#include <linux/kdebug.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040012#include <linux/export.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020013#include <linux/ptrace.h>
14#include <linux/kexec.h>
Ingo Molnarb8030902009-11-26 08:17:31 +010015#include <linux/sysfs.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020016#include <linux/bug.h>
17#include <linux/nmi.h>
18
Thomas Gleixnerafcd21d2019-04-14 17:59:52 +020019#include <asm/cpu_entry_area.h>
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020020#include <asm/stacktrace.h>
21
Thomas Gleixner2a594d42019-04-14 17:59:57 +020022static const char * const exception_stack_names[] = {
Thomas Gleixner8f34c5b2019-04-14 17:59:45 +020023 [ ESTACK_DF ] = "#DF",
24 [ ESTACK_NMI ] = "NMI",
Thomas Gleixner2a594d42019-04-14 17:59:57 +020025 [ ESTACK_DB2 ] = "#DB2",
26 [ ESTACK_DB1 ] = "#DB1",
Thomas Gleixner8f34c5b2019-04-14 17:59:45 +020027 [ ESTACK_DB ] = "#DB",
28 [ ESTACK_MCE ] = "#MC",
Josh Poimboeuf9c003902016-09-14 21:07:41 -050029};
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020030
Josh Poimboeuf3d02a9c2016-11-18 11:46:23 -060031const char *stack_type_name(enum stack_type type)
Frederic Weisbecker0406ca62009-07-01 21:02:09 +020032{
Thomas Gleixner2a594d42019-04-14 17:59:57 +020033 BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
Josh Poimboeufcb76c932016-09-14 21:07:42 -050034
Josh Poimboeuf3d02a9c2016-11-18 11:46:23 -060035 if (type == STACK_TYPE_IRQ)
36 return "IRQ";
37
Dave Hansen4fe2d8b2017-12-04 17:25:07 -080038 if (type == STACK_TYPE_ENTRY) {
39 /*
40 * On 64-bit, we have a generic entry stack that we
41 * use for all the kernel entry points, including
42 * SYSENTER.
43 */
44 return "ENTRY_TRAMPOLINE";
45 }
Andy Lutomirski33a2f1a2017-12-04 15:07:13 +010046
Josh Poimboeuf3d02a9c2016-11-18 11:46:23 -060047 if (type >= STACK_TYPE_EXCEPTION && type <= STACK_TYPE_EXCEPTION_LAST)
48 return exception_stack_names[type - STACK_TYPE_EXCEPTION];
49
50 return NULL;
Josh Poimboeufcb76c932016-09-14 21:07:42 -050051}
52
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +020053/**
54 * struct estack_pages - Page descriptor for exception stacks
55 * @offs: Offset from the start of the exception stack area
56 * @size: Size of the exception stack
57 * @type: Type to store in the stack_info struct
58 */
59struct estack_pages {
60 u32 offs;
61 u16 size;
62 u16 type;
Thomas Gleixnerafcd21d2019-04-14 17:59:52 +020063};
64
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +020065#define EPAGERANGE(st) \
66 [PFN_DOWN(CEA_ESTACK_OFFS(st)) ... \
67 PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = { \
68 .offs = CEA_ESTACK_OFFS(st), \
69 .size = CEA_ESTACK_SIZE(st), \
70 .type = STACK_TYPE_EXCEPTION + ESTACK_ ##st, }
Thomas Gleixnerafcd21d2019-04-14 17:59:52 +020071
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +020072/*
73 * Array of exception stack page descriptors. If the stack is larger than
74 * PAGE_SIZE, all pages covering a particular stack will have the same
75 * info. The guard pages including the not mapped DB2 stack are zeroed
76 * out.
77 */
78static const
79struct estack_pages estack_pages[CEA_ESTACK_PAGES] ____cacheline_aligned = {
80 EPAGERANGE(DF),
81 EPAGERANGE(NMI),
82 EPAGERANGE(DB1),
83 EPAGERANGE(DB),
84 EPAGERANGE(MCE),
Thomas Gleixnerafcd21d2019-04-14 17:59:52 +020085};
86
Josh Poimboeuffcd709e2016-09-14 21:07:44 -050087static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
Josh Poimboeufcb76c932016-09-14 21:07:42 -050088{
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +020089 unsigned long begin, end, stk = (unsigned long)stack;
90 const struct estack_pages *ep;
Josh Poimboeufcb76c932016-09-14 21:07:42 -050091 struct pt_regs *regs;
Thomas Gleixnerafcd21d2019-04-14 17:59:52 +020092 unsigned int k;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020093
Thomas Gleixner2a594d42019-04-14 17:59:57 +020094 BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +020095
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +020096 begin = (unsigned long)__this_cpu_read(cea_exception_stacks);
97 end = begin + sizeof(struct cea_exception_stacks);
98 /* Bail if @stack is outside the exception stack area. */
99 if (stk < begin || stk >= end)
100 return false;
Thomas Gleixnerafcd21d2019-04-14 17:59:52 +0200101
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +0200102 /* Calc page offset from start of exception stacks */
103 k = (stk - begin) >> PAGE_SHIFT;
104 /* Lookup the page descriptor */
105 ep = &estack_pages[k];
106 /* Guard page? */
107 if (!ep->size)
108 return false;
Josh Poimboeuf9c003902016-09-14 21:07:41 -0500109
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +0200110 begin += (unsigned long)ep->offs;
111 end = begin + (unsigned long)ep->size;
112 regs = (struct pt_regs *)end - 1;
Josh Poimboeuf9c003902016-09-14 21:07:41 -0500113
Thomas Gleixnerc450c8f2019-04-14 17:59:58 +0200114 info->type = ep->type;
115 info->begin = (unsigned long *)begin;
116 info->end = (unsigned long *)end;
117 info->next_sp = (unsigned long *)regs->sp;
118 return true;
Alexander van Heukelum6fcbede2008-09-30 13:12:15 +0200119}
120
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500121static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +0100122{
Thomas Gleixner758a2e32019-04-14 18:00:02 +0200123 unsigned long *end = (unsigned long *)this_cpu_read(hardirq_stack_ptr);
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500124 unsigned long *begin = end - (IRQ_STACK_SIZE / sizeof(long));
125
Josh Poimboeuf5fe599e2016-09-14 21:07:43 -0500126 /*
127 * This is a software stack, so 'end' can be a valid stack pointer.
128 * It just means the stack is empty.
129 */
Andy Lutomirskifa332152019-04-14 17:59:39 +0200130 if (stack < begin || stack >= end)
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500131 return false;
132
133 info->type = STACK_TYPE_IRQ;
134 info->begin = begin;
135 info->end = end;
136
137 /*
138 * The next stack pointer is the first thing pushed by the entry code
139 * after switching to the irq stack.
140 */
141 info->next_sp = (unsigned long *)*(end - 1);
142
143 return true;
Frederic Weisbeckeraf2d8282009-12-06 05:34:27 +0100144}
145
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500146int get_stack_info(unsigned long *stack, struct task_struct *task,
147 struct stack_info *info, unsigned long *visit_mask)
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500148{
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500149 if (!stack)
150 goto unknown;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500151
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500152 task = task ? : current;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500153
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500154 if (in_task_stack(stack, task, info))
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500155 goto recursion_check;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500156
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500157 if (task != current)
158 goto unknown;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500159
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500160 if (in_exception_stack(stack, info))
161 goto recursion_check;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500162
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500163 if (in_irq_stack(stack, info))
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500164 goto recursion_check;
165
Dave Hansen4fe2d8b2017-12-04 17:25:07 -0800166 if (in_entry_stack(stack, info))
Andy Lutomirski33a2f1a2017-12-04 15:07:13 +0100167 goto recursion_check;
168
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500169 goto unknown;
170
171recursion_check:
172 /*
173 * Make sure we don't iterate through any given stack more than once.
174 * If it comes up a second time then there's something wrong going on:
175 * just break out and report an unknown stack type.
176 */
177 if (visit_mask) {
Josh Poimboeuf0d2b8572016-10-26 10:41:50 -0500178 if (*visit_mask & (1UL << info->type)) {
179 printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500180 goto unknown;
Josh Poimboeuf0d2b8572016-10-26 10:41:50 -0500181 }
Josh Poimboeuffcd709e2016-09-14 21:07:44 -0500182 *visit_mask |= 1UL << info->type;
183 }
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500184
Josh Poimboeufcb76c932016-09-14 21:07:42 -0500185 return 0;
186
187unknown:
188 info->type = STACK_TYPE_UNKNOWN;
189 return -EINVAL;
Steven Rostedt2223f6f2014-02-06 09:41:32 -0500190}