traps.c 58 KB

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