1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6 #include <linux/bitfield.h>
7 #include <linux/bitops.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>
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/notifier.h>
29 #include <linux/irq.h>
30 #include <linux/perf_event.h>
32 #include <asm/addrspace.h>
33 #include <asm/bootinfo.h>
34 #include <asm/branch.h>
35 #include <asm/break.h>
37 #include <asm/exception.h>
42 #include <asm/loongarch.h>
43 #include <asm/mmu_context.h>
44 #include <asm/pgtable.h>
45 #include <asm/ptrace.h>
46 #include <asm/sections.h>
47 #include <asm/siginfo.h>
48 #include <asm/stacktrace.h>
50 #include <asm/types.h>
51 #include <asm/unwind.h>
52 #include <asm/uprobes.h>
54 #include "access-helper.h"
56 void *exception_table[EXCCODE_INT_START] = {
57 [0 ... EXCCODE_INT_START - 1] = handle_reserved,
59 [EXCCODE_TLBI] = handle_tlb_load,
60 [EXCCODE_TLBL] = handle_tlb_load,
61 [EXCCODE_TLBS] = handle_tlb_store,
62 [EXCCODE_TLBM] = handle_tlb_modify,
63 [EXCCODE_TLBNR] = handle_tlb_protect,
64 [EXCCODE_TLBNX] = handle_tlb_protect,
65 [EXCCODE_TLBPE] = handle_tlb_protect,
66 [EXCCODE_ADE] = handle_ade,
67 [EXCCODE_ALE] = handle_ale,
68 [EXCCODE_BCE] = handle_bce,
69 [EXCCODE_SYS] = handle_sys,
70 [EXCCODE_BP] = handle_bp,
71 [EXCCODE_INE] = handle_ri,
72 [EXCCODE_IPE] = handle_ri,
73 [EXCCODE_FPDIS] = handle_fpu,
74 [EXCCODE_LSXDIS] = handle_lsx,
75 [EXCCODE_LASXDIS] = handle_lasx,
76 [EXCCODE_FPE] = handle_fpe,
77 [EXCCODE_WATCH] = handle_watch,
78 [EXCCODE_BTDIS] = handle_lbt,
80 EXPORT_SYMBOL_GPL(exception_table);
82 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs,
83 const char *loglvl, bool user)
86 struct unwind_state state;
87 struct pt_regs *pregs = (struct pt_regs *)regs;
92 printk("%sCall Trace:", loglvl);
93 for (unwind_start(&state, task, pregs);
94 !unwind_done(&state); unwind_next_frame(&state)) {
95 addr = unwind_get_return_address(&state);
96 print_ip_sym(loglvl, addr);
98 printk("%s\n", loglvl);
101 static void show_stacktrace(struct task_struct *task,
102 const struct pt_regs *regs, const char *loglvl, bool user)
105 const int field = 2 * sizeof(unsigned long);
106 unsigned long stackdata;
107 unsigned long *sp = (unsigned long *)regs->regs[3];
109 printk("%sStack :", loglvl);
111 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
112 if (i && ((i % (64 / field)) == 0)) {
114 printk("%s ", loglvl);
121 if (__get_addr(&stackdata, sp++, user)) {
122 pr_cont(" (Bad stack address)");
126 pr_cont(" %0*lx", field, stackdata);
130 show_backtrace(task, regs, loglvl, user);
133 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
141 regs.regs[3] = (unsigned long)sp;
143 if (!task || task == current)
144 prepare_frametrace(®s);
146 regs.csr_era = task->thread.reg01;
148 regs.regs[3] = task->thread.reg03;
149 regs.regs[22] = task->thread.reg22;
153 show_stacktrace(task, ®s, loglvl, false);
156 static void show_code(unsigned int *pc, bool user)
163 for(i = -3 ; i < 6 ; i++) {
164 if (__get_inst(&insn, pc + i, user)) {
165 pr_cont(" (Bad address in era)\n");
168 pr_cont("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
173 static void print_bool_fragment(const char *key, unsigned long val, bool first)
175 /* e.g. "+PG", "-DA" */
176 pr_cont("%s%c%s", first ? "" : " ", val ? '+' : '-', key);
179 static void print_plv_fragment(const char *key, int val)
181 /* e.g. "PLV0", "PPLV3" */
182 pr_cont("%s%d", key, val);
185 static void print_memory_type_fragment(const char *key, unsigned long val)
187 const char *humanized_type;
191 humanized_type = "SUC";
194 humanized_type = "CC";
197 humanized_type = "WUC";
200 pr_cont(" %s=Reserved(%lu)", key, val);
204 /* e.g. " DATM=WUC" */
205 pr_cont(" %s=%s", key, humanized_type);
208 static void print_intr_fragment(const char *key, unsigned long val)
210 /* e.g. "LIE=0-1,3,5-7" */
211 pr_cont("%s=%*pbl", key, EXCCODE_INT_NUM, &val);
214 static void print_crmd(unsigned long x)
216 printk(" CRMD: %08lx (", x);
217 print_plv_fragment("PLV", (int) FIELD_GET(CSR_CRMD_PLV, x));
218 print_bool_fragment("IE", FIELD_GET(CSR_CRMD_IE, x), false);
219 print_bool_fragment("DA", FIELD_GET(CSR_CRMD_DA, x), false);
220 print_bool_fragment("PG", FIELD_GET(CSR_CRMD_PG, x), false);
221 print_memory_type_fragment("DACF", FIELD_GET(CSR_CRMD_DACF, x));
222 print_memory_type_fragment("DACM", FIELD_GET(CSR_CRMD_DACM, x));
223 print_bool_fragment("WE", FIELD_GET(CSR_CRMD_WE, x), false);
227 static void print_prmd(unsigned long x)
229 printk(" PRMD: %08lx (", x);
230 print_plv_fragment("PPLV", (int) FIELD_GET(CSR_PRMD_PPLV, x));
231 print_bool_fragment("PIE", FIELD_GET(CSR_PRMD_PIE, x), false);
232 print_bool_fragment("PWE", FIELD_GET(CSR_PRMD_PWE, x), false);
236 static void print_euen(unsigned long x)
238 printk(" EUEN: %08lx (", x);
239 print_bool_fragment("FPE", FIELD_GET(CSR_EUEN_FPEN, x), true);
240 print_bool_fragment("SXE", FIELD_GET(CSR_EUEN_LSXEN, x), false);
241 print_bool_fragment("ASXE", FIELD_GET(CSR_EUEN_LASXEN, x), false);
242 print_bool_fragment("BTE", FIELD_GET(CSR_EUEN_LBTEN, x), false);
246 static void print_ecfg(unsigned long x)
248 printk(" ECFG: %08lx (", x);
249 print_intr_fragment("LIE", FIELD_GET(CSR_ECFG_IM, x));
250 pr_cont(" VS=%d)\n", (int) FIELD_GET(CSR_ECFG_VS, x));
253 static const char *humanize_exc_name(unsigned int ecode, unsigned int esubcode)
256 * LoongArch users and developers are probably more familiar with
257 * those names found in the ISA manual, so we are going to print out
258 * the latter. This will require some mapping.
261 case EXCCODE_RSV: return "INT";
262 case EXCCODE_TLBL: return "PIL";
263 case EXCCODE_TLBS: return "PIS";
264 case EXCCODE_TLBI: return "PIF";
265 case EXCCODE_TLBM: return "PME";
266 case EXCCODE_TLBNR: return "PNR";
267 case EXCCODE_TLBNX: return "PNX";
268 case EXCCODE_TLBPE: return "PPI";
271 case EXSUBCODE_ADEF: return "ADEF";
272 case EXSUBCODE_ADEM: return "ADEM";
275 case EXCCODE_ALE: return "ALE";
276 case EXCCODE_BCE: return "BCE";
277 case EXCCODE_SYS: return "SYS";
278 case EXCCODE_BP: return "BRK";
279 case EXCCODE_INE: return "INE";
280 case EXCCODE_IPE: return "IPE";
281 case EXCCODE_FPDIS: return "FPD";
282 case EXCCODE_LSXDIS: return "SXD";
283 case EXCCODE_LASXDIS: return "ASXD";
286 case EXCSUBCODE_FPE: return "FPE";
287 case EXCSUBCODE_VFPE: return "VFPE";
292 case EXCSUBCODE_WPEF: return "WPEF";
293 case EXCSUBCODE_WPEM: return "WPEM";
296 case EXCCODE_BTDIS: return "BTD";
297 case EXCCODE_BTE: return "BTE";
298 case EXCCODE_GSPR: return "GSPR";
299 case EXCCODE_HVC: return "HVC";
302 case EXCSUBCODE_GCSC: return "GCSC";
303 case EXCSUBCODE_GCHC: return "GCHC";
307 * The manual did not mention the EXCCODE_SE case, but print out it
310 case EXCCODE_SE: return "SE";
316 static void print_estat(unsigned long x)
318 unsigned int ecode = FIELD_GET(CSR_ESTAT_EXC, x);
319 unsigned int esubcode = FIELD_GET(CSR_ESTAT_ESUBCODE, x);
321 printk("ESTAT: %08lx [%s] (", x, humanize_exc_name(ecode, esubcode));
322 print_intr_fragment("IS", FIELD_GET(CSR_ESTAT_IS, x));
323 pr_cont(" ECode=%d EsubCode=%d)\n", (int) ecode, (int) esubcode);
326 static void __show_regs(const struct pt_regs *regs)
328 const int field = 2 * sizeof(unsigned long);
329 unsigned int exccode = FIELD_GET(CSR_ESTAT_EXC, regs->csr_estat);
331 show_regs_print_info(KERN_DEFAULT);
333 /* Print saved GPRs except $zero (substituting with PC/ERA) */
334 #define GPR_FIELD(x) field, regs->regs[x]
335 printk("pc %0*lx ra %0*lx tp %0*lx sp %0*lx\n",
336 field, regs->csr_era, GPR_FIELD(1), GPR_FIELD(2), GPR_FIELD(3));
337 printk("a0 %0*lx a1 %0*lx a2 %0*lx a3 %0*lx\n",
338 GPR_FIELD(4), GPR_FIELD(5), GPR_FIELD(6), GPR_FIELD(7));
339 printk("a4 %0*lx a5 %0*lx a6 %0*lx a7 %0*lx\n",
340 GPR_FIELD(8), GPR_FIELD(9), GPR_FIELD(10), GPR_FIELD(11));
341 printk("t0 %0*lx t1 %0*lx t2 %0*lx t3 %0*lx\n",
342 GPR_FIELD(12), GPR_FIELD(13), GPR_FIELD(14), GPR_FIELD(15));
343 printk("t4 %0*lx t5 %0*lx t6 %0*lx t7 %0*lx\n",
344 GPR_FIELD(16), GPR_FIELD(17), GPR_FIELD(18), GPR_FIELD(19));
345 printk("t8 %0*lx u0 %0*lx s9 %0*lx s0 %0*lx\n",
346 GPR_FIELD(20), GPR_FIELD(21), GPR_FIELD(22), GPR_FIELD(23));
347 printk("s1 %0*lx s2 %0*lx s3 %0*lx s4 %0*lx\n",
348 GPR_FIELD(24), GPR_FIELD(25), GPR_FIELD(26), GPR_FIELD(27));
349 printk("s5 %0*lx s6 %0*lx s7 %0*lx s8 %0*lx\n",
350 GPR_FIELD(28), GPR_FIELD(29), GPR_FIELD(30), GPR_FIELD(31));
352 /* The slot for $zero is reused as the syscall restart flag */
354 printk("syscall restart flag: %0*lx\n", GPR_FIELD(0));
356 if (user_mode(regs)) {
357 printk(" ra: %0*lx\n", GPR_FIELD(1));
358 printk(" ERA: %0*lx\n", field, regs->csr_era);
360 printk(" ra: %0*lx %pS\n", GPR_FIELD(1), (void *) regs->regs[1]);
361 printk(" ERA: %0*lx %pS\n", field, regs->csr_era, (void *) regs->csr_era);
365 /* Print saved important CSRs */
366 print_crmd(regs->csr_crmd);
367 print_prmd(regs->csr_prmd);
368 print_euen(regs->csr_euen);
369 print_ecfg(regs->csr_ecfg);
370 print_estat(regs->csr_estat);
372 if (exccode >= EXCCODE_TLBL && exccode <= EXCCODE_ALE)
373 printk(" BADV: %0*lx\n", field, regs->csr_badvaddr);
375 printk(" PRID: %08x (%s, %s)\n", read_cpucfg(LOONGARCH_CPUCFG0),
376 cpu_family_string(), cpu_full_name_string());
379 void show_regs(struct pt_regs *regs)
381 __show_regs((struct pt_regs *)regs);
385 void show_registers(struct pt_regs *regs)
389 printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
390 current->comm, current->pid, current_thread_info(), current);
392 show_stacktrace(current, regs, KERN_DEFAULT, user_mode(regs));
393 show_code((void *)regs->csr_era, user_mode(regs));
397 static DEFINE_RAW_SPINLOCK(die_lock);
399 void die(const char *str, struct pt_regs *regs)
402 static int die_counter;
406 ret = notify_die(DIE_OOPS, str, regs, 0,
407 current->thread.trap_nr, SIGSEGV);
410 raw_spin_lock_irq(&die_lock);
413 printk("%s[#%d]:\n", str, ++die_counter);
414 show_registers(regs);
415 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
416 raw_spin_unlock_irq(&die_lock);
420 if (ret == NOTIFY_STOP)
423 if (regs && kexec_should_crash(current))
427 panic("Fatal exception in interrupt");
430 panic("Fatal exception");
432 make_task_dead(SIGSEGV);
435 static inline void setup_vint_size(unsigned int size)
441 if (vs == 0 || vs > 7)
442 panic("vint_size %d Not support yet", vs);
444 csr_xchg32(vs<<CSR_ECFG_VS_SHIFT, CSR_ECFG_VS, LOONGARCH_CSR_ECFG);
448 * Send SIGFPE according to FCSR Cause bits, which must have already
449 * been masked against Enable bits. This is impotant as Inexact can
450 * happen together with Overflow or Underflow, and `ptrace' can set
453 static void force_fcsr_sig(unsigned long fcsr,
454 void __user *fault_addr, struct task_struct *tsk)
456 int si_code = FPE_FLTUNK;
458 if (fcsr & FPU_CSR_INV_X)
459 si_code = FPE_FLTINV;
460 else if (fcsr & FPU_CSR_DIV_X)
461 si_code = FPE_FLTDIV;
462 else if (fcsr & FPU_CSR_OVF_X)
463 si_code = FPE_FLTOVF;
464 else if (fcsr & FPU_CSR_UDF_X)
465 si_code = FPE_FLTUND;
466 else if (fcsr & FPU_CSR_INE_X)
467 si_code = FPE_FLTRES;
469 force_sig_fault(SIGFPE, si_code, fault_addr);
472 static int process_fpemu_return(int sig, void __user *fault_addr, unsigned long fcsr)
481 force_fcsr_sig(fcsr, fault_addr, current);
485 force_sig_fault(SIGBUS, BUS_ADRERR, fault_addr);
489 mmap_read_lock(current->mm);
490 if (vma_lookup(current->mm, (unsigned long)fault_addr))
491 si_code = SEGV_ACCERR;
493 si_code = SEGV_MAPERR;
494 mmap_read_unlock(current->mm);
495 force_sig_fault(SIGSEGV, si_code, fault_addr);
505 * Delayed fp exceptions when doing a lazy ctx switch
507 asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
510 void __user *fault_addr;
511 irqentry_state_t state = irqentry_enter(regs);
513 if (notify_die(DIE_FP, "FP exception", regs, 0, current->thread.trap_nr,
514 SIGFPE) == NOTIFY_STOP)
517 /* Clear FCSR.Cause before enabling interrupts */
518 write_fcsr(LOONGARCH_FCSR0, fcsr & ~mask_fcsr_x(fcsr));
521 die_if_kernel("FP exception in kernel code", regs);
524 fault_addr = (void __user *) regs->csr_era;
526 /* Send a signal if required. */
527 process_fpemu_return(sig, fault_addr, fcsr);
531 irqentry_exit(regs, state);
534 asmlinkage void noinstr do_ade(struct pt_regs *regs)
536 irqentry_state_t state = irqentry_enter(regs);
538 die_if_kernel("Kernel ade access", regs);
539 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)regs->csr_badvaddr);
541 irqentry_exit(regs, state);
545 int unaligned_enabled __read_mostly = 1; /* Enabled by default */
546 int no_unaligned_warning __read_mostly = 1; /* Only 1 warning by default */
548 asmlinkage void noinstr do_ale(struct pt_regs *regs)
550 irqentry_state_t state = irqentry_enter(regs);
552 #ifndef CONFIG_ARCH_STRICT_ALIGN
553 die_if_kernel("Kernel ale access", regs);
554 force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
558 if (regs->csr_prmd & CSR_PRMD_PIE)
561 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, regs->csr_badvaddr);
564 * Did we catch a fault trying to load an instruction?
566 if (regs->csr_badvaddr == regs->csr_era)
568 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
570 if (!unaligned_enabled)
572 if (!no_unaligned_warning)
573 show_registers(regs);
575 pc = (unsigned int *)exception_era(regs);
577 emulate_load_store_insn(regs, (void __user *)regs->csr_badvaddr, pc);
582 die_if_kernel("Kernel ale access", regs);
583 force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
585 if (regs->csr_prmd & CSR_PRMD_PIE)
588 irqentry_exit(regs, state);
591 #ifdef CONFIG_GENERIC_BUG
592 int is_valid_bugaddr(unsigned long addr)
596 #endif /* CONFIG_GENERIC_BUG */
598 static void bug_handler(struct pt_regs *regs)
600 if (user_mode(regs)) {
605 switch (report_bug(regs->csr_era, regs)) {
606 case BUG_TRAP_TYPE_BUG:
607 die("Oops - BUG", regs);
610 case BUG_TRAP_TYPE_WARN:
611 /* Skip the BUG instruction and continue */
612 regs->csr_era += LOONGARCH_INSN_SIZE;
616 if (!fixup_exception(regs))
617 die("Oops - BUG", regs);
621 asmlinkage void noinstr do_bce(struct pt_regs *regs)
623 bool user = user_mode(regs);
624 unsigned long era = exception_era(regs);
625 u64 badv = 0, lower = 0, upper = ULONG_MAX;
626 union loongarch_instruction insn;
627 irqentry_state_t state = irqentry_enter(regs);
629 if (regs->csr_prmd & CSR_PRMD_PIE)
632 current->thread.trap_nr = read_csr_excode();
634 die_if_kernel("Bounds check error in kernel code", regs);
637 * Pull out the address that failed bounds checking, and the lower /
638 * upper bound, by minimally looking at the faulting instruction word
639 * and reading from the correct register.
641 if (__get_inst(&insn.word, (u32 *)era, user))
644 switch (insn.reg3_format.opcode) {
646 if (insn.reg3_format.rd != 0)
647 break; /* not asrtle */
648 badv = regs->regs[insn.reg3_format.rj];
649 upper = regs->regs[insn.reg3_format.rk];
653 if (insn.reg3_format.rd != 0)
654 break; /* not asrtgt */
655 badv = regs->regs[insn.reg3_format.rj];
656 lower = regs->regs[insn.reg3_format.rk];
671 badv = regs->regs[insn.reg3_format.rj];
672 upper = regs->regs[insn.reg3_format.rk];
687 badv = regs->regs[insn.reg3_format.rj];
688 lower = regs->regs[insn.reg3_format.rk];
692 force_sig_bnderr((void __user *)badv, (void __user *)lower, (void __user *)upper);
695 if (regs->csr_prmd & CSR_PRMD_PIE)
698 irqentry_exit(regs, state);
703 * Cannot pull out the instruction word, hence cannot provide more
704 * info than a regular SIGSEGV in this case.
710 asmlinkage void noinstr do_bp(struct pt_regs *regs)
712 bool user = user_mode(regs);
713 unsigned int opcode, bcode;
714 unsigned long era = exception_era(regs);
715 irqentry_state_t state = irqentry_enter(regs);
717 if (regs->csr_prmd & CSR_PRMD_PIE)
720 if (__get_inst(&opcode, (u32 *)era, user))
723 bcode = (opcode & 0x7fff);
726 * notify the kprobe handlers, if instruction is likely to
731 if (kgdb_breakpoint_handler(regs))
736 if (kprobe_breakpoint_handler(regs))
740 case BRK_KPROBE_SSTEPBP:
741 if (kprobe_singlestep_handler(regs))
746 if (uprobe_breakpoint_handler(regs))
750 case BRK_UPROBE_XOLBP:
751 if (uprobe_singlestep_handler(regs))
756 current->thread.trap_nr = read_csr_excode();
757 if (notify_die(DIE_TRAP, "Break", regs, bcode,
758 current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
769 die_if_kernel("Break instruction in kernel code", regs);
770 force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->csr_era);
773 die_if_kernel("Break instruction in kernel code", regs);
774 force_sig_fault(SIGFPE, FPE_INTOVF, (void __user *)regs->csr_era);
777 die_if_kernel("Break instruction in kernel code", regs);
778 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->csr_era);
783 if (regs->csr_prmd & CSR_PRMD_PIE)
786 irqentry_exit(regs, state);
794 asmlinkage void noinstr do_watch(struct pt_regs *regs)
796 irqentry_state_t state = irqentry_enter(regs);
798 #ifndef CONFIG_HAVE_HW_BREAKPOINT
799 pr_warn("Hardware watch point handler not implemented!\n");
801 if (kgdb_breakpoint_handler(regs))
804 if (test_tsk_thread_flag(current, TIF_SINGLESTEP)) {
805 int llbit = (csr_read32(LOONGARCH_CSR_LLBCTL) & 0x1);
806 unsigned long pc = instruction_pointer(regs);
807 union loongarch_instruction *ip = (union loongarch_instruction *)pc;
811 * When the ll-sc combo is encountered, it is regarded as an single
812 * instruction. So don't clear llbit and reset CSR.FWPS.Skip until
813 * the llsc execution is completed.
815 csr_write32(CSR_FWPC_SKIP, LOONGARCH_CSR_FWPS);
816 csr_write32(CSR_LLBCTL_KLO, LOONGARCH_CSR_LLBCTL);
820 if (pc == current->thread.single_step) {
822 * Certain insns are occasionally not skipped when CSR.FWPS.Skip is
823 * set, such as fld.d/fst.d. So singlestep needs to compare whether
824 * the csr_era is equal to the value of singlestep which last time set.
826 if (!is_self_loop_ins(ip, regs)) {
828 * Check if the given instruction the target pc is equal to the
829 * current pc, If yes, then we should not set the CSR.FWPS.SKIP
830 * bit to break the original instruction stream.
832 csr_write32(CSR_FWPC_SKIP, LOONGARCH_CSR_FWPS);
837 breakpoint_handler(regs);
838 watchpoint_handler(regs);
844 irqentry_exit(regs, state);
847 asmlinkage void noinstr do_ri(struct pt_regs *regs)
850 unsigned int __maybe_unused opcode;
851 unsigned int __user *era = (unsigned int __user *)exception_era(regs);
852 irqentry_state_t state = irqentry_enter(regs);
855 current->thread.trap_nr = read_csr_excode();
857 if (notify_die(DIE_RI, "RI Fault", regs, 0, current->thread.trap_nr,
858 SIGILL) == NOTIFY_STOP)
861 die_if_kernel("Reserved instruction in kernel code", regs);
863 if (unlikely(get_user(opcode, era) < 0)) {
865 current->thread.error_code = 1;
872 irqentry_exit(regs, state);
875 static void init_restore_fp(void)
878 /* First time FP context user. */
881 /* This task has formerly used the FP context */
886 BUG_ON(!is_fp_enabled());
889 static void init_restore_lsx(void)
893 if (!thread_lsx_context_live()) {
894 /* First time LSX context user */
897 set_thread_flag(TIF_LSX_CTX_LIVE);
899 if (!is_simd_owner()) {
900 if (is_fpu_owner()) {
901 restore_lsx_upper(current);
904 restore_lsx(current);
909 set_thread_flag(TIF_USEDSIMD);
911 BUG_ON(!is_fp_enabled());
912 BUG_ON(!is_lsx_enabled());
915 static void init_restore_lasx(void)
919 if (!thread_lasx_context_live()) {
920 /* First time LASX context user */
923 set_thread_flag(TIF_LASX_CTX_LIVE);
925 if (is_fpu_owner() || is_simd_owner()) {
927 restore_lasx_upper(current);
931 restore_lasx(current);
935 set_thread_flag(TIF_USEDSIMD);
937 BUG_ON(!is_fp_enabled());
938 BUG_ON(!is_lsx_enabled());
939 BUG_ON(!is_lasx_enabled());
942 asmlinkage void noinstr do_fpu(struct pt_regs *regs)
944 irqentry_state_t state = irqentry_enter(regs);
947 die_if_kernel("do_fpu invoked from kernel context!", regs);
948 BUG_ON(is_lsx_enabled());
949 BUG_ON(is_lasx_enabled());
956 irqentry_exit(regs, state);
959 asmlinkage void noinstr do_lsx(struct pt_regs *regs)
961 irqentry_state_t state = irqentry_enter(regs);
969 die_if_kernel("do_lsx invoked from kernel context!", regs);
970 BUG_ON(is_lasx_enabled());
978 irqentry_exit(regs, state);
981 asmlinkage void noinstr do_lasx(struct pt_regs *regs)
983 irqentry_state_t state = irqentry_enter(regs);
991 die_if_kernel("do_lasx invoked from kernel context!", regs);
999 irqentry_exit(regs, state);
1002 static void init_restore_lbt(void)
1004 if (!thread_lbt_context_live()) {
1005 /* First time LBT context user */
1007 set_thread_flag(TIF_LBT_CTX_LIVE);
1009 if (!is_lbt_owner())
1010 own_lbt_inatomic(1);
1013 BUG_ON(!is_lbt_enabled());
1016 asmlinkage void noinstr do_lbt(struct pt_regs *regs)
1018 irqentry_state_t state = irqentry_enter(regs);
1021 * BTD (Binary Translation Disable exception) can be triggered
1022 * during FP save/restore if TM (Top Mode) is on, which may
1023 * cause irq_enable during 'switch_to'. To avoid this situation
1024 * (including the user using 'MOVGR2GCSR' to turn on TM, which
1025 * will not trigger the BTE), we need to check PRMD first.
1027 if (regs->csr_prmd & CSR_PRMD_PIE)
1034 BUG_ON(is_lbt_enabled());
1041 if (regs->csr_prmd & CSR_PRMD_PIE)
1042 local_irq_disable();
1044 irqentry_exit(regs, state);
1047 asmlinkage void noinstr do_reserved(struct pt_regs *regs)
1049 irqentry_state_t state = irqentry_enter(regs);
1053 * Game over - no way to handle this if it ever occurs. Most probably
1054 * caused by a fatal error after another hardware/software error.
1056 pr_err("Caught reserved exception %u on pid:%d [%s] - should not happen\n",
1057 read_csr_excode(), current->pid, current->comm);
1058 die_if_kernel("do_reserved exception", regs);
1059 force_sig(SIGUNUSED);
1061 local_irq_disable();
1063 irqentry_exit(regs, state);
1066 asmlinkage void cache_parity_error(void)
1068 /* For the moment, report the problem and hang. */
1069 pr_err("Cache error exception:\n");
1070 pr_err("csr_merrctl == %08x\n", csr_read32(LOONGARCH_CSR_MERRCTL));
1071 pr_err("csr_merrera == %016lx\n", csr_read64(LOONGARCH_CSR_MERRERA));
1072 panic("Can't handle the cache error!");
1075 asmlinkage void noinstr handle_loongarch_irq(struct pt_regs *regs)
1077 struct pt_regs *old_regs;
1080 old_regs = set_irq_regs(regs);
1081 handle_arch_irq(regs);
1082 set_irq_regs(old_regs);
1086 asmlinkage void noinstr do_vint(struct pt_regs *regs, unsigned long sp)
1089 register unsigned long stack;
1090 irqentry_state_t state = irqentry_enter(regs);
1092 cpu = smp_processor_id();
1094 if (on_irq_stack(cpu, sp))
1095 handle_loongarch_irq(regs);
1097 stack = per_cpu(irq_stack, cpu) + IRQ_STACK_START;
1099 /* Save task's sp on IRQ stack for unwinding */
1100 *(unsigned long *)stack = sp;
1102 __asm__ __volatile__(
1103 "move $s0, $sp \n" /* Preserve sp */
1104 "move $sp, %[stk] \n" /* Switch stack */
1105 "move $a0, %[regs] \n"
1106 "bl handle_loongarch_irq \n"
1107 "move $sp, $s0 \n" /* Restore sp */
1109 : [stk] "r" (stack), [regs] "r" (regs)
1110 : "$a0", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7", "$s0",
1111 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8",
1115 irqentry_exit(regs, state);
1118 unsigned long eentry;
1119 unsigned long tlbrentry;
1121 long exception_handlers[VECSIZE * 128 / sizeof(long)] __aligned(SZ_64K);
1123 static void configure_exception_vector(void)
1125 eentry = (unsigned long)exception_handlers;
1126 tlbrentry = (unsigned long)exception_handlers + 80*VECSIZE;
1128 csr_write64(eentry, LOONGARCH_CSR_EENTRY);
1129 csr_write64(eentry, LOONGARCH_CSR_MERRENTRY);
1130 csr_write64(tlbrentry, LOONGARCH_CSR_TLBRENTRY);
1133 void per_cpu_trap_init(int cpu)
1137 setup_vint_size(VECSIZE);
1139 configure_exception_vector();
1141 if (!cpu_data[cpu].asid_cache)
1142 cpu_data[cpu].asid_cache = asid_first_version(cpu);
1145 current->active_mm = &init_mm;
1146 BUG_ON(current->mm);
1147 enter_lazy_tlb(&init_mm, current);
1149 /* Initialise exception handlers */
1151 for (i = 0; i < 64; i++)
1152 set_handler(i * VECSIZE, handle_reserved, VECSIZE);
1158 /* Install CPU exception handler */
1159 void set_handler(unsigned long offset, void *addr, unsigned long size)
1161 memcpy((void *)(eentry + offset), addr, size);
1162 local_flush_icache_range(eentry + offset, eentry + offset + size);
1165 static const char panic_null_cerr[] =
1166 "Trying to set NULL cache error exception handler\n";
1169 * Install uncached CPU exception handler.
1170 * This is suitable only for the cache error exception which is the only
1171 * exception handler that is being run uncached.
1173 void set_merr_handler(unsigned long offset, void *addr, unsigned long size)
1175 unsigned long uncached_eentry = TO_UNCACHE(__pa(eentry));
1178 panic(panic_null_cerr);
1180 memcpy((void *)(uncached_eentry + offset), addr, size);
1183 void __init trap_init(void)
1187 /* Set interrupt vector handler */
1188 for (i = EXCCODE_INT_START; i <= EXCCODE_INT_END; i++)
1189 set_handler(i * VECSIZE, handle_vint, VECSIZE);
1191 /* Set exception vector handler */
1192 for (i = EXCCODE_ADE; i <= EXCCODE_BTDIS; i++)
1193 set_handler(i * VECSIZE, exception_table[i], VECSIZE);
1195 cache_error_setup();
1197 local_flush_icache_range(eentry, eentry + 0x400);