traps.c 42 KB

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