traps.c 49 KB

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