traps.c 40 KB

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