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