traps.c 38 KB

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