traps.c 49 KB

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