traps.c 36 KB

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