traps.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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, 06 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/mipsregs.h>
  31. #include <asm/mipsmtregs.h>
  32. #include <asm/module.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/ptrace.h>
  35. #include <asm/sections.h>
  36. #include <asm/system.h>
  37. #include <asm/tlbdebug.h>
  38. #include <asm/traps.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/watch.h>
  42. #include <asm/types.h>
  43. extern asmlinkage void handle_int(void);
  44. extern asmlinkage void handle_tlbm(void);
  45. extern asmlinkage void handle_tlbl(void);
  46. extern asmlinkage void handle_tlbs(void);
  47. extern asmlinkage void handle_adel(void);
  48. extern asmlinkage void handle_ades(void);
  49. extern asmlinkage void handle_ibe(void);
  50. extern asmlinkage void handle_dbe(void);
  51. extern asmlinkage void handle_sys(void);
  52. extern asmlinkage void handle_bp(void);
  53. extern asmlinkage void handle_ri(void);
  54. extern asmlinkage void handle_cpu(void);
  55. extern asmlinkage void handle_ov(void);
  56. extern asmlinkage void handle_tr(void);
  57. extern asmlinkage void handle_fpe(void);
  58. extern asmlinkage void handle_mdmx(void);
  59. extern asmlinkage void handle_watch(void);
  60. extern asmlinkage void handle_mt(void);
  61. extern asmlinkage void handle_dsp(void);
  62. extern asmlinkage void handle_mcheck(void);
  63. extern asmlinkage void handle_reserved(void);
  64. extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
  65. struct mips_fpu_soft_struct *ctx);
  66. void (*board_be_init)(void);
  67. int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
  68. void (*board_nmi_handler_setup)(void);
  69. void (*board_ejtag_handler_setup)(void);
  70. void (*board_bind_eic_interrupt)(int irq, int regset);
  71. /*
  72. * These constant is for searching for possible module text segments.
  73. * MODULE_RANGE is a guess of how much space is likely to be vmalloced.
  74. */
  75. #define MODULE_RANGE (8*1024*1024)
  76. /*
  77. * This routine abuses get_user()/put_user() to reference pointers
  78. * with at least a bit of error checking ...
  79. */
  80. void show_stack(struct task_struct *task, unsigned long *sp)
  81. {
  82. const int field = 2 * sizeof(unsigned long);
  83. long stackdata;
  84. int i;
  85. if (!sp) {
  86. if (task && task != current)
  87. sp = (unsigned long *) task->thread.reg29;
  88. else
  89. sp = (unsigned long *) &sp;
  90. }
  91. printk("Stack :");
  92. i = 0;
  93. while ((unsigned long) sp & (PAGE_SIZE - 1)) {
  94. if (i && ((i % (64 / field)) == 0))
  95. printk("\n ");
  96. if (i > 39) {
  97. printk(" ...");
  98. break;
  99. }
  100. if (__get_user(stackdata, sp++)) {
  101. printk(" (Bad stack address)");
  102. break;
  103. }
  104. printk(" %0*lx", field, stackdata);
  105. i++;
  106. }
  107. printk("\n");
  108. }
  109. void show_trace(struct task_struct *task, unsigned long *stack)
  110. {
  111. const int field = 2 * sizeof(unsigned long);
  112. unsigned long addr;
  113. if (!stack) {
  114. if (task && task != current)
  115. stack = (unsigned long *) task->thread.reg29;
  116. else
  117. stack = (unsigned long *) &stack;
  118. }
  119. printk("Call Trace:");
  120. #ifdef CONFIG_KALLSYMS
  121. printk("\n");
  122. #endif
  123. while (!kstack_end(stack)) {
  124. addr = *stack++;
  125. if (__kernel_text_address(addr)) {
  126. printk(" [<%0*lx>] ", field, addr);
  127. print_symbol("%s\n", addr);
  128. }
  129. }
  130. printk("\n");
  131. }
  132. /*
  133. * The architecture-independent dump_stack generator
  134. */
  135. void dump_stack(void)
  136. {
  137. unsigned long stack;
  138. show_trace(current, &stack);
  139. }
  140. EXPORT_SYMBOL(dump_stack);
  141. void show_code(unsigned int *pc)
  142. {
  143. long i;
  144. printk("\nCode:");
  145. for(i = -3 ; i < 6 ; i++) {
  146. unsigned int insn;
  147. if (__get_user(insn, pc + i)) {
  148. printk(" (Bad address in epc)\n");
  149. break;
  150. }
  151. printk("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
  152. }
  153. }
  154. void show_regs(struct pt_regs *regs)
  155. {
  156. const int field = 2 * sizeof(unsigned long);
  157. unsigned int cause = regs->cp0_cause;
  158. int i;
  159. printk("Cpu %d\n", smp_processor_id());
  160. /*
  161. * Saved main processor registers
  162. */
  163. for (i = 0; i < 32; ) {
  164. if ((i % 4) == 0)
  165. printk("$%2d :", i);
  166. if (i == 0)
  167. printk(" %0*lx", field, 0UL);
  168. else if (i == 26 || i == 27)
  169. printk(" %*s", field, "");
  170. else
  171. printk(" %0*lx", field, regs->regs[i]);
  172. i++;
  173. if ((i % 4) == 0)
  174. printk("\n");
  175. }
  176. printk("Hi : %0*lx\n", field, regs->hi);
  177. printk("Lo : %0*lx\n", field, regs->lo);
  178. /*
  179. * Saved cp0 registers
  180. */
  181. printk("epc : %0*lx ", field, regs->cp0_epc);
  182. print_symbol("%s ", regs->cp0_epc);
  183. printk(" %s\n", print_tainted());
  184. printk("ra : %0*lx ", field, regs->regs[31]);
  185. print_symbol("%s\n", regs->regs[31]);
  186. printk("Status: %08x ", (uint32_t) regs->cp0_status);
  187. if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
  188. if (regs->cp0_status & ST0_KUO)
  189. printk("KUo ");
  190. if (regs->cp0_status & ST0_IEO)
  191. printk("IEo ");
  192. if (regs->cp0_status & ST0_KUP)
  193. printk("KUp ");
  194. if (regs->cp0_status & ST0_IEP)
  195. printk("IEp ");
  196. if (regs->cp0_status & ST0_KUC)
  197. printk("KUc ");
  198. if (regs->cp0_status & ST0_IEC)
  199. printk("IEc ");
  200. } else {
  201. if (regs->cp0_status & ST0_KX)
  202. printk("KX ");
  203. if (regs->cp0_status & ST0_SX)
  204. printk("SX ");
  205. if (regs->cp0_status & ST0_UX)
  206. printk("UX ");
  207. switch (regs->cp0_status & ST0_KSU) {
  208. case KSU_USER:
  209. printk("USER ");
  210. break;
  211. case KSU_SUPERVISOR:
  212. printk("SUPERVISOR ");
  213. break;
  214. case KSU_KERNEL:
  215. printk("KERNEL ");
  216. break;
  217. default:
  218. printk("BAD_MODE ");
  219. break;
  220. }
  221. if (regs->cp0_status & ST0_ERL)
  222. printk("ERL ");
  223. if (regs->cp0_status & ST0_EXL)
  224. printk("EXL ");
  225. if (regs->cp0_status & ST0_IE)
  226. printk("IE ");
  227. }
  228. printk("\n");
  229. printk("Cause : %08x\n", cause);
  230. cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  231. if (1 <= cause && cause <= 5)
  232. printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
  233. printk("PrId : %08x\n", read_c0_prid());
  234. }
  235. void show_registers(struct pt_regs *regs)
  236. {
  237. show_regs(regs);
  238. print_modules();
  239. printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
  240. current->comm, current->pid, current_thread_info(), current);
  241. show_stack(current, (long *) regs->regs[29]);
  242. show_trace(current, (long *) regs->regs[29]);
  243. show_code((unsigned int *) regs->cp0_epc);
  244. printk("\n");
  245. }
  246. static DEFINE_SPINLOCK(die_lock);
  247. NORET_TYPE void ATTRIB_NORET die(const char * str, struct pt_regs * regs)
  248. {
  249. static int die_counter;
  250. #ifdef CONFIG_MIPS_MT_SMTC
  251. unsigned long dvpret = dvpe();
  252. #endif /* CONFIG_MIPS_MT_SMTC */
  253. console_verbose();
  254. spin_lock_irq(&die_lock);
  255. bust_spinlocks(1);
  256. #ifdef CONFIG_MIPS_MT_SMTC
  257. mips_mt_regdump(dvpret);
  258. #endif /* CONFIG_MIPS_MT_SMTC */
  259. printk("%s[#%d]:\n", str, ++die_counter);
  260. show_registers(regs);
  261. spin_unlock_irq(&die_lock);
  262. do_exit(SIGSEGV);
  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 = task_thread_info(current);
  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. return 0;
  462. default:
  463. return -EFAULT;
  464. }
  465. }
  466. /* Not ours. */
  467. return -EFAULT;
  468. }
  469. asmlinkage void do_ov(struct pt_regs *regs)
  470. {
  471. siginfo_t info;
  472. die_if_kernel("Integer overflow", regs);
  473. info.si_code = FPE_INTOVF;
  474. info.si_signo = SIGFPE;
  475. info.si_errno = 0;
  476. info.si_addr = (void __user *) regs->cp0_epc;
  477. force_sig_info(SIGFPE, &info, current);
  478. }
  479. /*
  480. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  481. */
  482. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  483. {
  484. if (fcr31 & FPU_CSR_UNI_X) {
  485. int sig;
  486. preempt_disable();
  487. #ifdef CONFIG_PREEMPT
  488. if (!is_fpu_owner()) {
  489. /* We might lose fpu before disabling preempt... */
  490. own_fpu();
  491. BUG_ON(!used_math());
  492. restore_fp(current);
  493. }
  494. #endif
  495. /*
  496. * Unimplemented operation exception. If we've got the full
  497. * software emulator on-board, let's use it...
  498. *
  499. * Force FPU to dump state into task/thread context. We're
  500. * moving a lot of data here for what is probably a single
  501. * instruction, but the alternative is to pre-decode the FP
  502. * register operands before invoking the emulator, which seems
  503. * a bit extreme for what should be an infrequent event.
  504. */
  505. save_fp(current);
  506. /* Ensure 'resume' not overwrite saved fp context again. */
  507. lose_fpu();
  508. preempt_enable();
  509. /* Run the emulator */
  510. sig = fpu_emulator_cop1Handler (regs,
  511. &current->thread.fpu.soft);
  512. preempt_disable();
  513. own_fpu(); /* Using the FPU again. */
  514. /*
  515. * We can't allow the emulated instruction to leave any of
  516. * the cause bit set in $fcr31.
  517. */
  518. current->thread.fpu.soft.fcr31 &= ~FPU_CSR_ALL_X;
  519. /* Restore the hardware register state */
  520. restore_fp(current);
  521. preempt_enable();
  522. /* If something went wrong, signal */
  523. if (sig)
  524. force_sig(sig, current);
  525. return;
  526. }
  527. force_sig(SIGFPE, current);
  528. }
  529. asmlinkage void do_bp(struct pt_regs *regs)
  530. {
  531. unsigned int opcode, bcode;
  532. siginfo_t info;
  533. die_if_kernel("Break instruction in kernel code", regs);
  534. if (get_insn_opcode(regs, &opcode))
  535. return;
  536. /*
  537. * There is the ancient bug in the MIPS assemblers that the break
  538. * code starts left to bit 16 instead to bit 6 in the opcode.
  539. * Gas is bug-compatible, but not always, grrr...
  540. * We handle both cases with a simple heuristics. --macro
  541. */
  542. bcode = ((opcode >> 6) & ((1 << 20) - 1));
  543. if (bcode < (1 << 10))
  544. bcode <<= 10;
  545. /*
  546. * (A short test says that IRIX 5.3 sends SIGTRAP for all break
  547. * insns, even for break codes that indicate arithmetic failures.
  548. * Weird ...)
  549. * But should we continue the brokenness??? --macro
  550. */
  551. switch (bcode) {
  552. case BRK_OVERFLOW << 10:
  553. case BRK_DIVZERO << 10:
  554. if (bcode == (BRK_DIVZERO << 10))
  555. info.si_code = FPE_INTDIV;
  556. else
  557. info.si_code = FPE_INTOVF;
  558. info.si_signo = SIGFPE;
  559. info.si_errno = 0;
  560. info.si_addr = (void __user *) regs->cp0_epc;
  561. force_sig_info(SIGFPE, &info, current);
  562. break;
  563. default:
  564. force_sig(SIGTRAP, current);
  565. }
  566. }
  567. asmlinkage void do_tr(struct pt_regs *regs)
  568. {
  569. unsigned int opcode, tcode = 0;
  570. siginfo_t info;
  571. die_if_kernel("Trap instruction in kernel code", regs);
  572. if (get_insn_opcode(regs, &opcode))
  573. return;
  574. /* Immediate versions don't provide a code. */
  575. if (!(opcode & OPCODE))
  576. tcode = ((opcode >> 6) & ((1 << 10) - 1));
  577. /*
  578. * (A short test says that IRIX 5.3 sends SIGTRAP for all trap
  579. * insns, even for trap codes that indicate arithmetic failures.
  580. * Weird ...)
  581. * But should we continue the brokenness??? --macro
  582. */
  583. switch (tcode) {
  584. case BRK_OVERFLOW:
  585. case BRK_DIVZERO:
  586. if (tcode == BRK_DIVZERO)
  587. info.si_code = FPE_INTDIV;
  588. else
  589. info.si_code = FPE_INTOVF;
  590. info.si_signo = SIGFPE;
  591. info.si_errno = 0;
  592. info.si_addr = (void __user *) regs->cp0_epc;
  593. force_sig_info(SIGFPE, &info, current);
  594. break;
  595. default:
  596. force_sig(SIGTRAP, current);
  597. }
  598. }
  599. asmlinkage void do_ri(struct pt_regs *regs)
  600. {
  601. die_if_kernel("Reserved instruction in kernel code", regs);
  602. if (!cpu_has_llsc)
  603. if (!simulate_llsc(regs))
  604. return;
  605. if (!simulate_rdhwr(regs))
  606. return;
  607. force_sig(SIGILL, current);
  608. }
  609. asmlinkage void do_cpu(struct pt_regs *regs)
  610. {
  611. unsigned int cpid;
  612. die_if_kernel("do_cpu invoked from kernel context!", regs);
  613. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  614. switch (cpid) {
  615. case 0:
  616. if (!cpu_has_llsc)
  617. if (!simulate_llsc(regs))
  618. return;
  619. if (!simulate_rdhwr(regs))
  620. return;
  621. break;
  622. case 1:
  623. preempt_disable();
  624. own_fpu();
  625. if (used_math()) { /* Using the FPU again. */
  626. restore_fp(current);
  627. } else { /* First time FPU user. */
  628. init_fpu();
  629. set_used_math();
  630. }
  631. preempt_enable();
  632. if (!cpu_has_fpu) {
  633. int sig = fpu_emulator_cop1Handler(regs,
  634. &current->thread.fpu.soft);
  635. if (sig)
  636. force_sig(sig, current);
  637. #ifdef CONFIG_MIPS_MT_FPAFF
  638. else {
  639. /*
  640. * MIPS MT processors may have fewer FPU contexts
  641. * than CPU threads. If we've emulated more than
  642. * some threshold number of instructions, force
  643. * migration to a "CPU" that has FP support.
  644. */
  645. if(mt_fpemul_threshold > 0
  646. && ((current->thread.emulated_fp++
  647. > mt_fpemul_threshold))) {
  648. /*
  649. * If there's no FPU present, or if the
  650. * application has already restricted
  651. * the allowed set to exclude any CPUs
  652. * with FPUs, we'll skip the procedure.
  653. */
  654. if (cpus_intersects(current->cpus_allowed,
  655. mt_fpu_cpumask)) {
  656. cpumask_t tmask;
  657. cpus_and(tmask,
  658. current->thread.user_cpus_allowed,
  659. mt_fpu_cpumask);
  660. set_cpus_allowed(current, tmask);
  661. current->thread.mflags |= MF_FPUBOUND;
  662. }
  663. }
  664. }
  665. #endif /* CONFIG_MIPS_MT_FPAFF */
  666. }
  667. return;
  668. case 2:
  669. case 3:
  670. die_if_kernel("do_cpu invoked from kernel context!", regs);
  671. break;
  672. }
  673. force_sig(SIGILL, current);
  674. }
  675. asmlinkage void do_mdmx(struct pt_regs *regs)
  676. {
  677. force_sig(SIGILL, current);
  678. }
  679. asmlinkage void do_watch(struct pt_regs *regs)
  680. {
  681. /*
  682. * We use the watch exception where available to detect stack
  683. * overflows.
  684. */
  685. dump_tlb_all();
  686. show_regs(regs);
  687. panic("Caught WATCH exception - probably caused by stack overflow.");
  688. }
  689. asmlinkage void do_mcheck(struct pt_regs *regs)
  690. {
  691. show_regs(regs);
  692. dump_tlb_all();
  693. /*
  694. * Some chips may have other causes of machine check (e.g. SB1
  695. * graduation timer)
  696. */
  697. panic("Caught Machine Check exception - %scaused by multiple "
  698. "matching entries in the TLB.",
  699. (regs->cp0_status & ST0_TS) ? "" : "not ");
  700. }
  701. asmlinkage void do_mt(struct pt_regs *regs)
  702. {
  703. int subcode;
  704. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  705. subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
  706. >> VPECONTROL_EXCPT_SHIFT;
  707. switch (subcode) {
  708. case 0:
  709. printk(KERN_ERR "Thread Underflow\n");
  710. break;
  711. case 1:
  712. printk(KERN_ERR "Thread Overflow\n");
  713. break;
  714. case 2:
  715. printk(KERN_ERR "Invalid YIELD Qualifier\n");
  716. break;
  717. case 3:
  718. printk(KERN_ERR "Gating Storage Exception\n");
  719. break;
  720. case 4:
  721. printk(KERN_ERR "YIELD Scheduler Exception\n");
  722. break;
  723. case 5:
  724. printk(KERN_ERR "Gating Storage Schedulier Exception\n");
  725. break;
  726. default:
  727. printk(KERN_ERR "*** UNKNOWN THREAD EXCEPTION %d ***\n",
  728. subcode);
  729. break;
  730. }
  731. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  732. force_sig(SIGILL, current);
  733. }
  734. asmlinkage void do_dsp(struct pt_regs *regs)
  735. {
  736. if (cpu_has_dsp)
  737. panic("Unexpected DSP exception\n");
  738. force_sig(SIGILL, current);
  739. }
  740. asmlinkage void do_reserved(struct pt_regs *regs)
  741. {
  742. /*
  743. * Game over - no way to handle this if it ever occurs. Most probably
  744. * caused by a new unknown cpu type or after another deadly
  745. * hard/software error.
  746. */
  747. show_regs(regs);
  748. panic("Caught reserved exception %ld - should not happen.",
  749. (regs->cp0_cause & 0x7f) >> 2);
  750. }
  751. asmlinkage void do_default_vi(struct pt_regs *regs)
  752. {
  753. show_regs(regs);
  754. panic("Caught unexpected vectored interrupt.");
  755. }
  756. /*
  757. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  758. * it different ways.
  759. */
  760. static inline void parity_protection_init(void)
  761. {
  762. switch (current_cpu_data.cputype) {
  763. case CPU_24K:
  764. case CPU_5KC:
  765. write_c0_ecc(0x80000000);
  766. back_to_back_c0_hazard();
  767. /* Set the PE bit (bit 31) in the c0_errctl register. */
  768. printk(KERN_INFO "Cache parity protection %sabled\n",
  769. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  770. break;
  771. case CPU_20KC:
  772. case CPU_25KF:
  773. /* Clear the DE bit (bit 16) in the c0_status register. */
  774. printk(KERN_INFO "Enable cache parity protection for "
  775. "MIPS 20KC/25KF CPUs.\n");
  776. clear_c0_status(ST0_DE);
  777. break;
  778. default:
  779. break;
  780. }
  781. }
  782. asmlinkage void cache_parity_error(void)
  783. {
  784. const int field = 2 * sizeof(unsigned long);
  785. unsigned int reg_val;
  786. /* For the moment, report the problem and hang. */
  787. printk("Cache error exception:\n");
  788. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  789. reg_val = read_c0_cacheerr();
  790. printk("c0_cacheerr == %08x\n", reg_val);
  791. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  792. reg_val & (1<<30) ? "secondary" : "primary",
  793. reg_val & (1<<31) ? "data" : "insn");
  794. printk("Error bits: %s%s%s%s%s%s%s\n",
  795. reg_val & (1<<29) ? "ED " : "",
  796. reg_val & (1<<28) ? "ET " : "",
  797. reg_val & (1<<26) ? "EE " : "",
  798. reg_val & (1<<25) ? "EB " : "",
  799. reg_val & (1<<24) ? "EI " : "",
  800. reg_val & (1<<23) ? "E1 " : "",
  801. reg_val & (1<<22) ? "E0 " : "");
  802. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  803. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
  804. if (reg_val & (1<<22))
  805. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  806. if (reg_val & (1<<23))
  807. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  808. #endif
  809. panic("Can't handle the cache error!");
  810. }
  811. /*
  812. * SDBBP EJTAG debug exception handler.
  813. * We skip the instruction and return to the next instruction.
  814. */
  815. void ejtag_exception_handler(struct pt_regs *regs)
  816. {
  817. const int field = 2 * sizeof(unsigned long);
  818. unsigned long depc, old_epc;
  819. unsigned int debug;
  820. printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  821. depc = read_c0_depc();
  822. debug = read_c0_debug();
  823. printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  824. if (debug & 0x80000000) {
  825. /*
  826. * In branch delay slot.
  827. * We cheat a little bit here and use EPC to calculate the
  828. * debug return address (DEPC). EPC is restored after the
  829. * calculation.
  830. */
  831. old_epc = regs->cp0_epc;
  832. regs->cp0_epc = depc;
  833. __compute_return_epc(regs);
  834. depc = regs->cp0_epc;
  835. regs->cp0_epc = old_epc;
  836. } else
  837. depc += 4;
  838. write_c0_depc(depc);
  839. #if 0
  840. printk("\n\n----- Enable EJTAG single stepping ----\n\n");
  841. write_c0_debug(debug | 0x100);
  842. #endif
  843. }
  844. /*
  845. * NMI exception handler.
  846. */
  847. void nmi_exception_handler(struct pt_regs *regs)
  848. {
  849. #ifdef CONFIG_MIPS_MT_SMTC
  850. unsigned long dvpret = dvpe();
  851. bust_spinlocks(1);
  852. printk("NMI taken!!!!\n");
  853. mips_mt_regdump(dvpret);
  854. #else
  855. bust_spinlocks(1);
  856. printk("NMI taken!!!!\n");
  857. #endif /* CONFIG_MIPS_MT_SMTC */
  858. die("NMI", regs);
  859. while(1) ;
  860. }
  861. #define VECTORSPACING 0x100 /* for EI/VI mode */
  862. unsigned long ebase;
  863. unsigned long exception_handlers[32];
  864. unsigned long vi_handlers[64];
  865. /*
  866. * As a side effect of the way this is implemented we're limited
  867. * to interrupt handlers in the address range from
  868. * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
  869. */
  870. void *set_except_vector(int n, void *addr)
  871. {
  872. unsigned long handler = (unsigned long) addr;
  873. unsigned long old_handler = exception_handlers[n];
  874. exception_handlers[n] = handler;
  875. if (n == 0 && cpu_has_divec) {
  876. *(volatile u32 *)(ebase + 0x200) = 0x08000000 |
  877. (0x03ffffff & (handler >> 2));
  878. flush_icache_range(ebase + 0x200, ebase + 0x204);
  879. }
  880. return (void *)old_handler;
  881. }
  882. #ifdef CONFIG_CPU_MIPSR2
  883. /*
  884. * MIPSR2 shadow register set allocation
  885. * FIXME: SMP...
  886. */
  887. static struct shadow_registers {
  888. /*
  889. * Number of shadow register sets supported
  890. */
  891. unsigned long sr_supported;
  892. /*
  893. * Bitmap of allocated shadow registers
  894. */
  895. unsigned long sr_allocated;
  896. } shadow_registers;
  897. static void mips_srs_init(void)
  898. {
  899. #ifdef CONFIG_CPU_MIPSR2_SRS
  900. shadow_registers.sr_supported = ((read_c0_srsctl() >> 26) & 0x0f) + 1;
  901. printk(KERN_INFO "%d MIPSR2 register sets available\n",
  902. shadow_registers.sr_supported);
  903. #endif
  904. shadow_registers.sr_allocated = 1; /* Set 0 used by kernel */
  905. }
  906. int mips_srs_max(void)
  907. {
  908. return shadow_registers.sr_supported;
  909. }
  910. int mips_srs_alloc(void)
  911. {
  912. struct shadow_registers *sr = &shadow_registers;
  913. int set;
  914. again:
  915. set = find_first_zero_bit(&sr->sr_allocated, sr->sr_supported);
  916. if (set >= sr->sr_supported)
  917. return -1;
  918. if (test_and_set_bit(set, &sr->sr_allocated))
  919. goto again;
  920. return set;
  921. }
  922. void mips_srs_free(int set)
  923. {
  924. struct shadow_registers *sr = &shadow_registers;
  925. clear_bit(set, &sr->sr_allocated);
  926. }
  927. static void *set_vi_srs_handler(int n, void *addr, int srs)
  928. {
  929. unsigned long handler;
  930. unsigned long old_handler = vi_handlers[n];
  931. u32 *w;
  932. unsigned char *b;
  933. if (!cpu_has_veic && !cpu_has_vint)
  934. BUG();
  935. if (addr == NULL) {
  936. handler = (unsigned long) do_default_vi;
  937. srs = 0;
  938. } else
  939. handler = (unsigned long) addr;
  940. vi_handlers[n] = (unsigned long) addr;
  941. b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
  942. if (srs >= mips_srs_max())
  943. panic("Shadow register set %d not supported", srs);
  944. if (cpu_has_veic) {
  945. if (board_bind_eic_interrupt)
  946. board_bind_eic_interrupt (n, srs);
  947. } else if (cpu_has_vint) {
  948. /* SRSMap is only defined if shadow sets are implemented */
  949. if (mips_srs_max() > 1)
  950. change_c0_srsmap (0xf << n*4, srs << n*4);
  951. }
  952. if (srs == 0) {
  953. /*
  954. * If no shadow set is selected then use the default handler
  955. * that does normal register saving and a standard interrupt exit
  956. */
  957. extern char except_vec_vi, except_vec_vi_lui;
  958. extern char except_vec_vi_ori, except_vec_vi_end;
  959. #ifdef CONFIG_MIPS_MT_SMTC
  960. /*
  961. * We need to provide the SMTC vectored interrupt handler
  962. * not only with the address of the handler, but with the
  963. * Status.IM bit to be masked before going there.
  964. */
  965. extern char except_vec_vi_mori;
  966. const int mori_offset = &except_vec_vi_mori - &except_vec_vi;
  967. #endif /* CONFIG_MIPS_MT_SMTC */
  968. const int handler_len = &except_vec_vi_end - &except_vec_vi;
  969. const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
  970. const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
  971. if (handler_len > VECTORSPACING) {
  972. /*
  973. * Sigh... panicing won't help as the console
  974. * is probably not configured :(
  975. */
  976. panic ("VECTORSPACING too small");
  977. }
  978. memcpy (b, &except_vec_vi, handler_len);
  979. #ifdef CONFIG_MIPS_MT_SMTC
  980. if (n > 7)
  981. printk("Vector index %d exceeds SMTC maximum\n", n);
  982. w = (u32 *)(b + mori_offset);
  983. *w = (*w & 0xffff0000) | (0x100 << n);
  984. #endif /* CONFIG_MIPS_MT_SMTC */
  985. w = (u32 *)(b + lui_offset);
  986. *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
  987. w = (u32 *)(b + ori_offset);
  988. *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
  989. flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
  990. }
  991. else {
  992. /*
  993. * In other cases jump directly to the interrupt handler
  994. *
  995. * It is the handlers responsibility to save registers if required
  996. * (eg hi/lo) and return from the exception using "eret"
  997. */
  998. w = (u32 *)b;
  999. *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
  1000. *w = 0;
  1001. flush_icache_range((unsigned long)b, (unsigned long)(b+8));
  1002. }
  1003. return (void *)old_handler;
  1004. }
  1005. void *set_vi_handler(int n, void *addr)
  1006. {
  1007. return set_vi_srs_handler(n, addr, 0);
  1008. }
  1009. #endif
  1010. /*
  1011. * This is used by native signal handling
  1012. */
  1013. asmlinkage int (*save_fp_context)(struct sigcontext *sc);
  1014. asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
  1015. extern asmlinkage int _save_fp_context(struct sigcontext *sc);
  1016. extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
  1017. extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
  1018. extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
  1019. #ifdef CONFIG_SMP
  1020. static int smp_save_fp_context(struct sigcontext *sc)
  1021. {
  1022. return cpu_has_fpu
  1023. ? _save_fp_context(sc)
  1024. : fpu_emulator_save_context(sc);
  1025. }
  1026. static int smp_restore_fp_context(struct sigcontext *sc)
  1027. {
  1028. return cpu_has_fpu
  1029. ? _restore_fp_context(sc)
  1030. : fpu_emulator_restore_context(sc);
  1031. }
  1032. #endif
  1033. static inline void signal_init(void)
  1034. {
  1035. #ifdef CONFIG_SMP
  1036. /* For now just do the cpu_has_fpu check when the functions are invoked */
  1037. save_fp_context = smp_save_fp_context;
  1038. restore_fp_context = smp_restore_fp_context;
  1039. #else
  1040. if (cpu_has_fpu) {
  1041. save_fp_context = _save_fp_context;
  1042. restore_fp_context = _restore_fp_context;
  1043. } else {
  1044. save_fp_context = fpu_emulator_save_context;
  1045. restore_fp_context = fpu_emulator_restore_context;
  1046. }
  1047. #endif
  1048. }
  1049. #ifdef CONFIG_MIPS32_COMPAT
  1050. /*
  1051. * This is used by 32-bit signal stuff on the 64-bit kernel
  1052. */
  1053. asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
  1054. asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
  1055. extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
  1056. extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
  1057. extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
  1058. extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
  1059. static inline void signal32_init(void)
  1060. {
  1061. if (cpu_has_fpu) {
  1062. save_fp_context32 = _save_fp_context32;
  1063. restore_fp_context32 = _restore_fp_context32;
  1064. } else {
  1065. save_fp_context32 = fpu_emulator_save_context32;
  1066. restore_fp_context32 = fpu_emulator_restore_context32;
  1067. }
  1068. }
  1069. #endif
  1070. extern void cpu_cache_init(void);
  1071. extern void tlb_init(void);
  1072. extern void flush_tlb_handlers(void);
  1073. void __init per_cpu_trap_init(void)
  1074. {
  1075. unsigned int cpu = smp_processor_id();
  1076. unsigned int status_set = ST0_CU0;
  1077. #ifdef CONFIG_MIPS_MT_SMTC
  1078. int secondaryTC = 0;
  1079. int bootTC = (cpu == 0);
  1080. /*
  1081. * Only do per_cpu_trap_init() for first TC of Each VPE.
  1082. * Note that this hack assumes that the SMTC init code
  1083. * assigns TCs consecutively and in ascending order.
  1084. */
  1085. if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
  1086. ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
  1087. secondaryTC = 1;
  1088. #endif /* CONFIG_MIPS_MT_SMTC */
  1089. /*
  1090. * Disable coprocessors and select 32-bit or 64-bit addressing
  1091. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  1092. * flag that some firmware may have left set and the TS bit (for
  1093. * IP27). Set XX for ISA IV code to work.
  1094. */
  1095. #ifdef CONFIG_64BIT
  1096. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  1097. #endif
  1098. if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
  1099. status_set |= ST0_XX;
  1100. change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  1101. status_set);
  1102. if (cpu_has_dsp)
  1103. set_c0_status(ST0_MX);
  1104. #ifdef CONFIG_CPU_MIPSR2
  1105. write_c0_hwrena (0x0000000f); /* Allow rdhwr to all registers */
  1106. #endif
  1107. #ifdef CONFIG_MIPS_MT_SMTC
  1108. if (!secondaryTC) {
  1109. #endif /* CONFIG_MIPS_MT_SMTC */
  1110. /*
  1111. * Interrupt handling.
  1112. */
  1113. if (cpu_has_veic || cpu_has_vint) {
  1114. write_c0_ebase (ebase);
  1115. /* Setting vector spacing enables EI/VI mode */
  1116. change_c0_intctl (0x3e0, VECTORSPACING);
  1117. }
  1118. if (cpu_has_divec) {
  1119. if (cpu_has_mipsmt) {
  1120. unsigned int vpflags = dvpe();
  1121. set_c0_cause(CAUSEF_IV);
  1122. evpe(vpflags);
  1123. } else
  1124. set_c0_cause(CAUSEF_IV);
  1125. }
  1126. #ifdef CONFIG_MIPS_MT_SMTC
  1127. }
  1128. #endif /* CONFIG_MIPS_MT_SMTC */
  1129. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  1130. TLBMISS_HANDLER_SETUP();
  1131. atomic_inc(&init_mm.mm_count);
  1132. current->active_mm = &init_mm;
  1133. BUG_ON(current->mm);
  1134. enter_lazy_tlb(&init_mm, current);
  1135. #ifdef CONFIG_MIPS_MT_SMTC
  1136. if (bootTC) {
  1137. #endif /* CONFIG_MIPS_MT_SMTC */
  1138. cpu_cache_init();
  1139. tlb_init();
  1140. #ifdef CONFIG_MIPS_MT_SMTC
  1141. }
  1142. #endif /* CONFIG_MIPS_MT_SMTC */
  1143. }
  1144. /* Install CPU exception handler */
  1145. void __init set_handler (unsigned long offset, void *addr, unsigned long size)
  1146. {
  1147. memcpy((void *)(ebase + offset), addr, size);
  1148. flush_icache_range(ebase + offset, ebase + offset + size);
  1149. }
  1150. /* Install uncached CPU exception handler */
  1151. void __init set_uncached_handler (unsigned long offset, void *addr, unsigned long size)
  1152. {
  1153. #ifdef CONFIG_32BIT
  1154. unsigned long uncached_ebase = KSEG1ADDR(ebase);
  1155. #endif
  1156. #ifdef CONFIG_64BIT
  1157. unsigned long uncached_ebase = TO_UNCAC(ebase);
  1158. #endif
  1159. memcpy((void *)(uncached_ebase + offset), addr, size);
  1160. }
  1161. void __init trap_init(void)
  1162. {
  1163. extern char except_vec3_generic, except_vec3_r4000;
  1164. extern char except_vec4;
  1165. unsigned long i;
  1166. if (cpu_has_veic || cpu_has_vint)
  1167. ebase = (unsigned long) alloc_bootmem_low_pages (0x200 + VECTORSPACING*64);
  1168. else
  1169. ebase = CAC_BASE;
  1170. #ifdef CONFIG_CPU_MIPSR2
  1171. mips_srs_init();
  1172. #endif
  1173. per_cpu_trap_init();
  1174. /*
  1175. * Copy the generic exception handlers to their final destination.
  1176. * This will be overriden later as suitable for a particular
  1177. * configuration.
  1178. */
  1179. set_handler(0x180, &except_vec3_generic, 0x80);
  1180. /*
  1181. * Setup default vectors
  1182. */
  1183. for (i = 0; i <= 31; i++)
  1184. set_except_vector(i, handle_reserved);
  1185. /*
  1186. * Copy the EJTAG debug exception vector handler code to it's final
  1187. * destination.
  1188. */
  1189. if (cpu_has_ejtag && board_ejtag_handler_setup)
  1190. board_ejtag_handler_setup ();
  1191. /*
  1192. * Only some CPUs have the watch exceptions.
  1193. */
  1194. if (cpu_has_watch)
  1195. set_except_vector(23, handle_watch);
  1196. /*
  1197. * Initialise interrupt handlers
  1198. */
  1199. if (cpu_has_veic || cpu_has_vint) {
  1200. int nvec = cpu_has_veic ? 64 : 8;
  1201. for (i = 0; i < nvec; i++)
  1202. set_vi_handler(i, NULL);
  1203. }
  1204. else if (cpu_has_divec)
  1205. set_handler(0x200, &except_vec4, 0x8);
  1206. /*
  1207. * Some CPUs can enable/disable for cache parity detection, but does
  1208. * it different ways.
  1209. */
  1210. parity_protection_init();
  1211. /*
  1212. * The Data Bus Errors / Instruction Bus Errors are signaled
  1213. * by external hardware. Therefore these two exceptions
  1214. * may have board specific handlers.
  1215. */
  1216. if (board_be_init)
  1217. board_be_init();
  1218. set_except_vector(0, handle_int);
  1219. set_except_vector(1, handle_tlbm);
  1220. set_except_vector(2, handle_tlbl);
  1221. set_except_vector(3, handle_tlbs);
  1222. set_except_vector(4, handle_adel);
  1223. set_except_vector(5, handle_ades);
  1224. set_except_vector(6, handle_ibe);
  1225. set_except_vector(7, handle_dbe);
  1226. set_except_vector(8, handle_sys);
  1227. set_except_vector(9, handle_bp);
  1228. set_except_vector(10, handle_ri);
  1229. set_except_vector(11, handle_cpu);
  1230. set_except_vector(12, handle_ov);
  1231. set_except_vector(13, handle_tr);
  1232. if (current_cpu_data.cputype == CPU_R6000 ||
  1233. current_cpu_data.cputype == CPU_R6000A) {
  1234. /*
  1235. * The R6000 is the only R-series CPU that features a machine
  1236. * check exception (similar to the R4000 cache error) and
  1237. * unaligned ldc1/sdc1 exception. The handlers have not been
  1238. * written yet. Well, anyway there is no R6000 machine on the
  1239. * current list of targets for Linux/MIPS.
  1240. * (Duh, crap, there is someone with a triple R6k machine)
  1241. */
  1242. //set_except_vector(14, handle_mc);
  1243. //set_except_vector(15, handle_ndc);
  1244. }
  1245. if (board_nmi_handler_setup)
  1246. board_nmi_handler_setup();
  1247. if (cpu_has_fpu && !cpu_has_nofpuex)
  1248. set_except_vector(15, handle_fpe);
  1249. set_except_vector(22, handle_mdmx);
  1250. if (cpu_has_mcheck)
  1251. set_except_vector(24, handle_mcheck);
  1252. if (cpu_has_mipsmt)
  1253. set_except_vector(25, handle_mt);
  1254. if (cpu_has_dsp)
  1255. set_except_vector(26, handle_dsp);
  1256. if (cpu_has_vce)
  1257. /* Special exception: R4[04]00 uses also the divec space. */
  1258. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
  1259. else if (cpu_has_4kex)
  1260. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  1261. else
  1262. memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
  1263. signal_init();
  1264. #ifdef CONFIG_MIPS32_COMPAT
  1265. signal32_init();
  1266. #endif
  1267. flush_icache_range(ebase, ebase + 0x400);
  1268. flush_tlb_handlers();
  1269. }