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