traps.c 44 KB

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