traps.c 42 KB

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