traps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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 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 Maciej W. Rozycki
  13. */
  14. #include <linux/config.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/smp.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/kallsyms.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/branch.h>
  25. #include <asm/break.h>
  26. #include <asm/cpu.h>
  27. #include <asm/fpu.h>
  28. #include <asm/module.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/sections.h>
  32. #include <asm/system.h>
  33. #include <asm/tlbdebug.h>
  34. #include <asm/traps.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/watch.h>
  38. #include <asm/types.h>
  39. extern asmlinkage void handle_tlbm(void);
  40. extern asmlinkage void handle_tlbl(void);
  41. extern asmlinkage void handle_tlbs(void);
  42. extern asmlinkage void handle_adel(void);
  43. extern asmlinkage void handle_ades(void);
  44. extern asmlinkage void handle_ibe(void);
  45. extern asmlinkage void handle_dbe(void);
  46. extern asmlinkage void handle_sys(void);
  47. extern asmlinkage void handle_bp(void);
  48. extern asmlinkage void handle_ri(void);
  49. extern asmlinkage void handle_cpu(void);
  50. extern asmlinkage void handle_ov(void);
  51. extern asmlinkage void handle_tr(void);
  52. extern asmlinkage void handle_fpe(void);
  53. extern asmlinkage void handle_mdmx(void);
  54. extern asmlinkage void handle_watch(void);
  55. extern asmlinkage void handle_mcheck(void);
  56. extern asmlinkage void handle_reserved(void);
  57. extern int fpu_emulator_cop1Handler(int xcptno, struct pt_regs *xcp,
  58. struct mips_fpu_soft_struct *ctx);
  59. void (*board_be_init)(void);
  60. int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
  61. /*
  62. * These constant is for searching for possible module text segments.
  63. * MODULE_RANGE is a guess of how much space is likely to be vmalloced.
  64. */
  65. #define MODULE_RANGE (8*1024*1024)
  66. /*
  67. * This routine abuses get_user()/put_user() to reference pointers
  68. * with at least a bit of error checking ...
  69. */
  70. void show_stack(struct task_struct *task, unsigned long *sp)
  71. {
  72. const int field = 2 * sizeof(unsigned long);
  73. long stackdata;
  74. int i;
  75. if (!sp) {
  76. if (task && task != current)
  77. sp = (unsigned long *) task->thread.reg29;
  78. else
  79. sp = (unsigned long *) &sp;
  80. }
  81. printk("Stack :");
  82. i = 0;
  83. while ((unsigned long) sp & (PAGE_SIZE - 1)) {
  84. if (i && ((i % (64 / field)) == 0))
  85. printk("\n ");
  86. if (i > 39) {
  87. printk(" ...");
  88. break;
  89. }
  90. if (__get_user(stackdata, sp++)) {
  91. printk(" (Bad stack address)");
  92. break;
  93. }
  94. printk(" %0*lx", field, stackdata);
  95. i++;
  96. }
  97. printk("\n");
  98. }
  99. void show_trace(struct task_struct *task, unsigned long *stack)
  100. {
  101. const int field = 2 * sizeof(unsigned long);
  102. unsigned long addr;
  103. if (!stack) {
  104. if (task && task != current)
  105. stack = (unsigned long *) task->thread.reg29;
  106. else
  107. stack = (unsigned long *) &stack;
  108. }
  109. printk("Call Trace:");
  110. #ifdef CONFIG_KALLSYMS
  111. printk("\n");
  112. #endif
  113. while (!kstack_end(stack)) {
  114. addr = *stack++;
  115. if (__kernel_text_address(addr)) {
  116. printk(" [<%0*lx>] ", field, addr);
  117. print_symbol("%s\n", addr);
  118. }
  119. }
  120. printk("\n");
  121. }
  122. /*
  123. * The architecture-independent dump_stack generator
  124. */
  125. void dump_stack(void)
  126. {
  127. unsigned long stack;
  128. show_trace(current, &stack);
  129. }
  130. EXPORT_SYMBOL(dump_stack);
  131. void show_code(unsigned int *pc)
  132. {
  133. long i;
  134. printk("\nCode:");
  135. for(i = -3 ; i < 6 ; i++) {
  136. unsigned int insn;
  137. if (__get_user(insn, pc + i)) {
  138. printk(" (Bad address in epc)\n");
  139. break;
  140. }
  141. printk("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
  142. }
  143. }
  144. void show_regs(struct pt_regs *regs)
  145. {
  146. const int field = 2 * sizeof(unsigned long);
  147. unsigned int cause = regs->cp0_cause;
  148. int i;
  149. printk("Cpu %d\n", smp_processor_id());
  150. /*
  151. * Saved main processor registers
  152. */
  153. for (i = 0; i < 32; ) {
  154. if ((i % 4) == 0)
  155. printk("$%2d :", i);
  156. if (i == 0)
  157. printk(" %0*lx", field, 0UL);
  158. else if (i == 26 || i == 27)
  159. printk(" %*s", field, "");
  160. else
  161. printk(" %0*lx", field, regs->regs[i]);
  162. i++;
  163. if ((i % 4) == 0)
  164. printk("\n");
  165. }
  166. printk("Hi : %0*lx\n", field, regs->hi);
  167. printk("Lo : %0*lx\n", field, regs->lo);
  168. /*
  169. * Saved cp0 registers
  170. */
  171. printk("epc : %0*lx ", field, regs->cp0_epc);
  172. print_symbol("%s ", regs->cp0_epc);
  173. printk(" %s\n", print_tainted());
  174. printk("ra : %0*lx ", field, regs->regs[31]);
  175. print_symbol("%s\n", regs->regs[31]);
  176. printk("Status: %08x ", (uint32_t) regs->cp0_status);
  177. if (regs->cp0_status & ST0_KX)
  178. printk("KX ");
  179. if (regs->cp0_status & ST0_SX)
  180. printk("SX ");
  181. if (regs->cp0_status & ST0_UX)
  182. printk("UX ");
  183. switch (regs->cp0_status & ST0_KSU) {
  184. case KSU_USER:
  185. printk("USER ");
  186. break;
  187. case KSU_SUPERVISOR:
  188. printk("SUPERVISOR ");
  189. break;
  190. case KSU_KERNEL:
  191. printk("KERNEL ");
  192. break;
  193. default:
  194. printk("BAD_MODE ");
  195. break;
  196. }
  197. if (regs->cp0_status & ST0_ERL)
  198. printk("ERL ");
  199. if (regs->cp0_status & ST0_EXL)
  200. printk("EXL ");
  201. if (regs->cp0_status & ST0_IE)
  202. printk("IE ");
  203. printk("\n");
  204. printk("Cause : %08x\n", cause);
  205. cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  206. if (1 <= cause && cause <= 5)
  207. printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
  208. printk("PrId : %08x\n", read_c0_prid());
  209. }
  210. void show_registers(struct pt_regs *regs)
  211. {
  212. show_regs(regs);
  213. print_modules();
  214. printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
  215. current->comm, current->pid, current_thread_info(), current);
  216. show_stack(current, (long *) regs->regs[29]);
  217. show_trace(current, (long *) regs->regs[29]);
  218. show_code((unsigned int *) regs->cp0_epc);
  219. printk("\n");
  220. }
  221. static DEFINE_SPINLOCK(die_lock);
  222. NORET_TYPE void __die(const char * str, struct pt_regs * regs,
  223. const char * file, const char * func, unsigned long line)
  224. {
  225. static int die_counter;
  226. console_verbose();
  227. spin_lock_irq(&die_lock);
  228. printk("%s", str);
  229. if (file && func)
  230. printk(" in %s:%s, line %ld", file, func, line);
  231. printk("[#%d]:\n", ++die_counter);
  232. show_registers(regs);
  233. spin_unlock_irq(&die_lock);
  234. do_exit(SIGSEGV);
  235. }
  236. void __die_if_kernel(const char * str, struct pt_regs * regs,
  237. const char * file, const char * func, unsigned long line)
  238. {
  239. if (!user_mode(regs))
  240. __die(str, regs, file, func, line);
  241. }
  242. extern const struct exception_table_entry __start___dbe_table[];
  243. extern const struct exception_table_entry __stop___dbe_table[];
  244. void __declare_dbe_table(void)
  245. {
  246. __asm__ __volatile__(
  247. ".section\t__dbe_table,\"a\"\n\t"
  248. ".previous"
  249. );
  250. }
  251. /* Given an address, look for it in the exception tables. */
  252. static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
  253. {
  254. const struct exception_table_entry *e;
  255. e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
  256. if (!e)
  257. e = search_module_dbetables(addr);
  258. return e;
  259. }
  260. asmlinkage void do_be(struct pt_regs *regs)
  261. {
  262. const int field = 2 * sizeof(unsigned long);
  263. const struct exception_table_entry *fixup = NULL;
  264. int data = regs->cp0_cause & 4;
  265. int action = MIPS_BE_FATAL;
  266. /* XXX For now. Fixme, this searches the wrong table ... */
  267. if (data && !user_mode(regs))
  268. fixup = search_dbe_tables(exception_epc(regs));
  269. if (fixup)
  270. action = MIPS_BE_FIXUP;
  271. if (board_be_handler)
  272. action = board_be_handler(regs, fixup != 0);
  273. switch (action) {
  274. case MIPS_BE_DISCARD:
  275. return;
  276. case MIPS_BE_FIXUP:
  277. if (fixup) {
  278. regs->cp0_epc = fixup->nextinsn;
  279. return;
  280. }
  281. break;
  282. default:
  283. break;
  284. }
  285. /*
  286. * Assume it would be too dangerous to continue ...
  287. */
  288. printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
  289. data ? "Data" : "Instruction",
  290. field, regs->cp0_epc, field, regs->regs[31]);
  291. die_if_kernel("Oops", regs);
  292. force_sig(SIGBUS, current);
  293. }
  294. static inline int get_insn_opcode(struct pt_regs *regs, unsigned int *opcode)
  295. {
  296. unsigned int *epc;
  297. epc = (unsigned int *) regs->cp0_epc +
  298. ((regs->cp0_cause & CAUSEF_BD) != 0);
  299. if (!get_user(*opcode, epc))
  300. return 0;
  301. force_sig(SIGSEGV, current);
  302. return 1;
  303. }
  304. /*
  305. * ll/sc emulation
  306. */
  307. #define OPCODE 0xfc000000
  308. #define BASE 0x03e00000
  309. #define RT 0x001f0000
  310. #define OFFSET 0x0000ffff
  311. #define LL 0xc0000000
  312. #define SC 0xe0000000
  313. /*
  314. * The ll_bit is cleared by r*_switch.S
  315. */
  316. unsigned long ll_bit;
  317. static struct task_struct *ll_task = NULL;
  318. static inline void simulate_ll(struct pt_regs *regs, unsigned int opcode)
  319. {
  320. unsigned long value, *vaddr;
  321. long offset;
  322. int signal = 0;
  323. /*
  324. * analyse the ll instruction that just caused a ri exception
  325. * and put the referenced address to addr.
  326. */
  327. /* sign extend offset */
  328. offset = opcode & OFFSET;
  329. offset <<= 16;
  330. offset >>= 16;
  331. vaddr = (unsigned long *)((long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  332. if ((unsigned long)vaddr & 3) {
  333. signal = SIGBUS;
  334. goto sig;
  335. }
  336. if (get_user(value, vaddr)) {
  337. signal = SIGSEGV;
  338. goto sig;
  339. }
  340. preempt_disable();
  341. if (ll_task == NULL || ll_task == current) {
  342. ll_bit = 1;
  343. } else {
  344. ll_bit = 0;
  345. }
  346. ll_task = current;
  347. preempt_enable();
  348. regs->regs[(opcode & RT) >> 16] = value;
  349. compute_return_epc(regs);
  350. return;
  351. sig:
  352. force_sig(signal, current);
  353. }
  354. static inline void simulate_sc(struct pt_regs *regs, unsigned int opcode)
  355. {
  356. unsigned long *vaddr, reg;
  357. long offset;
  358. int signal = 0;
  359. /*
  360. * analyse the sc instruction that just caused a ri exception
  361. * and put the referenced address to addr.
  362. */
  363. /* sign extend offset */
  364. offset = opcode & OFFSET;
  365. offset <<= 16;
  366. offset >>= 16;
  367. vaddr = (unsigned long *)((long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  368. reg = (opcode & RT) >> 16;
  369. if ((unsigned long)vaddr & 3) {
  370. signal = SIGBUS;
  371. goto sig;
  372. }
  373. preempt_disable();
  374. if (ll_bit == 0 || ll_task != current) {
  375. regs->regs[reg] = 0;
  376. preempt_enable();
  377. compute_return_epc(regs);
  378. return;
  379. }
  380. preempt_enable();
  381. if (put_user(regs->regs[reg], vaddr)) {
  382. signal = SIGSEGV;
  383. goto sig;
  384. }
  385. regs->regs[reg] = 1;
  386. compute_return_epc(regs);
  387. return;
  388. sig:
  389. force_sig(signal, current);
  390. }
  391. /*
  392. * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
  393. * opcodes are supposed to result in coprocessor unusable exceptions if
  394. * executed on ll/sc-less processors. That's the theory. In practice a
  395. * few processors such as NEC's VR4100 throw reserved instruction exceptions
  396. * instead, so we're doing the emulation thing in both exception handlers.
  397. */
  398. static inline int simulate_llsc(struct pt_regs *regs)
  399. {
  400. unsigned int opcode;
  401. if (unlikely(get_insn_opcode(regs, &opcode)))
  402. return -EFAULT;
  403. if ((opcode & OPCODE) == LL) {
  404. simulate_ll(regs, opcode);
  405. return 0;
  406. }
  407. if ((opcode & OPCODE) == SC) {
  408. simulate_sc(regs, opcode);
  409. return 0;
  410. }
  411. return -EFAULT; /* Strange things going on ... */
  412. }
  413. asmlinkage void do_ov(struct pt_regs *regs)
  414. {
  415. siginfo_t info;
  416. info.si_code = FPE_INTOVF;
  417. info.si_signo = SIGFPE;
  418. info.si_errno = 0;
  419. info.si_addr = (void *)regs->cp0_epc;
  420. force_sig_info(SIGFPE, &info, current);
  421. }
  422. /*
  423. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  424. */
  425. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  426. {
  427. if (fcr31 & FPU_CSR_UNI_X) {
  428. int sig;
  429. preempt_disable();
  430. /*
  431. * Unimplemented operation exception. If we've got the full
  432. * software emulator on-board, let's use it...
  433. *
  434. * Force FPU to dump state into task/thread context. We're
  435. * moving a lot of data here for what is probably a single
  436. * instruction, but the alternative is to pre-decode the FP
  437. * register operands before invoking the emulator, which seems
  438. * a bit extreme for what should be an infrequent event.
  439. */
  440. save_fp(current);
  441. /* Run the emulator */
  442. sig = fpu_emulator_cop1Handler (0, regs,
  443. &current->thread.fpu.soft);
  444. /*
  445. * We can't allow the emulated instruction to leave any of
  446. * the cause bit set in $fcr31.
  447. */
  448. current->thread.fpu.soft.fcr31 &= ~FPU_CSR_ALL_X;
  449. /* Restore the hardware register state */
  450. restore_fp(current);
  451. preempt_enable();
  452. /* If something went wrong, signal */
  453. if (sig)
  454. force_sig(sig, current);
  455. return;
  456. }
  457. force_sig(SIGFPE, current);
  458. }
  459. asmlinkage void do_bp(struct pt_regs *regs)
  460. {
  461. unsigned int opcode, bcode;
  462. siginfo_t info;
  463. die_if_kernel("Break instruction in kernel code", regs);
  464. if (get_insn_opcode(regs, &opcode))
  465. return;
  466. /*
  467. * There is the ancient bug in the MIPS assemblers that the break
  468. * code starts left to bit 16 instead to bit 6 in the opcode.
  469. * Gas is bug-compatible, but not always, grrr...
  470. * We handle both cases with a simple heuristics. --macro
  471. */
  472. bcode = ((opcode >> 6) & ((1 << 20) - 1));
  473. if (bcode < (1 << 10))
  474. bcode <<= 10;
  475. /*
  476. * (A short test says that IRIX 5.3 sends SIGTRAP for all break
  477. * insns, even for break codes that indicate arithmetic failures.
  478. * Weird ...)
  479. * But should we continue the brokenness??? --macro
  480. */
  481. switch (bcode) {
  482. case BRK_OVERFLOW << 10:
  483. case BRK_DIVZERO << 10:
  484. if (bcode == (BRK_DIVZERO << 10))
  485. info.si_code = FPE_INTDIV;
  486. else
  487. info.si_code = FPE_INTOVF;
  488. info.si_signo = SIGFPE;
  489. info.si_errno = 0;
  490. info.si_addr = (void *)regs->cp0_epc;
  491. force_sig_info(SIGFPE, &info, current);
  492. break;
  493. default:
  494. force_sig(SIGTRAP, current);
  495. }
  496. }
  497. asmlinkage void do_tr(struct pt_regs *regs)
  498. {
  499. unsigned int opcode, tcode = 0;
  500. siginfo_t info;
  501. die_if_kernel("Trap instruction in kernel code", regs);
  502. if (get_insn_opcode(regs, &opcode))
  503. return;
  504. /* Immediate versions don't provide a code. */
  505. if (!(opcode & OPCODE))
  506. tcode = ((opcode >> 6) & ((1 << 10) - 1));
  507. /*
  508. * (A short test says that IRIX 5.3 sends SIGTRAP for all trap
  509. * insns, even for trap codes that indicate arithmetic failures.
  510. * Weird ...)
  511. * But should we continue the brokenness??? --macro
  512. */
  513. switch (tcode) {
  514. case BRK_OVERFLOW:
  515. case BRK_DIVZERO:
  516. if (tcode == BRK_DIVZERO)
  517. info.si_code = FPE_INTDIV;
  518. else
  519. info.si_code = FPE_INTOVF;
  520. info.si_signo = SIGFPE;
  521. info.si_errno = 0;
  522. info.si_addr = (void *)regs->cp0_epc;
  523. force_sig_info(SIGFPE, &info, current);
  524. break;
  525. default:
  526. force_sig(SIGTRAP, current);
  527. }
  528. }
  529. asmlinkage void do_ri(struct pt_regs *regs)
  530. {
  531. die_if_kernel("Reserved instruction in kernel code", regs);
  532. if (!cpu_has_llsc)
  533. if (!simulate_llsc(regs))
  534. return;
  535. force_sig(SIGILL, current);
  536. }
  537. asmlinkage void do_cpu(struct pt_regs *regs)
  538. {
  539. unsigned int cpid;
  540. die_if_kernel("do_cpu invoked from kernel context!", regs);
  541. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  542. switch (cpid) {
  543. case 0:
  544. if (cpu_has_llsc)
  545. break;
  546. if (!simulate_llsc(regs))
  547. return;
  548. break;
  549. case 1:
  550. preempt_disable();
  551. own_fpu();
  552. if (used_math()) { /* Using the FPU again. */
  553. restore_fp(current);
  554. } else { /* First time FPU user. */
  555. init_fpu();
  556. set_used_math();
  557. }
  558. if (!cpu_has_fpu) {
  559. int sig = fpu_emulator_cop1Handler(0, regs,
  560. &current->thread.fpu.soft);
  561. if (sig)
  562. force_sig(sig, current);
  563. }
  564. preempt_enable();
  565. return;
  566. case 2:
  567. case 3:
  568. break;
  569. }
  570. force_sig(SIGILL, current);
  571. }
  572. asmlinkage void do_mdmx(struct pt_regs *regs)
  573. {
  574. force_sig(SIGILL, current);
  575. }
  576. asmlinkage void do_watch(struct pt_regs *regs)
  577. {
  578. /*
  579. * We use the watch exception where available to detect stack
  580. * overflows.
  581. */
  582. dump_tlb_all();
  583. show_regs(regs);
  584. panic("Caught WATCH exception - probably caused by stack overflow.");
  585. }
  586. asmlinkage void do_mcheck(struct pt_regs *regs)
  587. {
  588. show_regs(regs);
  589. dump_tlb_all();
  590. /*
  591. * Some chips may have other causes of machine check (e.g. SB1
  592. * graduation timer)
  593. */
  594. panic("Caught Machine Check exception - %scaused by multiple "
  595. "matching entries in the TLB.",
  596. (regs->cp0_status & ST0_TS) ? "" : "not ");
  597. }
  598. asmlinkage void do_reserved(struct pt_regs *regs)
  599. {
  600. /*
  601. * Game over - no way to handle this if it ever occurs. Most probably
  602. * caused by a new unknown cpu type or after another deadly
  603. * hard/software error.
  604. */
  605. show_regs(regs);
  606. panic("Caught reserved exception %ld - should not happen.",
  607. (regs->cp0_cause & 0x7f) >> 2);
  608. }
  609. /*
  610. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  611. * it different ways.
  612. */
  613. static inline void parity_protection_init(void)
  614. {
  615. switch (current_cpu_data.cputype) {
  616. case CPU_24K:
  617. /* 24K cache parity not currently implemented in FPGA */
  618. printk(KERN_INFO "Disable cache parity protection for "
  619. "MIPS 24K CPU.\n");
  620. write_c0_ecc(read_c0_ecc() & ~0x80000000);
  621. break;
  622. case CPU_5KC:
  623. /* Set the PE bit (bit 31) in the c0_ecc register. */
  624. printk(KERN_INFO "Enable cache parity protection for "
  625. "MIPS 5KC/24K CPUs.\n");
  626. write_c0_ecc(read_c0_ecc() | 0x80000000);
  627. break;
  628. case CPU_20KC:
  629. case CPU_25KF:
  630. /* Clear the DE bit (bit 16) in the c0_status register. */
  631. printk(KERN_INFO "Enable cache parity protection for "
  632. "MIPS 20KC/25KF CPUs.\n");
  633. clear_c0_status(ST0_DE);
  634. break;
  635. default:
  636. break;
  637. }
  638. }
  639. asmlinkage void cache_parity_error(void)
  640. {
  641. const int field = 2 * sizeof(unsigned long);
  642. unsigned int reg_val;
  643. /* For the moment, report the problem and hang. */
  644. printk("Cache error exception:\n");
  645. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  646. reg_val = read_c0_cacheerr();
  647. printk("c0_cacheerr == %08x\n", reg_val);
  648. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  649. reg_val & (1<<30) ? "secondary" : "primary",
  650. reg_val & (1<<31) ? "data" : "insn");
  651. printk("Error bits: %s%s%s%s%s%s%s\n",
  652. reg_val & (1<<29) ? "ED " : "",
  653. reg_val & (1<<28) ? "ET " : "",
  654. reg_val & (1<<26) ? "EE " : "",
  655. reg_val & (1<<25) ? "EB " : "",
  656. reg_val & (1<<24) ? "EI " : "",
  657. reg_val & (1<<23) ? "E1 " : "",
  658. reg_val & (1<<22) ? "E0 " : "");
  659. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  660. #if defined(CONFIG_CPU_MIPS32) || defined (CONFIG_CPU_MIPS64)
  661. if (reg_val & (1<<22))
  662. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  663. if (reg_val & (1<<23))
  664. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  665. #endif
  666. panic("Can't handle the cache error!");
  667. }
  668. /*
  669. * SDBBP EJTAG debug exception handler.
  670. * We skip the instruction and return to the next instruction.
  671. */
  672. void ejtag_exception_handler(struct pt_regs *regs)
  673. {
  674. const int field = 2 * sizeof(unsigned long);
  675. unsigned long depc, old_epc;
  676. unsigned int debug;
  677. printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  678. depc = read_c0_depc();
  679. debug = read_c0_debug();
  680. printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  681. if (debug & 0x80000000) {
  682. /*
  683. * In branch delay slot.
  684. * We cheat a little bit here and use EPC to calculate the
  685. * debug return address (DEPC). EPC is restored after the
  686. * calculation.
  687. */
  688. old_epc = regs->cp0_epc;
  689. regs->cp0_epc = depc;
  690. __compute_return_epc(regs);
  691. depc = regs->cp0_epc;
  692. regs->cp0_epc = old_epc;
  693. } else
  694. depc += 4;
  695. write_c0_depc(depc);
  696. #if 0
  697. printk("\n\n----- Enable EJTAG single stepping ----\n\n");
  698. write_c0_debug(debug | 0x100);
  699. #endif
  700. }
  701. /*
  702. * NMI exception handler.
  703. */
  704. void nmi_exception_handler(struct pt_regs *regs)
  705. {
  706. printk("NMI taken!!!!\n");
  707. die("NMI", regs);
  708. while(1) ;
  709. }
  710. unsigned long exception_handlers[32];
  711. /*
  712. * As a side effect of the way this is implemented we're limited
  713. * to interrupt handlers in the address range from
  714. * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
  715. */
  716. void *set_except_vector(int n, void *addr)
  717. {
  718. unsigned long handler = (unsigned long) addr;
  719. unsigned long old_handler = exception_handlers[n];
  720. exception_handlers[n] = handler;
  721. if (n == 0 && cpu_has_divec) {
  722. *(volatile u32 *)(CAC_BASE + 0x200) = 0x08000000 |
  723. (0x03ffffff & (handler >> 2));
  724. flush_icache_range(CAC_BASE + 0x200, CAC_BASE + 0x204);
  725. }
  726. return (void *)old_handler;
  727. }
  728. /*
  729. * This is used by native signal handling
  730. */
  731. asmlinkage int (*save_fp_context)(struct sigcontext *sc);
  732. asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
  733. extern asmlinkage int _save_fp_context(struct sigcontext *sc);
  734. extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
  735. extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
  736. extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
  737. static inline void signal_init(void)
  738. {
  739. if (cpu_has_fpu) {
  740. save_fp_context = _save_fp_context;
  741. restore_fp_context = _restore_fp_context;
  742. } else {
  743. save_fp_context = fpu_emulator_save_context;
  744. restore_fp_context = fpu_emulator_restore_context;
  745. }
  746. }
  747. #ifdef CONFIG_MIPS32_COMPAT
  748. /*
  749. * This is used by 32-bit signal stuff on the 64-bit kernel
  750. */
  751. asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
  752. asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
  753. extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
  754. extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
  755. extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
  756. extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
  757. static inline void signal32_init(void)
  758. {
  759. if (cpu_has_fpu) {
  760. save_fp_context32 = _save_fp_context32;
  761. restore_fp_context32 = _restore_fp_context32;
  762. } else {
  763. save_fp_context32 = fpu_emulator_save_context32;
  764. restore_fp_context32 = fpu_emulator_restore_context32;
  765. }
  766. }
  767. #endif
  768. extern void cpu_cache_init(void);
  769. extern void tlb_init(void);
  770. void __init per_cpu_trap_init(void)
  771. {
  772. unsigned int cpu = smp_processor_id();
  773. unsigned int status_set = ST0_CU0;
  774. /*
  775. * Disable coprocessors and select 32-bit or 64-bit addressing
  776. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  777. * flag that some firmware may have left set and the TS bit (for
  778. * IP27). Set XX for ISA IV code to work.
  779. */
  780. #ifdef CONFIG_64BIT
  781. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  782. #endif
  783. if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
  784. status_set |= ST0_XX;
  785. change_c0_status(ST0_CU|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  786. status_set);
  787. /*
  788. * Some MIPS CPUs have a dedicated interrupt vector which reduces the
  789. * interrupt processing overhead. Use it where available.
  790. */
  791. if (cpu_has_divec)
  792. set_c0_cause(CAUSEF_IV);
  793. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  794. TLBMISS_HANDLER_SETUP();
  795. atomic_inc(&init_mm.mm_count);
  796. current->active_mm = &init_mm;
  797. BUG_ON(current->mm);
  798. enter_lazy_tlb(&init_mm, current);
  799. cpu_cache_init();
  800. tlb_init();
  801. }
  802. void __init trap_init(void)
  803. {
  804. extern char except_vec3_generic, except_vec3_r4000;
  805. extern char except_vec_ejtag_debug;
  806. extern char except_vec4;
  807. unsigned long i;
  808. per_cpu_trap_init();
  809. /*
  810. * Copy the generic exception handlers to their final destination.
  811. * This will be overriden later as suitable for a particular
  812. * configuration.
  813. */
  814. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  815. /*
  816. * Setup default vectors
  817. */
  818. for (i = 0; i <= 31; i++)
  819. set_except_vector(i, handle_reserved);
  820. /*
  821. * Copy the EJTAG debug exception vector handler code to it's final
  822. * destination.
  823. */
  824. if (cpu_has_ejtag)
  825. memcpy((void *)(CAC_BASE + 0x300), &except_vec_ejtag_debug, 0x80);
  826. /*
  827. * Only some CPUs have the watch exceptions.
  828. */
  829. if (cpu_has_watch)
  830. set_except_vector(23, handle_watch);
  831. /*
  832. * Some MIPS CPUs have a dedicated interrupt vector which reduces the
  833. * interrupt processing overhead. Use it where available.
  834. */
  835. if (cpu_has_divec)
  836. memcpy((void *)(CAC_BASE + 0x200), &except_vec4, 0x8);
  837. /*
  838. * Some CPUs can enable/disable for cache parity detection, but does
  839. * it different ways.
  840. */
  841. parity_protection_init();
  842. /*
  843. * The Data Bus Errors / Instruction Bus Errors are signaled
  844. * by external hardware. Therefore these two exceptions
  845. * may have board specific handlers.
  846. */
  847. if (board_be_init)
  848. board_be_init();
  849. set_except_vector(1, handle_tlbm);
  850. set_except_vector(2, handle_tlbl);
  851. set_except_vector(3, handle_tlbs);
  852. set_except_vector(4, handle_adel);
  853. set_except_vector(5, handle_ades);
  854. set_except_vector(6, handle_ibe);
  855. set_except_vector(7, handle_dbe);
  856. set_except_vector(8, handle_sys);
  857. set_except_vector(9, handle_bp);
  858. set_except_vector(10, handle_ri);
  859. set_except_vector(11, handle_cpu);
  860. set_except_vector(12, handle_ov);
  861. set_except_vector(13, handle_tr);
  862. set_except_vector(22, handle_mdmx);
  863. if (cpu_has_fpu && !cpu_has_nofpuex)
  864. set_except_vector(15, handle_fpe);
  865. if (cpu_has_mcheck)
  866. set_except_vector(24, handle_mcheck);
  867. if (cpu_has_vce)
  868. /* Special exception: R4[04]00 uses also the divec space. */
  869. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
  870. else if (cpu_has_4kex)
  871. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  872. else
  873. memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
  874. if (current_cpu_data.cputype == CPU_R6000 ||
  875. current_cpu_data.cputype == CPU_R6000A) {
  876. /*
  877. * The R6000 is the only R-series CPU that features a machine
  878. * check exception (similar to the R4000 cache error) and
  879. * unaligned ldc1/sdc1 exception. The handlers have not been
  880. * written yet. Well, anyway there is no R6000 machine on the
  881. * current list of targets for Linux/MIPS.
  882. * (Duh, crap, there is someone with a triple R6k machine)
  883. */
  884. //set_except_vector(14, handle_mc);
  885. //set_except_vector(15, handle_ndc);
  886. }
  887. signal_init();
  888. #ifdef CONFIG_MIPS32_COMPAT
  889. signal32_init();
  890. #endif
  891. flush_icache_range(CAC_BASE, CAC_BASE + 0x400);
  892. }