traps.c 41 KB

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