traps.c 44 KB

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