traps.c 38 KB

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