traps.c 45 KB

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