blob: 72c584d62f593d0d63eb737576ff3f286827f6d6 [file] [log] [blame]
Heiko Carstens1bca09f2013-03-14 13:44:25 +01001/*
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>
Christian Borntraeger10917b82016-03-15 14:57:36 -070014#include <linux/mm.h>
Heiko Carstens1bca09f2013-03-14 13:44:25 +010015#include <linux/module.h>
16#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010017#include <linux/sched/debug.h>
Heiko Carstens1bca09f2013-03-14 13:44:25 +010018#include <asm/processor.h>
19#include <asm/debug.h>
Heiko Carstens0f208222013-09-13 13:36:25 +020020#include <asm/dis.h>
Heiko Carstens1bca09f2013-03-14 13:44:25 +010021#include <asm/ipl.h>
22
Heiko Carstens1bca09f2013-03-14 13:44:25 +010023/*
Heiko Carstens758d39e2016-02-09 12:58:54 +010024 * For dump_trace we have tree different stack to consider:
Heiko Carstens1bca09f2013-03-14 13:44:25 +010025 * - the panic stack which is used if the kernel stack has overflown
26 * - the asynchronous interrupt stack (cpu related)
27 * - the synchronous kernel stack (process related)
Heiko Carstens758d39e2016-02-09 12:58:54 +010028 * The stack trace can start at any of the three stacks and can potentially
Heiko Carstens1bca09f2013-03-14 13:44:25 +010029 * touch all of them. The order is: panic stack, async stack, sync stack.
30 */
31static unsigned long
Heiko Carstens758d39e2016-02-09 12:58:54 +010032__dump_trace(dump_trace_func_t func, void *data, unsigned long sp,
33 unsigned long low, unsigned long high)
Heiko Carstens1bca09f2013-03-14 13:44:25 +010034{
35 struct stack_frame *sf;
36 struct pt_regs *regs;
37
38 while (1) {
Heiko Carstens1bca09f2013-03-14 13:44:25 +010039 if (sp < low || sp > high - sizeof(*sf))
40 return sp;
41 sf = (struct stack_frame *) sp;
Heiko Carstensd0208632016-10-17 11:08:31 +020042 if (func(data, sf->gprs[8], 0))
43 return sp;
Heiko Carstens1bca09f2013-03-14 13:44:25 +010044 /* Follow the backchain. */
45 while (1) {
46 low = sp;
Heiko Carstens9cb1cce2016-01-18 13:12:19 +010047 sp = sf->back_chain;
Heiko Carstens1bca09f2013-03-14 13:44:25 +010048 if (!sp)
49 break;
50 if (sp <= low || sp > high - sizeof(*sf))
51 return sp;
52 sf = (struct stack_frame *) sp;
Heiko Carstensd0208632016-10-17 11:08:31 +020053 if (func(data, sf->gprs[8], 1))
54 return sp;
Heiko Carstens1bca09f2013-03-14 13:44:25 +010055 }
56 /* Zero backchain detected, check for interrupt frame. */
57 sp = (unsigned long) (sf + 1);
58 if (sp <= low || sp > high - sizeof(*regs))
59 return sp;
60 regs = (struct pt_regs *) sp;
Heiko Carstens758d39e2016-02-09 12:58:54 +010061 if (!user_mode(regs)) {
Heiko Carstensd0208632016-10-17 11:08:31 +020062 if (func(data, regs->psw.addr, 1))
Heiko Carstens758d39e2016-02-09 12:58:54 +010063 return sp;
64 }
Heiko Carstens1bca09f2013-03-14 13:44:25 +010065 low = sp;
66 sp = regs->gprs[15];
67 }
68}
69
Heiko Carstens758d39e2016-02-09 12:58:54 +010070void dump_trace(dump_trace_func_t func, void *data, struct task_struct *task,
71 unsigned long sp)
Heiko Carstens1bca09f2013-03-14 13:44:25 +010072{
Heiko Carstens758d39e2016-02-09 12:58:54 +010073 unsigned long frame_size;
Heiko Carstens1bca09f2013-03-14 13:44:25 +010074
Heiko Carstens758d39e2016-02-09 12:58:54 +010075 frame_size = STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
Heiko Carstens1bca09f2013-03-14 13:44:25 +010076#ifdef CONFIG_CHECK_STACK
Heiko Carstens758d39e2016-02-09 12:58:54 +010077 sp = __dump_trace(func, data, sp,
Martin Schwidefsky9cc5c202013-06-04 11:00:08 +020078 S390_lowcore.panic_stack + frame_size - 4096,
79 S390_lowcore.panic_stack + frame_size);
Heiko Carstens1bca09f2013-03-14 13:44:25 +010080#endif
Heiko Carstens758d39e2016-02-09 12:58:54 +010081 sp = __dump_trace(func, data, sp,
Martin Schwidefsky9cc5c202013-06-04 11:00:08 +020082 S390_lowcore.async_stack + frame_size - ASYNC_SIZE,
83 S390_lowcore.async_stack + frame_size);
Heiko Carstensc3bdf2e2016-06-27 07:57:21 +020084 task = task ?: current;
85 __dump_trace(func, data, sp,
86 (unsigned long)task_stack_page(task),
87 (unsigned long)task_stack_page(task) + THREAD_SIZE);
Heiko Carstens758d39e2016-02-09 12:58:54 +010088}
89EXPORT_SYMBOL_GPL(dump_trace);
90
Heiko Carstensd0208632016-10-17 11:08:31 +020091static int show_address(void *data, unsigned long address, int reliable)
Heiko Carstens758d39e2016-02-09 12:58:54 +010092{
Heiko Carstensd0208632016-10-17 11:08:31 +020093 if (reliable)
94 printk(" [<%016lx>] %pSR \n", address, (void *)address);
95 else
96 printk("([<%016lx>] %pSR)\n", address, (void *)address);
Heiko Carstens758d39e2016-02-09 12:58:54 +010097 return 0;
98}
99
100static void show_trace(struct task_struct *task, unsigned long sp)
101{
102 if (!sp)
103 sp = task ? task->thread.ksp : current_stack_pointer();
104 printk("Call Trace:\n");
105 dump_trace(show_address, NULL, task, sp);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100106 if (!task)
107 task = current;
108 debug_show_held_locks(task);
109}
110
111void show_stack(struct task_struct *task, unsigned long *sp)
112{
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100113 unsigned long *stack;
114 int i;
115
Heiko Carstens76737ce2016-01-31 17:06:16 +0100116 stack = sp;
117 if (!stack) {
118 if (!task)
119 stack = (unsigned long *)current_stack_pointer();
120 else
121 stack = (unsigned long *)task->thread.ksp;
122 }
Heiko Carstens47ece7f2016-10-19 13:42:55 +0200123 printk(KERN_DEFAULT "Stack:\n");
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100124 for (i = 0; i < 20; i++) {
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100125 if (((addr_t) stack & (THREAD_SIZE-1)) == 0)
126 break;
Heiko Carstens47ece7f2016-10-19 13:42:55 +0200127 if (i % 4 == 0)
128 printk(KERN_DEFAULT " ");
129 pr_cont("%016lx%c", *stack++, i % 4 == 3 ? '\n' : ' ');
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100130 }
Heiko Carstens758d39e2016-02-09 12:58:54 +0100131 show_trace(task, (unsigned long)sp);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100132}
133
134static void show_last_breaking_event(struct pt_regs *regs)
135{
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100136 printk("Last Breaking-Event-Address:\n");
Heiko Carstens8237ac32013-09-02 11:20:00 +0200137 printk(" [<%016lx>] %pSR\n", regs->args[0], (void *)regs->args[0]);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100138}
139
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100140void show_registers(struct pt_regs *regs)
141{
Heiko Carstens1f021ea2016-01-31 17:36:59 +0100142 struct psw_bits *psw = &psw_bits(regs->psw);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100143 char *mode;
144
145 mode = user_mode(regs) ? "User" : "Krnl";
Heiko Carstens9ea80662014-04-07 16:26:37 +0200146 printk("%s PSW : %p %p", mode, (void *)regs->psw.mask, (void *)regs->psw.addr);
147 if (!user_mode(regs))
Heiko Carstensa7906342016-10-17 11:59:58 +0200148 pr_cont(" (%pSR)", (void *)regs->psw.addr);
149 pr_cont("\n");
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100150 printk(" R:%x T:%x IO:%x EX:%x Key:%x M:%x W:%x "
Heiko Carstens1f021ea2016-01-31 17:36:59 +0100151 "P:%x AS:%x CC:%x PM:%x", psw->r, psw->t, psw->i, psw->e,
152 psw->key, psw->m, psw->w, psw->p, psw->as, psw->cc, psw->pm);
Heiko Carstensa7906342016-10-17 11:59:58 +0200153 pr_cont(" RI:%x EA:%x\n", psw->ri, psw->eaba);
154 printk("%s GPRS: %016lx %016lx %016lx %016lx\n", mode,
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100155 regs->gprs[0], regs->gprs[1], regs->gprs[2], regs->gprs[3]);
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100156 printk(" %016lx %016lx %016lx %016lx\n",
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100157 regs->gprs[4], regs->gprs[5], regs->gprs[6], regs->gprs[7]);
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100158 printk(" %016lx %016lx %016lx %016lx\n",
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100159 regs->gprs[8], regs->gprs[9], regs->gprs[10], regs->gprs[11]);
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100160 printk(" %016lx %016lx %016lx %016lx\n",
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100161 regs->gprs[12], regs->gprs[13], regs->gprs[14], regs->gprs[15]);
162 show_code(regs);
163}
164
165void show_regs(struct pt_regs *regs)
166{
Tejun Heoa43cb952013-04-30 15:27:17 -0700167 show_regs_print_info(KERN_DEFAULT);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100168 show_registers(regs);
169 /* Show stack backtrace if pt_regs is from kernel mode */
170 if (!user_mode(regs))
Heiko Carstens758d39e2016-02-09 12:58:54 +0100171 show_trace(NULL, regs->gprs[15]);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100172 show_last_breaking_event(regs);
173}
174
175static DEFINE_SPINLOCK(die_lock);
176
177void die(struct pt_regs *regs, const char *str)
178{
179 static int die_counter;
180
181 oops_enter();
182 lgr_info_log();
183 debug_stop_all();
184 console_verbose();
185 spin_lock_irq(&die_lock);
186 bust_spinlocks(1);
Heiko Carstens413d4042014-11-19 13:31:08 +0100187 printk("%s: %04x ilc:%d [#%d] ", str, regs->int_code & 0xffff,
188 regs->int_code >> 17, ++die_counter);
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100189#ifdef CONFIG_PREEMPT
Heiko Carstens47ece7f2016-10-19 13:42:55 +0200190 pr_cont("PREEMPT ");
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100191#endif
192#ifdef CONFIG_SMP
Heiko Carstens47ece7f2016-10-19 13:42:55 +0200193 pr_cont("SMP ");
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100194#endif
Christian Borntraeger10917b82016-03-15 14:57:36 -0700195 if (debug_pagealloc_enabled())
Heiko Carstens47ece7f2016-10-19 13:42:55 +0200196 pr_cont("DEBUG_PAGEALLOC");
197 pr_cont("\n");
Heiko Carstens1bca09f2013-03-14 13:44:25 +0100198 notify_die(DIE_OOPS, str, regs, 0, regs->int_code & 0xffff, SIGSEGV);
199 print_modules();
200 show_regs(regs);
201 bust_spinlocks(0);
202 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
203 spin_unlock_irq(&die_lock);
204 if (in_interrupt())
205 panic("Fatal exception in interrupt");
206 if (panic_on_oops)
207 panic("Fatal exception: panic_on_oops");
208 oops_exit();
209 do_exit(SIGSEGV);
210}