traps.c 26 KB

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