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>
17 #include <linux/ptrace.h>
18 #include <nds32_intrinsic.h>
20 extern void show_pte(struct mm_struct *mm, unsigned long addr);
23 * Dump out the contents of some memory nicely...
25 void dump_mem(const char *lvl, unsigned long bottom, unsigned long top)
30 pr_emerg("%s(0x%08lx to 0x%08lx)\n", lvl, bottom, top);
32 for (first = bottom & ~31; first < top; first += 32) {
34 char str[sizeof(" 12345678") * 8 + 1];
36 memset(str, ' ', sizeof(str));
37 str[sizeof(str) - 1] = '\0';
39 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
40 if (p >= bottom && p < top) {
43 if (get_kernel_nofault(val,
44 (unsigned long *)p) == 0)
45 sprintf(str + i * 9, " %08lx", val);
47 sprintf(str + i * 9, " ????????");
50 pr_emerg("%s%04lx:%s\n", lvl, first & 0xffff, str);
54 EXPORT_SYMBOL(dump_mem);
56 #define LOOP_TIMES (100)
57 static void __dump(struct task_struct *tsk, unsigned long *base_reg,
60 unsigned long ret_addr;
61 int cnt = LOOP_TIMES, graph = 0;
62 printk("%sCall Trace:\n", loglvl);
63 if (!IS_ENABLED(CONFIG_FRAME_POINTER)) {
64 while (!kstack_end(base_reg)) {
65 ret_addr = *base_reg++;
66 if (__kernel_text_address(ret_addr)) {
67 ret_addr = ftrace_graph_ret_addr(
68 tsk, &graph, ret_addr, NULL);
69 print_ip_sym(loglvl, ret_addr);
75 while (!kstack_end((void *)base_reg) &&
76 !((unsigned long)base_reg & 0x3) &&
77 ((unsigned long)base_reg >= TASK_SIZE)) {
78 unsigned long next_fp;
79 ret_addr = base_reg[LP_OFFSET];
80 next_fp = base_reg[FP_OFFSET];
81 if (__kernel_text_address(ret_addr)) {
83 ret_addr = ftrace_graph_ret_addr(
84 tsk, &graph, ret_addr, NULL);
85 print_ip_sym(loglvl, ret_addr);
89 base_reg = (unsigned long *)next_fp;
92 printk("%s\n", loglvl);
95 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
97 unsigned long *base_reg;
101 if (!IS_ENABLED(CONFIG_FRAME_POINTER)) {
103 base_reg = (unsigned long *)(tsk->thread.cpu_context.sp);
105 __asm__ __volatile__("\tori\t%0, $sp, #0\n":"=r"(base_reg));
108 base_reg = (unsigned long *)(tsk->thread.cpu_context.fp);
110 __asm__ __volatile__("\tori\t%0, $fp, #0\n":"=r"(base_reg));
112 __dump(tsk, base_reg, loglvl);
116 DEFINE_SPINLOCK(die_lock);
119 * This function is protected against re-entrancy.
121 void die(const char *str, struct pt_regs *regs, int err)
123 struct task_struct *tsk = current;
124 static int die_counter;
127 spin_lock_irq(&die_lock);
130 pr_emerg("Internal error: %s: %x [#%d]\n", str, err, ++die_counter);
132 pr_emerg("CPU: %i\n", smp_processor_id());
134 pr_emerg("Process %s (pid: %d, stack limit = 0x%p)\n",
135 tsk->comm, tsk->pid, end_of_stack(tsk));
137 if (!user_mode(regs) || in_interrupt()) {
138 dump_mem("Stack: ", regs->sp, (regs->sp + PAGE_SIZE) & PAGE_MASK);
143 spin_unlock_irq(&die_lock);
149 void die_if_kernel(const char *str, struct pt_regs *regs, int err)
157 int bad_syscall(int n, struct pt_regs *regs)
159 if (current->personality != PER_LINUX) {
160 send_sig(SIGSEGV, current, 1);
161 return regs->uregs[0];
164 force_sig_fault(SIGILL, ILL_ILLTRP,
165 (void __user *)instruction_pointer(regs) - 4);
166 die_if_kernel("Oops - bad syscall", regs, n);
167 return regs->uregs[0];
170 void __pte_error(const char *file, int line, unsigned long val)
172 pr_emerg("%s:%d: bad pte %08lx.\n", file, line, val);
175 void __pmd_error(const char *file, int line, unsigned long val)
177 pr_emerg("%s:%d: bad pmd %08lx.\n", file, line, val);
180 void __pgd_error(const char *file, int line, unsigned long val)
182 pr_emerg("%s:%d: bad pgd %08lx.\n", file, line, val);
185 extern char *exception_vector, *exception_vector_end;
186 void __init trap_init(void)
191 void __init early_trap_init(void)
193 unsigned long ivb = 0;
194 unsigned long base = PAGE_OFFSET;
196 memcpy((unsigned long *)base, (unsigned long *)&exception_vector,
197 ((unsigned long)&exception_vector_end -
198 (unsigned long)&exception_vector));
199 ivb = __nds32__mfsr(NDS32_SR_IVB);
200 /* Check platform support. */
201 if (((ivb & IVB_mskNIVIC) >> IVB_offNIVIC) < 2)
203 ("IVIC mode is not allowed on the platform with interrupt controller\n");
204 __nds32__mtsr((ivb & ~IVB_mskESZ) | (IVB_valESZ16 << IVB_offESZ) |
205 IVB_BASE, NDS32_SR_IVB);
206 __nds32__mtsr(INT_MASK_INITAIAL_VAL, NDS32_SR_INT_MASK);
209 * 0x800 = 128 vectors * 16byte.
210 * It should be enough to flush a page.
212 cpu_cache_wbinval_page(base, true);
215 static void send_sigtrap(struct pt_regs *regs, int error_code, int si_code)
217 struct task_struct *tsk = current;
219 tsk->thread.trap_no = ENTRY_DEBUG_RELATED;
220 tsk->thread.error_code = error_code;
222 force_sig_fault(SIGTRAP, si_code,
223 (void __user *)instruction_pointer(regs));
226 void do_debug_trap(unsigned long entry, unsigned long addr,
227 unsigned long type, struct pt_regs *regs)
229 if (notify_die(DIE_OOPS, "Oops", regs, addr, type, SIGTRAP)
233 if (user_mode(regs)) {
235 send_sigtrap(regs, 0, TRAP_BRKPT);
238 if (!fixup_exception(regs))
239 die("unexpected kernel_trap", regs, 0);
243 void unhandled_interruption(struct pt_regs *regs)
245 pr_emerg("unhandled_interruption\n");
247 if (!user_mode(regs))
252 void unhandled_exceptions(unsigned long entry, unsigned long addr,
253 unsigned long type, struct pt_regs *regs)
255 pr_emerg("Unhandled Exception: entry: %lx addr:%lx itype:%lx\n", entry,
258 if (!user_mode(regs))
263 extern int do_page_fault(unsigned long entry, unsigned long addr,
264 unsigned int error_code, struct pt_regs *regs);
267 * 2:DEF dispatch for TLB MISC exception handler
270 void do_dispatch_tlb_misc(unsigned long entry, unsigned long addr,
271 unsigned long type, struct pt_regs *regs)
273 type = type & (ITYPE_mskINST | ITYPE_mskETYPE);
274 if ((type & ITYPE_mskETYPE) < 5) {
275 /* Permission exceptions */
276 do_page_fault(entry, addr, type, regs);
278 unhandled_exceptions(entry, addr, type, regs);
281 void do_revinsn(struct pt_regs *regs)
283 pr_emerg("Reserved Instruction\n");
285 if (!user_mode(regs))
290 #ifdef CONFIG_ALIGNMENT_TRAP
291 extern int unalign_access_mode;
292 extern int do_unaligned_access(unsigned long addr, struct pt_regs *regs);
294 void do_dispatch_general(unsigned long entry, unsigned long addr,
295 unsigned long itype, struct pt_regs *regs,
298 unsigned int swid = itype >> ITYPE_offSWID;
299 unsigned long type = itype & (ITYPE_mskINST | ITYPE_mskETYPE);
300 if (type == ETYPE_ALIGNMENT_CHECK) {
301 #ifdef CONFIG_ALIGNMENT_TRAP
302 /* Alignment check */
303 if (user_mode(regs) && unalign_access_mode) {
305 ret = do_unaligned_access(addr, regs);
312 ("Unhandled unaligned access exception\n");
315 do_page_fault(entry, addr, type, regs);
316 } else if (type == ETYPE_RESERVED_INSTRUCTION) {
317 /* Reserved instruction */
319 } else if (type == ETYPE_COPROCESSOR) {
321 #if IS_ENABLED(CONFIG_FPU)
322 unsigned int fucop_exist = __nds32__mfsr(NDS32_SR_FUCOP_EXIST);
323 unsigned int cpid = ((itype & ITYPE_mskCPID) >> ITYPE_offCPID);
325 if ((cpid == FPU_CPID) &&
326 (fucop_exist & FUCOP_EXIST_mskCP0ISFPU)) {
327 unsigned int subtype = (itype & ITYPE_mskSTYPE);
329 if (true == do_fpu_exception(subtype, regs))
333 unhandled_exceptions(entry, addr, type, regs);
334 } else if (type == ETYPE_TRAP && swid == SWID_RAISE_INTERRUPT_LEVEL) {
335 /* trap, used on v3 EDM target debugging workaround */
337 * DIPC(OIPC) is passed as parameter before
338 * interrupt is enabled, so the DIPC will not be corrupted
339 * even though interrupts are coming in
343 * 2. update pt_regs ipc with oipc
344 * 3. update pt_regs ipsw (clear DEX)
346 __asm__ volatile ("mtsr %0, $IPC\n\t"::"r" (oipc));
348 if (regs->pipsw & PSW_mskDEX) {
350 ("Nested Debug exception is possibly happened\n");
351 pr_emerg("ipc:%08x pipc:%08x\n",
352 (unsigned int)regs->ipc,
353 (unsigned int)regs->pipc);
355 do_debug_trap(entry, addr, itype, regs);
356 regs->ipsw &= ~PSW_mskDEX;
358 unhandled_exceptions(entry, addr, type, regs);