traps.c 49 KB

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