traps.c 58 KB

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