traps.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /* traps.c: high-level exception handler for FR-V
  2. *
  3. * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/signal.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/types.h>
  16. #include <linux/user.h>
  17. #include <linux/string.h>
  18. #include <linux/linkage.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <asm/asm-offsets.h>
  22. #include <asm/setup.h>
  23. #include <asm/fpu.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/siginfo.h>
  28. #include <asm/unaligned.h>
  29. void show_backtrace(struct pt_regs *, unsigned long);
  30. extern asmlinkage void __break_hijack_kernel_event(void);
  31. /*****************************************************************************/
  32. /*
  33. * instruction access error
  34. */
  35. asmlinkage void insn_access_error(unsigned long esfr1, unsigned long epcr0, unsigned long esr0)
  36. {
  37. siginfo_t info;
  38. die_if_kernel("-- Insn Access Error --\n"
  39. "EPCR0 : %08lx\n"
  40. "ESR0 : %08lx\n",
  41. epcr0, esr0);
  42. info.si_signo = SIGSEGV;
  43. info.si_code = SEGV_ACCERR;
  44. info.si_errno = 0;
  45. info.si_addr = (void __user *) ((epcr0 & EPCR0_V) ? (epcr0 & EPCR0_PC) : __frame->pc);
  46. force_sig_info(info.si_signo, &info, current);
  47. } /* end insn_access_error() */
  48. /*****************************************************************************/
  49. /*
  50. * handler for:
  51. * - illegal instruction
  52. * - privileged instruction
  53. * - unsupported trap
  54. * - debug exceptions
  55. */
  56. asmlinkage void illegal_instruction(unsigned long esfr1, unsigned long epcr0, unsigned long esr0)
  57. {
  58. siginfo_t info;
  59. die_if_kernel("-- Illegal Instruction --\n"
  60. "EPCR0 : %08lx\n"
  61. "ESR0 : %08lx\n"
  62. "ESFR1 : %08lx\n",
  63. epcr0, esr0, esfr1);
  64. info.si_errno = 0;
  65. info.si_addr = (void __user *) ((epcr0 & EPCR0_V) ? (epcr0 & EPCR0_PC) : __frame->pc);
  66. switch (__frame->tbr & TBR_TT) {
  67. case TBR_TT_ILLEGAL_INSTR:
  68. info.si_signo = SIGILL;
  69. info.si_code = ILL_ILLOPC;
  70. break;
  71. case TBR_TT_PRIV_INSTR:
  72. info.si_signo = SIGILL;
  73. info.si_code = ILL_PRVOPC;
  74. break;
  75. case TBR_TT_TRAP2 ... TBR_TT_TRAP126:
  76. info.si_signo = SIGILL;
  77. info.si_code = ILL_ILLTRP;
  78. break;
  79. /* GDB uses "tira gr0, #1" as a breakpoint instruction. */
  80. case TBR_TT_TRAP1:
  81. case TBR_TT_BREAK:
  82. info.si_signo = SIGTRAP;
  83. info.si_code =
  84. (__frame->__status & REG__STATUS_STEPPED) ? TRAP_TRACE : TRAP_BRKPT;
  85. break;
  86. }
  87. force_sig_info(info.si_signo, &info, current);
  88. } /* end illegal_instruction() */
  89. /*****************************************************************************/
  90. /*
  91. * handle atomic operations with errors
  92. * - arguments in gr8, gr9, gr10
  93. * - original memory value placed in gr5
  94. * - replacement memory value placed in gr9
  95. */
  96. asmlinkage void atomic_operation(unsigned long esfr1, unsigned long epcr0,
  97. unsigned long esr0)
  98. {
  99. static DEFINE_SPINLOCK(atomic_op_lock);
  100. unsigned long x, y, z;
  101. unsigned long __user *p;
  102. mm_segment_t oldfs;
  103. siginfo_t info;
  104. int ret;
  105. y = 0;
  106. z = 0;
  107. oldfs = get_fs();
  108. if (!user_mode(__frame))
  109. set_fs(KERNEL_DS);
  110. switch (__frame->tbr & TBR_TT) {
  111. /* TIRA gr0,#120
  112. * u32 __atomic_user_cmpxchg32(u32 *ptr, u32 test, u32 new)
  113. */
  114. case TBR_TT_ATOMIC_CMPXCHG32:
  115. p = (unsigned long __user *) __frame->gr8;
  116. x = __frame->gr9;
  117. y = __frame->gr10;
  118. for (;;) {
  119. ret = get_user(z, p);
  120. if (ret < 0)
  121. goto error;
  122. if (z != x)
  123. goto done;
  124. spin_lock_irq(&atomic_op_lock);
  125. if (__get_user(z, p) == 0) {
  126. if (z != x)
  127. goto done2;
  128. if (__put_user(y, p) == 0)
  129. goto done2;
  130. goto error2;
  131. }
  132. spin_unlock_irq(&atomic_op_lock);
  133. }
  134. /* TIRA gr0,#121
  135. * u32 __atomic_kernel_xchg32(void *v, u32 new)
  136. */
  137. case TBR_TT_ATOMIC_XCHG32:
  138. p = (unsigned long __user *) __frame->gr8;
  139. y = __frame->gr9;
  140. for (;;) {
  141. ret = get_user(z, p);
  142. if (ret < 0)
  143. goto error;
  144. spin_lock_irq(&atomic_op_lock);
  145. if (__get_user(z, p) == 0) {
  146. if (__put_user(y, p) == 0)
  147. goto done2;
  148. goto error2;
  149. }
  150. spin_unlock_irq(&atomic_op_lock);
  151. }
  152. /* TIRA gr0,#122
  153. * ulong __atomic_kernel_XOR_return(ulong i, ulong *v)
  154. */
  155. case TBR_TT_ATOMIC_XOR:
  156. p = (unsigned long __user *) __frame->gr8;
  157. x = __frame->gr9;
  158. for (;;) {
  159. ret = get_user(z, p);
  160. if (ret < 0)
  161. goto error;
  162. spin_lock_irq(&atomic_op_lock);
  163. if (__get_user(z, p) == 0) {
  164. y = x ^ z;
  165. if (__put_user(y, p) == 0)
  166. goto done2;
  167. goto error2;
  168. }
  169. spin_unlock_irq(&atomic_op_lock);
  170. }
  171. /* TIRA gr0,#123
  172. * ulong __atomic_kernel_OR_return(ulong i, ulong *v)
  173. */
  174. case TBR_TT_ATOMIC_OR:
  175. p = (unsigned long __user *) __frame->gr8;
  176. x = __frame->gr9;
  177. for (;;) {
  178. ret = get_user(z, p);
  179. if (ret < 0)
  180. goto error;
  181. spin_lock_irq(&atomic_op_lock);
  182. if (__get_user(z, p) == 0) {
  183. y = x ^ z;
  184. if (__put_user(y, p) == 0)
  185. goto done2;
  186. goto error2;
  187. }
  188. spin_unlock_irq(&atomic_op_lock);
  189. }
  190. /* TIRA gr0,#124
  191. * ulong __atomic_kernel_AND_return(ulong i, ulong *v)
  192. */
  193. case TBR_TT_ATOMIC_AND:
  194. p = (unsigned long __user *) __frame->gr8;
  195. x = __frame->gr9;
  196. for (;;) {
  197. ret = get_user(z, p);
  198. if (ret < 0)
  199. goto error;
  200. spin_lock_irq(&atomic_op_lock);
  201. if (__get_user(z, p) == 0) {
  202. y = x & z;
  203. if (__put_user(y, p) == 0)
  204. goto done2;
  205. goto error2;
  206. }
  207. spin_unlock_irq(&atomic_op_lock);
  208. }
  209. /* TIRA gr0,#125
  210. * int __atomic_user_sub_return(atomic_t *v, int i)
  211. */
  212. case TBR_TT_ATOMIC_SUB:
  213. p = (unsigned long __user *) __frame->gr8;
  214. x = __frame->gr9;
  215. for (;;) {
  216. ret = get_user(z, p);
  217. if (ret < 0)
  218. goto error;
  219. spin_lock_irq(&atomic_op_lock);
  220. if (__get_user(z, p) == 0) {
  221. y = z - x;
  222. if (__put_user(y, p) == 0)
  223. goto done2;
  224. goto error2;
  225. }
  226. spin_unlock_irq(&atomic_op_lock);
  227. }
  228. /* TIRA gr0,#126
  229. * int __atomic_user_add_return(atomic_t *v, int i)
  230. */
  231. case TBR_TT_ATOMIC_ADD:
  232. p = (unsigned long __user *) __frame->gr8;
  233. x = __frame->gr9;
  234. for (;;) {
  235. ret = get_user(z, p);
  236. if (ret < 0)
  237. goto error;
  238. spin_lock_irq(&atomic_op_lock);
  239. if (__get_user(z, p) == 0) {
  240. y = z + x;
  241. if (__put_user(y, p) == 0)
  242. goto done2;
  243. goto error2;
  244. }
  245. spin_unlock_irq(&atomic_op_lock);
  246. }
  247. default:
  248. BUG();
  249. }
  250. done2:
  251. spin_unlock_irq(&atomic_op_lock);
  252. done:
  253. if (!user_mode(__frame))
  254. set_fs(oldfs);
  255. __frame->gr5 = z;
  256. __frame->gr9 = y;
  257. return;
  258. error2:
  259. spin_unlock_irq(&atomic_op_lock);
  260. error:
  261. if (!user_mode(__frame))
  262. set_fs(oldfs);
  263. __frame->pc -= 4;
  264. die_if_kernel("-- Atomic Op Error --\n");
  265. info.si_signo = SIGSEGV;
  266. info.si_code = SEGV_ACCERR;
  267. info.si_errno = 0;
  268. info.si_addr = (void __user *) __frame->pc;
  269. force_sig_info(info.si_signo, &info, current);
  270. }
  271. /*****************************************************************************/
  272. /*
  273. *
  274. */
  275. asmlinkage void media_exception(unsigned long msr0, unsigned long msr1)
  276. {
  277. siginfo_t info;
  278. die_if_kernel("-- Media Exception --\n"
  279. "MSR0 : %08lx\n"
  280. "MSR1 : %08lx\n",
  281. msr0, msr1);
  282. info.si_signo = SIGFPE;
  283. info.si_code = FPE_MDAOVF;
  284. info.si_errno = 0;
  285. info.si_addr = (void __user *) __frame->pc;
  286. force_sig_info(info.si_signo, &info, current);
  287. } /* end media_exception() */
  288. /*****************************************************************************/
  289. /*
  290. * instruction or data access exception
  291. */
  292. asmlinkage void memory_access_exception(unsigned long esr0,
  293. unsigned long ear0,
  294. unsigned long epcr0)
  295. {
  296. siginfo_t info;
  297. #ifdef CONFIG_MMU
  298. unsigned long fixup;
  299. fixup = search_exception_table(__frame->pc);
  300. if (fixup) {
  301. __frame->pc = fixup;
  302. return;
  303. }
  304. #endif
  305. die_if_kernel("-- Memory Access Exception --\n"
  306. "ESR0 : %08lx\n"
  307. "EAR0 : %08lx\n"
  308. "EPCR0 : %08lx\n",
  309. esr0, ear0, epcr0);
  310. info.si_signo = SIGSEGV;
  311. info.si_code = SEGV_ACCERR;
  312. info.si_errno = 0;
  313. info.si_addr = NULL;
  314. if ((esr0 & (ESRx_VALID | ESR0_EAV)) == (ESRx_VALID | ESR0_EAV))
  315. info.si_addr = (void __user *) ear0;
  316. force_sig_info(info.si_signo, &info, current);
  317. } /* end memory_access_exception() */
  318. /*****************************************************************************/
  319. /*
  320. * data access error
  321. * - double-word data load from CPU control area (0xFExxxxxx)
  322. * - read performed on inactive or self-refreshing SDRAM
  323. * - error notification from slave device
  324. * - misaligned address
  325. * - access to out of bounds memory region
  326. * - user mode accessing privileged memory region
  327. * - write to R/O memory region
  328. */
  329. asmlinkage void data_access_error(unsigned long esfr1, unsigned long esr15, unsigned long ear15)
  330. {
  331. siginfo_t info;
  332. die_if_kernel("-- Data Access Error --\n"
  333. "ESR15 : %08lx\n"
  334. "EAR15 : %08lx\n",
  335. esr15, ear15);
  336. info.si_signo = SIGSEGV;
  337. info.si_code = SEGV_ACCERR;
  338. info.si_errno = 0;
  339. info.si_addr = (void __user *)
  340. (((esr15 & (ESRx_VALID|ESR15_EAV)) == (ESRx_VALID|ESR15_EAV)) ? ear15 : 0);
  341. force_sig_info(info.si_signo, &info, current);
  342. } /* end data_access_error() */
  343. /*****************************************************************************/
  344. /*
  345. * data store error - should only happen if accessing inactive or self-refreshing SDRAM
  346. */
  347. asmlinkage void data_store_error(unsigned long esfr1, unsigned long esr15)
  348. {
  349. die_if_kernel("-- Data Store Error --\n"
  350. "ESR15 : %08lx\n",
  351. esr15);
  352. BUG();
  353. } /* end data_store_error() */
  354. /*****************************************************************************/
  355. /*
  356. *
  357. */
  358. asmlinkage void division_exception(unsigned long esfr1, unsigned long esr0, unsigned long isr)
  359. {
  360. siginfo_t info;
  361. die_if_kernel("-- Division Exception --\n"
  362. "ESR0 : %08lx\n"
  363. "ISR : %08lx\n",
  364. esr0, isr);
  365. info.si_signo = SIGFPE;
  366. info.si_code = FPE_INTDIV;
  367. info.si_errno = 0;
  368. info.si_addr = (void __user *) __frame->pc;
  369. force_sig_info(info.si_signo, &info, current);
  370. } /* end division_exception() */
  371. /*****************************************************************************/
  372. /*
  373. *
  374. */
  375. asmlinkage void compound_exception(unsigned long esfr1,
  376. unsigned long esr0, unsigned long esr14, unsigned long esr15,
  377. unsigned long msr0, unsigned long msr1)
  378. {
  379. die_if_kernel("-- Compound Exception --\n"
  380. "ESR0 : %08lx\n"
  381. "ESR15 : %08lx\n"
  382. "ESR15 : %08lx\n"
  383. "MSR0 : %08lx\n"
  384. "MSR1 : %08lx\n",
  385. esr0, esr14, esr15, msr0, msr1);
  386. BUG();
  387. } /* end compound_exception() */
  388. /*****************************************************************************/
  389. /*
  390. * The architecture-independent backtrace generator
  391. */
  392. void dump_stack(void)
  393. {
  394. show_stack(NULL, NULL);
  395. }
  396. EXPORT_SYMBOL(dump_stack);
  397. void show_stack(struct task_struct *task, unsigned long *sp)
  398. {
  399. }
  400. void show_trace_task(struct task_struct *tsk)
  401. {
  402. printk("CONTEXT: stack=0x%lx frame=0x%p LR=0x%lx RET=0x%lx\n",
  403. tsk->thread.sp, tsk->thread.frame, tsk->thread.lr, tsk->thread.sched_lr);
  404. }
  405. static const char *regnames[] = {
  406. "PSR ", "ISR ", "CCR ", "CCCR",
  407. "LR ", "LCR ", "PC ", "_stt",
  408. "sys ", "GR8*", "GNE0", "GNE1",
  409. "IACH", "IACL",
  410. "TBR ", "SP ", "FP ", "GR3 ",
  411. "GR4 ", "GR5 ", "GR6 ", "GR7 ",
  412. "GR8 ", "GR9 ", "GR10", "GR11",
  413. "GR12", "GR13", "GR14", "GR15",
  414. "GR16", "GR17", "GR18", "GR19",
  415. "GR20", "GR21", "GR22", "GR23",
  416. "GR24", "GR25", "GR26", "GR27",
  417. "EFRM", "CURR", "GR30", "BFRM"
  418. };
  419. void show_regs(struct pt_regs *regs)
  420. {
  421. unsigned long *reg;
  422. int loop;
  423. printk("\n");
  424. printk("Frame: @%08lx [%s]\n",
  425. (unsigned long) regs,
  426. regs->psr & PSR_S ? "kernel" : "user");
  427. reg = (unsigned long *) regs;
  428. for (loop = 0; loop < NR_PT_REGS; loop++) {
  429. printk("%s %08lx", regnames[loop + 0], reg[loop + 0]);
  430. if (loop == NR_PT_REGS - 1 || loop % 5 == 4)
  431. printk("\n");
  432. else
  433. printk(" | ");
  434. }
  435. printk("Process %s (pid: %d)\n", current->comm, current->pid);
  436. }
  437. void die_if_kernel(const char *str, ...)
  438. {
  439. char buffer[256];
  440. va_list va;
  441. if (user_mode(__frame))
  442. return;
  443. va_start(va, str);
  444. vsprintf(buffer, str, va);
  445. va_end(va);
  446. console_verbose();
  447. printk("\n===================================\n");
  448. printk("%s\n", buffer);
  449. show_backtrace(__frame, 0);
  450. __break_hijack_kernel_event();
  451. do_exit(SIGSEGV);
  452. }
  453. /*****************************************************************************/
  454. /*
  455. * dump the contents of an exception frame
  456. */
  457. static void show_backtrace_regs(struct pt_regs *frame)
  458. {
  459. unsigned long *reg;
  460. int loop;
  461. /* print the registers for this frame */
  462. printk("<-- %s Frame: @%p -->\n",
  463. frame->psr & PSR_S ? "Kernel Mode" : "User Mode",
  464. frame);
  465. reg = (unsigned long *) frame;
  466. for (loop = 0; loop < NR_PT_REGS; loop++) {
  467. printk("%s %08lx", regnames[loop + 0], reg[loop + 0]);
  468. if (loop == NR_PT_REGS - 1 || loop % 5 == 4)
  469. printk("\n");
  470. else
  471. printk(" | ");
  472. }
  473. printk("--------\n");
  474. } /* end show_backtrace_regs() */
  475. /*****************************************************************************/
  476. /*
  477. * generate a backtrace of the kernel stack
  478. */
  479. void show_backtrace(struct pt_regs *frame, unsigned long sp)
  480. {
  481. struct pt_regs *frame0;
  482. unsigned long tos = 0, stop = 0, base;
  483. int format;
  484. base = ((((unsigned long) frame) + 8191) & ~8191) - sizeof(struct user_context);
  485. frame0 = (struct pt_regs *) base;
  486. if (sp) {
  487. tos = sp;
  488. stop = (unsigned long) frame;
  489. }
  490. printk("\nProcess %s (pid: %d)\n\n", current->comm, current->pid);
  491. for (;;) {
  492. /* dump stack segment between frames */
  493. //printk("%08lx -> %08lx\n", tos, stop);
  494. format = 0;
  495. while (tos < stop) {
  496. if (format == 0)
  497. printk(" %04lx :", tos & 0xffff);
  498. printk(" %08lx", *(unsigned long *) tos);
  499. tos += 4;
  500. format++;
  501. if (format == 8) {
  502. printk("\n");
  503. format = 0;
  504. }
  505. }
  506. if (format > 0)
  507. printk("\n");
  508. /* dump frame 0 outside of the loop */
  509. if (frame == frame0)
  510. break;
  511. tos = frame->sp;
  512. if (((unsigned long) frame) + sizeof(*frame) != tos) {
  513. printk("-- TOS %08lx does not follow frame %p --\n",
  514. tos, frame);
  515. break;
  516. }
  517. show_backtrace_regs(frame);
  518. /* dump the stack between this frame and the next */
  519. stop = (unsigned long) frame->next_frame;
  520. if (stop != base &&
  521. (stop < tos ||
  522. stop > base ||
  523. (stop < base && stop + sizeof(*frame) > base) ||
  524. stop & 3)) {
  525. printk("-- next_frame %08lx is invalid (range %08lx-%08lx) --\n",
  526. stop, tos, base);
  527. break;
  528. }
  529. /* move to next frame */
  530. frame = frame->next_frame;
  531. }
  532. /* we can always dump frame 0, even if the rest of the stack is corrupt */
  533. show_backtrace_regs(frame0);
  534. } /* end show_backtrace() */
  535. /*****************************************************************************/
  536. /*
  537. * initialise traps
  538. */
  539. void __init trap_init (void)
  540. {
  541. } /* end trap_init() */