traps.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2005-2017 Andes Technology Corporation
  3. #include <linux/module.h>
  4. #include <linux/personality.h>
  5. #include <linux/kallsyms.h>
  6. #include <linux/hardirq.h>
  7. #include <linux/kdebug.h>
  8. #include <linux/sched/task_stack.h>
  9. #include <linux/uaccess.h>
  10. #include <asm/proc-fns.h>
  11. #include <asm/unistd.h>
  12. #include <linux/ptrace.h>
  13. #include <nds32_intrinsic.h>
  14. extern void show_pte(struct mm_struct *mm, unsigned long addr);
  15. /*
  16. * Dump out the contents of some memory nicely...
  17. */
  18. void dump_mem(const char *lvl, unsigned long bottom, unsigned long top)
  19. {
  20. unsigned long first;
  21. mm_segment_t fs;
  22. int i;
  23. /*
  24. * We need to switch to kernel mode so that we can use __get_user
  25. * to safely read from kernel space. Note that we now dump the
  26. * code first, just in case the backtrace kills us.
  27. */
  28. fs = get_fs();
  29. set_fs(KERNEL_DS);
  30. pr_emerg("%s(0x%08lx to 0x%08lx)\n", lvl, bottom, top);
  31. for (first = bottom & ~31; first < top; first += 32) {
  32. unsigned long p;
  33. char str[sizeof(" 12345678") * 8 + 1];
  34. memset(str, ' ', sizeof(str));
  35. str[sizeof(str) - 1] = '\0';
  36. for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
  37. if (p >= bottom && p < top) {
  38. unsigned long val;
  39. if (__get_user(val, (unsigned long *)p) == 0)
  40. sprintf(str + i * 9, " %08lx", val);
  41. else
  42. sprintf(str + i * 9, " ????????");
  43. }
  44. }
  45. pr_emerg("%s%04lx:%s\n", lvl, first & 0xffff, str);
  46. }
  47. set_fs(fs);
  48. }
  49. EXPORT_SYMBOL(dump_mem);
  50. static void dump_instr(struct pt_regs *regs)
  51. {
  52. unsigned long addr = instruction_pointer(regs);
  53. mm_segment_t fs;
  54. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  55. int i;
  56. return;
  57. /*
  58. * We need to switch to kernel mode so that we can use __get_user
  59. * to safely read from kernel space. Note that we now dump the
  60. * code first, just in case the backtrace kills us.
  61. */
  62. fs = get_fs();
  63. set_fs(KERNEL_DS);
  64. pr_emerg("Code: ");
  65. for (i = -4; i < 1; i++) {
  66. unsigned int val, bad;
  67. bad = __get_user(val, &((u32 *) addr)[i]);
  68. if (!bad) {
  69. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  70. } else {
  71. p += sprintf(p, "bad PC value");
  72. break;
  73. }
  74. }
  75. pr_emerg("Code: %s\n", str);
  76. set_fs(fs);
  77. }
  78. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  79. #include <linux/ftrace.h>
  80. static void
  81. get_real_ret_addr(unsigned long *addr, struct task_struct *tsk, int *graph)
  82. {
  83. if (*addr == (unsigned long)return_to_handler) {
  84. int index = tsk->curr_ret_stack;
  85. if (tsk->ret_stack && index >= *graph) {
  86. index -= *graph;
  87. *addr = tsk->ret_stack[index].ret;
  88. (*graph)++;
  89. }
  90. }
  91. }
  92. #else
  93. static inline void
  94. get_real_ret_addr(unsigned long *addr, struct task_struct *tsk, int *graph)
  95. {
  96. }
  97. #endif
  98. #define LOOP_TIMES (100)
  99. static void __dump(struct task_struct *tsk, unsigned long *base_reg)
  100. {
  101. unsigned long ret_addr;
  102. int cnt = LOOP_TIMES, graph = 0;
  103. pr_emerg("Call Trace:\n");
  104. if (!IS_ENABLED(CONFIG_FRAME_POINTER)) {
  105. while (!kstack_end(base_reg)) {
  106. ret_addr = *base_reg++;
  107. if (__kernel_text_address(ret_addr)) {
  108. get_real_ret_addr(&ret_addr, tsk, &graph);
  109. print_ip_sym(ret_addr);
  110. }
  111. if (--cnt < 0)
  112. break;
  113. }
  114. } else {
  115. while (!kstack_end((void *)base_reg) &&
  116. !((unsigned long)base_reg & 0x3) &&
  117. ((unsigned long)base_reg >= TASK_SIZE)) {
  118. unsigned long next_fp;
  119. #if !defined(NDS32_ABI_2)
  120. ret_addr = base_reg[0];
  121. next_fp = base_reg[1];
  122. #else
  123. ret_addr = base_reg[-1];
  124. next_fp = base_reg[FP_OFFSET];
  125. #endif
  126. if (__kernel_text_address(ret_addr)) {
  127. get_real_ret_addr(&ret_addr, tsk, &graph);
  128. print_ip_sym(ret_addr);
  129. }
  130. if (--cnt < 0)
  131. break;
  132. base_reg = (unsigned long *)next_fp;
  133. }
  134. }
  135. pr_emerg("\n");
  136. }
  137. void show_stack(struct task_struct *tsk, unsigned long *sp)
  138. {
  139. unsigned long *base_reg;
  140. if (!tsk)
  141. tsk = current;
  142. if (!IS_ENABLED(CONFIG_FRAME_POINTER)) {
  143. if (tsk != current)
  144. base_reg = (unsigned long *)(tsk->thread.cpu_context.sp);
  145. else
  146. __asm__ __volatile__("\tori\t%0, $sp, #0\n":"=r"(base_reg));
  147. } else {
  148. if (tsk != current)
  149. base_reg = (unsigned long *)(tsk->thread.cpu_context.fp);
  150. else
  151. __asm__ __volatile__("\tori\t%0, $fp, #0\n":"=r"(base_reg));
  152. }
  153. __dump(tsk, base_reg);
  154. barrier();
  155. }
  156. DEFINE_SPINLOCK(die_lock);
  157. /*
  158. * This function is protected against re-entrancy.
  159. */
  160. void die(const char *str, struct pt_regs *regs, int err)
  161. {
  162. struct task_struct *tsk = current;
  163. static int die_counter;
  164. console_verbose();
  165. spin_lock_irq(&die_lock);
  166. bust_spinlocks(1);
  167. pr_emerg("Internal error: %s: %x [#%d]\n", str, err, ++die_counter);
  168. print_modules();
  169. pr_emerg("CPU: %i\n", smp_processor_id());
  170. show_regs(regs);
  171. pr_emerg("Process %s (pid: %d, stack limit = 0x%p)\n",
  172. tsk->comm, tsk->pid, task_thread_info(tsk) + 1);
  173. if (!user_mode(regs) || in_interrupt()) {
  174. dump_mem("Stack: ", regs->sp,
  175. THREAD_SIZE + (unsigned long)task_thread_info(tsk));
  176. dump_instr(regs);
  177. dump_stack();
  178. }
  179. bust_spinlocks(0);
  180. spin_unlock_irq(&die_lock);
  181. do_exit(SIGSEGV);
  182. }
  183. EXPORT_SYMBOL(die);
  184. void die_if_kernel(const char *str, struct pt_regs *regs, int err)
  185. {
  186. if (user_mode(regs))
  187. return;
  188. die(str, regs, err);
  189. }
  190. int bad_syscall(int n, struct pt_regs *regs)
  191. {
  192. siginfo_t info;
  193. if (current->personality != PER_LINUX) {
  194. send_sig(SIGSEGV, current, 1);
  195. return regs->uregs[0];
  196. }
  197. info.si_signo = SIGILL;
  198. info.si_errno = 0;
  199. info.si_code = ILL_ILLTRP;
  200. info.si_addr = (void __user *)instruction_pointer(regs) - 4;
  201. force_sig_info(SIGILL, &info, current);
  202. die_if_kernel("Oops - bad syscall", regs, n);
  203. return regs->uregs[0];
  204. }
  205. void __pte_error(const char *file, int line, unsigned long val)
  206. {
  207. pr_emerg("%s:%d: bad pte %08lx.\n", file, line, val);
  208. }
  209. void __pmd_error(const char *file, int line, unsigned long val)
  210. {
  211. pr_emerg("%s:%d: bad pmd %08lx.\n", file, line, val);
  212. }
  213. void __pgd_error(const char *file, int line, unsigned long val)
  214. {
  215. pr_emerg("%s:%d: bad pgd %08lx.\n", file, line, val);
  216. }
  217. extern char *exception_vector, *exception_vector_end;
  218. void __init trap_init(void)
  219. {
  220. return;
  221. }
  222. void __init early_trap_init(void)
  223. {
  224. unsigned long ivb = 0;
  225. unsigned long base = PAGE_OFFSET;
  226. memcpy((unsigned long *)base, (unsigned long *)&exception_vector,
  227. ((unsigned long)&exception_vector_end -
  228. (unsigned long)&exception_vector));
  229. ivb = __nds32__mfsr(NDS32_SR_IVB);
  230. /* Check platform support. */
  231. if (((ivb & IVB_mskNIVIC) >> IVB_offNIVIC) < 2)
  232. panic
  233. ("IVIC mode is not allowed on the platform with interrupt controller\n");
  234. __nds32__mtsr((ivb & ~IVB_mskESZ) | (IVB_valESZ16 << IVB_offESZ) |
  235. IVB_BASE, NDS32_SR_IVB);
  236. __nds32__mtsr(INT_MASK_INITAIAL_VAL, NDS32_SR_INT_MASK);
  237. /*
  238. * 0x800 = 128 vectors * 16byte.
  239. * It should be enough to flush a page.
  240. */
  241. cpu_cache_wbinval_page(base, true);
  242. }
  243. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  244. int error_code, int si_code)
  245. {
  246. struct siginfo info;
  247. tsk->thread.trap_no = ENTRY_DEBUG_RELATED;
  248. tsk->thread.error_code = error_code;
  249. memset(&info, 0, sizeof(info));
  250. info.si_signo = SIGTRAP;
  251. info.si_code = si_code;
  252. info.si_addr = (void __user *)instruction_pointer(regs);
  253. force_sig_info(SIGTRAP, &info, tsk);
  254. }
  255. void do_debug_trap(unsigned long entry, unsigned long addr,
  256. unsigned long type, struct pt_regs *regs)
  257. {
  258. if (notify_die(DIE_OOPS, "Oops", regs, addr, type, SIGTRAP)
  259. == NOTIFY_STOP)
  260. return;
  261. if (user_mode(regs)) {
  262. /* trap_signal */
  263. send_sigtrap(current, regs, 0, TRAP_BRKPT);
  264. } else {
  265. /* kernel_trap */
  266. if (!fixup_exception(regs))
  267. die("unexpected kernel_trap", regs, 0);
  268. }
  269. }
  270. void unhandled_interruption(struct pt_regs *regs)
  271. {
  272. siginfo_t si;
  273. pr_emerg("unhandled_interruption\n");
  274. show_regs(regs);
  275. if (!user_mode(regs))
  276. do_exit(SIGKILL);
  277. si.si_signo = SIGKILL;
  278. si.si_errno = 0;
  279. force_sig_info(SIGKILL, &si, current);
  280. }
  281. void unhandled_exceptions(unsigned long entry, unsigned long addr,
  282. unsigned long type, struct pt_regs *regs)
  283. {
  284. siginfo_t si;
  285. pr_emerg("Unhandled Exception: entry: %lx addr:%lx itype:%lx\n", entry,
  286. addr, type);
  287. show_regs(regs);
  288. if (!user_mode(regs))
  289. do_exit(SIGKILL);
  290. si.si_signo = SIGKILL;
  291. si.si_errno = 0;
  292. si.si_addr = (void *)addr;
  293. force_sig_info(SIGKILL, &si, current);
  294. }
  295. extern int do_page_fault(unsigned long entry, unsigned long addr,
  296. unsigned int error_code, struct pt_regs *regs);
  297. /*
  298. * 2:DEF dispatch for TLB MISC exception handler
  299. */
  300. void do_dispatch_tlb_misc(unsigned long entry, unsigned long addr,
  301. unsigned long type, struct pt_regs *regs)
  302. {
  303. type = type & (ITYPE_mskINST | ITYPE_mskETYPE);
  304. if ((type & ITYPE_mskETYPE) < 5) {
  305. /* Permission exceptions */
  306. do_page_fault(entry, addr, type, regs);
  307. } else
  308. unhandled_exceptions(entry, addr, type, regs);
  309. }
  310. void do_revinsn(struct pt_regs *regs)
  311. {
  312. siginfo_t si;
  313. pr_emerg("Reserved Instruction\n");
  314. show_regs(regs);
  315. if (!user_mode(regs))
  316. do_exit(SIGILL);
  317. si.si_signo = SIGILL;
  318. si.si_errno = 0;
  319. force_sig_info(SIGILL, &si, current);
  320. }
  321. #ifdef CONFIG_ALIGNMENT_TRAP
  322. extern int unalign_access_mode;
  323. extern int do_unaligned_access(unsigned long addr, struct pt_regs *regs);
  324. #endif
  325. void do_dispatch_general(unsigned long entry, unsigned long addr,
  326. unsigned long itype, struct pt_regs *regs,
  327. unsigned long oipc)
  328. {
  329. unsigned int swid = itype >> ITYPE_offSWID;
  330. unsigned long type = itype & (ITYPE_mskINST | ITYPE_mskETYPE);
  331. if (type == ETYPE_ALIGNMENT_CHECK) {
  332. #ifdef CONFIG_ALIGNMENT_TRAP
  333. /* Alignment check */
  334. if (user_mode(regs) && unalign_access_mode) {
  335. int ret;
  336. ret = do_unaligned_access(addr, regs);
  337. if (ret == 0)
  338. return;
  339. if (ret == -EFAULT)
  340. pr_emerg
  341. ("Unhandled unaligned access exception\n");
  342. }
  343. #endif
  344. do_page_fault(entry, addr, type, regs);
  345. } else if (type == ETYPE_RESERVED_INSTRUCTION) {
  346. /* Reserved instruction */
  347. do_revinsn(regs);
  348. } else if (type == ETYPE_TRAP && swid == SWID_RAISE_INTERRUPT_LEVEL) {
  349. /* trap, used on v3 EDM target debugging workaround */
  350. /*
  351. * DIPC(OIPC) is passed as parameter before
  352. * interrupt is enabled, so the DIPC will not be corrupted
  353. * even though interrupts are coming in
  354. */
  355. /*
  356. * 1. update ipc
  357. * 2. update pt_regs ipc with oipc
  358. * 3. update pt_regs ipsw (clear DEX)
  359. */
  360. __asm__ volatile ("mtsr %0, $IPC\n\t"::"r" (oipc));
  361. regs->ipc = oipc;
  362. if (regs->pipsw & PSW_mskDEX) {
  363. pr_emerg
  364. ("Nested Debug exception is possibly happened\n");
  365. pr_emerg("ipc:%08x pipc:%08x\n",
  366. (unsigned int)regs->ipc,
  367. (unsigned int)regs->pipc);
  368. }
  369. do_debug_trap(entry, addr, itype, regs);
  370. regs->ipsw &= ~PSW_mskDEX;
  371. } else
  372. unhandled_exceptions(entry, addr, type, regs);
  373. }