traps.c 41 KB

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