traps.c 41 KB

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