traps.c 44 KB

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