traps.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532
  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) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
  12. * Copyright (C) 2000, 2001, 2012 MIPS Technologies, Inc. All rights reserved.
  13. * Copyright (C) 2014, Imagination Technologies Ltd.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/bug.h>
  17. #include <linux/compiler.h>
  18. #include <linux/context_tracking.h>
  19. #include <linux/cpu_pm.h>
  20. #include <linux/kexec.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/extable.h>
  25. #include <linux/mm.h>
  26. #include <linux/sched/mm.h>
  27. #include <linux/sched/debug.h>
  28. #include <linux/smp.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/kallsyms.h>
  31. #include <linux/memblock.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/kgdb.h>
  35. #include <linux/kdebug.h>
  36. #include <linux/kprobes.h>
  37. #include <linux/notifier.h>
  38. #include <linux/kdb.h>
  39. #include <linux/irq.h>
  40. #include <linux/perf_event.h>
  41. #include <asm/addrspace.h>
  42. #include <asm/bootinfo.h>
  43. #include <asm/branch.h>
  44. #include <asm/break.h>
  45. #include <asm/cop2.h>
  46. #include <asm/cpu.h>
  47. #include <asm/cpu-type.h>
  48. #include <asm/dsp.h>
  49. #include <asm/fpu.h>
  50. #include <asm/fpu_emulator.h>
  51. #include <asm/idle.h>
  52. #include <asm/isa-rev.h>
  53. #include <asm/mips-cps.h>
  54. #include <asm/mips-r2-to-r6-emul.h>
  55. #include <asm/mipsregs.h>
  56. #include <asm/mipsmtregs.h>
  57. #include <asm/module.h>
  58. #include <asm/msa.h>
  59. #include <asm/ptrace.h>
  60. #include <asm/regdef.h>
  61. #include <asm/sections.h>
  62. #include <asm/siginfo.h>
  63. #include <asm/tlbdebug.h>
  64. #include <asm/traps.h>
  65. #include <linux/uaccess.h>
  66. #include <asm/watch.h>
  67. #include <asm/mmu_context.h>
  68. #include <asm/types.h>
  69. #include <asm/stacktrace.h>
  70. #include <asm/tlbex.h>
  71. #include <asm/uasm.h>
  72. #include <asm/mach-loongson64/cpucfg-emul.h>
  73. #include "access-helper.h"
  74. extern void check_wait(void);
  75. extern asmlinkage void rollback_handle_int(void);
  76. extern asmlinkage void handle_int(void);
  77. extern asmlinkage void handle_adel(void);
  78. extern asmlinkage void handle_ades(void);
  79. extern asmlinkage void handle_ibe(void);
  80. extern asmlinkage void handle_dbe(void);
  81. extern asmlinkage void handle_sys(void);
  82. extern asmlinkage void handle_bp(void);
  83. extern asmlinkage void handle_ri(void);
  84. extern asmlinkage void handle_ri_rdhwr_tlbp(void);
  85. extern asmlinkage void handle_ri_rdhwr(void);
  86. extern asmlinkage void handle_cpu(void);
  87. extern asmlinkage void handle_ov(void);
  88. extern asmlinkage void handle_tr(void);
  89. extern asmlinkage void handle_msa_fpe(void);
  90. extern asmlinkage void handle_fpe(void);
  91. extern asmlinkage void handle_ftlb(void);
  92. extern asmlinkage void handle_gsexc(void);
  93. extern asmlinkage void handle_msa(void);
  94. extern asmlinkage void handle_mdmx(void);
  95. extern asmlinkage void handle_watch(void);
  96. extern asmlinkage void handle_mt(void);
  97. extern asmlinkage void handle_dsp(void);
  98. extern asmlinkage void handle_mcheck(void);
  99. extern asmlinkage void handle_reserved(void);
  100. extern void tlb_do_page_fault_0(void);
  101. void (*board_be_init)(void);
  102. static int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
  103. void (*board_nmi_handler_setup)(void);
  104. void (*board_ejtag_handler_setup)(void);
  105. void (*board_bind_eic_interrupt)(int irq, int regset);
  106. void (*board_ebase_setup)(void);
  107. void(*board_cache_error_setup)(void);
  108. void mips_set_be_handler(int (*handler)(struct pt_regs *regs, int is_fixup))
  109. {
  110. board_be_handler = handler;
  111. }
  112. EXPORT_SYMBOL_GPL(mips_set_be_handler);
  113. static void show_raw_backtrace(unsigned long reg29, const char *loglvl,
  114. bool user)
  115. {
  116. unsigned long *sp = (unsigned long *)(reg29 & ~3);
  117. unsigned long addr;
  118. printk("%sCall Trace:", loglvl);
  119. #ifdef CONFIG_KALLSYMS
  120. printk("%s\n", loglvl);
  121. #endif
  122. while (!kstack_end(sp)) {
  123. if (__get_addr(&addr, sp++, user)) {
  124. printk("%s (Bad stack address)", loglvl);
  125. break;
  126. }
  127. if (__kernel_text_address(addr))
  128. print_ip_sym(loglvl, addr);
  129. }
  130. printk("%s\n", loglvl);
  131. }
  132. #ifdef CONFIG_KALLSYMS
  133. int raw_show_trace;
  134. static int __init set_raw_show_trace(char *str)
  135. {
  136. raw_show_trace = 1;
  137. return 1;
  138. }
  139. __setup("raw_show_trace", set_raw_show_trace);
  140. #endif
  141. static void show_backtrace(struct task_struct *task, const struct pt_regs *regs,
  142. const char *loglvl, bool user)
  143. {
  144. unsigned long sp = regs->regs[29];
  145. unsigned long ra = regs->regs[31];
  146. unsigned long pc = regs->cp0_epc;
  147. if (!task)
  148. task = current;
  149. if (raw_show_trace || user_mode(regs) || !__kernel_text_address(pc)) {
  150. show_raw_backtrace(sp, loglvl, user);
  151. return;
  152. }
  153. printk("%sCall Trace:\n", loglvl);
  154. do {
  155. print_ip_sym(loglvl, pc);
  156. pc = unwind_stack(task, &sp, pc, &ra);
  157. } while (pc);
  158. pr_cont("\n");
  159. }
  160. /*
  161. * This routine abuses get_user()/put_user() to reference pointers
  162. * with at least a bit of error checking ...
  163. */
  164. static void show_stacktrace(struct task_struct *task,
  165. const struct pt_regs *regs, const char *loglvl, bool user)
  166. {
  167. const int field = 2 * sizeof(unsigned long);
  168. unsigned long stackdata;
  169. int i;
  170. unsigned long *sp = (unsigned long *)regs->regs[29];
  171. printk("%sStack :", loglvl);
  172. i = 0;
  173. while ((unsigned long) sp & (PAGE_SIZE - 1)) {
  174. if (i && ((i % (64 / field)) == 0)) {
  175. pr_cont("\n");
  176. printk("%s ", loglvl);
  177. }
  178. if (i > 39) {
  179. pr_cont(" ...");
  180. break;
  181. }
  182. if (__get_addr(&stackdata, sp++, user)) {
  183. pr_cont(" (Bad stack address)");
  184. break;
  185. }
  186. pr_cont(" %0*lx", field, stackdata);
  187. i++;
  188. }
  189. pr_cont("\n");
  190. show_backtrace(task, regs, loglvl, user);
  191. }
  192. void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
  193. {
  194. struct pt_regs regs;
  195. regs.cp0_status = KSU_KERNEL;
  196. if (sp) {
  197. regs.regs[29] = (unsigned long)sp;
  198. regs.regs[31] = 0;
  199. regs.cp0_epc = 0;
  200. } else {
  201. if (task && task != current) {
  202. regs.regs[29] = task->thread.reg29;
  203. regs.regs[31] = 0;
  204. regs.cp0_epc = task->thread.reg31;
  205. } else {
  206. prepare_frametrace(&regs);
  207. }
  208. }
  209. show_stacktrace(task, &regs, loglvl, false);
  210. }
  211. static void show_code(void *pc, bool user)
  212. {
  213. long i;
  214. unsigned short *pc16 = NULL;
  215. printk("Code:");
  216. if ((unsigned long)pc & 1)
  217. pc16 = (u16 *)((unsigned long)pc & ~1);
  218. for(i = -3 ; i < 6 ; i++) {
  219. if (pc16) {
  220. u16 insn16;
  221. if (__get_inst16(&insn16, pc16 + i, user))
  222. goto bad_address;
  223. pr_cont("%c%04x%c", (i?' ':'<'), insn16, (i?' ':'>'));
  224. } else {
  225. u32 insn32;
  226. if (__get_inst32(&insn32, (u32 *)pc + i, user))
  227. goto bad_address;
  228. pr_cont("%c%08x%c", (i?' ':'<'), insn32, (i?' ':'>'));
  229. }
  230. }
  231. pr_cont("\n");
  232. return;
  233. bad_address:
  234. pr_cont(" (Bad address in epc)\n\n");
  235. }
  236. static void __show_regs(const struct pt_regs *regs)
  237. {
  238. const int field = 2 * sizeof(unsigned long);
  239. unsigned int cause = regs->cp0_cause;
  240. unsigned int exccode;
  241. int i;
  242. show_regs_print_info(KERN_DEFAULT);
  243. /*
  244. * Saved main processor registers
  245. */
  246. for (i = 0; i < 32; ) {
  247. if ((i % 4) == 0)
  248. printk("$%2d :", i);
  249. if (i == 0)
  250. pr_cont(" %0*lx", field, 0UL);
  251. else if (i == 26 || i == 27)
  252. pr_cont(" %*s", field, "");
  253. else
  254. pr_cont(" %0*lx", field, regs->regs[i]);
  255. i++;
  256. if ((i % 4) == 0)
  257. pr_cont("\n");
  258. }
  259. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  260. printk("Acx : %0*lx\n", field, regs->acx);
  261. #endif
  262. if (MIPS_ISA_REV < 6) {
  263. printk("Hi : %0*lx\n", field, regs->hi);
  264. printk("Lo : %0*lx\n", field, regs->lo);
  265. }
  266. /*
  267. * Saved cp0 registers
  268. */
  269. printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
  270. (void *) regs->cp0_epc);
  271. printk("ra : %0*lx %pS\n", field, regs->regs[31],
  272. (void *) regs->regs[31]);
  273. printk("Status: %08x ", (uint32_t) regs->cp0_status);
  274. if (cpu_has_3kex) {
  275. if (regs->cp0_status & ST0_KUO)
  276. pr_cont("KUo ");
  277. if (regs->cp0_status & ST0_IEO)
  278. pr_cont("IEo ");
  279. if (regs->cp0_status & ST0_KUP)
  280. pr_cont("KUp ");
  281. if (regs->cp0_status & ST0_IEP)
  282. pr_cont("IEp ");
  283. if (regs->cp0_status & ST0_KUC)
  284. pr_cont("KUc ");
  285. if (regs->cp0_status & ST0_IEC)
  286. pr_cont("IEc ");
  287. } else if (cpu_has_4kex) {
  288. if (regs->cp0_status & ST0_KX)
  289. pr_cont("KX ");
  290. if (regs->cp0_status & ST0_SX)
  291. pr_cont("SX ");
  292. if (regs->cp0_status & ST0_UX)
  293. pr_cont("UX ");
  294. switch (regs->cp0_status & ST0_KSU) {
  295. case KSU_USER:
  296. pr_cont("USER ");
  297. break;
  298. case KSU_SUPERVISOR:
  299. pr_cont("SUPERVISOR ");
  300. break;
  301. case KSU_KERNEL:
  302. pr_cont("KERNEL ");
  303. break;
  304. default:
  305. pr_cont("BAD_MODE ");
  306. break;
  307. }
  308. if (regs->cp0_status & ST0_ERL)
  309. pr_cont("ERL ");
  310. if (regs->cp0_status & ST0_EXL)
  311. pr_cont("EXL ");
  312. if (regs->cp0_status & ST0_IE)
  313. pr_cont("IE ");
  314. }
  315. pr_cont("\n");
  316. exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  317. printk("Cause : %08x (ExcCode %02x)\n", cause, exccode);
  318. if (1 <= exccode && exccode <= 5)
  319. printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
  320. printk("PrId : %08x (%s)\n", read_c0_prid(),
  321. cpu_name_string());
  322. }
  323. /*
  324. * FIXME: really the generic show_regs should take a const pointer argument.
  325. */
  326. void show_regs(struct pt_regs *regs)
  327. {
  328. __show_regs(regs);
  329. dump_stack();
  330. }
  331. void show_registers(struct pt_regs *regs)
  332. {
  333. const int field = 2 * sizeof(unsigned long);
  334. __show_regs(regs);
  335. print_modules();
  336. printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
  337. current->comm, current->pid, current_thread_info(), current,
  338. field, current_thread_info()->tp_value);
  339. if (cpu_has_userlocal) {
  340. unsigned long tls;
  341. tls = read_c0_userlocal();
  342. if (tls != current_thread_info()->tp_value)
  343. printk("*HwTLS: %0*lx\n", field, tls);
  344. }
  345. show_stacktrace(current, regs, KERN_DEFAULT, user_mode(regs));
  346. show_code((void *)regs->cp0_epc, user_mode(regs));
  347. printk("\n");
  348. }
  349. static DEFINE_RAW_SPINLOCK(die_lock);
  350. void __noreturn die(const char *str, struct pt_regs *regs)
  351. {
  352. static int die_counter;
  353. int sig = SIGSEGV;
  354. oops_enter();
  355. if (notify_die(DIE_OOPS, str, regs, 0, current->thread.trap_nr,
  356. SIGSEGV) == NOTIFY_STOP)
  357. sig = 0;
  358. console_verbose();
  359. raw_spin_lock_irq(&die_lock);
  360. bust_spinlocks(1);
  361. printk("%s[#%d]:\n", str, ++die_counter);
  362. show_registers(regs);
  363. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  364. raw_spin_unlock_irq(&die_lock);
  365. oops_exit();
  366. if (in_interrupt())
  367. panic("Fatal exception in interrupt");
  368. if (panic_on_oops)
  369. panic("Fatal exception");
  370. if (regs && kexec_should_crash(current))
  371. crash_kexec(regs);
  372. make_task_dead(sig);
  373. }
  374. extern struct exception_table_entry __start___dbe_table[];
  375. extern struct exception_table_entry __stop___dbe_table[];
  376. __asm__(
  377. " .section __dbe_table, \"a\"\n"
  378. " .previous \n");
  379. /* Given an address, look for it in the exception tables. */
  380. static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
  381. {
  382. const struct exception_table_entry *e;
  383. e = search_extable(__start___dbe_table,
  384. __stop___dbe_table - __start___dbe_table, addr);
  385. if (!e)
  386. e = search_module_dbetables(addr);
  387. return e;
  388. }
  389. asmlinkage void do_be(struct pt_regs *regs)
  390. {
  391. const int field = 2 * sizeof(unsigned long);
  392. const struct exception_table_entry *fixup = NULL;
  393. int data = regs->cp0_cause & 4;
  394. int action = MIPS_BE_FATAL;
  395. enum ctx_state prev_state;
  396. prev_state = exception_enter();
  397. /* XXX For now. Fixme, this searches the wrong table ... */
  398. if (data && !user_mode(regs))
  399. fixup = search_dbe_tables(exception_epc(regs));
  400. if (fixup)
  401. action = MIPS_BE_FIXUP;
  402. if (board_be_handler)
  403. action = board_be_handler(regs, fixup != NULL);
  404. else
  405. mips_cm_error_report();
  406. switch (action) {
  407. case MIPS_BE_DISCARD:
  408. goto out;
  409. case MIPS_BE_FIXUP:
  410. if (fixup) {
  411. regs->cp0_epc = fixup->nextinsn;
  412. goto out;
  413. }
  414. break;
  415. default:
  416. break;
  417. }
  418. /*
  419. * Assume it would be too dangerous to continue ...
  420. */
  421. printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
  422. data ? "Data" : "Instruction",
  423. field, regs->cp0_epc, field, regs->regs[31]);
  424. if (notify_die(DIE_OOPS, "bus error", regs, 0, current->thread.trap_nr,
  425. SIGBUS) == NOTIFY_STOP)
  426. goto out;
  427. die_if_kernel("Oops", regs);
  428. force_sig(SIGBUS);
  429. out:
  430. exception_exit(prev_state);
  431. }
  432. /*
  433. * ll/sc, rdhwr, sync emulation
  434. */
  435. #define OPCODE 0xfc000000
  436. #define BASE 0x03e00000
  437. #define RT 0x001f0000
  438. #define OFFSET 0x0000ffff
  439. #define LL 0xc0000000
  440. #define SC 0xe0000000
  441. #define SPEC0 0x00000000
  442. #define SPEC3 0x7c000000
  443. #define RD 0x0000f800
  444. #define FUNC 0x0000003f
  445. #define SYNC 0x0000000f
  446. #define RDHWR 0x0000003b
  447. /* microMIPS definitions */
  448. #define MM_POOL32A_FUNC 0xfc00ffff
  449. #define MM_RDHWR 0x00006b3c
  450. #define MM_RS 0x001f0000
  451. #define MM_RT 0x03e00000
  452. /*
  453. * The ll_bit is cleared by r*_switch.S
  454. */
  455. unsigned int ll_bit;
  456. struct task_struct *ll_task;
  457. static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
  458. {
  459. unsigned long value, __user *vaddr;
  460. long offset;
  461. /*
  462. * analyse the ll instruction that just caused a ri exception
  463. * and put the referenced address to addr.
  464. */
  465. /* sign extend offset */
  466. offset = opcode & OFFSET;
  467. offset <<= 16;
  468. offset >>= 16;
  469. vaddr = (unsigned long __user *)
  470. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  471. if ((unsigned long)vaddr & 3)
  472. return SIGBUS;
  473. if (get_user(value, vaddr))
  474. return SIGSEGV;
  475. preempt_disable();
  476. if (ll_task == NULL || ll_task == current) {
  477. ll_bit = 1;
  478. } else {
  479. ll_bit = 0;
  480. }
  481. ll_task = current;
  482. preempt_enable();
  483. regs->regs[(opcode & RT) >> 16] = value;
  484. return 0;
  485. }
  486. static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
  487. {
  488. unsigned long __user *vaddr;
  489. unsigned long reg;
  490. long offset;
  491. /*
  492. * analyse the sc instruction that just caused a ri exception
  493. * and put the referenced address to addr.
  494. */
  495. /* sign extend offset */
  496. offset = opcode & OFFSET;
  497. offset <<= 16;
  498. offset >>= 16;
  499. vaddr = (unsigned long __user *)
  500. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  501. reg = (opcode & RT) >> 16;
  502. if ((unsigned long)vaddr & 3)
  503. return SIGBUS;
  504. preempt_disable();
  505. if (ll_bit == 0 || ll_task != current) {
  506. regs->regs[reg] = 0;
  507. preempt_enable();
  508. return 0;
  509. }
  510. preempt_enable();
  511. if (put_user(regs->regs[reg], vaddr))
  512. return SIGSEGV;
  513. regs->regs[reg] = 1;
  514. return 0;
  515. }
  516. /*
  517. * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
  518. * opcodes are supposed to result in coprocessor unusable exceptions if
  519. * executed on ll/sc-less processors. That's the theory. In practice a
  520. * few processors such as NEC's VR4100 throw reserved instruction exceptions
  521. * instead, so we're doing the emulation thing in both exception handlers.
  522. */
  523. static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
  524. {
  525. if ((opcode & OPCODE) == LL) {
  526. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  527. 1, regs, 0);
  528. return simulate_ll(regs, opcode);
  529. }
  530. if ((opcode & OPCODE) == SC) {
  531. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  532. 1, regs, 0);
  533. return simulate_sc(regs, opcode);
  534. }
  535. return -1; /* Must be something else ... */
  536. }
  537. /*
  538. * Simulate trapping 'rdhwr' instructions to provide user accessible
  539. * registers not implemented in hardware.
  540. */
  541. static int simulate_rdhwr(struct pt_regs *regs, int rd, int rt)
  542. {
  543. struct thread_info *ti = task_thread_info(current);
  544. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  545. 1, regs, 0);
  546. switch (rd) {
  547. case MIPS_HWR_CPUNUM: /* CPU number */
  548. regs->regs[rt] = smp_processor_id();
  549. return 0;
  550. case MIPS_HWR_SYNCISTEP: /* SYNCI length */
  551. regs->regs[rt] = min(current_cpu_data.dcache.linesz,
  552. current_cpu_data.icache.linesz);
  553. return 0;
  554. case MIPS_HWR_CC: /* Read count register */
  555. regs->regs[rt] = read_c0_count();
  556. return 0;
  557. case MIPS_HWR_CCRES: /* Count register resolution */
  558. switch (current_cpu_type()) {
  559. case CPU_20KC:
  560. case CPU_25KF:
  561. regs->regs[rt] = 1;
  562. break;
  563. default:
  564. regs->regs[rt] = 2;
  565. }
  566. return 0;
  567. case MIPS_HWR_ULR: /* Read UserLocal register */
  568. regs->regs[rt] = ti->tp_value;
  569. return 0;
  570. default:
  571. return -1;
  572. }
  573. }
  574. static int simulate_rdhwr_normal(struct pt_regs *regs, unsigned int opcode)
  575. {
  576. if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
  577. int rd = (opcode & RD) >> 11;
  578. int rt = (opcode & RT) >> 16;
  579. simulate_rdhwr(regs, rd, rt);
  580. return 0;
  581. }
  582. /* Not ours. */
  583. return -1;
  584. }
  585. static int simulate_rdhwr_mm(struct pt_regs *regs, unsigned int opcode)
  586. {
  587. if ((opcode & MM_POOL32A_FUNC) == MM_RDHWR) {
  588. int rd = (opcode & MM_RS) >> 16;
  589. int rt = (opcode & MM_RT) >> 21;
  590. simulate_rdhwr(regs, rd, rt);
  591. return 0;
  592. }
  593. /* Not ours. */
  594. return -1;
  595. }
  596. static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
  597. {
  598. if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
  599. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  600. 1, regs, 0);
  601. return 0;
  602. }
  603. return -1; /* Must be something else ... */
  604. }
  605. /*
  606. * Loongson-3 CSR instructions emulation
  607. */
  608. #ifdef CONFIG_CPU_LOONGSON3_CPUCFG_EMULATION
  609. #define LWC2 0xc8000000
  610. #define RS BASE
  611. #define CSR_OPCODE2 0x00000118
  612. #define CSR_OPCODE2_MASK 0x000007ff
  613. #define CSR_FUNC_MASK RT
  614. #define CSR_FUNC_CPUCFG 0x8
  615. static int simulate_loongson3_cpucfg(struct pt_regs *regs,
  616. unsigned int opcode)
  617. {
  618. int op = opcode & OPCODE;
  619. int op2 = opcode & CSR_OPCODE2_MASK;
  620. int csr_func = (opcode & CSR_FUNC_MASK) >> 16;
  621. if (op == LWC2 && op2 == CSR_OPCODE2 && csr_func == CSR_FUNC_CPUCFG) {
  622. int rd = (opcode & RD) >> 11;
  623. int rs = (opcode & RS) >> 21;
  624. __u64 sel = regs->regs[rs];
  625. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
  626. /* Do not emulate on unsupported core models. */
  627. preempt_disable();
  628. if (!loongson3_cpucfg_emulation_enabled(&current_cpu_data)) {
  629. preempt_enable();
  630. return -1;
  631. }
  632. regs->regs[rd] = loongson3_cpucfg_read_synthesized(
  633. &current_cpu_data, sel);
  634. preempt_enable();
  635. return 0;
  636. }
  637. /* Not ours. */
  638. return -1;
  639. }
  640. #endif /* CONFIG_CPU_LOONGSON3_CPUCFG_EMULATION */
  641. asmlinkage void do_ov(struct pt_regs *regs)
  642. {
  643. enum ctx_state prev_state;
  644. prev_state = exception_enter();
  645. die_if_kernel("Integer overflow", regs);
  646. force_sig_fault(SIGFPE, FPE_INTOVF, (void __user *)regs->cp0_epc);
  647. exception_exit(prev_state);
  648. }
  649. #ifdef CONFIG_MIPS_FP_SUPPORT
  650. /*
  651. * Send SIGFPE according to FCSR Cause bits, which must have already
  652. * been masked against Enable bits. This is impotant as Inexact can
  653. * happen together with Overflow or Underflow, and `ptrace' can set
  654. * any bits.
  655. */
  656. void force_fcr31_sig(unsigned long fcr31, void __user *fault_addr,
  657. struct task_struct *tsk)
  658. {
  659. int si_code = FPE_FLTUNK;
  660. if (fcr31 & FPU_CSR_INV_X)
  661. si_code = FPE_FLTINV;
  662. else if (fcr31 & FPU_CSR_DIV_X)
  663. si_code = FPE_FLTDIV;
  664. else if (fcr31 & FPU_CSR_OVF_X)
  665. si_code = FPE_FLTOVF;
  666. else if (fcr31 & FPU_CSR_UDF_X)
  667. si_code = FPE_FLTUND;
  668. else if (fcr31 & FPU_CSR_INE_X)
  669. si_code = FPE_FLTRES;
  670. force_sig_fault_to_task(SIGFPE, si_code, fault_addr, tsk);
  671. }
  672. int process_fpemu_return(int sig, void __user *fault_addr, unsigned long fcr31)
  673. {
  674. int si_code;
  675. switch (sig) {
  676. case 0:
  677. return 0;
  678. case SIGFPE:
  679. force_fcr31_sig(fcr31, fault_addr, current);
  680. return 1;
  681. case SIGBUS:
  682. force_sig_fault(SIGBUS, BUS_ADRERR, fault_addr);
  683. return 1;
  684. case SIGSEGV:
  685. mmap_read_lock(current->mm);
  686. if (vma_lookup(current->mm, (unsigned long)fault_addr))
  687. si_code = SEGV_ACCERR;
  688. else
  689. si_code = SEGV_MAPERR;
  690. mmap_read_unlock(current->mm);
  691. force_sig_fault(SIGSEGV, si_code, fault_addr);
  692. return 1;
  693. default:
  694. force_sig(sig);
  695. return 1;
  696. }
  697. }
  698. static int simulate_fp(struct pt_regs *regs, unsigned int opcode,
  699. unsigned long old_epc, unsigned long old_ra)
  700. {
  701. union mips_instruction inst = { .word = opcode };
  702. void __user *fault_addr;
  703. unsigned long fcr31;
  704. int sig;
  705. /* If it's obviously not an FP instruction, skip it */
  706. switch (inst.i_format.opcode) {
  707. case cop1_op:
  708. case cop1x_op:
  709. case lwc1_op:
  710. case ldc1_op:
  711. case swc1_op:
  712. case sdc1_op:
  713. break;
  714. default:
  715. return -1;
  716. }
  717. /*
  718. * do_ri skipped over the instruction via compute_return_epc, undo
  719. * that for the FPU emulator.
  720. */
  721. regs->cp0_epc = old_epc;
  722. regs->regs[31] = old_ra;
  723. /* Run the emulator */
  724. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  725. &fault_addr);
  726. /*
  727. * We can't allow the emulated instruction to leave any
  728. * enabled Cause bits set in $fcr31.
  729. */
  730. fcr31 = mask_fcr31_x(current->thread.fpu.fcr31);
  731. current->thread.fpu.fcr31 &= ~fcr31;
  732. /* Restore the hardware register state */
  733. own_fpu(1);
  734. /* Send a signal if required. */
  735. process_fpemu_return(sig, fault_addr, fcr31);
  736. return 0;
  737. }
  738. /*
  739. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  740. */
  741. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  742. {
  743. enum ctx_state prev_state;
  744. void __user *fault_addr;
  745. int sig;
  746. prev_state = exception_enter();
  747. if (notify_die(DIE_FP, "FP exception", regs, 0, current->thread.trap_nr,
  748. SIGFPE) == NOTIFY_STOP)
  749. goto out;
  750. /* Clear FCSR.Cause before enabling interrupts */
  751. write_32bit_cp1_register(CP1_STATUS, fcr31 & ~mask_fcr31_x(fcr31));
  752. local_irq_enable();
  753. die_if_kernel("FP exception in kernel code", regs);
  754. if (fcr31 & FPU_CSR_UNI_X) {
  755. /*
  756. * Unimplemented operation exception. If we've got the full
  757. * software emulator on-board, let's use it...
  758. *
  759. * Force FPU to dump state into task/thread context. We're
  760. * moving a lot of data here for what is probably a single
  761. * instruction, but the alternative is to pre-decode the FP
  762. * register operands before invoking the emulator, which seems
  763. * a bit extreme for what should be an infrequent event.
  764. */
  765. /* Run the emulator */
  766. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  767. &fault_addr);
  768. /*
  769. * We can't allow the emulated instruction to leave any
  770. * enabled Cause bits set in $fcr31.
  771. */
  772. fcr31 = mask_fcr31_x(current->thread.fpu.fcr31);
  773. current->thread.fpu.fcr31 &= ~fcr31;
  774. /* Restore the hardware register state */
  775. own_fpu(1); /* Using the FPU again. */
  776. } else {
  777. sig = SIGFPE;
  778. fault_addr = (void __user *) regs->cp0_epc;
  779. }
  780. /* Send a signal if required. */
  781. process_fpemu_return(sig, fault_addr, fcr31);
  782. out:
  783. exception_exit(prev_state);
  784. }
  785. /*
  786. * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
  787. * emulated more than some threshold number of instructions, force migration to
  788. * a "CPU" that has FP support.
  789. */
  790. static void mt_ase_fp_affinity(void)
  791. {
  792. #ifdef CONFIG_MIPS_MT_FPAFF
  793. if (mt_fpemul_threshold > 0 &&
  794. ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
  795. /*
  796. * If there's no FPU present, or if the application has already
  797. * restricted the allowed set to exclude any CPUs with FPUs,
  798. * we'll skip the procedure.
  799. */
  800. if (cpumask_intersects(&current->cpus_mask, &mt_fpu_cpumask)) {
  801. cpumask_t tmask;
  802. current->thread.user_cpus_allowed
  803. = current->cpus_mask;
  804. cpumask_and(&tmask, &current->cpus_mask,
  805. &mt_fpu_cpumask);
  806. set_cpus_allowed_ptr(current, &tmask);
  807. set_thread_flag(TIF_FPUBOUND);
  808. }
  809. }
  810. #endif /* CONFIG_MIPS_MT_FPAFF */
  811. }
  812. #else /* !CONFIG_MIPS_FP_SUPPORT */
  813. static int simulate_fp(struct pt_regs *regs, unsigned int opcode,
  814. unsigned long old_epc, unsigned long old_ra)
  815. {
  816. return -1;
  817. }
  818. #endif /* !CONFIG_MIPS_FP_SUPPORT */
  819. void do_trap_or_bp(struct pt_regs *regs, unsigned int code, int si_code,
  820. const char *str)
  821. {
  822. char b[40];
  823. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  824. if (kgdb_ll_trap(DIE_TRAP, str, regs, code, current->thread.trap_nr,
  825. SIGTRAP) == NOTIFY_STOP)
  826. return;
  827. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  828. if (notify_die(DIE_TRAP, str, regs, code, current->thread.trap_nr,
  829. SIGTRAP) == NOTIFY_STOP)
  830. return;
  831. /*
  832. * A short test says that IRIX 5.3 sends SIGTRAP for all trap
  833. * insns, even for trap and break codes that indicate arithmetic
  834. * failures. Weird ...
  835. * But should we continue the brokenness??? --macro
  836. */
  837. switch (code) {
  838. case BRK_OVERFLOW:
  839. case BRK_DIVZERO:
  840. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  841. die_if_kernel(b, regs);
  842. force_sig_fault(SIGFPE,
  843. code == BRK_DIVZERO ? FPE_INTDIV : FPE_INTOVF,
  844. (void __user *) regs->cp0_epc);
  845. break;
  846. case BRK_BUG:
  847. die_if_kernel("Kernel bug detected", regs);
  848. force_sig(SIGTRAP);
  849. break;
  850. case BRK_MEMU:
  851. /*
  852. * This breakpoint code is used by the FPU emulator to retake
  853. * control of the CPU after executing the instruction from the
  854. * delay slot of an emulated branch.
  855. *
  856. * Terminate if exception was recognized as a delay slot return
  857. * otherwise handle as normal.
  858. */
  859. if (do_dsemulret(regs))
  860. return;
  861. die_if_kernel("Math emu break/trap", regs);
  862. force_sig(SIGTRAP);
  863. break;
  864. default:
  865. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  866. die_if_kernel(b, regs);
  867. if (si_code) {
  868. force_sig_fault(SIGTRAP, si_code, NULL);
  869. } else {
  870. force_sig(SIGTRAP);
  871. }
  872. }
  873. }
  874. asmlinkage void do_bp(struct pt_regs *regs)
  875. {
  876. unsigned long epc = msk_isa16_mode(exception_epc(regs));
  877. unsigned int opcode, bcode;
  878. enum ctx_state prev_state;
  879. bool user = user_mode(regs);
  880. prev_state = exception_enter();
  881. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  882. if (get_isa16_mode(regs->cp0_epc)) {
  883. u16 instr[2];
  884. if (__get_inst16(&instr[0], (u16 *)epc, user))
  885. goto out_sigsegv;
  886. if (!cpu_has_mmips) {
  887. /* MIPS16e mode */
  888. bcode = (instr[0] >> 5) & 0x3f;
  889. } else if (mm_insn_16bit(instr[0])) {
  890. /* 16-bit microMIPS BREAK */
  891. bcode = instr[0] & 0xf;
  892. } else {
  893. /* 32-bit microMIPS BREAK */
  894. if (__get_inst16(&instr[1], (u16 *)(epc + 2), user))
  895. goto out_sigsegv;
  896. opcode = (instr[0] << 16) | instr[1];
  897. bcode = (opcode >> 6) & ((1 << 20) - 1);
  898. }
  899. } else {
  900. if (__get_inst32(&opcode, (u32 *)epc, user))
  901. goto out_sigsegv;
  902. bcode = (opcode >> 6) & ((1 << 20) - 1);
  903. }
  904. /*
  905. * There is the ancient bug in the MIPS assemblers that the break
  906. * code starts left to bit 16 instead to bit 6 in the opcode.
  907. * Gas is bug-compatible, but not always, grrr...
  908. * We handle both cases with a simple heuristics. --macro
  909. */
  910. if (bcode >= (1 << 10))
  911. bcode = ((bcode & ((1 << 10) - 1)) << 10) | (bcode >> 10);
  912. /*
  913. * notify the kprobe handlers, if instruction is likely to
  914. * pertain to them.
  915. */
  916. switch (bcode) {
  917. case BRK_UPROBE:
  918. if (notify_die(DIE_UPROBE, "uprobe", regs, bcode,
  919. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  920. goto out;
  921. else
  922. break;
  923. case BRK_UPROBE_XOL:
  924. if (notify_die(DIE_UPROBE_XOL, "uprobe_xol", regs, bcode,
  925. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  926. goto out;
  927. else
  928. break;
  929. case BRK_KPROBE_BP:
  930. if (notify_die(DIE_BREAK, "debug", regs, bcode,
  931. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  932. goto out;
  933. else
  934. break;
  935. case BRK_KPROBE_SSTEPBP:
  936. if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode,
  937. current->thread.trap_nr, SIGTRAP) == NOTIFY_STOP)
  938. goto out;
  939. else
  940. break;
  941. default:
  942. break;
  943. }
  944. do_trap_or_bp(regs, bcode, TRAP_BRKPT, "Break");
  945. out:
  946. exception_exit(prev_state);
  947. return;
  948. out_sigsegv:
  949. force_sig(SIGSEGV);
  950. goto out;
  951. }
  952. asmlinkage void do_tr(struct pt_regs *regs)
  953. {
  954. u32 opcode, tcode = 0;
  955. enum ctx_state prev_state;
  956. u16 instr[2];
  957. bool user = user_mode(regs);
  958. unsigned long epc = msk_isa16_mode(exception_epc(regs));
  959. prev_state = exception_enter();
  960. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  961. if (get_isa16_mode(regs->cp0_epc)) {
  962. if (__get_inst16(&instr[0], (u16 *)(epc + 0), user) ||
  963. __get_inst16(&instr[1], (u16 *)(epc + 2), user))
  964. goto out_sigsegv;
  965. opcode = (instr[0] << 16) | instr[1];
  966. /* Immediate versions don't provide a code. */
  967. if (!(opcode & OPCODE))
  968. tcode = (opcode >> 12) & ((1 << 4) - 1);
  969. } else {
  970. if (__get_inst32(&opcode, (u32 *)epc, user))
  971. goto out_sigsegv;
  972. /* Immediate versions don't provide a code. */
  973. if (!(opcode & OPCODE))
  974. tcode = (opcode >> 6) & ((1 << 10) - 1);
  975. }
  976. do_trap_or_bp(regs, tcode, 0, "Trap");
  977. out:
  978. exception_exit(prev_state);
  979. return;
  980. out_sigsegv:
  981. force_sig(SIGSEGV);
  982. goto out;
  983. }
  984. asmlinkage void do_ri(struct pt_regs *regs)
  985. {
  986. unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
  987. unsigned long old_epc = regs->cp0_epc;
  988. unsigned long old31 = regs->regs[31];
  989. enum ctx_state prev_state;
  990. unsigned int opcode = 0;
  991. int status = -1;
  992. /*
  993. * Avoid any kernel code. Just emulate the R2 instruction
  994. * as quickly as possible.
  995. */
  996. if (mipsr2_emulation && cpu_has_mips_r6 &&
  997. likely(user_mode(regs)) &&
  998. likely(get_user(opcode, epc) >= 0)) {
  999. unsigned long fcr31 = 0;
  1000. status = mipsr2_decoder(regs, opcode, &fcr31);
  1001. switch (status) {
  1002. case 0:
  1003. case SIGEMT:
  1004. return;
  1005. case SIGILL:
  1006. goto no_r2_instr;
  1007. default:
  1008. process_fpemu_return(status,
  1009. &current->thread.cp0_baduaddr,
  1010. fcr31);
  1011. return;
  1012. }
  1013. }
  1014. no_r2_instr:
  1015. prev_state = exception_enter();
  1016. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  1017. if (notify_die(DIE_RI, "RI Fault", regs, 0, current->thread.trap_nr,
  1018. SIGILL) == NOTIFY_STOP)
  1019. goto out;
  1020. die_if_kernel("Reserved instruction in kernel code", regs);
  1021. if (unlikely(compute_return_epc(regs) < 0))
  1022. goto out;
  1023. if (!get_isa16_mode(regs->cp0_epc)) {
  1024. if (unlikely(get_user(opcode, epc) < 0))
  1025. status = SIGSEGV;
  1026. if (!cpu_has_llsc && status < 0)
  1027. status = simulate_llsc(regs, opcode);
  1028. if (status < 0)
  1029. status = simulate_rdhwr_normal(regs, opcode);
  1030. if (status < 0)
  1031. status = simulate_sync(regs, opcode);
  1032. if (status < 0)
  1033. status = simulate_fp(regs, opcode, old_epc, old31);
  1034. #ifdef CONFIG_CPU_LOONGSON3_CPUCFG_EMULATION
  1035. if (status < 0)
  1036. status = simulate_loongson3_cpucfg(regs, opcode);
  1037. #endif
  1038. } else if (cpu_has_mmips) {
  1039. unsigned short mmop[2] = { 0 };
  1040. if (unlikely(get_user(mmop[0], (u16 __user *)epc + 0) < 0))
  1041. status = SIGSEGV;
  1042. if (unlikely(get_user(mmop[1], (u16 __user *)epc + 1) < 0))
  1043. status = SIGSEGV;
  1044. opcode = mmop[0];
  1045. opcode = (opcode << 16) | mmop[1];
  1046. if (status < 0)
  1047. status = simulate_rdhwr_mm(regs, opcode);
  1048. }
  1049. if (status < 0)
  1050. status = SIGILL;
  1051. if (unlikely(status > 0)) {
  1052. regs->cp0_epc = old_epc; /* Undo skip-over. */
  1053. regs->regs[31] = old31;
  1054. force_sig(status);
  1055. }
  1056. out:
  1057. exception_exit(prev_state);
  1058. }
  1059. /*
  1060. * No lock; only written during early bootup by CPU 0.
  1061. */
  1062. static RAW_NOTIFIER_HEAD(cu2_chain);
  1063. int __ref register_cu2_notifier(struct notifier_block *nb)
  1064. {
  1065. return raw_notifier_chain_register(&cu2_chain, nb);
  1066. }
  1067. int cu2_notifier_call_chain(unsigned long val, void *v)
  1068. {
  1069. return raw_notifier_call_chain(&cu2_chain, val, v);
  1070. }
  1071. static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
  1072. void *data)
  1073. {
  1074. struct pt_regs *regs = data;
  1075. die_if_kernel("COP2: Unhandled kernel unaligned access or invalid "
  1076. "instruction", regs);
  1077. force_sig(SIGILL);
  1078. return NOTIFY_OK;
  1079. }
  1080. #ifdef CONFIG_MIPS_FP_SUPPORT
  1081. static int enable_restore_fp_context(int msa)
  1082. {
  1083. int err, was_fpu_owner, prior_msa;
  1084. bool first_fp;
  1085. /* Initialize context if it hasn't been used already */
  1086. first_fp = init_fp_ctx(current);
  1087. if (first_fp) {
  1088. preempt_disable();
  1089. err = own_fpu_inatomic(1);
  1090. if (msa && !err) {
  1091. enable_msa();
  1092. /*
  1093. * with MSA enabled, userspace can see MSACSR
  1094. * and MSA regs, but the values in them are from
  1095. * other task before current task, restore them
  1096. * from saved fp/msa context
  1097. */
  1098. write_msa_csr(current->thread.fpu.msacsr);
  1099. /*
  1100. * own_fpu_inatomic(1) just restore low 64bit,
  1101. * fix the high 64bit
  1102. */
  1103. init_msa_upper();
  1104. set_thread_flag(TIF_USEDMSA);
  1105. set_thread_flag(TIF_MSA_CTX_LIVE);
  1106. }
  1107. preempt_enable();
  1108. return err;
  1109. }
  1110. /*
  1111. * This task has formerly used the FP context.
  1112. *
  1113. * If this thread has no live MSA vector context then we can simply
  1114. * restore the scalar FP context. If it has live MSA vector context
  1115. * (that is, it has or may have used MSA since last performing a
  1116. * function call) then we'll need to restore the vector context. This
  1117. * applies even if we're currently only executing a scalar FP
  1118. * instruction. This is because if we were to later execute an MSA
  1119. * instruction then we'd either have to:
  1120. *
  1121. * - Restore the vector context & clobber any registers modified by
  1122. * scalar FP instructions between now & then.
  1123. *
  1124. * or
  1125. *
  1126. * - Not restore the vector context & lose the most significant bits
  1127. * of all vector registers.
  1128. *
  1129. * Neither of those options is acceptable. We cannot restore the least
  1130. * significant bits of the registers now & only restore the most
  1131. * significant bits later because the most significant bits of any
  1132. * vector registers whose aliased FP register is modified now will have
  1133. * been zeroed. We'd have no way to know that when restoring the vector
  1134. * context & thus may load an outdated value for the most significant
  1135. * bits of a vector register.
  1136. */
  1137. if (!msa && !thread_msa_context_live())
  1138. return own_fpu(1);
  1139. /*
  1140. * This task is using or has previously used MSA. Thus we require
  1141. * that Status.FR == 1.
  1142. */
  1143. preempt_disable();
  1144. was_fpu_owner = is_fpu_owner();
  1145. err = own_fpu_inatomic(0);
  1146. if (err)
  1147. goto out;
  1148. enable_msa();
  1149. write_msa_csr(current->thread.fpu.msacsr);
  1150. set_thread_flag(TIF_USEDMSA);
  1151. /*
  1152. * If this is the first time that the task is using MSA and it has
  1153. * previously used scalar FP in this time slice then we already nave
  1154. * FP context which we shouldn't clobber. We do however need to clear
  1155. * the upper 64b of each vector register so that this task has no
  1156. * opportunity to see data left behind by another.
  1157. */
  1158. prior_msa = test_and_set_thread_flag(TIF_MSA_CTX_LIVE);
  1159. if (!prior_msa && was_fpu_owner) {
  1160. init_msa_upper();
  1161. goto out;
  1162. }
  1163. if (!prior_msa) {
  1164. /*
  1165. * Restore the least significant 64b of each vector register
  1166. * from the existing scalar FP context.
  1167. */
  1168. _restore_fp(current);
  1169. /*
  1170. * The task has not formerly used MSA, so clear the upper 64b
  1171. * of each vector register such that it cannot see data left
  1172. * behind by another task.
  1173. */
  1174. init_msa_upper();
  1175. } else {
  1176. /* We need to restore the vector context. */
  1177. restore_msa(current);
  1178. /* Restore the scalar FP control & status register */
  1179. if (!was_fpu_owner)
  1180. write_32bit_cp1_register(CP1_STATUS,
  1181. current->thread.fpu.fcr31);
  1182. }
  1183. out:
  1184. preempt_enable();
  1185. return 0;
  1186. }
  1187. #else /* !CONFIG_MIPS_FP_SUPPORT */
  1188. static int enable_restore_fp_context(int msa)
  1189. {
  1190. return SIGILL;
  1191. }
  1192. #endif /* CONFIG_MIPS_FP_SUPPORT */
  1193. asmlinkage void do_cpu(struct pt_regs *regs)
  1194. {
  1195. enum ctx_state prev_state;
  1196. unsigned int __user *epc;
  1197. unsigned long old_epc, old31;
  1198. unsigned int opcode;
  1199. unsigned int cpid;
  1200. int status;
  1201. prev_state = exception_enter();
  1202. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  1203. if (cpid != 2)
  1204. die_if_kernel("do_cpu invoked from kernel context!", regs);
  1205. switch (cpid) {
  1206. case 0:
  1207. epc = (unsigned int __user *)exception_epc(regs);
  1208. old_epc = regs->cp0_epc;
  1209. old31 = regs->regs[31];
  1210. opcode = 0;
  1211. status = -1;
  1212. if (unlikely(compute_return_epc(regs) < 0))
  1213. break;
  1214. if (!get_isa16_mode(regs->cp0_epc)) {
  1215. if (unlikely(get_user(opcode, epc) < 0))
  1216. status = SIGSEGV;
  1217. if (!cpu_has_llsc && status < 0)
  1218. status = simulate_llsc(regs, opcode);
  1219. }
  1220. if (status < 0)
  1221. status = SIGILL;
  1222. if (unlikely(status > 0)) {
  1223. regs->cp0_epc = old_epc; /* Undo skip-over. */
  1224. regs->regs[31] = old31;
  1225. force_sig(status);
  1226. }
  1227. break;
  1228. #ifdef CONFIG_MIPS_FP_SUPPORT
  1229. case 3:
  1230. /*
  1231. * The COP3 opcode space and consequently the CP0.Status.CU3
  1232. * bit and the CP0.Cause.CE=3 encoding have been removed as
  1233. * of the MIPS III ISA. From the MIPS IV and MIPS32r2 ISAs
  1234. * up the space has been reused for COP1X instructions, that
  1235. * are enabled by the CP0.Status.CU1 bit and consequently
  1236. * use the CP0.Cause.CE=1 encoding for Coprocessor Unusable
  1237. * exceptions. Some FPU-less processors that implement one
  1238. * of these ISAs however use this code erroneously for COP1X
  1239. * instructions. Therefore we redirect this trap to the FP
  1240. * emulator too.
  1241. */
  1242. if (raw_cpu_has_fpu || !cpu_has_mips_4_5_64_r2_r6) {
  1243. force_sig(SIGILL);
  1244. break;
  1245. }
  1246. fallthrough;
  1247. case 1: {
  1248. void __user *fault_addr;
  1249. unsigned long fcr31;
  1250. int err, sig;
  1251. err = enable_restore_fp_context(0);
  1252. if (raw_cpu_has_fpu && !err)
  1253. break;
  1254. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 0,
  1255. &fault_addr);
  1256. /*
  1257. * We can't allow the emulated instruction to leave
  1258. * any enabled Cause bits set in $fcr31.
  1259. */
  1260. fcr31 = mask_fcr31_x(current->thread.fpu.fcr31);
  1261. current->thread.fpu.fcr31 &= ~fcr31;
  1262. /* Send a signal if required. */
  1263. if (!process_fpemu_return(sig, fault_addr, fcr31) && !err)
  1264. mt_ase_fp_affinity();
  1265. break;
  1266. }
  1267. #else /* CONFIG_MIPS_FP_SUPPORT */
  1268. case 1:
  1269. case 3:
  1270. force_sig(SIGILL);
  1271. break;
  1272. #endif /* CONFIG_MIPS_FP_SUPPORT */
  1273. case 2:
  1274. raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
  1275. break;
  1276. }
  1277. exception_exit(prev_state);
  1278. }
  1279. asmlinkage void do_msa_fpe(struct pt_regs *regs, unsigned int msacsr)
  1280. {
  1281. enum ctx_state prev_state;
  1282. prev_state = exception_enter();
  1283. current->thread.trap_nr = (regs->cp0_cause >> 2) & 0x1f;
  1284. if (notify_die(DIE_MSAFP, "MSA FP exception", regs, 0,
  1285. current->thread.trap_nr, SIGFPE) == NOTIFY_STOP)
  1286. goto out;
  1287. /* Clear MSACSR.Cause before enabling interrupts */
  1288. write_msa_csr(msacsr & ~MSA_CSR_CAUSEF);
  1289. local_irq_enable();
  1290. die_if_kernel("do_msa_fpe invoked from kernel context!", regs);
  1291. force_sig(SIGFPE);
  1292. out:
  1293. exception_exit(prev_state);
  1294. }
  1295. asmlinkage void do_msa(struct pt_regs *regs)
  1296. {
  1297. enum ctx_state prev_state;
  1298. int err;
  1299. prev_state = exception_enter();
  1300. if (!cpu_has_msa || test_thread_flag(TIF_32BIT_FPREGS)) {
  1301. force_sig(SIGILL);
  1302. goto out;
  1303. }
  1304. die_if_kernel("do_msa invoked from kernel context!", regs);
  1305. err = enable_restore_fp_context(1);
  1306. if (err)
  1307. force_sig(SIGILL);
  1308. out:
  1309. exception_exit(prev_state);
  1310. }
  1311. asmlinkage void do_mdmx(struct pt_regs *regs)
  1312. {
  1313. enum ctx_state prev_state;
  1314. prev_state = exception_enter();
  1315. force_sig(SIGILL);
  1316. exception_exit(prev_state);
  1317. }
  1318. /*
  1319. * Called with interrupts disabled.
  1320. */
  1321. asmlinkage void do_watch(struct pt_regs *regs)
  1322. {
  1323. enum ctx_state prev_state;
  1324. prev_state = exception_enter();
  1325. /*
  1326. * Clear WP (bit 22) bit of cause register so we don't loop
  1327. * forever.
  1328. */
  1329. clear_c0_cause(CAUSEF_WP);
  1330. /*
  1331. * If the current thread has the watch registers loaded, save
  1332. * their values and send SIGTRAP. Otherwise another thread
  1333. * left the registers set, clear them and continue.
  1334. */
  1335. if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
  1336. mips_read_watch_registers();
  1337. local_irq_enable();
  1338. force_sig_fault(SIGTRAP, TRAP_HWBKPT, NULL);
  1339. } else {
  1340. mips_clear_watch_registers();
  1341. local_irq_enable();
  1342. }
  1343. exception_exit(prev_state);
  1344. }
  1345. asmlinkage void do_mcheck(struct pt_regs *regs)
  1346. {
  1347. int multi_match = regs->cp0_status & ST0_TS;
  1348. enum ctx_state prev_state;
  1349. prev_state = exception_enter();
  1350. show_regs(regs);
  1351. if (multi_match) {
  1352. dump_tlb_regs();
  1353. pr_info("\n");
  1354. dump_tlb_all();
  1355. }
  1356. show_code((void *)regs->cp0_epc, user_mode(regs));
  1357. /*
  1358. * Some chips may have other causes of machine check (e.g. SB1
  1359. * graduation timer)
  1360. */
  1361. panic("Caught Machine Check exception - %scaused by multiple "
  1362. "matching entries in the TLB.",
  1363. (multi_match) ? "" : "not ");
  1364. }
  1365. asmlinkage void do_mt(struct pt_regs *regs)
  1366. {
  1367. int subcode;
  1368. subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
  1369. >> VPECONTROL_EXCPT_SHIFT;
  1370. switch (subcode) {
  1371. case 0:
  1372. printk(KERN_DEBUG "Thread Underflow\n");
  1373. break;
  1374. case 1:
  1375. printk(KERN_DEBUG "Thread Overflow\n");
  1376. break;
  1377. case 2:
  1378. printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
  1379. break;
  1380. case 3:
  1381. printk(KERN_DEBUG "Gating Storage Exception\n");
  1382. break;
  1383. case 4:
  1384. printk(KERN_DEBUG "YIELD Scheduler Exception\n");
  1385. break;
  1386. case 5:
  1387. printk(KERN_DEBUG "Gating Storage Scheduler Exception\n");
  1388. break;
  1389. default:
  1390. printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
  1391. subcode);
  1392. break;
  1393. }
  1394. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  1395. force_sig(SIGILL);
  1396. }
  1397. asmlinkage void do_dsp(struct pt_regs *regs)
  1398. {
  1399. if (cpu_has_dsp)
  1400. panic("Unexpected DSP exception");
  1401. force_sig(SIGILL);
  1402. }
  1403. asmlinkage void do_reserved(struct pt_regs *regs)
  1404. {
  1405. /*
  1406. * Game over - no way to handle this if it ever occurs. Most probably
  1407. * caused by a new unknown cpu type or after another deadly
  1408. * hard/software error.
  1409. */
  1410. show_regs(regs);
  1411. panic("Caught reserved exception %ld - should not happen.",
  1412. (regs->cp0_cause & 0x7f) >> 2);
  1413. }
  1414. static int __initdata l1parity = 1;
  1415. static int __init nol1parity(char *s)
  1416. {
  1417. l1parity = 0;
  1418. return 1;
  1419. }
  1420. __setup("nol1par", nol1parity);
  1421. static int __initdata l2parity = 1;
  1422. static int __init nol2parity(char *s)
  1423. {
  1424. l2parity = 0;
  1425. return 1;
  1426. }
  1427. __setup("nol2par", nol2parity);
  1428. /*
  1429. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  1430. * it different ways.
  1431. */
  1432. static inline __init void parity_protection_init(void)
  1433. {
  1434. #define ERRCTL_PE 0x80000000
  1435. #define ERRCTL_L2P 0x00800000
  1436. if (mips_cm_revision() >= CM_REV_CM3) {
  1437. ulong gcr_ectl, cp0_ectl;
  1438. /*
  1439. * With CM3 systems we need to ensure that the L1 & L2
  1440. * parity enables are set to the same value, since this
  1441. * is presumed by the hardware engineers.
  1442. *
  1443. * If the user disabled either of L1 or L2 ECC checking,
  1444. * disable both.
  1445. */
  1446. l1parity &= l2parity;
  1447. l2parity &= l1parity;
  1448. /* Probe L1 ECC support */
  1449. cp0_ectl = read_c0_ecc();
  1450. write_c0_ecc(cp0_ectl | ERRCTL_PE);
  1451. back_to_back_c0_hazard();
  1452. cp0_ectl = read_c0_ecc();
  1453. /* Probe L2 ECC support */
  1454. gcr_ectl = read_gcr_err_control();
  1455. if (!(gcr_ectl & CM_GCR_ERR_CONTROL_L2_ECC_SUPPORT) ||
  1456. !(cp0_ectl & ERRCTL_PE)) {
  1457. /*
  1458. * One of L1 or L2 ECC checking isn't supported,
  1459. * so we cannot enable either.
  1460. */
  1461. l1parity = l2parity = 0;
  1462. }
  1463. /* Configure L1 ECC checking */
  1464. if (l1parity)
  1465. cp0_ectl |= ERRCTL_PE;
  1466. else
  1467. cp0_ectl &= ~ERRCTL_PE;
  1468. write_c0_ecc(cp0_ectl);
  1469. back_to_back_c0_hazard();
  1470. WARN_ON(!!(read_c0_ecc() & ERRCTL_PE) != l1parity);
  1471. /* Configure L2 ECC checking */
  1472. if (l2parity)
  1473. gcr_ectl |= CM_GCR_ERR_CONTROL_L2_ECC_EN;
  1474. else
  1475. gcr_ectl &= ~CM_GCR_ERR_CONTROL_L2_ECC_EN;
  1476. write_gcr_err_control(gcr_ectl);
  1477. gcr_ectl = read_gcr_err_control();
  1478. gcr_ectl &= CM_GCR_ERR_CONTROL_L2_ECC_EN;
  1479. WARN_ON(!!gcr_ectl != l2parity);
  1480. pr_info("Cache parity protection %sabled\n",
  1481. l1parity ? "en" : "dis");
  1482. return;
  1483. }
  1484. switch (current_cpu_type()) {
  1485. case CPU_24K:
  1486. case CPU_34K:
  1487. case CPU_74K:
  1488. case CPU_1004K:
  1489. case CPU_1074K:
  1490. case CPU_INTERAPTIV:
  1491. case CPU_PROAPTIV:
  1492. case CPU_P5600:
  1493. case CPU_QEMU_GENERIC:
  1494. case CPU_P6600:
  1495. {
  1496. unsigned long errctl;
  1497. unsigned int l1parity_present, l2parity_present;
  1498. errctl = read_c0_ecc();
  1499. errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
  1500. /* probe L1 parity support */
  1501. write_c0_ecc(errctl | ERRCTL_PE);
  1502. back_to_back_c0_hazard();
  1503. l1parity_present = (read_c0_ecc() & ERRCTL_PE);
  1504. /* probe L2 parity support */
  1505. write_c0_ecc(errctl|ERRCTL_L2P);
  1506. back_to_back_c0_hazard();
  1507. l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
  1508. if (l1parity_present && l2parity_present) {
  1509. if (l1parity)
  1510. errctl |= ERRCTL_PE;
  1511. if (l1parity ^ l2parity)
  1512. errctl |= ERRCTL_L2P;
  1513. } else if (l1parity_present) {
  1514. if (l1parity)
  1515. errctl |= ERRCTL_PE;
  1516. } else if (l2parity_present) {
  1517. if (l2parity)
  1518. errctl |= ERRCTL_L2P;
  1519. } else {
  1520. /* No parity available */
  1521. }
  1522. printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
  1523. write_c0_ecc(errctl);
  1524. back_to_back_c0_hazard();
  1525. errctl = read_c0_ecc();
  1526. printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
  1527. if (l1parity_present)
  1528. printk(KERN_INFO "Cache parity protection %sabled\n",
  1529. (errctl & ERRCTL_PE) ? "en" : "dis");
  1530. if (l2parity_present) {
  1531. if (l1parity_present && l1parity)
  1532. errctl ^= ERRCTL_L2P;
  1533. printk(KERN_INFO "L2 cache parity protection %sabled\n",
  1534. (errctl & ERRCTL_L2P) ? "en" : "dis");
  1535. }
  1536. }
  1537. break;
  1538. case CPU_5KC:
  1539. case CPU_5KE:
  1540. case CPU_LOONGSON32:
  1541. write_c0_ecc(0x80000000);
  1542. back_to_back_c0_hazard();
  1543. /* Set the PE bit (bit 31) in the c0_errctl register. */
  1544. printk(KERN_INFO "Cache parity protection %sabled\n",
  1545. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  1546. break;
  1547. case CPU_20KC:
  1548. case CPU_25KF:
  1549. /* Clear the DE bit (bit 16) in the c0_status register. */
  1550. printk(KERN_INFO "Enable cache parity protection for "
  1551. "MIPS 20KC/25KF CPUs.\n");
  1552. clear_c0_status(ST0_DE);
  1553. break;
  1554. default:
  1555. break;
  1556. }
  1557. }
  1558. asmlinkage void cache_parity_error(void)
  1559. {
  1560. const int field = 2 * sizeof(unsigned long);
  1561. unsigned int reg_val;
  1562. /* For the moment, report the problem and hang. */
  1563. printk("Cache error exception:\n");
  1564. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1565. reg_val = read_c0_cacheerr();
  1566. printk("c0_cacheerr == %08x\n", reg_val);
  1567. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1568. reg_val & (1<<30) ? "secondary" : "primary",
  1569. reg_val & (1<<31) ? "data" : "insn");
  1570. if ((cpu_has_mips_r2_r6) &&
  1571. ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS)) {
  1572. pr_err("Error bits: %s%s%s%s%s%s%s%s\n",
  1573. reg_val & (1<<29) ? "ED " : "",
  1574. reg_val & (1<<28) ? "ET " : "",
  1575. reg_val & (1<<27) ? "ES " : "",
  1576. reg_val & (1<<26) ? "EE " : "",
  1577. reg_val & (1<<25) ? "EB " : "",
  1578. reg_val & (1<<24) ? "EI " : "",
  1579. reg_val & (1<<23) ? "E1 " : "",
  1580. reg_val & (1<<22) ? "E0 " : "");
  1581. } else {
  1582. pr_err("Error bits: %s%s%s%s%s%s%s\n",
  1583. reg_val & (1<<29) ? "ED " : "",
  1584. reg_val & (1<<28) ? "ET " : "",
  1585. reg_val & (1<<26) ? "EE " : "",
  1586. reg_val & (1<<25) ? "EB " : "",
  1587. reg_val & (1<<24) ? "EI " : "",
  1588. reg_val & (1<<23) ? "E1 " : "",
  1589. reg_val & (1<<22) ? "E0 " : "");
  1590. }
  1591. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  1592. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
  1593. if (reg_val & (1<<22))
  1594. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  1595. if (reg_val & (1<<23))
  1596. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  1597. #endif
  1598. panic("Can't handle the cache error!");
  1599. }
  1600. asmlinkage void do_ftlb(void)
  1601. {
  1602. const int field = 2 * sizeof(unsigned long);
  1603. unsigned int reg_val;
  1604. /* For the moment, report the problem and hang. */
  1605. if ((cpu_has_mips_r2_r6) &&
  1606. (((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS) ||
  1607. ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_LOONGSON))) {
  1608. pr_err("FTLB error exception, cp0_ecc=0x%08x:\n",
  1609. read_c0_ecc());
  1610. pr_err("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1611. reg_val = read_c0_cacheerr();
  1612. pr_err("c0_cacheerr == %08x\n", reg_val);
  1613. if ((reg_val & 0xc0000000) == 0xc0000000) {
  1614. pr_err("Decoded c0_cacheerr: FTLB parity error\n");
  1615. } else {
  1616. pr_err("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1617. reg_val & (1<<30) ? "secondary" : "primary",
  1618. reg_val & (1<<31) ? "data" : "insn");
  1619. }
  1620. } else {
  1621. pr_err("FTLB error exception\n");
  1622. }
  1623. /* Just print the cacheerr bits for now */
  1624. cache_parity_error();
  1625. }
  1626. asmlinkage void do_gsexc(struct pt_regs *regs, u32 diag1)
  1627. {
  1628. u32 exccode = (diag1 & LOONGSON_DIAG1_EXCCODE) >>
  1629. LOONGSON_DIAG1_EXCCODE_SHIFT;
  1630. enum ctx_state prev_state;
  1631. prev_state = exception_enter();
  1632. switch (exccode) {
  1633. case 0x08:
  1634. /* Undocumented exception, will trigger on certain
  1635. * also-undocumented instructions accessible from userspace.
  1636. * Processor state is not otherwise corrupted, but currently
  1637. * we don't know how to proceed. Maybe there is some
  1638. * undocumented control flag to enable the instructions?
  1639. */
  1640. force_sig(SIGILL);
  1641. break;
  1642. default:
  1643. /* None of the other exceptions, documented or not, have
  1644. * further details given; none are encountered in the wild
  1645. * either. Panic in case some of them turn out to be fatal.
  1646. */
  1647. show_regs(regs);
  1648. panic("Unhandled Loongson exception - GSCause = %08x", diag1);
  1649. }
  1650. exception_exit(prev_state);
  1651. }
  1652. /*
  1653. * SDBBP EJTAG debug exception handler.
  1654. * We skip the instruction and return to the next instruction.
  1655. */
  1656. void ejtag_exception_handler(struct pt_regs *regs)
  1657. {
  1658. const int field = 2 * sizeof(unsigned long);
  1659. unsigned long depc, old_epc, old_ra;
  1660. unsigned int debug;
  1661. printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  1662. depc = read_c0_depc();
  1663. debug = read_c0_debug();
  1664. printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  1665. if (debug & 0x80000000) {
  1666. /*
  1667. * In branch delay slot.
  1668. * We cheat a little bit here and use EPC to calculate the
  1669. * debug return address (DEPC). EPC is restored after the
  1670. * calculation.
  1671. */
  1672. old_epc = regs->cp0_epc;
  1673. old_ra = regs->regs[31];
  1674. regs->cp0_epc = depc;
  1675. compute_return_epc(regs);
  1676. depc = regs->cp0_epc;
  1677. regs->cp0_epc = old_epc;
  1678. regs->regs[31] = old_ra;
  1679. } else
  1680. depc += 4;
  1681. write_c0_depc(depc);
  1682. #if 0
  1683. printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
  1684. write_c0_debug(debug | 0x100);
  1685. #endif
  1686. }
  1687. /*
  1688. * NMI exception handler.
  1689. * No lock; only written during early bootup by CPU 0.
  1690. */
  1691. static RAW_NOTIFIER_HEAD(nmi_chain);
  1692. int register_nmi_notifier(struct notifier_block *nb)
  1693. {
  1694. return raw_notifier_chain_register(&nmi_chain, nb);
  1695. }
  1696. void __noreturn nmi_exception_handler(struct pt_regs *regs)
  1697. {
  1698. char str[100];
  1699. nmi_enter();
  1700. raw_notifier_call_chain(&nmi_chain, 0, regs);
  1701. bust_spinlocks(1);
  1702. snprintf(str, 100, "CPU%d NMI taken, CP0_EPC=%lx\n",
  1703. smp_processor_id(), regs->cp0_epc);
  1704. regs->cp0_epc = read_c0_errorepc();
  1705. die(str, regs);
  1706. nmi_exit();
  1707. }
  1708. unsigned long ebase;
  1709. EXPORT_SYMBOL_GPL(ebase);
  1710. unsigned long exception_handlers[32];
  1711. unsigned long vi_handlers[64];
  1712. void reserve_exception_space(phys_addr_t addr, unsigned long size)
  1713. {
  1714. /*
  1715. * reserve exception space on CPUs other than CPU0
  1716. * is too late, since memblock is unavailable when APs
  1717. * up
  1718. */
  1719. if (smp_processor_id() == 0)
  1720. memblock_reserve(addr, size);
  1721. }
  1722. void __init *set_except_vector(int n, void *addr)
  1723. {
  1724. unsigned long handler = (unsigned long) addr;
  1725. unsigned long old_handler;
  1726. #ifdef CONFIG_CPU_MICROMIPS
  1727. /*
  1728. * Only the TLB handlers are cache aligned with an even
  1729. * address. All other handlers are on an odd address and
  1730. * require no modification. Otherwise, MIPS32 mode will
  1731. * be entered when handling any TLB exceptions. That
  1732. * would be bad...since we must stay in microMIPS mode.
  1733. */
  1734. if (!(handler & 0x1))
  1735. handler |= 1;
  1736. #endif
  1737. old_handler = xchg(&exception_handlers[n], handler);
  1738. if (n == 0 && cpu_has_divec) {
  1739. #ifdef CONFIG_CPU_MICROMIPS
  1740. unsigned long jump_mask = ~((1 << 27) - 1);
  1741. #else
  1742. unsigned long jump_mask = ~((1 << 28) - 1);
  1743. #endif
  1744. u32 *buf = (u32 *)(ebase + 0x200);
  1745. if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
  1746. uasm_i_j(&buf, handler & ~jump_mask);
  1747. uasm_i_nop(&buf);
  1748. } else {
  1749. UASM_i_LA(&buf, GPR_K0, handler);
  1750. uasm_i_jr(&buf, GPR_K0);
  1751. uasm_i_nop(&buf);
  1752. }
  1753. local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
  1754. }
  1755. return (void *)old_handler;
  1756. }
  1757. static void do_default_vi(void)
  1758. {
  1759. show_regs(get_irq_regs());
  1760. panic("Caught unexpected vectored interrupt.");
  1761. }
  1762. void *set_vi_handler(int n, vi_handler_t addr)
  1763. {
  1764. extern const u8 except_vec_vi[];
  1765. extern const u8 except_vec_vi_ori[], except_vec_vi_end[];
  1766. extern const u8 rollback_except_vec_vi[];
  1767. unsigned long handler;
  1768. unsigned long old_handler = vi_handlers[n];
  1769. int srssets = current_cpu_data.srsets;
  1770. u16 *h;
  1771. unsigned char *b;
  1772. const u8 *vec_start;
  1773. int ori_offset;
  1774. int handler_len;
  1775. BUG_ON(!cpu_has_veic && !cpu_has_vint);
  1776. if (addr == NULL) {
  1777. handler = (unsigned long) do_default_vi;
  1778. } else
  1779. handler = (unsigned long) addr;
  1780. vi_handlers[n] = handler;
  1781. b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
  1782. if (cpu_has_veic) {
  1783. if (board_bind_eic_interrupt)
  1784. board_bind_eic_interrupt(n, 0);
  1785. } else if (cpu_has_vint) {
  1786. /* SRSMap is only defined if shadow sets are implemented */
  1787. if (srssets > 1)
  1788. change_c0_srsmap(0xf << n*4, 0 << n*4);
  1789. }
  1790. vec_start = using_rollback_handler() ? rollback_except_vec_vi :
  1791. except_vec_vi;
  1792. #if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN)
  1793. ori_offset = except_vec_vi_ori - vec_start + 2;
  1794. #else
  1795. ori_offset = except_vec_vi_ori - vec_start;
  1796. #endif
  1797. handler_len = except_vec_vi_end - vec_start;
  1798. if (handler_len > VECTORSPACING) {
  1799. /*
  1800. * Sigh... panicing won't help as the console
  1801. * is probably not configured :(
  1802. */
  1803. panic("VECTORSPACING too small");
  1804. }
  1805. set_handler(((unsigned long)b - ebase), vec_start,
  1806. #ifdef CONFIG_CPU_MICROMIPS
  1807. (handler_len - 1));
  1808. #else
  1809. handler_len);
  1810. #endif
  1811. /* insert offset into vi_handlers[] */
  1812. h = (u16 *)(b + ori_offset);
  1813. *h = n * sizeof(handler);
  1814. local_flush_icache_range((unsigned long)b,
  1815. (unsigned long)(b+handler_len));
  1816. return (void *)old_handler;
  1817. }
  1818. /*
  1819. * Timer interrupt
  1820. */
  1821. int cp0_compare_irq;
  1822. EXPORT_SYMBOL_GPL(cp0_compare_irq);
  1823. int cp0_compare_irq_shift;
  1824. /*
  1825. * Performance counter IRQ or -1 if shared with timer
  1826. */
  1827. int cp0_perfcount_irq;
  1828. EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
  1829. /*
  1830. * Fast debug channel IRQ or -1 if not present
  1831. */
  1832. int cp0_fdc_irq;
  1833. EXPORT_SYMBOL_GPL(cp0_fdc_irq);
  1834. static int noulri;
  1835. static int __init ulri_disable(char *s)
  1836. {
  1837. pr_info("Disabling ulri\n");
  1838. noulri = 1;
  1839. return 1;
  1840. }
  1841. __setup("noulri", ulri_disable);
  1842. /* configure STATUS register */
  1843. static void configure_status(void)
  1844. {
  1845. /*
  1846. * Disable coprocessors and select 32-bit or 64-bit addressing
  1847. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  1848. * flag that some firmware may have left set and the TS bit (for
  1849. * IP27). Set XX for ISA IV code to work.
  1850. */
  1851. unsigned int status_set = ST0_KERNEL_CUMASK;
  1852. #ifdef CONFIG_64BIT
  1853. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  1854. #endif
  1855. if (current_cpu_data.isa_level & MIPS_CPU_ISA_IV)
  1856. status_set |= ST0_XX;
  1857. if (cpu_has_dsp)
  1858. status_set |= ST0_MX;
  1859. change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  1860. status_set);
  1861. back_to_back_c0_hazard();
  1862. }
  1863. unsigned int hwrena;
  1864. EXPORT_SYMBOL_GPL(hwrena);
  1865. /* configure HWRENA register */
  1866. static void configure_hwrena(void)
  1867. {
  1868. hwrena = cpu_hwrena_impl_bits;
  1869. if (cpu_has_mips_r2_r6)
  1870. hwrena |= MIPS_HWRENA_CPUNUM |
  1871. MIPS_HWRENA_SYNCISTEP |
  1872. MIPS_HWRENA_CC |
  1873. MIPS_HWRENA_CCRES;
  1874. if (!noulri && cpu_has_userlocal)
  1875. hwrena |= MIPS_HWRENA_ULR;
  1876. if (hwrena)
  1877. write_c0_hwrena(hwrena);
  1878. }
  1879. static void configure_exception_vector(void)
  1880. {
  1881. if (cpu_has_mips_r2_r6) {
  1882. unsigned long sr = set_c0_status(ST0_BEV);
  1883. /* If available, use WG to set top bits of EBASE */
  1884. if (cpu_has_ebase_wg) {
  1885. #ifdef CONFIG_64BIT
  1886. write_c0_ebase_64(ebase | MIPS_EBASE_WG);
  1887. #else
  1888. write_c0_ebase(ebase | MIPS_EBASE_WG);
  1889. #endif
  1890. }
  1891. write_c0_ebase(ebase);
  1892. write_c0_status(sr);
  1893. }
  1894. if (cpu_has_veic || cpu_has_vint) {
  1895. /* Setting vector spacing enables EI/VI mode */
  1896. change_c0_intctl(0x3e0, VECTORSPACING);
  1897. }
  1898. if (cpu_has_divec) {
  1899. if (cpu_has_mipsmt) {
  1900. unsigned int vpflags = dvpe();
  1901. set_c0_cause(CAUSEF_IV);
  1902. evpe(vpflags);
  1903. } else
  1904. set_c0_cause(CAUSEF_IV);
  1905. }
  1906. }
  1907. void per_cpu_trap_init(bool is_boot_cpu)
  1908. {
  1909. unsigned int cpu = smp_processor_id();
  1910. configure_status();
  1911. configure_hwrena();
  1912. configure_exception_vector();
  1913. /*
  1914. * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
  1915. *
  1916. * o read IntCtl.IPTI to determine the timer interrupt
  1917. * o read IntCtl.IPPCI to determine the performance counter interrupt
  1918. * o read IntCtl.IPFDC to determine the fast debug channel interrupt
  1919. */
  1920. if (cpu_has_mips_r2_r6) {
  1921. cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
  1922. cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
  1923. cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
  1924. cp0_fdc_irq = (read_c0_intctl() >> INTCTLB_IPFDC) & 7;
  1925. if (!cp0_fdc_irq)
  1926. cp0_fdc_irq = -1;
  1927. } else {
  1928. cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
  1929. cp0_compare_irq_shift = CP0_LEGACY_PERFCNT_IRQ;
  1930. cp0_perfcount_irq = -1;
  1931. cp0_fdc_irq = -1;
  1932. }
  1933. if (cpu_has_mmid)
  1934. cpu_data[cpu].asid_cache = 0;
  1935. else if (!cpu_data[cpu].asid_cache)
  1936. cpu_data[cpu].asid_cache = asid_first_version(cpu);
  1937. mmgrab(&init_mm);
  1938. current->active_mm = &init_mm;
  1939. BUG_ON(current->mm);
  1940. enter_lazy_tlb(&init_mm, current);
  1941. /* Boot CPU's cache setup in setup_arch(). */
  1942. if (!is_boot_cpu)
  1943. cpu_cache_init();
  1944. tlb_init();
  1945. TLBMISS_HANDLER_SETUP();
  1946. }
  1947. /* Install CPU exception handler */
  1948. void set_handler(unsigned long offset, const void *addr, unsigned long size)
  1949. {
  1950. #ifdef CONFIG_CPU_MICROMIPS
  1951. memcpy((void *)(ebase + offset), ((unsigned char *)addr - 1), size);
  1952. #else
  1953. memcpy((void *)(ebase + offset), addr, size);
  1954. #endif
  1955. local_flush_icache_range(ebase + offset, ebase + offset + size);
  1956. }
  1957. static const char panic_null_cerr[] =
  1958. "Trying to set NULL cache error exception handler\n";
  1959. /*
  1960. * Install uncached CPU exception handler.
  1961. * This is suitable only for the cache error exception which is the only
  1962. * exception handler that is being run uncached.
  1963. */
  1964. void set_uncached_handler(unsigned long offset, void *addr,
  1965. unsigned long size)
  1966. {
  1967. unsigned long uncached_ebase = CKSEG1ADDR_OR_64BIT(__pa(ebase));
  1968. if (!addr)
  1969. panic(panic_null_cerr);
  1970. memcpy((void *)(uncached_ebase + offset), addr, size);
  1971. }
  1972. static int __initdata rdhwr_noopt;
  1973. static int __init set_rdhwr_noopt(char *str)
  1974. {
  1975. rdhwr_noopt = 1;
  1976. return 1;
  1977. }
  1978. __setup("rdhwr_noopt", set_rdhwr_noopt);
  1979. void __init trap_init(void)
  1980. {
  1981. extern char except_vec3_generic;
  1982. extern char except_vec4;
  1983. extern char except_vec3_r4000;
  1984. unsigned long i, vec_size;
  1985. phys_addr_t ebase_pa;
  1986. check_wait();
  1987. if (!cpu_has_mips_r2_r6) {
  1988. ebase = CAC_BASE;
  1989. vec_size = 0x400;
  1990. } else {
  1991. if (cpu_has_veic || cpu_has_vint)
  1992. vec_size = 0x200 + VECTORSPACING*64;
  1993. else
  1994. vec_size = PAGE_SIZE;
  1995. ebase_pa = memblock_phys_alloc(vec_size, 1 << fls(vec_size));
  1996. if (!ebase_pa)
  1997. panic("%s: Failed to allocate %lu bytes align=0x%x\n",
  1998. __func__, vec_size, 1 << fls(vec_size));
  1999. /*
  2000. * Try to ensure ebase resides in KSeg0 if possible.
  2001. *
  2002. * It shouldn't generally be in XKPhys on MIPS64 to avoid
  2003. * hitting a poorly defined exception base for Cache Errors.
  2004. * The allocation is likely to be in the low 512MB of physical,
  2005. * in which case we should be able to convert to KSeg0.
  2006. *
  2007. * EVA is special though as it allows segments to be rearranged
  2008. * and to become uncached during cache error handling.
  2009. */
  2010. if (!IS_ENABLED(CONFIG_EVA) && ebase_pa < 0x20000000)
  2011. ebase = CKSEG0ADDR(ebase_pa);
  2012. else
  2013. ebase = (unsigned long)phys_to_virt(ebase_pa);
  2014. if (ebase_pa >= 0x20000000)
  2015. pr_warn("ebase(%pa) should better be in KSeg0",
  2016. &ebase_pa);
  2017. }
  2018. if (cpu_has_mmips) {
  2019. unsigned int config3 = read_c0_config3();
  2020. if (IS_ENABLED(CONFIG_CPU_MICROMIPS))
  2021. write_c0_config3(config3 | MIPS_CONF3_ISA_OE);
  2022. else
  2023. write_c0_config3(config3 & ~MIPS_CONF3_ISA_OE);
  2024. }
  2025. if (board_ebase_setup)
  2026. board_ebase_setup();
  2027. per_cpu_trap_init(true);
  2028. memblock_set_bottom_up(false);
  2029. /*
  2030. * Copy the generic exception handlers to their final destination.
  2031. * This will be overridden later as suitable for a particular
  2032. * configuration.
  2033. */
  2034. set_handler(0x180, &except_vec3_generic, 0x80);
  2035. /*
  2036. * Setup default vectors
  2037. */
  2038. for (i = 0; i <= 31; i++)
  2039. set_except_vector(i, handle_reserved);
  2040. /*
  2041. * Copy the EJTAG debug exception vector handler code to its final
  2042. * destination.
  2043. */
  2044. if (cpu_has_ejtag && board_ejtag_handler_setup)
  2045. board_ejtag_handler_setup();
  2046. /*
  2047. * Only some CPUs have the watch exceptions.
  2048. */
  2049. if (cpu_has_watch)
  2050. set_except_vector(EXCCODE_WATCH, handle_watch);
  2051. /*
  2052. * Initialise interrupt handlers
  2053. */
  2054. if (cpu_has_veic || cpu_has_vint) {
  2055. int nvec = cpu_has_veic ? 64 : 8;
  2056. for (i = 0; i < nvec; i++)
  2057. set_vi_handler(i, NULL);
  2058. }
  2059. else if (cpu_has_divec)
  2060. set_handler(0x200, &except_vec4, 0x8);
  2061. /*
  2062. * Some CPUs can enable/disable for cache parity detection, but does
  2063. * it different ways.
  2064. */
  2065. parity_protection_init();
  2066. /*
  2067. * The Data Bus Errors / Instruction Bus Errors are signaled
  2068. * by external hardware. Therefore these two exceptions
  2069. * may have board specific handlers.
  2070. */
  2071. if (board_be_init)
  2072. board_be_init();
  2073. set_except_vector(EXCCODE_INT, using_rollback_handler() ?
  2074. rollback_handle_int : handle_int);
  2075. set_except_vector(EXCCODE_MOD, handle_tlbm);
  2076. set_except_vector(EXCCODE_TLBL, handle_tlbl);
  2077. set_except_vector(EXCCODE_TLBS, handle_tlbs);
  2078. set_except_vector(EXCCODE_ADEL, handle_adel);
  2079. set_except_vector(EXCCODE_ADES, handle_ades);
  2080. set_except_vector(EXCCODE_IBE, handle_ibe);
  2081. set_except_vector(EXCCODE_DBE, handle_dbe);
  2082. set_except_vector(EXCCODE_SYS, handle_sys);
  2083. set_except_vector(EXCCODE_BP, handle_bp);
  2084. if (rdhwr_noopt)
  2085. set_except_vector(EXCCODE_RI, handle_ri);
  2086. else {
  2087. if (cpu_has_vtag_icache)
  2088. set_except_vector(EXCCODE_RI, handle_ri_rdhwr_tlbp);
  2089. else if (current_cpu_type() == CPU_LOONGSON64)
  2090. set_except_vector(EXCCODE_RI, handle_ri_rdhwr_tlbp);
  2091. else
  2092. set_except_vector(EXCCODE_RI, handle_ri_rdhwr);
  2093. }
  2094. set_except_vector(EXCCODE_CPU, handle_cpu);
  2095. set_except_vector(EXCCODE_OV, handle_ov);
  2096. set_except_vector(EXCCODE_TR, handle_tr);
  2097. set_except_vector(EXCCODE_MSAFPE, handle_msa_fpe);
  2098. if (board_nmi_handler_setup)
  2099. board_nmi_handler_setup();
  2100. if (cpu_has_fpu && !cpu_has_nofpuex)
  2101. set_except_vector(EXCCODE_FPE, handle_fpe);
  2102. if (cpu_has_ftlbparex)
  2103. set_except_vector(MIPS_EXCCODE_TLBPAR, handle_ftlb);
  2104. if (cpu_has_gsexcex)
  2105. set_except_vector(LOONGSON_EXCCODE_GSEXC, handle_gsexc);
  2106. if (cpu_has_rixiex) {
  2107. set_except_vector(EXCCODE_TLBRI, tlb_do_page_fault_0);
  2108. set_except_vector(EXCCODE_TLBXI, tlb_do_page_fault_0);
  2109. }
  2110. set_except_vector(EXCCODE_MSADIS, handle_msa);
  2111. set_except_vector(EXCCODE_MDMX, handle_mdmx);
  2112. if (cpu_has_mcheck)
  2113. set_except_vector(EXCCODE_MCHECK, handle_mcheck);
  2114. if (cpu_has_mipsmt)
  2115. set_except_vector(EXCCODE_THREAD, handle_mt);
  2116. set_except_vector(EXCCODE_DSPDIS, handle_dsp);
  2117. if (board_cache_error_setup)
  2118. board_cache_error_setup();
  2119. if (cpu_has_vce)
  2120. /* Special exception: R4[04]00 uses also the divec space. */
  2121. set_handler(0x180, &except_vec3_r4000, 0x100);
  2122. else if (cpu_has_4kex)
  2123. set_handler(0x180, &except_vec3_generic, 0x80);
  2124. else
  2125. set_handler(0x080, &except_vec3_generic, 0x80);
  2126. local_flush_icache_range(ebase, ebase + vec_size);
  2127. sort_extable(__start___dbe_table, __stop___dbe_table);
  2128. cu2_notifier(default_cu2_call, 0x80000000); /* Run last */
  2129. }
  2130. static int trap_pm_notifier(struct notifier_block *self, unsigned long cmd,
  2131. void *v)
  2132. {
  2133. switch (cmd) {
  2134. case CPU_PM_ENTER_FAILED:
  2135. case CPU_PM_EXIT:
  2136. configure_status();
  2137. configure_hwrena();
  2138. configure_exception_vector();
  2139. /* Restore register with CPU number for TLB handlers */
  2140. TLBMISS_HANDLER_RESTORE();
  2141. break;
  2142. }
  2143. return NOTIFY_OK;
  2144. }
  2145. static struct notifier_block trap_pm_notifier_block = {
  2146. .notifier_call = trap_pm_notifier,
  2147. };
  2148. static int __init trap_pm_init(void)
  2149. {
  2150. return cpu_pm_register_notifier(&trap_pm_notifier_block);
  2151. }
  2152. arch_initcall(trap_pm_init);