traps.c 41 KB

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