traps.c 56 KB

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