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