traps.c 60 KB

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