xref: /linux/arch/loongarch/kernel/traps.c (revision 1c9f8dff62d85ce00b0e99f774a84bd783af7cac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author: Huacai Chen <chenhuacai@loongson.cn>
4  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
5  */
6 #include <linux/bitfield.h>
7 #include <linux/bitops.h>
8 #include <linux/bug.h>
9 #include <linux/compiler.h>
10 #include <linux/context_tracking.h>
11 #include <linux/entry-common.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/kexec.h>
15 #include <linux/module.h>
16 #include <linux/extable.h>
17 #include <linux/mm.h>
18 #include <linux/sched/mm.h>
19 #include <linux/sched/debug.h>
20 #include <linux/smp.h>
21 #include <linux/spinlock.h>
22 #include <linux/kallsyms.h>
23 #include <linux/memblock.h>
24 #include <linux/interrupt.h>
25 #include <linux/ptrace.h>
26 #include <linux/kgdb.h>
27 #include <linux/kdebug.h>
28 #include <linux/kprobes.h>
29 #include <linux/notifier.h>
30 #include <linux/irq.h>
31 #include <linux/perf_event.h>
32 
33 #include <asm/addrspace.h>
34 #include <asm/bootinfo.h>
35 #include <asm/branch.h>
36 #include <asm/break.h>
37 #include <asm/cpu.h>
38 #include <asm/fpu.h>
39 #include <asm/inst.h>
40 #include <asm/loongarch.h>
41 #include <asm/mmu_context.h>
42 #include <asm/pgtable.h>
43 #include <asm/ptrace.h>
44 #include <asm/sections.h>
45 #include <asm/siginfo.h>
46 #include <asm/stacktrace.h>
47 #include <asm/tlb.h>
48 #include <asm/types.h>
49 #include <asm/unwind.h>
50 #include <asm/uprobes.h>
51 
52 #include "access-helper.h"
53 
54 extern asmlinkage void handle_ade(void);
55 extern asmlinkage void handle_ale(void);
56 extern asmlinkage void handle_bce(void);
57 extern asmlinkage void handle_sys(void);
58 extern asmlinkage void handle_bp(void);
59 extern asmlinkage void handle_ri(void);
60 extern asmlinkage void handle_fpu(void);
61 extern asmlinkage void handle_fpe(void);
62 extern asmlinkage void handle_lbt(void);
63 extern asmlinkage void handle_lsx(void);
64 extern asmlinkage void handle_lasx(void);
65 extern asmlinkage void handle_reserved(void);
66 extern asmlinkage void handle_watch(void);
67 extern asmlinkage void handle_vint(void);
68 
69 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs,
70 			   const char *loglvl, bool user)
71 {
72 	unsigned long addr;
73 	struct unwind_state state;
74 	struct pt_regs *pregs = (struct pt_regs *)regs;
75 
76 	if (!task)
77 		task = current;
78 
79 	printk("%sCall Trace:", loglvl);
80 	for (unwind_start(&state, task, pregs);
81 	      !unwind_done(&state); unwind_next_frame(&state)) {
82 		addr = unwind_get_return_address(&state);
83 		print_ip_sym(loglvl, addr);
84 	}
85 	printk("%s\n", loglvl);
86 }
87 
88 static void show_stacktrace(struct task_struct *task,
89 	const struct pt_regs *regs, const char *loglvl, bool user)
90 {
91 	int i;
92 	const int field = 2 * sizeof(unsigned long);
93 	unsigned long stackdata;
94 	unsigned long *sp = (unsigned long *)regs->regs[3];
95 
96 	printk("%sStack :", loglvl);
97 	i = 0;
98 	while ((unsigned long) sp & (PAGE_SIZE - 1)) {
99 		if (i && ((i % (64 / field)) == 0)) {
100 			pr_cont("\n");
101 			printk("%s       ", loglvl);
102 		}
103 		if (i > 39) {
104 			pr_cont(" ...");
105 			break;
106 		}
107 
108 		if (__get_addr(&stackdata, sp++, user)) {
109 			pr_cont(" (Bad stack address)");
110 			break;
111 		}
112 
113 		pr_cont(" %0*lx", field, stackdata);
114 		i++;
115 	}
116 	pr_cont("\n");
117 	show_backtrace(task, regs, loglvl, user);
118 }
119 
120 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
121 {
122 	struct pt_regs regs;
123 
124 	regs.csr_crmd = 0;
125 	if (sp) {
126 		regs.csr_era = 0;
127 		regs.regs[1] = 0;
128 		regs.regs[3] = (unsigned long)sp;
129 	} else {
130 		if (!task || task == current)
131 			prepare_frametrace(&regs);
132 		else {
133 			regs.csr_era = task->thread.reg01;
134 			regs.regs[1] = 0;
135 			regs.regs[3] = task->thread.reg03;
136 			regs.regs[22] = task->thread.reg22;
137 		}
138 	}
139 
140 	show_stacktrace(task, &regs, loglvl, false);
141 }
142 
143 static void show_code(unsigned int *pc, bool user)
144 {
145 	long i;
146 	unsigned int insn;
147 
148 	printk("Code:");
149 
150 	for(i = -3 ; i < 6 ; i++) {
151 		if (__get_inst(&insn, pc + i, user)) {
152 			pr_cont(" (Bad address in era)\n");
153 			break;
154 		}
155 		pr_cont("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
156 	}
157 	pr_cont("\n");
158 }
159 
160 static void print_bool_fragment(const char *key, unsigned long val, bool first)
161 {
162 	/* e.g. "+PG", "-DA" */
163 	pr_cont("%s%c%s", first ? "" : " ", val ? '+' : '-', key);
164 }
165 
166 static void print_plv_fragment(const char *key, int val)
167 {
168 	/* e.g. "PLV0", "PPLV3" */
169 	pr_cont("%s%d", key, val);
170 }
171 
172 static void print_memory_type_fragment(const char *key, unsigned long val)
173 {
174 	const char *humanized_type;
175 
176 	switch (val) {
177 	case 0:
178 		humanized_type = "SUC";
179 		break;
180 	case 1:
181 		humanized_type = "CC";
182 		break;
183 	case 2:
184 		humanized_type = "WUC";
185 		break;
186 	default:
187 		pr_cont(" %s=Reserved(%lu)", key, val);
188 		return;
189 	}
190 
191 	/* e.g. " DATM=WUC" */
192 	pr_cont(" %s=%s", key, humanized_type);
193 }
194 
195 static void print_intr_fragment(const char *key, unsigned long val)
196 {
197 	/* e.g. "LIE=0-1,3,5-7" */
198 	pr_cont("%s=%*pbl", key, EXCCODE_INT_NUM, &val);
199 }
200 
201 static void print_crmd(unsigned long x)
202 {
203 	printk(" CRMD: %08lx (", x);
204 	print_plv_fragment("PLV", (int) FIELD_GET(CSR_CRMD_PLV, x));
205 	print_bool_fragment("IE", FIELD_GET(CSR_CRMD_IE, x), false);
206 	print_bool_fragment("DA", FIELD_GET(CSR_CRMD_DA, x), false);
207 	print_bool_fragment("PG", FIELD_GET(CSR_CRMD_PG, x), false);
208 	print_memory_type_fragment("DACF", FIELD_GET(CSR_CRMD_DACF, x));
209 	print_memory_type_fragment("DACM", FIELD_GET(CSR_CRMD_DACM, x));
210 	print_bool_fragment("WE", FIELD_GET(CSR_CRMD_WE, x), false);
211 	pr_cont(")\n");
212 }
213 
214 static void print_prmd(unsigned long x)
215 {
216 	printk(" PRMD: %08lx (", x);
217 	print_plv_fragment("PPLV", (int) FIELD_GET(CSR_PRMD_PPLV, x));
218 	print_bool_fragment("PIE", FIELD_GET(CSR_PRMD_PIE, x), false);
219 	print_bool_fragment("PWE", FIELD_GET(CSR_PRMD_PWE, x), false);
220 	pr_cont(")\n");
221 }
222 
223 static void print_euen(unsigned long x)
224 {
225 	printk(" EUEN: %08lx (", x);
226 	print_bool_fragment("FPE", FIELD_GET(CSR_EUEN_FPEN, x), true);
227 	print_bool_fragment("SXE", FIELD_GET(CSR_EUEN_LSXEN, x), false);
228 	print_bool_fragment("ASXE", FIELD_GET(CSR_EUEN_LASXEN, x), false);
229 	print_bool_fragment("BTE", FIELD_GET(CSR_EUEN_LBTEN, x), false);
230 	pr_cont(")\n");
231 }
232 
233 static void print_ecfg(unsigned long x)
234 {
235 	printk(" ECFG: %08lx (", x);
236 	print_intr_fragment("LIE", FIELD_GET(CSR_ECFG_IM, x));
237 	pr_cont(" VS=%d)\n", (int) FIELD_GET(CSR_ECFG_VS, x));
238 }
239 
240 static const char *humanize_exc_name(unsigned int ecode, unsigned int esubcode)
241 {
242 	/*
243 	 * LoongArch users and developers are probably more familiar with
244 	 * those names found in the ISA manual, so we are going to print out
245 	 * the latter. This will require some mapping.
246 	 */
247 	switch (ecode) {
248 	case EXCCODE_RSV: return "INT";
249 	case EXCCODE_TLBL: return "PIL";
250 	case EXCCODE_TLBS: return "PIS";
251 	case EXCCODE_TLBI: return "PIF";
252 	case EXCCODE_TLBM: return "PME";
253 	case EXCCODE_TLBNR: return "PNR";
254 	case EXCCODE_TLBNX: return "PNX";
255 	case EXCCODE_TLBPE: return "PPI";
256 	case EXCCODE_ADE:
257 		switch (esubcode) {
258 		case EXSUBCODE_ADEF: return "ADEF";
259 		case EXSUBCODE_ADEM: return "ADEM";
260 		}
261 		break;
262 	case EXCCODE_ALE: return "ALE";
263 	case EXCCODE_BCE: return "BCE";
264 	case EXCCODE_SYS: return "SYS";
265 	case EXCCODE_BP: return "BRK";
266 	case EXCCODE_INE: return "INE";
267 	case EXCCODE_IPE: return "IPE";
268 	case EXCCODE_FPDIS: return "FPD";
269 	case EXCCODE_LSXDIS: return "SXD";
270 	case EXCCODE_LASXDIS: return "ASXD";
271 	case EXCCODE_FPE:
272 		switch (esubcode) {
273 		case EXCSUBCODE_FPE: return "FPE";
274 		case EXCSUBCODE_VFPE: return "VFPE";
275 		}
276 		break;
277 	case EXCCODE_WATCH:
278 		switch (esubcode) {
279 		case EXCSUBCODE_WPEF: return "WPEF";
280 		case EXCSUBCODE_WPEM: return "WPEM";
281 		}
282 		break;
283 	case EXCCODE_BTDIS: return "BTD";
284 	case EXCCODE_BTE: return "BTE";
285 	case EXCCODE_GSPR: return "GSPR";
286 	case EXCCODE_HVC: return "HVC";
287 	case EXCCODE_GCM:
288 		switch (esubcode) {
289 		case EXCSUBCODE_GCSC: return "GCSC";
290 		case EXCSUBCODE_GCHC: return "GCHC";
291 		}
292 		break;
293 	/*
294 	 * The manual did not mention the EXCCODE_SE case, but print out it
295 	 * nevertheless.
296 	 */
297 	case EXCCODE_SE: return "SE";
298 	}
299 
300 	return "???";
301 }
302 
303 static void print_estat(unsigned long x)
304 {
305 	unsigned int ecode = FIELD_GET(CSR_ESTAT_EXC, x);
306 	unsigned int esubcode = FIELD_GET(CSR_ESTAT_ESUBCODE, x);
307 
308 	printk("ESTAT: %08lx [%s] (", x, humanize_exc_name(ecode, esubcode));
309 	print_intr_fragment("IS", FIELD_GET(CSR_ESTAT_IS, x));
310 	pr_cont(" ECode=%d EsubCode=%d)\n", (int) ecode, (int) esubcode);
311 }
312 
313 static void __show_regs(const struct pt_regs *regs)
314 {
315 	const int field = 2 * sizeof(unsigned long);
316 	unsigned int exccode = FIELD_GET(CSR_ESTAT_EXC, regs->csr_estat);
317 
318 	show_regs_print_info(KERN_DEFAULT);
319 
320 	/* Print saved GPRs except $zero (substituting with PC/ERA) */
321 #define GPR_FIELD(x) field, regs->regs[x]
322 	printk("pc %0*lx ra %0*lx tp %0*lx sp %0*lx\n",
323 	       field, regs->csr_era, GPR_FIELD(1), GPR_FIELD(2), GPR_FIELD(3));
324 	printk("a0 %0*lx a1 %0*lx a2 %0*lx a3 %0*lx\n",
325 	       GPR_FIELD(4), GPR_FIELD(5), GPR_FIELD(6), GPR_FIELD(7));
326 	printk("a4 %0*lx a5 %0*lx a6 %0*lx a7 %0*lx\n",
327 	       GPR_FIELD(8), GPR_FIELD(9), GPR_FIELD(10), GPR_FIELD(11));
328 	printk("t0 %0*lx t1 %0*lx t2 %0*lx t3 %0*lx\n",
329 	       GPR_FIELD(12), GPR_FIELD(13), GPR_FIELD(14), GPR_FIELD(15));
330 	printk("t4 %0*lx t5 %0*lx t6 %0*lx t7 %0*lx\n",
331 	       GPR_FIELD(16), GPR_FIELD(17), GPR_FIELD(18), GPR_FIELD(19));
332 	printk("t8 %0*lx u0 %0*lx s9 %0*lx s0 %0*lx\n",
333 	       GPR_FIELD(20), GPR_FIELD(21), GPR_FIELD(22), GPR_FIELD(23));
334 	printk("s1 %0*lx s2 %0*lx s3 %0*lx s4 %0*lx\n",
335 	       GPR_FIELD(24), GPR_FIELD(25), GPR_FIELD(26), GPR_FIELD(27));
336 	printk("s5 %0*lx s6 %0*lx s7 %0*lx s8 %0*lx\n",
337 	       GPR_FIELD(28), GPR_FIELD(29), GPR_FIELD(30), GPR_FIELD(31));
338 
339 	/* The slot for $zero is reused as the syscall restart flag */
340 	if (regs->regs[0])
341 		printk("syscall restart flag: %0*lx\n", GPR_FIELD(0));
342 
343 	if (user_mode(regs)) {
344 		printk("   ra: %0*lx\n", GPR_FIELD(1));
345 		printk("  ERA: %0*lx\n", field, regs->csr_era);
346 	} else {
347 		printk("   ra: %0*lx %pS\n", GPR_FIELD(1), (void *) regs->regs[1]);
348 		printk("  ERA: %0*lx %pS\n", field, regs->csr_era, (void *) regs->csr_era);
349 	}
350 #undef GPR_FIELD
351 
352 	/* Print saved important CSRs */
353 	print_crmd(regs->csr_crmd);
354 	print_prmd(regs->csr_prmd);
355 	print_euen(regs->csr_euen);
356 	print_ecfg(regs->csr_ecfg);
357 	print_estat(regs->csr_estat);
358 
359 	if (exccode >= EXCCODE_TLBL && exccode <= EXCCODE_ALE)
360 		printk(" BADV: %0*lx\n", field, regs->csr_badvaddr);
361 
362 	printk(" PRID: %08x (%s, %s)\n", read_cpucfg(LOONGARCH_CPUCFG0),
363 	       cpu_family_string(), cpu_full_name_string());
364 }
365 
366 void show_regs(struct pt_regs *regs)
367 {
368 	__show_regs((struct pt_regs *)regs);
369 	dump_stack();
370 }
371 
372 void show_registers(struct pt_regs *regs)
373 {
374 	__show_regs(regs);
375 	print_modules();
376 	printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
377 	       current->comm, current->pid, current_thread_info(), current);
378 
379 	show_stacktrace(current, regs, KERN_DEFAULT, user_mode(regs));
380 	show_code((void *)regs->csr_era, user_mode(regs));
381 	printk("\n");
382 }
383 
384 static DEFINE_RAW_SPINLOCK(die_lock);
385 
386 void die(const char *str, struct pt_regs *regs)
387 {
388 	int ret;
389 	static int die_counter;
390 
391 	oops_enter();
392 
393 	ret = notify_die(DIE_OOPS, str, regs, 0,
394 			 current->thread.trap_nr, SIGSEGV);
395 
396 	console_verbose();
397 	raw_spin_lock_irq(&die_lock);
398 	bust_spinlocks(1);
399 
400 	printk("%s[#%d]:\n", str, ++die_counter);
401 	show_registers(regs);
402 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
403 	raw_spin_unlock_irq(&die_lock);
404 
405 	oops_exit();
406 
407 	if (ret == NOTIFY_STOP)
408 		return;
409 
410 	if (regs && kexec_should_crash(current))
411 		crash_kexec(regs);
412 
413 	if (in_interrupt())
414 		panic("Fatal exception in interrupt");
415 
416 	if (panic_on_oops)
417 		panic("Fatal exception");
418 
419 	make_task_dead(SIGSEGV);
420 }
421 
422 static inline void setup_vint_size(unsigned int size)
423 {
424 	unsigned int vs;
425 
426 	vs = ilog2(size/4);
427 
428 	if (vs == 0 || vs > 7)
429 		panic("vint_size %d Not support yet", vs);
430 
431 	csr_xchg32(vs<<CSR_ECFG_VS_SHIFT, CSR_ECFG_VS, LOONGARCH_CSR_ECFG);
432 }
433 
434 /*
435  * Send SIGFPE according to FCSR Cause bits, which must have already
436  * been masked against Enable bits.  This is impotant as Inexact can
437  * happen together with Overflow or Underflow, and `ptrace' can set
438  * any bits.
439  */
440 void force_fcsr_sig(unsigned long fcsr, void __user *fault_addr,
441 		     struct task_struct *tsk)
442 {
443 	int si_code = FPE_FLTUNK;
444 
445 	if (fcsr & FPU_CSR_INV_X)
446 		si_code = FPE_FLTINV;
447 	else if (fcsr & FPU_CSR_DIV_X)
448 		si_code = FPE_FLTDIV;
449 	else if (fcsr & FPU_CSR_OVF_X)
450 		si_code = FPE_FLTOVF;
451 	else if (fcsr & FPU_CSR_UDF_X)
452 		si_code = FPE_FLTUND;
453 	else if (fcsr & FPU_CSR_INE_X)
454 		si_code = FPE_FLTRES;
455 
456 	force_sig_fault(SIGFPE, si_code, fault_addr);
457 }
458 
459 int process_fpemu_return(int sig, void __user *fault_addr, unsigned long fcsr)
460 {
461 	int si_code;
462 
463 	switch (sig) {
464 	case 0:
465 		return 0;
466 
467 	case SIGFPE:
468 		force_fcsr_sig(fcsr, fault_addr, current);
469 		return 1;
470 
471 	case SIGBUS:
472 		force_sig_fault(SIGBUS, BUS_ADRERR, fault_addr);
473 		return 1;
474 
475 	case SIGSEGV:
476 		mmap_read_lock(current->mm);
477 		if (vma_lookup(current->mm, (unsigned long)fault_addr))
478 			si_code = SEGV_ACCERR;
479 		else
480 			si_code = SEGV_MAPERR;
481 		mmap_read_unlock(current->mm);
482 		force_sig_fault(SIGSEGV, si_code, fault_addr);
483 		return 1;
484 
485 	default:
486 		force_sig(sig);
487 		return 1;
488 	}
489 }
490 
491 /*
492  * Delayed fp exceptions when doing a lazy ctx switch
493  */
494 asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
495 {
496 	int sig;
497 	void __user *fault_addr;
498 	irqentry_state_t state = irqentry_enter(regs);
499 
500 	if (notify_die(DIE_FP, "FP exception", regs, 0, current->thread.trap_nr,
501 		       SIGFPE) == NOTIFY_STOP)
502 		goto out;
503 
504 	/* Clear FCSR.Cause before enabling interrupts */
505 	write_fcsr(LOONGARCH_FCSR0, fcsr & ~mask_fcsr_x(fcsr));
506 	local_irq_enable();
507 
508 	die_if_kernel("FP exception in kernel code", regs);
509 
510 	sig = SIGFPE;
511 	fault_addr = (void __user *) regs->csr_era;
512 
513 	/* Send a signal if required.  */
514 	process_fpemu_return(sig, fault_addr, fcsr);
515 
516 out:
517 	local_irq_disable();
518 	irqentry_exit(regs, state);
519 }
520 
521 asmlinkage void noinstr do_ade(struct pt_regs *regs)
522 {
523 	irqentry_state_t state = irqentry_enter(regs);
524 
525 	die_if_kernel("Kernel ade access", regs);
526 	force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)regs->csr_badvaddr);
527 
528 	irqentry_exit(regs, state);
529 }
530 
531 /* sysctl hooks */
532 int unaligned_enabled __read_mostly = 1;	/* Enabled by default */
533 int no_unaligned_warning __read_mostly = 1;	/* Only 1 warning by default */
534 
535 asmlinkage void noinstr do_ale(struct pt_regs *regs)
536 {
537 	irqentry_state_t state = irqentry_enter(regs);
538 
539 #ifndef CONFIG_ARCH_STRICT_ALIGN
540 	die_if_kernel("Kernel ale access", regs);
541 	force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
542 #else
543 	unsigned int *pc;
544 
545 	perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, regs->csr_badvaddr);
546 
547 	/*
548 	 * Did we catch a fault trying to load an instruction?
549 	 */
550 	if (regs->csr_badvaddr == regs->csr_era)
551 		goto sigbus;
552 	if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
553 		goto sigbus;
554 	if (!unaligned_enabled)
555 		goto sigbus;
556 	if (!no_unaligned_warning)
557 		show_registers(regs);
558 
559 	pc = (unsigned int *)exception_era(regs);
560 
561 	emulate_load_store_insn(regs, (void __user *)regs->csr_badvaddr, pc);
562 
563 	goto out;
564 
565 sigbus:
566 	die_if_kernel("Kernel ale access", regs);
567 	force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
568 out:
569 #endif
570 	irqentry_exit(regs, state);
571 }
572 
573 #ifdef CONFIG_GENERIC_BUG
574 int is_valid_bugaddr(unsigned long addr)
575 {
576 	return 1;
577 }
578 #endif /* CONFIG_GENERIC_BUG */
579 
580 static void bug_handler(struct pt_regs *regs)
581 {
582 	switch (report_bug(regs->csr_era, regs)) {
583 	case BUG_TRAP_TYPE_BUG:
584 	case BUG_TRAP_TYPE_NONE:
585 		die_if_kernel("Oops - BUG", regs);
586 		force_sig(SIGTRAP);
587 		break;
588 
589 	case BUG_TRAP_TYPE_WARN:
590 		/* Skip the BUG instruction and continue */
591 		regs->csr_era += LOONGARCH_INSN_SIZE;
592 		break;
593 	}
594 }
595 
596 asmlinkage void noinstr do_bce(struct pt_regs *regs)
597 {
598 	bool user = user_mode(regs);
599 	unsigned long era = exception_era(regs);
600 	u64 badv = 0, lower = 0, upper = ULONG_MAX;
601 	union loongarch_instruction insn;
602 	irqentry_state_t state = irqentry_enter(regs);
603 
604 	if (regs->csr_prmd & CSR_PRMD_PIE)
605 		local_irq_enable();
606 
607 	current->thread.trap_nr = read_csr_excode();
608 
609 	die_if_kernel("Bounds check error in kernel code", regs);
610 
611 	/*
612 	 * Pull out the address that failed bounds checking, and the lower /
613 	 * upper bound, by minimally looking at the faulting instruction word
614 	 * and reading from the correct register.
615 	 */
616 	if (__get_inst(&insn.word, (u32 *)era, user))
617 		goto bad_era;
618 
619 	switch (insn.reg3_format.opcode) {
620 	case asrtle_op:
621 		if (insn.reg3_format.rd != 0)
622 			break;	/* not asrtle */
623 		badv = regs->regs[insn.reg3_format.rj];
624 		upper = regs->regs[insn.reg3_format.rk];
625 		break;
626 
627 	case asrtgt_op:
628 		if (insn.reg3_format.rd != 0)
629 			break;	/* not asrtgt */
630 		badv = regs->regs[insn.reg3_format.rj];
631 		lower = regs->regs[insn.reg3_format.rk];
632 		break;
633 
634 	case ldleb_op:
635 	case ldleh_op:
636 	case ldlew_op:
637 	case ldled_op:
638 	case stleb_op:
639 	case stleh_op:
640 	case stlew_op:
641 	case stled_op:
642 	case fldles_op:
643 	case fldled_op:
644 	case fstles_op:
645 	case fstled_op:
646 		badv = regs->regs[insn.reg3_format.rj];
647 		upper = regs->regs[insn.reg3_format.rk];
648 		break;
649 
650 	case ldgtb_op:
651 	case ldgth_op:
652 	case ldgtw_op:
653 	case ldgtd_op:
654 	case stgtb_op:
655 	case stgth_op:
656 	case stgtw_op:
657 	case stgtd_op:
658 	case fldgts_op:
659 	case fldgtd_op:
660 	case fstgts_op:
661 	case fstgtd_op:
662 		badv = regs->regs[insn.reg3_format.rj];
663 		lower = regs->regs[insn.reg3_format.rk];
664 		break;
665 	}
666 
667 	force_sig_bnderr((void __user *)badv, (void __user *)lower, (void __user *)upper);
668 
669 out:
670 	if (regs->csr_prmd & CSR_PRMD_PIE)
671 		local_irq_disable();
672 
673 	irqentry_exit(regs, state);
674 	return;
675 
676 bad_era:
677 	/*
678 	 * Cannot pull out the instruction word, hence cannot provide more
679 	 * info than a regular SIGSEGV in this case.
680 	 */
681 	force_sig(SIGSEGV);
682 	goto out;
683 }
684 
685 asmlinkage void noinstr do_bp(struct pt_regs *regs)
686 {
687 	bool user = user_mode(regs);
688 	unsigned int opcode, bcode;
689 	unsigned long era = exception_era(regs);
690 	irqentry_state_t state = irqentry_enter(regs);
691 
692 	if (regs->csr_prmd & CSR_PRMD_PIE)
693 		local_irq_enable();
694 
695 	if (__get_inst(&opcode, (u32 *)era, user))
696 		goto out_sigsegv;
697 
698 	bcode = (opcode & 0x7fff);
699 
700 	/*
701 	 * notify the kprobe handlers, if instruction is likely to
702 	 * pertain to them.
703 	 */
704 	switch (bcode) {
705 	case BRK_KPROBE_BP:
706 		if (kprobe_breakpoint_handler(regs))
707 			goto out;
708 		else
709 			break;
710 	case BRK_KPROBE_SSTEPBP:
711 		if (kprobe_singlestep_handler(regs))
712 			goto out;
713 		else
714 			break;
715 	case BRK_UPROBE_BP:
716 		if (uprobe_breakpoint_handler(regs))
717 			goto out;
718 		else
719 			break;
720 	case BRK_UPROBE_XOLBP:
721 		if (uprobe_singlestep_handler(regs))
722 			goto out;
723 		else
724 			break;
725 	default:
726 		current->thread.trap_nr = read_csr_excode();
727 		if (notify_die(DIE_TRAP, "Break", regs, bcode,
728 			       current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
729 			goto out;
730 		else
731 			break;
732 	}
733 
734 	switch (bcode) {
735 	case BRK_BUG:
736 		bug_handler(regs);
737 		break;
738 	case BRK_DIVZERO:
739 		die_if_kernel("Break instruction in kernel code", regs);
740 		force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->csr_era);
741 		break;
742 	case BRK_OVERFLOW:
743 		die_if_kernel("Break instruction in kernel code", regs);
744 		force_sig_fault(SIGFPE, FPE_INTOVF, (void __user *)regs->csr_era);
745 		break;
746 	default:
747 		die_if_kernel("Break instruction in kernel code", regs);
748 		force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->csr_era);
749 		break;
750 	}
751 
752 out:
753 	if (regs->csr_prmd & CSR_PRMD_PIE)
754 		local_irq_disable();
755 
756 	irqentry_exit(regs, state);
757 	return;
758 
759 out_sigsegv:
760 	force_sig(SIGSEGV);
761 	goto out;
762 }
763 
764 asmlinkage void noinstr do_watch(struct pt_regs *regs)
765 {
766 	irqentry_state_t state = irqentry_enter(regs);
767 
768 #ifndef CONFIG_HAVE_HW_BREAKPOINT
769 	pr_warn("Hardware watch point handler not implemented!\n");
770 #else
771 	if (test_tsk_thread_flag(current, TIF_SINGLESTEP)) {
772 		int llbit = (csr_read32(LOONGARCH_CSR_LLBCTL) & 0x1);
773 		unsigned long pc = instruction_pointer(regs);
774 		union loongarch_instruction *ip = (union loongarch_instruction *)pc;
775 
776 		if (llbit) {
777 			/*
778 			 * When the ll-sc combo is encountered, it is regarded as an single
779 			 * instruction. So don't clear llbit and reset CSR.FWPS.Skip until
780 			 * the llsc execution is completed.
781 			 */
782 			csr_write32(CSR_FWPC_SKIP, LOONGARCH_CSR_FWPS);
783 			csr_write32(CSR_LLBCTL_KLO, LOONGARCH_CSR_LLBCTL);
784 			goto out;
785 		}
786 
787 		if (pc == current->thread.single_step) {
788 			/*
789 			 * Certain insns are occasionally not skipped when CSR.FWPS.Skip is
790 			 * set, such as fld.d/fst.d. So singlestep needs to compare whether
791 			 * the csr_era is equal to the value of singlestep which last time set.
792 			 */
793 			if (!is_self_loop_ins(ip, regs)) {
794 				/*
795 				 * Check if the given instruction the target pc is equal to the
796 				 * current pc, If yes, then we should not set the CSR.FWPS.SKIP
797 				 * bit to break the original instruction stream.
798 				 */
799 				csr_write32(CSR_FWPC_SKIP, LOONGARCH_CSR_FWPS);
800 				goto out;
801 			}
802 		}
803 	} else {
804 		breakpoint_handler(regs);
805 		watchpoint_handler(regs);
806 	}
807 
808 	force_sig(SIGTRAP);
809 out:
810 #endif
811 	irqentry_exit(regs, state);
812 }
813 
814 asmlinkage void noinstr do_ri(struct pt_regs *regs)
815 {
816 	int status = SIGILL;
817 	unsigned int opcode = 0;
818 	unsigned int __user *era = (unsigned int __user *)exception_era(regs);
819 	irqentry_state_t state = irqentry_enter(regs);
820 
821 	local_irq_enable();
822 	current->thread.trap_nr = read_csr_excode();
823 
824 	if (notify_die(DIE_RI, "RI Fault", regs, 0, current->thread.trap_nr,
825 		       SIGILL) == NOTIFY_STOP)
826 		goto out;
827 
828 	die_if_kernel("Reserved instruction in kernel code", regs);
829 
830 	if (unlikely(get_user(opcode, era) < 0)) {
831 		status = SIGSEGV;
832 		current->thread.error_code = 1;
833 	}
834 
835 	force_sig(status);
836 
837 out:
838 	local_irq_disable();
839 	irqentry_exit(regs, state);
840 }
841 
842 static void init_restore_fp(void)
843 {
844 	if (!used_math()) {
845 		/* First time FP context user. */
846 		init_fpu();
847 	} else {
848 		/* This task has formerly used the FP context */
849 		if (!is_fpu_owner())
850 			own_fpu_inatomic(1);
851 	}
852 
853 	BUG_ON(!is_fp_enabled());
854 }
855 
856 static void init_restore_lsx(void)
857 {
858 	enable_lsx();
859 
860 	if (!thread_lsx_context_live()) {
861 		/* First time LSX context user */
862 		init_restore_fp();
863 		init_lsx_upper();
864 		set_thread_flag(TIF_LSX_CTX_LIVE);
865 	} else {
866 		if (!is_simd_owner()) {
867 			if (is_fpu_owner()) {
868 				restore_lsx_upper(current);
869 			} else {
870 				__own_fpu();
871 				restore_lsx(current);
872 			}
873 		}
874 	}
875 
876 	set_thread_flag(TIF_USEDSIMD);
877 
878 	BUG_ON(!is_fp_enabled());
879 	BUG_ON(!is_lsx_enabled());
880 }
881 
882 static void init_restore_lasx(void)
883 {
884 	enable_lasx();
885 
886 	if (!thread_lasx_context_live()) {
887 		/* First time LASX context user */
888 		init_restore_lsx();
889 		init_lasx_upper();
890 		set_thread_flag(TIF_LASX_CTX_LIVE);
891 	} else {
892 		if (is_fpu_owner() || is_simd_owner()) {
893 			init_restore_lsx();
894 			restore_lasx_upper(current);
895 		} else {
896 			__own_fpu();
897 			enable_lsx();
898 			restore_lasx(current);
899 		}
900 	}
901 
902 	set_thread_flag(TIF_USEDSIMD);
903 
904 	BUG_ON(!is_fp_enabled());
905 	BUG_ON(!is_lsx_enabled());
906 	BUG_ON(!is_lasx_enabled());
907 }
908 
909 asmlinkage void noinstr do_fpu(struct pt_regs *regs)
910 {
911 	irqentry_state_t state = irqentry_enter(regs);
912 
913 	local_irq_enable();
914 	die_if_kernel("do_fpu invoked from kernel context!", regs);
915 	BUG_ON(is_lsx_enabled());
916 	BUG_ON(is_lasx_enabled());
917 
918 	preempt_disable();
919 	init_restore_fp();
920 	preempt_enable();
921 
922 	local_irq_disable();
923 	irqentry_exit(regs, state);
924 }
925 
926 asmlinkage void noinstr do_lsx(struct pt_regs *regs)
927 {
928 	irqentry_state_t state = irqentry_enter(regs);
929 
930 	local_irq_enable();
931 	if (!cpu_has_lsx) {
932 		force_sig(SIGILL);
933 		goto out;
934 	}
935 
936 	die_if_kernel("do_lsx invoked from kernel context!", regs);
937 	BUG_ON(is_lasx_enabled());
938 
939 	preempt_disable();
940 	init_restore_lsx();
941 	preempt_enable();
942 
943 out:
944 	local_irq_disable();
945 	irqentry_exit(regs, state);
946 }
947 
948 asmlinkage void noinstr do_lasx(struct pt_regs *regs)
949 {
950 	irqentry_state_t state = irqentry_enter(regs);
951 
952 	local_irq_enable();
953 	if (!cpu_has_lasx) {
954 		force_sig(SIGILL);
955 		goto out;
956 	}
957 
958 	die_if_kernel("do_lasx invoked from kernel context!", regs);
959 
960 	preempt_disable();
961 	init_restore_lasx();
962 	preempt_enable();
963 
964 out:
965 	local_irq_disable();
966 	irqentry_exit(regs, state);
967 }
968 
969 asmlinkage void noinstr do_lbt(struct pt_regs *regs)
970 {
971 	irqentry_state_t state = irqentry_enter(regs);
972 
973 	local_irq_enable();
974 	force_sig(SIGILL);
975 	local_irq_disable();
976 
977 	irqentry_exit(regs, state);
978 }
979 
980 asmlinkage void noinstr do_reserved(struct pt_regs *regs)
981 {
982 	irqentry_state_t state = irqentry_enter(regs);
983 
984 	local_irq_enable();
985 	/*
986 	 * Game over - no way to handle this if it ever occurs.	Most probably
987 	 * caused by a fatal error after another hardware/software error.
988 	 */
989 	pr_err("Caught reserved exception %u on pid:%d [%s] - should not happen\n",
990 		read_csr_excode(), current->pid, current->comm);
991 	die_if_kernel("do_reserved exception", regs);
992 	force_sig(SIGUNUSED);
993 
994 	local_irq_disable();
995 
996 	irqentry_exit(regs, state);
997 }
998 
999 asmlinkage void cache_parity_error(void)
1000 {
1001 	/* For the moment, report the problem and hang. */
1002 	pr_err("Cache error exception:\n");
1003 	pr_err("csr_merrctl == %08x\n", csr_read32(LOONGARCH_CSR_MERRCTL));
1004 	pr_err("csr_merrera == %016lx\n", csr_read64(LOONGARCH_CSR_MERRERA));
1005 	panic("Can't handle the cache error!");
1006 }
1007 
1008 asmlinkage void noinstr handle_loongarch_irq(struct pt_regs *regs)
1009 {
1010 	struct pt_regs *old_regs;
1011 
1012 	irq_enter_rcu();
1013 	old_regs = set_irq_regs(regs);
1014 	handle_arch_irq(regs);
1015 	set_irq_regs(old_regs);
1016 	irq_exit_rcu();
1017 }
1018 
1019 asmlinkage void noinstr do_vint(struct pt_regs *regs, unsigned long sp)
1020 {
1021 	register int cpu;
1022 	register unsigned long stack;
1023 	irqentry_state_t state = irqentry_enter(regs);
1024 
1025 	cpu = smp_processor_id();
1026 
1027 	if (on_irq_stack(cpu, sp))
1028 		handle_loongarch_irq(regs);
1029 	else {
1030 		stack = per_cpu(irq_stack, cpu) + IRQ_STACK_START;
1031 
1032 		/* Save task's sp on IRQ stack for unwinding */
1033 		*(unsigned long *)stack = sp;
1034 
1035 		__asm__ __volatile__(
1036 		"move	$s0, $sp		\n" /* Preserve sp */
1037 		"move	$sp, %[stk]		\n" /* Switch stack */
1038 		"move	$a0, %[regs]		\n"
1039 		"bl	handle_loongarch_irq	\n"
1040 		"move	$sp, $s0		\n" /* Restore sp */
1041 		: /* No outputs */
1042 		: [stk] "r" (stack), [regs] "r" (regs)
1043 		: "$a0", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7", "$s0",
1044 		  "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8",
1045 		  "memory");
1046 	}
1047 
1048 	irqentry_exit(regs, state);
1049 }
1050 
1051 unsigned long eentry;
1052 unsigned long tlbrentry;
1053 
1054 long exception_handlers[VECSIZE * 128 / sizeof(long)] __aligned(SZ_64K);
1055 
1056 static void configure_exception_vector(void)
1057 {
1058 	eentry    = (unsigned long)exception_handlers;
1059 	tlbrentry = (unsigned long)exception_handlers + 80*VECSIZE;
1060 
1061 	csr_write64(eentry, LOONGARCH_CSR_EENTRY);
1062 	csr_write64(eentry, LOONGARCH_CSR_MERRENTRY);
1063 	csr_write64(tlbrentry, LOONGARCH_CSR_TLBRENTRY);
1064 }
1065 
1066 void per_cpu_trap_init(int cpu)
1067 {
1068 	unsigned int i;
1069 
1070 	setup_vint_size(VECSIZE);
1071 
1072 	configure_exception_vector();
1073 
1074 	if (!cpu_data[cpu].asid_cache)
1075 		cpu_data[cpu].asid_cache = asid_first_version(cpu);
1076 
1077 	mmgrab(&init_mm);
1078 	current->active_mm = &init_mm;
1079 	BUG_ON(current->mm);
1080 	enter_lazy_tlb(&init_mm, current);
1081 
1082 	/* Initialise exception handlers */
1083 	if (cpu == 0)
1084 		for (i = 0; i < 64; i++)
1085 			set_handler(i * VECSIZE, handle_reserved, VECSIZE);
1086 
1087 	tlb_init(cpu);
1088 	cpu_cache_init();
1089 }
1090 
1091 /* Install CPU exception handler */
1092 void set_handler(unsigned long offset, void *addr, unsigned long size)
1093 {
1094 	memcpy((void *)(eentry + offset), addr, size);
1095 	local_flush_icache_range(eentry + offset, eentry + offset + size);
1096 }
1097 
1098 static const char panic_null_cerr[] =
1099 	"Trying to set NULL cache error exception handler\n";
1100 
1101 /*
1102  * Install uncached CPU exception handler.
1103  * This is suitable only for the cache error exception which is the only
1104  * exception handler that is being run uncached.
1105  */
1106 void set_merr_handler(unsigned long offset, void *addr, unsigned long size)
1107 {
1108 	unsigned long uncached_eentry = TO_UNCACHE(__pa(eentry));
1109 
1110 	if (!addr)
1111 		panic(panic_null_cerr);
1112 
1113 	memcpy((void *)(uncached_eentry + offset), addr, size);
1114 }
1115 
1116 void __init trap_init(void)
1117 {
1118 	long i;
1119 
1120 	/* Set interrupt vector handler */
1121 	for (i = EXCCODE_INT_START; i <= EXCCODE_INT_END; i++)
1122 		set_handler(i * VECSIZE, handle_vint, VECSIZE);
1123 
1124 	set_handler(EXCCODE_ADE * VECSIZE, handle_ade, VECSIZE);
1125 	set_handler(EXCCODE_ALE * VECSIZE, handle_ale, VECSIZE);
1126 	set_handler(EXCCODE_BCE * VECSIZE, handle_bce, VECSIZE);
1127 	set_handler(EXCCODE_SYS * VECSIZE, handle_sys, VECSIZE);
1128 	set_handler(EXCCODE_BP * VECSIZE, handle_bp, VECSIZE);
1129 	set_handler(EXCCODE_INE * VECSIZE, handle_ri, VECSIZE);
1130 	set_handler(EXCCODE_IPE * VECSIZE, handle_ri, VECSIZE);
1131 	set_handler(EXCCODE_FPDIS * VECSIZE, handle_fpu, VECSIZE);
1132 	set_handler(EXCCODE_LSXDIS * VECSIZE, handle_lsx, VECSIZE);
1133 	set_handler(EXCCODE_LASXDIS * VECSIZE, handle_lasx, VECSIZE);
1134 	set_handler(EXCCODE_FPE * VECSIZE, handle_fpe, VECSIZE);
1135 	set_handler(EXCCODE_BTDIS * VECSIZE, handle_lbt, VECSIZE);
1136 	set_handler(EXCCODE_WATCH * VECSIZE, handle_watch, VECSIZE);
1137 
1138 	cache_error_setup();
1139 
1140 	local_flush_icache_range(eentry, eentry + 0x400);
1141 }
1142