traps.c 39 KB

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