traps.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. if (current->personality != PER_LINUX) {
  193. send_sig(SIGSEGV, current, 1);
  194. return regs->uregs[0];
  195. }
  196. force_sig_fault(SIGILL, ILL_ILLTRP,
  197. (void __user *)instruction_pointer(regs) - 4, current);
  198. die_if_kernel("Oops - bad syscall", regs, n);
  199. return regs->uregs[0];
  200. }
  201. void __pte_error(const char *file, int line, unsigned long val)
  202. {
  203. pr_emerg("%s:%d: bad pte %08lx.\n", file, line, val);
  204. }
  205. void __pmd_error(const char *file, int line, unsigned long val)
  206. {
  207. pr_emerg("%s:%d: bad pmd %08lx.\n", file, line, val);
  208. }
  209. void __pgd_error(const char *file, int line, unsigned long val)
  210. {
  211. pr_emerg("%s:%d: bad pgd %08lx.\n", file, line, val);
  212. }
  213. extern char *exception_vector, *exception_vector_end;
  214. void __init trap_init(void)
  215. {
  216. return;
  217. }
  218. void __init early_trap_init(void)
  219. {
  220. unsigned long ivb = 0;
  221. unsigned long base = PAGE_OFFSET;
  222. memcpy((unsigned long *)base, (unsigned long *)&exception_vector,
  223. ((unsigned long)&exception_vector_end -
  224. (unsigned long)&exception_vector));
  225. ivb = __nds32__mfsr(NDS32_SR_IVB);
  226. /* Check platform support. */
  227. if (((ivb & IVB_mskNIVIC) >> IVB_offNIVIC) < 2)
  228. panic
  229. ("IVIC mode is not allowed on the platform with interrupt controller\n");
  230. __nds32__mtsr((ivb & ~IVB_mskESZ) | (IVB_valESZ16 << IVB_offESZ) |
  231. IVB_BASE, NDS32_SR_IVB);
  232. __nds32__mtsr(INT_MASK_INITAIAL_VAL, NDS32_SR_INT_MASK);
  233. /*
  234. * 0x800 = 128 vectors * 16byte.
  235. * It should be enough to flush a page.
  236. */
  237. cpu_cache_wbinval_page(base, true);
  238. }
  239. void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  240. int error_code, int si_code)
  241. {
  242. tsk->thread.trap_no = ENTRY_DEBUG_RELATED;
  243. tsk->thread.error_code = error_code;
  244. force_sig_fault(SIGTRAP, si_code,
  245. (void __user *)instruction_pointer(regs), tsk);
  246. }
  247. void do_debug_trap(unsigned long entry, unsigned long addr,
  248. unsigned long type, struct pt_regs *regs)
  249. {
  250. if (notify_die(DIE_OOPS, "Oops", regs, addr, type, SIGTRAP)
  251. == NOTIFY_STOP)
  252. return;
  253. if (user_mode(regs)) {
  254. /* trap_signal */
  255. send_sigtrap(current, regs, 0, TRAP_BRKPT);
  256. } else {
  257. /* kernel_trap */
  258. if (!fixup_exception(regs))
  259. die("unexpected kernel_trap", regs, 0);
  260. }
  261. }
  262. void unhandled_interruption(struct pt_regs *regs)
  263. {
  264. pr_emerg("unhandled_interruption\n");
  265. show_regs(regs);
  266. if (!user_mode(regs))
  267. do_exit(SIGKILL);
  268. force_sig(SIGKILL, current);
  269. }
  270. void unhandled_exceptions(unsigned long entry, unsigned long addr,
  271. unsigned long type, struct pt_regs *regs)
  272. {
  273. pr_emerg("Unhandled Exception: entry: %lx addr:%lx itype:%lx\n", entry,
  274. addr, type);
  275. show_regs(regs);
  276. if (!user_mode(regs))
  277. do_exit(SIGKILL);
  278. force_sig(SIGKILL, current);
  279. }
  280. extern int do_page_fault(unsigned long entry, unsigned long addr,
  281. unsigned int error_code, struct pt_regs *regs);
  282. /*
  283. * 2:DEF dispatch for TLB MISC exception handler
  284. */
  285. void do_dispatch_tlb_misc(unsigned long entry, unsigned long addr,
  286. unsigned long type, struct pt_regs *regs)
  287. {
  288. type = type & (ITYPE_mskINST | ITYPE_mskETYPE);
  289. if ((type & ITYPE_mskETYPE) < 5) {
  290. /* Permission exceptions */
  291. do_page_fault(entry, addr, type, regs);
  292. } else
  293. unhandled_exceptions(entry, addr, type, regs);
  294. }
  295. void do_revinsn(struct pt_regs *regs)
  296. {
  297. pr_emerg("Reserved Instruction\n");
  298. show_regs(regs);
  299. if (!user_mode(regs))
  300. do_exit(SIGILL);
  301. force_sig(SIGILL, current);
  302. }
  303. #ifdef CONFIG_ALIGNMENT_TRAP
  304. extern int unalign_access_mode;
  305. extern int do_unaligned_access(unsigned long addr, struct pt_regs *regs);
  306. #endif
  307. void do_dispatch_general(unsigned long entry, unsigned long addr,
  308. unsigned long itype, struct pt_regs *regs,
  309. unsigned long oipc)
  310. {
  311. unsigned int swid = itype >> ITYPE_offSWID;
  312. unsigned long type = itype & (ITYPE_mskINST | ITYPE_mskETYPE);
  313. if (type == ETYPE_ALIGNMENT_CHECK) {
  314. #ifdef CONFIG_ALIGNMENT_TRAP
  315. /* Alignment check */
  316. if (user_mode(regs) && unalign_access_mode) {
  317. int ret;
  318. ret = do_unaligned_access(addr, regs);
  319. if (ret == 0)
  320. return;
  321. if (ret == -EFAULT)
  322. pr_emerg
  323. ("Unhandled unaligned access exception\n");
  324. }
  325. #endif
  326. do_page_fault(entry, addr, type, regs);
  327. } else if (type == ETYPE_RESERVED_INSTRUCTION) {
  328. /* Reserved instruction */
  329. do_revinsn(regs);
  330. } else if (type == ETYPE_TRAP && swid == SWID_RAISE_INTERRUPT_LEVEL) {
  331. /* trap, used on v3 EDM target debugging workaround */
  332. /*
  333. * DIPC(OIPC) is passed as parameter before
  334. * interrupt is enabled, so the DIPC will not be corrupted
  335. * even though interrupts are coming in
  336. */
  337. /*
  338. * 1. update ipc
  339. * 2. update pt_regs ipc with oipc
  340. * 3. update pt_regs ipsw (clear DEX)
  341. */
  342. __asm__ volatile ("mtsr %0, $IPC\n\t"::"r" (oipc));
  343. regs->ipc = oipc;
  344. if (regs->pipsw & PSW_mskDEX) {
  345. pr_emerg
  346. ("Nested Debug exception is possibly happened\n");
  347. pr_emerg("ipc:%08x pipc:%08x\n",
  348. (unsigned int)regs->ipc,
  349. (unsigned int)regs->pipc);
  350. }
  351. do_debug_trap(entry, addr, itype, regs);
  352. regs->ipsw &= ~PSW_mskDEX;
  353. } else
  354. unhandled_exceptions(entry, addr, type, regs);
  355. }