traps.c 45 KB

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