Merge branch 'i2c-mux/for-current' of https://github.com/peda-r/i2c-mux into i2c...
[sfrench/cifs-2.6.git] / arch / openrisc / kernel / traps.c
1 /*
2  * OpenRISC traps.c
3  *
4  * Linux architectural port borrowing liberally from similar works of
5  * others.  All original copyrights apply as per the original source
6  * declaration.
7  *
8  * Modifications for the OpenRISC architecture:
9  * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10  * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  *
17  *  Here we handle the break vectors not used by the system call
18  *  mechanism, as well as some general stack/register dumping
19  *  things.
20  *
21  */
22
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/sched/debug.h>
26 #include <linux/sched/task_stack.h>
27 #include <linux/kernel.h>
28 #include <linux/extable.h>
29 #include <linux/kmod.h>
30 #include <linux/string.h>
31 #include <linux/errno.h>
32 #include <linux/ptrace.h>
33 #include <linux/timer.h>
34 #include <linux/mm.h>
35 #include <linux/kallsyms.h>
36 #include <linux/uaccess.h>
37
38 #include <asm/segment.h>
39 #include <asm/io.h>
40 #include <asm/pgtable.h>
41
42 extern char _etext, _stext;
43
44 int kstack_depth_to_print = 0x180;
45 int lwa_flag;
46 unsigned long __user *lwa_addr;
47
48 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
49 {
50         return p > (void *)tinfo && p < (void *)tinfo + THREAD_SIZE - 3;
51 }
52
53 void show_trace(struct task_struct *task, unsigned long *stack)
54 {
55         struct thread_info *context;
56         unsigned long addr;
57
58         context = (struct thread_info *)
59             ((unsigned long)stack & (~(THREAD_SIZE - 1)));
60
61         while (valid_stack_ptr(context, stack)) {
62                 addr = *stack++;
63                 if (__kernel_text_address(addr)) {
64                         printk(" [<%08lx>]", addr);
65                         print_symbol(" %s", addr);
66                         printk("\n");
67                 }
68         }
69         printk(" =======================\n");
70 }
71
72 /* displays a short stack trace */
73 void show_stack(struct task_struct *task, unsigned long *esp)
74 {
75         unsigned long addr, *stack;
76         int i;
77
78         if (esp == NULL)
79                 esp = (unsigned long *)&esp;
80
81         stack = esp;
82
83         printk("Stack dump [0x%08lx]:\n", (unsigned long)esp);
84         for (i = 0; i < kstack_depth_to_print; i++) {
85                 if (kstack_end(stack))
86                         break;
87                 if (__get_user(addr, stack)) {
88                         /* This message matches "failing address" marked
89                            s390 in ksymoops, so lines containing it will
90                            not be filtered out by ksymoops.  */
91                         printk("Failing address 0x%lx\n", (unsigned long)stack);
92                         break;
93                 }
94                 stack++;
95
96                 printk("sp + %02d: 0x%08lx\n", i * 4, addr);
97         }
98         printk("\n");
99
100         show_trace(task, esp);
101
102         return;
103 }
104
105 void show_trace_task(struct task_struct *tsk)
106 {
107         /*
108          * TODO: SysRq-T trace dump...
109          */
110 }
111
112 void show_registers(struct pt_regs *regs)
113 {
114         int i;
115         int in_kernel = 1;
116         unsigned long esp;
117
118         esp = (unsigned long)(&regs->sp);
119         if (user_mode(regs))
120                 in_kernel = 0;
121
122         printk("CPU #: %d\n"
123                "   PC: %08lx    SR: %08lx    SP: %08lx\n",
124                smp_processor_id(), regs->pc, regs->sr, regs->sp);
125         printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
126                0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
127         printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
128                regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
129         printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
130                regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
131         printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
132                regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
133         printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
134                regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
135         printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
136                regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
137         printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
138                regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
139         printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
140                regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
141         printk("  RES: %08lx oGPR11: %08lx\n",
142                regs->gpr[11], regs->orig_gpr11);
143
144         printk("Process %s (pid: %d, stackpage=%08lx)\n",
145                current->comm, current->pid, (unsigned long)current);
146         /*
147          * When in-kernel, we also print out the stack and code at the
148          * time of the fault..
149          */
150         if (in_kernel) {
151
152                 printk("\nStack: ");
153                 show_stack(NULL, (unsigned long *)esp);
154
155                 printk("\nCode: ");
156                 if (regs->pc < PAGE_OFFSET)
157                         goto bad;
158
159                 for (i = -24; i < 24; i++) {
160                         unsigned char c;
161                         if (__get_user(c, &((unsigned char *)regs->pc)[i])) {
162 bad:
163                                 printk(" Bad PC value.");
164                                 break;
165                         }
166
167                         if (i == 0)
168                                 printk("(%02x) ", c);
169                         else
170                                 printk("%02x ", c);
171                 }
172         }
173         printk("\n");
174 }
175
176 void nommu_dump_state(struct pt_regs *regs,
177                       unsigned long ea, unsigned long vector)
178 {
179         int i;
180         unsigned long addr, stack = regs->sp;
181
182         printk("\n\r[nommu_dump_state] :: ea %lx, vector %lx\n\r", ea, vector);
183
184         printk("CPU #: %d\n"
185                "   PC: %08lx    SR: %08lx    SP: %08lx\n",
186                0, regs->pc, regs->sr, regs->sp);
187         printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
188                0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
189         printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
190                regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
191         printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
192                regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
193         printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
194                regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
195         printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
196                regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
197         printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
198                regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
199         printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
200                regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
201         printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
202                regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
203         printk("  RES: %08lx oGPR11: %08lx\n",
204                regs->gpr[11], regs->orig_gpr11);
205
206         printk("Process %s (pid: %d, stackpage=%08lx)\n",
207                ((struct task_struct *)(__pa(current)))->comm,
208                ((struct task_struct *)(__pa(current)))->pid,
209                (unsigned long)current);
210
211         printk("\nStack: ");
212         printk("Stack dump [0x%08lx]:\n", (unsigned long)stack);
213         for (i = 0; i < kstack_depth_to_print; i++) {
214                 if (((long)stack & (THREAD_SIZE - 1)) == 0)
215                         break;
216                 stack++;
217
218                 printk("%lx :: sp + %02d: 0x%08lx\n", stack, i * 4,
219                        *((unsigned long *)(__pa(stack))));
220         }
221         printk("\n");
222
223         printk("Call Trace:   ");
224         i = 1;
225         while (((long)stack & (THREAD_SIZE - 1)) != 0) {
226                 addr = *((unsigned long *)__pa(stack));
227                 stack++;
228
229                 if (kernel_text_address(addr)) {
230                         if (i && ((i % 6) == 0))
231                                 printk("\n ");
232                         printk(" [<%08lx>]", addr);
233                         i++;
234                 }
235         }
236         printk("\n");
237
238         printk("\nCode: ");
239
240         for (i = -24; i < 24; i++) {
241                 unsigned char c;
242                 c = ((unsigned char *)(__pa(regs->pc)))[i];
243
244                 if (i == 0)
245                         printk("(%02x) ", c);
246                 else
247                         printk("%02x ", c);
248         }
249         printk("\n");
250 }
251
252 /* This is normally the 'Oops' routine */
253 void die(const char *str, struct pt_regs *regs, long err)
254 {
255
256         console_verbose();
257         printk("\n%s#: %04lx\n", str, err & 0xffff);
258         show_registers(regs);
259 #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
260         printk("\n\nUNHANDLED_EXCEPTION: entering infinite loop\n");
261
262         /* shut down interrupts */
263         local_irq_disable();
264
265         __asm__ __volatile__("l.nop   1");
266         do {} while (1);
267 #endif
268         do_exit(SIGSEGV);
269 }
270
271 /* This is normally the 'Oops' routine */
272 void die_if_kernel(const char *str, struct pt_regs *regs, long err)
273 {
274         if (user_mode(regs))
275                 return;
276
277         die(str, regs, err);
278 }
279
280 void unhandled_exception(struct pt_regs *regs, int ea, int vector)
281 {
282         printk("Unable to handle exception at EA =0x%x, vector 0x%x",
283                ea, vector);
284         die("Oops", regs, 9);
285 }
286
287 void __init trap_init(void)
288 {
289         /* Nothing needs to be done */
290 }
291
292 asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
293 {
294         siginfo_t info;
295         memset(&info, 0, sizeof(info));
296         info.si_signo = SIGTRAP;
297         info.si_code = TRAP_TRACE;
298         info.si_addr = (void *)address;
299         force_sig_info(SIGTRAP, &info, current);
300
301         regs->pc += 4;
302 }
303
304 asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
305 {
306         siginfo_t info;
307
308         if (user_mode(regs)) {
309                 /* Send a SIGSEGV */
310                 info.si_signo = SIGSEGV;
311                 info.si_errno = 0;
312                 /* info.si_code has been set above */
313                 info.si_addr = (void *)address;
314                 force_sig_info(SIGSEGV, &info, current);
315         } else {
316                 printk("KERNEL: Unaligned Access 0x%.8lx\n", address);
317                 show_registers(regs);
318                 die("Die:", regs, address);
319         }
320
321 }
322
323 asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address)
324 {
325         siginfo_t info;
326
327         if (user_mode(regs)) {
328                 /* Send a SIGBUS */
329                 info.si_signo = SIGBUS;
330                 info.si_errno = 0;
331                 info.si_code = BUS_ADRERR;
332                 info.si_addr = (void *)address;
333                 force_sig_info(SIGBUS, &info, current);
334         } else {                /* Kernel mode */
335                 printk("KERNEL: Bus error (SIGBUS) 0x%.8lx\n", address);
336                 show_registers(regs);
337                 die("Die:", regs, address);
338         }
339 }
340
341 static inline int in_delay_slot(struct pt_regs *regs)
342 {
343 #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
344         /* No delay slot flag, do the old way */
345         unsigned int op, insn;
346
347         insn = *((unsigned int *)regs->pc);
348         op = insn >> 26;
349         switch (op) {
350         case 0x00: /* l.j */
351         case 0x01: /* l.jal */
352         case 0x03: /* l.bnf */
353         case 0x04: /* l.bf */
354         case 0x11: /* l.jr */
355         case 0x12: /* l.jalr */
356                 return 1;
357         default:
358                 return 0;
359         }
360 #else
361         return regs->sr & SPR_SR_DSX;
362 #endif
363 }
364
365 static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
366 {
367         int displacement;
368         unsigned int rb, op, jmp;
369
370         if (unlikely(in_delay_slot(regs))) {
371                 /* In delay slot, instruction at pc is a branch, simulate it */
372                 jmp = *((unsigned int *)regs->pc);
373
374                 displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
375                 rb = (jmp & 0x0000ffff) >> 11;
376                 op = jmp >> 26;
377
378                 switch (op) {
379                 case 0x00: /* l.j */
380                         regs->pc += displacement;
381                         return;
382                 case 0x01: /* l.jal */
383                         regs->pc += displacement;
384                         regs->gpr[9] = regs->pc + 8;
385                         return;
386                 case 0x03: /* l.bnf */
387                         if (regs->sr & SPR_SR_F)
388                                 regs->pc += 8;
389                         else
390                                 regs->pc += displacement;
391                         return;
392                 case 0x04: /* l.bf */
393                         if (regs->sr & SPR_SR_F)
394                                 regs->pc += displacement;
395                         else
396                                 regs->pc += 8;
397                         return;
398                 case 0x11: /* l.jr */
399                         regs->pc = regs->gpr[rb];
400                         return;
401                 case 0x12: /* l.jalr */
402                         regs->pc = regs->gpr[rb];
403                         regs->gpr[9] = regs->pc + 8;
404                         return;
405                 default:
406                         break;
407                 }
408         } else {
409                 regs->pc += 4;
410         }
411 }
412
413 static inline void simulate_lwa(struct pt_regs *regs, unsigned long address,
414                                 unsigned int insn)
415 {
416         unsigned int ra, rd;
417         unsigned long value;
418         unsigned long orig_pc;
419         long imm;
420
421         const struct exception_table_entry *entry;
422
423         orig_pc = regs->pc;
424         adjust_pc(regs, address);
425
426         ra = (insn >> 16) & 0x1f;
427         rd = (insn >> 21) & 0x1f;
428         imm = (short)insn;
429         lwa_addr = (unsigned long __user *)(regs->gpr[ra] + imm);
430
431         if ((unsigned long)lwa_addr & 0x3) {
432                 do_unaligned_access(regs, address);
433                 return;
434         }
435
436         if (get_user(value, lwa_addr)) {
437                 if (user_mode(regs)) {
438                         force_sig(SIGSEGV, current);
439                         return;
440                 }
441
442                 if ((entry = search_exception_tables(orig_pc))) {
443                         regs->pc = entry->fixup;
444                         return;
445                 }
446
447                 /* kernel access in kernel space, load it directly */
448                 value = *((unsigned long *)lwa_addr);
449         }
450
451         lwa_flag = 1;
452         regs->gpr[rd] = value;
453 }
454
455 static inline void simulate_swa(struct pt_regs *regs, unsigned long address,
456                                 unsigned int insn)
457 {
458         unsigned long __user *vaddr;
459         unsigned long orig_pc;
460         unsigned int ra, rb;
461         long imm;
462
463         const struct exception_table_entry *entry;
464
465         orig_pc = regs->pc;
466         adjust_pc(regs, address);
467
468         ra = (insn >> 16) & 0x1f;
469         rb = (insn >> 11) & 0x1f;
470         imm = (short)(((insn & 0x2200000) >> 10) | (insn & 0x7ff));
471         vaddr = (unsigned long __user *)(regs->gpr[ra] + imm);
472
473         if (!lwa_flag || vaddr != lwa_addr) {
474                 regs->sr &= ~SPR_SR_F;
475                 return;
476         }
477
478         if ((unsigned long)vaddr & 0x3) {
479                 do_unaligned_access(regs, address);
480                 return;
481         }
482
483         if (put_user(regs->gpr[rb], vaddr)) {
484                 if (user_mode(regs)) {
485                         force_sig(SIGSEGV, current);
486                         return;
487                 }
488
489                 if ((entry = search_exception_tables(orig_pc))) {
490                         regs->pc = entry->fixup;
491                         return;
492                 }
493
494                 /* kernel access in kernel space, store it directly */
495                 *((unsigned long *)vaddr) = regs->gpr[rb];
496         }
497
498         lwa_flag = 0;
499         regs->sr |= SPR_SR_F;
500 }
501
502 #define INSN_LWA        0x1b
503 #define INSN_SWA        0x33
504
505 asmlinkage void do_illegal_instruction(struct pt_regs *regs,
506                                        unsigned long address)
507 {
508         siginfo_t info;
509         unsigned int op;
510         unsigned int insn = *((unsigned int *)address);
511
512         op = insn >> 26;
513
514         switch (op) {
515         case INSN_LWA:
516                 simulate_lwa(regs, address, insn);
517                 return;
518
519         case INSN_SWA:
520                 simulate_swa(regs, address, insn);
521                 return;
522
523         default:
524                 break;
525         }
526
527         if (user_mode(regs)) {
528                 /* Send a SIGILL */
529                 info.si_signo = SIGILL;
530                 info.si_errno = 0;
531                 info.si_code = ILL_ILLOPC;
532                 info.si_addr = (void *)address;
533                 force_sig_info(SIGBUS, &info, current);
534         } else {                /* Kernel mode */
535                 printk("KERNEL: Illegal instruction (SIGILL) 0x%.8lx\n",
536                        address);
537                 show_registers(regs);
538                 die("Die:", regs, address);
539         }
540 }