PageRenderTime 322ms CodeModel.GetById 38ms RepoModel.GetById 2ms app.codeStats 32ms

/arch/mips/kernel/traps.c

http://github.com/mirrors/linux
C | 2472 lines | 1727 code | 366 blank | 379 comment | 330 complexity | feec4225bb4818f89b5fb703aa91380c MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1994 - 1999, 2000, 01, 06 Ralf Baechle
  7. * Copyright (C) 1995, 1996 Paul M. Antoine
  8. * Copyright (C) 1998 Ulf Carlsson
  9. * Copyright (C) 1999 Silicon Graphics, Inc.
  10. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  11. * Copyright (C) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
  12. * Copyright (C) 2000, 2001, 2012 MIPS Technologies, Inc. All rights reserved.
  13. * Copyright (C) 2014, Imagination Technologies Ltd.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/bug.h>
  17. #include <linux/compiler.h>
  18. #include <linux/context_tracking.h>
  19. #include <linux/cpu_pm.h>
  20. #include <linux/kexec.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/extable.h>
  25. #include <linux/mm.h>
  26. #include <linux/sched/mm.h>
  27. #include <linux/sched/debug.h>
  28. #include <linux/smp.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/kallsyms.h>
  31. #include <linux/memblock.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/kgdb.h>
  35. #include <linux/kdebug.h>
  36. #include <linux/kprobes.h>
  37. #include <linux/notifier.h>
  38. #include <linux/kdb.h>
  39. #include <linux/irq.h>
  40. #include <linux/perf_event.h>
  41. #include <asm/addrspace.h>
  42. #include <asm/bootinfo.h>
  43. #include <asm/branch.h>
  44. #include <asm/break.h>
  45. #include <asm/cop2.h>
  46. #include <asm/cpu.h>
  47. #include <asm/cpu-type.h>
  48. #include <asm/dsp.h>
  49. #include <asm/fpu.h>
  50. #include <asm/fpu_emulator.h>
  51. #include <asm/idle.h>
  52. #include <asm/isa-rev.h>
  53. #include <asm/mips-cps.h>
  54. #include <asm/mips-r2-to-r6-emul.h>
  55. #include <asm/mipsregs.h>
  56. #include <asm/mipsmtregs.h>
  57. #include <asm/module.h>
  58. #include <asm/msa.h>
  59. #include <asm/pgtable.h>
  60. #include <asm/ptrace.h>
  61. #include <asm/sections.h>
  62. #include <asm/siginfo.h>
  63. #include <asm/tlbdebug.h>
  64. #include <asm/traps.h>
  65. #include <linux/uaccess.h>
  66. #include <asm/watch.h>
  67. #include <asm/mmu_context.h>
  68. #include <asm/types.h>
  69. #include <asm/stacktrace.h>
  70. #include <asm/tlbex.h>
  71. #include <asm/uasm.h>
  72. extern void check_wait(void);
  73. extern asmlinkage void rollback_handle_int(void);
  74. extern asmlinkage void handle_int(void);
  75. extern asmlinkage void handle_adel(void);
  76. extern asmlinkage void handle_ades(void);
  77. extern asmlinkage void handle_ibe(void);
  78. extern asmlinkage void handle_dbe(void);
  79. extern asmlinkage void handle_sys(void);
  80. extern asmlinkage void handle_bp(void);
  81. extern asmlinkage void handle_ri(void);
  82. extern asmlinkage void handle_ri_rdhwr_tlbp(void);
  83. extern asmlinkage void handle_ri_rdhwr(void);
  84. extern asmlinkage void handle_cpu(void);
  85. extern asmlinkage void handle_ov(void);
  86. extern asmlinkage void handle_tr(void);
  87. extern asmlinkage void handle_msa_fpe(void);
  88. extern asmlinkage void handle_fpe(void);
  89. extern asmlinkage void handle_ftlb(void);
  90. extern asmlinkage void handle_msa(void);
  91. extern asmlinkage void handle_mdmx(void);
  92. extern asmlinkage void handle_watch(void);
  93. extern asmlinkage void handle_mt(void);
  94. extern asmlinkage void handle_dsp(void);
  95. extern asmlinkage void handle_mcheck(void);
  96. extern asmlinkage void handle_reserved(void);
  97. extern void tlb_do_page_fault_0(void);
  98. void (*board_be_init)(void);
  99. int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
  100. void (*board_nmi_handler_setup)(void);
  101. void (*board_ejtag_handler_setup)(void);
  102. void (*board_bind_eic_interrupt)(int irq, int regset);
  103. void (*board_ebase_setup)(void);
  104. void(*board_cache_error_setup)(void);
  105. static void show_raw_backtrace(unsigned long reg29)
  106. {
  107. unsigned long *sp = (unsigned long *)(reg29 & ~3);
  108. unsigned long addr;
  109. printk("Call Trace:");
  110. #ifdef CONFIG_KALLSYMS
  111. printk("\n");
  112. #endif
  113. while (!kstack_end(sp)) {
  114. unsigned long __user *p =
  115. (unsigned long __user *)(unsigned long)sp++;
  116. if (__get_user(addr, p)) {
  117. printk(" (Bad stack address)");
  118. break;
  119. }
  120. if (__kernel_text_address(addr))
  121. print_ip_sym(addr);
  122. }
  123. printk("\n");
  124. }
  125. #ifdef CONFIG_KALLSYMS
  126. int raw_show_trace;
  127. static int __init set_raw_show_trace(char *str)
  128. {
  129. raw_show_trace = 1;
  130. return 1;
  131. }
  132. __setup("raw_show_trace", set_raw_show_trace);
  133. #endif
  134. static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
  135. {
  136. unsigned long sp = regs->regs[29];
  137. unsigned long ra = regs->regs[31];
  138. unsigned long pc = regs->cp0_epc;
  139. if (!task)
  140. task = current;
  141. if (raw_show_trace || user_mode(regs) || !__kernel_text_address(pc)) {
  142. show_raw_backtrace(sp);
  143. return;
  144. }
  145. printk("Call Trace:\n");
  146. do {
  147. print_ip_sym(pc);
  148. pc = unwind_stack(task, &sp, pc, &ra);
  149. } while (pc);
  150. pr_cont("\n");
  151. }
  152. /*
  153. * This routine abuses get_user()/put_user() to reference pointers
  154. * with at least a bit of error checking ...
  155. */
  156. static void show_stacktrace(struct task_struct *task,
  157. const struct pt_regs *regs)
  158. {
  159. const int field = 2 * sizeof(unsigned long);
  160. long stackdata;
  161. int i;
  162. unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
  163. printk("Stack :");
  164. i = 0;
  165. while ((unsigned long) sp & (PAGE_SIZE - 1)) {
  166. if (i && ((i % (64 / field)) == 0)) {
  167. pr_cont("\n");
  168. printk(" ");
  169. }
  170. if (i > 39) {
  171. pr_cont(" ...");
  172. break;
  173. }
  174. if (__get_user(stackdata, sp++)) {
  175. pr_cont(" (Bad stack address)");
  176. break;
  177. }
  178. pr_cont(" %0*lx", field, stackdata);
  179. i++;
  180. }
  181. pr_cont("\n");
  182. show_backtrace(task, regs);
  183. }
  184. void show_stack(struct task_struct *task, unsigned long *sp)
  185. {
  186. struct pt_regs regs;
  187. mm_segment_t old_fs = get_fs();
  188. regs.cp0_status = KSU_KERNEL;
  189. if (sp) {
  190. regs.regs[29] = (unsigned long)sp;
  191. regs.regs[31] = 0;
  192. regs.cp0_epc = 0;
  193. } else {
  194. if (task && task != current) {
  195. regs.regs[29] = task->thread.reg29;
  196. regs.regs[31] = 0;
  197. regs.cp0_epc = task->thread.reg31;
  198. } else {
  199. prepare_frametrace(&regs);
  200. }
  201. }
  202. /*
  203. * show_stack() deals exclusively with kernel mode, so be sure to access
  204. * the stack in the kernel (not user) address space.
  205. */
  206. set_fs(KERNEL_DS);
  207. show_stacktrace(task, &regs);
  208. set_fs(old_fs);
  209. }
  210. static void show_code(unsigned int __user *pc)
  211. {
  212. long i;
  213. unsigned short __user *pc16 = NULL;
  214. printk("Code:");
  215. if ((unsigned long)pc & 1)
  216. pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
  217. for(i = -3 ; i < 6 ; i++) {
  218. unsigned int insn;
  219. if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
  220. pr_cont(" (Bad address in epc)\n");
  221. break;
  222. }
  223. pr_cont("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
  224. }
  225. pr_cont("\n");
  226. }
  227. static void __show_regs(const struct pt_regs *regs)
  228. {
  229. const int field = 2 * sizeof(unsigned long);
  230. unsigned int cause = regs->cp0_cause;
  231. unsigned int exccode;
  232. int i;
  233. show_regs_print_info(KERN_DEFAULT);
  234. /*
  235. * Saved main processor registers
  236. */
  237. for (i = 0; i < 32; ) {
  238. if ((i % 4) == 0)
  239. printk("$%2d :", i);
  240. if (i == 0)
  241. pr_cont(" %0*lx", field, 0UL);
  242. else if (i == 26 || i == 27)
  243. pr_cont(" %*s", field, "");
  244. else
  245. pr_cont(" %0*lx", field, regs->regs[i]);
  246. i++;
  247. if ((i % 4) == 0)
  248. pr_cont("\n");
  249. }
  250. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  251. printk("Acx : %0*lx\n", field, regs->acx);
  252. #endif
  253. if (MIPS_ISA_REV < 6) {
  254. printk("Hi : %0*lx\n", field, regs->hi);
  255. printk("Lo : %0*lx\n", field, regs->lo);
  256. }
  257. /*
  258. * Saved cp0 registers
  259. */
  260. printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
  261. (void *) regs->cp0_epc);
  262. printk("ra : %0*lx %pS\n", field, regs->regs[31],
  263. (void *) regs->regs[31]);
  264. printk("Status: %08x ", (uint32_t) regs->cp0_status);
  265. if (cpu_has_3kex) {
  266. if (regs->cp0_status & ST0_KUO)
  267. pr_cont("KUo ");
  268. if (regs->cp0_status & ST0_IEO)
  269. pr_cont("IEo ");
  270. if (regs->cp0_status & ST0_KUP)
  271. pr_cont("KUp ");
  272. if (regs->cp0_status & ST0_IEP)
  273. pr_cont("IEp ");
  274. if (regs->cp0_status & ST0_KUC)
  275. pr_cont("KUc ");
  276. if (regs->cp0_status & ST0_IEC)
  277. pr_cont("IEc ");
  278. } else if (cpu_has_4kex) {
  279. if (regs->cp0_status & ST0_KX)
  280. pr_cont("KX ");
  281. if (regs->cp0_status & ST0_SX)
  282. pr_cont("SX ");
  283. if (regs->cp0_status & ST0_UX)
  284. pr_cont("UX ");
  285. switch (regs->cp0_status & ST0_KSU) {
  286. case KSU_USER:
  287. pr_cont("USER ");
  288. break;
  289. case KSU_SUPERVISOR:
  290. pr_cont("SUPERVISOR ");
  291. break;
  292. case KSU_KERNEL:
  293. pr_cont("KERNEL ");
  294. break;
  295. default:
  296. pr_cont("BAD_MODE ");
  297. break;
  298. }
  299. if (regs->cp0_status & ST0_ERL)
  300. pr_cont("ERL ");
  301. if (regs->cp0_status & ST0_EXL)
  302. pr_cont("EXL ");
  303. if (regs->cp0_status & ST0_IE)
  304. pr_cont("IE ");
  305. }
  306. pr_cont("\n");
  307. exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  308. printk("Cause : %08x (ExcCode %02x)\n", cause, exccode);
  309. if (1 <= exccode && exccode <= 5)
  310. printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
  311. printk("PrId : %08x (%s)\n", read_c0_prid(),
  312. cpu_name_string());
  313. }
  314. /*
  315. * FIXME: really the generic show_regs should take a const pointer argument.
  316. */
  317. void show_regs(struct pt_regs *regs)
  318. {
  319. __show_regs(regs);
  320. dump_stack();
  321. }
  322. void show_registers(struct pt_regs *regs)
  323. {
  324. const int field = 2 * sizeof(unsigned long);
  325. mm_segment_t old_fs = get_fs();
  326. __show_regs(regs);
  327. print_modules();
  328. printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
  329. current->comm, current->pid, current_thread_info(), current,
  330. field, current_thread_info()->tp_value);
  331. if (cpu_has_userlocal) {
  332. unsigned long tls;
  333. tls = read_c0_userlocal();
  334. if (tls != current_thread_info()->tp_value)
  335. printk("*HwTLS: %0*lx\n", field, tls);
  336. }
  337. if (!user_mode(regs))
  338. /* Necessary for getting the correct stack content */
  339. set_fs(KERNEL_DS);
  340. show_stacktrace(current, regs);
  341. show_code((unsigned int __user *) regs->cp0_epc);
  342. printk("\n");
  343. set_fs(old_fs);
  344. }
  345. static DEFINE_RAW_SPINLOCK(die_lock);
  346. void __noreturn die(const char *str, struct pt_regs *regs)
  347. {
  348. static int die_counter;
  349. int sig = SIGSEGV;
  350. oops_enter();
  351. if (notify_die(DIE_OOPS, str, regs, 0, current->thread.trap_nr,
  352. SIGSEGV) == NOTIFY_STOP)
  353. sig = 0;
  354. console_verbose();
  355. raw_spin_lock_irq(&die_lock);
  356. bust_spinlocks(1);
  357. printk("%s[#%d]:\n", str, ++die_counter);
  358. show_registers(regs);
  359. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  360. raw_spin_unlock_irq(&die_lock);
  361. oops_exit();
  362. if (in_interrupt())
  363. panic("Fatal exception in interrupt");
  364. if (panic_on_oops)
  365. panic("Fatal exception");
  366. if (regs && kexec_should_crash(current))
  367. crash_kexec(regs);
  368. do_exit(sig);
  369. }
  370. extern struct exception_table_entry __start___dbe_table[];
  371. extern struct exception_table_entry __stop___dbe_table[];
  372. __asm__(
  373. " .section __dbe_table, \"a\"\n"
  374. " .previous \n");
  375. /* Given an address, look for it in the exception tables. */
  376. static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
  377. {
  378. const struct exception_table_entry *e;
  379. e = search_extable(__start___dbe_table,
  380. __stop___dbe_table - __start___dbe_table, addr);
  381. if (!e)
  382. e = search_module_dbetables(addr);
  383. return e;
  384. }
  385. asmlinkage void do_be(struct pt_regs *regs)
  386. {
  387. const int field = 2 * sizeof(unsigned long);
  388. const struct exception_table_entry *fixup = NULL;
  389. int data = regs->cp0_cause & 4;
  390. int action = MIPS_BE_FATAL;
  391. enum ctx_state prev_state;
  392. prev_state = exception_enter();
  393. /* XXX For now. Fixme, this searches the wrong table ... */
  394. if (data && !user_mode(regs))
  395. fixup = search_dbe_tables(exception_epc(regs));
  396. if (fixup)
  397. action = MIPS_BE_FIXUP;
  398. if (board_be_handler)
  399. action = board_be_handler(regs, fixup != NULL);
  400. else
  401. mips_cm_error_report();
  402. switch (action) {
  403. case MIPS_BE_DISCARD:
  404. goto out;
  405. case MIPS_BE_FIXUP:
  406. if (fixup) {
  407. regs->cp0_epc = fixup->nextinsn;
  408. goto out;
  409. }
  410. break;
  411. default:
  412. break;
  413. }
  414. /*
  415. * Assume it would be too dangerous to continue ...
  416. */
  417. printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
  418. data ? "Data" : "Instruction",
  419. field, regs->cp0_epc, field, regs->regs[31]);
  420. if (notify_die(DIE_OOPS, "bus error", regs, 0, current->thread.trap_nr,
  421. SIGBUS) == NOTIFY_STOP)
  422. goto out;
  423. die_if_kernel("Oops", regs);
  424. force_sig(SIGBUS);
  425. out:
  426. exception_exit(prev_state);
  427. }
  428. /*
  429. * ll/sc, rdhwr, sync emulation
  430. */
  431. #define OPCODE 0xfc000000
  432. #define BASE 0x03e00000
  433. #define RT 0x001f0000
  434. #define OFFSET 0x0000ffff
  435. #define LL 0xc0000000
  436. #define SC 0xe0000000
  437. #define SPEC0 0x00000000
  438. #define SPEC3 0x7c000000
  439. #define RD 0x0000f800
  440. #define FUNC 0x0000003f
  441. #define SYNC 0x0000000f
  442. #define RDHWR 0x0000003b
  443. /* microMIPS definitions */
  444. #define MM_POOL32A_FUNC 0xfc00ffff
  445. #define MM_RDHWR 0x00006b3c
  446. #define MM_RS 0x001f0000
  447. #define MM_RT 0x03e00000
  448. /*
  449. * The ll_bit is cleared by r*_switch.S
  450. */
  451. unsigned int ll_bit;
  452. struct task_struct *ll_task;
  453. static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
  454. {
  455. unsigned long value, __user *vaddr;
  456. long offset;
  457. /*
  458. * analyse the ll instruction that just caused a ri exception
  459. * and put the referenced address to addr.
  460. */
  461. /* sign extend offset */
  462. offset = opcode & OFFSET;
  463. offset <<= 16;
  464. offset >>= 16;
  465. vaddr = (unsigned long __user *)
  466. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  467. if ((unsigned long)vaddr & 3)
  468. return SIGBUS;
  469. if (get_user(value, vaddr))
  470. return SIGSEGV;
  471. preempt_disable();
  472. if (ll_task == NULL || ll_task == current) {
  473. ll_bit = 1;
  474. } else {
  475. ll_bit = 0;
  476. }
  477. ll_task = current;
  478. preempt_enable();
  479. regs->regs[(opcode & RT) >> 16] = value;
  480. return 0;
  481. }
  482. static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
  483. {
  484. unsigned long __user *vaddr;
  485. unsigned long reg;
  486. long offset;
  487. /*
  488. * analyse the sc instruction that just caused a ri exception
  489. * and put the referenced address to addr.
  490. */
  491. /* sign extend offset */
  492. offset = opcode & OFFSET;
  493. offset <<= 16;
  494. offset >>= 16;
  495. vaddr = (unsigned long __user *)
  496. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  497. reg = (opcode & RT) >> 16;
  498. if ((unsigned long)vaddr & 3)
  499. return SIGBUS;
  500. preempt_disable();
  501. if (ll_bit == 0 || ll_task != current) {
  502. regs->regs[reg] = 0;
  503. preempt_enable();
  504. return 0;
  505. }
  506. preempt_enable();
  507. if (put_user(regs->regs[reg], vaddr))
  508. return SIGSEGV;
  509. regs->regs[reg] = 1;
  510. return 0;
  511. }
  512. /*
  513. * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
  514. * opcodes are supposed to result in coprocessor unusable exceptions if
  515. * executed on ll/sc-less processors. That's the theory. In practice a
  516. * few processors such as NEC's VR4100 throw reserved instruction exceptions
  517. * instead, so we're doing the emulation thing in both exception handlers.
  518. */
  519. static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
  520. {
  521. if ((opcode & OPCODE) == LL) {
  522. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  523. 1, regs, 0);
  524. return simulate_ll(regs, opcode);
  525. }
  526. if ((opcode & OPCODE) == SC) {
  527. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  528. 1, regs, 0);
  529. return simulate_sc(regs, opcode);
  530. }
  531. return -1; /* Must be something else ... */
  532. }
  533. /*
  534. * Simulate trapping 'rdhwr' instructions to provide user accessible
  535. * registers not implemented in hardware.
  536. */
  537. static int simulate_rdhwr(struct pt_regs *regs, int rd, int rt)
  538. {
  539. struct thread_info *ti = task_thread_info(current);
  540. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  541. 1, regs, 0);
  542. switch (rd) {
  543. case MIPS_HWR_CPUNUM: /* CPU number */
  544. regs->regs[rt] = smp_processor_id();
  545. return 0;
  546. case MIPS_HWR_SYNCISTEP: /* SYNCI length */
  547. regs->regs[rt] = min(current_cpu_data.dcache.linesz,
  548. current_cpu_data.icache.linesz);
  549. return 0;
  550. case MIPS_HWR_CC: /* Read count register */
  551. regs->regs[rt] = read_c0_count();
  552. return 0;
  553. case MIPS_HWR_CCRES: /* Count register resolution */
  554. switch (current_cpu_type()) {
  555. case CPU_20KC:
  556. case CPU_25KF:
  557. regs->regs[rt] = 1;
  558. break;
  559. default:
  560. regs->regs[rt] = 2;
  561. }
  562. return 0;
  563. case MIPS_HWR_ULR: /* Read UserLocal register */
  564. regs->regs[rt] = ti->tp_value;
  565. return 0;
  566. default:
  567. return -1;
  568. }
  569. }
  570. static int simulate_rdhwr_normal(struct pt_regs *regs, unsigned int opcode)
  571. {
  572. if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
  573. int rd = (opcode & RD) >> 11;
  574. int rt = (opcode & RT) >> 16;
  575. simulate_rdhwr(regs, rd, rt);
  576. return 0;
  577. }
  578. /* Not ours. */
  579. return -1;
  580. }
  581. static int simulate_rdhwr_mm(struct pt_regs *regs, unsigned int opcode)
  582. {
  583. if ((opcode & MM_POOL32A_FUNC) == MM_RDHWR) {
  584. int rd = (opcode & MM_RS) >> 16;
  585. int rt = (opcode & MM_RT) >> 21;
  586. simulate_rdhwr(regs, rd, rt);
  587. return 0;
  588. }
  589. /* Not ours. */
  590. return -1;
  591. }
  592. static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
  593. {
  594. if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
  595. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  596. 1, regs, 0);
  597. return 0;
  598. }
  599. return -1; /* Must be something else ... */
  600. }
  601. asmlinkage void do_ov(struct pt_regs *regs)
  602. {
  603. enum ctx_state prev_state;
  604. prev_state = exception_enter();
  605. die_if_kernel("Integer overflow", regs);
  606. force_sig_fault(SIGFPE, FPE_INTOVF, (void __user *)regs->cp0_epc);
  607. exception_exit(prev_state);
  608. }
  609. #ifdef CONFIG_MIPS_FP_SUPPORT
  610. /*
  611. * Send SIGFPE according to FCSR Cause bits, which must have already
  612. * been masked against Enable bits. This is impotant as Inexact can
  613. * happen together with Overflow or Underflow, and `ptrace' can set
  614. * any bits.
  615. */
  616. void force_fcr31_sig(unsigned long fcr31, void __user *fault_addr,
  617. struct task_struct *tsk)
  618. {
  619. int si_code = FPE_FLTUNK;
  620. if (fcr31 & FPU_CSR_INV_X)
  621. si_code = FPE_FLTINV;
  622. else if (fcr31 & FPU_CSR_DIV_X)
  623. si_code = FPE_FLTDIV;
  624. else if (fcr31 & FPU_CSR_OVF_X)
  625. si_code = FPE_FLTOVF;
  626. else if (fcr31 & FPU_CSR_UDF_X)
  627. si_code = FPE_FLTUND;
  628. else if (fcr31 & FPU_CSR_INE_X)
  629. si_code = FPE_FLTRES;
  630. force_sig_fault_to_task(SIGFPE, si_code, fault_addr, tsk);
  631. }
  632. int process_fpemu_return(int sig, void __user *fault_addr, unsigned long fcr31)
  633. {
  634. int si_code;
  635. struct vm_area_struct *vma;
  636. switch (sig) {
  637. case 0:
  638. return 0;
  639. case SIGFPE:
  640. force_fcr31_sig(fcr31, fault_addr, current);
  641. return 1;
  642. case SIGBUS:
  643. force_sig_fault(SIGBUS, BUS_ADRERR, fault_addr);
  644. return 1;
  645. case SIGSEGV:
  646. down_read(&current->mm->mmap_sem);
  647. vma = find_vma(current->mm, (unsigned long)fault_addr);
  648. if (vma && (vma->vm_start <= (unsigned long)fault_addr))
  649. si_code = SEGV_ACCERR;
  650. else
  651. si_code = SEGV_MAPERR;
  652. up_read(&current->mm->mmap_sem);
  653. force_sig_fault(SIGSEGV, si_code, fault_addr);
  654. return 1;
  655. default:
  656. force_sig(sig);
  657. return 1;
  658. }
  659. }
  660. static int simulate_fp(struct pt_regs *regs, unsigned int opcode,
  661. unsigned long old_epc, unsigned long old_ra)
  662. {
  663. union mips_instruction inst = { .word = opcode };
  664. void __user *fault_addr;
  665. unsigned long fcr31;
  666. int sig;
  667. /* If it's obviously not an FP instruction, skip it */
  668. switch (inst.i_format.opcode) {
  669. case cop1_op:
  670. case cop1x_op:
  671. case lwc1_op:
  672. case ldc1_op:
  673. case swc1_op:
  674. case sdc1_op:
  675. break;
  676. default:
  677. return -1;
  678. }
  679. /*
  680. * do_ri skipped over the instruction via compute_return_epc, undo
  681. * that for the FPU emulator.
  682. */
  683. regs->cp0_epc = old_epc;
  684. regs->regs[31] = old_ra;
  685. /* Run the emulator */
  686. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  687. &fault_addr);
  688. /*
  689. * We can't allow the emulated instruction to leave any
  690. * enabled Cause bits set in $fcr31.
  691. */
  692. fcr31 = mask_fcr31_x(current->thread.fpu.fcr31);
  693. current->thread.fpu.fcr31 &= ~fcr31;
  694. /* Restore the hardware register state */
  695. own_fpu(1);
  696. /* Send a signal if required. */
  697. process_fpemu_return(sig, fault_addr, fcr31);
  698. return 0;
  699. }
  700. /*
  701. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  702. */
  703. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  704. {
  705. enum ctx_state prev_state;
  706. void __user *fault_addr;
  707. int sig;
  708. prev_state = exception_enter();
  709. if (notify_die(DIE_FP, "FP exception", regs, 0, current->thread.trap_nr,
  710. SIGFPE) == NOTIFY_STOP)
  711. goto out;
  712. /* Clear FCSR.Cause before enabling interrupts */
  713. write_32bit_cp1_register(CP1_STATUS, fcr31 & ~mask_fcr31_x(fcr31));
  714. local_irq_enable();
  715. die_if_kernel("FP exception in kernel code", regs);
  716. if (fcr31 & FPU_CSR_UNI_X) {
  717. /*
  718. * Unimplemented operation exception. If we've got the full
  719. * software emulator on-board, let's use it...
  720. *
  721. * Force FPU to dump state into task/thread context. We're
  722. * moving a lot of data here for what is probably a single
  723. * instruction, but the alternative is to pre-decode the FP
  724. * register operands before invoking the emulator, which seems
  725. * a bit extreme for what should be an infrequent event.
  726. */
  727. /* Run the emulator */
  728. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  729. &fault_addr);
  730. /*
  731. * We can't allow the emulated instruction to leave any
  732. * enabled Cause bits set in $fcr31.
  733. */
  734. fcr31 = mask_fcr31_x(current->thread.fpu.fcr31);
  735. current->thread.fpu.fcr31 &= ~fcr31;
  736. /* Restore the hardware register state */
  737. own_fpu(1); /* Using the FPU again. */
  738. } else {
  739. sig = SIGFPE;
  740. fault_addr = (void __user *) regs->cp0_epc;
  741. }
  742. /* Send a signal if required. */
  743. process_fpemu_return(sig, fault_addr, fcr31);
  744. out:
  745. exception_exit(prev_state);
  746. }
  747. /*
  748. * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
  749. * emulated more than some threshold number of instructions, force migration to
  750. * a "CPU" that has FP support.
  751. */
  752. static void mt_ase_fp_affinity(void)
  753. {
  754. #ifdef CONFIG_MIPS_MT_FPAFF
  755. if (mt_fpemul_threshold > 0 &&
  756. ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
  757. /*
  758. * If there's no FPU present, or if the application has already
  759. * restricted the allowed set to exclude any CPUs with FPUs,
  760. * we'll skip the procedure.
  761. */
  762. if (cpumask_intersects(&current->cpus_mask, &mt_fpu_cpumask)) {
  763. cpumask_t tmask;
  764. current->thread.user_cpus_allowed
  765. = current->cpus_mask;
  766. cpumask_and(&tmask, &current->cpus_mask,
  767. &mt_fpu_cpumask);
  768. set_cpus_allowed_ptr(current, &tmask);
  769. set_thread_flag(TIF_FPUBOUND);
  770. }
  771. }
  772. #endif /* CONFIG_MIPS_MT_FPAFF */
  773. }
  774. #else /* !CONFIG_MIPS_FP_SUPPORT */
  775. static int simulate_fp(struct pt_regs *regs, unsigned int opcode,
  776. unsigned long old_epc, unsigned long old_ra)
  777. {
  778. return -1;
  779. }
  780. #endif /* !CONFIG_MIPS_FP_SUPPORT */
  781. void do_trap_or_bp(struct pt_regs *regs, unsigned int code, int si_code,
  782. const char *str)
  783. {
  784. char b[40];
  785. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  786. if (kgdb_ll_trap(DIE_TRAP, str, regs, code, current->thread.trap_nr,
  787. SIGTRAP) == NOTIFY_STOP)
  788. return;
  789. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  790. if (notify_die(DIE_TRAP, str, regs, code, current->thread.trap_nr,
  791. SIGTRAP) == NOTIFY_STOP)
  792. return;
  793. /*
  794. * A short test says that IRIX 5.3 sends SIGTRAP for all trap
  795. * insns, even for trap and break codes that indicate arithmetic
  796. * failures. Weird ...
  797. * But should we continue the brokenness??? --macro
  798. */
  799. switch (code) {
  800. case BRK_OVERFLOW:
  801. case BRK_DIVZERO:
  802. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  803. die_if_kernel(b, regs);
  804. force_sig_fault(SIGFPE,
  805. code == BRK_DIVZERO ? FPE_INTDIV : FPE_INTOVF,
  806. (void __user *) regs->cp0_epc);
  807. break;
  808. case BRK_BUG:
  809. die_if_kernel("Kernel bug detected", regs);
  810. force_sig(SIGTRAP);
  811. break;
  812. case BRK_MEMU:
  813. /*
  814. * This breakpoint code is used by the FPU emulator to retake
  815. * control of the CPU after executing the instruction from the
  816. * delay slot of an emulated branch.
  817. *
  818. * Terminate if exception was recognized as a delay slot return
  819. * otherwise handle as normal.
  820. */
  821. if (do_dsemulret(regs))
  822. return;
  823. die_if_kernel("Math emu break/trap", regs);
  824. force_sig(SIGTRAP);
  825. break;
  826. default:
  827. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  828. die_if_kernel(b, regs);
  829. if (si_code) {
  830. force_sig_fault(SIGTRAP, si_code, NULL);
  831. } else {
  832. force_sig(SIGTRAP);
  833. }
  834. }
  835. }
  836. asmlinkage void do_bp(struct pt_regs *regs)
  837. {
  838. unsigned long epc = msk_isa16_mode(exception_epc(regs));
  839. unsigned int opcode, bcode;
  840. enum ctx_state prev_state;
  841. mm_segment_t seg;
  842. seg = get_fs();
  843. if (!user_mode(regs))
  844. set_fs(KERNEL_DS);
  845. prev_state = exception_enter();
  846. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  847. if (get_isa16_mode(regs->cp0_epc)) {
  848. u16 instr[2];
  849. if (__get_user(instr[0], (u16 __user *)epc))
  850. goto out_sigsegv;
  851. if (!cpu_has_mmips) {
  852. /* MIPS16e mode */
  853. bcode = (instr[0] >> 5) & 0x3f;
  854. } else if (mm_insn_16bit(instr[0])) {
  855. /* 16-bit microMIPS BREAK */
  856. bcode = instr[0] & 0xf;
  857. } else {
  858. /* 32-bit microMIPS BREAK */
  859. if (__get_user(instr[1], (u16 __user *)(epc + 2)))
  860. goto out_sigsegv;
  861. opcode = (instr[0] << 16) | instr[1];
  862. bcode = (opcode >> 6) & ((1 << 20) - 1);
  863. }
  864. } else {
  865. if (__get_user(opcode, (unsigned int __user *)epc))
  866. goto out_sigsegv;
  867. bcode = (opcode >> 6) & ((1 << 20) - 1);
  868. }
  869. /*
  870. * There is the ancient bug in the MIPS assemblers that the break
  871. * code starts left to bit 16 instead to bit 6 in the opcode.
  872. * Gas is bug-compatible, but not always, grrr...
  873. * We handle both cases with a simple heuristics. --macro
  874. */
  875. if (bcode >= (1 << 10))
  876. bcode = ((bcode & ((1 << 10) - 1)) << 10) | (bcode >> 10);
  877. /*
  878. * notify the kprobe handlers, if instruction is likely to
  879. * pertain to them.
  880. */
  881. switch (bcode) {
  882. case BRK_UPROBE:
  883. if (notify_die(DIE_UPROBE, "uprobe", regs, bcode,
  884. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  885. goto out;
  886. else
  887. break;
  888. case BRK_UPROBE_XOL:
  889. if (notify_die(DIE_UPROBE_XOL, "uprobe_xol", regs, bcode,
  890. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  891. goto out;
  892. else
  893. break;
  894. case BRK_KPROBE_BP:
  895. if (notify_die(DIE_BREAK, "debug", regs, bcode,
  896. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  897. goto out;
  898. else
  899. break;
  900. case BRK_KPROBE_SSTEPBP:
  901. if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode,
  902. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  903. goto out;
  904. else
  905. break;
  906. default:
  907. break;
  908. }
  909. do_trap_or_bp(regs, bcode, TRAP_BRKPT, "Break");
  910. out:
  911. set_fs(seg);
  912. exception_exit(prev_state);
  913. return;
  914. out_sigsegv:
  915. force_sig(SIGSEGV);
  916. goto out;
  917. }
  918. asmlinkage void do_tr(struct pt_regs *regs)
  919. {
  920. u32 opcode, tcode = 0;
  921. enum ctx_state prev_state;
  922. u16 instr[2];
  923. mm_segment_t seg;
  924. unsigned long epc = msk_isa16_mode(exception_epc(regs));
  925. seg = get_fs();
  926. if (!user_mode(regs))
  927. set_fs(KERNEL_DS);
  928. prev_state = exception_enter();
  929. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  930. if (get_isa16_mode(regs->cp0_epc)) {
  931. if (__get_user(instr[0], (u16 __user *)(epc + 0)) ||
  932. __get_user(instr[1], (u16 __user *)(epc + 2)))
  933. goto out_sigsegv;
  934. opcode = (instr[0] << 16) | instr[1];
  935. /* Immediate versions don't provide a code. */
  936. if (!(opcode & OPCODE))
  937. tcode = (opcode >> 12) & ((1 << 4) - 1);
  938. } else {
  939. if (__get_user(opcode, (u32 __user *)epc))
  940. goto out_sigsegv;
  941. /* Immediate versions don't provide a code. */
  942. if (!(opcode & OPCODE))
  943. tcode = (opcode >> 6) & ((1 << 10) - 1);
  944. }
  945. do_trap_or_bp(regs, tcode, 0, "Trap");
  946. out:
  947. set_fs(seg);
  948. exception_exit(prev_state);
  949. return;
  950. out_sigsegv:
  951. force_sig(SIGSEGV);
  952. goto out;
  953. }
  954. asmlinkage void do_ri(struct pt_regs *regs)
  955. {
  956. unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
  957. unsigned long old_epc = regs->cp0_epc;
  958. unsigned long old31 = regs->regs[31];
  959. enum ctx_state prev_state;
  960. unsigned int opcode = 0;
  961. int status = -1;
  962. /*
  963. * Avoid any kernel code. Just emulate the R2 instruction
  964. * as quickly as possible.
  965. */
  966. if (mipsr2_emulation && cpu_has_mips_r6 &&
  967. likely(user_mode(regs)) &&
  968. likely(get_user(opcode, epc) >= 0)) {
  969. unsigned long fcr31 = 0;
  970. status = mipsr2_decoder(regs, opcode, &fcr31);
  971. switch (status) {
  972. case 0:
  973. case SIGEMT:
  974. return;
  975. case SIGILL:
  976. goto no_r2_instr;
  977. default:
  978. process_fpemu_return(status,
  979. &current->thread.cp0_baduaddr,
  980. fcr31);
  981. return;
  982. }
  983. }
  984. no_r2_instr:
  985. prev_state = exception_enter();
  986. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  987. if (notify_die(DIE_RI, "RI Fault", regs, 0, current->thread.trap_nr,
  988. SIGILL) == NOTIFY_STOP)
  989. goto out;
  990. die_if_kernel("Reserved instruction in kernel code", regs);
  991. if (unlikely(compute_return_epc(regs) < 0))
  992. goto out;
  993. if (!get_isa16_mode(regs->cp0_epc)) {
  994. if (unlikely(get_user(opcode, epc) < 0))
  995. status = SIGSEGV;
  996. if (!cpu_has_llsc && status < 0)
  997. status = simulate_llsc(regs, opcode);
  998. if (status < 0)
  999. status = simulate_rdhwr_normal(regs, opcode);
  1000. if (status < 0)
  1001. status = simulate_sync(regs, opcode);
  1002. if (status < 0)
  1003. status = simulate_fp(regs, opcode, old_epc, old31);
  1004. } else if (cpu_has_mmips) {
  1005. unsigned short mmop[2] = { 0 };
  1006. if (unlikely(get_user(mmop[0], (u16 __user *)epc + 0) < 0))
  1007. status = SIGSEGV;
  1008. if (unlikely(get_user(mmop[1], (u16 __user *)epc + 1) < 0))
  1009. status = SIGSEGV;
  1010. opcode = mmop[0];
  1011. opcode = (opcode << 16) | mmop[1];
  1012. if (status < 0)
  1013. status = simulate_rdhwr_mm(regs, opcode);
  1014. }
  1015. if (status < 0)
  1016. status = SIGILL;
  1017. if (unlikely(status > 0)) {
  1018. regs->cp0_epc = old_epc; /* Undo skip-over. */
  1019. regs->regs[31] = old31;
  1020. force_sig(status);
  1021. }
  1022. out:
  1023. exception_exit(prev_state);
  1024. }
  1025. /*
  1026. * No lock; only written during early bootup by CPU 0.
  1027. */
  1028. static RAW_NOTIFIER_HEAD(cu2_chain);
  1029. int __ref register_cu2_notifier(struct notifier_block *nb)
  1030. {
  1031. return raw_notifier_chain_register(&cu2_chain, nb);
  1032. }
  1033. int cu2_notifier_call_chain(unsigned long val, void *v)
  1034. {
  1035. return raw_notifier_call_chain(&cu2_chain, val, v);
  1036. }
  1037. static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
  1038. void *data)
  1039. {
  1040. struct pt_regs *regs = data;
  1041. die_if_kernel("COP2: Unhandled kernel unaligned access or invalid "
  1042. "instruction", regs);
  1043. force_sig(SIGILL);
  1044. return NOTIFY_OK;
  1045. }
  1046. #ifdef CONFIG_MIPS_FP_SUPPORT
  1047. static int enable_restore_fp_context(int msa)
  1048. {
  1049. int err, was_fpu_owner, prior_msa;
  1050. bool first_fp;
  1051. /* Initialize context if it hasn't been used already */
  1052. first_fp = init_fp_ctx(current);
  1053. if (first_fp) {
  1054. preempt_disable();
  1055. err = own_fpu_inatomic(1);
  1056. if (msa && !err) {
  1057. enable_msa();
  1058. set_thread_flag(TIF_USEDMSA);
  1059. set_thread_flag(TIF_MSA_CTX_LIVE);
  1060. }
  1061. preempt_enable();
  1062. return err;
  1063. }
  1064. /*
  1065. * This task has formerly used the FP context.
  1066. *
  1067. * If this thread has no live MSA vector context then we can simply
  1068. * restore the scalar FP context. If it has live MSA vector context
  1069. * (that is, it has or may have used MSA since last performing a
  1070. * function call) then we'll need to restore the vector context. This
  1071. * applies even if we're currently only executing a scalar FP
  1072. * instruction. This is because if we were to later execute an MSA
  1073. * instruction then we'd either have to:
  1074. *
  1075. * - Restore the vector context & clobber any registers modified by
  1076. * scalar FP instructions between now & then.
  1077. *
  1078. * or
  1079. *
  1080. * - Not restore the vector context & lose the most significant bits
  1081. * of all vector registers.
  1082. *
  1083. * Neither of those options is acceptable. We cannot restore the least
  1084. * significant bits of the registers now & only restore the most
  1085. * significant bits later because the most significant bits of any
  1086. * vector registers whose aliased FP register is modified now will have
  1087. * been zeroed. We'd have no way to know that when restoring the vector
  1088. * context & thus may load an outdated value for the most significant
  1089. * bits of a vector register.
  1090. */
  1091. if (!msa && !thread_msa_context_live())
  1092. return own_fpu(1);
  1093. /*
  1094. * This task is using or has previously used MSA. Thus we require
  1095. * that Status.FR == 1.
  1096. */
  1097. preempt_disable();
  1098. was_fpu_owner = is_fpu_owner();
  1099. err = own_fpu_inatomic(0);
  1100. if (err)
  1101. goto out;
  1102. enable_msa();
  1103. write_msa_csr(current->thread.fpu.msacsr);
  1104. set_thread_flag(TIF_USEDMSA);
  1105. /*
  1106. * If this is the first time that the task is using MSA and it has
  1107. * previously used scalar FP in this time slice then we already nave
  1108. * FP context which we shouldn't clobber. We do however need to clear
  1109. * the upper 64b of each vector register so that this task has no
  1110. * opportunity to see data left behind by another.
  1111. */
  1112. prior_msa = test_and_set_thread_flag(TIF_MSA_CTX_LIVE);
  1113. if (!prior_msa && was_fpu_owner) {
  1114. init_msa_upper();
  1115. goto out;
  1116. }
  1117. if (!prior_msa) {
  1118. /*
  1119. * Restore the least significant 64b of each vector register
  1120. * from the existing scalar FP context.
  1121. */
  1122. _restore_fp(current);
  1123. /*
  1124. * The task has not formerly used MSA, so clear the upper 64b
  1125. * of each vector register such that it cannot see data left
  1126. * behind by another task.
  1127. */
  1128. init_msa_upper();
  1129. } else {
  1130. /* We need to restore the vector context. */
  1131. restore_msa(current);
  1132. /* Restore the scalar FP control & status register */
  1133. if (!was_fpu_owner)
  1134. write_32bit_cp1_register(CP1_STATUS,
  1135. current->thread.fpu.fcr31);
  1136. }
  1137. out:
  1138. preempt_enable();
  1139. return 0;
  1140. }
  1141. #else /* !CONFIG_MIPS_FP_SUPPORT */
  1142. static int enable_restore_fp_context(int msa)
  1143. {
  1144. return SIGILL;
  1145. }
  1146. #endif /* CONFIG_MIPS_FP_SUPPORT */
  1147. asmlinkage void do_cpu(struct pt_regs *regs)
  1148. {
  1149. enum ctx_state prev_state;
  1150. unsigned int __user *epc;
  1151. unsigned long old_epc, old31;
  1152. unsigned int opcode;
  1153. unsigned int cpid;
  1154. int status;
  1155. prev_state = exception_enter();
  1156. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  1157. if (cpid != 2)
  1158. die_if_kernel("do_cpu invoked from kernel context!", regs);
  1159. switch (cpid) {
  1160. case 0:
  1161. epc = (unsigned int __user *)exception_epc(regs);
  1162. old_epc = regs->cp0_epc;
  1163. old31 = regs->regs[31];
  1164. opcode = 0;
  1165. status = -1;
  1166. if (unlikely(compute_return_epc(regs) < 0))
  1167. break;
  1168. if (!get_isa16_mode(regs->cp0_epc)) {
  1169. if (unlikely(get_user(opcode, epc) < 0))
  1170. status = SIGSEGV;
  1171. if (!cpu_has_llsc && status < 0)
  1172. status = simulate_llsc(regs, opcode);
  1173. }
  1174. if (status < 0)
  1175. status = SIGILL;
  1176. if (unlikely(status > 0)) {
  1177. regs->cp0_epc = old_epc; /* Undo skip-over. */
  1178. regs->regs[31] = old31;
  1179. force_sig(status);
  1180. }
  1181. break;
  1182. #ifdef CONFIG_MIPS_FP_SUPPORT
  1183. case 3:
  1184. /*
  1185. * The COP3 opcode space and consequently the CP0.Status.CU3
  1186. * bit and the CP0.Cause.CE=3 encoding have been removed as
  1187. * of the MIPS III ISA. From the MIPS IV and MIPS32r2 ISAs
  1188. * up the space has been reused for COP1X instructions, that
  1189. * are enabled by the CP0.Status.CU1 bit and consequently
  1190. * use the CP0.Cause.CE=1 encoding for Coprocessor Unusable
  1191. * exceptions. Some FPU-less processors that implement one
  1192. * of these ISAs however use this code erroneously for COP1X
  1193. * instructions. Therefore we redirect this trap to the FP
  1194. * emulator too.
  1195. */
  1196. if (raw_cpu_has_fpu || !cpu_has_mips_4_5_64_r2_r6) {
  1197. force_sig(SIGILL);
  1198. break;
  1199. }
  1200. /* Fall through. */
  1201. case 1: {
  1202. void __user *fault_addr;
  1203. unsigned long fcr31;
  1204. int err, sig;
  1205. err = enable_restore_fp_context(0);
  1206. if (raw_cpu_has_fpu && !err)
  1207. break;
  1208. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 0,
  1209. &fault_addr);
  1210. /*
  1211. * We can't allow the emulated instruction to leave
  1212. * any enabled Cause bits set in $fcr31.
  1213. */
  1214. fcr31 = mask_fcr31_x(current->thread.fpu.fcr31);
  1215. current->thread.fpu.fcr31 &= ~fcr31;
  1216. /* Send a signal if required. */
  1217. if (!process_fpemu_return(sig, fault_addr, fcr31) && !err)
  1218. mt_ase_fp_affinity();
  1219. break;
  1220. }
  1221. #else /* CONFIG_MIPS_FP_SUPPORT */
  1222. case 1:
  1223. case 3:
  1224. force_sig(SIGILL);
  1225. break;
  1226. #endif /* CONFIG_MIPS_FP_SUPPORT */
  1227. case 2:
  1228. raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
  1229. break;
  1230. }
  1231. exception_exit(prev_state);
  1232. }
  1233. asmlinkage void do_msa_fpe(struct pt_regs *regs, unsigned int msacsr)
  1234. {
  1235. enum ctx_state prev_state;
  1236. prev_state = exception_enter();
  1237. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  1238. if (notify_die(DIE_MSAFP, "MSA FP exception", regs, 0,
  1239. current->thread.trap_nr, SIGFPE) == NOTIFY_STOP)
  1240. goto out;
  1241. /* Clear MSACSR.Cause before enabling interrupts */
  1242. write_msa_csr(msacsr & ~MSA_CSR_CAUSEF);
  1243. local_irq_enable();
  1244. die_if_kernel("do_msa_fpe invoked from kernel context!", regs);
  1245. force_sig(SIGFPE);
  1246. out:
  1247. exception_exit(prev_state);
  1248. }
  1249. asmlinkage void do_msa(struct pt_regs *regs)
  1250. {
  1251. enum ctx_state prev_state;
  1252. int err;
  1253. prev_state = exception_enter();
  1254. if (!cpu_has_msa || test_thread_flag(TIF_32BIT_FPREGS)) {
  1255. force_sig(SIGILL);
  1256. goto out;
  1257. }
  1258. die_if_kernel("do_msa invoked from kernel context!", regs);
  1259. err = enable_restore_fp_context(1);
  1260. if (err)
  1261. force_sig(SIGILL);
  1262. out:
  1263. exception_exit(prev_state);
  1264. }
  1265. asmlinkage void do_mdmx(struct pt_regs *regs)
  1266. {
  1267. enum ctx_state prev_state;
  1268. prev_state = exception_enter();
  1269. force_sig(SIGILL);
  1270. exception_exit(prev_state);
  1271. }
  1272. /*
  1273. * Called with interrupts disabled.
  1274. */
  1275. asmlinkage void do_watch(struct pt_regs *regs)
  1276. {
  1277. enum ctx_state prev_state;
  1278. prev_state = exception_enter();
  1279. /*
  1280. * Clear WP (bit 22) bit of cause register so we don't loop
  1281. * forever.
  1282. */
  1283. clear_c0_cause(CAUSEF_WP);
  1284. /*
  1285. * If the current thread has the watch registers loaded, save
  1286. * their values and send SIGTRAP. Otherwise another thread
  1287. * left the registers set, clear them and continue.
  1288. */
  1289. if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
  1290. mips_read_watch_registers();
  1291. local_irq_enable();
  1292. force_sig_fault(SIGTRAP, TRAP_HWBKPT, NULL);
  1293. } else {
  1294. mips_clear_watch_registers();
  1295. local_irq_enable();
  1296. }
  1297. exception_exit(prev_state);
  1298. }
  1299. asmlinkage void do_mcheck(struct pt_regs *regs)
  1300. {
  1301. int multi_match = regs->cp0_status & ST0_TS;
  1302. enum ctx_state prev_state;
  1303. mm_segment_t old_fs = get_fs();
  1304. prev_state = exception_enter();
  1305. show_regs(regs);
  1306. if (multi_match) {
  1307. dump_tlb_regs();
  1308. pr_info("\n");
  1309. dump_tlb_all();
  1310. }
  1311. if (!user_mode(regs))
  1312. set_fs(KERNEL_DS);
  1313. show_code((unsigned int __user *) regs->cp0_epc);
  1314. set_fs(old_fs);
  1315. /*
  1316. * Some chips may have other causes of machine check (e.g. SB1
  1317. * graduation timer)
  1318. */
  1319. panic("Caught Machine Check exception - %scaused by multiple "
  1320. "matching entries in the TLB.",
  1321. (multi_match) ? "" : "not ");
  1322. }
  1323. asmlinkage void do_mt(struct pt_regs *regs)
  1324. {
  1325. int subcode;
  1326. subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
  1327. >> VPECONTROL_EXCPT_SHIFT;
  1328. switch (subcode) {
  1329. case 0:
  1330. printk(KERN_DEBUG "Thread Underflow\n");
  1331. break;
  1332. case 1:
  1333. printk(KERN_DEBUG "Thread Overflow\n");
  1334. break;
  1335. case 2:
  1336. printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
  1337. break;
  1338. case 3:
  1339. printk(KERN_DEBUG "Gating Storage Exception\n");
  1340. break;
  1341. case 4:
  1342. printk(KERN_DEBUG "YIELD Scheduler Exception\n");
  1343. break;
  1344. case 5:
  1345. printk(KERN_DEBUG "Gating Storage Scheduler Exception\n");
  1346. break;
  1347. default:
  1348. printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
  1349. subcode);
  1350. break;
  1351. }
  1352. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  1353. force_sig(SIGILL);
  1354. }
  1355. asmlinkage void do_dsp(struct pt_regs *regs)
  1356. {
  1357. if (cpu_has_dsp)
  1358. panic("Unexpected DSP exception");
  1359. force_sig(SIGILL);
  1360. }
  1361. asmlinkage void do_reserved(struct pt_regs *regs)
  1362. {
  1363. /*
  1364. * Game over - no way to handle this if it ever occurs. Most probably
  1365. * caused by a new unknown cpu type or after another deadly
  1366. * hard/software error.
  1367. */
  1368. show_regs(regs);
  1369. panic("Caught reserved exception %ld - should not happen.",
  1370. (regs->cp0_cause & 0x7f) >> 2);
  1371. }
  1372. static int __initdata l1parity = 1;
  1373. static int __init nol1parity(char *s)
  1374. {
  1375. l1parity = 0;
  1376. return 1;
  1377. }
  1378. __setup("nol1par", nol1parity);
  1379. static int __initdata l2parity = 1;
  1380. static int __init nol2parity(char *s)
  1381. {
  1382. l2parity = 0;
  1383. return 1;
  1384. }
  1385. __setup("nol2par", nol2parity);
  1386. /*
  1387. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  1388. * it different ways.
  1389. */
  1390. static inline void parity_protection_init(void)
  1391. {
  1392. #define ERRCTL_PE 0x80000000
  1393. #define ERRCTL_L2P 0x00800000
  1394. if (mips_cm_revision() >= CM_REV_CM3) {
  1395. ulong gcr_ectl, cp0_ectl;
  1396. /*
  1397. * With CM3 systems we need to ensure that the L1 & L2
  1398. * parity enables are set to the same value, since this
  1399. * is presumed by the hardware engineers.
  1400. *
  1401. * If the user disabled either of L1 or L2 ECC checking,
  1402. * disable both.
  1403. */
  1404. l1parity &= l2parity;
  1405. l2parity &= l1parity;
  1406. /* Probe L1 ECC support */
  1407. cp0_ectl = read_c0_ecc();
  1408. write_c0_ecc(cp0_ectl | ERRCTL_PE);
  1409. back_to_back_c0_hazard();
  1410. cp0_ectl = read_c0_ecc();
  1411. /* Probe L2 ECC support */
  1412. gcr_ectl = read_gcr_err_control();
  1413. if (!(gcr_ectl & CM_GCR_ERR_CONTROL_L2_ECC_SUPPORT) ||
  1414. !(cp0_ectl & ERRCTL_PE)) {
  1415. /*
  1416. * One of L1 or L2 ECC checking isn't supported,
  1417. * so we cannot enable either.
  1418. */
  1419. l1parity = l2parity = 0;
  1420. }
  1421. /* Configure L1 ECC checking */
  1422. if (l1parity)
  1423. cp0_ectl |= ERRCTL_PE;
  1424. else
  1425. cp0_ectl &= ~ERRCTL_PE;
  1426. write_c0_ecc(cp0_ectl);
  1427. back_to_back_c0_hazard();
  1428. WARN_ON(!!(read_c0_ecc() & ERRCTL_PE) != l1parity);
  1429. /* Configure L2 ECC checking */
  1430. if (l2parity)
  1431. gcr_ectl |= CM_GCR_ERR_CONTROL_L2_ECC_EN;
  1432. else
  1433. gcr_ectl &= ~CM_GCR_ERR_CONTROL_L2_ECC_EN;
  1434. write_gcr_err_control(gcr_ectl);
  1435. gcr_ectl = read_gcr_err_control();
  1436. gcr_ectl &= CM_GCR_ERR_CONTROL_L2_ECC_EN;
  1437. WARN_ON(!!gcr_ectl != l2parity);
  1438. pr_info("Cache parity protection %sabled\n",
  1439. l1parity ? "en" : "dis");
  1440. return;
  1441. }
  1442. switch (current_cpu_type()) {
  1443. case CPU_24K:
  1444. case CPU_34K:
  1445. case CPU_74K:
  1446. case CPU_1004K:
  1447. case CPU_1074K:
  1448. case CPU_INTERAPTIV:
  1449. case CPU_PROAPTIV:
  1450. case CPU_P5600:
  1451. case CPU_QEMU_GENERIC:
  1452. case CPU_P6600:
  1453. {
  1454. unsigned long errctl;
  1455. unsigned int l1parity_present, l2parity_present;
  1456. errctl = read_c0_ecc();
  1457. errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
  1458. /* probe L1 parity support */
  1459. write_c0_ecc(errctl | ERRCTL_PE);
  1460. back_to_back_c0_hazard();
  1461. l1parity_present = (read_c0_ecc() & ERRCTL_PE);
  1462. /* probe L2 parity support */
  1463. write_c0_ecc(errctl|ERRCTL_L2P);
  1464. back_to_back_c0_hazard();
  1465. l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
  1466. if (l1parity_present && l2parity_present) {
  1467. if (l1parity)
  1468. errctl |= ERRCTL_PE;
  1469. if (l1parity ^ l2parity)
  1470. errctl |= ERRCTL_L2P;
  1471. } else if (l1parity_present) {
  1472. if (l1parity)
  1473. errctl |= ERRCTL_PE;
  1474. } else if (l2parity_present) {
  1475. if (l2parity)
  1476. errctl |= ERRCTL_L2P;
  1477. } else {
  1478. /* No parity available */
  1479. }
  1480. printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
  1481. write_c0_ecc(errctl);
  1482. back_to_back_c0_hazard();
  1483. errctl = read_c0_ecc();
  1484. printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
  1485. if (l1parity_present)
  1486. printk(KERN_INFO "Cache parity protection %sabled\n",
  1487. (errctl & ERRCTL_PE) ? "en" : "dis");
  1488. if (l2parity_present) {
  1489. if (l1parity_present && l1parity)
  1490. errctl ^= ERRCTL_L2P;
  1491. printk(KERN_INFO "L2 cache parity protection %sabled\n",
  1492. (errctl & ERRCTL_L2P) ? "en" : "dis");
  1493. }
  1494. }
  1495. break;
  1496. case CPU_5KC:
  1497. case CPU_5KE:
  1498. case CPU_LOONGSON32:
  1499. write_c0_ecc(0x80000000);
  1500. back_to_back_c0_hazard();
  1501. /* Set the PE bit (bit 31) in the c0_errctl register. */
  1502. printk(KERN_INFO "Cache parity protection %sabled\n",
  1503. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  1504. break;
  1505. case CPU_20KC:
  1506. case CPU_25KF:
  1507. /* Clear the DE bit (bit 16) in the c0_status register. */
  1508. printk(KERN_INFO "Enable cache parity protection for "
  1509. "MIPS 20KC/25KF CPUs.\n");
  1510. clear_c0_status(ST0_DE);
  1511. break;
  1512. default:
  1513. break;
  1514. }
  1515. }
  1516. asmlinkage void cache_parity_error(void)
  1517. {
  1518. const int field = 2 * sizeof(unsigned long);
  1519. unsigned int reg_val;
  1520. /* For the moment, report the problem and hang. */
  1521. printk("Cache error exception:\n");
  1522. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1523. reg_val = read_c0_cacheerr();
  1524. printk("c0_cacheerr == %08x\n", reg_val);
  1525. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1526. reg_val & (1<<30) ? "secondary" : "primary",
  1527. reg_val & (1<<31) ? "data" : "insn");
  1528. if ((cpu_has_mips_r2_r6) &&
  1529. ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS)) {
  1530. pr_err("Error bits: %s%s%s%s%s%s%s%s\n",
  1531. reg_val & (1<<29) ? "ED " : "",
  1532. reg_val & (1<<28) ? "ET " : "",
  1533. reg_val & (1<<27) ? "ES " : "",
  1534. reg_val & (1<<26) ? "EE " : "",
  1535. reg_val & (1<<25) ? "EB " : "",
  1536. reg_val & (1<<24) ? "EI " : "",
  1537. reg_val & (1<<23) ? "E1 " : "",
  1538. reg_val & (1<<22) ? "E0 " : "");
  1539. } else {
  1540. pr_err("Error bits: %s%s%s%s%s%s%s\n",
  1541. reg_val & (1<<29) ? "ED " : "",
  1542. reg_val & (1<<28) ? "ET " : "",
  1543. reg_val & (1<<26) ? "EE " : "",
  1544. reg_val & (1<<25) ? "EB " : "",
  1545. reg_val & (1<<24) ? "EI " : "",
  1546. reg_val & (1<<23) ? "E1 " : "",
  1547. reg_val & (1<<22) ? "E0 " : "");
  1548. }
  1549. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  1550. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
  1551. if (reg_val & (1<<22))
  1552. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  1553. if (reg_val & (1<<23))
  1554. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  1555. #endif
  1556. panic("Can't handle the cache error!");
  1557. }
  1558. asmlinkage void do_ftlb(void)
  1559. {
  1560. const int field = 2 * sizeof(unsigned long);
  1561. unsigned int reg_val;
  1562. /* For the moment, report the problem and hang. */
  1563. if ((cpu_has_mips_r2_r6) &&
  1564. (((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS) ||
  1565. ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_LOONGSON))) {
  1566. pr_err("FTLB error exception, cp0_ecc=0x%08x:\n",
  1567. read_c0_ecc());
  1568. pr_err("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1569. reg_val = read_c0_cacheerr();
  1570. pr_err("c0_cacheerr == %08x\n", reg_val);
  1571. if ((reg_val & 0xc0000000) == 0xc0000000) {
  1572. pr_err("Decoded c0_cacheerr: FTLB parity error\n");
  1573. } else {
  1574. pr_err("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1575. reg_val & (1<<30) ? "secondary" : "primary",
  1576. reg_val & (1<<31) ? "data" : "insn");
  1577. }
  1578. } else {
  1579. pr_err("FTLB error exception\n");
  1580. }
  1581. /* Just print the cacheerr bits for now */
  1582. cache_parity_error();
  1583. }
  1584. /*
  1585. * SDBBP EJTAG debug exception handler.
  1586. * We skip the instruction and return to the next instruction.
  1587. */
  1588. void ejtag_exception_handler(struct pt_regs *regs)
  1589. {
  1590. const int field = 2 * sizeof(unsigned long);
  1591. unsigned long depc, old_epc, old_ra;
  1592. unsigned int debug;
  1593. printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  1594. depc = read_c0_depc();
  1595. debug = read_c0_debug();
  1596. printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  1597. if (debug & 0x80000000) {
  1598. /*
  1599. * In branch delay slot.
  1600. * We cheat a little bit here and use EPC to calculate the
  1601. * debug return address (DEPC). EPC is restored after the
  1602. * calculation.
  1603. */
  1604. old_epc = regs->cp0_epc;
  1605. old_ra = regs->regs[31];
  1606. regs->cp0_epc = depc;
  1607. compute_return_epc(regs);
  1608. depc = regs->cp0_epc;
  1609. regs->cp0_epc = old_epc;
  1610. regs->regs[31] = old_ra;
  1611. } else
  1612. depc += 4;
  1613. write_c0_depc(depc);
  1614. #if 0
  1615. printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
  1616. write_c0_debug(debug | 0x100);
  1617. #endif
  1618. }
  1619. /*
  1620. * NMI exception handler.
  1621. * No lock; only written during early bootup by CPU 0.
  1622. */
  1623. static RAW_NOTIFIER_HEAD(nmi_chain);
  1624. int register_nmi_notifier(struct notifier_block *nb)
  1625. {
  1626. return raw_notifier_chain_register(&nmi_chain, nb);
  1627. }
  1628. void __noreturn nmi_exception_handler(struct pt_regs *regs)
  1629. {
  1630. char str[100];
  1631. nmi_enter();
  1632. raw_notifier_call_chain(&nmi_chain, 0, regs);
  1633. bust_spinlocks(1);
  1634. snprintf(str, 100, "CPU%d NMI taken, CP0_EPC=%lx\n",
  1635. smp_processor_id(), regs->cp0_epc);
  1636. regs->cp0_epc = read_c0_errorepc();
  1637. die(str, regs);
  1638. nmi_exit();
  1639. }
  1640. #define VECTORSPACING 0x100 /* for EI/VI mode */
  1641. unsigned long ebase;
  1642. EXPORT_SYMBOL_GPL(ebase);
  1643. unsigned long exception_handlers[32];
  1644. unsigned long vi_handlers[64];
  1645. void __init *set_except_vector(int n, void *addr)
  1646. {
  1647. unsigned long handler = (unsigned long) addr;
  1648. unsigned long old_handler;
  1649. #ifdef CONFIG_CPU_MICROMIPS
  1650. /*
  1651. * Only the TLB handlers are cache aligned with an even
  1652. * address. All other handlers are on an odd address and
  1653. * require no modification. Otherwise, MIPS32 mode will
  1654. * be entered when handling any TLB exceptions. That
  1655. * would be bad...since we must stay in microMIPS mode.
  1656. */
  1657. if (!(handler & 0x1))
  1658. handler |= 1;
  1659. #endif
  1660. old_handler = xchg(&exception_handlers[n], handler);
  1661. if (n == 0 && cpu_has_divec) {
  1662. #ifdef CONFIG_CPU_MICROMIPS
  1663. unsigned long jump_mask = ~((1 << 27) - 1);
  1664. #else
  1665. unsigned long jump_mask = ~((1 << 28) - 1);
  1666. #endif
  1667. u32 *buf = (u32 *)(ebase + 0x200);
  1668. unsigned int k0 = 26;
  1669. if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
  1670. uasm_i_j(&buf, handler & ~jump_mask);
  1671. uasm_i_nop(&buf);
  1672. } else {
  1673. UASM_i_LA(&buf, k0, handler);
  1674. uasm_i_jr(&buf, k0);
  1675. uasm_i_nop(&buf);
  1676. }
  1677. local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
  1678. }
  1679. return (void *)old_handler;
  1680. }
  1681. static void do_default_vi(void)
  1682. {
  1683. show_regs(get_irq_regs());
  1684. panic("Caught unexpected vectored interrupt.");
  1685. }
  1686. static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
  1687. {
  1688. unsigned long handler;
  1689. unsigned long old_handler = vi_handlers[n];
  1690. int srssets = current_cpu_data.srsets;
  1691. u16 *h;
  1692. unsigned char *b;
  1693. BUG_ON(!cpu_has_veic && !cpu_has_vint);
  1694. if (addr == NULL) {
  1695. handler = (unsigned long) do_default_vi;
  1696. srs = 0;
  1697. } else
  1698. handler = (unsigned long) addr;
  1699. vi_handlers[n] = handler;
  1700. b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
  1701. if (srs >= srssets)
  1702. panic("Shadow register set %d not supported", srs);
  1703. if (cpu_has_veic) {
  1704. if (board_bind_eic_interrupt)
  1705. board_bind_eic_interrupt(n, srs);
  1706. } else if (cpu_has_vint) {
  1707. /* SRSMap is only defined if shadow sets are implemented */
  1708. if (srssets > 1)
  1709. change_c0_srsmap(0xf << n*4, srs << n*4);
  1710. }
  1711. if (srs == 0) {
  1712. /*
  1713. * If no shadow set is selected then use the default handler
  1714. * that does normal register saving and standard interrupt exit
  1715. */
  1716. extern char except_vec_vi, except_vec_vi_lui;
  1717. extern char except_vec_vi_ori, except_vec_vi_end;
  1718. extern char rollback_except_vec_vi;
  1719. char *vec_start = using_rollback_handler() ?
  1720. &rollback_except_vec_vi : &except_vec_vi;
  1721. #if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN)
  1722. const int lui_offset = &except_vec_vi_lui - vec_start + 2;
  1723. const int ori_offset = &except_vec_vi_ori - vec_start + 2;
  1724. #else
  1725. const int lui_offset = &except_vec_vi_lui - vec_start;
  1726. const int ori_offset = &except_vec_vi_ori - vec_start;
  1727. #endif
  1728. const int handler_len = &except_vec_vi_end - vec_start;
  1729. if (handler_len > VECTORSPACING) {
  1730. /*
  1731. * Sigh... panicing won't help as the console
  1732. * is probably not configured :(
  1733. */
  1734. panic("VECTORSPACING too small");
  1735. }
  1736. set_handler(((unsigned long)b - ebase), vec_start,
  1737. #ifdef CONFIG_CPU_MICROMIPS
  1738. (handler_len - 1));
  1739. #else
  1740. handler_len);
  1741. #endif
  1742. h = (u16 *)(b + lui_offset);
  1743. *h = (handler >> 16) & 0xffff;
  1744. h = (u16 *)(b + ori_offset);
  1745. *h = (handler & 0xffff);
  1746. local_flush_icache_range((unsigned long)b,
  1747. (unsigned long)(b+handler_len));
  1748. }
  1749. else {
  1750. /*
  1751. * In other cases jump directly to the interrupt handler. It
  1752. * is the handler's responsibility to save registers if required
  1753. * (eg hi/lo) and return from the exception using "eret".
  1754. */
  1755. u32 insn;
  1756. h = (u16 *)b;
  1757. /* j handler */
  1758. #ifdef CONFIG_CPU_MICROMIPS
  1759. insn = 0xd4000000 | (((u32)handler & 0x07ffffff) >> 1);
  1760. #else
  1761. insn = 0x08000000 | (((u32)handler & 0x0fffffff) >> 2);
  1762. #endif
  1763. h[0] = (insn >> 16) & 0xffff;
  1764. h[1] = insn & 0xffff;
  1765. h[2] = 0;
  1766. h[3] = 0;
  1767. local_flush_icache_range((unsigned long)b,
  1768. (unsigned long)(b+8));
  1769. }
  1770. return (void *)old_handler;
  1771. }
  1772. void *set_vi_handler(int n, vi_handler_t addr)
  1773. {
  1774. return set_vi_srs_handler(n, addr, 0);
  1775. }
  1776. extern void tlb_init(void);
  1777. /*
  1778. * Timer interrupt
  1779. */
  1780. int cp0_compare_irq;
  1781. EXPORT_SYMBOL_GPL(cp0_compare_irq);
  1782. int cp0_compare_irq_shift;
  1783. /*
  1784. * Performance counter IRQ or -1 if shared with timer
  1785. */
  1786. int cp0_perfcount_irq;
  1787. EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
  1788. /*
  1789. * Fast debug channel IRQ or -1 if not present
  1790. */
  1791. int cp0_fdc_irq;
  1792. EXPORT_SYMBOL_GPL(cp0_fdc_irq);
  1793. static int noulri;
  1794. static int __init ulri_disable(char *s)
  1795. {
  1796. pr_info("Disabling ulri\n");
  1797. noulri = 1;
  1798. return 1;
  1799. }
  1800. __setup("noulri", ulri_disable);
  1801. /* configure STATUS register */
  1802. static void configure_status(void)
  1803. {
  1804. /*
  1805. * Disable coprocessors and select 32-bit or 64-bit addressing
  1806. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  1807. * flag that some firmware may have left set and the TS bit (for
  1808. * IP27). Set XX for ISA IV code to work.
  1809. */
  1810. unsigned int status_set = ST0_CU0;
  1811. #ifdef CONFIG_64BIT
  1812. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  1813. #endif
  1814. if (current_cpu_data.isa_level & MIPS_CPU_ISA_IV)
  1815. status_set |= ST0_XX;
  1816. if (cpu_has_dsp)
  1817. status_set |= ST0_MX;
  1818. change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  1819. status_set);
  1820. }
  1821. unsigned int hwrena;
  1822. EXPORT_SYMBOL_GPL(hwrena);
  1823. /* configure HWRENA register */
  1824. static void configure_hwrena(void)
  1825. {
  1826. hwrena = cpu_hwrena_impl_bits;
  1827. if (cpu_has_mips_r2_r6)
  1828. hwrena |= MIPS_HWRENA_CPUNUM |
  1829. MIPS_HWRENA_SYNCISTEP |
  1830. MIPS_HWRENA_CC |
  1831. MIPS_HWRENA_CCRES;
  1832. if (!noulri && cpu_has_userlocal)
  1833. hwrena |= MIPS_HWRENA_ULR;
  1834. if (hwrena)
  1835. write_c0_hwrena(hwrena);
  1836. }
  1837. static void configure_exception_vector(void)
  1838. {
  1839. if (cpu_has_mips_r2_r6) {
  1840. unsigned long sr = set_c0_status(ST0_BEV);
  1841. /* If available, use WG to set top bits of EBASE */
  1842. if (cpu_has_ebase_wg) {
  1843. #ifdef CONFIG_64BIT
  1844. write_c0_ebase_64(ebase | MIPS_EBASE_WG);
  1845. #else
  1846. write_c0_ebase(ebase | MIPS_EBASE_WG);
  1847. #endif
  1848. }
  1849. write_c0_ebase(ebase);
  1850. write_c0_status(sr);
  1851. }
  1852. if (cpu_has_veic || cpu_has_vint) {
  1853. /* Setting vector spacing enables EI/VI mode */
  1854. change_c0_intctl(0x3e0, VECTORSPACING);
  1855. }
  1856. if (cpu_has_divec) {
  1857. if (cpu_has_mipsmt) {
  1858. unsigned int vpflags = dvpe();
  1859. set_c0_cause(CAUSEF_IV);
  1860. evpe(vpflags);
  1861. } else
  1862. set_c0_cause(CAUSEF_IV);
  1863. }
  1864. }
  1865. void per_cpu_trap_init(bool is_boot_cpu)
  1866. {
  1867. unsigned int cpu = smp_processor_id();
  1868. configure_status();
  1869. configure_hwrena();
  1870. configure_exception_vector();
  1871. /*
  1872. * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
  1873. *
  1874. * o read IntCtl.IPTI to determine the timer interrupt
  1875. * o read IntCtl.IPPCI to determine the performance counter interrupt
  1876. * o read IntCtl.IPFDC to determine the fast debug channel interrupt
  1877. */
  1878. if (cpu_has_mips_r2_r6) {
  1879. cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
  1880. cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
  1881. cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
  1882. cp0_fdc_irq = (read_c0_intctl() >> INTCTLB_IPFDC) & 7;
  1883. if (!cp0_fdc_irq)
  1884. cp0_fdc_irq = -1;
  1885. } else {
  1886. cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
  1887. cp0_compare_irq_shift = CP0_LEGACY_PERFCNT_IRQ;
  1888. cp0_perfcount_irq = -1;
  1889. cp0_fdc_irq = -1;
  1890. }
  1891. if (cpu_has_mmid)
  1892. cpu_data[cpu].asid_cache = 0;
  1893. else if (!cpu_data[cpu].asid_cache)
  1894. cpu_data[cpu].asid_cache = asid_first_version(cpu);
  1895. mmgrab(&init_mm);
  1896. current->active_mm = &init_mm;
  1897. BUG_ON(current->mm);
  1898. enter_lazy_tlb(&init_mm, current);
  1899. /* Boot CPU's cache setup in setup_arch(). */
  1900. if (!is_boot_cpu)
  1901. cpu_cache_init();
  1902. tlb_init();
  1903. TLBMISS_HANDLER_SETUP();
  1904. }
  1905. /* Install CPU exception handler */
  1906. void set_handler(unsigned long offset, void *addr, unsigned long size)
  1907. {
  1908. #ifdef CONFIG_CPU_MICROMIPS
  1909. memcpy((void *)(ebase + offset), ((unsigned char *)addr - 1), size);
  1910. #else
  1911. memcpy((void *)(ebase + offset), addr, size);
  1912. #endif
  1913. local_flush_icache_range(ebase + offset, ebase + offset + size);
  1914. }
  1915. static const char panic_null_cerr[] =
  1916. "Trying to set NULL cache error exception handler\n";
  1917. /*
  1918. * Install uncached CPU exception handler.
  1919. * This is suitable only for the cache error exception which is the only
  1920. * exception handler that is being run uncached.
  1921. */
  1922. void set_uncached_handler(unsigned long offset, void *addr,
  1923. unsigned long size)
  1924. {
  1925. unsigned long uncached_ebase = CKSEG1ADDR(ebase);
  1926. if (!addr)
  1927. panic(panic_null_cerr);
  1928. memcpy((void *)(uncached_ebase + offset), addr, size);
  1929. }
  1930. static int __initdata rdhwr_noopt;
  1931. static int __init set_rdhwr_noopt(char *str)
  1932. {
  1933. rdhwr_noopt = 1;
  1934. return 1;
  1935. }
  1936. __setup("rdhwr_noopt", set_rdhwr_noopt);
  1937. void __init trap_init(void)
  1938. {
  1939. extern char except_vec3_generic;
  1940. extern char except_vec4;
  1941. extern char except_vec3_r4000;
  1942. unsigned long i, vec_size;
  1943. phys_addr_t ebase_pa;
  1944. check_wait();
  1945. if (!cpu_has_mips_r2_r6) {
  1946. ebase = CAC_BASE;
  1947. ebase_pa = virt_to_phys((void *)ebase);
  1948. vec_size = 0x400;
  1949. memblock_reserve(ebase_pa, vec_size);
  1950. } else {
  1951. if (cpu_has_veic || cpu_has_vint)
  1952. vec_size = 0x200 + VECTORSPACING*64;
  1953. else
  1954. vec_size = PAGE_SIZE;
  1955. ebase_pa = memblock_phys_alloc(vec_size, 1 << fls(vec_size));
  1956. if (!ebase_pa)
  1957. panic("%s: Failed to allocate %lu bytes align=0x%x\n",
  1958. __func__, vec_size, 1 << fls(vec_size));
  1959. /*
  1960. * Try to ensure ebase resides in KSeg0 if possible.
  1961. *
  1962. * It shouldn't generally be in XKPhys on MIPS64 to avoid
  1963. * hitting a poorly defined exception base for Cache Errors.
  1964. * The allocation is likely to be in the low 512MB of physical,
  1965. * in which case we should be able to convert to KSeg0.
  1966. *
  1967. * EVA is special though as it allows segments to be rearranged
  1968. * and to become uncached during cache error handling.
  1969. */
  1970. if (!IS_ENABLED(CONFIG_EVA) && !WARN_ON(ebase_pa >= 0x20000000))
  1971. ebase = CKSEG0ADDR(ebase_pa);
  1972. else
  1973. ebase = (unsigned long)phys_to_virt(ebase_pa);
  1974. }
  1975. if (cpu_has_mmips) {
  1976. unsigned int config3 = read_c0_config3();
  1977. if (IS_ENABLED(CONFIG_CPU_MICROMIPS))
  1978. write_c0_config3(config3 | MIPS_CONF3_ISA_OE);
  1979. else
  1980. write_c0_config3(config3 & ~MIPS_CONF3_ISA_OE);
  1981. }
  1982. if (board_ebase_setup)
  1983. board_ebase_setup();
  1984. per_cpu_trap_init(true);
  1985. memblock_set_bottom_up(false);
  1986. /*
  1987. * Copy the generic exception handlers to their final destination.
  1988. * This will be overridden later as suitable for a particular
  1989. * configuration.
  1990. */
  1991. set_handler(0x180, &except_vec3_generic, 0x80);
  1992. /*
  1993. * Setup default vectors
  1994. */
  1995. for (i = 0; i <= 31; i++)
  1996. set_except_vector(i, handle_reserved);
  1997. /*
  1998. * Copy the EJTAG debug exception vector handler code to it's final
  1999. * destination.
  2000. */
  2001. if (cpu_has_ejtag && board_ejtag_handler_setup)
  2002. board_ejtag_handler_setup();
  2003. /*
  2004. * Only some CPUs have the watch exceptions.
  2005. */
  2006. if (cpu_has_watch)
  2007. set_except_vector(EXCCODE_WATCH, handle_watch);
  2008. /*
  2009. * Initialise interrupt handlers
  2010. */
  2011. if (cpu_has_veic || cpu_has_vint) {
  2012. int nvec = cpu_has_veic ? 64 : 8;
  2013. for (i = 0; i < nvec; i++)
  2014. set_vi_handler(i, NULL);
  2015. }
  2016. else if (cpu_has_divec)
  2017. set_handler(0x200, &except_vec4, 0x8);
  2018. /*
  2019. * Some CPUs can enable/disable for cache parity detection, but does
  2020. * it different ways.
  2021. */
  2022. parity_protection_init();
  2023. /*
  2024. * The Data Bus Errors / Instruction Bus Errors are signaled
  2025. * by external hardware. Therefore these two exceptions
  2026. * may have board specific handlers.
  2027. */
  2028. if (board_be_init)
  2029. board_be_init();
  2030. set_except_vector(EXCCODE_INT, using_rollback_handler() ?
  2031. rollback_handle_int : handle_int);
  2032. set_except_vector(EXCCODE_MOD, handle_tlbm);
  2033. set_except_vector(EXCCODE_TLBL, handle_tlbl);
  2034. set_except_vector(EXCCODE_TLBS, handle_tlbs);
  2035. set_except_vector(EXCCODE_ADEL, handle_adel);
  2036. set_except_vector(EXCCODE_ADES, handle_ades);
  2037. set_except_vector(EXCCODE_IBE, handle_ibe);
  2038. set_except_vector(EXCCODE_DBE, handle_dbe);
  2039. set_except_vector(EXCCODE_SYS, handle_sys);
  2040. set_except_vector(EXCCODE_BP, handle_bp);
  2041. if (rdhwr_noopt)
  2042. set_except_vector(EXCCODE_RI, handle_ri);
  2043. else {
  2044. if (cpu_has_vtag_icache)
  2045. set_except_vector(EXCCODE_RI, handle_ri_rdhwr_tlbp);
  2046. else if (current_cpu_type() == CPU_LOONGSON64)
  2047. set_except_vector(EXCCODE_RI, handle_ri_rdhwr_tlbp);
  2048. else
  2049. set_except_vector(EXCCODE_RI, handle_ri_rdhwr);
  2050. }
  2051. set_except_vector(EXCCODE_CPU, handle_cpu);
  2052. set_except_vector(EXCCODE_OV, handle_ov);
  2053. set_except_vector(EXCCODE_TR, handle_tr);
  2054. set_except_vector(EXCCODE_MSAFPE, handle_msa_fpe);
  2055. if (board_nmi_handler_setup)
  2056. board_nmi_handler_setup();
  2057. if (cpu_has_fpu && !cpu_has_nofpuex)
  2058. set_except_vector(EXCCODE_FPE, handle_fpe);
  2059. set_except_vector(MIPS_EXCCODE_TLBPAR, handle_ftlb);
  2060. if (cpu_has_rixiex) {
  2061. set_except_vector(EXCCODE_TLBRI, tlb_do_page_fault_0);
  2062. set_except_vector(EXCCODE_TLBXI, tlb_do_page_fault_0);
  2063. }
  2064. set_except_vector(EXCCODE_MSADIS, handle_msa);
  2065. set_except_vector(EXCCODE_MDMX, handle_mdmx);
  2066. if (cpu_has_mcheck)
  2067. set_except_vector(EXCCODE_MCHECK, handle_mcheck);
  2068. if (cpu_has_mipsmt)
  2069. set_except_vector(EXCCODE_THREAD, handle_mt);
  2070. set_except_vector(EXCCODE_DSPDIS, handle_dsp);
  2071. if (board_cache_error_setup)
  2072. board_cache_error_setup();
  2073. if (cpu_has_vce)
  2074. /* Special exception: R4[04]00 uses also the divec space. */
  2075. set_handler(0x180, &except_vec3_r4000, 0x100);
  2076. else if (cpu_has_4kex)
  2077. set_handler(0x180, &except_vec3_generic, 0x80);
  2078. else
  2079. set_handler(0x080, &except_vec3_generic, 0x80);
  2080. local_flush_icache_range(ebase, ebase + vec_size);
  2081. sort_extable(__start___dbe_table, __stop___dbe_table);
  2082. cu2_notifier(default_cu2_call, 0x80000000); /* Run last */
  2083. }
  2084. static int trap_pm_notifier(struct notifier_block *self, unsigned long cmd,
  2085. void *v)
  2086. {
  2087. switch (cmd) {
  2088. case CPU_PM_ENTER_FAILED:
  2089. case CPU_PM_EXIT:
  2090. configure_status();
  2091. configure_hwrena();
  2092. configure_exception_vector();
  2093. /* Restore register with CPU number for TLB handlers */
  2094. TLBMISS_HANDLER_RESTORE();
  2095. break;
  2096. }
  2097. return NOTIFY_OK;
  2098. }
  2099. static struct notifier_block trap_pm_notifier_block = {
  2100. .notifier_call = trap_pm_notifier,
  2101. };
  2102. static int __init trap_pm_init(void)
  2103. {
  2104. return cpu_pm_register_notifier(&trap_pm_notifier_block);
  2105. }
  2106. arch_initcall(trap_pm_init);