traps.c 27 KB

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