traps.c 58 KB

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