traps.c 60 KB

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