traps.c 43 KB

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