traps.c 54 KB

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