traps.c 37 KB

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