traps.c 43 KB

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