traps.c 42 KB

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