traps.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  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) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
  12. * Copyright (C) 2000, 2001, 2012 MIPS Technologies, Inc. All rights reserved.
  13. * Copyright (C) 2014, Imagination Technologies Ltd.
  14. */
  15. #include <linux/bug.h>
  16. #include <linux/compiler.h>
  17. #include <linux/context_tracking.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/kexec.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.h>
  24. #include <linux/sched.h>
  25. #include <linux/smp.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/kallsyms.h>
  28. #include <linux/bootmem.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/kgdb.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/kprobes.h>
  34. #include <linux/notifier.h>
  35. #include <linux/kdb.h>
  36. #include <linux/irq.h>
  37. #include <linux/perf_event.h>
  38. #include <asm/bootinfo.h>
  39. #include <asm/branch.h>
  40. #include <asm/break.h>
  41. #include <asm/cop2.h>
  42. #include <asm/cpu.h>
  43. #include <asm/cpu-type.h>
  44. #include <asm/dsp.h>
  45. #include <asm/fpu.h>
  46. #include <asm/fpu_emulator.h>
  47. #include <asm/idle.h>
  48. #include <asm/mipsregs.h>
  49. #include <asm/mipsmtregs.h>
  50. #include <asm/module.h>
  51. #include <asm/msa.h>
  52. #include <asm/pgtable.h>
  53. #include <asm/ptrace.h>
  54. #include <asm/sections.h>
  55. #include <asm/tlbdebug.h>
  56. #include <asm/traps.h>
  57. #include <asm/uaccess.h>
  58. #include <asm/watch.h>
  59. #include <asm/mmu_context.h>
  60. #include <asm/types.h>
  61. #include <asm/stacktrace.h>
  62. #include <asm/uasm.h>
  63. extern void check_wait(void);
  64. extern asmlinkage void rollback_handle_int(void);
  65. extern asmlinkage void handle_int(void);
  66. extern u32 handle_tlbl[];
  67. extern u32 handle_tlbs[];
  68. extern u32 handle_tlbm[];
  69. extern asmlinkage void handle_adel(void);
  70. extern asmlinkage void handle_ades(void);
  71. extern asmlinkage void handle_ibe(void);
  72. extern asmlinkage void handle_dbe(void);
  73. extern asmlinkage void handle_sys(void);
  74. extern asmlinkage void handle_bp(void);
  75. extern asmlinkage void handle_ri(void);
  76. extern asmlinkage void handle_ri_rdhwr_vivt(void);
  77. extern asmlinkage void handle_ri_rdhwr(void);
  78. extern asmlinkage void handle_cpu(void);
  79. extern asmlinkage void handle_ov(void);
  80. extern asmlinkage void handle_tr(void);
  81. extern asmlinkage void handle_msa_fpe(void);
  82. extern asmlinkage void handle_fpe(void);
  83. extern asmlinkage void handle_ftlb(void);
  84. extern asmlinkage void handle_msa(void);
  85. extern asmlinkage void handle_mdmx(void);
  86. extern asmlinkage void handle_watch(void);
  87. extern asmlinkage void handle_mt(void);
  88. extern asmlinkage void handle_dsp(void);
  89. extern asmlinkage void handle_mcheck(void);
  90. extern asmlinkage void handle_reserved(void);
  91. extern void tlb_do_page_fault_0(void);
  92. void (*board_be_init)(void);
  93. int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
  94. void (*board_nmi_handler_setup)(void);
  95. void (*board_ejtag_handler_setup)(void);
  96. void (*board_bind_eic_interrupt)(int irq, int regset);
  97. void (*board_ebase_setup)(void);
  98. void(*board_cache_error_setup)(void);
  99. static void show_raw_backtrace(unsigned long reg29)
  100. {
  101. unsigned long *sp = (unsigned long *)(reg29 & ~3);
  102. unsigned long addr;
  103. printk("Call Trace:");
  104. #ifdef CONFIG_KALLSYMS
  105. printk("\n");
  106. #endif
  107. while (!kstack_end(sp)) {
  108. unsigned long __user *p =
  109. (unsigned long __user *)(unsigned long)sp++;
  110. if (__get_user(addr, p)) {
  111. printk(" (Bad stack address)");
  112. break;
  113. }
  114. if (__kernel_text_address(addr))
  115. print_ip_sym(addr);
  116. }
  117. printk("\n");
  118. }
  119. #ifdef CONFIG_KALLSYMS
  120. int raw_show_trace;
  121. static int __init set_raw_show_trace(char *str)
  122. {
  123. raw_show_trace = 1;
  124. return 1;
  125. }
  126. __setup("raw_show_trace", set_raw_show_trace);
  127. #endif
  128. static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
  129. {
  130. unsigned long sp = regs->regs[29];
  131. unsigned long ra = regs->regs[31];
  132. unsigned long pc = regs->cp0_epc;
  133. if (!task)
  134. task = current;
  135. if (raw_show_trace || !__kernel_text_address(pc)) {
  136. show_raw_backtrace(sp);
  137. return;
  138. }
  139. printk("Call Trace:\n");
  140. do {
  141. print_ip_sym(pc);
  142. pc = unwind_stack(task, &sp, pc, &ra);
  143. } while (pc);
  144. printk("\n");
  145. }
  146. /*
  147. * This routine abuses get_user()/put_user() to reference pointers
  148. * with at least a bit of error checking ...
  149. */
  150. static void show_stacktrace(struct task_struct *task,
  151. const struct pt_regs *regs)
  152. {
  153. const int field = 2 * sizeof(unsigned long);
  154. long stackdata;
  155. int i;
  156. unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
  157. printk("Stack :");
  158. i = 0;
  159. while ((unsigned long) sp & (PAGE_SIZE - 1)) {
  160. if (i && ((i % (64 / field)) == 0))
  161. printk("\n ");
  162. if (i > 39) {
  163. printk(" ...");
  164. break;
  165. }
  166. if (__get_user(stackdata, sp++)) {
  167. printk(" (Bad stack address)");
  168. break;
  169. }
  170. printk(" %0*lx", field, stackdata);
  171. i++;
  172. }
  173. printk("\n");
  174. show_backtrace(task, regs);
  175. }
  176. void show_stack(struct task_struct *task, unsigned long *sp)
  177. {
  178. struct pt_regs regs;
  179. if (sp) {
  180. regs.regs[29] = (unsigned long)sp;
  181. regs.regs[31] = 0;
  182. regs.cp0_epc = 0;
  183. } else {
  184. if (task && task != current) {
  185. regs.regs[29] = task->thread.reg29;
  186. regs.regs[31] = 0;
  187. regs.cp0_epc = task->thread.reg31;
  188. #ifdef CONFIG_KGDB_KDB
  189. } else if (atomic_read(&kgdb_active) != -1 &&
  190. kdb_current_regs) {
  191. memcpy(&regs, kdb_current_regs, sizeof(regs));
  192. #endif /* CONFIG_KGDB_KDB */
  193. } else {
  194. prepare_frametrace(&regs);
  195. }
  196. }
  197. show_stacktrace(task, &regs);
  198. }
  199. static void show_code(unsigned int __user *pc)
  200. {
  201. long i;
  202. unsigned short __user *pc16 = NULL;
  203. printk("\nCode:");
  204. if ((unsigned long)pc & 1)
  205. pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
  206. for(i = -3 ; i < 6 ; i++) {
  207. unsigned int insn;
  208. if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
  209. printk(" (Bad address in epc)\n");
  210. break;
  211. }
  212. printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
  213. }
  214. }
  215. static void __show_regs(const struct pt_regs *regs)
  216. {
  217. const int field = 2 * sizeof(unsigned long);
  218. unsigned int cause = regs->cp0_cause;
  219. int i;
  220. show_regs_print_info(KERN_DEFAULT);
  221. /*
  222. * Saved main processor registers
  223. */
  224. for (i = 0; i < 32; ) {
  225. if ((i % 4) == 0)
  226. printk("$%2d :", i);
  227. if (i == 0)
  228. printk(" %0*lx", field, 0UL);
  229. else if (i == 26 || i == 27)
  230. printk(" %*s", field, "");
  231. else
  232. printk(" %0*lx", field, regs->regs[i]);
  233. i++;
  234. if ((i % 4) == 0)
  235. printk("\n");
  236. }
  237. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  238. printk("Acx : %0*lx\n", field, regs->acx);
  239. #endif
  240. printk("Hi : %0*lx\n", field, regs->hi);
  241. printk("Lo : %0*lx\n", field, regs->lo);
  242. /*
  243. * Saved cp0 registers
  244. */
  245. printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
  246. (void *) regs->cp0_epc);
  247. printk(" %s\n", print_tainted());
  248. printk("ra : %0*lx %pS\n", field, regs->regs[31],
  249. (void *) regs->regs[31]);
  250. printk("Status: %08x ", (uint32_t) regs->cp0_status);
  251. if (cpu_has_3kex) {
  252. if (regs->cp0_status & ST0_KUO)
  253. printk("KUo ");
  254. if (regs->cp0_status & ST0_IEO)
  255. printk("IEo ");
  256. if (regs->cp0_status & ST0_KUP)
  257. printk("KUp ");
  258. if (regs->cp0_status & ST0_IEP)
  259. printk("IEp ");
  260. if (regs->cp0_status & ST0_KUC)
  261. printk("KUc ");
  262. if (regs->cp0_status & ST0_IEC)
  263. printk("IEc ");
  264. } else if (cpu_has_4kex) {
  265. if (regs->cp0_status & ST0_KX)
  266. printk("KX ");
  267. if (regs->cp0_status & ST0_SX)
  268. printk("SX ");
  269. if (regs->cp0_status & ST0_UX)
  270. printk("UX ");
  271. switch (regs->cp0_status & ST0_KSU) {
  272. case KSU_USER:
  273. printk("USER ");
  274. break;
  275. case KSU_SUPERVISOR:
  276. printk("SUPERVISOR ");
  277. break;
  278. case KSU_KERNEL:
  279. printk("KERNEL ");
  280. break;
  281. default:
  282. printk("BAD_MODE ");
  283. break;
  284. }
  285. if (regs->cp0_status & ST0_ERL)
  286. printk("ERL ");
  287. if (regs->cp0_status & ST0_EXL)
  288. printk("EXL ");
  289. if (regs->cp0_status & ST0_IE)
  290. printk("IE ");
  291. }
  292. printk("\n");
  293. printk("Cause : %08x\n", cause);
  294. cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  295. if (1 <= cause && cause <= 5)
  296. printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
  297. printk("PrId : %08x (%s)\n", read_c0_prid(),
  298. cpu_name_string());
  299. }
  300. /*
  301. * FIXME: really the generic show_regs should take a const pointer argument.
  302. */
  303. void show_regs(struct pt_regs *regs)
  304. {
  305. __show_regs((struct pt_regs *)regs);
  306. }
  307. void show_registers(struct pt_regs *regs)
  308. {
  309. const int field = 2 * sizeof(unsigned long);
  310. mm_segment_t old_fs = get_fs();
  311. __show_regs(regs);
  312. print_modules();
  313. printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
  314. current->comm, current->pid, current_thread_info(), current,
  315. field, current_thread_info()->tp_value);
  316. if (cpu_has_userlocal) {
  317. unsigned long tls;
  318. tls = read_c0_userlocal();
  319. if (tls != current_thread_info()->tp_value)
  320. printk("*HwTLS: %0*lx\n", field, tls);
  321. }
  322. if (!user_mode(regs))
  323. /* Necessary for getting the correct stack content */
  324. set_fs(KERNEL_DS);
  325. show_stacktrace(current, regs);
  326. show_code((unsigned int __user *) regs->cp0_epc);
  327. printk("\n");
  328. set_fs(old_fs);
  329. }
  330. static int regs_to_trapnr(struct pt_regs *regs)
  331. {
  332. return (regs->cp0_cause >> 2) & 0x1f;
  333. }
  334. static DEFINE_RAW_SPINLOCK(die_lock);
  335. void __noreturn die(const char *str, struct pt_regs *regs)
  336. {
  337. static int die_counter;
  338. int sig = SIGSEGV;
  339. oops_enter();
  340. if (notify_die(DIE_OOPS, str, regs, 0, regs_to_trapnr(regs),
  341. SIGSEGV) == NOTIFY_STOP)
  342. sig = 0;
  343. console_verbose();
  344. raw_spin_lock_irq(&die_lock);
  345. bust_spinlocks(1);
  346. printk("%s[#%d]:\n", str, ++die_counter);
  347. show_registers(regs);
  348. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  349. raw_spin_unlock_irq(&die_lock);
  350. oops_exit();
  351. if (in_interrupt())
  352. panic("Fatal exception in interrupt");
  353. if (panic_on_oops) {
  354. printk(KERN_EMERG "Fatal exception: panic in 5 seconds");
  355. ssleep(5);
  356. panic("Fatal exception");
  357. }
  358. if (regs && kexec_should_crash(current))
  359. crash_kexec(regs);
  360. do_exit(sig);
  361. }
  362. extern struct exception_table_entry __start___dbe_table[];
  363. extern struct exception_table_entry __stop___dbe_table[];
  364. __asm__(
  365. " .section __dbe_table, \"a\"\n"
  366. " .previous \n");
  367. /* Given an address, look for it in the exception tables. */
  368. static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
  369. {
  370. const struct exception_table_entry *e;
  371. e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
  372. if (!e)
  373. e = search_module_dbetables(addr);
  374. return e;
  375. }
  376. asmlinkage void do_be(struct pt_regs *regs)
  377. {
  378. const int field = 2 * sizeof(unsigned long);
  379. const struct exception_table_entry *fixup = NULL;
  380. int data = regs->cp0_cause & 4;
  381. int action = MIPS_BE_FATAL;
  382. enum ctx_state prev_state;
  383. prev_state = exception_enter();
  384. /* XXX For now. Fixme, this searches the wrong table ... */
  385. if (data && !user_mode(regs))
  386. fixup = search_dbe_tables(exception_epc(regs));
  387. if (fixup)
  388. action = MIPS_BE_FIXUP;
  389. if (board_be_handler)
  390. action = board_be_handler(regs, fixup != NULL);
  391. switch (action) {
  392. case MIPS_BE_DISCARD:
  393. goto out;
  394. case MIPS_BE_FIXUP:
  395. if (fixup) {
  396. regs->cp0_epc = fixup->nextinsn;
  397. goto out;
  398. }
  399. break;
  400. default:
  401. break;
  402. }
  403. /*
  404. * Assume it would be too dangerous to continue ...
  405. */
  406. printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
  407. data ? "Data" : "Instruction",
  408. field, regs->cp0_epc, field, regs->regs[31]);
  409. if (notify_die(DIE_OOPS, "bus error", regs, 0, regs_to_trapnr(regs),
  410. SIGBUS) == NOTIFY_STOP)
  411. goto out;
  412. die_if_kernel("Oops", regs);
  413. force_sig(SIGBUS, current);
  414. out:
  415. exception_exit(prev_state);
  416. }
  417. /*
  418. * ll/sc, rdhwr, sync emulation
  419. */
  420. #define OPCODE 0xfc000000
  421. #define BASE 0x03e00000
  422. #define RT 0x001f0000
  423. #define OFFSET 0x0000ffff
  424. #define LL 0xc0000000
  425. #define SC 0xe0000000
  426. #define SPEC0 0x00000000
  427. #define SPEC3 0x7c000000
  428. #define RD 0x0000f800
  429. #define FUNC 0x0000003f
  430. #define SYNC 0x0000000f
  431. #define RDHWR 0x0000003b
  432. /* microMIPS definitions */
  433. #define MM_POOL32A_FUNC 0xfc00ffff
  434. #define MM_RDHWR 0x00006b3c
  435. #define MM_RS 0x001f0000
  436. #define MM_RT 0x03e00000
  437. /*
  438. * The ll_bit is cleared by r*_switch.S
  439. */
  440. unsigned int ll_bit;
  441. struct task_struct *ll_task;
  442. static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
  443. {
  444. unsigned long value, __user *vaddr;
  445. long offset;
  446. /*
  447. * analyse the ll instruction that just caused a ri exception
  448. * and put the referenced address to addr.
  449. */
  450. /* sign extend offset */
  451. offset = opcode & OFFSET;
  452. offset <<= 16;
  453. offset >>= 16;
  454. vaddr = (unsigned long __user *)
  455. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  456. if ((unsigned long)vaddr & 3)
  457. return SIGBUS;
  458. if (get_user(value, vaddr))
  459. return SIGSEGV;
  460. preempt_disable();
  461. if (ll_task == NULL || ll_task == current) {
  462. ll_bit = 1;
  463. } else {
  464. ll_bit = 0;
  465. }
  466. ll_task = current;
  467. preempt_enable();
  468. regs->regs[(opcode & RT) >> 16] = value;
  469. return 0;
  470. }
  471. static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
  472. {
  473. unsigned long __user *vaddr;
  474. unsigned long reg;
  475. long offset;
  476. /*
  477. * analyse the sc instruction that just caused a ri exception
  478. * and put the referenced address to addr.
  479. */
  480. /* sign extend offset */
  481. offset = opcode & OFFSET;
  482. offset <<= 16;
  483. offset >>= 16;
  484. vaddr = (unsigned long __user *)
  485. ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
  486. reg = (opcode & RT) >> 16;
  487. if ((unsigned long)vaddr & 3)
  488. return SIGBUS;
  489. preempt_disable();
  490. if (ll_bit == 0 || ll_task != current) {
  491. regs->regs[reg] = 0;
  492. preempt_enable();
  493. return 0;
  494. }
  495. preempt_enable();
  496. if (put_user(regs->regs[reg], vaddr))
  497. return SIGSEGV;
  498. regs->regs[reg] = 1;
  499. return 0;
  500. }
  501. /*
  502. * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
  503. * opcodes are supposed to result in coprocessor unusable exceptions if
  504. * executed on ll/sc-less processors. That's the theory. In practice a
  505. * few processors such as NEC's VR4100 throw reserved instruction exceptions
  506. * instead, so we're doing the emulation thing in both exception handlers.
  507. */
  508. static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
  509. {
  510. if ((opcode & OPCODE) == LL) {
  511. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  512. 1, regs, 0);
  513. return simulate_ll(regs, opcode);
  514. }
  515. if ((opcode & OPCODE) == SC) {
  516. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  517. 1, regs, 0);
  518. return simulate_sc(regs, opcode);
  519. }
  520. return -1; /* Must be something else ... */
  521. }
  522. /*
  523. * Simulate trapping 'rdhwr' instructions to provide user accessible
  524. * registers not implemented in hardware.
  525. */
  526. static int simulate_rdhwr(struct pt_regs *regs, int rd, int rt)
  527. {
  528. struct thread_info *ti = task_thread_info(current);
  529. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  530. 1, regs, 0);
  531. switch (rd) {
  532. case 0: /* CPU number */
  533. regs->regs[rt] = smp_processor_id();
  534. return 0;
  535. case 1: /* SYNCI length */
  536. regs->regs[rt] = min(current_cpu_data.dcache.linesz,
  537. current_cpu_data.icache.linesz);
  538. return 0;
  539. case 2: /* Read count register */
  540. regs->regs[rt] = read_c0_count();
  541. return 0;
  542. case 3: /* Count register resolution */
  543. switch (current_cpu_type()) {
  544. case CPU_20KC:
  545. case CPU_25KF:
  546. regs->regs[rt] = 1;
  547. break;
  548. default:
  549. regs->regs[rt] = 2;
  550. }
  551. return 0;
  552. case 29:
  553. regs->regs[rt] = ti->tp_value;
  554. return 0;
  555. default:
  556. return -1;
  557. }
  558. }
  559. static int simulate_rdhwr_normal(struct pt_regs *regs, unsigned int opcode)
  560. {
  561. if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
  562. int rd = (opcode & RD) >> 11;
  563. int rt = (opcode & RT) >> 16;
  564. simulate_rdhwr(regs, rd, rt);
  565. return 0;
  566. }
  567. /* Not ours. */
  568. return -1;
  569. }
  570. static int simulate_rdhwr_mm(struct pt_regs *regs, unsigned short opcode)
  571. {
  572. if ((opcode & MM_POOL32A_FUNC) == MM_RDHWR) {
  573. int rd = (opcode & MM_RS) >> 16;
  574. int rt = (opcode & MM_RT) >> 21;
  575. simulate_rdhwr(regs, rd, rt);
  576. return 0;
  577. }
  578. /* Not ours. */
  579. return -1;
  580. }
  581. static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
  582. {
  583. if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
  584. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
  585. 1, regs, 0);
  586. return 0;
  587. }
  588. return -1; /* Must be something else ... */
  589. }
  590. asmlinkage void do_ov(struct pt_regs *regs)
  591. {
  592. enum ctx_state prev_state;
  593. siginfo_t info;
  594. prev_state = exception_enter();
  595. die_if_kernel("Integer overflow", regs);
  596. info.si_code = FPE_INTOVF;
  597. info.si_signo = SIGFPE;
  598. info.si_errno = 0;
  599. info.si_addr = (void __user *) regs->cp0_epc;
  600. force_sig_info(SIGFPE, &info, current);
  601. exception_exit(prev_state);
  602. }
  603. int process_fpemu_return(int sig, void __user *fault_addr)
  604. {
  605. if (sig == SIGSEGV || sig == SIGBUS) {
  606. struct siginfo si = {0};
  607. si.si_addr = fault_addr;
  608. si.si_signo = sig;
  609. if (sig == SIGSEGV) {
  610. down_read(&current->mm->mmap_sem);
  611. if (find_vma(current->mm, (unsigned long)fault_addr))
  612. si.si_code = SEGV_ACCERR;
  613. else
  614. si.si_code = SEGV_MAPERR;
  615. up_read(&current->mm->mmap_sem);
  616. } else {
  617. si.si_code = BUS_ADRERR;
  618. }
  619. force_sig_info(sig, &si, current);
  620. return 1;
  621. } else if (sig) {
  622. force_sig(sig, current);
  623. return 1;
  624. } else {
  625. return 0;
  626. }
  627. }
  628. static int simulate_fp(struct pt_regs *regs, unsigned int opcode,
  629. unsigned long old_epc, unsigned long old_ra)
  630. {
  631. union mips_instruction inst = { .word = opcode };
  632. void __user *fault_addr = NULL;
  633. int sig;
  634. /* If it's obviously not an FP instruction, skip it */
  635. switch (inst.i_format.opcode) {
  636. case cop1_op:
  637. case cop1x_op:
  638. case lwc1_op:
  639. case ldc1_op:
  640. case swc1_op:
  641. case sdc1_op:
  642. break;
  643. default:
  644. return -1;
  645. }
  646. /*
  647. * do_ri skipped over the instruction via compute_return_epc, undo
  648. * that for the FPU emulator.
  649. */
  650. regs->cp0_epc = old_epc;
  651. regs->regs[31] = old_ra;
  652. /* Save the FP context to struct thread_struct */
  653. lose_fpu(1);
  654. /* Run the emulator */
  655. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  656. &fault_addr);
  657. /* If something went wrong, signal */
  658. process_fpemu_return(sig, fault_addr);
  659. /* Restore the hardware register state */
  660. own_fpu(1);
  661. return 0;
  662. }
  663. /*
  664. * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
  665. */
  666. asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
  667. {
  668. enum ctx_state prev_state;
  669. siginfo_t info = {0};
  670. prev_state = exception_enter();
  671. if (notify_die(DIE_FP, "FP exception", regs, 0, regs_to_trapnr(regs),
  672. SIGFPE) == NOTIFY_STOP)
  673. goto out;
  674. die_if_kernel("FP exception in kernel code", regs);
  675. if (fcr31 & FPU_CSR_UNI_X) {
  676. int sig;
  677. void __user *fault_addr = NULL;
  678. /*
  679. * Unimplemented operation exception. If we've got the full
  680. * software emulator on-board, let's use it...
  681. *
  682. * Force FPU to dump state into task/thread context. We're
  683. * moving a lot of data here for what is probably a single
  684. * instruction, but the alternative is to pre-decode the FP
  685. * register operands before invoking the emulator, which seems
  686. * a bit extreme for what should be an infrequent event.
  687. */
  688. /* Ensure 'resume' not overwrite saved fp context again. */
  689. lose_fpu(1);
  690. /* Run the emulator */
  691. sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
  692. &fault_addr);
  693. /*
  694. * We can't allow the emulated instruction to leave any of
  695. * the cause bit set in $fcr31.
  696. */
  697. current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
  698. /* Restore the hardware register state */
  699. own_fpu(1); /* Using the FPU again. */
  700. /* If something went wrong, signal */
  701. process_fpemu_return(sig, fault_addr);
  702. goto out;
  703. } else if (fcr31 & FPU_CSR_INV_X)
  704. info.si_code = FPE_FLTINV;
  705. else if (fcr31 & FPU_CSR_DIV_X)
  706. info.si_code = FPE_FLTDIV;
  707. else if (fcr31 & FPU_CSR_OVF_X)
  708. info.si_code = FPE_FLTOVF;
  709. else if (fcr31 & FPU_CSR_UDF_X)
  710. info.si_code = FPE_FLTUND;
  711. else if (fcr31 & FPU_CSR_INE_X)
  712. info.si_code = FPE_FLTRES;
  713. else
  714. info.si_code = __SI_FAULT;
  715. info.si_signo = SIGFPE;
  716. info.si_errno = 0;
  717. info.si_addr = (void __user *) regs->cp0_epc;
  718. force_sig_info(SIGFPE, &info, current);
  719. out:
  720. exception_exit(prev_state);
  721. }
  722. static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
  723. const char *str)
  724. {
  725. siginfo_t info;
  726. char b[40];
  727. #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
  728. if (kgdb_ll_trap(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  729. return;
  730. #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
  731. if (notify_die(DIE_TRAP, str, regs, code, regs_to_trapnr(regs),
  732. SIGTRAP) == NOTIFY_STOP)
  733. return;
  734. /*
  735. * A short test says that IRIX 5.3 sends SIGTRAP for all trap
  736. * insns, even for trap and break codes that indicate arithmetic
  737. * failures. Weird ...
  738. * But should we continue the brokenness??? --macro
  739. */
  740. switch (code) {
  741. case BRK_OVERFLOW:
  742. case BRK_DIVZERO:
  743. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  744. die_if_kernel(b, regs);
  745. if (code == BRK_DIVZERO)
  746. info.si_code = FPE_INTDIV;
  747. else
  748. info.si_code = FPE_INTOVF;
  749. info.si_signo = SIGFPE;
  750. info.si_errno = 0;
  751. info.si_addr = (void __user *) regs->cp0_epc;
  752. force_sig_info(SIGFPE, &info, current);
  753. break;
  754. case BRK_BUG:
  755. die_if_kernel("Kernel bug detected", regs);
  756. force_sig(SIGTRAP, current);
  757. break;
  758. case BRK_MEMU:
  759. /*
  760. * Address errors may be deliberately induced by the FPU
  761. * emulator to retake control of the CPU after executing the
  762. * instruction in the delay slot of an emulated branch.
  763. *
  764. * Terminate if exception was recognized as a delay slot return
  765. * otherwise handle as normal.
  766. */
  767. if (do_dsemulret(regs))
  768. return;
  769. die_if_kernel("Math emu break/trap", regs);
  770. force_sig(SIGTRAP, current);
  771. break;
  772. default:
  773. scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
  774. die_if_kernel(b, regs);
  775. force_sig(SIGTRAP, current);
  776. }
  777. }
  778. asmlinkage void do_bp(struct pt_regs *regs)
  779. {
  780. unsigned int opcode, bcode;
  781. enum ctx_state prev_state;
  782. unsigned long epc;
  783. u16 instr[2];
  784. mm_segment_t seg;
  785. seg = get_fs();
  786. if (!user_mode(regs))
  787. set_fs(KERNEL_DS);
  788. prev_state = exception_enter();
  789. if (get_isa16_mode(regs->cp0_epc)) {
  790. /* Calculate EPC. */
  791. epc = exception_epc(regs);
  792. if (cpu_has_mmips) {
  793. if ((__get_user(instr[0], (u16 __user *)msk_isa16_mode(epc)) ||
  794. (__get_user(instr[1], (u16 __user *)msk_isa16_mode(epc + 2)))))
  795. goto out_sigsegv;
  796. opcode = (instr[0] << 16) | instr[1];
  797. } else {
  798. /* MIPS16e mode */
  799. if (__get_user(instr[0],
  800. (u16 __user *)msk_isa16_mode(epc)))
  801. goto out_sigsegv;
  802. bcode = (instr[0] >> 6) & 0x3f;
  803. do_trap_or_bp(regs, bcode, "Break");
  804. goto out;
  805. }
  806. } else {
  807. if (__get_user(opcode,
  808. (unsigned int __user *) exception_epc(regs)))
  809. goto out_sigsegv;
  810. }
  811. /*
  812. * There is the ancient bug in the MIPS assemblers that the break
  813. * code starts left to bit 16 instead to bit 6 in the opcode.
  814. * Gas is bug-compatible, but not always, grrr...
  815. * We handle both cases with a simple heuristics. --macro
  816. */
  817. bcode = ((opcode >> 6) & ((1 << 20) - 1));
  818. if (bcode >= (1 << 10))
  819. bcode >>= 10;
  820. /*
  821. * notify the kprobe handlers, if instruction is likely to
  822. * pertain to them.
  823. */
  824. switch (bcode) {
  825. case BRK_KPROBE_BP:
  826. if (notify_die(DIE_BREAK, "debug", regs, bcode,
  827. regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  828. goto out;
  829. else
  830. break;
  831. case BRK_KPROBE_SSTEPBP:
  832. if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode,
  833. regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
  834. goto out;
  835. else
  836. break;
  837. default:
  838. break;
  839. }
  840. do_trap_or_bp(regs, bcode, "Break");
  841. out:
  842. set_fs(seg);
  843. exception_exit(prev_state);
  844. return;
  845. out_sigsegv:
  846. force_sig(SIGSEGV, current);
  847. goto out;
  848. }
  849. asmlinkage void do_tr(struct pt_regs *regs)
  850. {
  851. u32 opcode, tcode = 0;
  852. enum ctx_state prev_state;
  853. u16 instr[2];
  854. mm_segment_t seg;
  855. unsigned long epc = msk_isa16_mode(exception_epc(regs));
  856. seg = get_fs();
  857. if (!user_mode(regs))
  858. set_fs(get_ds());
  859. prev_state = exception_enter();
  860. if (get_isa16_mode(regs->cp0_epc)) {
  861. if (__get_user(instr[0], (u16 __user *)(epc + 0)) ||
  862. __get_user(instr[1], (u16 __user *)(epc + 2)))
  863. goto out_sigsegv;
  864. opcode = (instr[0] << 16) | instr[1];
  865. /* Immediate versions don't provide a code. */
  866. if (!(opcode & OPCODE))
  867. tcode = (opcode >> 12) & ((1 << 4) - 1);
  868. } else {
  869. if (__get_user(opcode, (u32 __user *)epc))
  870. goto out_sigsegv;
  871. /* Immediate versions don't provide a code. */
  872. if (!(opcode & OPCODE))
  873. tcode = (opcode >> 6) & ((1 << 10) - 1);
  874. }
  875. do_trap_or_bp(regs, tcode, "Trap");
  876. out:
  877. set_fs(seg);
  878. exception_exit(prev_state);
  879. return;
  880. out_sigsegv:
  881. force_sig(SIGSEGV, current);
  882. goto out;
  883. }
  884. asmlinkage void do_ri(struct pt_regs *regs)
  885. {
  886. unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
  887. unsigned long old_epc = regs->cp0_epc;
  888. unsigned long old31 = regs->regs[31];
  889. enum ctx_state prev_state;
  890. unsigned int opcode = 0;
  891. int status = -1;
  892. prev_state = exception_enter();
  893. if (notify_die(DIE_RI, "RI Fault", regs, 0, regs_to_trapnr(regs),
  894. SIGILL) == NOTIFY_STOP)
  895. goto out;
  896. die_if_kernel("Reserved instruction in kernel code", regs);
  897. if (unlikely(compute_return_epc(regs) < 0))
  898. goto out;
  899. if (get_isa16_mode(regs->cp0_epc)) {
  900. unsigned short mmop[2] = { 0 };
  901. if (unlikely(get_user(mmop[0], epc) < 0))
  902. status = SIGSEGV;
  903. if (unlikely(get_user(mmop[1], epc) < 0))
  904. status = SIGSEGV;
  905. opcode = (mmop[0] << 16) | mmop[1];
  906. if (status < 0)
  907. status = simulate_rdhwr_mm(regs, opcode);
  908. } else {
  909. if (unlikely(get_user(opcode, epc) < 0))
  910. status = SIGSEGV;
  911. if (!cpu_has_llsc && status < 0)
  912. status = simulate_llsc(regs, opcode);
  913. if (status < 0)
  914. status = simulate_rdhwr_normal(regs, opcode);
  915. if (status < 0)
  916. status = simulate_sync(regs, opcode);
  917. if (status < 0)
  918. status = simulate_fp(regs, opcode, old_epc, old31);
  919. }
  920. if (status < 0)
  921. status = SIGILL;
  922. if (unlikely(status > 0)) {
  923. regs->cp0_epc = old_epc; /* Undo skip-over. */
  924. regs->regs[31] = old31;
  925. force_sig(status, current);
  926. }
  927. out:
  928. exception_exit(prev_state);
  929. }
  930. /*
  931. * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
  932. * emulated more than some threshold number of instructions, force migration to
  933. * a "CPU" that has FP support.
  934. */
  935. static void mt_ase_fp_affinity(void)
  936. {
  937. #ifdef CONFIG_MIPS_MT_FPAFF
  938. if (mt_fpemul_threshold > 0 &&
  939. ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
  940. /*
  941. * If there's no FPU present, or if the application has already
  942. * restricted the allowed set to exclude any CPUs with FPUs,
  943. * we'll skip the procedure.
  944. */
  945. if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
  946. cpumask_t tmask;
  947. current->thread.user_cpus_allowed
  948. = current->cpus_allowed;
  949. cpus_and(tmask, current->cpus_allowed,
  950. mt_fpu_cpumask);
  951. set_cpus_allowed_ptr(current, &tmask);
  952. set_thread_flag(TIF_FPUBOUND);
  953. }
  954. }
  955. #endif /* CONFIG_MIPS_MT_FPAFF */
  956. }
  957. /*
  958. * No lock; only written during early bootup by CPU 0.
  959. */
  960. static RAW_NOTIFIER_HEAD(cu2_chain);
  961. int __ref register_cu2_notifier(struct notifier_block *nb)
  962. {
  963. return raw_notifier_chain_register(&cu2_chain, nb);
  964. }
  965. int cu2_notifier_call_chain(unsigned long val, void *v)
  966. {
  967. return raw_notifier_call_chain(&cu2_chain, val, v);
  968. }
  969. static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
  970. void *data)
  971. {
  972. struct pt_regs *regs = data;
  973. die_if_kernel("COP2: Unhandled kernel unaligned access or invalid "
  974. "instruction", regs);
  975. force_sig(SIGILL, current);
  976. return NOTIFY_OK;
  977. }
  978. static int enable_restore_fp_context(int msa)
  979. {
  980. int err, was_fpu_owner, prior_msa;
  981. if (!used_math()) {
  982. /* First time FP context user. */
  983. preempt_disable();
  984. err = init_fpu();
  985. if (msa && !err) {
  986. enable_msa();
  987. _init_msa_upper();
  988. set_thread_flag(TIF_USEDMSA);
  989. set_thread_flag(TIF_MSA_CTX_LIVE);
  990. }
  991. preempt_enable();
  992. if (!err)
  993. set_used_math();
  994. return err;
  995. }
  996. /*
  997. * This task has formerly used the FP context.
  998. *
  999. * If this thread has no live MSA vector context then we can simply
  1000. * restore the scalar FP context. If it has live MSA vector context
  1001. * (that is, it has or may have used MSA since last performing a
  1002. * function call) then we'll need to restore the vector context. This
  1003. * applies even if we're currently only executing a scalar FP
  1004. * instruction. This is because if we were to later execute an MSA
  1005. * instruction then we'd either have to:
  1006. *
  1007. * - Restore the vector context & clobber any registers modified by
  1008. * scalar FP instructions between now & then.
  1009. *
  1010. * or
  1011. *
  1012. * - Not restore the vector context & lose the most significant bits
  1013. * of all vector registers.
  1014. *
  1015. * Neither of those options is acceptable. We cannot restore the least
  1016. * significant bits of the registers now & only restore the most
  1017. * significant bits later because the most significant bits of any
  1018. * vector registers whose aliased FP register is modified now will have
  1019. * been zeroed. We'd have no way to know that when restoring the vector
  1020. * context & thus may load an outdated value for the most significant
  1021. * bits of a vector register.
  1022. */
  1023. if (!msa && !thread_msa_context_live())
  1024. return own_fpu(1);
  1025. /*
  1026. * This task is using or has previously used MSA. Thus we require
  1027. * that Status.FR == 1.
  1028. */
  1029. preempt_disable();
  1030. was_fpu_owner = is_fpu_owner();
  1031. err = own_fpu_inatomic(0);
  1032. if (err)
  1033. goto out;
  1034. enable_msa();
  1035. write_msa_csr(current->thread.fpu.msacsr);
  1036. set_thread_flag(TIF_USEDMSA);
  1037. /*
  1038. * If this is the first time that the task is using MSA and it has
  1039. * previously used scalar FP in this time slice then we already nave
  1040. * FP context which we shouldn't clobber. We do however need to clear
  1041. * the upper 64b of each vector register so that this task has no
  1042. * opportunity to see data left behind by another.
  1043. */
  1044. prior_msa = test_and_set_thread_flag(TIF_MSA_CTX_LIVE);
  1045. if (!prior_msa && was_fpu_owner) {
  1046. _init_msa_upper();
  1047. goto out;
  1048. }
  1049. if (!prior_msa) {
  1050. /*
  1051. * Restore the least significant 64b of each vector register
  1052. * from the existing scalar FP context.
  1053. */
  1054. _restore_fp(current);
  1055. /*
  1056. * The task has not formerly used MSA, so clear the upper 64b
  1057. * of each vector register such that it cannot see data left
  1058. * behind by another task.
  1059. */
  1060. _init_msa_upper();
  1061. } else {
  1062. /* We need to restore the vector context. */
  1063. restore_msa(current);
  1064. /* Restore the scalar FP control & status register */
  1065. if (!was_fpu_owner)
  1066. write_32bit_cp1_register(CP1_STATUS,
  1067. current->thread.fpu.fcr31);
  1068. }
  1069. out:
  1070. preempt_enable();
  1071. return 0;
  1072. }
  1073. asmlinkage void do_cpu(struct pt_regs *regs)
  1074. {
  1075. enum ctx_state prev_state;
  1076. unsigned int __user *epc;
  1077. unsigned long old_epc, old31;
  1078. unsigned int opcode;
  1079. unsigned int cpid;
  1080. int status, err;
  1081. unsigned long __maybe_unused flags;
  1082. prev_state = exception_enter();
  1083. cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
  1084. if (cpid != 2)
  1085. die_if_kernel("do_cpu invoked from kernel context!", regs);
  1086. switch (cpid) {
  1087. case 0:
  1088. epc = (unsigned int __user *)exception_epc(regs);
  1089. old_epc = regs->cp0_epc;
  1090. old31 = regs->regs[31];
  1091. opcode = 0;
  1092. status = -1;
  1093. if (unlikely(compute_return_epc(regs) < 0))
  1094. goto out;
  1095. if (get_isa16_mode(regs->cp0_epc)) {
  1096. unsigned short mmop[2] = { 0 };
  1097. if (unlikely(get_user(mmop[0], epc) < 0))
  1098. status = SIGSEGV;
  1099. if (unlikely(get_user(mmop[1], epc) < 0))
  1100. status = SIGSEGV;
  1101. opcode = (mmop[0] << 16) | mmop[1];
  1102. if (status < 0)
  1103. status = simulate_rdhwr_mm(regs, opcode);
  1104. } else {
  1105. if (unlikely(get_user(opcode, epc) < 0))
  1106. status = SIGSEGV;
  1107. if (!cpu_has_llsc && status < 0)
  1108. status = simulate_llsc(regs, opcode);
  1109. if (status < 0)
  1110. status = simulate_rdhwr_normal(regs, opcode);
  1111. }
  1112. if (status < 0)
  1113. status = SIGILL;
  1114. if (unlikely(status > 0)) {
  1115. regs->cp0_epc = old_epc; /* Undo skip-over. */
  1116. regs->regs[31] = old31;
  1117. force_sig(status, current);
  1118. }
  1119. goto out;
  1120. case 3:
  1121. /*
  1122. * Old (MIPS I and MIPS II) processors will set this code
  1123. * for COP1X opcode instructions that replaced the original
  1124. * COP3 space. We don't limit COP1 space instructions in
  1125. * the emulator according to the CPU ISA, so we want to
  1126. * treat COP1X instructions consistently regardless of which
  1127. * code the CPU chose. Therefore we redirect this trap to
  1128. * the FP emulator too.
  1129. *
  1130. * Then some newer FPU-less processors use this code
  1131. * erroneously too, so they are covered by this choice
  1132. * as well.
  1133. */
  1134. if (raw_cpu_has_fpu)
  1135. break;
  1136. /* Fall through. */
  1137. case 1:
  1138. err = enable_restore_fp_context(0);
  1139. if (!raw_cpu_has_fpu || err) {
  1140. int sig;
  1141. void __user *fault_addr = NULL;
  1142. sig = fpu_emulator_cop1Handler(regs,
  1143. &current->thread.fpu,
  1144. 0, &fault_addr);
  1145. if (!process_fpemu_return(sig, fault_addr) && !err)
  1146. mt_ase_fp_affinity();
  1147. }
  1148. goto out;
  1149. case 2:
  1150. raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
  1151. goto out;
  1152. }
  1153. force_sig(SIGILL, current);
  1154. out:
  1155. exception_exit(prev_state);
  1156. }
  1157. asmlinkage void do_msa_fpe(struct pt_regs *regs)
  1158. {
  1159. enum ctx_state prev_state;
  1160. prev_state = exception_enter();
  1161. die_if_kernel("do_msa_fpe invoked from kernel context!", regs);
  1162. force_sig(SIGFPE, current);
  1163. exception_exit(prev_state);
  1164. }
  1165. asmlinkage void do_msa(struct pt_regs *regs)
  1166. {
  1167. enum ctx_state prev_state;
  1168. int err;
  1169. prev_state = exception_enter();
  1170. if (!cpu_has_msa || test_thread_flag(TIF_32BIT_FPREGS)) {
  1171. force_sig(SIGILL, current);
  1172. goto out;
  1173. }
  1174. die_if_kernel("do_msa invoked from kernel context!", regs);
  1175. err = enable_restore_fp_context(1);
  1176. if (err)
  1177. force_sig(SIGILL, current);
  1178. out:
  1179. exception_exit(prev_state);
  1180. }
  1181. asmlinkage void do_mdmx(struct pt_regs *regs)
  1182. {
  1183. enum ctx_state prev_state;
  1184. prev_state = exception_enter();
  1185. force_sig(SIGILL, current);
  1186. exception_exit(prev_state);
  1187. }
  1188. /*
  1189. * Called with interrupts disabled.
  1190. */
  1191. asmlinkage void do_watch(struct pt_regs *regs)
  1192. {
  1193. enum ctx_state prev_state;
  1194. u32 cause;
  1195. prev_state = exception_enter();
  1196. /*
  1197. * Clear WP (bit 22) bit of cause register so we don't loop
  1198. * forever.
  1199. */
  1200. cause = read_c0_cause();
  1201. cause &= ~(1 << 22);
  1202. write_c0_cause(cause);
  1203. /*
  1204. * If the current thread has the watch registers loaded, save
  1205. * their values and send SIGTRAP. Otherwise another thread
  1206. * left the registers set, clear them and continue.
  1207. */
  1208. if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
  1209. mips_read_watch_registers();
  1210. local_irq_enable();
  1211. force_sig(SIGTRAP, current);
  1212. } else {
  1213. mips_clear_watch_registers();
  1214. local_irq_enable();
  1215. }
  1216. exception_exit(prev_state);
  1217. }
  1218. asmlinkage void do_mcheck(struct pt_regs *regs)
  1219. {
  1220. const int field = 2 * sizeof(unsigned long);
  1221. int multi_match = regs->cp0_status & ST0_TS;
  1222. enum ctx_state prev_state;
  1223. prev_state = exception_enter();
  1224. show_regs(regs);
  1225. if (multi_match) {
  1226. pr_err("Index : %0x\n", read_c0_index());
  1227. pr_err("Pagemask: %0x\n", read_c0_pagemask());
  1228. pr_err("EntryHi : %0*lx\n", field, read_c0_entryhi());
  1229. pr_err("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
  1230. pr_err("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
  1231. pr_err("Wired : %0x\n", read_c0_wired());
  1232. pr_err("Pagegrain: %0x\n", read_c0_pagegrain());
  1233. if (cpu_has_htw) {
  1234. pr_err("PWField : %0*lx\n", field, read_c0_pwfield());
  1235. pr_err("PWSize : %0*lx\n", field, read_c0_pwsize());
  1236. pr_err("PWCtl : %0x\n", read_c0_pwctl());
  1237. }
  1238. pr_err("\n");
  1239. dump_tlb_all();
  1240. }
  1241. show_code((unsigned int __user *) regs->cp0_epc);
  1242. /*
  1243. * Some chips may have other causes of machine check (e.g. SB1
  1244. * graduation timer)
  1245. */
  1246. panic("Caught Machine Check exception - %scaused by multiple "
  1247. "matching entries in the TLB.",
  1248. (multi_match) ? "" : "not ");
  1249. }
  1250. asmlinkage void do_mt(struct pt_regs *regs)
  1251. {
  1252. int subcode;
  1253. subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
  1254. >> VPECONTROL_EXCPT_SHIFT;
  1255. switch (subcode) {
  1256. case 0:
  1257. printk(KERN_DEBUG "Thread Underflow\n");
  1258. break;
  1259. case 1:
  1260. printk(KERN_DEBUG "Thread Overflow\n");
  1261. break;
  1262. case 2:
  1263. printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
  1264. break;
  1265. case 3:
  1266. printk(KERN_DEBUG "Gating Storage Exception\n");
  1267. break;
  1268. case 4:
  1269. printk(KERN_DEBUG "YIELD Scheduler Exception\n");
  1270. break;
  1271. case 5:
  1272. printk(KERN_DEBUG "Gating Storage Scheduler Exception\n");
  1273. break;
  1274. default:
  1275. printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
  1276. subcode);
  1277. break;
  1278. }
  1279. die_if_kernel("MIPS MT Thread exception in kernel", regs);
  1280. force_sig(SIGILL, current);
  1281. }
  1282. asmlinkage void do_dsp(struct pt_regs *regs)
  1283. {
  1284. if (cpu_has_dsp)
  1285. panic("Unexpected DSP exception");
  1286. force_sig(SIGILL, current);
  1287. }
  1288. asmlinkage void do_reserved(struct pt_regs *regs)
  1289. {
  1290. /*
  1291. * Game over - no way to handle this if it ever occurs. Most probably
  1292. * caused by a new unknown cpu type or after another deadly
  1293. * hard/software error.
  1294. */
  1295. show_regs(regs);
  1296. panic("Caught reserved exception %ld - should not happen.",
  1297. (regs->cp0_cause & 0x7f) >> 2);
  1298. }
  1299. static int __initdata l1parity = 1;
  1300. static int __init nol1parity(char *s)
  1301. {
  1302. l1parity = 0;
  1303. return 1;
  1304. }
  1305. __setup("nol1par", nol1parity);
  1306. static int __initdata l2parity = 1;
  1307. static int __init nol2parity(char *s)
  1308. {
  1309. l2parity = 0;
  1310. return 1;
  1311. }
  1312. __setup("nol2par", nol2parity);
  1313. /*
  1314. * Some MIPS CPUs can enable/disable for cache parity detection, but do
  1315. * it different ways.
  1316. */
  1317. static inline void parity_protection_init(void)
  1318. {
  1319. switch (current_cpu_type()) {
  1320. case CPU_24K:
  1321. case CPU_34K:
  1322. case CPU_74K:
  1323. case CPU_1004K:
  1324. case CPU_1074K:
  1325. case CPU_INTERAPTIV:
  1326. case CPU_PROAPTIV:
  1327. case CPU_P5600:
  1328. {
  1329. #define ERRCTL_PE 0x80000000
  1330. #define ERRCTL_L2P 0x00800000
  1331. unsigned long errctl;
  1332. unsigned int l1parity_present, l2parity_present;
  1333. errctl = read_c0_ecc();
  1334. errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
  1335. /* probe L1 parity support */
  1336. write_c0_ecc(errctl | ERRCTL_PE);
  1337. back_to_back_c0_hazard();
  1338. l1parity_present = (read_c0_ecc() & ERRCTL_PE);
  1339. /* probe L2 parity support */
  1340. write_c0_ecc(errctl|ERRCTL_L2P);
  1341. back_to_back_c0_hazard();
  1342. l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
  1343. if (l1parity_present && l2parity_present) {
  1344. if (l1parity)
  1345. errctl |= ERRCTL_PE;
  1346. if (l1parity ^ l2parity)
  1347. errctl |= ERRCTL_L2P;
  1348. } else if (l1parity_present) {
  1349. if (l1parity)
  1350. errctl |= ERRCTL_PE;
  1351. } else if (l2parity_present) {
  1352. if (l2parity)
  1353. errctl |= ERRCTL_L2P;
  1354. } else {
  1355. /* No parity available */
  1356. }
  1357. printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
  1358. write_c0_ecc(errctl);
  1359. back_to_back_c0_hazard();
  1360. errctl = read_c0_ecc();
  1361. printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
  1362. if (l1parity_present)
  1363. printk(KERN_INFO "Cache parity protection %sabled\n",
  1364. (errctl & ERRCTL_PE) ? "en" : "dis");
  1365. if (l2parity_present) {
  1366. if (l1parity_present && l1parity)
  1367. errctl ^= ERRCTL_L2P;
  1368. printk(KERN_INFO "L2 cache parity protection %sabled\n",
  1369. (errctl & ERRCTL_L2P) ? "en" : "dis");
  1370. }
  1371. }
  1372. break;
  1373. case CPU_5KC:
  1374. case CPU_5KE:
  1375. case CPU_LOONGSON1:
  1376. write_c0_ecc(0x80000000);
  1377. back_to_back_c0_hazard();
  1378. /* Set the PE bit (bit 31) in the c0_errctl register. */
  1379. printk(KERN_INFO "Cache parity protection %sabled\n",
  1380. (read_c0_ecc() & 0x80000000) ? "en" : "dis");
  1381. break;
  1382. case CPU_20KC:
  1383. case CPU_25KF:
  1384. /* Clear the DE bit (bit 16) in the c0_status register. */
  1385. printk(KERN_INFO "Enable cache parity protection for "
  1386. "MIPS 20KC/25KF CPUs.\n");
  1387. clear_c0_status(ST0_DE);
  1388. break;
  1389. default:
  1390. break;
  1391. }
  1392. }
  1393. asmlinkage void cache_parity_error(void)
  1394. {
  1395. const int field = 2 * sizeof(unsigned long);
  1396. unsigned int reg_val;
  1397. /* For the moment, report the problem and hang. */
  1398. printk("Cache error exception:\n");
  1399. printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1400. reg_val = read_c0_cacheerr();
  1401. printk("c0_cacheerr == %08x\n", reg_val);
  1402. printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1403. reg_val & (1<<30) ? "secondary" : "primary",
  1404. reg_val & (1<<31) ? "data" : "insn");
  1405. if (cpu_has_mips_r2 &&
  1406. ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS)) {
  1407. pr_err("Error bits: %s%s%s%s%s%s%s%s\n",
  1408. reg_val & (1<<29) ? "ED " : "",
  1409. reg_val & (1<<28) ? "ET " : "",
  1410. reg_val & (1<<27) ? "ES " : "",
  1411. reg_val & (1<<26) ? "EE " : "",
  1412. reg_val & (1<<25) ? "EB " : "",
  1413. reg_val & (1<<24) ? "EI " : "",
  1414. reg_val & (1<<23) ? "E1 " : "",
  1415. reg_val & (1<<22) ? "E0 " : "");
  1416. } else {
  1417. pr_err("Error bits: %s%s%s%s%s%s%s\n",
  1418. reg_val & (1<<29) ? "ED " : "",
  1419. reg_val & (1<<28) ? "ET " : "",
  1420. reg_val & (1<<26) ? "EE " : "",
  1421. reg_val & (1<<25) ? "EB " : "",
  1422. reg_val & (1<<24) ? "EI " : "",
  1423. reg_val & (1<<23) ? "E1 " : "",
  1424. reg_val & (1<<22) ? "E0 " : "");
  1425. }
  1426. printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
  1427. #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
  1428. if (reg_val & (1<<22))
  1429. printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
  1430. if (reg_val & (1<<23))
  1431. printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
  1432. #endif
  1433. panic("Can't handle the cache error!");
  1434. }
  1435. asmlinkage void do_ftlb(void)
  1436. {
  1437. const int field = 2 * sizeof(unsigned long);
  1438. unsigned int reg_val;
  1439. /* For the moment, report the problem and hang. */
  1440. if (cpu_has_mips_r2 &&
  1441. ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS)) {
  1442. pr_err("FTLB error exception, cp0_ecc=0x%08x:\n",
  1443. read_c0_ecc());
  1444. pr_err("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
  1445. reg_val = read_c0_cacheerr();
  1446. pr_err("c0_cacheerr == %08x\n", reg_val);
  1447. if ((reg_val & 0xc0000000) == 0xc0000000) {
  1448. pr_err("Decoded c0_cacheerr: FTLB parity error\n");
  1449. } else {
  1450. pr_err("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
  1451. reg_val & (1<<30) ? "secondary" : "primary",
  1452. reg_val & (1<<31) ? "data" : "insn");
  1453. }
  1454. } else {
  1455. pr_err("FTLB error exception\n");
  1456. }
  1457. /* Just print the cacheerr bits for now */
  1458. cache_parity_error();
  1459. }
  1460. /*
  1461. * SDBBP EJTAG debug exception handler.
  1462. * We skip the instruction and return to the next instruction.
  1463. */
  1464. void ejtag_exception_handler(struct pt_regs *regs)
  1465. {
  1466. const int field = 2 * sizeof(unsigned long);
  1467. unsigned long depc, old_epc, old_ra;
  1468. unsigned int debug;
  1469. printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
  1470. depc = read_c0_depc();
  1471. debug = read_c0_debug();
  1472. printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
  1473. if (debug & 0x80000000) {
  1474. /*
  1475. * In branch delay slot.
  1476. * We cheat a little bit here and use EPC to calculate the
  1477. * debug return address (DEPC). EPC is restored after the
  1478. * calculation.
  1479. */
  1480. old_epc = regs->cp0_epc;
  1481. old_ra = regs->regs[31];
  1482. regs->cp0_epc = depc;
  1483. compute_return_epc(regs);
  1484. depc = regs->cp0_epc;
  1485. regs->cp0_epc = old_epc;
  1486. regs->regs[31] = old_ra;
  1487. } else
  1488. depc += 4;
  1489. write_c0_depc(depc);
  1490. #if 0
  1491. printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
  1492. write_c0_debug(debug | 0x100);
  1493. #endif
  1494. }
  1495. /*
  1496. * NMI exception handler.
  1497. * No lock; only written during early bootup by CPU 0.
  1498. */
  1499. static RAW_NOTIFIER_HEAD(nmi_chain);
  1500. int register_nmi_notifier(struct notifier_block *nb)
  1501. {
  1502. return raw_notifier_chain_register(&nmi_chain, nb);
  1503. }
  1504. void __noreturn nmi_exception_handler(struct pt_regs *regs)
  1505. {
  1506. char str[100];
  1507. raw_notifier_call_chain(&nmi_chain, 0, regs);
  1508. bust_spinlocks(1);
  1509. snprintf(str, 100, "CPU%d NMI taken, CP0_EPC=%lx\n",
  1510. smp_processor_id(), regs->cp0_epc);
  1511. regs->cp0_epc = read_c0_errorepc();
  1512. die(str, regs);
  1513. }
  1514. #define VECTORSPACING 0x100 /* for EI/VI mode */
  1515. unsigned long ebase;
  1516. unsigned long exception_handlers[32];
  1517. unsigned long vi_handlers[64];
  1518. void __init *set_except_vector(int n, void *addr)
  1519. {
  1520. unsigned long handler = (unsigned long) addr;
  1521. unsigned long old_handler;
  1522. #ifdef CONFIG_CPU_MICROMIPS
  1523. /*
  1524. * Only the TLB handlers are cache aligned with an even
  1525. * address. All other handlers are on an odd address and
  1526. * require no modification. Otherwise, MIPS32 mode will
  1527. * be entered when handling any TLB exceptions. That
  1528. * would be bad...since we must stay in microMIPS mode.
  1529. */
  1530. if (!(handler & 0x1))
  1531. handler |= 1;
  1532. #endif
  1533. old_handler = xchg(&exception_handlers[n], handler);
  1534. if (n == 0 && cpu_has_divec) {
  1535. #ifdef CONFIG_CPU_MICROMIPS
  1536. unsigned long jump_mask = ~((1 << 27) - 1);
  1537. #else
  1538. unsigned long jump_mask = ~((1 << 28) - 1);
  1539. #endif
  1540. u32 *buf = (u32 *)(ebase + 0x200);
  1541. unsigned int k0 = 26;
  1542. if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
  1543. uasm_i_j(&buf, handler & ~jump_mask);
  1544. uasm_i_nop(&buf);
  1545. } else {
  1546. UASM_i_LA(&buf, k0, handler);
  1547. uasm_i_jr(&buf, k0);
  1548. uasm_i_nop(&buf);
  1549. }
  1550. local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
  1551. }
  1552. return (void *)old_handler;
  1553. }
  1554. static void do_default_vi(void)
  1555. {
  1556. show_regs(get_irq_regs());
  1557. panic("Caught unexpected vectored interrupt.");
  1558. }
  1559. static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
  1560. {
  1561. unsigned long handler;
  1562. unsigned long old_handler = vi_handlers[n];
  1563. int srssets = current_cpu_data.srsets;
  1564. u16 *h;
  1565. unsigned char *b;
  1566. BUG_ON(!cpu_has_veic && !cpu_has_vint);
  1567. if (addr == NULL) {
  1568. handler = (unsigned long) do_default_vi;
  1569. srs = 0;
  1570. } else
  1571. handler = (unsigned long) addr;
  1572. vi_handlers[n] = handler;
  1573. b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
  1574. if (srs >= srssets)
  1575. panic("Shadow register set %d not supported", srs);
  1576. if (cpu_has_veic) {
  1577. if (board_bind_eic_interrupt)
  1578. board_bind_eic_interrupt(n, srs);
  1579. } else if (cpu_has_vint) {
  1580. /* SRSMap is only defined if shadow sets are implemented */
  1581. if (srssets > 1)
  1582. change_c0_srsmap(0xf << n*4, srs << n*4);
  1583. }
  1584. if (srs == 0) {
  1585. /*
  1586. * If no shadow set is selected then use the default handler
  1587. * that does normal register saving and standard interrupt exit
  1588. */
  1589. extern char except_vec_vi, except_vec_vi_lui;
  1590. extern char except_vec_vi_ori, except_vec_vi_end;
  1591. extern char rollback_except_vec_vi;
  1592. char *vec_start = using_rollback_handler() ?
  1593. &rollback_except_vec_vi : &except_vec_vi;
  1594. #if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN)
  1595. const int lui_offset = &except_vec_vi_lui - vec_start + 2;
  1596. const int ori_offset = &except_vec_vi_ori - vec_start + 2;
  1597. #else
  1598. const int lui_offset = &except_vec_vi_lui - vec_start;
  1599. const int ori_offset = &except_vec_vi_ori - vec_start;
  1600. #endif
  1601. const int handler_len = &except_vec_vi_end - vec_start;
  1602. if (handler_len > VECTORSPACING) {
  1603. /*
  1604. * Sigh... panicing won't help as the console
  1605. * is probably not configured :(
  1606. */
  1607. panic("VECTORSPACING too small");
  1608. }
  1609. set_handler(((unsigned long)b - ebase), vec_start,
  1610. #ifdef CONFIG_CPU_MICROMIPS
  1611. (handler_len - 1));
  1612. #else
  1613. handler_len);
  1614. #endif
  1615. h = (u16 *)(b + lui_offset);
  1616. *h = (handler >> 16) & 0xffff;
  1617. h = (u16 *)(b + ori_offset);
  1618. *h = (handler & 0xffff);
  1619. local_flush_icache_range((unsigned long)b,
  1620. (unsigned long)(b+handler_len));
  1621. }
  1622. else {
  1623. /*
  1624. * In other cases jump directly to the interrupt handler. It
  1625. * is the handler's responsibility to save registers if required
  1626. * (eg hi/lo) and return from the exception using "eret".
  1627. */
  1628. u32 insn;
  1629. h = (u16 *)b;
  1630. /* j handler */
  1631. #ifdef CONFIG_CPU_MICROMIPS
  1632. insn = 0xd4000000 | (((u32)handler & 0x07ffffff) >> 1);
  1633. #else
  1634. insn = 0x08000000 | (((u32)handler & 0x0fffffff) >> 2);
  1635. #endif
  1636. h[0] = (insn >> 16) & 0xffff;
  1637. h[1] = insn & 0xffff;
  1638. h[2] = 0;
  1639. h[3] = 0;
  1640. local_flush_icache_range((unsigned long)b,
  1641. (unsigned long)(b+8));
  1642. }
  1643. return (void *)old_handler;
  1644. }
  1645. void *set_vi_handler(int n, vi_handler_t addr)
  1646. {
  1647. return set_vi_srs_handler(n, addr, 0);
  1648. }
  1649. extern void tlb_init(void);
  1650. /*
  1651. * Timer interrupt
  1652. */
  1653. int cp0_compare_irq;
  1654. EXPORT_SYMBOL_GPL(cp0_compare_irq);
  1655. int cp0_compare_irq_shift;
  1656. /*
  1657. * Performance counter IRQ or -1 if shared with timer
  1658. */
  1659. int cp0_perfcount_irq;
  1660. EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
  1661. static int noulri;
  1662. static int __init ulri_disable(char *s)
  1663. {
  1664. pr_info("Disabling ulri\n");
  1665. noulri = 1;
  1666. return 1;
  1667. }
  1668. __setup("noulri", ulri_disable);
  1669. /* configure STATUS register */
  1670. static void configure_status(void)
  1671. {
  1672. /*
  1673. * Disable coprocessors and select 32-bit or 64-bit addressing
  1674. * and the 16/32 or 32/32 FPR register model. Reset the BEV
  1675. * flag that some firmware may have left set and the TS bit (for
  1676. * IP27). Set XX for ISA IV code to work.
  1677. */
  1678. unsigned int status_set = ST0_CU0;
  1679. #ifdef CONFIG_64BIT
  1680. status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
  1681. #endif
  1682. if (current_cpu_data.isa_level & MIPS_CPU_ISA_IV)
  1683. status_set |= ST0_XX;
  1684. if (cpu_has_dsp)
  1685. status_set |= ST0_MX;
  1686. change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
  1687. status_set);
  1688. }
  1689. /* configure HWRENA register */
  1690. static void configure_hwrena(void)
  1691. {
  1692. unsigned int hwrena = cpu_hwrena_impl_bits;
  1693. if (cpu_has_mips_r2)
  1694. hwrena |= 0x0000000f;
  1695. if (!noulri && cpu_has_userlocal)
  1696. hwrena |= (1 << 29);
  1697. if (hwrena)
  1698. write_c0_hwrena(hwrena);
  1699. }
  1700. static void configure_exception_vector(void)
  1701. {
  1702. if (cpu_has_veic || cpu_has_vint) {
  1703. unsigned long sr = set_c0_status(ST0_BEV);
  1704. write_c0_ebase(ebase);
  1705. write_c0_status(sr);
  1706. /* Setting vector spacing enables EI/VI mode */
  1707. change_c0_intctl(0x3e0, VECTORSPACING);
  1708. }
  1709. if (cpu_has_divec) {
  1710. if (cpu_has_mipsmt) {
  1711. unsigned int vpflags = dvpe();
  1712. set_c0_cause(CAUSEF_IV);
  1713. evpe(vpflags);
  1714. } else
  1715. set_c0_cause(CAUSEF_IV);
  1716. }
  1717. }
  1718. void per_cpu_trap_init(bool is_boot_cpu)
  1719. {
  1720. unsigned int cpu = smp_processor_id();
  1721. configure_status();
  1722. configure_hwrena();
  1723. configure_exception_vector();
  1724. /*
  1725. * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
  1726. *
  1727. * o read IntCtl.IPTI to determine the timer interrupt
  1728. * o read IntCtl.IPPCI to determine the performance counter interrupt
  1729. */
  1730. if (cpu_has_mips_r2) {
  1731. cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
  1732. cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
  1733. cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
  1734. if (cp0_perfcount_irq == cp0_compare_irq)
  1735. cp0_perfcount_irq = -1;
  1736. } else {
  1737. cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
  1738. cp0_compare_irq_shift = CP0_LEGACY_PERFCNT_IRQ;
  1739. cp0_perfcount_irq = -1;
  1740. }
  1741. if (!cpu_data[cpu].asid_cache)
  1742. cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
  1743. atomic_inc(&init_mm.mm_count);
  1744. current->active_mm = &init_mm;
  1745. BUG_ON(current->mm);
  1746. enter_lazy_tlb(&init_mm, current);
  1747. /* Boot CPU's cache setup in setup_arch(). */
  1748. if (!is_boot_cpu)
  1749. cpu_cache_init();
  1750. tlb_init();
  1751. TLBMISS_HANDLER_SETUP();
  1752. }
  1753. /* Install CPU exception handler */
  1754. void set_handler(unsigned long offset, void *addr, unsigned long size)
  1755. {
  1756. #ifdef CONFIG_CPU_MICROMIPS
  1757. memcpy((void *)(ebase + offset), ((unsigned char *)addr - 1), size);
  1758. #else
  1759. memcpy((void *)(ebase + offset), addr, size);
  1760. #endif
  1761. local_flush_icache_range(ebase + offset, ebase + offset + size);
  1762. }
  1763. static char panic_null_cerr[] =
  1764. "Trying to set NULL cache error exception handler";
  1765. /*
  1766. * Install uncached CPU exception handler.
  1767. * This is suitable only for the cache error exception which is the only
  1768. * exception handler that is being run uncached.
  1769. */
  1770. void set_uncached_handler(unsigned long offset, void *addr,
  1771. unsigned long size)
  1772. {
  1773. unsigned long uncached_ebase = CKSEG1ADDR(ebase);
  1774. if (!addr)
  1775. panic(panic_null_cerr);
  1776. memcpy((void *)(uncached_ebase + offset), addr, size);
  1777. }
  1778. static int __initdata rdhwr_noopt;
  1779. static int __init set_rdhwr_noopt(char *str)
  1780. {
  1781. rdhwr_noopt = 1;
  1782. return 1;
  1783. }
  1784. __setup("rdhwr_noopt", set_rdhwr_noopt);
  1785. void __init trap_init(void)
  1786. {
  1787. extern char except_vec3_generic;
  1788. extern char except_vec4;
  1789. extern char except_vec3_r4000;
  1790. unsigned long i;
  1791. check_wait();
  1792. #if defined(CONFIG_KGDB)
  1793. if (kgdb_early_setup)
  1794. return; /* Already done */
  1795. #endif
  1796. if (cpu_has_veic || cpu_has_vint) {
  1797. unsigned long size = 0x200 + VECTORSPACING*64;
  1798. ebase = (unsigned long)
  1799. __alloc_bootmem(size, 1 << fls(size), 0);
  1800. } else {
  1801. #ifdef CONFIG_KVM_GUEST
  1802. #define KVM_GUEST_KSEG0 0x40000000
  1803. ebase = KVM_GUEST_KSEG0;
  1804. #else
  1805. ebase = CKSEG0;
  1806. #endif
  1807. if (cpu_has_mips_r2)
  1808. ebase += (read_c0_ebase() & 0x3ffff000);
  1809. }
  1810. if (cpu_has_mmips) {
  1811. unsigned int config3 = read_c0_config3();
  1812. if (IS_ENABLED(CONFIG_CPU_MICROMIPS))
  1813. write_c0_config3(config3 | MIPS_CONF3_ISA_OE);
  1814. else
  1815. write_c0_config3(config3 & ~MIPS_CONF3_ISA_OE);
  1816. }
  1817. if (board_ebase_setup)
  1818. board_ebase_setup();
  1819. per_cpu_trap_init(true);
  1820. /*
  1821. * Copy the generic exception handlers to their final destination.
  1822. * This will be overriden later as suitable for a particular
  1823. * configuration.
  1824. */
  1825. set_handler(0x180, &except_vec3_generic, 0x80);
  1826. /*
  1827. * Setup default vectors
  1828. */
  1829. for (i = 0; i <= 31; i++)
  1830. set_except_vector(i, handle_reserved);
  1831. /*
  1832. * Copy the EJTAG debug exception vector handler code to it's final
  1833. * destination.
  1834. */
  1835. if (cpu_has_ejtag && board_ejtag_handler_setup)
  1836. board_ejtag_handler_setup();
  1837. /*
  1838. * Only some CPUs have the watch exceptions.
  1839. */
  1840. if (cpu_has_watch)
  1841. set_except_vector(23, handle_watch);
  1842. /*
  1843. * Initialise interrupt handlers
  1844. */
  1845. if (cpu_has_veic || cpu_has_vint) {
  1846. int nvec = cpu_has_veic ? 64 : 8;
  1847. for (i = 0; i < nvec; i++)
  1848. set_vi_handler(i, NULL);
  1849. }
  1850. else if (cpu_has_divec)
  1851. set_handler(0x200, &except_vec4, 0x8);
  1852. /*
  1853. * Some CPUs can enable/disable for cache parity detection, but does
  1854. * it different ways.
  1855. */
  1856. parity_protection_init();
  1857. /*
  1858. * The Data Bus Errors / Instruction Bus Errors are signaled
  1859. * by external hardware. Therefore these two exceptions
  1860. * may have board specific handlers.
  1861. */
  1862. if (board_be_init)
  1863. board_be_init();
  1864. set_except_vector(0, using_rollback_handler() ? rollback_handle_int
  1865. : handle_int);
  1866. set_except_vector(1, handle_tlbm);
  1867. set_except_vector(2, handle_tlbl);
  1868. set_except_vector(3, handle_tlbs);
  1869. set_except_vector(4, handle_adel);
  1870. set_except_vector(5, handle_ades);
  1871. set_except_vector(6, handle_ibe);
  1872. set_except_vector(7, handle_dbe);
  1873. set_except_vector(8, handle_sys);
  1874. set_except_vector(9, handle_bp);
  1875. set_except_vector(10, rdhwr_noopt ? handle_ri :
  1876. (cpu_has_vtag_icache ?
  1877. handle_ri_rdhwr_vivt : handle_ri_rdhwr));
  1878. set_except_vector(11, handle_cpu);
  1879. set_except_vector(12, handle_ov);
  1880. set_except_vector(13, handle_tr);
  1881. set_except_vector(14, handle_msa_fpe);
  1882. if (current_cpu_type() == CPU_R6000 ||
  1883. current_cpu_type() == CPU_R6000A) {
  1884. /*
  1885. * The R6000 is the only R-series CPU that features a machine
  1886. * check exception (similar to the R4000 cache error) and
  1887. * unaligned ldc1/sdc1 exception. The handlers have not been
  1888. * written yet. Well, anyway there is no R6000 machine on the
  1889. * current list of targets for Linux/MIPS.
  1890. * (Duh, crap, there is someone with a triple R6k machine)
  1891. */
  1892. //set_except_vector(14, handle_mc);
  1893. //set_except_vector(15, handle_ndc);
  1894. }
  1895. if (board_nmi_handler_setup)
  1896. board_nmi_handler_setup();
  1897. if (cpu_has_fpu && !cpu_has_nofpuex)
  1898. set_except_vector(15, handle_fpe);
  1899. set_except_vector(16, handle_ftlb);
  1900. if (cpu_has_rixiex) {
  1901. set_except_vector(19, tlb_do_page_fault_0);
  1902. set_except_vector(20, tlb_do_page_fault_0);
  1903. }
  1904. set_except_vector(21, handle_msa);
  1905. set_except_vector(22, handle_mdmx);
  1906. if (cpu_has_mcheck)
  1907. set_except_vector(24, handle_mcheck);
  1908. if (cpu_has_mipsmt)
  1909. set_except_vector(25, handle_mt);
  1910. set_except_vector(26, handle_dsp);
  1911. if (board_cache_error_setup)
  1912. board_cache_error_setup();
  1913. if (cpu_has_vce)
  1914. /* Special exception: R4[04]00 uses also the divec space. */
  1915. set_handler(0x180, &except_vec3_r4000, 0x100);
  1916. else if (cpu_has_4kex)
  1917. set_handler(0x180, &except_vec3_generic, 0x80);
  1918. else
  1919. set_handler(0x080, &except_vec3_generic, 0x80);
  1920. local_flush_icache_range(ebase, ebase + 0x400);
  1921. sort_extable(__start___dbe_table, __stop___dbe_table);
  1922. cu2_notifier(default_cu2_call, 0x80000000); /* Run last */
  1923. }
  1924. static int trap_pm_notifier(struct notifier_block *self, unsigned long cmd,
  1925. void *v)
  1926. {
  1927. switch (cmd) {
  1928. case CPU_PM_ENTER_FAILED:
  1929. case CPU_PM_EXIT:
  1930. configure_status();
  1931. configure_hwrena();
  1932. configure_exception_vector();
  1933. /* Restore register with CPU number for TLB handlers */
  1934. TLBMISS_HANDLER_RESTORE();
  1935. break;
  1936. }
  1937. return NOTIFY_OK;
  1938. }
  1939. static struct notifier_block trap_pm_notifier_block = {
  1940. .notifier_call = trap_pm_notifier,
  1941. };
  1942. static int __init trap_pm_init(void)
  1943. {
  1944. return cpu_pm_register_notifier(&trap_pm_notifier_block);
  1945. }
  1946. arch_initcall(trap_pm_init);