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