traps.c 28 KB

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