traps.c 42 KB

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