traps.c 41 KB

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