traps.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. show_regs(regs);
  692. dump_tlb_all();
  693. /*
  694. * Some chips may have other causes of machine check (e.g. SB1
  695. * graduation timer)
  696. */
  697. panic("Caught Machine Check exception - %scaused by multiple "
  698. "matching entries in the TLB.",
  699. (regs->cp0_status & ST0_TS) ? "" : "not ");
  700. }
  701. asmlinkage void do_mt(struct pt_regs *regs)
  702. {
  703. int subcode;
  704. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  705. subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
  706. >> VPECONTROL_EXCPT_SHIFT;
  707. switch (subcode) {
  708. case 0:
  709. printk(KERN_ERR "Thread Underflow\n");
  710. break;
  711. case 1:
  712. printk(KERN_ERR "Thread Overflow\n");
  713. break;
  714. case 2:
  715. printk(KERN_ERR "Invalid YIELD Qualifier\n");
  716. break;
  717. case 3:
  718. printk(KERN_ERR "Gating Storage Exception\n");
  719. break;
  720. case 4:
  721. printk(KERN_ERR "YIELD Scheduler Exception\n");
  722. break;
  723. case 5:
  724. printk(KERN_ERR "Gating Storage Schedulier Exception\n");
  725. break;
  726. default:
  727. printk(KERN_ERR "*** UNKNOWN THREAD EXCEPTION %d ***\n",
  728. subcode);
  729. break;
  730. }
  731. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  732. force_sig(SIGILL, current);
  733. }
  734. asmlinkage void do_dsp(struct pt_regs *regs)
  735. {
  736. if (cpu_has_dsp)
  737. panic("Unexpected DSP exception\n");
  738. force_sig(SIGILL, current);
  739. }
  740. asmlinkage void do_reserved(struct pt_regs *regs)
  741. {
  742. /*
  743. * Game over - no way to handle this if it ever occurs. Most probably
  744. * caused by a new unknown cpu type or after another deadly
  745. * hard/software error.
  746. */
  747. show_regs(regs);
  748. panic("Caught reserved exception %ld - should not happen.",
  749. (regs->cp0_cause & 0x7f) >> 2);
  750. }
  751. asmlinkage void do_default_vi(struct pt_regs *regs)
  752. {
  753. show_regs(regs);
  754. panic("Caught unexpected vectored interrupt.");
  755. }
  756. /*
  757. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  758. * it different ways.
  759. */
  760. static inline void parity_protection_init(void)
  761. {
  762. switch (current_cpu_data.cputype) {
  763. case CPU_24K:
  764. case CPU_34K:
  765. case CPU_5KC:
  766. write_c0_ecc(0x80000000);
  767. back_to_back_c0_hazard();
  768. /* Set the PE bit (bit 31) in the c0_errctl register. */
  769. printk(KERN_INFO "Cache parity protection %sabled\n",
  770. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  771. break;
  772. case CPU_20KC:
  773. case CPU_25KF:
  774. /* Clear the DE bit (bit 16) in the c0_status register. */
  775. printk(KERN_INFO "Enable cache parity protection for "
  776. "MIPS 20KC/25KF CPUs.\n");
  777. clear_c0_status(ST0_DE);
  778. break;
  779. default:
  780. break;
  781. }
  782. }
  783. asmlinkage void cache_parity_error(void)
  784. {
  785. const int field = 2 * sizeof(unsigned long);
  786. unsigned int reg_val;
  787. /* For the moment, report the problem and hang. */
  788. printk("Cache error exception:\n");
  789. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  790. reg_val = read_c0_cacheerr();
  791. printk("c0_cacheerr == %08x\n", reg_val);
  792. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  793. reg_val & (1<<30) ? "secondary" : "primary",
  794. reg_val & (1<<31) ? "data" : "insn");
  795. printk("Error bits: %s%s%s%s%s%s%s\n",
  796. reg_val & (1<<29) ? "ED " : "",
  797. reg_val & (1<<28) ? "ET " : "",
  798. reg_val & (1<<26) ? "EE " : "",
  799. reg_val & (1<<25) ? "EB " : "",
  800. reg_val & (1<<24) ? "EI " : "",
  801. reg_val & (1<<23) ? "E1 " : "",
  802. reg_val & (1<<22) ? "E0 " : "");
  803. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  804. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
  805. if (reg_val & (1<<22))
  806. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  807. if (reg_val & (1<<23))
  808. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  809. #endif
  810. panic("Can't handle the cache error!");
  811. }
  812. /*
  813. * SDBBP EJTAG debug exception handler.
  814. * We skip the instruction and return to the next instruction.
  815. */
  816. void ejtag_exception_handler(struct pt_regs *regs)
  817. {
  818. const int field = 2 * sizeof(unsigned long);
  819. unsigned long depc, old_epc;
  820. unsigned int debug;
  821. printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  822. depc = read_c0_depc();
  823. debug = read_c0_debug();
  824. printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  825. if (debug & 0x80000000) {
  826. /*
  827. * In branch delay slot.
  828. * We cheat a little bit here and use EPC to calculate the
  829. * debug return address (DEPC). EPC is restored after the
  830. * calculation.
  831. */
  832. old_epc = regs->cp0_epc;
  833. regs->cp0_epc = depc;
  834. __compute_return_epc(regs);
  835. depc = regs->cp0_epc;
  836. regs->cp0_epc = old_epc;
  837. } else
  838. depc += 4;
  839. write_c0_depc(depc);
  840. #if 0
  841. printk("\n\n----- Enable EJTAG single stepping ----\n\n");
  842. write_c0_debug(debug | 0x100);
  843. #endif
  844. }
  845. /*
  846. * NMI exception handler.
  847. */
  848. void nmi_exception_handler(struct pt_regs *regs)
  849. {
  850. #ifdef CONFIG_MIPS_MT_SMTC
  851. unsigned long dvpret = dvpe();
  852. bust_spinlocks(1);
  853. printk("NMI taken!!!!\n");
  854. mips_mt_regdump(dvpret);
  855. #else
  856. bust_spinlocks(1);
  857. printk("NMI taken!!!!\n");
  858. #endif /* CONFIG_MIPS_MT_SMTC */
  859. die("NMI", regs);
  860. while(1) ;
  861. }
  862. #define VECTORSPACING 0x100 /* for EI/VI mode */
  863. unsigned long ebase;
  864. unsigned long exception_handlers[32];
  865. unsigned long vi_handlers[64];
  866. /*
  867. * As a side effect of the way this is implemented we're limited
  868. * to interrupt handlers in the address range from
  869. * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
  870. */
  871. void *set_except_vector(int n, void *addr)
  872. {
  873. unsigned long handler = (unsigned long) addr;
  874. unsigned long old_handler = exception_handlers[n];
  875. exception_handlers[n] = handler;
  876. if (n == 0 && cpu_has_divec) {
  877. *(volatile u32 *)(ebase + 0x200) = 0x08000000 |
  878. (0x03ffffff & (handler >> 2));
  879. flush_icache_range(ebase + 0x200, ebase + 0x204);
  880. }
  881. return (void *)old_handler;
  882. }
  883. #ifdef CONFIG_CPU_MIPSR2
  884. /*
  885. * MIPSR2 shadow register set allocation
  886. * FIXME: SMP...
  887. */
  888. static struct shadow_registers {
  889. /*
  890. * Number of shadow register sets supported
  891. */
  892. unsigned long sr_supported;
  893. /*
  894. * Bitmap of allocated shadow registers
  895. */
  896. unsigned long sr_allocated;
  897. } shadow_registers;
  898. static void mips_srs_init(void)
  899. {
  900. #ifdef CONFIG_CPU_MIPSR2_SRS
  901. shadow_registers.sr_supported = ((read_c0_srsctl() >> 26) & 0x0f) + 1;
  902. printk(KERN_INFO "%d MIPSR2 register sets available\n",
  903. shadow_registers.sr_supported);
  904. #endif
  905. shadow_registers.sr_allocated = 1; /* Set 0 used by kernel */
  906. }
  907. int mips_srs_max(void)
  908. {
  909. return shadow_registers.sr_supported;
  910. }
  911. int mips_srs_alloc(void)
  912. {
  913. struct shadow_registers *sr = &shadow_registers;
  914. int set;
  915. again:
  916. set = find_first_zero_bit(&sr->sr_allocated, sr->sr_supported);
  917. if (set >= sr->sr_supported)
  918. return -1;
  919. if (test_and_set_bit(set, &sr->sr_allocated))
  920. goto again;
  921. return set;
  922. }
  923. void mips_srs_free(int set)
  924. {
  925. struct shadow_registers *sr = &shadow_registers;
  926. clear_bit(set, &sr->sr_allocated);
  927. }
  928. static void *set_vi_srs_handler(int n, void *addr, int srs)
  929. {
  930. unsigned long handler;
  931. unsigned long old_handler = vi_handlers[n];
  932. u32 *w;
  933. unsigned char *b;
  934. if (!cpu_has_veic && !cpu_has_vint)
  935. BUG();
  936. if (addr == NULL) {
  937. handler = (unsigned long) do_default_vi;
  938. srs = 0;
  939. } else
  940. handler = (unsigned long) addr;
  941. vi_handlers[n] = (unsigned long) addr;
  942. b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
  943. if (srs >= mips_srs_max())
  944. panic("Shadow register set %d not supported", srs);
  945. if (cpu_has_veic) {
  946. if (board_bind_eic_interrupt)
  947. board_bind_eic_interrupt (n, srs);
  948. } else if (cpu_has_vint) {
  949. /* SRSMap is only defined if shadow sets are implemented */
  950. if (mips_srs_max() > 1)
  951. change_c0_srsmap (0xf << n*4, srs << n*4);
  952. }
  953. if (srs == 0) {
  954. /*
  955. * If no shadow set is selected then use the default handler
  956. * that does normal register saving and a standard interrupt exit
  957. */
  958. extern char except_vec_vi, except_vec_vi_lui;
  959. extern char except_vec_vi_ori, except_vec_vi_end;
  960. #ifdef CONFIG_MIPS_MT_SMTC
  961. /*
  962. * We need to provide the SMTC vectored interrupt handler
  963. * not only with the address of the handler, but with the
  964. * Status.IM bit to be masked before going there.
  965. */
  966. extern char except_vec_vi_mori;
  967. const int mori_offset = &except_vec_vi_mori - &except_vec_vi;
  968. #endif /* CONFIG_MIPS_MT_SMTC */
  969. const int handler_len = &except_vec_vi_end - &except_vec_vi;
  970. const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
  971. const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
  972. if (handler_len > VECTORSPACING) {
  973. /*
  974. * Sigh... panicing won't help as the console
  975. * is probably not configured :(
  976. */
  977. panic ("VECTORSPACING too small");
  978. }
  979. memcpy (b, &except_vec_vi, handler_len);
  980. #ifdef CONFIG_MIPS_MT_SMTC
  981. if (n > 7)
  982. printk("Vector index %d exceeds SMTC maximum\n", n);
  983. w = (u32 *)(b + mori_offset);
  984. *w = (*w & 0xffff0000) | (0x100 << n);
  985. #endif /* CONFIG_MIPS_MT_SMTC */
  986. w = (u32 *)(b + lui_offset);
  987. *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
  988. w = (u32 *)(b + ori_offset);
  989. *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
  990. flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
  991. }
  992. else {
  993. /*
  994. * In other cases jump directly to the interrupt handler
  995. *
  996. * It is the handlers responsibility to save registers if required
  997. * (eg hi/lo) and return from the exception using "eret"
  998. */
  999. w = (u32 *)b;
  1000. *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
  1001. *w = 0;
  1002. flush_icache_range((unsigned long)b, (unsigned long)(b+8));
  1003. }
  1004. return (void *)old_handler;
  1005. }
  1006. void *set_vi_handler(int n, void *addr)
  1007. {
  1008. return set_vi_srs_handler(n, addr, 0);
  1009. }
  1010. #endif
  1011. /*
  1012. * This is used by native signal handling
  1013. */
  1014. asmlinkage int (*save_fp_context)(struct sigcontext *sc);
  1015. asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
  1016. extern asmlinkage int _save_fp_context(struct sigcontext *sc);
  1017. extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
  1018. extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
  1019. extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
  1020. #ifdef CONFIG_SMP
  1021. static int smp_save_fp_context(struct sigcontext *sc)
  1022. {
  1023. return cpu_has_fpu
  1024. ? _save_fp_context(sc)
  1025. : fpu_emulator_save_context(sc);
  1026. }
  1027. static int smp_restore_fp_context(struct sigcontext *sc)
  1028. {
  1029. return cpu_has_fpu
  1030. ? _restore_fp_context(sc)
  1031. : fpu_emulator_restore_context(sc);
  1032. }
  1033. #endif
  1034. static inline void signal_init(void)
  1035. {
  1036. #ifdef CONFIG_SMP
  1037. /* For now just do the cpu_has_fpu check when the functions are invoked */
  1038. save_fp_context = smp_save_fp_context;
  1039. restore_fp_context = smp_restore_fp_context;
  1040. #else
  1041. if (cpu_has_fpu) {
  1042. save_fp_context = _save_fp_context;
  1043. restore_fp_context = _restore_fp_context;
  1044. } else {
  1045. save_fp_context = fpu_emulator_save_context;
  1046. restore_fp_context = fpu_emulator_restore_context;
  1047. }
  1048. #endif
  1049. }
  1050. #ifdef CONFIG_MIPS32_COMPAT
  1051. /*
  1052. * This is used by 32-bit signal stuff on the 64-bit kernel
  1053. */
  1054. asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
  1055. asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
  1056. extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
  1057. extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
  1058. extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
  1059. extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
  1060. static inline void signal32_init(void)
  1061. {
  1062. if (cpu_has_fpu) {
  1063. save_fp_context32 = _save_fp_context32;
  1064. restore_fp_context32 = _restore_fp_context32;
  1065. } else {
  1066. save_fp_context32 = fpu_emulator_save_context32;
  1067. restore_fp_context32 = fpu_emulator_restore_context32;
  1068. }
  1069. }
  1070. #endif
  1071. extern void cpu_cache_init(void);
  1072. extern void tlb_init(void);
  1073. extern void flush_tlb_handlers(void);
  1074. void __init per_cpu_trap_init(void)
  1075. {
  1076. unsigned int cpu = smp_processor_id();
  1077. unsigned int status_set = ST0_CU0;
  1078. #ifdef CONFIG_MIPS_MT_SMTC
  1079. int secondaryTC = 0;
  1080. int bootTC = (cpu == 0);
  1081. /*
  1082. * Only do per_cpu_trap_init() for first TC of Each VPE.
  1083. * Note that this hack assumes that the SMTC init code
  1084. * assigns TCs consecutively and in ascending order.
  1085. */
  1086. if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
  1087. ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
  1088. secondaryTC = 1;
  1089. #endif /* CONFIG_MIPS_MT_SMTC */
  1090. /*
  1091. * Disable coprocessors and select 32-bit or 64-bit addressing
  1092. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  1093. * flag that some firmware may have left set and the TS bit (for
  1094. * IP27). Set XX for ISA IV code to work.
  1095. */
  1096. #ifdef CONFIG_64BIT
  1097. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  1098. #endif
  1099. if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
  1100. status_set |= ST0_XX;
  1101. change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  1102. status_set);
  1103. if (cpu_has_dsp)
  1104. set_c0_status(ST0_MX);
  1105. #ifdef CONFIG_CPU_MIPSR2
  1106. write_c0_hwrena (0x0000000f); /* Allow rdhwr to all registers */
  1107. #endif
  1108. #ifdef CONFIG_MIPS_MT_SMTC
  1109. if (!secondaryTC) {
  1110. #endif /* CONFIG_MIPS_MT_SMTC */
  1111. /*
  1112. * Interrupt handling.
  1113. */
  1114. if (cpu_has_veic || cpu_has_vint) {
  1115. write_c0_ebase (ebase);
  1116. /* Setting vector spacing enables EI/VI mode */
  1117. change_c0_intctl (0x3e0, VECTORSPACING);
  1118. }
  1119. if (cpu_has_divec) {
  1120. if (cpu_has_mipsmt) {
  1121. unsigned int vpflags = dvpe();
  1122. set_c0_cause(CAUSEF_IV);
  1123. evpe(vpflags);
  1124. } else
  1125. set_c0_cause(CAUSEF_IV);
  1126. }
  1127. #ifdef CONFIG_MIPS_MT_SMTC
  1128. }
  1129. #endif /* CONFIG_MIPS_MT_SMTC */
  1130. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  1131. TLBMISS_HANDLER_SETUP();
  1132. atomic_inc(&init_mm.mm_count);
  1133. current->active_mm = &init_mm;
  1134. BUG_ON(current->mm);
  1135. enter_lazy_tlb(&init_mm, current);
  1136. #ifdef CONFIG_MIPS_MT_SMTC
  1137. if (bootTC) {
  1138. #endif /* CONFIG_MIPS_MT_SMTC */
  1139. cpu_cache_init();
  1140. tlb_init();
  1141. #ifdef CONFIG_MIPS_MT_SMTC
  1142. }
  1143. #endif /* CONFIG_MIPS_MT_SMTC */
  1144. }
  1145. /* Install CPU exception handler */
  1146. void __init set_handler (unsigned long offset, void *addr, unsigned long size)
  1147. {
  1148. memcpy((void *)(ebase + offset), addr, size);
  1149. flush_icache_range(ebase + offset, ebase + offset + size);
  1150. }
  1151. /* Install uncached CPU exception handler */
  1152. void __init set_uncached_handler (unsigned long offset, void *addr, unsigned long size)
  1153. {
  1154. #ifdef CONFIG_32BIT
  1155. unsigned long uncached_ebase = KSEG1ADDR(ebase);
  1156. #endif
  1157. #ifdef CONFIG_64BIT
  1158. unsigned long uncached_ebase = TO_UNCAC(ebase);
  1159. #endif
  1160. memcpy((void *)(uncached_ebase + offset), addr, size);
  1161. }
  1162. void __init trap_init(void)
  1163. {
  1164. extern char except_vec3_generic, except_vec3_r4000;
  1165. extern char except_vec4;
  1166. unsigned long i;
  1167. if (cpu_has_veic || cpu_has_vint)
  1168. ebase = (unsigned long) alloc_bootmem_low_pages (0x200 + VECTORSPACING*64);
  1169. else
  1170. ebase = CAC_BASE;
  1171. #ifdef CONFIG_CPU_MIPSR2
  1172. mips_srs_init();
  1173. #endif
  1174. per_cpu_trap_init();
  1175. /*
  1176. * Copy the generic exception handlers to their final destination.
  1177. * This will be overriden later as suitable for a particular
  1178. * configuration.
  1179. */
  1180. set_handler(0x180, &except_vec3_generic, 0x80);
  1181. /*
  1182. * Setup default vectors
  1183. */
  1184. for (i = 0; i <= 31; i++)
  1185. set_except_vector(i, handle_reserved);
  1186. /*
  1187. * Copy the EJTAG debug exception vector handler code to it's final
  1188. * destination.
  1189. */
  1190. if (cpu_has_ejtag && board_ejtag_handler_setup)
  1191. board_ejtag_handler_setup ();
  1192. /*
  1193. * Only some CPUs have the watch exceptions.
  1194. */
  1195. if (cpu_has_watch)
  1196. set_except_vector(23, handle_watch);
  1197. /*
  1198. * Initialise interrupt handlers
  1199. */
  1200. if (cpu_has_veic || cpu_has_vint) {
  1201. int nvec = cpu_has_veic ? 64 : 8;
  1202. for (i = 0; i < nvec; i++)
  1203. set_vi_handler(i, NULL);
  1204. }
  1205. else if (cpu_has_divec)
  1206. set_handler(0x200, &except_vec4, 0x8);
  1207. /*
  1208. * Some CPUs can enable/disable for cache parity detection, but does
  1209. * it different ways.
  1210. */
  1211. parity_protection_init();
  1212. /*
  1213. * The Data Bus Errors / Instruction Bus Errors are signaled
  1214. * by external hardware. Therefore these two exceptions
  1215. * may have board specific handlers.
  1216. */
  1217. if (board_be_init)
  1218. board_be_init();
  1219. set_except_vector(0, handle_int);
  1220. set_except_vector(1, handle_tlbm);
  1221. set_except_vector(2, handle_tlbl);
  1222. set_except_vector(3, handle_tlbs);
  1223. set_except_vector(4, handle_adel);
  1224. set_except_vector(5, handle_ades);
  1225. set_except_vector(6, handle_ibe);
  1226. set_except_vector(7, handle_dbe);
  1227. set_except_vector(8, handle_sys);
  1228. set_except_vector(9, handle_bp);
  1229. set_except_vector(10, handle_ri);
  1230. set_except_vector(11, handle_cpu);
  1231. set_except_vector(12, handle_ov);
  1232. set_except_vector(13, handle_tr);
  1233. if (current_cpu_data.cputype == CPU_R6000 ||
  1234. current_cpu_data.cputype == CPU_R6000A) {
  1235. /*
  1236. * The R6000 is the only R-series CPU that features a machine
  1237. * check exception (similar to the R4000 cache error) and
  1238. * unaligned ldc1/sdc1 exception. The handlers have not been
  1239. * written yet. Well, anyway there is no R6000 machine on the
  1240. * current list of targets for Linux/MIPS.
  1241. * (Duh, crap, there is someone with a triple R6k machine)
  1242. */
  1243. //set_except_vector(14, handle_mc);
  1244. //set_except_vector(15, handle_ndc);
  1245. }
  1246. if (board_nmi_handler_setup)
  1247. board_nmi_handler_setup();
  1248. if (cpu_has_fpu && !cpu_has_nofpuex)
  1249. set_except_vector(15, handle_fpe);
  1250. set_except_vector(22, handle_mdmx);
  1251. if (cpu_has_mcheck)
  1252. set_except_vector(24, handle_mcheck);
  1253. if (cpu_has_mipsmt)
  1254. set_except_vector(25, handle_mt);
  1255. if (cpu_has_dsp)
  1256. set_except_vector(26, handle_dsp);
  1257. if (cpu_has_vce)
  1258. /* Special exception: R4[04]00 uses also the divec space. */
  1259. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
  1260. else if (cpu_has_4kex)
  1261. memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
  1262. else
  1263. memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
  1264. signal_init();
  1265. #ifdef CONFIG_MIPS32_COMPAT
  1266. signal32_init();
  1267. #endif
  1268. flush_icache_range(ebase, ebase + 0x400);
  1269. flush_tlb_handlers();
  1270. }