traps.c 38 KB

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