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