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 __user *epc;
  297. epc = (unsigned int __user *) 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, __user *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 __user *)
  332. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  333. if ((unsigned long)vaddr & 3) {
  334. signal = SIGBUS;
  335. goto sig;
  336. }
  337. if (get_user(value, vaddr)) {
  338. signal = SIGSEGV;
  339. goto sig;
  340. }
  341. preempt_disable();
  342. if (ll_task == NULL || ll_task == current) {
  343. ll_bit = 1;
  344. } else {
  345. ll_bit = 0;
  346. }
  347. ll_task = current;
  348. preempt_enable();
  349. compute_return_epc(regs);
  350. regs->regs[(opcode & RT) >> 16] = value;
  351. return;
  352. sig:
  353. force_sig(signal, current);
  354. }
  355. static inline void simulate_sc(struct pt_regs *regs, unsigned int opcode)
  356. {
  357. unsigned long __user *vaddr;
  358. unsigned long reg;
  359. long offset;
  360. int signal = 0;
  361. /*
  362. * analyse the sc instruction that just caused a ri exception
  363. * and put the referenced address to addr.
  364. */
  365. /* sign extend offset */
  366. offset = opcode & OFFSET;
  367. offset <<= 16;
  368. offset >>= 16;
  369. vaddr = (unsigned long __user *)
  370. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  371. reg = (opcode & RT) >> 16;
  372. if ((unsigned long)vaddr & 3) {
  373. signal = SIGBUS;
  374. goto sig;
  375. }
  376. preempt_disable();
  377. if (ll_bit == 0 || ll_task != current) {
  378. compute_return_epc(regs);
  379. regs->regs[reg] = 0;
  380. preempt_enable();
  381. return;
  382. }
  383. preempt_enable();
  384. if (put_user(regs->regs[reg], vaddr)) {
  385. signal = SIGSEGV;
  386. goto sig;
  387. }
  388. compute_return_epc(regs);
  389. regs->regs[reg] = 1;
  390. return;
  391. sig:
  392. force_sig(signal, current);
  393. }
  394. /*
  395. * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
  396. * opcodes are supposed to result in coprocessor unusable exceptions if
  397. * executed on ll/sc-less processors. That's the theory. In practice a
  398. * few processors such as NEC's VR4100 throw reserved instruction exceptions
  399. * instead, so we're doing the emulation thing in both exception handlers.
  400. */
  401. static inline int simulate_llsc(struct pt_regs *regs)
  402. {
  403. unsigned int opcode;
  404. if (unlikely(get_insn_opcode(regs, &opcode)))
  405. return -EFAULT;
  406. if ((opcode & OPCODE) == LL) {
  407. simulate_ll(regs, opcode);
  408. return 0;
  409. }
  410. if ((opcode & OPCODE) == SC) {
  411. simulate_sc(regs, opcode);
  412. return 0;
  413. }
  414. return -EFAULT; /* Strange things going on ... */
  415. }
  416. asmlinkage void do_ov(struct pt_regs *regs)
  417. {
  418. siginfo_t info;
  419. info.si_code = FPE_INTOVF;
  420. info.si_signo = SIGFPE;
  421. info.si_errno = 0;
  422. info.si_addr = (void __user *) regs->cp0_epc;
  423. force_sig_info(SIGFPE, &info, current);
  424. }
  425. /*
  426. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  427. */
  428. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  429. {
  430. if (fcr31 & FPU_CSR_UNI_X) {
  431. int sig;
  432. preempt_disable();
  433. /*
  434. * Unimplemented operation exception. If we've got the full
  435. * software emulator on-board, let's use it...
  436. *
  437. * Force FPU to dump state into task/thread context. We're
  438. * moving a lot of data here for what is probably a single
  439. * instruction, but the alternative is to pre-decode the FP
  440. * register operands before invoking the emulator, which seems
  441. * a bit extreme for what should be an infrequent event.
  442. */
  443. save_fp(current);
  444. /* Run the emulator */
  445. sig = fpu_emulator_cop1Handler (0, regs,
  446. &current->thread.fpu.soft);
  447. /*
  448. * We can't allow the emulated instruction to leave any of
  449. * the cause bit set in $fcr31.
  450. */
  451. current->thread.fpu.soft.fcr31 &= ~FPU_CSR_ALL_X;
  452. /* Restore the hardware register state */
  453. restore_fp(current);
  454. preempt_enable();
  455. /* If something went wrong, signal */
  456. if (sig)
  457. force_sig(sig, current);
  458. return;
  459. }
  460. force_sig(SIGFPE, current);
  461. }
  462. asmlinkage void do_bp(struct pt_regs *regs)
  463. {
  464. unsigned int opcode, bcode;
  465. siginfo_t info;
  466. die_if_kernel("Break instruction in kernel code", regs);
  467. if (get_insn_opcode(regs, &opcode))
  468. return;
  469. /*
  470. * There is the ancient bug in the MIPS assemblers that the break
  471. * code starts left to bit 16 instead to bit 6 in the opcode.
  472. * Gas is bug-compatible, but not always, grrr...
  473. * We handle both cases with a simple heuristics. --macro
  474. */
  475. bcode = ((opcode >> 6) & ((1 << 20) - 1));
  476. if (bcode < (1 << 10))
  477. bcode <<= 10;
  478. /*
  479. * (A short test says that IRIX 5.3 sends SIGTRAP for all break
  480. * insns, even for break codes that indicate arithmetic failures.
  481. * Weird ...)
  482. * But should we continue the brokenness??? --macro
  483. */
  484. switch (bcode) {
  485. case BRK_OVERFLOW << 10:
  486. case BRK_DIVZERO << 10:
  487. if (bcode == (BRK_DIVZERO << 10))
  488. info.si_code = FPE_INTDIV;
  489. else
  490. info.si_code = FPE_INTOVF;
  491. info.si_signo = SIGFPE;
  492. info.si_errno = 0;
  493. info.si_addr = (void __user *) regs->cp0_epc;
  494. force_sig_info(SIGFPE, &info, current);
  495. break;
  496. default:
  497. force_sig(SIGTRAP, current);
  498. }
  499. }
  500. asmlinkage void do_tr(struct pt_regs *regs)
  501. {
  502. unsigned int opcode, tcode = 0;
  503. siginfo_t info;
  504. die_if_kernel("Trap instruction in kernel code", regs);
  505. if (get_insn_opcode(regs, &opcode))
  506. return;
  507. /* Immediate versions don't provide a code. */
  508. if (!(opcode & OPCODE))
  509. tcode = ((opcode >> 6) & ((1 << 10) - 1));
  510. /*
  511. * (A short test says that IRIX 5.3 sends SIGTRAP for all trap
  512. * insns, even for trap codes that indicate arithmetic failures.
  513. * Weird ...)
  514. * But should we continue the brokenness??? --macro
  515. */
  516. switch (tcode) {
  517. case BRK_OVERFLOW:
  518. case BRK_DIVZERO:
  519. if (tcode == BRK_DIVZERO)
  520. info.si_code = FPE_INTDIV;
  521. else
  522. info.si_code = FPE_INTOVF;
  523. info.si_signo = SIGFPE;
  524. info.si_errno = 0;
  525. info.si_addr = (void __user *) regs->cp0_epc;
  526. force_sig_info(SIGFPE, &info, current);
  527. break;
  528. default:
  529. force_sig(SIGTRAP, current);
  530. }
  531. }
  532. asmlinkage void do_ri(struct pt_regs *regs)
  533. {
  534. die_if_kernel("Reserved instruction in kernel code", regs);
  535. if (!cpu_has_llsc)
  536. if (!simulate_llsc(regs))
  537. return;
  538. force_sig(SIGILL, current);
  539. }
  540. asmlinkage void do_cpu(struct pt_regs *regs)
  541. {
  542. unsigned int cpid;
  543. die_if_kernel("do_cpu invoked from kernel context!", regs);
  544. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  545. switch (cpid) {
  546. case 0:
  547. if (cpu_has_llsc)
  548. break;
  549. if (!simulate_llsc(regs))
  550. return;
  551. break;
  552. case 1:
  553. preempt_disable();
  554. own_fpu();
  555. if (used_math()) { /* Using the FPU again. */
  556. restore_fp(current);
  557. } else { /* First time FPU user. */
  558. init_fpu();
  559. set_used_math();
  560. }
  561. if (!cpu_has_fpu) {
  562. int sig = fpu_emulator_cop1Handler(0, regs,
  563. &current->thread.fpu.soft);
  564. if (sig)
  565. force_sig(sig, current);
  566. }
  567. preempt_enable();
  568. return;
  569. case 2:
  570. case 3:
  571. break;
  572. }
  573. force_sig(SIGILL, current);
  574. }
  575. asmlinkage void do_mdmx(struct pt_regs *regs)
  576. {
  577. force_sig(SIGILL, current);
  578. }
  579. asmlinkage void do_watch(struct pt_regs *regs)
  580. {
  581. /*
  582. * We use the watch exception where available to detect stack
  583. * overflows.
  584. */
  585. dump_tlb_all();
  586. show_regs(regs);
  587. panic("Caught WATCH exception - probably caused by stack overflow.");
  588. }
  589. asmlinkage void do_mcheck(struct pt_regs *regs)
  590. {
  591. show_regs(regs);
  592. dump_tlb_all();
  593. /*
  594. * Some chips may have other causes of machine check (e.g. SB1
  595. * graduation timer)
  596. */
  597. panic("Caught Machine Check exception - %scaused by multiple "
  598. "matching entries in the TLB.",
  599. (regs->cp0_status & ST0_TS) ? "" : "not ");
  600. }
  601. asmlinkage void do_reserved(struct pt_regs *regs)
  602. {
  603. /*
  604. * Game over - no way to handle this if it ever occurs. Most probably
  605. * caused by a new unknown cpu type or after another deadly
  606. * hard/software error.
  607. */
  608. show_regs(regs);
  609. panic("Caught reserved exception %ld - should not happen.",
  610. (regs->cp0_cause & 0x7f) >> 2);
  611. }
  612. /*
  613. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  614. * it different ways.
  615. */
  616. static inline void parity_protection_init(void)
  617. {
  618. switch (current_cpu_data.cputype) {
  619. case CPU_24K:
  620. case CPU_5KC:
  621. write_c0_ecc(0x80000000);
  622. back_to_back_c0_hazard();
  623. /* Set the PE bit (bit 31) in the c0_errctl register. */
  624. printk(KERN_INFO "Cache parity protection %sabled\n",
  625. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  626. break;
  627. case CPU_20KC:
  628. case CPU_25KF:
  629. /* Clear the DE bit (bit 16) in the c0_status register. */
  630. printk(KERN_INFO "Enable cache parity protection for "
  631. "MIPS 20KC/25KF CPUs.\n");
  632. clear_c0_status(ST0_DE);
  633. break;
  634. default:
  635. break;
  636. }
  637. }
  638. asmlinkage void cache_parity_error(void)
  639. {
  640. const int field = 2 * sizeof(unsigned long);
  641. unsigned int reg_val;
  642. /* For the moment, report the problem and hang. */
  643. printk("Cache error exception:\n");
  644. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  645. reg_val = read_c0_cacheerr();
  646. printk("c0_cacheerr == %08x\n", reg_val);
  647. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  648. reg_val & (1<<30) ? "secondary" : "primary",
  649. reg_val & (1<<31) ? "data" : "insn");
  650. printk("Error bits: %s%s%s%s%s%s%s\n",
  651. reg_val & (1<<29) ? "ED " : "",
  652. reg_val & (1<<28) ? "ET " : "",
  653. reg_val & (1<<26) ? "EE " : "",
  654. reg_val & (1<<25) ? "EB " : "",
  655. reg_val & (1<<24) ? "EI " : "",
  656. reg_val & (1<<23) ? "E1 " : "",
  657. reg_val & (1<<22) ? "E0 " : "");
  658. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  659. #if defined(CONFIG_CPU_MIPS32) || defined (CONFIG_CPU_MIPS64)
  660. if (reg_val & (1<<22))
  661. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  662. if (reg_val & (1<<23))
  663. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  664. #endif
  665. panic("Can't handle the cache error!");
  666. }
  667. /*
  668. * SDBBP EJTAG debug exception handler.
  669. * We skip the instruction and return to the next instruction.
  670. */
  671. void ejtag_exception_handler(struct pt_regs *regs)
  672. {
  673. const int field = 2 * sizeof(unsigned long);
  674. unsigned long depc, old_epc;
  675. unsigned int debug;
  676. printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  677. depc = read_c0_depc();
  678. debug = read_c0_debug();
  679. printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  680. if (debug & 0x80000000) {
  681. /*
  682. * In branch delay slot.
  683. * We cheat a little bit here and use EPC to calculate the
  684. * debug return address (DEPC). EPC is restored after the
  685. * calculation.
  686. */
  687. old_epc = regs->cp0_epc;
  688. regs->cp0_epc = depc;
  689. __compute_return_epc(regs);
  690. depc = regs->cp0_epc;
  691. regs->cp0_epc = old_epc;
  692. } else
  693. depc += 4;
  694. write_c0_depc(depc);
  695. #if 0
  696. printk("\n\n----- Enable EJTAG single stepping ----\n\n");
  697. write_c0_debug(debug | 0x100);
  698. #endif
  699. }
  700. /*
  701. * NMI exception handler.
  702. */
  703. void nmi_exception_handler(struct pt_regs *regs)
  704. {
  705. printk("NMI taken!!!!\n");
  706. die("NMI", regs);
  707. while(1) ;
  708. }
  709. unsigned long exception_handlers[32];
  710. /*
  711. * As a side effect of the way this is implemented we're limited
  712. * to interrupt handlers in the address range from
  713. * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
  714. */
  715. void *set_except_vector(int n, void *addr)
  716. {
  717. unsigned long handler = (unsigned long) addr;
  718. unsigned long old_handler = exception_handlers[n];
  719. exception_handlers[n] = handler;
  720. if (n == 0 && cpu_has_divec) {
  721. *(volatile u32 *)(CAC_BASE + 0x200) = 0x08000000 |
  722. (0x03ffffff & (handler >> 2));
  723. flush_icache_range(CAC_BASE + 0x200, CAC_BASE + 0x204);
  724. }
  725. return (void *)old_handler;
  726. }
  727. /*
  728. * This is used by native signal handling
  729. */
  730. asmlinkage int (*save_fp_context)(struct sigcontext *sc);
  731. asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
  732. extern asmlinkage int _save_fp_context(struct sigcontext *sc);
  733. extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
  734. extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
  735. extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
  736. static inline void signal_init(void)
  737. {
  738. if (cpu_has_fpu) {
  739. save_fp_context = _save_fp_context;
  740. restore_fp_context = _restore_fp_context;
  741. } else {
  742. save_fp_context = fpu_emulator_save_context;
  743. restore_fp_context = fpu_emulator_restore_context;
  744. }
  745. }
  746. #ifdef CONFIG_MIPS32_COMPAT
  747. /*
  748. * This is used by 32-bit signal stuff on the 64-bit kernel
  749. */
  750. asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
  751. asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
  752. extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
  753. extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
  754. extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
  755. extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
  756. static inline void signal32_init(void)
  757. {
  758. if (cpu_has_fpu) {
  759. save_fp_context32 = _save_fp_context32;
  760. restore_fp_context32 = _restore_fp_context32;
  761. } else {
  762. save_fp_context32 = fpu_emulator_save_context32;
  763. restore_fp_context32 = fpu_emulator_restore_context32;
  764. }
  765. }
  766. #endif
  767. extern void cpu_cache_init(void);
  768. extern void tlb_init(void);
  769. void __init per_cpu_trap_init(void)
  770. {
  771. unsigned int cpu = smp_processor_id();
  772. unsigned int status_set = ST0_CU0;
  773. /*
  774. * Disable coprocessors and select 32-bit or 64-bit addressing
  775. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  776. * flag that some firmware may have left set and the TS bit (for
  777. * IP27). Set XX for ISA IV code to work.
  778. */
  779. #ifdef CONFIG_64BIT
  780. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  781. #endif
  782. if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
  783. status_set |= ST0_XX;
  784. change_c0_status(ST0_CU|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  785. status_set);
  786. /*
  787. * Some MIPS CPUs have a dedicated interrupt vector which reduces the
  788. * interrupt processing overhead. Use it where available.
  789. */
  790. if (cpu_has_divec)
  791. set_c0_cause(CAUSEF_IV);
  792. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  793. TLBMISS_HANDLER_SETUP();
  794. atomic_inc(&init_mm.mm_count);
  795. current->active_mm = &init_mm;
  796. BUG_ON(current->mm);
  797. enter_lazy_tlb(&init_mm, current);
  798. cpu_cache_init();
  799. tlb_init();
  800. }
  801. void __init trap_init(void)
  802. {
  803. extern char except_vec3_generic, except_vec3_r4000;
  804. extern char except_vec_ejtag_debug;
  805. extern char except_vec4;
  806. unsigned long i;
  807. per_cpu_trap_init();
  808. /*
  809. * Copy the generic exception handlers to their final destination.
  810. * This will be overriden later as suitable for a particular
  811. * configuration.
  812. */
  813. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  814. /*
  815. * Setup default vectors
  816. */
  817. for (i = 0; i <= 31; i++)
  818. set_except_vector(i, handle_reserved);
  819. /*
  820. * Copy the EJTAG debug exception vector handler code to it's final
  821. * destination.
  822. */
  823. if (cpu_has_ejtag)
  824. memcpy((void *)(CAC_BASE + 0x300), &except_vec_ejtag_debug, 0x80);
  825. /*
  826. * Only some CPUs have the watch exceptions.
  827. */
  828. if (cpu_has_watch)
  829. set_except_vector(23, handle_watch);
  830. /*
  831. * Some MIPS CPUs have a dedicated interrupt vector which reduces the
  832. * interrupt processing overhead. Use it where available.
  833. */
  834. if (cpu_has_divec)
  835. memcpy((void *)(CAC_BASE + 0x200), &except_vec4, 0x8);
  836. /*
  837. * Some CPUs can enable/disable for cache parity detection, but does
  838. * it different ways.
  839. */
  840. parity_protection_init();
  841. /*
  842. * The Data Bus Errors / Instruction Bus Errors are signaled
  843. * by external hardware. Therefore these two exceptions
  844. * may have board specific handlers.
  845. */
  846. if (board_be_init)
  847. board_be_init();
  848. set_except_vector(1, handle_tlbm);
  849. set_except_vector(2, handle_tlbl);
  850. set_except_vector(3, handle_tlbs);
  851. set_except_vector(4, handle_adel);
  852. set_except_vector(5, handle_ades);
  853. set_except_vector(6, handle_ibe);
  854. set_except_vector(7, handle_dbe);
  855. set_except_vector(8, handle_sys);
  856. set_except_vector(9, handle_bp);
  857. set_except_vector(10, handle_ri);
  858. set_except_vector(11, handle_cpu);
  859. set_except_vector(12, handle_ov);
  860. set_except_vector(13, handle_tr);
  861. set_except_vector(22, handle_mdmx);
  862. if (cpu_has_fpu && !cpu_has_nofpuex)
  863. set_except_vector(15, handle_fpe);
  864. if (cpu_has_mcheck)
  865. set_except_vector(24, handle_mcheck);
  866. if (cpu_has_vce)
  867. /* Special exception: R4[04]00 uses also the divec space. */
  868. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
  869. else if (cpu_has_4kex)
  870. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  871. else
  872. memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
  873. if (current_cpu_data.cputype == CPU_R6000 ||
  874. current_cpu_data.cputype == CPU_R6000A) {
  875. /*
  876. * The R6000 is the only R-series CPU that features a machine
  877. * check exception (similar to the R4000 cache error) and
  878. * unaligned ldc1/sdc1 exception. The handlers have not been
  879. * written yet. Well, anyway there is no R6000 machine on the
  880. * current list of targets for Linux/MIPS.
  881. * (Duh, crap, there is someone with a triple R6k machine)
  882. */
  883. //set_except_vector(14, handle_mc);
  884. //set_except_vector(15, handle_ndc);
  885. }
  886. signal_init();
  887. #ifdef CONFIG_MIPS32_COMPAT
  888. signal32_init();
  889. #endif
  890. flush_icache_range(CAC_BASE, CAC_BASE + 0x400);
  891. }