1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2005-2017 Andes Technology Corporation
4 #include <linux/module.h>
5 #include <linux/personality.h>
6 #include <linux/kallsyms.h>
7 #include <linux/hardirq.h>
8 #include <linux/kdebug.h>
9 #include <linux/sched/task_stack.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
13 #include <asm/proc-fns.h>
14 #include <asm/unistd.h>
16 #include <linux/ptrace.h>
17 #include <nds32_intrinsic.h>
19 extern void show_pte(struct mm_struct *mm, unsigned long addr);
22 * Dump out the contents of some memory nicely...
24 void dump_mem(const char *lvl, unsigned long bottom, unsigned long top)
31 * We need to switch to kernel mode so that we can use __get_user
32 * to safely read from kernel space. Note that we now dump the
33 * code first, just in case the backtrace kills us.
38 pr_emerg("%s(0x%08lx to 0x%08lx)\n", lvl, bottom, top);
40 for (first = bottom & ~31; first < top; first += 32) {
42 char str[sizeof(" 12345678") * 8 + 1];
44 memset(str, ' ', sizeof(str));
45 str[sizeof(str) - 1] = '\0';
47 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
48 if (p >= bottom && p < top) {
50 if (__get_user(val, (unsigned long *)p) == 0)
51 sprintf(str + i * 9, " %08lx", val);
53 sprintf(str + i * 9, " ????????");
56 pr_emerg("%s%04lx:%s\n", lvl, first & 0xffff, str);
62 EXPORT_SYMBOL(dump_mem);
64 static void dump_instr(struct pt_regs *regs)
66 unsigned long addr = instruction_pointer(regs);
68 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
73 * We need to switch to kernel mode so that we can use __get_user
74 * to safely read from kernel space. Note that we now dump the
75 * code first, just in case the backtrace kills us.
81 for (i = -4; i < 1; i++) {
82 unsigned int val, bad;
84 bad = __get_user(val, &((u32 *) addr)[i]);
87 p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
89 p += sprintf(p, "bad PC value");
93 pr_emerg("Code: %s\n", str);
98 #define LOOP_TIMES (100)
99 static void __dump(struct task_struct *tsk, unsigned long *base_reg)
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 ret_addr = ftrace_graph_ret_addr(
109 tsk, &graph, ret_addr, NULL);
110 print_ip_sym(ret_addr);
116 while (!kstack_end((void *)base_reg) &&
117 !((unsigned long)base_reg & 0x3) &&
118 ((unsigned long)base_reg >= TASK_SIZE)) {
119 unsigned long next_fp;
120 ret_addr = base_reg[LP_OFFSET];
121 next_fp = base_reg[FP_OFFSET];
122 if (__kernel_text_address(ret_addr)) {
124 ret_addr = ftrace_graph_ret_addr(
125 tsk, &graph, ret_addr, NULL);
126 print_ip_sym(ret_addr);
130 base_reg = (unsigned long *)next_fp;
136 void show_stack(struct task_struct *tsk, unsigned long *sp)
138 unsigned long *base_reg;
142 if (!IS_ENABLED(CONFIG_FRAME_POINTER)) {
144 base_reg = (unsigned long *)(tsk->thread.cpu_context.sp);
146 __asm__ __volatile__("\tori\t%0, $sp, #0\n":"=r"(base_reg));
149 base_reg = (unsigned long *)(tsk->thread.cpu_context.fp);
151 __asm__ __volatile__("\tori\t%0, $fp, #0\n":"=r"(base_reg));
153 __dump(tsk, base_reg);
157 DEFINE_SPINLOCK(die_lock);
160 * This function is protected against re-entrancy.
162 void die(const char *str, struct pt_regs *regs, int err)
164 struct task_struct *tsk = current;
165 static int die_counter;
168 spin_lock_irq(&die_lock);
171 pr_emerg("Internal error: %s: %x [#%d]\n", str, err, ++die_counter);
173 pr_emerg("CPU: %i\n", smp_processor_id());
175 pr_emerg("Process %s (pid: %d, stack limit = 0x%p)\n",
176 tsk->comm, tsk->pid, end_of_stack(tsk));
178 if (!user_mode(regs) || in_interrupt()) {
179 dump_mem("Stack: ", regs->sp, (regs->sp + PAGE_SIZE) & PAGE_MASK);
185 spin_unlock_irq(&die_lock);
191 void die_if_kernel(const char *str, struct pt_regs *regs, int err)
199 int bad_syscall(int n, struct pt_regs *regs)
201 if (current->personality != PER_LINUX) {
202 send_sig(SIGSEGV, current, 1);
203 return regs->uregs[0];
206 force_sig_fault(SIGILL, ILL_ILLTRP,
207 (void __user *)instruction_pointer(regs) - 4, current);
208 die_if_kernel("Oops - bad syscall", regs, n);
209 return regs->uregs[0];
212 void __pte_error(const char *file, int line, unsigned long val)
214 pr_emerg("%s:%d: bad pte %08lx.\n", file, line, val);
217 void __pmd_error(const char *file, int line, unsigned long val)
219 pr_emerg("%s:%d: bad pmd %08lx.\n", file, line, val);
222 void __pgd_error(const char *file, int line, unsigned long val)
224 pr_emerg("%s:%d: bad pgd %08lx.\n", file, line, val);
227 extern char *exception_vector, *exception_vector_end;
228 void __init trap_init(void)
233 void __init early_trap_init(void)
235 unsigned long ivb = 0;
236 unsigned long base = PAGE_OFFSET;
238 memcpy((unsigned long *)base, (unsigned long *)&exception_vector,
239 ((unsigned long)&exception_vector_end -
240 (unsigned long)&exception_vector));
241 ivb = __nds32__mfsr(NDS32_SR_IVB);
242 /* Check platform support. */
243 if (((ivb & IVB_mskNIVIC) >> IVB_offNIVIC) < 2)
245 ("IVIC mode is not allowed on the platform with interrupt controller\n");
246 __nds32__mtsr((ivb & ~IVB_mskESZ) | (IVB_valESZ16 << IVB_offESZ) |
247 IVB_BASE, NDS32_SR_IVB);
248 __nds32__mtsr(INT_MASK_INITAIAL_VAL, NDS32_SR_INT_MASK);
251 * 0x800 = 128 vectors * 16byte.
252 * It should be enough to flush a page.
254 cpu_cache_wbinval_page(base, true);
257 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
258 int error_code, int si_code)
260 tsk->thread.trap_no = ENTRY_DEBUG_RELATED;
261 tsk->thread.error_code = error_code;
263 force_sig_fault(SIGTRAP, si_code,
264 (void __user *)instruction_pointer(regs), tsk);
267 void do_debug_trap(unsigned long entry, unsigned long addr,
268 unsigned long type, struct pt_regs *regs)
270 if (notify_die(DIE_OOPS, "Oops", regs, addr, type, SIGTRAP)
274 if (user_mode(regs)) {
276 send_sigtrap(current, regs, 0, TRAP_BRKPT);
279 if (!fixup_exception(regs))
280 die("unexpected kernel_trap", regs, 0);
284 void unhandled_interruption(struct pt_regs *regs)
286 pr_emerg("unhandled_interruption\n");
288 if (!user_mode(regs))
290 force_sig(SIGKILL, current);
293 void unhandled_exceptions(unsigned long entry, unsigned long addr,
294 unsigned long type, struct pt_regs *regs)
296 pr_emerg("Unhandled Exception: entry: %lx addr:%lx itype:%lx\n", entry,
299 if (!user_mode(regs))
301 force_sig(SIGKILL, current);
304 extern int do_page_fault(unsigned long entry, unsigned long addr,
305 unsigned int error_code, struct pt_regs *regs);
308 * 2:DEF dispatch for TLB MISC exception handler
311 void do_dispatch_tlb_misc(unsigned long entry, unsigned long addr,
312 unsigned long type, struct pt_regs *regs)
314 type = type & (ITYPE_mskINST | ITYPE_mskETYPE);
315 if ((type & ITYPE_mskETYPE) < 5) {
316 /* Permission exceptions */
317 do_page_fault(entry, addr, type, regs);
319 unhandled_exceptions(entry, addr, type, regs);
322 void do_revinsn(struct pt_regs *regs)
324 pr_emerg("Reserved Instruction\n");
326 if (!user_mode(regs))
328 force_sig(SIGILL, current);
331 #ifdef CONFIG_ALIGNMENT_TRAP
332 extern int unalign_access_mode;
333 extern int do_unaligned_access(unsigned long addr, struct pt_regs *regs);
335 void do_dispatch_general(unsigned long entry, unsigned long addr,
336 unsigned long itype, struct pt_regs *regs,
339 unsigned int swid = itype >> ITYPE_offSWID;
340 unsigned long type = itype & (ITYPE_mskINST | ITYPE_mskETYPE);
341 if (type == ETYPE_ALIGNMENT_CHECK) {
342 #ifdef CONFIG_ALIGNMENT_TRAP
343 /* Alignment check */
344 if (user_mode(regs) && unalign_access_mode) {
346 ret = do_unaligned_access(addr, regs);
353 ("Unhandled unaligned access exception\n");
356 do_page_fault(entry, addr, type, regs);
357 } else if (type == ETYPE_RESERVED_INSTRUCTION) {
358 /* Reserved instruction */
360 } else if (type == ETYPE_TRAP && swid == SWID_RAISE_INTERRUPT_LEVEL) {
361 /* trap, used on v3 EDM target debugging workaround */
363 * DIPC(OIPC) is passed as parameter before
364 * interrupt is enabled, so the DIPC will not be corrupted
365 * even though interrupts are coming in
369 * 2. update pt_regs ipc with oipc
370 * 3. update pt_regs ipsw (clear DEX)
372 __asm__ volatile ("mtsr %0, $IPC\n\t"::"r" (oipc));
374 if (regs->pipsw & PSW_mskDEX) {
376 ("Nested Debug exception is possibly happened\n");
377 pr_emerg("ipc:%08x pipc:%08x\n",
378 (unsigned int)regs->ipc,
379 (unsigned int)regs->pipc);
381 do_debug_trap(entry, addr, itype, regs);
382 regs->ipsw &= ~PSW_mskDEX;
384 unhandled_exceptions(entry, addr, type, regs);