traps.c 38 KB

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