traps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/sched/debug.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <linux/kernel.h>
  27. #include <linux/extable.h>
  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>
  35. #include <linux/uaccess.h>
  36. #include <asm/segment.h>
  37. #include <asm/io.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/unwinder.h>
  40. #include <asm/sections.h>
  41. int kstack_depth_to_print = 0x180;
  42. int lwa_flag;
  43. unsigned long __user *lwa_addr;
  44. void print_trace(void *data, unsigned long addr, int reliable)
  45. {
  46. pr_emerg("[<%p>] %s%pS\n", (void *) addr, reliable ? "" : "? ",
  47. (void *) addr);
  48. }
  49. /* displays a short stack trace */
  50. void show_stack(struct task_struct *task, unsigned long *esp)
  51. {
  52. if (esp == NULL)
  53. esp = (unsigned long *)&esp;
  54. pr_emerg("Call trace:\n");
  55. unwind_stack(NULL, esp, print_trace);
  56. }
  57. void show_trace_task(struct task_struct *tsk)
  58. {
  59. /*
  60. * TODO: SysRq-T trace dump...
  61. */
  62. }
  63. void show_registers(struct pt_regs *regs)
  64. {
  65. int i;
  66. int in_kernel = 1;
  67. unsigned long esp;
  68. esp = (unsigned long)(regs->sp);
  69. if (user_mode(regs))
  70. in_kernel = 0;
  71. printk("CPU #: %d\n"
  72. " PC: %08lx SR: %08lx SP: %08lx\n",
  73. smp_processor_id(), regs->pc, regs->sr, regs->sp);
  74. printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
  75. 0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
  76. printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
  77. regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
  78. printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
  79. regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
  80. printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
  81. regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
  82. printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
  83. regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
  84. printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
  85. regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
  86. printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
  87. regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
  88. printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
  89. regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
  90. printk(" RES: %08lx oGPR11: %08lx\n",
  91. regs->gpr[11], regs->orig_gpr11);
  92. printk("Process %s (pid: %d, stackpage=%08lx)\n",
  93. current->comm, current->pid, (unsigned long)current);
  94. /*
  95. * When in-kernel, we also print out the stack and code at the
  96. * time of the fault..
  97. */
  98. if (in_kernel) {
  99. printk("\nStack: ");
  100. show_stack(NULL, (unsigned long *)esp);
  101. printk("\nCode: ");
  102. if (regs->pc < PAGE_OFFSET)
  103. goto bad;
  104. for (i = -24; i < 24; i++) {
  105. unsigned char c;
  106. if (__get_user(c, &((unsigned char *)regs->pc)[i])) {
  107. bad:
  108. printk(" Bad PC value.");
  109. break;
  110. }
  111. if (i == 0)
  112. printk("(%02x) ", c);
  113. else
  114. printk("%02x ", c);
  115. }
  116. }
  117. printk("\n");
  118. }
  119. void nommu_dump_state(struct pt_regs *regs,
  120. unsigned long ea, unsigned long vector)
  121. {
  122. int i;
  123. unsigned long addr, stack = regs->sp;
  124. printk("\n\r[nommu_dump_state] :: ea %lx, vector %lx\n\r", ea, vector);
  125. printk("CPU #: %d\n"
  126. " PC: %08lx SR: %08lx SP: %08lx\n",
  127. 0, regs->pc, regs->sr, regs->sp);
  128. printk("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
  129. 0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
  130. printk("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
  131. regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
  132. printk("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
  133. regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
  134. printk("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
  135. regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
  136. printk("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
  137. regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
  138. printk("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
  139. regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
  140. printk("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
  141. regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
  142. printk("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
  143. regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
  144. printk(" RES: %08lx oGPR11: %08lx\n",
  145. regs->gpr[11], regs->orig_gpr11);
  146. printk("Process %s (pid: %d, stackpage=%08lx)\n",
  147. ((struct task_struct *)(__pa(current)))->comm,
  148. ((struct task_struct *)(__pa(current)))->pid,
  149. (unsigned long)current);
  150. printk("\nStack: ");
  151. printk("Stack dump [0x%08lx]:\n", (unsigned long)stack);
  152. for (i = 0; i < kstack_depth_to_print; i++) {
  153. if (((long)stack & (THREAD_SIZE - 1)) == 0)
  154. break;
  155. stack++;
  156. printk("%lx :: sp + %02d: 0x%08lx\n", stack, i * 4,
  157. *((unsigned long *)(__pa(stack))));
  158. }
  159. printk("\n");
  160. printk("Call Trace: ");
  161. i = 1;
  162. while (((long)stack & (THREAD_SIZE - 1)) != 0) {
  163. addr = *((unsigned long *)__pa(stack));
  164. stack++;
  165. if (kernel_text_address(addr)) {
  166. if (i && ((i % 6) == 0))
  167. printk("\n ");
  168. printk(" [<%08lx>]", addr);
  169. i++;
  170. }
  171. }
  172. printk("\n");
  173. printk("\nCode: ");
  174. for (i = -24; i < 24; i++) {
  175. unsigned char c;
  176. c = ((unsigned char *)(__pa(regs->pc)))[i];
  177. if (i == 0)
  178. printk("(%02x) ", c);
  179. else
  180. printk("%02x ", c);
  181. }
  182. printk("\n");
  183. }
  184. /* This is normally the 'Oops' routine */
  185. void die(const char *str, struct pt_regs *regs, long err)
  186. {
  187. console_verbose();
  188. printk("\n%s#: %04lx\n", str, err & 0xffff);
  189. show_registers(regs);
  190. #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
  191. printk("\n\nUNHANDLED_EXCEPTION: entering infinite loop\n");
  192. /* shut down interrupts */
  193. local_irq_disable();
  194. __asm__ __volatile__("l.nop 1");
  195. do {} while (1);
  196. #endif
  197. do_exit(SIGSEGV);
  198. }
  199. /* This is normally the 'Oops' routine */
  200. void die_if_kernel(const char *str, struct pt_regs *regs, long err)
  201. {
  202. if (user_mode(regs))
  203. return;
  204. die(str, regs, err);
  205. }
  206. void unhandled_exception(struct pt_regs *regs, int ea, int vector)
  207. {
  208. printk("Unable to handle exception at EA =0x%x, vector 0x%x",
  209. ea, vector);
  210. die("Oops", regs, 9);
  211. }
  212. void __init trap_init(void)
  213. {
  214. /* Nothing needs to be done */
  215. }
  216. asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
  217. {
  218. force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)address, current);
  219. regs->pc += 4;
  220. }
  221. asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
  222. {
  223. if (user_mode(regs)) {
  224. /* Send a SIGBUS */
  225. force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)address, current);
  226. } else {
  227. printk("KERNEL: Unaligned Access 0x%.8lx\n", address);
  228. show_registers(regs);
  229. die("Die:", regs, address);
  230. }
  231. }
  232. asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address)
  233. {
  234. if (user_mode(regs)) {
  235. /* Send a SIGBUS */
  236. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, current);
  237. } else { /* Kernel mode */
  238. printk("KERNEL: Bus error (SIGBUS) 0x%.8lx\n", address);
  239. show_registers(regs);
  240. die("Die:", regs, address);
  241. }
  242. }
  243. static inline int in_delay_slot(struct pt_regs *regs)
  244. {
  245. #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
  246. /* No delay slot flag, do the old way */
  247. unsigned int op, insn;
  248. insn = *((unsigned int *)regs->pc);
  249. op = insn >> 26;
  250. switch (op) {
  251. case 0x00: /* l.j */
  252. case 0x01: /* l.jal */
  253. case 0x03: /* l.bnf */
  254. case 0x04: /* l.bf */
  255. case 0x11: /* l.jr */
  256. case 0x12: /* l.jalr */
  257. return 1;
  258. default:
  259. return 0;
  260. }
  261. #else
  262. return mfspr(SPR_SR) & SPR_SR_DSX;
  263. #endif
  264. }
  265. static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
  266. {
  267. int displacement;
  268. unsigned int rb, op, jmp;
  269. if (unlikely(in_delay_slot(regs))) {
  270. /* In delay slot, instruction at pc is a branch, simulate it */
  271. jmp = *((unsigned int *)regs->pc);
  272. displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
  273. rb = (jmp & 0x0000ffff) >> 11;
  274. op = jmp >> 26;
  275. switch (op) {
  276. case 0x00: /* l.j */
  277. regs->pc += displacement;
  278. return;
  279. case 0x01: /* l.jal */
  280. regs->pc += displacement;
  281. regs->gpr[9] = regs->pc + 8;
  282. return;
  283. case 0x03: /* l.bnf */
  284. if (regs->sr & SPR_SR_F)
  285. regs->pc += 8;
  286. else
  287. regs->pc += displacement;
  288. return;
  289. case 0x04: /* l.bf */
  290. if (regs->sr & SPR_SR_F)
  291. regs->pc += displacement;
  292. else
  293. regs->pc += 8;
  294. return;
  295. case 0x11: /* l.jr */
  296. regs->pc = regs->gpr[rb];
  297. return;
  298. case 0x12: /* l.jalr */
  299. regs->pc = regs->gpr[rb];
  300. regs->gpr[9] = regs->pc + 8;
  301. return;
  302. default:
  303. break;
  304. }
  305. } else {
  306. regs->pc += 4;
  307. }
  308. }
  309. static inline void simulate_lwa(struct pt_regs *regs, unsigned long address,
  310. unsigned int insn)
  311. {
  312. unsigned int ra, rd;
  313. unsigned long value;
  314. unsigned long orig_pc;
  315. long imm;
  316. const struct exception_table_entry *entry;
  317. orig_pc = regs->pc;
  318. adjust_pc(regs, address);
  319. ra = (insn >> 16) & 0x1f;
  320. rd = (insn >> 21) & 0x1f;
  321. imm = (short)insn;
  322. lwa_addr = (unsigned long __user *)(regs->gpr[ra] + imm);
  323. if ((unsigned long)lwa_addr & 0x3) {
  324. do_unaligned_access(regs, address);
  325. return;
  326. }
  327. if (get_user(value, lwa_addr)) {
  328. if (user_mode(regs)) {
  329. force_sig(SIGSEGV, current);
  330. return;
  331. }
  332. if ((entry = search_exception_tables(orig_pc))) {
  333. regs->pc = entry->fixup;
  334. return;
  335. }
  336. /* kernel access in kernel space, load it directly */
  337. value = *((unsigned long *)lwa_addr);
  338. }
  339. lwa_flag = 1;
  340. regs->gpr[rd] = value;
  341. }
  342. static inline void simulate_swa(struct pt_regs *regs, unsigned long address,
  343. unsigned int insn)
  344. {
  345. unsigned long __user *vaddr;
  346. unsigned long orig_pc;
  347. unsigned int ra, rb;
  348. long imm;
  349. const struct exception_table_entry *entry;
  350. orig_pc = regs->pc;
  351. adjust_pc(regs, address);
  352. ra = (insn >> 16) & 0x1f;
  353. rb = (insn >> 11) & 0x1f;
  354. imm = (short)(((insn & 0x2200000) >> 10) | (insn & 0x7ff));
  355. vaddr = (unsigned long __user *)(regs->gpr[ra] + imm);
  356. if (!lwa_flag || vaddr != lwa_addr) {
  357. regs->sr &= ~SPR_SR_F;
  358. return;
  359. }
  360. if ((unsigned long)vaddr & 0x3) {
  361. do_unaligned_access(regs, address);
  362. return;
  363. }
  364. if (put_user(regs->gpr[rb], vaddr)) {
  365. if (user_mode(regs)) {
  366. force_sig(SIGSEGV, current);
  367. return;
  368. }
  369. if ((entry = search_exception_tables(orig_pc))) {
  370. regs->pc = entry->fixup;
  371. return;
  372. }
  373. /* kernel access in kernel space, store it directly */
  374. *((unsigned long *)vaddr) = regs->gpr[rb];
  375. }
  376. lwa_flag = 0;
  377. regs->sr |= SPR_SR_F;
  378. }
  379. #define INSN_LWA 0x1b
  380. #define INSN_SWA 0x33
  381. asmlinkage void do_illegal_instruction(struct pt_regs *regs,
  382. unsigned long address)
  383. {
  384. unsigned int op;
  385. unsigned int insn = *((unsigned int *)address);
  386. op = insn >> 26;
  387. switch (op) {
  388. case INSN_LWA:
  389. simulate_lwa(regs, address, insn);
  390. return;
  391. case INSN_SWA:
  392. simulate_swa(regs, address, insn);
  393. return;
  394. default:
  395. break;
  396. }
  397. if (user_mode(regs)) {
  398. /* Send a SIGILL */
  399. force_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)address, current);
  400. } else { /* Kernel mode */
  401. printk("KERNEL: Illegal instruction (SIGILL) 0x%.8lx\n",
  402. address);
  403. show_registers(regs);
  404. die("Die:", regs, address);
  405. }
  406. }