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/notifier.h>
29 #include <linux/irq.h>
30 #include <linux/perf_event.h>
31
32 #include <asm/addrspace.h>
33 #include <asm/bootinfo.h>
34 #include <asm/branch.h>
35 #include <asm/break.h>
36 #include <asm/cpu.h>
37 #include <asm/exception.h>
38 #include <asm/fpu.h>
39 #include <asm/lbt.h>
40 #include <asm/inst.h>
41 #include <asm/kgdb.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>
49 #include <asm/tlb.h>
50 #include <asm/types.h>
51 #include <asm/unwind.h>
52 #include <asm/uprobes.h>
53
54 #include "access-helper.h"
55
56 void *exception_table[EXCCODE_INT_START] = {
57 [0 ... EXCCODE_INT_START - 1] = handle_reserved,
58
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,
79 };
80 EXPORT_SYMBOL_GPL(exception_table);
81
show_backtrace(struct task_struct * task,const struct pt_regs * regs,const char * loglvl,bool user)82 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs,
83 const char *loglvl, bool user)
84 {
85 unsigned long addr;
86 struct unwind_state state;
87 struct pt_regs *pregs = (struct pt_regs *)regs;
88
89 if (!task)
90 task = current;
91
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);
97 }
98 printk("%s\n", loglvl);
99 }
100
show_stacktrace(struct task_struct * task,const struct pt_regs * regs,const char * loglvl,bool user)101 static void show_stacktrace(struct task_struct *task,
102 const struct pt_regs *regs, const char *loglvl, bool user)
103 {
104 int i;
105 const int field = 2 * sizeof(unsigned long);
106 unsigned long stackdata;
107 unsigned long *sp = (unsigned long *)regs->regs[3];
108
109 printk("%sStack :", loglvl);
110 i = 0;
111 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
112 if (i && ((i % (64 / field)) == 0)) {
113 pr_cont("\n");
114 printk("%s ", loglvl);
115 }
116 if (i > 39) {
117 pr_cont(" ...");
118 break;
119 }
120
121 if (__get_addr(&stackdata, sp++, user)) {
122 pr_cont(" (Bad stack address)");
123 break;
124 }
125
126 pr_cont(" %0*lx", field, stackdata);
127 i++;
128 }
129 pr_cont("\n");
130 show_backtrace(task, regs, loglvl, user);
131 }
132
show_stack(struct task_struct * task,unsigned long * sp,const char * loglvl)133 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
134 {
135 struct pt_regs regs;
136
137 regs.csr_crmd = 0;
138 if (sp) {
139 regs.csr_era = 0;
140 regs.regs[1] = 0;
141 regs.regs[3] = (unsigned long)sp;
142 } else {
143 if (!task || task == current)
144 prepare_frametrace(®s);
145 else {
146 regs.csr_era = task->thread.reg01;
147 regs.regs[1] = 0;
148 regs.regs[3] = task->thread.reg03;
149 regs.regs[22] = task->thread.reg22;
150 }
151 }
152
153 show_stacktrace(task, ®s, loglvl, false);
154 }
155
show_code(unsigned int * pc,bool user)156 static void show_code(unsigned int *pc, bool user)
157 {
158 long i;
159 unsigned int insn;
160
161 printk("Code:");
162
163 for(i = -3 ; i < 6 ; i++) {
164 if (__get_inst(&insn, pc + i, user)) {
165 pr_cont(" (Bad address in era)\n");
166 break;
167 }
168 pr_cont("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
169 }
170 pr_cont("\n");
171 }
172
print_bool_fragment(const char * key,unsigned long val,bool first)173 static void print_bool_fragment(const char *key, unsigned long val, bool first)
174 {
175 /* e.g. "+PG", "-DA" */
176 pr_cont("%s%c%s", first ? "" : " ", val ? '+' : '-', key);
177 }
178
print_plv_fragment(const char * key,int val)179 static void print_plv_fragment(const char *key, int val)
180 {
181 /* e.g. "PLV0", "PPLV3" */
182 pr_cont("%s%d", key, val);
183 }
184
print_memory_type_fragment(const char * key,unsigned long val)185 static void print_memory_type_fragment(const char *key, unsigned long val)
186 {
187 const char *humanized_type;
188
189 switch (val) {
190 case 0:
191 humanized_type = "SUC";
192 break;
193 case 1:
194 humanized_type = "CC";
195 break;
196 case 2:
197 humanized_type = "WUC";
198 break;
199 default:
200 pr_cont(" %s=Reserved(%lu)", key, val);
201 return;
202 }
203
204 /* e.g. " DATM=WUC" */
205 pr_cont(" %s=%s", key, humanized_type);
206 }
207
print_intr_fragment(const char * key,unsigned long val)208 static void print_intr_fragment(const char *key, unsigned long val)
209 {
210 /* e.g. "LIE=0-1,3,5-7" */
211 pr_cont("%s=%*pbl", key, EXCCODE_INT_NUM, &val);
212 }
213
print_crmd(unsigned long x)214 static void print_crmd(unsigned long x)
215 {
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);
224 pr_cont(")\n");
225 }
226
print_prmd(unsigned long x)227 static void print_prmd(unsigned long x)
228 {
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);
233 pr_cont(")\n");
234 }
235
print_euen(unsigned long x)236 static void print_euen(unsigned long x)
237 {
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);
243 pr_cont(")\n");
244 }
245
print_ecfg(unsigned long x)246 static void print_ecfg(unsigned long x)
247 {
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));
251 }
252
humanize_exc_name(unsigned int ecode,unsigned int esubcode)253 static const char *humanize_exc_name(unsigned int ecode, unsigned int esubcode)
254 {
255 /*
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.
259 */
260 switch (ecode) {
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";
269 case EXCCODE_ADE:
270 switch (esubcode) {
271 case EXSUBCODE_ADEF: return "ADEF";
272 case EXSUBCODE_ADEM: return "ADEM";
273 }
274 break;
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";
284 case EXCCODE_FPE:
285 switch (esubcode) {
286 case EXCSUBCODE_FPE: return "FPE";
287 case EXCSUBCODE_VFPE: return "VFPE";
288 }
289 break;
290 case EXCCODE_WATCH:
291 switch (esubcode) {
292 case EXCSUBCODE_WPEF: return "WPEF";
293 case EXCSUBCODE_WPEM: return "WPEM";
294 }
295 break;
296 case EXCCODE_BTDIS: return "BTD";
297 case EXCCODE_BTE: return "BTE";
298 case EXCCODE_GSPR: return "GSPR";
299 case EXCCODE_HVC: return "HVC";
300 case EXCCODE_GCM:
301 switch (esubcode) {
302 case EXCSUBCODE_GCSC: return "GCSC";
303 case EXCSUBCODE_GCHC: return "GCHC";
304 }
305 break;
306 /*
307 * The manual did not mention the EXCCODE_SE case, but print out it
308 * nevertheless.
309 */
310 case EXCCODE_SE: return "SE";
311 }
312
313 return "???";
314 }
315
print_estat(unsigned long x)316 static void print_estat(unsigned long x)
317 {
318 unsigned int ecode = FIELD_GET(CSR_ESTAT_EXC, x);
319 unsigned int esubcode = FIELD_GET(CSR_ESTAT_ESUBCODE, x);
320
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);
324 }
325
__show_regs(const struct pt_regs * regs)326 static void __show_regs(const struct pt_regs *regs)
327 {
328 const int field = 2 * sizeof(unsigned long);
329 unsigned int exccode = FIELD_GET(CSR_ESTAT_EXC, regs->csr_estat);
330
331 show_regs_print_info(KERN_DEFAULT);
332
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));
351
352 /* The slot for $zero is reused as the syscall restart flag */
353 if (regs->regs[0])
354 printk("syscall restart flag: %0*lx\n", GPR_FIELD(0));
355
356 if (user_mode(regs)) {
357 printk(" ra: %0*lx\n", GPR_FIELD(1));
358 printk(" ERA: %0*lx\n", field, regs->csr_era);
359 } else {
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);
362 }
363 #undef GPR_FIELD
364
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);
371
372 if (exccode >= EXCCODE_TLBL && exccode <= EXCCODE_ALE)
373 printk(" BADV: %0*lx\n", field, regs->csr_badvaddr);
374
375 printk(" PRID: %08x (%s, %s)\n", read_cpucfg(LOONGARCH_CPUCFG0),
376 cpu_family_string(), cpu_full_name_string());
377 }
378
show_regs(struct pt_regs * regs)379 void show_regs(struct pt_regs *regs)
380 {
381 __show_regs((struct pt_regs *)regs);
382 dump_stack();
383 }
384
show_registers(struct pt_regs * regs)385 void show_registers(struct pt_regs *regs)
386 {
387 __show_regs(regs);
388 print_modules();
389 printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
390 current->comm, current->pid, current_thread_info(), current);
391
392 show_stacktrace(current, regs, KERN_DEFAULT, user_mode(regs));
393 show_code((void *)regs->csr_era, user_mode(regs));
394 printk("\n");
395 }
396
397 static DEFINE_RAW_SPINLOCK(die_lock);
398
die(const char * str,struct pt_regs * regs)399 void die(const char *str, struct pt_regs *regs)
400 {
401 int ret;
402 static int die_counter;
403
404 oops_enter();
405
406 ret = notify_die(DIE_OOPS, str, regs, 0,
407 current->thread.trap_nr, SIGSEGV);
408
409 console_verbose();
410 raw_spin_lock_irq(&die_lock);
411 bust_spinlocks(1);
412
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);
417
418 oops_exit();
419
420 if (ret == NOTIFY_STOP)
421 return;
422
423 if (regs && kexec_should_crash(current))
424 crash_kexec(regs);
425
426 if (in_interrupt())
427 panic("Fatal exception in interrupt");
428
429 if (panic_on_oops)
430 panic("Fatal exception");
431
432 make_task_dead(SIGSEGV);
433 }
434
setup_vint_size(unsigned int size)435 static inline void setup_vint_size(unsigned int size)
436 {
437 unsigned int vs;
438
439 vs = ilog2(size/4);
440
441 if (vs == 0 || vs > 7)
442 panic("vint_size %d Not support yet", vs);
443
444 csr_xchg32(vs<<CSR_ECFG_VS_SHIFT, CSR_ECFG_VS, LOONGARCH_CSR_ECFG);
445 }
446
447 /*
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
451 * any bits.
452 */
force_fcsr_sig(unsigned long fcsr,void __user * fault_addr,struct task_struct * tsk)453 static void force_fcsr_sig(unsigned long fcsr,
454 void __user *fault_addr, struct task_struct *tsk)
455 {
456 int si_code = FPE_FLTUNK;
457
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;
468
469 force_sig_fault(SIGFPE, si_code, fault_addr);
470 }
471
process_fpemu_return(int sig,void __user * fault_addr,unsigned long fcsr)472 static int process_fpemu_return(int sig, void __user *fault_addr, unsigned long fcsr)
473 {
474 int si_code;
475
476 switch (sig) {
477 case 0:
478 return 0;
479
480 case SIGFPE:
481 force_fcsr_sig(fcsr, fault_addr, current);
482 return 1;
483
484 case SIGBUS:
485 force_sig_fault(SIGBUS, BUS_ADRERR, fault_addr);
486 return 1;
487
488 case SIGSEGV:
489 mmap_read_lock(current->mm);
490 if (vma_lookup(current->mm, (unsigned long)fault_addr))
491 si_code = SEGV_ACCERR;
492 else
493 si_code = SEGV_MAPERR;
494 mmap_read_unlock(current->mm);
495 force_sig_fault(SIGSEGV, si_code, fault_addr);
496 return 1;
497
498 default:
499 force_sig(sig);
500 return 1;
501 }
502 }
503
504 /*
505 * Delayed fp exceptions when doing a lazy ctx switch
506 */
do_fpe(struct pt_regs * regs,unsigned long fcsr)507 asmlinkage void noinstr do_fpe(struct pt_regs *regs, unsigned long fcsr)
508 {
509 int sig;
510 void __user *fault_addr;
511 irqentry_state_t state = irqentry_enter(regs);
512
513 if (notify_die(DIE_FP, "FP exception", regs, 0, current->thread.trap_nr,
514 SIGFPE) == NOTIFY_STOP)
515 goto out;
516
517 /* Clear FCSR.Cause before enabling interrupts */
518 write_fcsr(LOONGARCH_FCSR0, fcsr & ~mask_fcsr_x(fcsr));
519 local_irq_enable();
520
521 die_if_kernel("FP exception in kernel code", regs);
522
523 sig = SIGFPE;
524 fault_addr = (void __user *) regs->csr_era;
525
526 /* Send a signal if required. */
527 process_fpemu_return(sig, fault_addr, fcsr);
528
529 out:
530 local_irq_disable();
531 irqentry_exit(regs, state);
532 }
533
do_ade(struct pt_regs * regs)534 asmlinkage void noinstr do_ade(struct pt_regs *regs)
535 {
536 irqentry_state_t state = irqentry_enter(regs);
537
538 die_if_kernel("Kernel ade access", regs);
539 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)regs->csr_badvaddr);
540
541 irqentry_exit(regs, state);
542 }
543
544 /* sysctl hooks */
545 int unaligned_enabled __read_mostly = 1; /* Enabled by default */
546 int no_unaligned_warning __read_mostly = 1; /* Only 1 warning by default */
547
do_ale(struct pt_regs * regs)548 asmlinkage void noinstr do_ale(struct pt_regs *regs)
549 {
550 irqentry_state_t state = irqentry_enter(regs);
551
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);
555 #else
556 bool pie = regs_irqs_disabled(regs);
557 unsigned int *pc;
558
559 if (!pie)
560 local_irq_enable();
561
562 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, regs->csr_badvaddr);
563
564 /*
565 * Did we catch a fault trying to load an instruction?
566 */
567 if (regs->csr_badvaddr == regs->csr_era)
568 goto sigbus;
569 if (user_mode(regs) && !test_thread_flag(TIF_FIXADE))
570 goto sigbus;
571 if (!unaligned_enabled)
572 goto sigbus;
573 if (!no_unaligned_warning)
574 show_registers(regs);
575
576 pc = (unsigned int *)exception_era(regs);
577
578 emulate_load_store_insn(regs, (void __user *)regs->csr_badvaddr, pc);
579
580 goto out;
581
582 sigbus:
583 die_if_kernel("Kernel ale access", regs);
584 force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
585 out:
586 if (!pie)
587 local_irq_disable();
588 #endif
589 irqentry_exit(regs, state);
590 }
591
592 #ifdef CONFIG_GENERIC_BUG
is_valid_bugaddr(unsigned long addr)593 int is_valid_bugaddr(unsigned long addr)
594 {
595 return 1;
596 }
597 #endif /* CONFIG_GENERIC_BUG */
598
bug_handler(struct pt_regs * regs)599 static void bug_handler(struct pt_regs *regs)
600 {
601 switch (report_bug(regs->csr_era, regs)) {
602 case BUG_TRAP_TYPE_BUG:
603 case BUG_TRAP_TYPE_NONE:
604 die_if_kernel("Oops - BUG", regs);
605 force_sig(SIGTRAP);
606 break;
607
608 case BUG_TRAP_TYPE_WARN:
609 /* Skip the BUG instruction and continue */
610 regs->csr_era += LOONGARCH_INSN_SIZE;
611 break;
612 }
613 }
614
do_bce(struct pt_regs * regs)615 asmlinkage void noinstr do_bce(struct pt_regs *regs)
616 {
617 bool user = user_mode(regs);
618 bool pie = regs_irqs_disabled(regs);
619 unsigned long era = exception_era(regs);
620 u64 badv = 0, lower = 0, upper = ULONG_MAX;
621 union loongarch_instruction insn;
622 irqentry_state_t state = irqentry_enter(regs);
623
624 if (!pie)
625 local_irq_enable();
626
627 current->thread.trap_nr = read_csr_excode();
628
629 die_if_kernel("Bounds check error in kernel code", regs);
630
631 /*
632 * Pull out the address that failed bounds checking, and the lower /
633 * upper bound, by minimally looking at the faulting instruction word
634 * and reading from the correct register.
635 */
636 if (__get_inst(&insn.word, (u32 *)era, user))
637 goto bad_era;
638
639 switch (insn.reg3_format.opcode) {
640 case asrtle_op:
641 if (insn.reg3_format.rd != 0)
642 break; /* not asrtle */
643 badv = regs->regs[insn.reg3_format.rj];
644 upper = regs->regs[insn.reg3_format.rk];
645 break;
646
647 case asrtgt_op:
648 if (insn.reg3_format.rd != 0)
649 break; /* not asrtgt */
650 badv = regs->regs[insn.reg3_format.rj];
651 lower = regs->regs[insn.reg3_format.rk];
652 break;
653
654 case ldleb_op:
655 case ldleh_op:
656 case ldlew_op:
657 case ldled_op:
658 case stleb_op:
659 case stleh_op:
660 case stlew_op:
661 case stled_op:
662 case fldles_op:
663 case fldled_op:
664 case fstles_op:
665 case fstled_op:
666 badv = regs->regs[insn.reg3_format.rj];
667 upper = regs->regs[insn.reg3_format.rk];
668 break;
669
670 case ldgtb_op:
671 case ldgth_op:
672 case ldgtw_op:
673 case ldgtd_op:
674 case stgtb_op:
675 case stgth_op:
676 case stgtw_op:
677 case stgtd_op:
678 case fldgts_op:
679 case fldgtd_op:
680 case fstgts_op:
681 case fstgtd_op:
682 badv = regs->regs[insn.reg3_format.rj];
683 lower = regs->regs[insn.reg3_format.rk];
684 break;
685 }
686
687 force_sig_bnderr((void __user *)badv, (void __user *)lower, (void __user *)upper);
688
689 out:
690 if (!pie)
691 local_irq_disable();
692
693 irqentry_exit(regs, state);
694 return;
695
696 bad_era:
697 /*
698 * Cannot pull out the instruction word, hence cannot provide more
699 * info than a regular SIGSEGV in this case.
700 */
701 force_sig(SIGSEGV);
702 goto out;
703 }
704
do_bp(struct pt_regs * regs)705 asmlinkage void noinstr do_bp(struct pt_regs *regs)
706 {
707 bool user = user_mode(regs);
708 bool pie = regs_irqs_disabled(regs);
709 unsigned int opcode, bcode;
710 unsigned long era = exception_era(regs);
711 irqentry_state_t state = irqentry_enter(regs);
712
713 if (!pie)
714 local_irq_enable();
715
716 if (__get_inst(&opcode, (u32 *)era, user))
717 goto out_sigsegv;
718
719 bcode = (opcode & 0x7fff);
720
721 /*
722 * notify the kprobe handlers, if instruction is likely to
723 * pertain to them.
724 */
725 switch (bcode) {
726 case BRK_KDB:
727 if (kgdb_breakpoint_handler(regs))
728 goto out;
729 else
730 break;
731 case BRK_KPROBE_BP:
732 if (kprobe_breakpoint_handler(regs))
733 goto out;
734 else
735 break;
736 case BRK_KPROBE_SSTEPBP:
737 if (kprobe_singlestep_handler(regs))
738 goto out;
739 else
740 break;
741 case BRK_UPROBE_BP:
742 if (uprobe_breakpoint_handler(regs))
743 goto out;
744 else
745 break;
746 case BRK_UPROBE_XOLBP:
747 if (uprobe_singlestep_handler(regs))
748 goto out;
749 else
750 break;
751 default:
752 current->thread.trap_nr = read_csr_excode();
753 if (notify_die(DIE_TRAP, "Break", regs, bcode,
754 current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
755 goto out;
756 else
757 break;
758 }
759
760 switch (bcode) {
761 case BRK_BUG:
762 bug_handler(regs);
763 break;
764 case BRK_DIVZERO:
765 die_if_kernel("Break instruction in kernel code", regs);
766 force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->csr_era);
767 break;
768 case BRK_OVERFLOW:
769 die_if_kernel("Break instruction in kernel code", regs);
770 force_sig_fault(SIGFPE, FPE_INTOVF, (void __user *)regs->csr_era);
771 break;
772 default:
773 die_if_kernel("Break instruction in kernel code", regs);
774 force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->csr_era);
775 break;
776 }
777
778 out:
779 if (!pie)
780 local_irq_disable();
781
782 irqentry_exit(regs, state);
783 return;
784
785 out_sigsegv:
786 force_sig(SIGSEGV);
787 goto out;
788 }
789
do_watch(struct pt_regs * regs)790 asmlinkage void noinstr do_watch(struct pt_regs *regs)
791 {
792 irqentry_state_t state = irqentry_enter(regs);
793
794 #ifndef CONFIG_HAVE_HW_BREAKPOINT
795 pr_warn("Hardware watch point handler not implemented!\n");
796 #else
797 if (kgdb_breakpoint_handler(regs))
798 goto out;
799
800 if (test_tsk_thread_flag(current, TIF_SINGLESTEP)) {
801 int llbit = (csr_read32(LOONGARCH_CSR_LLBCTL) & 0x1);
802 unsigned long pc = instruction_pointer(regs);
803 union loongarch_instruction *ip = (union loongarch_instruction *)pc;
804
805 if (llbit) {
806 /*
807 * When the ll-sc combo is encountered, it is regarded as an single
808 * instruction. So don't clear llbit and reset CSR.FWPS.Skip until
809 * the llsc execution is completed.
810 */
811 csr_write32(CSR_FWPC_SKIP, LOONGARCH_CSR_FWPS);
812 csr_write32(CSR_LLBCTL_KLO, LOONGARCH_CSR_LLBCTL);
813 goto out;
814 }
815
816 if (pc == current->thread.single_step) {
817 /*
818 * Certain insns are occasionally not skipped when CSR.FWPS.Skip is
819 * set, such as fld.d/fst.d. So singlestep needs to compare whether
820 * the csr_era is equal to the value of singlestep which last time set.
821 */
822 if (!is_self_loop_ins(ip, regs)) {
823 /*
824 * Check if the given instruction the target pc is equal to the
825 * current pc, If yes, then we should not set the CSR.FWPS.SKIP
826 * bit to break the original instruction stream.
827 */
828 csr_write32(CSR_FWPC_SKIP, LOONGARCH_CSR_FWPS);
829 goto out;
830 }
831 }
832 } else {
833 breakpoint_handler(regs);
834 watchpoint_handler(regs);
835 }
836
837 force_sig(SIGTRAP);
838 out:
839 #endif
840 irqentry_exit(regs, state);
841 }
842
do_ri(struct pt_regs * regs)843 asmlinkage void noinstr do_ri(struct pt_regs *regs)
844 {
845 int status = SIGILL;
846 unsigned int __maybe_unused opcode;
847 unsigned int __user *era = (unsigned int __user *)exception_era(regs);
848 irqentry_state_t state = irqentry_enter(regs);
849
850 local_irq_enable();
851 current->thread.trap_nr = read_csr_excode();
852
853 if (notify_die(DIE_RI, "RI Fault", regs, 0, current->thread.trap_nr,
854 SIGILL) == NOTIFY_STOP)
855 goto out;
856
857 die_if_kernel("Reserved instruction in kernel code", regs);
858
859 if (unlikely(get_user(opcode, era) < 0)) {
860 status = SIGSEGV;
861 current->thread.error_code = 1;
862 }
863
864 force_sig(status);
865
866 out:
867 local_irq_disable();
868 irqentry_exit(regs, state);
869 }
870
init_restore_fp(void)871 static void init_restore_fp(void)
872 {
873 if (!used_math()) {
874 /* First time FP context user. */
875 init_fpu();
876 } else {
877 /* This task has formerly used the FP context */
878 if (!is_fpu_owner())
879 own_fpu_inatomic(1);
880 }
881
882 BUG_ON(!is_fp_enabled());
883 }
884
init_restore_lsx(void)885 static void init_restore_lsx(void)
886 {
887 enable_lsx();
888
889 if (!thread_lsx_context_live()) {
890 /* First time LSX context user */
891 init_restore_fp();
892 init_lsx_upper();
893 set_thread_flag(TIF_LSX_CTX_LIVE);
894 } else {
895 if (!is_simd_owner()) {
896 if (is_fpu_owner()) {
897 restore_lsx_upper(current);
898 } else {
899 __own_fpu();
900 restore_lsx(current);
901 }
902 }
903 }
904
905 set_thread_flag(TIF_USEDSIMD);
906
907 BUG_ON(!is_fp_enabled());
908 BUG_ON(!is_lsx_enabled());
909 }
910
init_restore_lasx(void)911 static void init_restore_lasx(void)
912 {
913 enable_lasx();
914
915 if (!thread_lasx_context_live()) {
916 /* First time LASX context user */
917 init_restore_lsx();
918 init_lasx_upper();
919 set_thread_flag(TIF_LASX_CTX_LIVE);
920 } else {
921 if (is_fpu_owner() || is_simd_owner()) {
922 init_restore_lsx();
923 restore_lasx_upper(current);
924 } else {
925 __own_fpu();
926 enable_lsx();
927 restore_lasx(current);
928 }
929 }
930
931 set_thread_flag(TIF_USEDSIMD);
932
933 BUG_ON(!is_fp_enabled());
934 BUG_ON(!is_lsx_enabled());
935 BUG_ON(!is_lasx_enabled());
936 }
937
do_fpu(struct pt_regs * regs)938 asmlinkage void noinstr do_fpu(struct pt_regs *regs)
939 {
940 irqentry_state_t state = irqentry_enter(regs);
941
942 local_irq_enable();
943 die_if_kernel("do_fpu invoked from kernel context!", regs);
944 BUG_ON(is_lsx_enabled());
945 BUG_ON(is_lasx_enabled());
946
947 preempt_disable();
948 init_restore_fp();
949 preempt_enable();
950
951 local_irq_disable();
952 irqentry_exit(regs, state);
953 }
954
do_lsx(struct pt_regs * regs)955 asmlinkage void noinstr do_lsx(struct pt_regs *regs)
956 {
957 irqentry_state_t state = irqentry_enter(regs);
958
959 local_irq_enable();
960 if (!cpu_has_lsx) {
961 force_sig(SIGILL);
962 goto out;
963 }
964
965 die_if_kernel("do_lsx invoked from kernel context!", regs);
966 BUG_ON(is_lasx_enabled());
967
968 preempt_disable();
969 init_restore_lsx();
970 preempt_enable();
971
972 out:
973 local_irq_disable();
974 irqentry_exit(regs, state);
975 }
976
do_lasx(struct pt_regs * regs)977 asmlinkage void noinstr do_lasx(struct pt_regs *regs)
978 {
979 irqentry_state_t state = irqentry_enter(regs);
980
981 local_irq_enable();
982 if (!cpu_has_lasx) {
983 force_sig(SIGILL);
984 goto out;
985 }
986
987 die_if_kernel("do_lasx invoked from kernel context!", regs);
988
989 preempt_disable();
990 init_restore_lasx();
991 preempt_enable();
992
993 out:
994 local_irq_disable();
995 irqentry_exit(regs, state);
996 }
997
init_restore_lbt(void)998 static void init_restore_lbt(void)
999 {
1000 if (!thread_lbt_context_live()) {
1001 /* First time LBT context user */
1002 init_lbt();
1003 set_thread_flag(TIF_LBT_CTX_LIVE);
1004 } else {
1005 if (!is_lbt_owner())
1006 own_lbt_inatomic(1);
1007 }
1008
1009 BUG_ON(!is_lbt_enabled());
1010 }
1011
do_lbt(struct pt_regs * regs)1012 asmlinkage void noinstr do_lbt(struct pt_regs *regs)
1013 {
1014 bool pie = regs_irqs_disabled(regs);
1015 irqentry_state_t state = irqentry_enter(regs);
1016
1017 /*
1018 * BTD (Binary Translation Disable exception) can be triggered
1019 * during FP save/restore if TM (Top Mode) is on, which may
1020 * cause irq_enable during 'switch_to'. To avoid this situation
1021 * (including the user using 'MOVGR2GCSR' to turn on TM, which
1022 * will not trigger the BTE), we need to check PRMD first.
1023 */
1024 if (!pie)
1025 local_irq_enable();
1026
1027 if (!cpu_has_lbt) {
1028 force_sig(SIGILL);
1029 goto out;
1030 }
1031 BUG_ON(is_lbt_enabled());
1032
1033 preempt_disable();
1034 init_restore_lbt();
1035 preempt_enable();
1036
1037 out:
1038 if (!pie)
1039 local_irq_disable();
1040
1041 irqentry_exit(regs, state);
1042 }
1043
do_reserved(struct pt_regs * regs)1044 asmlinkage void noinstr do_reserved(struct pt_regs *regs)
1045 {
1046 irqentry_state_t state = irqentry_enter(regs);
1047
1048 local_irq_enable();
1049 /*
1050 * Game over - no way to handle this if it ever occurs. Most probably
1051 * caused by a fatal error after another hardware/software error.
1052 */
1053 pr_err("Caught reserved exception %u on pid:%d [%s] - should not happen\n",
1054 read_csr_excode(), current->pid, current->comm);
1055 die_if_kernel("do_reserved exception", regs);
1056 force_sig(SIGUNUSED);
1057
1058 local_irq_disable();
1059
1060 irqentry_exit(regs, state);
1061 }
1062
cache_parity_error(void)1063 asmlinkage void cache_parity_error(void)
1064 {
1065 /* For the moment, report the problem and hang. */
1066 pr_err("Cache error exception:\n");
1067 pr_err("csr_merrctl == %08x\n", csr_read32(LOONGARCH_CSR_MERRCTL));
1068 pr_err("csr_merrera == %016lx\n", csr_read64(LOONGARCH_CSR_MERRERA));
1069 panic("Can't handle the cache error!");
1070 }
1071
handle_loongarch_irq(struct pt_regs * regs)1072 asmlinkage void noinstr handle_loongarch_irq(struct pt_regs *regs)
1073 {
1074 struct pt_regs *old_regs;
1075
1076 irq_enter_rcu();
1077 old_regs = set_irq_regs(regs);
1078 handle_arch_irq(regs);
1079 set_irq_regs(old_regs);
1080 irq_exit_rcu();
1081 }
1082
do_vint(struct pt_regs * regs,unsigned long sp)1083 asmlinkage void noinstr do_vint(struct pt_regs *regs, unsigned long sp)
1084 {
1085 register int cpu;
1086 register unsigned long stack;
1087 irqentry_state_t state = irqentry_enter(regs);
1088
1089 cpu = smp_processor_id();
1090
1091 if (on_irq_stack(cpu, sp))
1092 handle_loongarch_irq(regs);
1093 else {
1094 stack = per_cpu(irq_stack, cpu) + IRQ_STACK_START;
1095
1096 /* Save task's sp on IRQ stack for unwinding */
1097 *(unsigned long *)stack = sp;
1098
1099 __asm__ __volatile__(
1100 "move $s0, $sp \n" /* Preserve sp */
1101 "move $sp, %[stk] \n" /* Switch stack */
1102 "move $a0, %[regs] \n"
1103 "bl handle_loongarch_irq \n"
1104 "move $sp, $s0 \n" /* Restore sp */
1105 : /* No outputs */
1106 : [stk] "r" (stack), [regs] "r" (regs)
1107 : "$a0", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7", "$s0",
1108 "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8",
1109 "memory");
1110 }
1111
1112 irqentry_exit(regs, state);
1113 }
1114
1115 unsigned long eentry;
1116 unsigned long tlbrentry;
1117
1118 long exception_handlers[VECSIZE * 128 / sizeof(long)] __aligned(SZ_64K);
1119
configure_exception_vector(void)1120 static void configure_exception_vector(void)
1121 {
1122 eentry = (unsigned long)exception_handlers;
1123 tlbrentry = (unsigned long)exception_handlers + 80*VECSIZE;
1124
1125 csr_write64(eentry, LOONGARCH_CSR_EENTRY);
1126 csr_write64(eentry, LOONGARCH_CSR_MERRENTRY);
1127 csr_write64(tlbrentry, LOONGARCH_CSR_TLBRENTRY);
1128 }
1129
per_cpu_trap_init(int cpu)1130 void per_cpu_trap_init(int cpu)
1131 {
1132 unsigned int i;
1133
1134 setup_vint_size(VECSIZE);
1135
1136 configure_exception_vector();
1137
1138 if (!cpu_data[cpu].asid_cache)
1139 cpu_data[cpu].asid_cache = asid_first_version(cpu);
1140
1141 mmgrab(&init_mm);
1142 current->active_mm = &init_mm;
1143 BUG_ON(current->mm);
1144 enter_lazy_tlb(&init_mm, current);
1145
1146 /* Initialise exception handlers */
1147 if (cpu == 0)
1148 for (i = 0; i < 64; i++)
1149 set_handler(i * VECSIZE, handle_reserved, VECSIZE);
1150
1151 tlb_init(cpu);
1152 cpu_cache_init();
1153 }
1154
1155 /* Install CPU exception handler */
set_handler(unsigned long offset,void * addr,unsigned long size)1156 void set_handler(unsigned long offset, void *addr, unsigned long size)
1157 {
1158 memcpy((void *)(eentry + offset), addr, size);
1159 local_flush_icache_range(eentry + offset, eentry + offset + size);
1160 }
1161
1162 static const char panic_null_cerr[] =
1163 "Trying to set NULL cache error exception handler\n";
1164
1165 /*
1166 * Install uncached CPU exception handler.
1167 * This is suitable only for the cache error exception which is the only
1168 * exception handler that is being run uncached.
1169 */
set_merr_handler(unsigned long offset,void * addr,unsigned long size)1170 void set_merr_handler(unsigned long offset, void *addr, unsigned long size)
1171 {
1172 unsigned long uncached_eentry = TO_UNCACHE(__pa(eentry));
1173
1174 if (!addr)
1175 panic(panic_null_cerr);
1176
1177 memcpy((void *)(uncached_eentry + offset), addr, size);
1178 }
1179
trap_init(void)1180 void __init trap_init(void)
1181 {
1182 long i;
1183
1184 /* Set interrupt vector handler */
1185 for (i = EXCCODE_INT_START; i <= EXCCODE_INT_END; i++)
1186 set_handler(i * VECSIZE, handle_vint, VECSIZE);
1187
1188 /* Set exception vector handler */
1189 for (i = EXCCODE_ADE; i <= EXCCODE_BTDIS; i++)
1190 set_handler(i * VECSIZE, exception_table[i], VECSIZE);
1191
1192 cache_error_setup();
1193
1194 local_flush_icache_range(eentry, eentry + 0x400);
1195 }
1196