PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/mips/kernel/traps.c

https://bitbucket.org/sola/android_board_snowball_kernel
C | 1795 lines | 1272 code | 265 blank | 258 comment | 260 complexity | 90c2d74b0b267cb42999f0b9f0e5dda4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.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) 2000, 01 MIPS Technologies, Inc.
  12. * Copyright (C) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
  13. */
  14. #include <linux/bug.h>
  15. #include <linux/compiler.h>
  16. #include <linux/init.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/smp.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/kgdb.h>
  27. #include <linux/kdebug.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/notifier.h>
  30. #include <linux/kdb.h>
  31. #include <linux/irq.h>
  32. #include <linux/perf_event.h>
  33. #include <asm/bootinfo.h>
  34. #include <asm/branch.h>
  35. #include <asm/break.h>
  36. #include <asm/cop2.h>
  37. #include <asm/cpu.h>
  38. #include <asm/dsp.h>
  39. #include <asm/fpu.h>
  40. #include <asm/fpu_emulator.h>
  41. #include <asm/mipsregs.h>
  42. #include <asm/mipsmtregs.h>
  43. #include <asm/module.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/ptrace.h>
  46. #include <asm/sections.h>
  47. #include <asm/system.h>
  48. #include <asm/tlbdebug.h>
  49. #include <asm/traps.h>
  50. #include <asm/uaccess.h>
  51. #include <asm/watch.h>
  52. #include <asm/mmu_context.h>
  53. #include <asm/types.h>
  54. #include <asm/stacktrace.h>
  55. #include <asm/uasm.h>
  56. extern void check_wait(void);
  57. extern asmlinkage void r4k_wait(void);
  58. extern asmlinkage void rollback_handle_int(void);
  59. extern asmlinkage void handle_int(void);
  60. extern asmlinkage void handle_tlbm(void);
  61. extern asmlinkage void handle_tlbl(void);
  62. extern asmlinkage void handle_tlbs(void);
  63. extern asmlinkage void handle_adel(void);
  64. extern asmlinkage void handle_ades(void);
  65. extern asmlinkage void handle_ibe(void);
  66. extern asmlinkage void handle_dbe(void);
  67. extern asmlinkage void handle_sys(void);
  68. extern asmlinkage void handle_bp(void);
  69. extern asmlinkage void handle_ri(void);
  70. extern asmlinkage void handle_ri_rdhwr_vivt(void);
  71. extern asmlinkage void handle_ri_rdhwr(void);
  72. extern asmlinkage void handle_cpu(void);
  73. extern asmlinkage void handle_ov(void);
  74. extern asmlinkage void handle_tr(void);
  75. extern asmlinkage void handle_fpe(void);
  76. extern asmlinkage void handle_mdmx(void);
  77. extern asmlinkage void handle_watch(void);
  78. extern asmlinkage void handle_mt(void);
  79. extern asmlinkage void handle_dsp(void);
  80. extern asmlinkage void handle_mcheck(void);
  81. extern asmlinkage void handle_reserved(void);
  82. extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
  83. struct mips_fpu_struct *ctx, int has_fpu,
  84. void *__user *fault_addr);
  85. void (*board_be_init)(void);
  86. int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
  87. void (*board_nmi_handler_setup)(void);
  88. void (*board_ejtag_handler_setup)(void);
  89. void (*board_bind_eic_interrupt)(int irq, int regset);
  90. static void show_raw_backtrace(unsigned long reg29)
  91. {
  92. unsigned long *sp = (unsigned long *)(reg29 & ~3);
  93. unsigned long addr;
  94. printk("Call Trace:");
  95. #ifdef CONFIG_KALLSYMS
  96. printk("\n");
  97. #endif
  98. while (!kstack_end(sp)) {
  99. unsigned long __user *p =
  100. (unsigned long __user *)(unsigned long)sp++;
  101. if (__get_user(addr, p)) {
  102. printk(" (Bad stack address)");
  103. break;
  104. }
  105. if (__kernel_text_address(addr))
  106. print_ip_sym(addr);
  107. }
  108. printk("\n");
  109. }
  110. #ifdef CONFIG_KALLSYMS
  111. int raw_show_trace;
  112. static int __init set_raw_show_trace(char *str)
  113. {
  114. raw_show_trace = 1;
  115. return 1;
  116. }
  117. __setup("raw_show_trace", set_raw_show_trace);
  118. #endif
  119. static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
  120. {
  121. unsigned long sp = regs->regs[29];
  122. unsigned long ra = regs->regs[31];
  123. unsigned long pc = regs->cp0_epc;
  124. if (raw_show_trace || !__kernel_text_address(pc)) {
  125. show_raw_backtrace(sp);
  126. return;
  127. }
  128. printk("Call Trace:\n");
  129. do {
  130. print_ip_sym(pc);
  131. pc = unwind_stack(task, &sp, pc, &ra);
  132. } while (pc);
  133. printk("\n");
  134. }
  135. /*
  136. * This routine abuses get_user()/put_user() to reference pointers
  137. * with at least a bit of error checking ...
  138. */
  139. static void show_stacktrace(struct task_struct *task,
  140. const struct pt_regs *regs)
  141. {
  142. const int field = 2 * sizeof(unsigned long);
  143. long stackdata;
  144. int i;
  145. unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
  146. printk("Stack :");
  147. i = 0;
  148. while ((unsigned long) sp & (PAGE_SIZE - 1)) {
  149. if (i && ((i % (64 / field)) == 0))
  150. printk("\n ");
  151. if (i > 39) {
  152. printk(" ...");
  153. break;
  154. }
  155. if (__get_user(stackdata, sp++)) {
  156. printk(" (Bad stack address)");
  157. break;
  158. }
  159. printk(" %0*lx", field, stackdata);
  160. i++;
  161. }
  162. printk("\n");
  163. show_backtrace(task, regs);
  164. }
  165. void show_stack(struct task_struct *task, unsigned long *sp)
  166. {
  167. struct pt_regs regs;
  168. if (sp) {
  169. regs.regs[29] = (unsigned long)sp;
  170. regs.regs[31] = 0;
  171. regs.cp0_epc = 0;
  172. } else {
  173. if (task && task != current) {
  174. regs.regs[29] = task->thread.reg29;
  175. regs.regs[31] = 0;
  176. regs.cp0_epc = task->thread.reg31;
  177. #ifdef CONFIG_KGDB_KDB
  178. } else if (atomic_read(&kgdb_active) != -1 &&
  179. kdb_current_regs) {
  180. memcpy(&regs, kdb_current_regs, sizeof(regs));
  181. #endif /* CONFIG_KGDB_KDB */
  182. } else {
  183. prepare_frametrace(&regs);
  184. }
  185. }
  186. show_stacktrace(task, &regs);
  187. }
  188. /*
  189. * The architecture-independent dump_stack generator
  190. */
  191. void dump_stack(void)
  192. {
  193. struct pt_regs regs;
  194. prepare_frametrace(&regs);
  195. show_backtrace(current, &regs);
  196. }
  197. EXPORT_SYMBOL(dump_stack);
  198. static void show_code(unsigned int __user *pc)
  199. {
  200. long i;
  201. unsigned short __user *pc16 = NULL;
  202. printk("\nCode:");
  203. if ((unsigned long)pc & 1)
  204. pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
  205. for(i = -3 ; i < 6 ; i++) {
  206. unsigned int insn;
  207. if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
  208. printk(" (Bad address in epc)\n");
  209. break;
  210. }
  211. printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
  212. }
  213. }
  214. static void __show_regs(const struct pt_regs *regs)
  215. {
  216. const int field = 2 * sizeof(unsigned long);
  217. unsigned int cause = regs->cp0_cause;
  218. int i;
  219. printk("Cpu %d\n", smp_processor_id());
  220. /*
  221. * Saved main processor registers
  222. */
  223. for (i = 0; i < 32; ) {
  224. if ((i % 4) == 0)
  225. printk("$%2d :", i);
  226. if (i == 0)
  227. printk(" %0*lx", field, 0UL);
  228. else if (i == 26 || i == 27)
  229. printk(" %*s", field, "");
  230. else
  231. printk(" %0*lx", field, regs->regs[i]);
  232. i++;
  233. if ((i % 4) == 0)
  234. printk("\n");
  235. }
  236. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  237. printk("Acx : %0*lx\n", field, regs->acx);
  238. #endif
  239. printk("Hi : %0*lx\n", field, regs->hi);
  240. printk("Lo : %0*lx\n", field, regs->lo);
  241. /*
  242. * Saved cp0 registers
  243. */
  244. printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
  245. (void *) regs->cp0_epc);
  246. printk(" %s\n", print_tainted());
  247. printk("ra : %0*lx %pS\n", field, regs->regs[31],
  248. (void *) regs->regs[31]);
  249. printk("Status: %08x ", (uint32_t) regs->cp0_status);
  250. if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
  251. if (regs->cp0_status & ST0_KUO)
  252. printk("KUo ");
  253. if (regs->cp0_status & ST0_IEO)
  254. printk("IEo ");
  255. if (regs->cp0_status & ST0_KUP)
  256. printk("KUp ");
  257. if (regs->cp0_status & ST0_IEP)
  258. printk("IEp ");
  259. if (regs->cp0_status & ST0_KUC)
  260. printk("KUc ");
  261. if (regs->cp0_status & ST0_IEC)
  262. printk("IEc ");
  263. } else {
  264. if (regs->cp0_status & ST0_KX)
  265. printk("KX ");
  266. if (regs->cp0_status & ST0_SX)
  267. printk("SX ");
  268. if (regs->cp0_status & ST0_UX)
  269. printk("UX ");
  270. switch (regs->cp0_status & ST0_KSU) {
  271. case KSU_USER:
  272. printk("USER ");
  273. break;
  274. case KSU_SUPERVISOR:
  275. printk("SUPERVISOR ");
  276. break;
  277. case KSU_KERNEL:
  278. printk("KERNEL ");
  279. break;
  280. default:
  281. printk("BAD_MODE ");
  282. break;
  283. }
  284. if (regs->cp0_status & ST0_ERL)
  285. printk("ERL ");
  286. if (regs->cp0_status & ST0_EXL)
  287. printk("EXL ");
  288. if (regs->cp0_status & ST0_IE)
  289. printk("IE ");
  290. }
  291. printk("\n");
  292. printk("Cause : %08x\n", cause);
  293. cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  294. if (1 <= cause && cause <= 5)
  295. printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
  296. printk("PrId : %08x (%s)\n", read_c0_prid(),
  297. cpu_name_string());
  298. }
  299. /*
  300. * FIXME: really the generic show_regs should take a const pointer argument.
  301. */
  302. void show_regs(struct pt_regs *regs)
  303. {
  304. __show_regs((struct pt_regs *)regs);
  305. }
  306. void show_registers(struct pt_regs *regs)
  307. {
  308. const int field = 2 * sizeof(unsigned long);
  309. __show_regs(regs);
  310. print_modules();
  311. printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
  312. current->comm, current->pid, current_thread_info(), current,
  313. field, current_thread_info()->tp_value);
  314. if (cpu_has_userlocal) {
  315. unsigned long tls;
  316. tls = read_c0_userlocal();
  317. if (tls != current_thread_info()->tp_value)
  318. printk("*HwTLS: %0*lx\n", field, tls);
  319. }
  320. show_stacktrace(current, regs);
  321. show_code((unsigned int __user *) regs->cp0_epc);
  322. printk("\n");
  323. }
  324. static int regs_to_trapnr(struct pt_regs *regs)
  325. {
  326. return (regs->cp0_cause >> 2) & 0x1f;
  327. }
  328. static DEFINE_SPINLOCK(die_lock);
  329. void __noreturn die(const char *str, struct pt_regs *regs)
  330. {
  331. static int die_counter;
  332. int sig = SIGSEGV;
  333. #ifdef CONFIG_MIPS_MT_SMTC
  334. unsigned long dvpret = dvpe();
  335. #endif /* CONFIG_MIPS_MT_SMTC */
  336. if (notify_die(DIE_OOPS, str, regs, 0, regs_to_trapnr(regs), SIGSEGV) == NOTIFY_STOP)
  337. sig = 0;
  338. console_verbose();
  339. spin_lock_irq(&die_lock);
  340. bust_spinlocks(1);
  341. #ifdef CONFIG_MIPS_MT_SMTC
  342. mips_mt_regdump(dvpret);
  343. #endif /* CONFIG_MIPS_MT_SMTC */
  344. printk("%s[#%d]:\n", str, ++die_counter);
  345. show_registers(regs);
  346. add_taint(TAINT_DIE);
  347. spin_unlock_irq(&die_lock);
  348. if (in_interrupt())
  349. panic("Fatal exception in interrupt");
  350. if (panic_on_oops) {
  351. printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
  352. ssleep(5);
  353. panic("Fatal exception");
  354. }
  355. do_exit(sig);
  356. }
  357. extern struct exception_table_entry __start___dbe_table[];
  358. extern struct exception_table_entry __stop___dbe_table[];
  359. __asm__(
  360. " .section __dbe_table, \"a\"\n"
  361. " .previous \n");
  362. /* Given an address, look for it in the exception tables. */
  363. static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
  364. {
  365. const struct exception_table_entry *e;
  366. e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
  367. if (!e)
  368. e = search_module_dbetables(addr);
  369. return e;
  370. }
  371. asmlinkage void do_be(struct pt_regs *regs)
  372. {
  373. const int field = 2 * sizeof(unsigned long);
  374. const struct exception_table_entry *fixup = NULL;
  375. int data = regs->cp0_cause & 4;
  376. int action = MIPS_BE_FATAL;
  377. /* XXX For now. Fixme, this searches the wrong table ... */
  378. if (data && !user_mode(regs))
  379. fixup = search_dbe_tables(exception_epc(regs));
  380. if (fixup)
  381. action = MIPS_BE_FIXUP;
  382. if (board_be_handler)
  383. action = board_be_handler(regs, fixup != NULL);
  384. switch (action) {
  385. case MIPS_BE_DISCARD:
  386. return;
  387. case MIPS_BE_FIXUP:
  388. if (fixup) {
  389. regs->cp0_epc = fixup->nextinsn;
  390. return;
  391. }
  392. break;
  393. default:
  394. break;
  395. }
  396. /*
  397. * Assume it would be too dangerous to continue ...
  398. */
  399. printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
  400. data ? "Data" : "Instruction",
  401. field, regs->cp0_epc, field, regs->regs[31]);
  402. if (notify_die(DIE_OOPS, "bus error", regs, 0, regs_to_trapnr(regs), SIGBUS)
  403. == NOTIFY_STOP)
  404. return;
  405. die_if_kernel("Oops", regs);
  406. force_sig(SIGBUS, current);
  407. }
  408. /*
  409. * ll/sc, rdhwr, sync emulation
  410. */
  411. #define OPCODE 0xfc000000
  412. #define BASE 0x03e00000
  413. #define RT 0x001f0000
  414. #define OFFSET 0x0000ffff
  415. #define LL 0xc0000000
  416. #define SC 0xe0000000
  417. #define SPEC0 0x00000000
  418. #define SPEC3 0x7c000000
  419. #define RD 0x0000f800
  420. #define FUNC 0x0000003f
  421. #define SYNC 0x0000000f
  422. #define RDHWR 0x0000003b
  423. /*
  424. * The ll_bit is cleared by r*_switch.S
  425. */
  426. unsigned int ll_bit;
  427. struct task_struct *ll_task;
  428. static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
  429. {
  430. unsigned long value, __user *vaddr;
  431. long offset;
  432. /*
  433. * analyse the ll instruction that just caused a ri exception
  434. * and put the referenced address to addr.
  435. */
  436. /* sign extend offset */
  437. offset = opcode & OFFSET;
  438. offset <<= 16;
  439. offset >>= 16;
  440. vaddr = (unsigned long __user *)
  441. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  442. if ((unsigned long)vaddr & 3)
  443. return SIGBUS;
  444. if (get_user(value, vaddr))
  445. return SIGSEGV;
  446. preempt_disable();
  447. if (ll_task == NULL || ll_task == current) {
  448. ll_bit = 1;
  449. } else {
  450. ll_bit = 0;
  451. }
  452. ll_task = current;
  453. preempt_enable();
  454. regs->regs[(opcode & RT) >> 16] = value;
  455. return 0;
  456. }
  457. static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
  458. {
  459. unsigned long __user *vaddr;
  460. unsigned long reg;
  461. long offset;
  462. /*
  463. * analyse the sc instruction that just caused a ri exception
  464. * and put the referenced address to addr.
  465. */
  466. /* sign extend offset */
  467. offset = opcode & OFFSET;
  468. offset <<= 16;
  469. offset >>= 16;
  470. vaddr = (unsigned long __user *)
  471. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  472. reg = (opcode & RT) >> 16;
  473. if ((unsigned long)vaddr & 3)
  474. return SIGBUS;
  475. preempt_disable();
  476. if (ll_bit == 0 || ll_task != current) {
  477. regs->regs[reg] = 0;
  478. preempt_enable();
  479. return 0;
  480. }
  481. preempt_enable();
  482. if (put_user(regs->regs[reg], vaddr))
  483. return SIGSEGV;
  484. regs->regs[reg] = 1;
  485. return 0;
  486. }
  487. /*
  488. * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
  489. * opcodes are supposed to result in coprocessor unusable exceptions if
  490. * executed on ll/sc-less processors. That's the theory. In practice a
  491. * few processors such as NEC's VR4100 throw reserved instruction exceptions
  492. * instead, so we're doing the emulation thing in both exception handlers.
  493. */
  494. static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
  495. {
  496. if ((opcode & OPCODE) == LL) {
  497. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  498. 1, 0, regs, 0);
  499. return simulate_ll(regs, opcode);
  500. }
  501. if ((opcode & OPCODE) == SC) {
  502. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  503. 1, 0, regs, 0);
  504. return simulate_sc(regs, opcode);
  505. }
  506. return -1; /* Must be something else ... */
  507. }
  508. /*
  509. * Simulate trapping 'rdhwr' instructions to provide user accessible
  510. * registers not implemented in hardware.
  511. */
  512. static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
  513. {
  514. struct thread_info *ti = task_thread_info(current);
  515. if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
  516. int rd = (opcode & RD) >> 11;
  517. int rt = (opcode & RT) >> 16;
  518. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  519. 1, 0, regs, 0);
  520. switch (rd) {
  521. case 0: /* CPU number */
  522. regs->regs[rt] = smp_processor_id();
  523. return 0;
  524. case 1: /* SYNCI length */
  525. regs->regs[rt] = min(current_cpu_data.dcache.linesz,
  526. current_cpu_data.icache.linesz);
  527. return 0;
  528. case 2: /* Read count register */
  529. regs->regs[rt] = read_c0_count();
  530. return 0;
  531. case 3: /* Count register resolution */
  532. switch (current_cpu_data.cputype) {
  533. case CPU_20KC:
  534. case CPU_25KF:
  535. regs->regs[rt] = 1;
  536. break;
  537. default:
  538. regs->regs[rt] = 2;
  539. }
  540. return 0;
  541. case 29:
  542. regs->regs[rt] = ti->tp_value;
  543. return 0;
  544. default:
  545. return -1;
  546. }
  547. }
  548. /* Not ours. */
  549. return -1;
  550. }
  551. static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
  552. {
  553. if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
  554. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  555. 1, 0, regs, 0);
  556. return 0;
  557. }
  558. return -1; /* Must be something else ... */
  559. }
  560. asmlinkage void do_ov(struct pt_regs *regs)
  561. {
  562. siginfo_t info;
  563. die_if_kernel("Integer overflow", regs);
  564. info.si_code = FPE_INTOVF;
  565. info.si_signo = SIGFPE;
  566. info.si_errno = 0;
  567. info.si_addr = (void __user *) regs->cp0_epc;
  568. force_sig_info(SIGFPE, &info, current);
  569. }
  570. static int process_fpemu_return(int sig, void __user *fault_addr)
  571. {
  572. if (sig == SIGSEGV || sig == SIGBUS) {
  573. struct siginfo si = {0};
  574. si.si_addr = fault_addr;
  575. si.si_signo = sig;
  576. if (sig == SIGSEGV) {
  577. if (find_vma(current->mm, (unsigned long)fault_addr))
  578. si.si_code = SEGV_ACCERR;
  579. else
  580. si.si_code = SEGV_MAPERR;
  581. } else {
  582. si.si_code = BUS_ADRERR;
  583. }
  584. force_sig_info(sig, &si, current);
  585. return 1;
  586. } else if (sig) {
  587. force_sig(sig, current);
  588. return 1;
  589. } else {
  590. return 0;
  591. }
  592. }
  593. /*
  594. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  595. */
  596. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  597. {
  598. siginfo_t info = {0};
  599. if (notify_die(DIE_FP, "FP exception", regs, 0, regs_to_trapnr(regs), SIGFPE)
  600. == NOTIFY_STOP)
  601. return;
  602. die_if_kernel("FP exception in kernel code", regs);
  603. if (fcr31 & FPU_CSR_UNI_X) {
  604. int sig;
  605. void __user *fault_addr = NULL;
  606. /*
  607. * Unimplemented operation exception. If we've got the full
  608. * software emulator on-board, let's use it...
  609. *
  610. * Force FPU to dump state into task/thread context. We're
  611. * moving a lot of data here for what is probably a single
  612. * instruction, but the alternative is to pre-decode the FP
  613. * register operands before invoking the emulator, which seems
  614. * a bit extreme for what should be an infrequent event.
  615. */
  616. /* Ensure 'resume' not overwrite saved fp context again. */
  617. lose_fpu(1);
  618. /* Run the emulator */
  619. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  620. &fault_addr);
  621. /*
  622. * We can't allow the emulated instruction to leave any of
  623. * the cause bit set in $fcr31.
  624. */
  625. current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
  626. /* Restore the hardware register state */
  627. own_fpu(1); /* Using the FPU again. */
  628. /* If something went wrong, signal */
  629. process_fpemu_return(sig, fault_addr);
  630. return;
  631. } else if (fcr31 & FPU_CSR_INV_X)
  632. info.si_code = FPE_FLTINV;
  633. else if (fcr31 & FPU_CSR_DIV_X)
  634. info.si_code = FPE_FLTDIV;
  635. else if (fcr31 & FPU_CSR_OVF_X)
  636. info.si_code = FPE_FLTOVF;
  637. else if (fcr31 & FPU_CSR_UDF_X)
  638. info.si_code = FPE_FLTUND;
  639. else if (fcr31 & FPU_CSR_INE_X)
  640. info.si_code = FPE_FLTRES;
  641. else
  642. info.si_code = __SI_FAULT;
  643. info.si_signo = SIGFPE;
  644. info.si_errno = 0;
  645. info.si_addr = (void __user *) regs->cp0_epc;
  646. force_sig_info(SIGFPE, &info, current);
  647. }
  648. static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
  649. const char *str)
  650. {
  651. siginfo_t info;
  652. char b[40];
  653. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  654. if (kgdb_ll_trap(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  655. return;
  656. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  657. if (notify_die(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  658. return;
  659. /*
  660. * A short test says that IRIX 5.3 sends SIGTRAP for all trap
  661. * insns, even for trap and break codes that indicate arithmetic
  662. * failures. Weird ...
  663. * But should we continue the brokenness??? --macro
  664. */
  665. switch (code) {
  666. case BRK_OVERFLOW:
  667. case BRK_DIVZERO:
  668. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  669. die_if_kernel(b, regs);
  670. if (code == BRK_DIVZERO)
  671. info.si_code = FPE_INTDIV;
  672. else
  673. info.si_code = FPE_INTOVF;
  674. info.si_signo = SIGFPE;
  675. info.si_errno = 0;
  676. info.si_addr = (void __user *) regs->cp0_epc;
  677. force_sig_info(SIGFPE, &info, current);
  678. break;
  679. case BRK_BUG:
  680. die_if_kernel("Kernel bug detected", regs);
  681. force_sig(SIGTRAP, current);
  682. break;
  683. case BRK_MEMU:
  684. /*
  685. * Address errors may be deliberately induced by the FPU
  686. * emulator to retake control of the CPU after executing the
  687. * instruction in the delay slot of an emulated branch.
  688. *
  689. * Terminate if exception was recognized as a delay slot return
  690. * otherwise handle as normal.
  691. */
  692. if (do_dsemulret(regs))
  693. return;
  694. die_if_kernel("Math emu break/trap", regs);
  695. force_sig(SIGTRAP, current);
  696. break;
  697. default:
  698. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  699. die_if_kernel(b, regs);
  700. force_sig(SIGTRAP, current);
  701. }
  702. }
  703. asmlinkage void do_bp(struct pt_regs *regs)
  704. {
  705. unsigned int opcode, bcode;
  706. if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
  707. goto out_sigsegv;
  708. /*
  709. * There is the ancient bug in the MIPS assemblers that the break
  710. * code starts left to bit 16 instead to bit 6 in the opcode.
  711. * Gas is bug-compatible, but not always, grrr...
  712. * We handle both cases with a simple heuristics. --macro
  713. */
  714. bcode = ((opcode >> 6) & ((1 << 20) - 1));
  715. if (bcode >= (1 << 10))
  716. bcode >>= 10;
  717. /*
  718. * notify the kprobe handlers, if instruction is likely to
  719. * pertain to them.
  720. */
  721. switch (bcode) {
  722. case BRK_KPROBE_BP:
  723. if (notify_die(DIE_BREAK, "debug", regs, bcode, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  724. return;
  725. else
  726. break;
  727. case BRK_KPROBE_SSTEPBP:
  728. if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  729. return;
  730. else
  731. break;
  732. default:
  733. break;
  734. }
  735. do_trap_or_bp(regs, bcode, "Break");
  736. return;
  737. out_sigsegv:
  738. force_sig(SIGSEGV, current);
  739. }
  740. asmlinkage void do_tr(struct pt_regs *regs)
  741. {
  742. unsigned int opcode, tcode = 0;
  743. if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
  744. goto out_sigsegv;
  745. /* Immediate versions don't provide a code. */
  746. if (!(opcode & OPCODE))
  747. tcode = ((opcode >> 6) & ((1 << 10) - 1));
  748. do_trap_or_bp(regs, tcode, "Trap");
  749. return;
  750. out_sigsegv:
  751. force_sig(SIGSEGV, current);
  752. }
  753. asmlinkage void do_ri(struct pt_regs *regs)
  754. {
  755. unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
  756. unsigned long old_epc = regs->cp0_epc;
  757. unsigned int opcode = 0;
  758. int status = -1;
  759. if (notify_die(DIE_RI, "RI Fault", regs, 0, regs_to_trapnr(regs), SIGILL)
  760. == NOTIFY_STOP)
  761. return;
  762. die_if_kernel("Reserved instruction in kernel code", regs);
  763. if (unlikely(compute_return_epc(regs) < 0))
  764. return;
  765. if (unlikely(get_user(opcode, epc) < 0))
  766. status = SIGSEGV;
  767. if (!cpu_has_llsc && status < 0)
  768. status = simulate_llsc(regs, opcode);
  769. if (status < 0)
  770. status = simulate_rdhwr(regs, opcode);
  771. if (status < 0)
  772. status = simulate_sync(regs, opcode);
  773. if (status < 0)
  774. status = SIGILL;
  775. if (unlikely(status > 0)) {
  776. regs->cp0_epc = old_epc; /* Undo skip-over. */
  777. force_sig(status, current);
  778. }
  779. }
  780. /*
  781. * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
  782. * emulated more than some threshold number of instructions, force migration to
  783. * a "CPU" that has FP support.
  784. */
  785. static void mt_ase_fp_affinity(void)
  786. {
  787. #ifdef CONFIG_MIPS_MT_FPAFF
  788. if (mt_fpemul_threshold > 0 &&
  789. ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
  790. /*
  791. * If there's no FPU present, or if the application has already
  792. * restricted the allowed set to exclude any CPUs with FPUs,
  793. * we'll skip the procedure.
  794. */
  795. if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
  796. cpumask_t tmask;
  797. current->thread.user_cpus_allowed
  798. = current->cpus_allowed;
  799. cpus_and(tmask, current->cpus_allowed,
  800. mt_fpu_cpumask);
  801. set_cpus_allowed_ptr(current, &tmask);
  802. set_thread_flag(TIF_FPUBOUND);
  803. }
  804. }
  805. #endif /* CONFIG_MIPS_MT_FPAFF */
  806. }
  807. /*
  808. * No lock; only written during early bootup by CPU 0.
  809. */
  810. static RAW_NOTIFIER_HEAD(cu2_chain);
  811. int __ref register_cu2_notifier(struct notifier_block *nb)
  812. {
  813. return raw_notifier_chain_register(&cu2_chain, nb);
  814. }
  815. int cu2_notifier_call_chain(unsigned long val, void *v)
  816. {
  817. return raw_notifier_call_chain(&cu2_chain, val, v);
  818. }
  819. static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
  820. void *data)
  821. {
  822. struct pt_regs *regs = data;
  823. switch (action) {
  824. default:
  825. die_if_kernel("Unhandled kernel unaligned access or invalid "
  826. "instruction", regs);
  827. /* Fall through */
  828. case CU2_EXCEPTION:
  829. force_sig(SIGILL, current);
  830. }
  831. return NOTIFY_OK;
  832. }
  833. asmlinkage void do_cpu(struct pt_regs *regs)
  834. {
  835. unsigned int __user *epc;
  836. unsigned long old_epc;
  837. unsigned int opcode;
  838. unsigned int cpid;
  839. int status;
  840. unsigned long __maybe_unused flags;
  841. die_if_kernel("do_cpu invoked from kernel context!", regs);
  842. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  843. switch (cpid) {
  844. case 0:
  845. epc = (unsigned int __user *)exception_epc(regs);
  846. old_epc = regs->cp0_epc;
  847. opcode = 0;
  848. status = -1;
  849. if (unlikely(compute_return_epc(regs) < 0))
  850. return;
  851. if (unlikely(get_user(opcode, epc) < 0))
  852. status = SIGSEGV;
  853. if (!cpu_has_llsc && status < 0)
  854. status = simulate_llsc(regs, opcode);
  855. if (status < 0)
  856. status = simulate_rdhwr(regs, opcode);
  857. if (status < 0)
  858. status = SIGILL;
  859. if (unlikely(status > 0)) {
  860. regs->cp0_epc = old_epc; /* Undo skip-over. */
  861. force_sig(status, current);
  862. }
  863. return;
  864. case 1:
  865. if (used_math()) /* Using the FPU again. */
  866. own_fpu(1);
  867. else { /* First time FPU user. */
  868. init_fpu();
  869. set_used_math();
  870. }
  871. if (!raw_cpu_has_fpu) {
  872. int sig;
  873. void __user *fault_addr = NULL;
  874. sig = fpu_emulator_cop1Handler(regs,
  875. &current->thread.fpu,
  876. 0, &fault_addr);
  877. if (!process_fpemu_return(sig, fault_addr))
  878. mt_ase_fp_affinity();
  879. }
  880. return;
  881. case 2:
  882. raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
  883. return;
  884. case 3:
  885. break;
  886. }
  887. force_sig(SIGILL, current);
  888. }
  889. asmlinkage void do_mdmx(struct pt_regs *regs)
  890. {
  891. force_sig(SIGILL, current);
  892. }
  893. /*
  894. * Called with interrupts disabled.
  895. */
  896. asmlinkage void do_watch(struct pt_regs *regs)
  897. {
  898. u32 cause;
  899. /*
  900. * Clear WP (bit 22) bit of cause register so we don't loop
  901. * forever.
  902. */
  903. cause = read_c0_cause();
  904. cause &= ~(1 << 22);
  905. write_c0_cause(cause);
  906. /*
  907. * If the current thread has the watch registers loaded, save
  908. * their values and send SIGTRAP. Otherwise another thread
  909. * left the registers set, clear them and continue.
  910. */
  911. if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
  912. mips_read_watch_registers();
  913. local_irq_enable();
  914. force_sig(SIGTRAP, current);
  915. } else {
  916. mips_clear_watch_registers();
  917. local_irq_enable();
  918. }
  919. }
  920. asmlinkage void do_mcheck(struct pt_regs *regs)
  921. {
  922. const int field = 2 * sizeof(unsigned long);
  923. int multi_match = regs->cp0_status & ST0_TS;
  924. show_regs(regs);
  925. if (multi_match) {
  926. printk("Index : %0x\n", read_c0_index());
  927. printk("Pagemask: %0x\n", read_c0_pagemask());
  928. printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
  929. printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
  930. printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
  931. printk("\n");
  932. dump_tlb_all();
  933. }
  934. show_code((unsigned int __user *) regs->cp0_epc);
  935. /*
  936. * Some chips may have other causes of machine check (e.g. SB1
  937. * graduation timer)
  938. */
  939. panic("Caught Machine Check exception - %scaused by multiple "
  940. "matching entries in the TLB.",
  941. (multi_match) ? "" : "not ");
  942. }
  943. asmlinkage void do_mt(struct pt_regs *regs)
  944. {
  945. int subcode;
  946. subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
  947. >> VPECONTROL_EXCPT_SHIFT;
  948. switch (subcode) {
  949. case 0:
  950. printk(KERN_DEBUG "Thread Underflow\n");
  951. break;
  952. case 1:
  953. printk(KERN_DEBUG "Thread Overflow\n");
  954. break;
  955. case 2:
  956. printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
  957. break;
  958. case 3:
  959. printk(KERN_DEBUG "Gating Storage Exception\n");
  960. break;
  961. case 4:
  962. printk(KERN_DEBUG "YIELD Scheduler Exception\n");
  963. break;
  964. case 5:
  965. printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
  966. break;
  967. default:
  968. printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
  969. subcode);
  970. break;
  971. }
  972. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  973. force_sig(SIGILL, current);
  974. }
  975. asmlinkage void do_dsp(struct pt_regs *regs)
  976. {
  977. if (cpu_has_dsp)
  978. panic("Unexpected DSP exception\n");
  979. force_sig(SIGILL, current);
  980. }
  981. asmlinkage void do_reserved(struct pt_regs *regs)
  982. {
  983. /*
  984. * Game over - no way to handle this if it ever occurs. Most probably
  985. * caused by a new unknown cpu type or after another deadly
  986. * hard/software error.
  987. */
  988. show_regs(regs);
  989. panic("Caught reserved exception %ld - should not happen.",
  990. (regs->cp0_cause & 0x7f) >> 2);
  991. }
  992. static int __initdata l1parity = 1;
  993. static int __init nol1parity(char *s)
  994. {
  995. l1parity = 0;
  996. return 1;
  997. }
  998. __setup("nol1par", nol1parity);
  999. static int __initdata l2parity = 1;
  1000. static int __init nol2parity(char *s)
  1001. {
  1002. l2parity = 0;
  1003. return 1;
  1004. }
  1005. __setup("nol2par", nol2parity);
  1006. /*
  1007. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  1008. * it different ways.
  1009. */
  1010. static inline void parity_protection_init(void)
  1011. {
  1012. switch (current_cpu_type()) {
  1013. case CPU_24K:
  1014. case CPU_34K:
  1015. case CPU_74K:
  1016. case CPU_1004K:
  1017. {
  1018. #define ERRCTL_PE 0x80000000
  1019. #define ERRCTL_L2P 0x00800000
  1020. unsigned long errctl;
  1021. unsigned int l1parity_present, l2parity_present;
  1022. errctl = read_c0_ecc();
  1023. errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
  1024. /* probe L1 parity support */
  1025. write_c0_ecc(errctl | ERRCTL_PE);
  1026. back_to_back_c0_hazard();
  1027. l1parity_present = (read_c0_ecc() & ERRCTL_PE);
  1028. /* probe L2 parity support */
  1029. write_c0_ecc(errctl|ERRCTL_L2P);
  1030. back_to_back_c0_hazard();
  1031. l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
  1032. if (l1parity_present && l2parity_present) {
  1033. if (l1parity)
  1034. errctl |= ERRCTL_PE;
  1035. if (l1parity ^ l2parity)
  1036. errctl |= ERRCTL_L2P;
  1037. } else if (l1parity_present) {
  1038. if (l1parity)
  1039. errctl |= ERRCTL_PE;
  1040. } else if (l2parity_present) {
  1041. if (l2parity)
  1042. errctl |= ERRCTL_L2P;
  1043. } else {
  1044. /* No parity available */
  1045. }
  1046. printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
  1047. write_c0_ecc(errctl);
  1048. back_to_back_c0_hazard();
  1049. errctl = read_c0_ecc();
  1050. printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
  1051. if (l1parity_present)
  1052. printk(KERN_INFO "Cache parity protection %sabled\n",
  1053. (errctl & ERRCTL_PE) ? "en" : "dis");
  1054. if (l2parity_present) {
  1055. if (l1parity_present && l1parity)
  1056. errctl ^= ERRCTL_L2P;
  1057. printk(KERN_INFO "L2 cache parity protection %sabled\n",
  1058. (errctl & ERRCTL_L2P) ? "en" : "dis");
  1059. }
  1060. }
  1061. break;
  1062. case CPU_5KC:
  1063. write_c0_ecc(0x80000000);
  1064. back_to_back_c0_hazard();
  1065. /* Set the PE bit (bit 31) in the c0_errctl register. */
  1066. printk(KERN_INFO "Cache parity protection %sabled\n",
  1067. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  1068. break;
  1069. case CPU_20KC:
  1070. case CPU_25KF:
  1071. /* Clear the DE bit (bit 16) in the c0_status register. */
  1072. printk(KERN_INFO "Enable cache parity protection for "
  1073. "MIPS 20KC/25KF CPUs.\n");
  1074. clear_c0_status(ST0_DE);
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. }
  1080. asmlinkage void cache_parity_error(void)
  1081. {
  1082. const int field = 2 * sizeof(unsigned long);
  1083. unsigned int reg_val;
  1084. /* For the moment, report the problem and hang. */
  1085. printk("Cache error exception:\n");
  1086. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1087. reg_val = read_c0_cacheerr();
  1088. printk("c0_cacheerr == %08x\n", reg_val);
  1089. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1090. reg_val & (1<<30) ? "secondary" : "primary",
  1091. reg_val & (1<<31) ? "data" : "insn");
  1092. printk("Error bits: %s%s%s%s%s%s%s\n",
  1093. reg_val & (1<<29) ? "ED " : "",
  1094. reg_val & (1<<28) ? "ET " : "",
  1095. reg_val & (1<<26) ? "EE " : "",
  1096. reg_val & (1<<25) ? "EB " : "",
  1097. reg_val & (1<<24) ? "EI " : "",
  1098. reg_val & (1<<23) ? "E1 " : "",
  1099. reg_val & (1<<22) ? "E0 " : "");
  1100. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  1101. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
  1102. if (reg_val & (1<<22))
  1103. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  1104. if (reg_val & (1<<23))
  1105. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  1106. #endif
  1107. panic("Can't handle the cache error!");
  1108. }
  1109. /*
  1110. * SDBBP EJTAG debug exception handler.
  1111. * We skip the instruction and return to the next instruction.
  1112. */
  1113. void ejtag_exception_handler(struct pt_regs *regs)
  1114. {
  1115. const int field = 2 * sizeof(unsigned long);
  1116. unsigned long depc, old_epc;
  1117. unsigned int debug;
  1118. printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  1119. depc = read_c0_depc();
  1120. debug = read_c0_debug();
  1121. printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  1122. if (debug & 0x80000000) {
  1123. /*
  1124. * In branch delay slot.
  1125. * We cheat a little bit here and use EPC to calculate the
  1126. * debug return address (DEPC). EPC is restored after the
  1127. * calculation.
  1128. */
  1129. old_epc = regs->cp0_epc;
  1130. regs->cp0_epc = depc;
  1131. __compute_return_epc(regs);
  1132. depc = regs->cp0_epc;
  1133. regs->cp0_epc = old_epc;
  1134. } else
  1135. depc += 4;
  1136. write_c0_depc(depc);
  1137. #if 0
  1138. printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
  1139. write_c0_debug(debug | 0x100);
  1140. #endif
  1141. }
  1142. /*
  1143. * NMI exception handler.
  1144. */
  1145. NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
  1146. {
  1147. bust_spinlocks(1);
  1148. printk("NMI taken!!!!\n");
  1149. die("NMI", regs);
  1150. }
  1151. #define VECTORSPACING 0x100 /* for EI/VI mode */
  1152. unsigned long ebase;
  1153. unsigned long exception_handlers[32];
  1154. unsigned long vi_handlers[64];
  1155. void __init *set_except_vector(int n, void *addr)
  1156. {
  1157. unsigned long handler = (unsigned long) addr;
  1158. unsigned long old_handler = exception_handlers[n];
  1159. exception_handlers[n] = handler;
  1160. if (n == 0 && cpu_has_divec) {
  1161. unsigned long jump_mask = ~((1 << 28) - 1);
  1162. u32 *buf = (u32 *)(ebase + 0x200);
  1163. unsigned int k0 = 26;
  1164. if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
  1165. uasm_i_j(&buf, handler & ~jump_mask);
  1166. uasm_i_nop(&buf);
  1167. } else {
  1168. UASM_i_LA(&buf, k0, handler);
  1169. uasm_i_jr(&buf, k0);
  1170. uasm_i_nop(&buf);
  1171. }
  1172. local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
  1173. }
  1174. return (void *)old_handler;
  1175. }
  1176. static asmlinkage void do_default_vi(void)
  1177. {
  1178. show_regs(get_irq_regs());
  1179. panic("Caught unexpected vectored interrupt.");
  1180. }
  1181. static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
  1182. {
  1183. unsigned long handler;
  1184. unsigned long old_handler = vi_handlers[n];
  1185. int srssets = current_cpu_data.srsets;
  1186. u32 *w;
  1187. unsigned char *b;
  1188. BUG_ON(!cpu_has_veic && !cpu_has_vint);
  1189. if (addr == NULL) {
  1190. handler = (unsigned long) do_default_vi;
  1191. srs = 0;
  1192. } else
  1193. handler = (unsigned long) addr;
  1194. vi_handlers[n] = (unsigned long) addr;
  1195. b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
  1196. if (srs >= srssets)
  1197. panic("Shadow register set %d not supported", srs);
  1198. if (cpu_has_veic) {
  1199. if (board_bind_eic_interrupt)
  1200. board_bind_eic_interrupt(n, srs);
  1201. } else if (cpu_has_vint) {
  1202. /* SRSMap is only defined if shadow sets are implemented */
  1203. if (srssets > 1)
  1204. change_c0_srsmap(0xf << n*4, srs << n*4);
  1205. }
  1206. if (srs == 0) {
  1207. /*
  1208. * If no shadow set is selected then use the default handler
  1209. * that does normal register saving and a standard interrupt exit
  1210. */
  1211. extern char except_vec_vi, except_vec_vi_lui;
  1212. extern char except_vec_vi_ori, except_vec_vi_end;
  1213. extern char rollback_except_vec_vi;
  1214. char *vec_start = (cpu_wait == r4k_wait) ?
  1215. &rollback_except_vec_vi : &except_vec_vi;
  1216. #ifdef CONFIG_MIPS_MT_SMTC
  1217. /*
  1218. * We need to provide the SMTC vectored interrupt handler
  1219. * not only with the address of the handler, but with the
  1220. * Status.IM bit to be masked before going there.
  1221. */
  1222. extern char except_vec_vi_mori;
  1223. const int mori_offset = &except_vec_vi_mori - vec_start;
  1224. #endif /* CONFIG_MIPS_MT_SMTC */
  1225. const int handler_len = &except_vec_vi_end - vec_start;
  1226. const int lui_offset = &except_vec_vi_lui - vec_start;
  1227. const int ori_offset = &except_vec_vi_ori - vec_start;
  1228. if (handler_len > VECTORSPACING) {
  1229. /*
  1230. * Sigh... panicing won't help as the console
  1231. * is probably not configured :(
  1232. */
  1233. panic("VECTORSPACING too small");
  1234. }
  1235. memcpy(b, vec_start, handler_len);
  1236. #ifdef CONFIG_MIPS_MT_SMTC
  1237. BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
  1238. w = (u32 *)(b + mori_offset);
  1239. *w = (*w & 0xffff0000) | (0x100 << n);
  1240. #endif /* CONFIG_MIPS_MT_SMTC */
  1241. w = (u32 *)(b + lui_offset);
  1242. *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
  1243. w = (u32 *)(b + ori_offset);
  1244. *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
  1245. local_flush_icache_range((unsigned long)b,
  1246. (unsigned long)(b+handler_len));
  1247. }
  1248. else {
  1249. /*
  1250. * In other cases jump directly to the interrupt handler
  1251. *
  1252. * It is the handlers responsibility to save registers if required
  1253. * (eg hi/lo) and return from the exception using "eret"
  1254. */
  1255. w = (u32 *)b;
  1256. *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
  1257. *w = 0;
  1258. local_flush_icache_range((unsigned long)b,
  1259. (unsigned long)(b+8));
  1260. }
  1261. return (void *)old_handler;
  1262. }
  1263. void *set_vi_handler(int n, vi_handler_t addr)
  1264. {
  1265. return set_vi_srs_handler(n, addr, 0);
  1266. }
  1267. extern void cpu_cache_init(void);
  1268. extern void tlb_init(void);
  1269. extern void flush_tlb_handlers(void);
  1270. /*
  1271. * Timer interrupt
  1272. */
  1273. int cp0_compare_irq;
  1274. int cp0_compare_irq_shift;
  1275. /*
  1276. * Performance counter IRQ or -1 if shared with timer
  1277. */
  1278. int cp0_perfcount_irq;
  1279. EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
  1280. static int __cpuinitdata noulri;
  1281. static int __init ulri_disable(char *s)
  1282. {
  1283. pr_info("Disabling ulri\n");
  1284. noulri = 1;
  1285. return 1;
  1286. }
  1287. __setup("noulri", ulri_disable);
  1288. void __cpuinit per_cpu_trap_init(void)
  1289. {
  1290. unsigned int cpu = smp_processor_id();
  1291. unsigned int status_set = ST0_CU0;
  1292. unsigned int hwrena = cpu_hwrena_impl_bits;
  1293. #ifdef CONFIG_MIPS_MT_SMTC
  1294. int secondaryTC = 0;
  1295. int bootTC = (cpu == 0);
  1296. /*
  1297. * Only do per_cpu_trap_init() for first TC of Each VPE.
  1298. * Note that this hack assumes that the SMTC init code
  1299. * assigns TCs consecutively and in ascending order.
  1300. */
  1301. if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
  1302. ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
  1303. secondaryTC = 1;
  1304. #endif /* CONFIG_MIPS_MT_SMTC */
  1305. /*
  1306. * Disable coprocessors and select 32-bit or 64-bit addressing
  1307. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  1308. * flag that some firmware may have left set and the TS bit (for
  1309. * IP27). Set XX for ISA IV code to work.
  1310. */
  1311. #ifdef CONFIG_64BIT
  1312. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  1313. #endif
  1314. if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
  1315. status_set |= ST0_XX;
  1316. if (cpu_has_dsp)
  1317. status_set |= ST0_MX;
  1318. change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  1319. status_set);
  1320. if (cpu_has_mips_r2)
  1321. hwrena |= 0x0000000f;
  1322. if (!noulri && cpu_has_userlocal)
  1323. hwrena |= (1 << 29);
  1324. if (hwrena)
  1325. write_c0_hwrena(hwrena);
  1326. #ifdef CONFIG_MIPS_MT_SMTC
  1327. if (!secondaryTC) {
  1328. #endif /* CONFIG_MIPS_MT_SMTC */
  1329. if (cpu_has_veic || cpu_has_vint) {
  1330. unsigned long sr = set_c0_status(ST0_BEV);
  1331. write_c0_ebase(ebase);
  1332. write_c0_status(sr);
  1333. /* Setting vector spacing enables EI/VI mode */
  1334. change_c0_intctl(0x3e0, VECTORSPACING);
  1335. }
  1336. if (cpu_has_divec) {
  1337. if (cpu_has_mipsmt) {
  1338. unsigned int vpflags = dvpe();
  1339. set_c0_cause(CAUSEF_IV);
  1340. evpe(vpflags);
  1341. } else
  1342. set_c0_cause(CAUSEF_IV);
  1343. }
  1344. /*
  1345. * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
  1346. *
  1347. * o read IntCtl.IPTI to determine the timer interrupt
  1348. * o read IntCtl.IPPCI to determine the performance counter interrupt
  1349. */
  1350. if (cpu_has_mips_r2) {
  1351. cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
  1352. cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
  1353. cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
  1354. if (cp0_perfcount_irq == cp0_compare_irq)
  1355. cp0_perfcount_irq = -1;
  1356. } else {
  1357. cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
  1358. cp0_compare_irq_shift = cp0_compare_irq;
  1359. cp0_perfcount_irq = -1;
  1360. }
  1361. #ifdef CONFIG_MIPS_MT_SMTC
  1362. }
  1363. #endif /* CONFIG_MIPS_MT_SMTC */
  1364. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  1365. atomic_inc(&init_mm.mm_count);
  1366. current->active_mm = &init_mm;
  1367. BUG_ON(current->mm);
  1368. enter_lazy_tlb(&init_mm, current);
  1369. #ifdef CONFIG_MIPS_MT_SMTC
  1370. if (bootTC) {
  1371. #endif /* CONFIG_MIPS_MT_SMTC */
  1372. cpu_cache_init();
  1373. tlb_init();
  1374. #ifdef CONFIG_MIPS_MT_SMTC
  1375. } else if (!secondaryTC) {
  1376. /*
  1377. * First TC in non-boot VPE must do subset of tlb_init()
  1378. * for MMU countrol registers.
  1379. */
  1380. write_c0_pagemask(PM_DEFAULT_MASK);
  1381. write_c0_wired(0);
  1382. }
  1383. #endif /* CONFIG_MIPS_MT_SMTC */
  1384. TLBMISS_HANDLER_SETUP();
  1385. }
  1386. /* Install CPU exception handler */
  1387. void __init set_handler(unsigned long offset, void *addr, unsigned long size)
  1388. {
  1389. memcpy((void *)(ebase + offset), addr, size);
  1390. local_flush_icache_range(ebase + offset, ebase + offset + size);
  1391. }
  1392. static char panic_null_cerr[] __cpuinitdata =
  1393. "Trying to set NULL cache error exception handler";
  1394. /*
  1395. * Install uncached CPU exception handler.
  1396. * This is suitable only for the cache error exception which is the only
  1397. * exception handler that is being run uncached.
  1398. */
  1399. void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
  1400. unsigned long size)
  1401. {
  1402. unsigned long uncached_ebase = CKSEG1ADDR(ebase);
  1403. if (!addr)
  1404. panic(panic_null_cerr);
  1405. memcpy((void *)(uncached_ebase + offset), addr, size);
  1406. }
  1407. static int __initdata rdhwr_noopt;
  1408. static int __init set_rdhwr_noopt(char *str)
  1409. {
  1410. rdhwr_noopt = 1;
  1411. return 1;
  1412. }
  1413. __setup("rdhwr_noopt", set_rdhwr_noopt);
  1414. void __init trap_init(void)
  1415. {
  1416. extern char except_vec3_generic, except_vec3_r4000;
  1417. extern char except_vec4;
  1418. unsigned long i;
  1419. int rollback;
  1420. check_wait();
  1421. rollback = (cpu_wait == r4k_wait);
  1422. #if defined(CONFIG_KGDB)
  1423. if (kgdb_early_setup)
  1424. return; /* Already done */
  1425. #endif
  1426. if (cpu_has_veic || cpu_has_vint) {
  1427. unsigned long size = 0x200 + VECTORSPACING*64;
  1428. ebase = (unsigned long)
  1429. __alloc_bootmem(size, 1 << fls(size), 0);
  1430. } else {
  1431. ebase = CKSEG0;
  1432. if (cpu_has_mips_r2)
  1433. ebase += (read_c0_ebase() & 0x3ffff000);
  1434. }
  1435. per_cpu_trap_init();
  1436. /*
  1437. * Copy the generic exception handlers to their final destination.
  1438. * This will be overriden later as suitable for a particular
  1439. * configuration.
  1440. */
  1441. set_handler(0x180, &except_vec3_generic, 0x80);
  1442. /*
  1443. * Setup default vectors
  1444. */
  1445. for (i = 0; i <= 31; i++)
  1446. set_except_vector(i, handle_reserved);
  1447. /*
  1448. * Copy the EJTAG debug exception vector handler code to it's final
  1449. * destination.
  1450. */
  1451. if (cpu_has_ejtag && board_ejtag_handler_setup)
  1452. board_ejtag_handler_setup();
  1453. /*
  1454. * Only some CPUs have the watch exceptions.
  1455. */
  1456. if (cpu_has_watch)
  1457. set_except_vector(23, handle_watch);
  1458. /*
  1459. * Initialise interrupt handlers
  1460. */
  1461. if (cpu_has_veic || cpu_has_vint) {
  1462. int nvec = cpu_has_veic ? 64 : 8;
  1463. for (i = 0; i < nvec; i++)
  1464. set_vi_handler(i, NULL);
  1465. }
  1466. else if (cpu_has_divec)
  1467. set_handler(0x200, &except_vec4, 0x8);
  1468. /*
  1469. * Some CPUs can enable/disable for cache parity detection, but does
  1470. * it different ways.
  1471. */
  1472. parity_protection_init();
  1473. /*
  1474. * The Data Bus Errors / Instruction Bus Errors are signaled
  1475. * by external hardware. Therefore these two exceptions
  1476. * may have board specific handlers.
  1477. */
  1478. if (board_be_init)
  1479. board_be_init();
  1480. set_except_vector(0, rollback ? rollback_handle_int : handle_int);
  1481. set_except_vector(1, handle_tlbm);
  1482. set_except_vector(2, handle_tlbl);
  1483. set_except_vector(3, handle_tlbs);
  1484. set_except_vector(4, handle_adel);
  1485. set_except_vector(5, handle_ades);
  1486. set_except_vector(6, handle_ibe);
  1487. set_except_vector(7, handle_dbe);
  1488. set_except_vector(8, handle_sys);
  1489. set_except_vector(9, handle_bp);
  1490. set_except_vector(10, rdhwr_noopt ? handle_ri :
  1491. (cpu_has_vtag_icache ?
  1492. handle_ri_rdhwr_vivt : handle_ri_rdhwr));
  1493. set_except_vector(11, handle_cpu);
  1494. set_except_vector(12, handle_ov);
  1495. set_except_vector(13, handle_tr);
  1496. if (current_cpu_type() == CPU_R6000 ||
  1497. current_cpu_type() == CPU_R6000A) {
  1498. /*
  1499. * The R6000 is the only R-series CPU that features a machine
  1500. * check exception (similar to the R4000 cache error) and
  1501. * unaligned ldc1/sdc1 exception. The handlers have not been
  1502. * written yet. Well, anyway there is no R6000 machine on the
  1503. * current list of targets for Linux/MIPS.
  1504. * (Duh, crap, there is someone with a triple R6k machine)
  1505. */
  1506. //set_except_vector(14, handle_mc);
  1507. //set_except_vector(15, handle_ndc);
  1508. }
  1509. if (board_nmi_handler_setup)
  1510. board_nmi_handler_setup();
  1511. if (cpu_has_fpu && !cpu_has_nofpuex)
  1512. set_except_vector(15, handle_fpe);
  1513. set_except_vector(22, handle_mdmx);
  1514. if (cpu_has_mcheck)
  1515. set_except_vector(24, handle_mcheck);
  1516. if (cpu_has_mipsmt)
  1517. set_except_vector(25, handle_mt);
  1518. set_except_vector(26, handle_dsp);
  1519. if (cpu_has_vce)
  1520. /* Special exception: R4[04]00 uses also the divec space. */
  1521. memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
  1522. else if (cpu_has_4kex)
  1523. memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
  1524. else
  1525. memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
  1526. local_flush_icache_range(ebase, ebase + 0x400);
  1527. flush_tlb_handlers();
  1528. sort_extable(__start___dbe_table, __stop___dbe_table);
  1529. cu2_notifier(default_cu2_call, 0x80000000); /* Run last */
  1530. }