traps.c 60 KB

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