traps.c 42 KB

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