traps.c 43 KB

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