traps.c 37 KB

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