traps.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. case CPU_5KC:
  618. write_c0_ecc(0x80000000);
  619. back_to_back_c0_hazard();
  620. /* Set the PE bit (bit 31) in the c0_errctl register. */
  621. printk(KERN_INFO "Cache parity protection %sabled\n",
  622. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  623. break;
  624. case CPU_20KC:
  625. case CPU_25KF:
  626. /* Clear the DE bit (bit 16) in the c0_status register. */
  627. printk(KERN_INFO "Enable cache parity protection for "
  628. "MIPS 20KC/25KF CPUs.\n");
  629. clear_c0_status(ST0_DE);
  630. break;
  631. default:
  632. break;
  633. }
  634. }
  635. asmlinkage void cache_parity_error(void)
  636. {
  637. const int field = 2 * sizeof(unsigned long);
  638. unsigned int reg_val;
  639. /* For the moment, report the problem and hang. */
  640. printk("Cache error exception:\n");
  641. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  642. reg_val = read_c0_cacheerr();
  643. printk("c0_cacheerr == %08x\n", reg_val);
  644. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  645. reg_val & (1<<30) ? "secondary" : "primary",
  646. reg_val & (1<<31) ? "data" : "insn");
  647. printk("Error bits: %s%s%s%s%s%s%s\n",
  648. reg_val & (1<<29) ? "ED " : "",
  649. reg_val & (1<<28) ? "ET " : "",
  650. reg_val & (1<<26) ? "EE " : "",
  651. reg_val & (1<<25) ? "EB " : "",
  652. reg_val & (1<<24) ? "EI " : "",
  653. reg_val & (1<<23) ? "E1 " : "",
  654. reg_val & (1<<22) ? "E0 " : "");
  655. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  656. #if defined(CONFIG_CPU_MIPS32) || defined (CONFIG_CPU_MIPS64)
  657. if (reg_val & (1<<22))
  658. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  659. if (reg_val & (1<<23))
  660. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  661. #endif
  662. panic("Can't handle the cache error!");
  663. }
  664. /*
  665. * SDBBP EJTAG debug exception handler.
  666. * We skip the instruction and return to the next instruction.
  667. */
  668. void ejtag_exception_handler(struct pt_regs *regs)
  669. {
  670. const int field = 2 * sizeof(unsigned long);
  671. unsigned long depc, old_epc;
  672. unsigned int debug;
  673. printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  674. depc = read_c0_depc();
  675. debug = read_c0_debug();
  676. printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  677. if (debug & 0x80000000) {
  678. /*
  679. * In branch delay slot.
  680. * We cheat a little bit here and use EPC to calculate the
  681. * debug return address (DEPC). EPC is restored after the
  682. * calculation.
  683. */
  684. old_epc = regs->cp0_epc;
  685. regs->cp0_epc = depc;
  686. __compute_return_epc(regs);
  687. depc = regs->cp0_epc;
  688. regs->cp0_epc = old_epc;
  689. } else
  690. depc += 4;
  691. write_c0_depc(depc);
  692. #if 0
  693. printk("\n\n----- Enable EJTAG single stepping ----\n\n");
  694. write_c0_debug(debug | 0x100);
  695. #endif
  696. }
  697. /*
  698. * NMI exception handler.
  699. */
  700. void nmi_exception_handler(struct pt_regs *regs)
  701. {
  702. printk("NMI taken!!!!\n");
  703. die("NMI", regs);
  704. while(1) ;
  705. }
  706. unsigned long exception_handlers[32];
  707. /*
  708. * As a side effect of the way this is implemented we're limited
  709. * to interrupt handlers in the address range from
  710. * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
  711. */
  712. void *set_except_vector(int n, void *addr)
  713. {
  714. unsigned long handler = (unsigned long) addr;
  715. unsigned long old_handler = exception_handlers[n];
  716. exception_handlers[n] = handler;
  717. if (n == 0 && cpu_has_divec) {
  718. *(volatile u32 *)(CAC_BASE + 0x200) = 0x08000000 |
  719. (0x03ffffff & (handler >> 2));
  720. flush_icache_range(CAC_BASE + 0x200, CAC_BASE + 0x204);
  721. }
  722. return (void *)old_handler;
  723. }
  724. /*
  725. * This is used by native signal handling
  726. */
  727. asmlinkage int (*save_fp_context)(struct sigcontext *sc);
  728. asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
  729. extern asmlinkage int _save_fp_context(struct sigcontext *sc);
  730. extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
  731. extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
  732. extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
  733. static inline void signal_init(void)
  734. {
  735. if (cpu_has_fpu) {
  736. save_fp_context = _save_fp_context;
  737. restore_fp_context = _restore_fp_context;
  738. } else {
  739. save_fp_context = fpu_emulator_save_context;
  740. restore_fp_context = fpu_emulator_restore_context;
  741. }
  742. }
  743. #ifdef CONFIG_MIPS32_COMPAT
  744. /*
  745. * This is used by 32-bit signal stuff on the 64-bit kernel
  746. */
  747. asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
  748. asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
  749. extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
  750. extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
  751. extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
  752. extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
  753. static inline void signal32_init(void)
  754. {
  755. if (cpu_has_fpu) {
  756. save_fp_context32 = _save_fp_context32;
  757. restore_fp_context32 = _restore_fp_context32;
  758. } else {
  759. save_fp_context32 = fpu_emulator_save_context32;
  760. restore_fp_context32 = fpu_emulator_restore_context32;
  761. }
  762. }
  763. #endif
  764. extern void cpu_cache_init(void);
  765. extern void tlb_init(void);
  766. void __init per_cpu_trap_init(void)
  767. {
  768. unsigned int cpu = smp_processor_id();
  769. unsigned int status_set = ST0_CU0;
  770. /*
  771. * Disable coprocessors and select 32-bit or 64-bit addressing
  772. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  773. * flag that some firmware may have left set and the TS bit (for
  774. * IP27). Set XX for ISA IV code to work.
  775. */
  776. #ifdef CONFIG_64BIT
  777. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  778. #endif
  779. if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
  780. status_set |= ST0_XX;
  781. change_c0_status(ST0_CU|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  782. status_set);
  783. /*
  784. * Some MIPS CPUs have a dedicated interrupt vector which reduces the
  785. * interrupt processing overhead. Use it where available.
  786. */
  787. if (cpu_has_divec)
  788. set_c0_cause(CAUSEF_IV);
  789. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  790. TLBMISS_HANDLER_SETUP();
  791. atomic_inc(&init_mm.mm_count);
  792. current->active_mm = &init_mm;
  793. BUG_ON(current->mm);
  794. enter_lazy_tlb(&init_mm, current);
  795. cpu_cache_init();
  796. tlb_init();
  797. }
  798. void __init trap_init(void)
  799. {
  800. extern char except_vec3_generic, except_vec3_r4000;
  801. extern char except_vec_ejtag_debug;
  802. extern char except_vec4;
  803. unsigned long i;
  804. per_cpu_trap_init();
  805. /*
  806. * Copy the generic exception handlers to their final destination.
  807. * This will be overriden later as suitable for a particular
  808. * configuration.
  809. */
  810. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  811. /*
  812. * Setup default vectors
  813. */
  814. for (i = 0; i <= 31; i++)
  815. set_except_vector(i, handle_reserved);
  816. /*
  817. * Copy the EJTAG debug exception vector handler code to it's final
  818. * destination.
  819. */
  820. if (cpu_has_ejtag)
  821. memcpy((void *)(CAC_BASE + 0x300), &except_vec_ejtag_debug, 0x80);
  822. /*
  823. * Only some CPUs have the watch exceptions.
  824. */
  825. if (cpu_has_watch)
  826. set_except_vector(23, handle_watch);
  827. /*
  828. * Some MIPS CPUs have a dedicated interrupt vector which reduces the
  829. * interrupt processing overhead. Use it where available.
  830. */
  831. if (cpu_has_divec)
  832. memcpy((void *)(CAC_BASE + 0x200), &except_vec4, 0x8);
  833. /*
  834. * Some CPUs can enable/disable for cache parity detection, but does
  835. * it different ways.
  836. */
  837. parity_protection_init();
  838. /*
  839. * The Data Bus Errors / Instruction Bus Errors are signaled
  840. * by external hardware. Therefore these two exceptions
  841. * may have board specific handlers.
  842. */
  843. if (board_be_init)
  844. board_be_init();
  845. set_except_vector(1, handle_tlbm);
  846. set_except_vector(2, handle_tlbl);
  847. set_except_vector(3, handle_tlbs);
  848. set_except_vector(4, handle_adel);
  849. set_except_vector(5, handle_ades);
  850. set_except_vector(6, handle_ibe);
  851. set_except_vector(7, handle_dbe);
  852. set_except_vector(8, handle_sys);
  853. set_except_vector(9, handle_bp);
  854. set_except_vector(10, handle_ri);
  855. set_except_vector(11, handle_cpu);
  856. set_except_vector(12, handle_ov);
  857. set_except_vector(13, handle_tr);
  858. set_except_vector(22, handle_mdmx);
  859. if (cpu_has_fpu && !cpu_has_nofpuex)
  860. set_except_vector(15, handle_fpe);
  861. if (cpu_has_mcheck)
  862. set_except_vector(24, handle_mcheck);
  863. if (cpu_has_vce)
  864. /* Special exception: R4[04]00 uses also the divec space. */
  865. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
  866. else if (cpu_has_4kex)
  867. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  868. else
  869. memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
  870. if (current_cpu_data.cputype == CPU_R6000 ||
  871. current_cpu_data.cputype == CPU_R6000A) {
  872. /*
  873. * The R6000 is the only R-series CPU that features a machine
  874. * check exception (similar to the R4000 cache error) and
  875. * unaligned ldc1/sdc1 exception. The handlers have not been
  876. * written yet. Well, anyway there is no R6000 machine on the
  877. * current list of targets for Linux/MIPS.
  878. * (Duh, crap, there is someone with a triple R6k machine)
  879. */
  880. //set_except_vector(14, handle_mc);
  881. //set_except_vector(15, handle_ndc);
  882. }
  883. signal_init();
  884. #ifdef CONFIG_MIPS32_COMPAT
  885. signal32_init();
  886. #endif
  887. flush_icache_range(CAC_BASE, CAC_BASE + 0x400);
  888. }