traps.c 58 KB

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