4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
8 * Modifications for the OpenRISC architecture:
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.
17 * Here we handle the break vectors not used by the system call
18 * mechanism, as well as some general stack/register dumping
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>
35 #include <linux/kallsyms.h>
36 #include <linux/uaccess.h>
38 #include <asm/segment.h>
40 #include <asm/pgtable.h>
42 extern char _etext, _stext;
44 int kstack_depth_to_print = 0x180;
46 unsigned long __user *lwa_addr;
48 static inline int valid_stack_ptr(struct thread_info *tinfo, void *p)
50 return p > (void *)tinfo && p < (void *)tinfo + THREAD_SIZE - 3;
53 void show_trace(struct task_struct *task, unsigned long *stack)
55 struct thread_info *context;
58 context = (struct thread_info *)
59 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
61 while (valid_stack_ptr(context, stack)) {
63 if (__kernel_text_address(addr)) {
64 printk(" [<%08lx>]", addr);
65 print_symbol(" %s", addr);
69 printk(" =======================\n");
72 /* displays a short stack trace */
73 void show_stack(struct task_struct *task, unsigned long *esp)
75 unsigned long addr, *stack;
79 esp = (unsigned long *)&esp;
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))
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);
96 printk("sp + %02d: 0x%08lx\n", i * 4, addr);
100 show_trace(task, esp);
105 void show_trace_task(struct task_struct *tsk)
108 * TODO: SysRq-T trace dump...
112 void show_registers(struct pt_regs *regs)
118 esp = (unsigned long)(®s->sp);
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);
144 printk("Process %s (pid: %d, stackpage=%08lx)\n",
145 current->comm, current->pid, (unsigned long)current);
147 * When in-kernel, we also print out the stack and code at the
148 * time of the fault..
153 show_stack(NULL, (unsigned long *)esp);
156 if (regs->pc < PAGE_OFFSET)
159 for (i = -24; i < 24; i++) {
161 if (__get_user(c, &((unsigned char *)regs->pc)[i])) {
163 printk(" Bad PC value.");
168 printk("(%02x) ", c);
176 void nommu_dump_state(struct pt_regs *regs,
177 unsigned long ea, unsigned long vector)
180 unsigned long addr, stack = regs->sp;
182 printk("\n\r[nommu_dump_state] :: ea %lx, vector %lx\n\r", ea, vector);
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);
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);
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)
218 printk("%lx :: sp + %02d: 0x%08lx\n", stack, i * 4,
219 *((unsigned long *)(__pa(stack))));
223 printk("Call Trace: ");
225 while (((long)stack & (THREAD_SIZE - 1)) != 0) {
226 addr = *((unsigned long *)__pa(stack));
229 if (kernel_text_address(addr)) {
230 if (i && ((i % 6) == 0))
232 printk(" [<%08lx>]", addr);
240 for (i = -24; i < 24; i++) {
242 c = ((unsigned char *)(__pa(regs->pc)))[i];
245 printk("(%02x) ", c);
252 /* This is normally the 'Oops' routine */
253 void die(const char *str, struct pt_regs *regs, long err)
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");
262 /* shut down interrupts */
265 __asm__ __volatile__("l.nop 1");
271 /* This is normally the 'Oops' routine */
272 void die_if_kernel(const char *str, struct pt_regs *regs, long err)
280 void unhandled_exception(struct pt_regs *regs, int ea, int vector)
282 printk("Unable to handle exception at EA =0x%x, vector 0x%x",
284 die("Oops", regs, 9);
287 void __init trap_init(void)
289 /* Nothing needs to be done */
292 asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
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);
304 asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
308 if (user_mode(regs)) {
310 info.si_signo = SIGSEGV;
312 /* info.si_code has been set above */
313 info.si_addr = (void *)address;
314 force_sig_info(SIGSEGV, &info, current);
316 printk("KERNEL: Unaligned Access 0x%.8lx\n", address);
317 show_registers(regs);
318 die("Die:", regs, address);
323 asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address)
327 if (user_mode(regs)) {
329 info.si_signo = SIGBUS;
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);
341 static inline int in_delay_slot(struct pt_regs *regs)
343 #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
344 /* No delay slot flag, do the old way */
345 unsigned int op, insn;
347 insn = *((unsigned int *)regs->pc);
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 */
361 return regs->sr & SPR_SR_DSX;
365 static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
368 unsigned int rb, op, jmp;
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);
374 displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
375 rb = (jmp & 0x0000ffff) >> 11;
380 regs->pc += displacement;
382 case 0x01: /* l.jal */
383 regs->pc += displacement;
384 regs->gpr[9] = regs->pc + 8;
386 case 0x03: /* l.bnf */
387 if (regs->sr & SPR_SR_F)
390 regs->pc += displacement;
392 case 0x04: /* l.bf */
393 if (regs->sr & SPR_SR_F)
394 regs->pc += displacement;
398 case 0x11: /* l.jr */
399 regs->pc = regs->gpr[rb];
401 case 0x12: /* l.jalr */
402 regs->pc = regs->gpr[rb];
403 regs->gpr[9] = regs->pc + 8;
413 static inline void simulate_lwa(struct pt_regs *regs, unsigned long address,
418 unsigned long orig_pc;
421 const struct exception_table_entry *entry;
424 adjust_pc(regs, address);
426 ra = (insn >> 16) & 0x1f;
427 rd = (insn >> 21) & 0x1f;
429 lwa_addr = (unsigned long __user *)(regs->gpr[ra] + imm);
431 if ((unsigned long)lwa_addr & 0x3) {
432 do_unaligned_access(regs, address);
436 if (get_user(value, lwa_addr)) {
437 if (user_mode(regs)) {
438 force_sig(SIGSEGV, current);
442 if ((entry = search_exception_tables(orig_pc))) {
443 regs->pc = entry->fixup;
447 /* kernel access in kernel space, load it directly */
448 value = *((unsigned long *)lwa_addr);
452 regs->gpr[rd] = value;
455 static inline void simulate_swa(struct pt_regs *regs, unsigned long address,
458 unsigned long __user *vaddr;
459 unsigned long orig_pc;
463 const struct exception_table_entry *entry;
466 adjust_pc(regs, address);
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);
473 if (!lwa_flag || vaddr != lwa_addr) {
474 regs->sr &= ~SPR_SR_F;
478 if ((unsigned long)vaddr & 0x3) {
479 do_unaligned_access(regs, address);
483 if (put_user(regs->gpr[rb], vaddr)) {
484 if (user_mode(regs)) {
485 force_sig(SIGSEGV, current);
489 if ((entry = search_exception_tables(orig_pc))) {
490 regs->pc = entry->fixup;
494 /* kernel access in kernel space, store it directly */
495 *((unsigned long *)vaddr) = regs->gpr[rb];
499 regs->sr |= SPR_SR_F;
502 #define INSN_LWA 0x1b
503 #define INSN_SWA 0x33
505 asmlinkage void do_illegal_instruction(struct pt_regs *regs,
506 unsigned long address)
510 unsigned int insn = *((unsigned int *)address);
516 simulate_lwa(regs, address, insn);
520 simulate_swa(regs, address, insn);
527 if (user_mode(regs)) {
529 info.si_signo = SIGILL;
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",
537 show_registers(regs);
538 die("Die:", regs, address);