traps.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenRISC traps.c
  4. *
  5. * Linux architectural port borrowing liberally from similar works of
  6. * others. All original copyrights apply as per the original source
  7. * declaration.
  8. *
  9. * Modifications for the OpenRISC architecture:
  10. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  11. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  12. *
  13. * Here we handle the break vectors not used by the system call
  14. * mechanism, as well as some general stack/register dumping
  15. * things.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/debug.h>
  20. #include <linux/sched/task_stack.h>
  21. #include <linux/kernel.h>
  22. #include <linux/extable.h>
  23. #include <linux/kmod.h>
  24. #include <linux/string.h>
  25. #include <linux/errno.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/timer.h>
  28. #include <linux/mm.h>
  29. #include <linux/kallsyms.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/bug.h>
  32. #include <asm/fpu.h>
  33. #include <asm/io.h>
  34. #include <asm/processor.h>
  35. #include <asm/unwinder.h>
  36. #include <asm/sections.h>
  37. int lwa_flag;
  38. static unsigned long __user *lwa_addr;
  39. asmlinkage void unhandled_exception(struct pt_regs *regs, int ea, int vector);
  40. asmlinkage void do_trap(struct pt_regs *regs, unsigned long address);
  41. asmlinkage void do_fpe_trap(struct pt_regs *regs, unsigned long address);
  42. asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address);
  43. asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address);
  44. asmlinkage void do_illegal_instruction(struct pt_regs *regs,
  45. unsigned long address);
  46. static void print_trace(void *data, unsigned long addr, int reliable)
  47. {
  48. const char *loglvl = data;
  49. pr_info("%s[<%p>] %s%pS\n", loglvl, (void *) addr, reliable ? "" : "? ",
  50. (void *) addr);
  51. }
  52. static void print_data(unsigned long base_addr, unsigned long word, int i)
  53. {
  54. if (i == 0)
  55. pr_info("(%08lx:)\t%08lx", base_addr + (i * 4), word);
  56. else
  57. pr_info(" %08lx:\t%08lx", base_addr + (i * 4), word);
  58. }
  59. /* displays a short stack trace */
  60. void show_stack(struct task_struct *task, unsigned long *esp, const char *loglvl)
  61. {
  62. if (esp == NULL)
  63. esp = (unsigned long *)&esp;
  64. pr_info("%sCall trace:\n", loglvl);
  65. unwind_stack((void *)loglvl, esp, print_trace);
  66. }
  67. void show_registers(struct pt_regs *regs)
  68. {
  69. int i;
  70. int in_kernel = 1;
  71. unsigned long esp;
  72. esp = (unsigned long)(regs->sp);
  73. if (user_mode(regs))
  74. in_kernel = 0;
  75. pr_info("CPU #: %d\n"
  76. " PC: %08lx SR: %08lx SP: %08lx\n",
  77. smp_processor_id(), regs->pc, regs->sr, regs->sp);
  78. pr_info("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
  79. 0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
  80. pr_info("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
  81. regs->gpr[4], regs->gpr[5], regs->gpr[6], regs->gpr[7]);
  82. pr_info("GPR08: %08lx GPR09: %08lx GPR10: %08lx GPR11: %08lx\n",
  83. regs->gpr[8], regs->gpr[9], regs->gpr[10], regs->gpr[11]);
  84. pr_info("GPR12: %08lx GPR13: %08lx GPR14: %08lx GPR15: %08lx\n",
  85. regs->gpr[12], regs->gpr[13], regs->gpr[14], regs->gpr[15]);
  86. pr_info("GPR16: %08lx GPR17: %08lx GPR18: %08lx GPR19: %08lx\n",
  87. regs->gpr[16], regs->gpr[17], regs->gpr[18], regs->gpr[19]);
  88. pr_info("GPR20: %08lx GPR21: %08lx GPR22: %08lx GPR23: %08lx\n",
  89. regs->gpr[20], regs->gpr[21], regs->gpr[22], regs->gpr[23]);
  90. pr_info("GPR24: %08lx GPR25: %08lx GPR26: %08lx GPR27: %08lx\n",
  91. regs->gpr[24], regs->gpr[25], regs->gpr[26], regs->gpr[27]);
  92. pr_info("GPR28: %08lx GPR29: %08lx GPR30: %08lx GPR31: %08lx\n",
  93. regs->gpr[28], regs->gpr[29], regs->gpr[30], regs->gpr[31]);
  94. pr_info(" RES: %08lx oGPR11: %08lx\n",
  95. regs->gpr[11], regs->orig_gpr11);
  96. pr_info("Process %s (pid: %d, stackpage=%08lx)\n",
  97. current->comm, current->pid, (unsigned long)current);
  98. /*
  99. * When in-kernel, we also print out the stack and code at the
  100. * time of the fault..
  101. */
  102. if (in_kernel) {
  103. pr_info("\nStack: ");
  104. show_stack(NULL, (unsigned long *)esp, KERN_EMERG);
  105. if (esp < PAGE_OFFSET)
  106. goto bad_stack;
  107. pr_info("\n");
  108. for (i = -8; i < 24; i += 1) {
  109. unsigned long word;
  110. if (__get_user(word, &((unsigned long *)esp)[i])) {
  111. bad_stack:
  112. pr_info(" Bad Stack value.");
  113. break;
  114. }
  115. print_data(esp, word, i);
  116. }
  117. pr_info("\nCode: ");
  118. if (regs->pc < PAGE_OFFSET)
  119. goto bad;
  120. for (i = -6; i < 6; i += 1) {
  121. unsigned long word;
  122. if (__get_user(word, &((unsigned long *)regs->pc)[i])) {
  123. bad:
  124. pr_info(" Bad PC value.");
  125. break;
  126. }
  127. print_data(regs->pc, word, i);
  128. }
  129. }
  130. pr_info("\n");
  131. }
  132. /* This is normally the 'Oops' routine */
  133. void __noreturn die(const char *str, struct pt_regs *regs, long err)
  134. {
  135. console_verbose();
  136. pr_emerg("\n%s#: %04lx\n", str, err & 0xffff);
  137. show_registers(regs);
  138. #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
  139. pr_emerg("\n\nUNHANDLED_EXCEPTION: entering infinite loop\n");
  140. /* shut down interrupts */
  141. local_irq_disable();
  142. __asm__ __volatile__("l.nop 1");
  143. do {} while (1);
  144. #endif
  145. make_task_dead(SIGSEGV);
  146. }
  147. asmlinkage void unhandled_exception(struct pt_regs *regs, int ea, int vector)
  148. {
  149. pr_emerg("Unable to handle exception at EA =0x%x, vector 0x%x",
  150. ea, vector);
  151. die("Oops", regs, 9);
  152. }
  153. asmlinkage void do_fpe_trap(struct pt_regs *regs, unsigned long address)
  154. {
  155. if (user_mode(regs)) {
  156. int code = FPE_FLTUNK;
  157. #ifdef CONFIG_FPU
  158. unsigned long fpcsr;
  159. save_fpu(current);
  160. fpcsr = current->thread.fpcsr;
  161. if (fpcsr & SPR_FPCSR_IVF)
  162. code = FPE_FLTINV;
  163. else if (fpcsr & SPR_FPCSR_OVF)
  164. code = FPE_FLTOVF;
  165. else if (fpcsr & SPR_FPCSR_UNF)
  166. code = FPE_FLTUND;
  167. else if (fpcsr & SPR_FPCSR_DZF)
  168. code = FPE_FLTDIV;
  169. else if (fpcsr & SPR_FPCSR_IXF)
  170. code = FPE_FLTRES;
  171. /* Clear all flags */
  172. current->thread.fpcsr &= ~SPR_FPCSR_ALLF;
  173. restore_fpu(current);
  174. #endif
  175. force_sig_fault(SIGFPE, code, (void __user *)regs->pc);
  176. } else {
  177. pr_emerg("KERNEL: Illegal fpe exception 0x%.8lx\n", regs->pc);
  178. die("Die:", regs, SIGFPE);
  179. }
  180. }
  181. asmlinkage void do_trap(struct pt_regs *regs, unsigned long address)
  182. {
  183. if (user_mode(regs)) {
  184. force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->pc);
  185. } else {
  186. pr_emerg("KERNEL: Illegal trap exception 0x%.8lx\n", regs->pc);
  187. die("Die:", regs, SIGILL);
  188. }
  189. }
  190. asmlinkage void do_unaligned_access(struct pt_regs *regs, unsigned long address)
  191. {
  192. if (user_mode(regs)) {
  193. /* Send a SIGBUS */
  194. force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)address);
  195. } else {
  196. pr_emerg("KERNEL: Unaligned Access 0x%.8lx\n", address);
  197. die("Die:", regs, address);
  198. }
  199. }
  200. asmlinkage void do_bus_fault(struct pt_regs *regs, unsigned long address)
  201. {
  202. if (user_mode(regs)) {
  203. /* Send a SIGBUS */
  204. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
  205. } else { /* Kernel mode */
  206. pr_emerg("KERNEL: Bus error (SIGBUS) 0x%.8lx\n", address);
  207. die("Die:", regs, address);
  208. }
  209. }
  210. static inline int in_delay_slot(struct pt_regs *regs)
  211. {
  212. #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
  213. /* No delay slot flag, do the old way */
  214. unsigned int op, insn;
  215. insn = *((unsigned int *)regs->pc);
  216. op = insn >> 26;
  217. switch (op) {
  218. case 0x00: /* l.j */
  219. case 0x01: /* l.jal */
  220. case 0x03: /* l.bnf */
  221. case 0x04: /* l.bf */
  222. case 0x11: /* l.jr */
  223. case 0x12: /* l.jalr */
  224. return 1;
  225. default:
  226. return 0;
  227. }
  228. #else
  229. return mfspr(SPR_SR) & SPR_SR_DSX;
  230. #endif
  231. }
  232. static inline void adjust_pc(struct pt_regs *regs, unsigned long address)
  233. {
  234. int displacement;
  235. unsigned int rb, op, jmp;
  236. if (unlikely(in_delay_slot(regs))) {
  237. /* In delay slot, instruction at pc is a branch, simulate it */
  238. jmp = *((unsigned int *)regs->pc);
  239. displacement = sign_extend32(((jmp) & 0x3ffffff) << 2, 27);
  240. rb = (jmp & 0x0000ffff) >> 11;
  241. op = jmp >> 26;
  242. switch (op) {
  243. case 0x00: /* l.j */
  244. regs->pc += displacement;
  245. return;
  246. case 0x01: /* l.jal */
  247. regs->pc += displacement;
  248. regs->gpr[9] = regs->pc + 8;
  249. return;
  250. case 0x03: /* l.bnf */
  251. if (regs->sr & SPR_SR_F)
  252. regs->pc += 8;
  253. else
  254. regs->pc += displacement;
  255. return;
  256. case 0x04: /* l.bf */
  257. if (regs->sr & SPR_SR_F)
  258. regs->pc += displacement;
  259. else
  260. regs->pc += 8;
  261. return;
  262. case 0x11: /* l.jr */
  263. regs->pc = regs->gpr[rb];
  264. return;
  265. case 0x12: /* l.jalr */
  266. regs->pc = regs->gpr[rb];
  267. regs->gpr[9] = regs->pc + 8;
  268. return;
  269. default:
  270. break;
  271. }
  272. } else {
  273. regs->pc += 4;
  274. }
  275. }
  276. static inline void simulate_lwa(struct pt_regs *regs, unsigned long address,
  277. unsigned int insn)
  278. {
  279. unsigned int ra, rd;
  280. unsigned long value;
  281. unsigned long orig_pc;
  282. long imm;
  283. const struct exception_table_entry *entry;
  284. orig_pc = regs->pc;
  285. adjust_pc(regs, address);
  286. ra = (insn >> 16) & 0x1f;
  287. rd = (insn >> 21) & 0x1f;
  288. imm = (short)insn;
  289. lwa_addr = (unsigned long __user *)(regs->gpr[ra] + imm);
  290. if ((unsigned long)lwa_addr & 0x3) {
  291. do_unaligned_access(regs, address);
  292. return;
  293. }
  294. if (get_user(value, lwa_addr)) {
  295. if (user_mode(regs)) {
  296. force_sig(SIGSEGV);
  297. return;
  298. }
  299. if ((entry = search_exception_tables(orig_pc))) {
  300. regs->pc = entry->fixup;
  301. return;
  302. }
  303. /* kernel access in kernel space, load it directly */
  304. value = *((unsigned long *)lwa_addr);
  305. }
  306. lwa_flag = 1;
  307. regs->gpr[rd] = value;
  308. }
  309. static inline void simulate_swa(struct pt_regs *regs, unsigned long address,
  310. unsigned int insn)
  311. {
  312. unsigned long __user *vaddr;
  313. unsigned long orig_pc;
  314. unsigned int ra, rb;
  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. rb = (insn >> 11) & 0x1f;
  321. imm = (short)(((insn & 0x2200000) >> 10) | (insn & 0x7ff));
  322. vaddr = (unsigned long __user *)(regs->gpr[ra] + imm);
  323. if (!lwa_flag || vaddr != lwa_addr) {
  324. regs->sr &= ~SPR_SR_F;
  325. return;
  326. }
  327. if ((unsigned long)vaddr & 0x3) {
  328. do_unaligned_access(regs, address);
  329. return;
  330. }
  331. if (put_user(regs->gpr[rb], vaddr)) {
  332. if (user_mode(regs)) {
  333. force_sig(SIGSEGV);
  334. return;
  335. }
  336. if ((entry = search_exception_tables(orig_pc))) {
  337. regs->pc = entry->fixup;
  338. return;
  339. }
  340. /* kernel access in kernel space, store it directly */
  341. *((unsigned long *)vaddr) = regs->gpr[rb];
  342. }
  343. lwa_flag = 0;
  344. regs->sr |= SPR_SR_F;
  345. }
  346. #define INSN_LWA 0x1b
  347. #define INSN_SWA 0x33
  348. asmlinkage void do_illegal_instruction(struct pt_regs *regs,
  349. unsigned long address)
  350. {
  351. unsigned int op;
  352. unsigned int insn = *((unsigned int *)address);
  353. op = insn >> 26;
  354. switch (op) {
  355. case INSN_LWA:
  356. simulate_lwa(regs, address, insn);
  357. return;
  358. case INSN_SWA:
  359. simulate_swa(regs, address, insn);
  360. return;
  361. default:
  362. break;
  363. }
  364. if (user_mode(regs)) {
  365. /* Send a SIGILL */
  366. force_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)address);
  367. } else { /* Kernel mode */
  368. pr_emerg("KERNEL: Illegal instruction (SIGILL) 0x%.8lx\n",
  369. address);
  370. die("Die:", regs, address);
  371. }
  372. }