traps.c 40 KB

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