traps.c 37 KB

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