traps.c 37 KB

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