traps.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * arch/score/kernel/traps.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Chen Liqin <liqin.chen@sunplusct.com>
  8. * Lennox Wu <lennox.wu@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/module.h>
  26. #include <linux/sched.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/irq.h>
  29. #include <asm/irq_regs.h>
  30. unsigned long exception_handlers[32];
  31. /*
  32. * The architecture-independent show_stack generator
  33. */
  34. void show_stack(struct task_struct *task, unsigned long *sp)
  35. {
  36. int i;
  37. long stackdata;
  38. sp = sp ? sp : (unsigned long *)&sp;
  39. printk(KERN_NOTICE "Stack: ");
  40. i = 1;
  41. while ((long) sp & (PAGE_SIZE - 1)) {
  42. if (i && ((i % 8) == 0))
  43. printk(KERN_NOTICE "\n");
  44. if (i > 40) {
  45. printk(KERN_NOTICE " ...");
  46. break;
  47. }
  48. if (__get_user(stackdata, sp++)) {
  49. printk(KERN_NOTICE " (Bad stack address)");
  50. break;
  51. }
  52. printk(KERN_NOTICE " %08lx", stackdata);
  53. i++;
  54. }
  55. printk(KERN_NOTICE "\n");
  56. }
  57. static void show_trace(long *sp)
  58. {
  59. int i;
  60. long addr;
  61. sp = sp ? sp : (long *) &sp;
  62. printk(KERN_NOTICE "Call Trace: ");
  63. i = 1;
  64. while ((long) sp & (PAGE_SIZE - 1)) {
  65. if (__get_user(addr, sp++)) {
  66. if (i && ((i % 6) == 0))
  67. printk(KERN_NOTICE "\n");
  68. printk(KERN_NOTICE " (Bad stack address)\n");
  69. break;
  70. }
  71. if (kernel_text_address(addr)) {
  72. if (i && ((i % 6) == 0))
  73. printk(KERN_NOTICE "\n");
  74. if (i > 40) {
  75. printk(KERN_NOTICE " ...");
  76. break;
  77. }
  78. printk(KERN_NOTICE " [<%08lx>]", addr);
  79. i++;
  80. }
  81. }
  82. printk(KERN_NOTICE "\n");
  83. }
  84. static void show_code(unsigned int *pc)
  85. {
  86. long i;
  87. printk(KERN_NOTICE "\nCode:");
  88. for (i = -3; i < 6; i++) {
  89. unsigned long insn;
  90. if (__get_user(insn, pc + i)) {
  91. printk(KERN_NOTICE " (Bad address in epc)\n");
  92. break;
  93. }
  94. printk(KERN_NOTICE "%c%08lx%c", (i ? ' ' : '<'),
  95. insn, (i ? ' ' : '>'));
  96. }
  97. }
  98. /*
  99. * FIXME: really the generic show_regs should take a const pointer argument.
  100. */
  101. void show_regs(struct pt_regs *regs)
  102. {
  103. printk("r0 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  104. regs->regs[0], regs->regs[1], regs->regs[2], regs->regs[3],
  105. regs->regs[4], regs->regs[5], regs->regs[6], regs->regs[7]);
  106. printk("r8 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  107. regs->regs[8], regs->regs[9], regs->regs[10], regs->regs[11],
  108. regs->regs[12], regs->regs[13], regs->regs[14], regs->regs[15]);
  109. printk("r16: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  110. regs->regs[16], regs->regs[17], regs->regs[18], regs->regs[19],
  111. regs->regs[20], regs->regs[21], regs->regs[22], regs->regs[23]);
  112. printk("r24: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  113. regs->regs[24], regs->regs[25], regs->regs[26], regs->regs[27],
  114. regs->regs[28], regs->regs[29], regs->regs[30], regs->regs[31]);
  115. printk("CEH : %08lx\n", regs->ceh);
  116. printk("CEL : %08lx\n", regs->cel);
  117. printk("EMA:%08lx, epc:%08lx %s\nPSR: %08lx\nECR:%08lx\nCondition : %08lx\n",
  118. regs->cp0_ema, regs->cp0_epc, print_tainted(), regs->cp0_psr,
  119. regs->cp0_ecr, regs->cp0_condition);
  120. }
  121. static void show_registers(struct pt_regs *regs)
  122. {
  123. show_regs(regs);
  124. printk(KERN_NOTICE "Process %s (pid: %d, stackpage=%08lx)\n",
  125. current->comm, current->pid, (unsigned long) current);
  126. show_stack(current_thread_info()->task, (long *) regs->regs[0]);
  127. show_trace((long *) regs->regs[0]);
  128. show_code((unsigned int *) regs->cp0_epc);
  129. printk(KERN_NOTICE "\n");
  130. }
  131. /*
  132. * The architecture-independent dump_stack generator
  133. */
  134. void dump_stack(void)
  135. {
  136. show_stack(current_thread_info()->task,
  137. (long *) get_irq_regs()->regs[0]);
  138. }
  139. EXPORT_SYMBOL(dump_stack);
  140. void __die(const char *str, struct pt_regs *regs, const char *file,
  141. const char *func, unsigned long line)
  142. {
  143. console_verbose();
  144. printk("%s", str);
  145. if (file && func)
  146. printk(" in %s:%s, line %ld", file, func, line);
  147. printk(":\n");
  148. show_registers(regs);
  149. do_exit(SIGSEGV);
  150. }
  151. void __die_if_kernel(const char *str, struct pt_regs *regs,
  152. const char *file, const char *func, unsigned long line)
  153. {
  154. if (!user_mode(regs))
  155. __die(str, regs, file, func, line);
  156. }
  157. asmlinkage void do_adelinsn(struct pt_regs *regs)
  158. {
  159. printk("do_ADE-linsn:ema:0x%08lx:epc:0x%08lx\n",
  160. regs->cp0_ema, regs->cp0_epc);
  161. die_if_kernel("do_ade execution Exception\n", regs);
  162. force_sig(SIGBUS, current);
  163. }
  164. asmlinkage void do_adedata(struct pt_regs *regs)
  165. {
  166. const struct exception_table_entry *fixup;
  167. fixup = search_exception_tables(regs->cp0_epc);
  168. if (fixup) {
  169. regs->cp0_epc = fixup->fixup;
  170. return;
  171. }
  172. printk("do_ADE-data:ema:0x%08lx:epc:0x%08lx\n",
  173. regs->cp0_ema, regs->cp0_epc);
  174. die_if_kernel("do_ade execution Exception\n", regs);
  175. force_sig(SIGBUS, current);
  176. }
  177. asmlinkage void do_pel(struct pt_regs *regs)
  178. {
  179. die_if_kernel("do_pel execution Exception", regs);
  180. force_sig(SIGFPE, current);
  181. }
  182. asmlinkage void do_cee(struct pt_regs *regs)
  183. {
  184. die_if_kernel("do_cee execution Exception", regs);
  185. force_sig(SIGFPE, current);
  186. }
  187. asmlinkage void do_cpe(struct pt_regs *regs)
  188. {
  189. die_if_kernel("do_cpe execution Exception", regs);
  190. force_sig(SIGFPE, current);
  191. }
  192. asmlinkage void do_be(struct pt_regs *regs)
  193. {
  194. die_if_kernel("do_be execution Exception", regs);
  195. force_sig(SIGBUS, current);
  196. }
  197. asmlinkage void do_ov(struct pt_regs *regs)
  198. {
  199. siginfo_t info;
  200. die_if_kernel("do_ov execution Exception", regs);
  201. info.si_code = FPE_INTOVF;
  202. info.si_signo = SIGFPE;
  203. info.si_errno = 0;
  204. info.si_addr = (void *)regs->cp0_epc;
  205. force_sig_info(SIGFPE, &info, current);
  206. }
  207. asmlinkage void do_tr(struct pt_regs *regs)
  208. {
  209. die_if_kernel("do_tr execution Exception", regs);
  210. force_sig(SIGTRAP, current);
  211. }
  212. asmlinkage void do_ri(struct pt_regs *regs)
  213. {
  214. unsigned long epc_insn;
  215. unsigned long epc = regs->cp0_epc;
  216. read_tsk_long(current, epc, &epc_insn);
  217. if (current->thread.single_step == 1) {
  218. if ((epc == current->thread.addr1) ||
  219. (epc == current->thread.addr2)) {
  220. user_disable_single_step(current);
  221. force_sig(SIGTRAP, current);
  222. return;
  223. } else
  224. BUG();
  225. } else if ((epc_insn == BREAKPOINT32_INSN) ||
  226. ((epc_insn & 0x0000FFFF) == 0x7002) ||
  227. ((epc_insn & 0xFFFF0000) == 0x70020000)) {
  228. force_sig(SIGTRAP, current);
  229. return;
  230. } else {
  231. die_if_kernel("do_ri execution Exception", regs);
  232. force_sig(SIGILL, current);
  233. }
  234. }
  235. asmlinkage void do_ccu(struct pt_regs *regs)
  236. {
  237. die_if_kernel("do_ccu execution Exception", regs);
  238. force_sig(SIGILL, current);
  239. }
  240. asmlinkage void do_reserved(struct pt_regs *regs)
  241. {
  242. /*
  243. * Game over - no way to handle this if it ever occurs. Most probably
  244. * caused by a new unknown cpu type or after another deadly
  245. * hard/software error.
  246. */
  247. die_if_kernel("do_reserved execution Exception", regs);
  248. show_regs(regs);
  249. panic("Caught reserved exception - should not happen.");
  250. }
  251. /*
  252. * NMI exception handler.
  253. */
  254. void nmi_exception_handler(struct pt_regs *regs)
  255. {
  256. die_if_kernel("nmi_exception_handler execution Exception", regs);
  257. die("NMI", regs);
  258. }
  259. /* Install CPU exception handler */
  260. void *set_except_vector(int n, void *addr)
  261. {
  262. unsigned long handler = (unsigned long) addr;
  263. unsigned long old_handler = exception_handlers[n];
  264. exception_handlers[n] = handler;
  265. return (void *)old_handler;
  266. }
  267. void __init trap_init(void)
  268. {
  269. int i;
  270. pgd_current = (unsigned long)init_mm.pgd;
  271. /* DEBUG EXCEPTION */
  272. memcpy((void *)DEBUG_VECTOR_BASE_ADDR,
  273. &debug_exception_vector, DEBUG_VECTOR_SIZE);
  274. /* NMI EXCEPTION */
  275. memcpy((void *)GENERAL_VECTOR_BASE_ADDR,
  276. &general_exception_vector, GENERAL_VECTOR_SIZE);
  277. /*
  278. * Initialise exception handlers
  279. */
  280. for (i = 0; i <= 31; i++)
  281. set_except_vector(i, handle_reserved);
  282. set_except_vector(1, handle_nmi);
  283. set_except_vector(2, handle_adelinsn);
  284. set_except_vector(3, handle_tlb_refill);
  285. set_except_vector(4, handle_tlb_invaild);
  286. set_except_vector(5, handle_ibe);
  287. set_except_vector(6, handle_pel);
  288. set_except_vector(7, handle_sys);
  289. set_except_vector(8, handle_ccu);
  290. set_except_vector(9, handle_ri);
  291. set_except_vector(10, handle_tr);
  292. set_except_vector(11, handle_adedata);
  293. set_except_vector(12, handle_adedata);
  294. set_except_vector(13, handle_tlb_refill);
  295. set_except_vector(14, handle_tlb_invaild);
  296. set_except_vector(15, handle_mod);
  297. set_except_vector(16, handle_cee);
  298. set_except_vector(17, handle_cpe);
  299. set_except_vector(18, handle_dbe);
  300. flush_icache_range(DEBUG_VECTOR_BASE_ADDR, IRQ_VECTOR_BASE_ADDR);
  301. atomic_inc(&init_mm.mm_count);
  302. current->active_mm = &init_mm;
  303. cpu_cache_init();
  304. }