xref: /openbmc/linux/arch/s390/kernel/dumpstack.c (revision f35e839a)
1 /*
2  * Stack dumping functions
3  *
4  *  Copyright IBM Corp. 1999, 2013
5  */
6 
7 #include <linux/kallsyms.h>
8 #include <linux/hardirq.h>
9 #include <linux/kprobes.h>
10 #include <linux/utsname.h>
11 #include <linux/export.h>
12 #include <linux/kdebug.h>
13 #include <linux/ptrace.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <asm/processor.h>
17 #include <asm/debug.h>
18 #include <asm/ipl.h>
19 
20 #ifndef CONFIG_64BIT
21 #define LONG "%08lx "
22 #define FOURLONG "%08lx %08lx %08lx %08lx\n"
23 static int kstack_depth_to_print = 12;
24 #else /* CONFIG_64BIT */
25 #define LONG "%016lx "
26 #define FOURLONG "%016lx %016lx %016lx %016lx\n"
27 static int kstack_depth_to_print = 20;
28 #endif /* CONFIG_64BIT */
29 
30 /*
31  * For show_trace we have tree different stack to consider:
32  *   - the panic stack which is used if the kernel stack has overflown
33  *   - the asynchronous interrupt stack (cpu related)
34  *   - the synchronous kernel stack (process related)
35  * The stack trace can start at any of the three stack and can potentially
36  * touch all of them. The order is: panic stack, async stack, sync stack.
37  */
38 static unsigned long
39 __show_trace(unsigned long sp, unsigned long low, unsigned long high)
40 {
41 	struct stack_frame *sf;
42 	struct pt_regs *regs;
43 
44 	while (1) {
45 		sp = sp & PSW_ADDR_INSN;
46 		if (sp < low || sp > high - sizeof(*sf))
47 			return sp;
48 		sf = (struct stack_frame *) sp;
49 		printk("([<%016lx>] ", sf->gprs[8] & PSW_ADDR_INSN);
50 		print_symbol("%s)\n", sf->gprs[8] & PSW_ADDR_INSN);
51 		/* Follow the backchain. */
52 		while (1) {
53 			low = sp;
54 			sp = sf->back_chain & PSW_ADDR_INSN;
55 			if (!sp)
56 				break;
57 			if (sp <= low || sp > high - sizeof(*sf))
58 				return sp;
59 			sf = (struct stack_frame *) sp;
60 			printk(" [<%016lx>] ", sf->gprs[8] & PSW_ADDR_INSN);
61 			print_symbol("%s\n", sf->gprs[8] & PSW_ADDR_INSN);
62 		}
63 		/* Zero backchain detected, check for interrupt frame. */
64 		sp = (unsigned long) (sf + 1);
65 		if (sp <= low || sp > high - sizeof(*regs))
66 			return sp;
67 		regs = (struct pt_regs *) sp;
68 		printk(" [<%016lx>] ", regs->psw.addr & PSW_ADDR_INSN);
69 		print_symbol("%s\n", regs->psw.addr & PSW_ADDR_INSN);
70 		low = sp;
71 		sp = regs->gprs[15];
72 	}
73 }
74 
75 static void show_trace(struct task_struct *task, unsigned long *stack)
76 {
77 	register unsigned long __r15 asm ("15");
78 	unsigned long sp;
79 
80 	sp = (unsigned long) stack;
81 	if (!sp)
82 		sp = task ? task->thread.ksp : __r15;
83 	printk("Call Trace:\n");
84 #ifdef CONFIG_CHECK_STACK
85 	sp = __show_trace(sp, S390_lowcore.panic_stack - 4096,
86 			  S390_lowcore.panic_stack);
87 #endif
88 	sp = __show_trace(sp, S390_lowcore.async_stack - ASYNC_SIZE,
89 			  S390_lowcore.async_stack);
90 	if (task)
91 		__show_trace(sp, (unsigned long) task_stack_page(task),
92 			     (unsigned long) task_stack_page(task) + THREAD_SIZE);
93 	else
94 		__show_trace(sp, S390_lowcore.thread_info,
95 			     S390_lowcore.thread_info + THREAD_SIZE);
96 	if (!task)
97 		task = current;
98 	debug_show_held_locks(task);
99 }
100 
101 void show_stack(struct task_struct *task, unsigned long *sp)
102 {
103 	register unsigned long *__r15 asm ("15");
104 	unsigned long *stack;
105 	int i;
106 
107 	if (!sp)
108 		stack = task ? (unsigned long *) task->thread.ksp : __r15;
109 	else
110 		stack = sp;
111 
112 	for (i = 0; i < kstack_depth_to_print; i++) {
113 		if (((addr_t) stack & (THREAD_SIZE-1)) == 0)
114 			break;
115 		if ((i * sizeof(long) % 32) == 0)
116 			printk("%s       ", i == 0 ? "" : "\n");
117 		printk(LONG, *stack++);
118 	}
119 	printk("\n");
120 	show_trace(task, sp);
121 }
122 
123 static void show_last_breaking_event(struct pt_regs *regs)
124 {
125 #ifdef CONFIG_64BIT
126 	printk("Last Breaking-Event-Address:\n");
127 	printk(" [<%016lx>] ", regs->args[0] & PSW_ADDR_INSN);
128 	print_symbol("%s\n", regs->args[0] & PSW_ADDR_INSN);
129 #endif
130 }
131 
132 static inline int mask_bits(struct pt_regs *regs, unsigned long bits)
133 {
134 	return (regs->psw.mask & bits) / ((~bits + 1) & bits);
135 }
136 
137 void show_registers(struct pt_regs *regs)
138 {
139 	char *mode;
140 
141 	mode = user_mode(regs) ? "User" : "Krnl";
142 	printk("%s PSW : %p %p",
143 	       mode, (void *) regs->psw.mask,
144 	       (void *) regs->psw.addr);
145 	print_symbol(" (%s)\n", regs->psw.addr & PSW_ADDR_INSN);
146 	printk("           R:%x T:%x IO:%x EX:%x Key:%x M:%x W:%x "
147 	       "P:%x AS:%x CC:%x PM:%x", mask_bits(regs, PSW_MASK_PER),
148 	       mask_bits(regs, PSW_MASK_DAT), mask_bits(regs, PSW_MASK_IO),
149 	       mask_bits(regs, PSW_MASK_EXT), mask_bits(regs, PSW_MASK_KEY),
150 	       mask_bits(regs, PSW_MASK_MCHECK), mask_bits(regs, PSW_MASK_WAIT),
151 	       mask_bits(regs, PSW_MASK_PSTATE), mask_bits(regs, PSW_MASK_ASC),
152 	       mask_bits(regs, PSW_MASK_CC), mask_bits(regs, PSW_MASK_PM));
153 #ifdef CONFIG_64BIT
154 	printk(" EA:%x", mask_bits(regs, PSW_MASK_EA | PSW_MASK_BA));
155 #endif
156 	printk("\n%s GPRS: " FOURLONG, mode,
157 	       regs->gprs[0], regs->gprs[1], regs->gprs[2], regs->gprs[3]);
158 	printk("           " FOURLONG,
159 	       regs->gprs[4], regs->gprs[5], regs->gprs[6], regs->gprs[7]);
160 	printk("           " FOURLONG,
161 	       regs->gprs[8], regs->gprs[9], regs->gprs[10], regs->gprs[11]);
162 	printk("           " FOURLONG,
163 	       regs->gprs[12], regs->gprs[13], regs->gprs[14], regs->gprs[15]);
164 	show_code(regs);
165 }
166 
167 void show_regs(struct pt_regs *regs)
168 {
169 	show_regs_print_info(KERN_DEFAULT);
170 	show_registers(regs);
171 	/* Show stack backtrace if pt_regs is from kernel mode */
172 	if (!user_mode(regs))
173 		show_trace(NULL, (unsigned long *) regs->gprs[15]);
174 	show_last_breaking_event(regs);
175 }
176 
177 static DEFINE_SPINLOCK(die_lock);
178 
179 void die(struct pt_regs *regs, const char *str)
180 {
181 	static int die_counter;
182 
183 	oops_enter();
184 	lgr_info_log();
185 	debug_stop_all();
186 	console_verbose();
187 	spin_lock_irq(&die_lock);
188 	bust_spinlocks(1);
189 	printk("%s: %04x [#%d] ", str, regs->int_code & 0xffff, ++die_counter);
190 #ifdef CONFIG_PREEMPT
191 	printk("PREEMPT ");
192 #endif
193 #ifdef CONFIG_SMP
194 	printk("SMP ");
195 #endif
196 #ifdef CONFIG_DEBUG_PAGEALLOC
197 	printk("DEBUG_PAGEALLOC");
198 #endif
199 	printk("\n");
200 	notify_die(DIE_OOPS, str, regs, 0, regs->int_code & 0xffff, SIGSEGV);
201 	print_modules();
202 	show_regs(regs);
203 	bust_spinlocks(0);
204 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
205 	spin_unlock_irq(&die_lock);
206 	if (in_interrupt())
207 		panic("Fatal exception in interrupt");
208 	if (panic_on_oops)
209 		panic("Fatal exception: panic_on_oops");
210 	oops_exit();
211 	do_exit(SIGSEGV);
212 }
213