Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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