traps.c 54 KB

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