traps.c 37 KB

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