traps.c 42 KB

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