traps.c 27 KB

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