traps.c 26 KB

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