traps.c 32 KB

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