traps.c 43 KB

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