xref: /linux-6.15/arch/x86/kernel/dumpstack_32.c (revision cfeeed27)
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  */
5 #include <linux/kallsyms.h>
6 #include <linux/kprobes.h>
7 #include <linux/uaccess.h>
8 #include <linux/hardirq.h>
9 #include <linux/kdebug.h>
10 #include <linux/export.h>
11 #include <linux/ptrace.h>
12 #include <linux/kexec.h>
13 #include <linux/sysfs.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
16 
17 #include <asm/stacktrace.h>
18 
19 static void *is_irq_stack(void *p, void *irq)
20 {
21 	if (p < irq || p >= (irq + THREAD_SIZE))
22 		return NULL;
23 	return irq + THREAD_SIZE;
24 }
25 
26 
27 static void *is_hardirq_stack(unsigned long *stack)
28 {
29 	void *irq = this_cpu_read(hardirq_stack);
30 
31 	return is_irq_stack(stack, irq);
32 }
33 
34 static void *is_softirq_stack(unsigned long *stack)
35 {
36 	void *irq = this_cpu_read(softirq_stack);
37 
38 	return is_irq_stack(stack, irq);
39 }
40 
41 void dump_trace(struct task_struct *task, struct pt_regs *regs,
42 		unsigned long *stack, unsigned long bp,
43 		const struct stacktrace_ops *ops, void *data)
44 {
45 	int graph = 0;
46 	u32 *prev_esp;
47 
48 	task = task ? : current;
49 	stack = stack ? : get_stack_pointer(task, regs);
50 	bp = bp ? : (unsigned long)get_frame_pointer(task, regs);
51 
52 	for (;;) {
53 		void *end_stack;
54 
55 		end_stack = is_hardirq_stack(stack);
56 		if (!end_stack)
57 			end_stack = is_softirq_stack(stack);
58 
59 		bp = ops->walk_stack(task, stack, bp, ops, data,
60 				     end_stack, &graph);
61 
62 		/* Stop if not on irq stack */
63 		if (!end_stack)
64 			break;
65 
66 		/* The previous esp is saved on the bottom of the stack */
67 		prev_esp = (u32 *)(end_stack - THREAD_SIZE);
68 		stack = (unsigned long *)*prev_esp;
69 		if (!stack)
70 			break;
71 
72 		if (ops->stack(data, "IRQ") < 0)
73 			break;
74 		touch_nmi_watchdog();
75 	}
76 }
77 EXPORT_SYMBOL(dump_trace);
78 
79 void
80 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
81 		   unsigned long *sp, unsigned long bp, char *log_lvl)
82 {
83 	unsigned long *stack;
84 	int i;
85 
86 	sp = sp ? : get_stack_pointer(task, regs);
87 
88 	stack = sp;
89 	for (i = 0; i < kstack_depth_to_print; i++) {
90 		if (kstack_end(stack))
91 			break;
92 		if ((i % STACKSLOTS_PER_LINE) == 0) {
93 			if (i != 0)
94 				pr_cont("\n");
95 			printk("%s %08lx", log_lvl, *stack++);
96 		} else
97 			pr_cont(" %08lx", *stack++);
98 		touch_nmi_watchdog();
99 	}
100 	pr_cont("\n");
101 	show_trace_log_lvl(task, regs, sp, bp, log_lvl);
102 }
103 
104 
105 void show_regs(struct pt_regs *regs)
106 {
107 	int i;
108 
109 	show_regs_print_info(KERN_EMERG);
110 	__show_regs(regs, !user_mode(regs));
111 
112 	/*
113 	 * When in-kernel, we also print out the stack and code at the
114 	 * time of the fault..
115 	 */
116 	if (!user_mode(regs)) {
117 		unsigned int code_prologue = code_bytes * 43 / 64;
118 		unsigned int code_len = code_bytes;
119 		unsigned char c;
120 		u8 *ip;
121 
122 		pr_emerg("Stack:\n");
123 		show_stack_log_lvl(NULL, regs, NULL, 0, KERN_EMERG);
124 
125 		pr_emerg("Code:");
126 
127 		ip = (u8 *)regs->ip - code_prologue;
128 		if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
129 			/* try starting at IP */
130 			ip = (u8 *)regs->ip;
131 			code_len = code_len - code_prologue + 1;
132 		}
133 		for (i = 0; i < code_len; i++, ip++) {
134 			if (ip < (u8 *)PAGE_OFFSET ||
135 					probe_kernel_address(ip, c)) {
136 				pr_cont("  Bad EIP value.");
137 				break;
138 			}
139 			if (ip == (u8 *)regs->ip)
140 				pr_cont(" <%02x>", c);
141 			else
142 				pr_cont(" %02x", c);
143 		}
144 	}
145 	pr_cont("\n");
146 }
147 
148 int is_valid_bugaddr(unsigned long ip)
149 {
150 	unsigned short ud2;
151 
152 	if (ip < PAGE_OFFSET)
153 		return 0;
154 	if (probe_kernel_address((unsigned short *)ip, ud2))
155 		return 0;
156 
157 	return ud2 == 0x0b0f;
158 }
159