traps.c 42 KB

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