traps.c 36 KB

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