traps.c 38 KB

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