traps.c 43 KB

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