1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
6 #include <linux/sched/debug.h>
7 #include <linux/kallsyms.h>
8 #include <linux/kprobes.h>
9 #include <linux/uaccess.h>
10 #include <linux/hardirq.h>
11 #include <linux/kdebug.h>
12 #include <linux/export.h>
13 #include <linux/ptrace.h>
14 #include <linux/kexec.h>
15 #include <linux/sysfs.h>
16 #include <linux/bug.h>
17 #include <linux/nmi.h>
19 #include <asm/stacktrace.h>
21 const char *stack_type_name(enum stack_type type)
23 if (type == STACK_TYPE_IRQ)
26 if (type == STACK_TYPE_SOFTIRQ)
32 static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
34 unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack);
35 unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
38 * This is a software stack, so 'end' can be a valid stack pointer.
39 * It just means the stack is empty.
41 if (stack <= begin || stack > end)
44 info->type = STACK_TYPE_IRQ;
49 * See irq_32.c -- the next stack pointer is stored at the beginning of
52 info->next_sp = (unsigned long *)*begin;
57 static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
59 unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack);
60 unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
63 * This is a software stack, so 'end' can be a valid stack pointer.
64 * It just means the stack is empty.
66 if (stack <= begin || stack > end)
69 info->type = STACK_TYPE_SOFTIRQ;
74 * The next stack pointer is stored at the beginning of the stack.
77 info->next_sp = (unsigned long *)*begin;
82 int get_stack_info(unsigned long *stack, struct task_struct *task,
83 struct stack_info *info, unsigned long *visit_mask)
88 task = task ? : current;
90 if (in_task_stack(stack, task, info))
96 if (in_hardirq_stack(stack, info))
99 if (in_softirq_stack(stack, info))
100 goto recursion_check;
106 * Make sure we don't iterate through any given stack more than once.
107 * If it comes up a second time then there's something wrong going on:
108 * just break out and report an unknown stack type.
111 if (*visit_mask & (1UL << info->type)) {
112 printk_deferred_once(KERN_WARNING "WARNING: stack recursion on stack type %d\n", info->type);
115 *visit_mask |= 1UL << info->type;
121 info->type = STACK_TYPE_UNKNOWN;
125 void show_regs(struct pt_regs *regs)
129 show_regs_print_info(KERN_EMERG);
130 __show_regs(regs, !user_mode(regs));
133 * When in-kernel, we also print out the stack and code at the
134 * time of the fault..
136 if (!user_mode(regs)) {
137 unsigned int code_prologue = code_bytes * 43 / 64;
138 unsigned int code_len = code_bytes;
142 show_trace_log_lvl(current, regs, NULL, KERN_EMERG);
146 ip = (u8 *)regs->ip - code_prologue;
147 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
148 /* try starting at IP */
150 code_len = code_len - code_prologue + 1;
152 for (i = 0; i < code_len; i++, ip++) {
153 if (ip < (u8 *)PAGE_OFFSET ||
154 probe_kernel_address(ip, c)) {
155 pr_cont(" Bad EIP value.");
158 if (ip == (u8 *)regs->ip)
159 pr_cont(" <%02x>", c);