traps.c 37 KB

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