traps.c 37 KB

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