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