traps.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 1995-1996 Gary Thomas ([email protected])
  4. * Copyright 2007-2010 Freescale Semiconductor, Inc.
  5. *
  6. * Modified by Cort Dougan ([email protected])
  7. * and Paul Mackerras ([email protected])
  8. */
  9. /*
  10. * This file handles the architecture-dependent parts of hardware exceptions
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/debug.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/pkeys.h>
  18. #include <linux/stddef.h>
  19. #include <linux/unistd.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/user.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/extable.h>
  25. #include <linux/module.h> /* print_modules */
  26. #include <linux/prctl.h>
  27. #include <linux/delay.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/kexec.h>
  30. #include <linux/backlight.h>
  31. #include <linux/bug.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/ratelimit.h>
  34. #include <linux/context_tracking.h>
  35. #include <linux/smp.h>
  36. #include <linux/console.h>
  37. #include <linux/kmsg_dump.h>
  38. #include <linux/debugfs.h>
  39. #include <asm/emulated_ops.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/interrupt.h>
  42. #include <asm/io.h>
  43. #include <asm/machdep.h>
  44. #include <asm/rtas.h>
  45. #include <asm/pmc.h>
  46. #include <asm/reg.h>
  47. #ifdef CONFIG_PMAC_BACKLIGHT
  48. #include <asm/backlight.h>
  49. #endif
  50. #ifdef CONFIG_PPC64
  51. #include <asm/firmware.h>
  52. #include <asm/processor.h>
  53. #endif
  54. #include <asm/kexec.h>
  55. #include <asm/ppc-opcode.h>
  56. #include <asm/rio.h>
  57. #include <asm/fadump.h>
  58. #include <asm/switch_to.h>
  59. #include <asm/tm.h>
  60. #include <asm/debug.h>
  61. #include <asm/asm-prototypes.h>
  62. #include <asm/hmi.h>
  63. #include <sysdev/fsl_pci.h>
  64. #include <asm/kprobes.h>
  65. #include <asm/stacktrace.h>
  66. #include <asm/nmi.h>
  67. #include <asm/disassemble.h>
  68. #include <asm/udbg.h>
  69. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
  70. int (*__debugger)(struct pt_regs *regs) __read_mostly;
  71. int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
  72. int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
  73. int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
  74. int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
  75. int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
  76. int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
  77. EXPORT_SYMBOL(__debugger);
  78. EXPORT_SYMBOL(__debugger_ipi);
  79. EXPORT_SYMBOL(__debugger_bpt);
  80. EXPORT_SYMBOL(__debugger_sstep);
  81. EXPORT_SYMBOL(__debugger_iabr_match);
  82. EXPORT_SYMBOL(__debugger_break_match);
  83. EXPORT_SYMBOL(__debugger_fault_handler);
  84. #endif
  85. /* Transactional Memory trap debug */
  86. #ifdef TM_DEBUG_SW
  87. #define TM_DEBUG(x...) printk(KERN_INFO x)
  88. #else
  89. #define TM_DEBUG(x...) do { } while(0)
  90. #endif
  91. static const char *signame(int signr)
  92. {
  93. switch (signr) {
  94. case SIGBUS: return "bus error";
  95. case SIGFPE: return "floating point exception";
  96. case SIGILL: return "illegal instruction";
  97. case SIGSEGV: return "segfault";
  98. case SIGTRAP: return "unhandled trap";
  99. }
  100. return "unknown signal";
  101. }
  102. /*
  103. * Trap & Exception support
  104. */
  105. #ifdef CONFIG_PMAC_BACKLIGHT
  106. static void pmac_backlight_unblank(void)
  107. {
  108. mutex_lock(&pmac_backlight_mutex);
  109. if (pmac_backlight) {
  110. struct backlight_properties *props;
  111. props = &pmac_backlight->props;
  112. props->brightness = props->max_brightness;
  113. props->power = FB_BLANK_UNBLANK;
  114. backlight_update_status(pmac_backlight);
  115. }
  116. mutex_unlock(&pmac_backlight_mutex);
  117. }
  118. #else
  119. static inline void pmac_backlight_unblank(void) { }
  120. #endif
  121. /*
  122. * If oops/die is expected to crash the machine, return true here.
  123. *
  124. * This should not be expected to be 100% accurate, there may be
  125. * notifiers registered or other unexpected conditions that may bring
  126. * down the kernel. Or if the current process in the kernel is holding
  127. * locks or has other critical state, the kernel may become effectively
  128. * unusable anyway.
  129. */
  130. bool die_will_crash(void)
  131. {
  132. if (should_fadump_crash())
  133. return true;
  134. if (kexec_should_crash(current))
  135. return true;
  136. if (in_interrupt() || panic_on_oops ||
  137. !current->pid || is_global_init(current))
  138. return true;
  139. return false;
  140. }
  141. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  142. static int die_owner = -1;
  143. static unsigned int die_nest_count;
  144. static int die_counter;
  145. extern void panic_flush_kmsg_start(void)
  146. {
  147. /*
  148. * These are mostly taken from kernel/panic.c, but tries to do
  149. * relatively minimal work. Don't use delay functions (TB may
  150. * be broken), don't crash dump (need to set a firmware log),
  151. * don't run notifiers. We do want to get some information to
  152. * Linux console.
  153. */
  154. console_verbose();
  155. bust_spinlocks(1);
  156. }
  157. extern void panic_flush_kmsg_end(void)
  158. {
  159. kmsg_dump(KMSG_DUMP_PANIC);
  160. bust_spinlocks(0);
  161. debug_locks_off();
  162. console_flush_on_panic(CONSOLE_FLUSH_PENDING);
  163. }
  164. static unsigned long oops_begin(struct pt_regs *regs)
  165. {
  166. int cpu;
  167. unsigned long flags;
  168. oops_enter();
  169. /* racy, but better than risking deadlock. */
  170. raw_local_irq_save(flags);
  171. cpu = smp_processor_id();
  172. if (!arch_spin_trylock(&die_lock)) {
  173. if (cpu == die_owner)
  174. /* nested oops. should stop eventually */;
  175. else
  176. arch_spin_lock(&die_lock);
  177. }
  178. die_nest_count++;
  179. die_owner = cpu;
  180. console_verbose();
  181. bust_spinlocks(1);
  182. if (machine_is(powermac))
  183. pmac_backlight_unblank();
  184. return flags;
  185. }
  186. NOKPROBE_SYMBOL(oops_begin);
  187. static void oops_end(unsigned long flags, struct pt_regs *regs,
  188. int signr)
  189. {
  190. bust_spinlocks(0);
  191. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  192. die_nest_count--;
  193. oops_exit();
  194. printk("\n");
  195. if (!die_nest_count) {
  196. /* Nest count reaches zero, release the lock. */
  197. die_owner = -1;
  198. arch_spin_unlock(&die_lock);
  199. }
  200. raw_local_irq_restore(flags);
  201. /*
  202. * system_reset_excption handles debugger, crash dump, panic, for 0x100
  203. */
  204. if (TRAP(regs) == INTERRUPT_SYSTEM_RESET)
  205. return;
  206. crash_fadump(regs, "die oops");
  207. if (kexec_should_crash(current))
  208. crash_kexec(regs);
  209. if (!signr)
  210. return;
  211. /*
  212. * While our oops output is serialised by a spinlock, output
  213. * from panic() called below can race and corrupt it. If we
  214. * know we are going to panic, delay for 1 second so we have a
  215. * chance to get clean backtraces from all CPUs that are oopsing.
  216. */
  217. if (in_interrupt() || panic_on_oops || !current->pid ||
  218. is_global_init(current)) {
  219. mdelay(MSEC_PER_SEC);
  220. }
  221. if (panic_on_oops)
  222. panic("Fatal exception");
  223. make_task_dead(signr);
  224. }
  225. NOKPROBE_SYMBOL(oops_end);
  226. static char *get_mmu_str(void)
  227. {
  228. if (early_radix_enabled())
  229. return " MMU=Radix";
  230. if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
  231. return " MMU=Hash";
  232. return "";
  233. }
  234. static int __die(const char *str, struct pt_regs *regs, long err)
  235. {
  236. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  237. printk("%s PAGE_SIZE=%luK%s%s%s%s%s%s %s\n",
  238. IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) ? "LE" : "BE",
  239. PAGE_SIZE / 1024, get_mmu_str(),
  240. IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
  241. IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
  242. IS_ENABLED(CONFIG_SMP) ? (" NR_CPUS=" __stringify(NR_CPUS)) : "",
  243. debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
  244. IS_ENABLED(CONFIG_NUMA) ? " NUMA" : "",
  245. ppc_md.name ? ppc_md.name : "");
  246. if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
  247. return 1;
  248. print_modules();
  249. show_regs(regs);
  250. return 0;
  251. }
  252. NOKPROBE_SYMBOL(__die);
  253. void die(const char *str, struct pt_regs *regs, long err)
  254. {
  255. unsigned long flags;
  256. /*
  257. * system_reset_excption handles debugger, crash dump, panic, for 0x100
  258. */
  259. if (TRAP(regs) != INTERRUPT_SYSTEM_RESET) {
  260. if (debugger(regs))
  261. return;
  262. }
  263. flags = oops_begin(regs);
  264. if (__die(str, regs, err))
  265. err = 0;
  266. oops_end(flags, regs, err);
  267. }
  268. NOKPROBE_SYMBOL(die);
  269. void user_single_step_report(struct pt_regs *regs)
  270. {
  271. force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)regs->nip);
  272. }
  273. static void show_signal_msg(int signr, struct pt_regs *regs, int code,
  274. unsigned long addr)
  275. {
  276. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  277. DEFAULT_RATELIMIT_BURST);
  278. if (!show_unhandled_signals)
  279. return;
  280. if (!unhandled_signal(current, signr))
  281. return;
  282. if (!__ratelimit(&rs))
  283. return;
  284. pr_info("%s[%d]: %s (%d) at %lx nip %lx lr %lx code %x",
  285. current->comm, current->pid, signame(signr), signr,
  286. addr, regs->nip, regs->link, code);
  287. print_vma_addr(KERN_CONT " in ", regs->nip);
  288. pr_cont("\n");
  289. show_user_instructions(regs);
  290. }
  291. static bool exception_common(int signr, struct pt_regs *regs, int code,
  292. unsigned long addr)
  293. {
  294. if (!user_mode(regs)) {
  295. die("Exception in kernel mode", regs, signr);
  296. return false;
  297. }
  298. /*
  299. * Must not enable interrupts even for user-mode exception, because
  300. * this can be called from machine check, which may be a NMI or IRQ
  301. * which don't like interrupts being enabled. Could check for
  302. * in_hardirq || in_nmi perhaps, but there doesn't seem to be a good
  303. * reason why _exception() should enable irqs for an exception handler,
  304. * the handlers themselves do that directly.
  305. */
  306. show_signal_msg(signr, regs, code, addr);
  307. current->thread.trap_nr = code;
  308. return true;
  309. }
  310. void _exception_pkey(struct pt_regs *regs, unsigned long addr, int key)
  311. {
  312. if (!exception_common(SIGSEGV, regs, SEGV_PKUERR, addr))
  313. return;
  314. force_sig_pkuerr((void __user *) addr, key);
  315. }
  316. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  317. {
  318. if (!exception_common(signr, regs, code, addr))
  319. return;
  320. force_sig_fault(signr, code, (void __user *)addr);
  321. }
  322. /*
  323. * The interrupt architecture has a quirk in that the HV interrupts excluding
  324. * the NMIs (0x100 and 0x200) do not clear MSR[RI] at entry. The first thing
  325. * that an interrupt handler must do is save off a GPR into a scratch register,
  326. * and all interrupts on POWERNV (HV=1) use the HSPRG1 register as scratch.
  327. * Therefore an NMI can clobber an HV interrupt's live HSPRG1 without noticing
  328. * that it is non-reentrant, which leads to random data corruption.
  329. *
  330. * The solution is for NMI interrupts in HV mode to check if they originated
  331. * from these critical HV interrupt regions. If so, then mark them not
  332. * recoverable.
  333. *
  334. * An alternative would be for HV NMIs to use SPRG for scratch to avoid the
  335. * HSPRG1 clobber, however this would cause guest SPRG to be clobbered. Linux
  336. * guests should always have MSR[RI]=0 when its scratch SPRG is in use, so
  337. * that would work. However any other guest OS that may have the SPRG live
  338. * and MSR[RI]=1 could encounter silent corruption.
  339. *
  340. * Builds that do not support KVM could take this second option to increase
  341. * the recoverability of NMIs.
  342. */
  343. noinstr void hv_nmi_check_nonrecoverable(struct pt_regs *regs)
  344. {
  345. #ifdef CONFIG_PPC_POWERNV
  346. unsigned long kbase = (unsigned long)_stext;
  347. unsigned long nip = regs->nip;
  348. if (!(regs->msr & MSR_RI))
  349. return;
  350. if (!(regs->msr & MSR_HV))
  351. return;
  352. if (regs->msr & MSR_PR)
  353. return;
  354. /*
  355. * Now test if the interrupt has hit a range that may be using
  356. * HSPRG1 without having RI=0 (i.e., an HSRR interrupt). The
  357. * problem ranges all run un-relocated. Test real and virt modes
  358. * at the same time by dropping the high bit of the nip (virt mode
  359. * entry points still have the +0x4000 offset).
  360. */
  361. nip &= ~0xc000000000000000ULL;
  362. if ((nip >= 0x500 && nip < 0x600) || (nip >= 0x4500 && nip < 0x4600))
  363. goto nonrecoverable;
  364. if ((nip >= 0x980 && nip < 0xa00) || (nip >= 0x4980 && nip < 0x4a00))
  365. goto nonrecoverable;
  366. if ((nip >= 0xe00 && nip < 0xec0) || (nip >= 0x4e00 && nip < 0x4ec0))
  367. goto nonrecoverable;
  368. if ((nip >= 0xf80 && nip < 0xfa0) || (nip >= 0x4f80 && nip < 0x4fa0))
  369. goto nonrecoverable;
  370. /* Trampoline code runs un-relocated so subtract kbase. */
  371. if (nip >= (unsigned long)(start_real_trampolines - kbase) &&
  372. nip < (unsigned long)(end_real_trampolines - kbase))
  373. goto nonrecoverable;
  374. if (nip >= (unsigned long)(start_virt_trampolines - kbase) &&
  375. nip < (unsigned long)(end_virt_trampolines - kbase))
  376. goto nonrecoverable;
  377. return;
  378. nonrecoverable:
  379. regs->msr &= ~MSR_RI;
  380. local_paca->hsrr_valid = 0;
  381. local_paca->srr_valid = 0;
  382. #endif
  383. }
  384. DEFINE_INTERRUPT_HANDLER_NMI(system_reset_exception)
  385. {
  386. unsigned long hsrr0, hsrr1;
  387. bool saved_hsrrs = false;
  388. /*
  389. * System reset can interrupt code where HSRRs are live and MSR[RI]=1.
  390. * The system reset interrupt itself may clobber HSRRs (e.g., to call
  391. * OPAL), so save them here and restore them before returning.
  392. *
  393. * Machine checks don't need to save HSRRs, as the real mode handler
  394. * is careful to avoid them, and the regular handler is not delivered
  395. * as an NMI.
  396. */
  397. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  398. hsrr0 = mfspr(SPRN_HSRR0);
  399. hsrr1 = mfspr(SPRN_HSRR1);
  400. saved_hsrrs = true;
  401. }
  402. hv_nmi_check_nonrecoverable(regs);
  403. __this_cpu_inc(irq_stat.sreset_irqs);
  404. /* See if any machine dependent calls */
  405. if (ppc_md.system_reset_exception) {
  406. if (ppc_md.system_reset_exception(regs))
  407. goto out;
  408. }
  409. if (debugger(regs))
  410. goto out;
  411. kmsg_dump(KMSG_DUMP_OOPS);
  412. /*
  413. * A system reset is a request to dump, so we always send
  414. * it through the crashdump code (if fadump or kdump are
  415. * registered).
  416. */
  417. crash_fadump(regs, "System Reset");
  418. crash_kexec(regs);
  419. /*
  420. * We aren't the primary crash CPU. We need to send it
  421. * to a holding pattern to avoid it ending up in the panic
  422. * code.
  423. */
  424. crash_kexec_secondary(regs);
  425. /*
  426. * No debugger or crash dump registered, print logs then
  427. * panic.
  428. */
  429. die("System Reset", regs, SIGABRT);
  430. mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
  431. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  432. nmi_panic(regs, "System Reset");
  433. out:
  434. #ifdef CONFIG_PPC_BOOK3S_64
  435. BUG_ON(get_paca()->in_nmi == 0);
  436. if (get_paca()->in_nmi > 1)
  437. die("Unrecoverable nested System Reset", regs, SIGABRT);
  438. #endif
  439. /* Must die if the interrupt is not recoverable */
  440. if (regs_is_unrecoverable(regs)) {
  441. /* For the reason explained in die_mce, nmi_exit before die */
  442. nmi_exit();
  443. die("Unrecoverable System Reset", regs, SIGABRT);
  444. }
  445. if (saved_hsrrs) {
  446. mtspr(SPRN_HSRR0, hsrr0);
  447. mtspr(SPRN_HSRR1, hsrr1);
  448. }
  449. /* What should we do here? We could issue a shutdown or hard reset. */
  450. return 0;
  451. }
  452. /*
  453. * I/O accesses can cause machine checks on powermacs.
  454. * Check if the NIP corresponds to the address of a sync
  455. * instruction for which there is an entry in the exception
  456. * table.
  457. * -- paulus.
  458. */
  459. static inline int check_io_access(struct pt_regs *regs)
  460. {
  461. #ifdef CONFIG_PPC32
  462. unsigned long msr = regs->msr;
  463. const struct exception_table_entry *entry;
  464. unsigned int *nip = (unsigned int *)regs->nip;
  465. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  466. && (entry = search_exception_tables(regs->nip)) != NULL) {
  467. /*
  468. * Check that it's a sync instruction, or somewhere
  469. * in the twi; isync; nop sequence that inb/inw/inl uses.
  470. * As the address is in the exception table
  471. * we should be able to read the instr there.
  472. * For the debug message, we look at the preceding
  473. * load or store.
  474. */
  475. if (*nip == PPC_RAW_NOP())
  476. nip -= 2;
  477. else if (*nip == PPC_RAW_ISYNC())
  478. --nip;
  479. if (*nip == PPC_RAW_SYNC() || get_op(*nip) == OP_TRAP) {
  480. unsigned int rb;
  481. --nip;
  482. rb = (*nip >> 11) & 0x1f;
  483. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  484. (*nip & 0x100)? "OUT to": "IN from",
  485. regs->gpr[rb] - _IO_BASE, nip);
  486. regs_set_recoverable(regs);
  487. regs_set_return_ip(regs, extable_fixup(entry));
  488. return 1;
  489. }
  490. }
  491. #endif /* CONFIG_PPC32 */
  492. return 0;
  493. }
  494. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  495. /* On 4xx, the reason for the machine check or program exception
  496. is in the ESR. */
  497. #define get_reason(regs) ((regs)->esr)
  498. #define REASON_FP ESR_FP
  499. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  500. #define REASON_PRIVILEGED ESR_PPR
  501. #define REASON_TRAP ESR_PTR
  502. #define REASON_PREFIXED 0
  503. #define REASON_BOUNDARY 0
  504. /* single-step stuff */
  505. #define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
  506. #define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
  507. #define clear_br_trace(regs) do {} while(0)
  508. #else
  509. /* On non-4xx, the reason for the machine check or program
  510. exception is in the MSR. */
  511. #define get_reason(regs) ((regs)->msr)
  512. #define REASON_TM SRR1_PROGTM
  513. #define REASON_FP SRR1_PROGFPE
  514. #define REASON_ILLEGAL SRR1_PROGILL
  515. #define REASON_PRIVILEGED SRR1_PROGPRIV
  516. #define REASON_TRAP SRR1_PROGTRAP
  517. #define REASON_PREFIXED SRR1_PREFIXED
  518. #define REASON_BOUNDARY SRR1_BOUNDARY
  519. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  520. #define clear_single_step(regs) (regs_set_return_msr((regs), (regs)->msr & ~MSR_SE))
  521. #define clear_br_trace(regs) (regs_set_return_msr((regs), (regs)->msr & ~MSR_BE))
  522. #endif
  523. #define inst_length(reason) (((reason) & REASON_PREFIXED) ? 8 : 4)
  524. #if defined(CONFIG_PPC_E500)
  525. int machine_check_e500mc(struct pt_regs *regs)
  526. {
  527. unsigned long mcsr = mfspr(SPRN_MCSR);
  528. unsigned long pvr = mfspr(SPRN_PVR);
  529. unsigned long reason = mcsr;
  530. int recoverable = 1;
  531. if (reason & MCSR_LD) {
  532. recoverable = fsl_rio_mcheck_exception(regs);
  533. if (recoverable == 1)
  534. goto silent_out;
  535. }
  536. printk("Machine check in kernel mode.\n");
  537. printk("Caused by (from MCSR=%lx): ", reason);
  538. if (reason & MCSR_MCP)
  539. pr_cont("Machine Check Signal\n");
  540. if (reason & MCSR_ICPERR) {
  541. pr_cont("Instruction Cache Parity Error\n");
  542. /*
  543. * This is recoverable by invalidating the i-cache.
  544. */
  545. mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
  546. while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
  547. ;
  548. /*
  549. * This will generally be accompanied by an instruction
  550. * fetch error report -- only treat MCSR_IF as fatal
  551. * if it wasn't due to an L1 parity error.
  552. */
  553. reason &= ~MCSR_IF;
  554. }
  555. if (reason & MCSR_DCPERR_MC) {
  556. pr_cont("Data Cache Parity Error\n");
  557. /*
  558. * In write shadow mode we auto-recover from the error, but it
  559. * may still get logged and cause a machine check. We should
  560. * only treat the non-write shadow case as non-recoverable.
  561. */
  562. /* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
  563. * is not implemented but L1 data cache always runs in write
  564. * shadow mode. Hence on data cache parity errors HW will
  565. * automatically invalidate the L1 Data Cache.
  566. */
  567. if (PVR_VER(pvr) != PVR_VER_E6500) {
  568. if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
  569. recoverable = 0;
  570. }
  571. }
  572. if (reason & MCSR_L2MMU_MHIT) {
  573. pr_cont("Hit on multiple TLB entries\n");
  574. recoverable = 0;
  575. }
  576. if (reason & MCSR_NMI)
  577. pr_cont("Non-maskable interrupt\n");
  578. if (reason & MCSR_IF) {
  579. pr_cont("Instruction Fetch Error Report\n");
  580. recoverable = 0;
  581. }
  582. if (reason & MCSR_LD) {
  583. pr_cont("Load Error Report\n");
  584. recoverable = 0;
  585. }
  586. if (reason & MCSR_ST) {
  587. pr_cont("Store Error Report\n");
  588. recoverable = 0;
  589. }
  590. if (reason & MCSR_LDG) {
  591. pr_cont("Guarded Load Error Report\n");
  592. recoverable = 0;
  593. }
  594. if (reason & MCSR_TLBSYNC)
  595. pr_cont("Simultaneous tlbsync operations\n");
  596. if (reason & MCSR_BSL2_ERR) {
  597. pr_cont("Level 2 Cache Error\n");
  598. recoverable = 0;
  599. }
  600. if (reason & MCSR_MAV) {
  601. u64 addr;
  602. addr = mfspr(SPRN_MCAR);
  603. addr |= (u64)mfspr(SPRN_MCARU) << 32;
  604. pr_cont("Machine Check %s Address: %#llx\n",
  605. reason & MCSR_MEA ? "Effective" : "Physical", addr);
  606. }
  607. silent_out:
  608. mtspr(SPRN_MCSR, mcsr);
  609. return mfspr(SPRN_MCSR) == 0 && recoverable;
  610. }
  611. int machine_check_e500(struct pt_regs *regs)
  612. {
  613. unsigned long reason = mfspr(SPRN_MCSR);
  614. if (reason & MCSR_BUS_RBERR) {
  615. if (fsl_rio_mcheck_exception(regs))
  616. return 1;
  617. if (fsl_pci_mcheck_exception(regs))
  618. return 1;
  619. }
  620. printk("Machine check in kernel mode.\n");
  621. printk("Caused by (from MCSR=%lx): ", reason);
  622. if (reason & MCSR_MCP)
  623. pr_cont("Machine Check Signal\n");
  624. if (reason & MCSR_ICPERR)
  625. pr_cont("Instruction Cache Parity Error\n");
  626. if (reason & MCSR_DCP_PERR)
  627. pr_cont("Data Cache Push Parity Error\n");
  628. if (reason & MCSR_DCPERR)
  629. pr_cont("Data Cache Parity Error\n");
  630. if (reason & MCSR_BUS_IAERR)
  631. pr_cont("Bus - Instruction Address Error\n");
  632. if (reason & MCSR_BUS_RAERR)
  633. pr_cont("Bus - Read Address Error\n");
  634. if (reason & MCSR_BUS_WAERR)
  635. pr_cont("Bus - Write Address Error\n");
  636. if (reason & MCSR_BUS_IBERR)
  637. pr_cont("Bus - Instruction Data Error\n");
  638. if (reason & MCSR_BUS_RBERR)
  639. pr_cont("Bus - Read Data Bus Error\n");
  640. if (reason & MCSR_BUS_WBERR)
  641. pr_cont("Bus - Write Data Bus Error\n");
  642. if (reason & MCSR_BUS_IPERR)
  643. pr_cont("Bus - Instruction Parity Error\n");
  644. if (reason & MCSR_BUS_RPERR)
  645. pr_cont("Bus - Read Parity Error\n");
  646. return 0;
  647. }
  648. int machine_check_generic(struct pt_regs *regs)
  649. {
  650. return 0;
  651. }
  652. #elif defined(CONFIG_PPC32)
  653. int machine_check_generic(struct pt_regs *regs)
  654. {
  655. unsigned long reason = regs->msr;
  656. printk("Machine check in kernel mode.\n");
  657. printk("Caused by (from SRR1=%lx): ", reason);
  658. switch (reason & 0x601F0000) {
  659. case 0x80000:
  660. pr_cont("Machine check signal\n");
  661. break;
  662. case 0x40000:
  663. case 0x140000: /* 7450 MSS error and TEA */
  664. pr_cont("Transfer error ack signal\n");
  665. break;
  666. case 0x20000:
  667. pr_cont("Data parity error signal\n");
  668. break;
  669. case 0x10000:
  670. pr_cont("Address parity error signal\n");
  671. break;
  672. case 0x20000000:
  673. pr_cont("L1 Data Cache error\n");
  674. break;
  675. case 0x40000000:
  676. pr_cont("L1 Instruction Cache error\n");
  677. break;
  678. case 0x00100000:
  679. pr_cont("L2 data cache parity error\n");
  680. break;
  681. default:
  682. pr_cont("Unknown values in msr\n");
  683. }
  684. return 0;
  685. }
  686. #endif /* everything else */
  687. void die_mce(const char *str, struct pt_regs *regs, long err)
  688. {
  689. /*
  690. * The machine check wants to kill the interrupted context,
  691. * but make_task_dead() checks for in_interrupt() and panics
  692. * in that case, so exit the irq/nmi before calling die.
  693. */
  694. if (in_nmi())
  695. nmi_exit();
  696. else
  697. irq_exit();
  698. die(str, regs, err);
  699. }
  700. /*
  701. * BOOK3S_64 does not usually call this handler as a non-maskable interrupt
  702. * (it uses its own early real-mode handler to handle the MCE proper
  703. * and then raises irq_work to call this handler when interrupts are
  704. * enabled). The only time when this is not true is if the early handler
  705. * is unrecoverable, then it does call this directly to try to get a
  706. * message out.
  707. */
  708. static void __machine_check_exception(struct pt_regs *regs)
  709. {
  710. int recover = 0;
  711. __this_cpu_inc(irq_stat.mce_exceptions);
  712. add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
  713. /* See if any machine dependent calls. In theory, we would want
  714. * to call the CPU first, and call the ppc_md. one if the CPU
  715. * one returns a positive number. However there is existing code
  716. * that assumes the board gets a first chance, so let's keep it
  717. * that way for now and fix things later. --BenH.
  718. */
  719. if (ppc_md.machine_check_exception)
  720. recover = ppc_md.machine_check_exception(regs);
  721. else if (cur_cpu_spec->machine_check)
  722. recover = cur_cpu_spec->machine_check(regs);
  723. if (recover > 0)
  724. goto bail;
  725. if (debugger_fault_handler(regs))
  726. goto bail;
  727. if (check_io_access(regs))
  728. goto bail;
  729. die_mce("Machine check", regs, SIGBUS);
  730. bail:
  731. /* Must die if the interrupt is not recoverable */
  732. if (regs_is_unrecoverable(regs))
  733. die_mce("Unrecoverable Machine check", regs, SIGBUS);
  734. }
  735. #ifdef CONFIG_PPC_BOOK3S_64
  736. DEFINE_INTERRUPT_HANDLER_RAW(machine_check_early_boot)
  737. {
  738. udbg_printf("Machine check (early boot)\n");
  739. udbg_printf("SRR0=0x%016lx SRR1=0x%016lx\n", regs->nip, regs->msr);
  740. udbg_printf(" DAR=0x%016lx DSISR=0x%08lx\n", regs->dar, regs->dsisr);
  741. udbg_printf(" LR=0x%016lx R1=0x%08lx\n", regs->link, regs->gpr[1]);
  742. udbg_printf("------\n");
  743. die("Machine check (early boot)", regs, SIGBUS);
  744. for (;;)
  745. ;
  746. return 0;
  747. }
  748. DEFINE_INTERRUPT_HANDLER_ASYNC(machine_check_exception_async)
  749. {
  750. __machine_check_exception(regs);
  751. }
  752. #endif
  753. DEFINE_INTERRUPT_HANDLER_NMI(machine_check_exception)
  754. {
  755. __machine_check_exception(regs);
  756. return 0;
  757. }
  758. DEFINE_INTERRUPT_HANDLER(SMIException) /* async? */
  759. {
  760. die("System Management Interrupt", regs, SIGABRT);
  761. }
  762. #ifdef CONFIG_VSX
  763. static void p9_hmi_special_emu(struct pt_regs *regs)
  764. {
  765. unsigned int ra, rb, t, i, sel, instr, rc;
  766. const void __user *addr;
  767. u8 vbuf[16] __aligned(16), *vdst;
  768. unsigned long ea, msr, msr_mask;
  769. bool swap;
  770. if (__get_user(instr, (unsigned int __user *)regs->nip))
  771. return;
  772. /*
  773. * lxvb16x opcode: 0x7c0006d8
  774. * lxvd2x opcode: 0x7c000698
  775. * lxvh8x opcode: 0x7c000658
  776. * lxvw4x opcode: 0x7c000618
  777. */
  778. if ((instr & 0xfc00073e) != 0x7c000618) {
  779. pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx"
  780. " instr=%08x\n",
  781. smp_processor_id(), current->comm, current->pid,
  782. regs->nip, instr);
  783. return;
  784. }
  785. /* Grab vector registers into the task struct */
  786. msr = regs->msr; /* Grab msr before we flush the bits */
  787. flush_vsx_to_thread(current);
  788. enable_kernel_altivec();
  789. /*
  790. * Is userspace running with a different endian (this is rare but
  791. * not impossible)
  792. */
  793. swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
  794. /* Decode the instruction */
  795. ra = (instr >> 16) & 0x1f;
  796. rb = (instr >> 11) & 0x1f;
  797. t = (instr >> 21) & 0x1f;
  798. if (instr & 1)
  799. vdst = (u8 *)&current->thread.vr_state.vr[t];
  800. else
  801. vdst = (u8 *)&current->thread.fp_state.fpr[t][0];
  802. /* Grab the vector address */
  803. ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0);
  804. if (is_32bit_task())
  805. ea &= 0xfffffffful;
  806. addr = (__force const void __user *)ea;
  807. /* Check it */
  808. if (!access_ok(addr, 16)) {
  809. pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx"
  810. " instr=%08x addr=%016lx\n",
  811. smp_processor_id(), current->comm, current->pid,
  812. regs->nip, instr, (unsigned long)addr);
  813. return;
  814. }
  815. /* Read the vector */
  816. rc = 0;
  817. if ((unsigned long)addr & 0xfUL)
  818. /* unaligned case */
  819. rc = __copy_from_user_inatomic(vbuf, addr, 16);
  820. else
  821. __get_user_atomic_128_aligned(vbuf, addr, rc);
  822. if (rc) {
  823. pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx"
  824. " instr=%08x addr=%016lx\n",
  825. smp_processor_id(), current->comm, current->pid,
  826. regs->nip, instr, (unsigned long)addr);
  827. return;
  828. }
  829. pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx"
  830. " instr=%08x addr=%016lx\n",
  831. smp_processor_id(), current->comm, current->pid, regs->nip,
  832. instr, (unsigned long) addr);
  833. /* Grab instruction "selector" */
  834. sel = (instr >> 6) & 3;
  835. /*
  836. * Check to make sure the facility is actually enabled. This
  837. * could happen if we get a false positive hit.
  838. *
  839. * lxvd2x/lxvw4x always check MSR VSX sel = 0,2
  840. * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3
  841. */
  842. msr_mask = MSR_VSX;
  843. if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */
  844. msr_mask = MSR_VEC;
  845. if (!(msr & msr_mask)) {
  846. pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx"
  847. " instr=%08x msr:%016lx\n",
  848. smp_processor_id(), current->comm, current->pid,
  849. regs->nip, instr, msr);
  850. return;
  851. }
  852. /* Do logging here before we modify sel based on endian */
  853. switch (sel) {
  854. case 0: /* lxvw4x */
  855. PPC_WARN_EMULATED(lxvw4x, regs);
  856. break;
  857. case 1: /* lxvh8x */
  858. PPC_WARN_EMULATED(lxvh8x, regs);
  859. break;
  860. case 2: /* lxvd2x */
  861. PPC_WARN_EMULATED(lxvd2x, regs);
  862. break;
  863. case 3: /* lxvb16x */
  864. PPC_WARN_EMULATED(lxvb16x, regs);
  865. break;
  866. }
  867. #ifdef __LITTLE_ENDIAN__
  868. /*
  869. * An LE kernel stores the vector in the task struct as an LE
  870. * byte array (effectively swapping both the components and
  871. * the content of the components). Those instructions expect
  872. * the components to remain in ascending address order, so we
  873. * swap them back.
  874. *
  875. * If we are running a BE user space, the expectation is that
  876. * of a simple memcpy, so forcing the emulation to look like
  877. * a lxvb16x should do the trick.
  878. */
  879. if (swap)
  880. sel = 3;
  881. switch (sel) {
  882. case 0: /* lxvw4x */
  883. for (i = 0; i < 4; i++)
  884. ((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i];
  885. break;
  886. case 1: /* lxvh8x */
  887. for (i = 0; i < 8; i++)
  888. ((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i];
  889. break;
  890. case 2: /* lxvd2x */
  891. for (i = 0; i < 2; i++)
  892. ((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i];
  893. break;
  894. case 3: /* lxvb16x */
  895. for (i = 0; i < 16; i++)
  896. vdst[i] = vbuf[15-i];
  897. break;
  898. }
  899. #else /* __LITTLE_ENDIAN__ */
  900. /* On a big endian kernel, a BE userspace only needs a memcpy */
  901. if (!swap)
  902. sel = 3;
  903. /* Otherwise, we need to swap the content of the components */
  904. switch (sel) {
  905. case 0: /* lxvw4x */
  906. for (i = 0; i < 4; i++)
  907. ((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]);
  908. break;
  909. case 1: /* lxvh8x */
  910. for (i = 0; i < 8; i++)
  911. ((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]);
  912. break;
  913. case 2: /* lxvd2x */
  914. for (i = 0; i < 2; i++)
  915. ((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]);
  916. break;
  917. case 3: /* lxvb16x */
  918. memcpy(vdst, vbuf, 16);
  919. break;
  920. }
  921. #endif /* !__LITTLE_ENDIAN__ */
  922. /* Go to next instruction */
  923. regs_add_return_ip(regs, 4);
  924. }
  925. #endif /* CONFIG_VSX */
  926. DEFINE_INTERRUPT_HANDLER_ASYNC(handle_hmi_exception)
  927. {
  928. struct pt_regs *old_regs;
  929. old_regs = set_irq_regs(regs);
  930. #ifdef CONFIG_VSX
  931. /* Real mode flagged P9 special emu is needed */
  932. if (local_paca->hmi_p9_special_emu) {
  933. local_paca->hmi_p9_special_emu = 0;
  934. /*
  935. * We don't want to take page faults while doing the
  936. * emulation, we just replay the instruction if necessary.
  937. */
  938. pagefault_disable();
  939. p9_hmi_special_emu(regs);
  940. pagefault_enable();
  941. }
  942. #endif /* CONFIG_VSX */
  943. if (ppc_md.handle_hmi_exception)
  944. ppc_md.handle_hmi_exception(regs);
  945. set_irq_regs(old_regs);
  946. }
  947. DEFINE_INTERRUPT_HANDLER(unknown_exception)
  948. {
  949. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  950. regs->nip, regs->msr, regs->trap);
  951. _exception(SIGTRAP, regs, TRAP_UNK, 0);
  952. }
  953. DEFINE_INTERRUPT_HANDLER_ASYNC(unknown_async_exception)
  954. {
  955. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  956. regs->nip, regs->msr, regs->trap);
  957. _exception(SIGTRAP, regs, TRAP_UNK, 0);
  958. }
  959. DEFINE_INTERRUPT_HANDLER_NMI(unknown_nmi_exception)
  960. {
  961. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  962. regs->nip, regs->msr, regs->trap);
  963. _exception(SIGTRAP, regs, TRAP_UNK, 0);
  964. return 0;
  965. }
  966. DEFINE_INTERRUPT_HANDLER(instruction_breakpoint_exception)
  967. {
  968. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  969. 5, SIGTRAP) == NOTIFY_STOP)
  970. return;
  971. if (debugger_iabr_match(regs))
  972. return;
  973. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  974. }
  975. DEFINE_INTERRUPT_HANDLER(RunModeException)
  976. {
  977. _exception(SIGTRAP, regs, TRAP_UNK, 0);
  978. }
  979. static void __single_step_exception(struct pt_regs *regs)
  980. {
  981. clear_single_step(regs);
  982. clear_br_trace(regs);
  983. if (kprobe_post_handler(regs))
  984. return;
  985. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  986. 5, SIGTRAP) == NOTIFY_STOP)
  987. return;
  988. if (debugger_sstep(regs))
  989. return;
  990. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  991. }
  992. DEFINE_INTERRUPT_HANDLER(single_step_exception)
  993. {
  994. __single_step_exception(regs);
  995. }
  996. /*
  997. * After we have successfully emulated an instruction, we have to
  998. * check if the instruction was being single-stepped, and if so,
  999. * pretend we got a single-step exception. This was pointed out
  1000. * by Kumar Gala. -- paulus
  1001. */
  1002. static void emulate_single_step(struct pt_regs *regs)
  1003. {
  1004. if (single_stepping(regs))
  1005. __single_step_exception(regs);
  1006. }
  1007. #ifdef CONFIG_PPC_FPU_REGS
  1008. static inline int __parse_fpscr(unsigned long fpscr)
  1009. {
  1010. int ret = FPE_FLTUNK;
  1011. /* Invalid operation */
  1012. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  1013. ret = FPE_FLTINV;
  1014. /* Overflow */
  1015. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  1016. ret = FPE_FLTOVF;
  1017. /* Underflow */
  1018. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  1019. ret = FPE_FLTUND;
  1020. /* Divide by zero */
  1021. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  1022. ret = FPE_FLTDIV;
  1023. /* Inexact result */
  1024. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  1025. ret = FPE_FLTRES;
  1026. return ret;
  1027. }
  1028. #endif
  1029. static void parse_fpe(struct pt_regs *regs)
  1030. {
  1031. int code = 0;
  1032. flush_fp_to_thread(current);
  1033. #ifdef CONFIG_PPC_FPU_REGS
  1034. code = __parse_fpscr(current->thread.fp_state.fpscr);
  1035. #endif
  1036. _exception(SIGFPE, regs, code, regs->nip);
  1037. }
  1038. /*
  1039. * Illegal instruction emulation support. Originally written to
  1040. * provide the PVR to user applications using the mfspr rd, PVR.
  1041. * Return non-zero if we can't emulate, or -EFAULT if the associated
  1042. * memory access caused an access fault. Return zero on success.
  1043. *
  1044. * There are a couple of ways to do this, either "decode" the instruction
  1045. * or directly match lots of bits. In this case, matching lots of
  1046. * bits is faster and easier.
  1047. *
  1048. */
  1049. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  1050. {
  1051. u8 rT = (instword >> 21) & 0x1f;
  1052. u8 rA = (instword >> 16) & 0x1f;
  1053. u8 NB_RB = (instword >> 11) & 0x1f;
  1054. u32 num_bytes;
  1055. unsigned long EA;
  1056. int pos = 0;
  1057. /* Early out if we are an invalid form of lswx */
  1058. if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
  1059. if ((rT == rA) || (rT == NB_RB))
  1060. return -EINVAL;
  1061. EA = (rA == 0) ? 0 : regs->gpr[rA];
  1062. switch (instword & PPC_INST_STRING_MASK) {
  1063. case PPC_INST_LSWX:
  1064. case PPC_INST_STSWX:
  1065. EA += NB_RB;
  1066. num_bytes = regs->xer & 0x7f;
  1067. break;
  1068. case PPC_INST_LSWI:
  1069. case PPC_INST_STSWI:
  1070. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  1071. break;
  1072. default:
  1073. return -EINVAL;
  1074. }
  1075. while (num_bytes != 0)
  1076. {
  1077. u8 val;
  1078. u32 shift = 8 * (3 - (pos & 0x3));
  1079. /* if process is 32-bit, clear upper 32 bits of EA */
  1080. if ((regs->msr & MSR_64BIT) == 0)
  1081. EA &= 0xFFFFFFFF;
  1082. switch ((instword & PPC_INST_STRING_MASK)) {
  1083. case PPC_INST_LSWX:
  1084. case PPC_INST_LSWI:
  1085. if (get_user(val, (u8 __user *)EA))
  1086. return -EFAULT;
  1087. /* first time updating this reg,
  1088. * zero it out */
  1089. if (pos == 0)
  1090. regs->gpr[rT] = 0;
  1091. regs->gpr[rT] |= val << shift;
  1092. break;
  1093. case PPC_INST_STSWI:
  1094. case PPC_INST_STSWX:
  1095. val = regs->gpr[rT] >> shift;
  1096. if (put_user(val, (u8 __user *)EA))
  1097. return -EFAULT;
  1098. break;
  1099. }
  1100. /* move EA to next address */
  1101. EA += 1;
  1102. num_bytes--;
  1103. /* manage our position within the register */
  1104. if (++pos == 4) {
  1105. pos = 0;
  1106. if (++rT == 32)
  1107. rT = 0;
  1108. }
  1109. }
  1110. return 0;
  1111. }
  1112. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  1113. {
  1114. u32 ra,rs;
  1115. unsigned long tmp;
  1116. ra = (instword >> 16) & 0x1f;
  1117. rs = (instword >> 21) & 0x1f;
  1118. tmp = regs->gpr[rs];
  1119. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  1120. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  1121. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  1122. regs->gpr[ra] = tmp;
  1123. return 0;
  1124. }
  1125. static int emulate_isel(struct pt_regs *regs, u32 instword)
  1126. {
  1127. u8 rT = (instword >> 21) & 0x1f;
  1128. u8 rA = (instword >> 16) & 0x1f;
  1129. u8 rB = (instword >> 11) & 0x1f;
  1130. u8 BC = (instword >> 6) & 0x1f;
  1131. u8 bit;
  1132. unsigned long tmp;
  1133. tmp = (rA == 0) ? 0 : regs->gpr[rA];
  1134. bit = (regs->ccr >> (31 - BC)) & 0x1;
  1135. regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
  1136. return 0;
  1137. }
  1138. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1139. static inline bool tm_abort_check(struct pt_regs *regs, int cause)
  1140. {
  1141. /* If we're emulating a load/store in an active transaction, we cannot
  1142. * emulate it as the kernel operates in transaction suspended context.
  1143. * We need to abort the transaction. This creates a persistent TM
  1144. * abort so tell the user what caused it with a new code.
  1145. */
  1146. if (MSR_TM_TRANSACTIONAL(regs->msr)) {
  1147. tm_enable();
  1148. tm_abort(cause);
  1149. return true;
  1150. }
  1151. return false;
  1152. }
  1153. #else
  1154. static inline bool tm_abort_check(struct pt_regs *regs, int reason)
  1155. {
  1156. return false;
  1157. }
  1158. #endif
  1159. static int emulate_instruction(struct pt_regs *regs)
  1160. {
  1161. u32 instword;
  1162. u32 rd;
  1163. if (!user_mode(regs))
  1164. return -EINVAL;
  1165. if (get_user(instword, (u32 __user *)(regs->nip)))
  1166. return -EFAULT;
  1167. /* Emulate the mfspr rD, PVR. */
  1168. if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
  1169. PPC_WARN_EMULATED(mfpvr, regs);
  1170. rd = (instword >> 21) & 0x1f;
  1171. regs->gpr[rd] = mfspr(SPRN_PVR);
  1172. return 0;
  1173. }
  1174. /* Emulating the dcba insn is just a no-op. */
  1175. if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
  1176. PPC_WARN_EMULATED(dcba, regs);
  1177. return 0;
  1178. }
  1179. /* Emulate the mcrxr insn. */
  1180. if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
  1181. int shift = (instword >> 21) & 0x1c;
  1182. unsigned long msk = 0xf0000000UL >> shift;
  1183. PPC_WARN_EMULATED(mcrxr, regs);
  1184. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  1185. regs->xer &= ~0xf0000000UL;
  1186. return 0;
  1187. }
  1188. /* Emulate load/store string insn. */
  1189. if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
  1190. if (tm_abort_check(regs,
  1191. TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
  1192. return -EINVAL;
  1193. PPC_WARN_EMULATED(string, regs);
  1194. return emulate_string_inst(regs, instword);
  1195. }
  1196. /* Emulate the popcntb (Population Count Bytes) instruction. */
  1197. if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
  1198. PPC_WARN_EMULATED(popcntb, regs);
  1199. return emulate_popcntb_inst(regs, instword);
  1200. }
  1201. /* Emulate isel (Integer Select) instruction */
  1202. if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
  1203. PPC_WARN_EMULATED(isel, regs);
  1204. return emulate_isel(regs, instword);
  1205. }
  1206. /* Emulate sync instruction variants */
  1207. if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
  1208. PPC_WARN_EMULATED(sync, regs);
  1209. asm volatile("sync");
  1210. return 0;
  1211. }
  1212. #ifdef CONFIG_PPC64
  1213. /* Emulate the mfspr rD, DSCR. */
  1214. if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
  1215. PPC_INST_MFSPR_DSCR_USER) ||
  1216. ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
  1217. PPC_INST_MFSPR_DSCR)) &&
  1218. cpu_has_feature(CPU_FTR_DSCR)) {
  1219. PPC_WARN_EMULATED(mfdscr, regs);
  1220. rd = (instword >> 21) & 0x1f;
  1221. regs->gpr[rd] = mfspr(SPRN_DSCR);
  1222. return 0;
  1223. }
  1224. /* Emulate the mtspr DSCR, rD. */
  1225. if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
  1226. PPC_INST_MTSPR_DSCR_USER) ||
  1227. ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
  1228. PPC_INST_MTSPR_DSCR)) &&
  1229. cpu_has_feature(CPU_FTR_DSCR)) {
  1230. PPC_WARN_EMULATED(mtdscr, regs);
  1231. rd = (instword >> 21) & 0x1f;
  1232. current->thread.dscr = regs->gpr[rd];
  1233. current->thread.dscr_inherit = 1;
  1234. mtspr(SPRN_DSCR, current->thread.dscr);
  1235. return 0;
  1236. }
  1237. #endif
  1238. return -EINVAL;
  1239. }
  1240. int is_valid_bugaddr(unsigned long addr)
  1241. {
  1242. return is_kernel_addr(addr);
  1243. }
  1244. #ifdef CONFIG_MATH_EMULATION
  1245. static int emulate_math(struct pt_regs *regs)
  1246. {
  1247. int ret;
  1248. ret = do_mathemu(regs);
  1249. if (ret >= 0)
  1250. PPC_WARN_EMULATED(math, regs);
  1251. switch (ret) {
  1252. case 0:
  1253. emulate_single_step(regs);
  1254. return 0;
  1255. case 1: {
  1256. int code = 0;
  1257. code = __parse_fpscr(current->thread.fp_state.fpscr);
  1258. _exception(SIGFPE, regs, code, regs->nip);
  1259. return 0;
  1260. }
  1261. case -EFAULT:
  1262. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  1263. return 0;
  1264. }
  1265. return -1;
  1266. }
  1267. #else
  1268. static inline int emulate_math(struct pt_regs *regs) { return -1; }
  1269. #endif
  1270. static void do_program_check(struct pt_regs *regs)
  1271. {
  1272. unsigned int reason = get_reason(regs);
  1273. /* We can now get here via a FP Unavailable exception if the core
  1274. * has no FPU, in that case the reason flags will be 0 */
  1275. if (reason & REASON_FP) {
  1276. /* IEEE FP exception */
  1277. parse_fpe(regs);
  1278. return;
  1279. }
  1280. if (reason & REASON_TRAP) {
  1281. unsigned long bugaddr;
  1282. /* Debugger is first in line to stop recursive faults in
  1283. * rcu_lock, notify_die, or atomic_notifier_call_chain */
  1284. if (debugger_bpt(regs))
  1285. return;
  1286. if (kprobe_handler(regs))
  1287. return;
  1288. /* trap exception */
  1289. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  1290. == NOTIFY_STOP)
  1291. return;
  1292. bugaddr = regs->nip;
  1293. /*
  1294. * Fixup bugaddr for BUG_ON() in real mode
  1295. */
  1296. if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
  1297. bugaddr += PAGE_OFFSET;
  1298. if (!(regs->msr & MSR_PR) && /* not user-mode */
  1299. report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
  1300. const struct exception_table_entry *entry;
  1301. entry = search_exception_tables(bugaddr);
  1302. if (entry) {
  1303. regs_set_return_ip(regs, extable_fixup(entry) + regs->nip - bugaddr);
  1304. return;
  1305. }
  1306. }
  1307. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  1308. return;
  1309. }
  1310. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1311. if (reason & REASON_TM) {
  1312. /* This is a TM "Bad Thing Exception" program check.
  1313. * This occurs when:
  1314. * - An rfid/hrfid/mtmsrd attempts to cause an illegal
  1315. * transition in TM states.
  1316. * - A trechkpt is attempted when transactional.
  1317. * - A treclaim is attempted when non transactional.
  1318. * - A tend is illegally attempted.
  1319. * - writing a TM SPR when transactional.
  1320. *
  1321. * If usermode caused this, it's done something illegal and
  1322. * gets a SIGILL slap on the wrist. We call it an illegal
  1323. * operand to distinguish from the instruction just being bad
  1324. * (e.g. executing a 'tend' on a CPU without TM!); it's an
  1325. * illegal /placement/ of a valid instruction.
  1326. */
  1327. if (user_mode(regs)) {
  1328. _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
  1329. return;
  1330. } else {
  1331. printk(KERN_EMERG "Unexpected TM Bad Thing exception "
  1332. "at %lx (msr 0x%lx) tm_scratch=%llx\n",
  1333. regs->nip, regs->msr, get_paca()->tm_scratch);
  1334. die("Unrecoverable exception", regs, SIGABRT);
  1335. }
  1336. }
  1337. #endif
  1338. /*
  1339. * If we took the program check in the kernel skip down to sending a
  1340. * SIGILL. The subsequent cases all relate to emulating instructions
  1341. * which we should only do for userspace. We also do not want to enable
  1342. * interrupts for kernel faults because that might lead to further
  1343. * faults, and loose the context of the original exception.
  1344. */
  1345. if (!user_mode(regs))
  1346. goto sigill;
  1347. interrupt_cond_local_irq_enable(regs);
  1348. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  1349. * but there seems to be a hardware bug on the 405GP (RevD)
  1350. * that means ESR is sometimes set incorrectly - either to
  1351. * ESR_DST (!?) or 0. In the process of chasing this with the
  1352. * hardware people - not sure if it can happen on any illegal
  1353. * instruction or only on FP instructions, whether there is a
  1354. * pattern to occurrences etc. -dgibson 31/Mar/2003
  1355. */
  1356. if (!emulate_math(regs))
  1357. return;
  1358. /* Try to emulate it if we should. */
  1359. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  1360. switch (emulate_instruction(regs)) {
  1361. case 0:
  1362. regs_add_return_ip(regs, 4);
  1363. emulate_single_step(regs);
  1364. return;
  1365. case -EFAULT:
  1366. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  1367. return;
  1368. }
  1369. }
  1370. sigill:
  1371. if (reason & REASON_PRIVILEGED)
  1372. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1373. else
  1374. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1375. }
  1376. DEFINE_INTERRUPT_HANDLER(program_check_exception)
  1377. {
  1378. do_program_check(regs);
  1379. }
  1380. /*
  1381. * This occurs when running in hypervisor mode on POWER6 or later
  1382. * and an illegal instruction is encountered.
  1383. */
  1384. DEFINE_INTERRUPT_HANDLER(emulation_assist_interrupt)
  1385. {
  1386. regs_set_return_msr(regs, regs->msr | REASON_ILLEGAL);
  1387. do_program_check(regs);
  1388. }
  1389. DEFINE_INTERRUPT_HANDLER(alignment_exception)
  1390. {
  1391. int sig, code, fixed = 0;
  1392. unsigned long reason;
  1393. interrupt_cond_local_irq_enable(regs);
  1394. reason = get_reason(regs);
  1395. if (reason & REASON_BOUNDARY) {
  1396. sig = SIGBUS;
  1397. code = BUS_ADRALN;
  1398. goto bad;
  1399. }
  1400. if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
  1401. return;
  1402. /* we don't implement logging of alignment exceptions */
  1403. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  1404. fixed = fix_alignment(regs);
  1405. if (fixed == 1) {
  1406. /* skip over emulated instruction */
  1407. regs_add_return_ip(regs, inst_length(reason));
  1408. emulate_single_step(regs);
  1409. return;
  1410. }
  1411. /* Operand address was bad */
  1412. if (fixed == -EFAULT) {
  1413. sig = SIGSEGV;
  1414. code = SEGV_ACCERR;
  1415. } else {
  1416. sig = SIGBUS;
  1417. code = BUS_ADRALN;
  1418. }
  1419. bad:
  1420. if (user_mode(regs))
  1421. _exception(sig, regs, code, regs->dar);
  1422. else
  1423. bad_page_fault(regs, sig);
  1424. }
  1425. DEFINE_INTERRUPT_HANDLER(stack_overflow_exception)
  1426. {
  1427. die("Kernel stack overflow", regs, SIGSEGV);
  1428. }
  1429. DEFINE_INTERRUPT_HANDLER(kernel_fp_unavailable_exception)
  1430. {
  1431. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  1432. "%lx at %lx\n", regs->trap, regs->nip);
  1433. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  1434. }
  1435. DEFINE_INTERRUPT_HANDLER(altivec_unavailable_exception)
  1436. {
  1437. if (user_mode(regs)) {
  1438. /* A user program has executed an altivec instruction,
  1439. but this kernel doesn't support altivec. */
  1440. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1441. return;
  1442. }
  1443. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  1444. "%lx at %lx\n", regs->trap, regs->nip);
  1445. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  1446. }
  1447. DEFINE_INTERRUPT_HANDLER(vsx_unavailable_exception)
  1448. {
  1449. if (user_mode(regs)) {
  1450. /* A user program has executed an vsx instruction,
  1451. but this kernel doesn't support vsx. */
  1452. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1453. return;
  1454. }
  1455. printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
  1456. "%lx at %lx\n", regs->trap, regs->nip);
  1457. die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
  1458. }
  1459. #ifdef CONFIG_PPC_BOOK3S_64
  1460. static void tm_unavailable(struct pt_regs *regs)
  1461. {
  1462. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1463. if (user_mode(regs)) {
  1464. current->thread.load_tm++;
  1465. regs_set_return_msr(regs, regs->msr | MSR_TM);
  1466. tm_enable();
  1467. tm_restore_sprs(&current->thread);
  1468. return;
  1469. }
  1470. #endif
  1471. pr_emerg("Unrecoverable TM Unavailable Exception "
  1472. "%lx at %lx\n", regs->trap, regs->nip);
  1473. die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
  1474. }
  1475. DEFINE_INTERRUPT_HANDLER(facility_unavailable_exception)
  1476. {
  1477. static char *facility_strings[] = {
  1478. [FSCR_FP_LG] = "FPU",
  1479. [FSCR_VECVSX_LG] = "VMX/VSX",
  1480. [FSCR_DSCR_LG] = "DSCR",
  1481. [FSCR_PM_LG] = "PMU SPRs",
  1482. [FSCR_BHRB_LG] = "BHRB",
  1483. [FSCR_TM_LG] = "TM",
  1484. [FSCR_EBB_LG] = "EBB",
  1485. [FSCR_TAR_LG] = "TAR",
  1486. [FSCR_MSGP_LG] = "MSGP",
  1487. [FSCR_SCV_LG] = "SCV",
  1488. [FSCR_PREFIX_LG] = "PREFIX",
  1489. };
  1490. char *facility = "unknown";
  1491. u64 value;
  1492. u32 instword, rd;
  1493. u8 status;
  1494. bool hv;
  1495. hv = (TRAP(regs) == INTERRUPT_H_FAC_UNAVAIL);
  1496. if (hv)
  1497. value = mfspr(SPRN_HFSCR);
  1498. else
  1499. value = mfspr(SPRN_FSCR);
  1500. status = value >> 56;
  1501. if ((hv || status >= 2) &&
  1502. (status < ARRAY_SIZE(facility_strings)) &&
  1503. facility_strings[status])
  1504. facility = facility_strings[status];
  1505. /* We should not have taken this interrupt in kernel */
  1506. if (!user_mode(regs)) {
  1507. pr_emerg("Facility '%s' unavailable (%d) exception in kernel mode at %lx\n",
  1508. facility, status, regs->nip);
  1509. die("Unexpected facility unavailable exception", regs, SIGABRT);
  1510. }
  1511. interrupt_cond_local_irq_enable(regs);
  1512. if (status == FSCR_DSCR_LG) {
  1513. /*
  1514. * User is accessing the DSCR register using the problem
  1515. * state only SPR number (0x03) either through a mfspr or
  1516. * a mtspr instruction. If it is a write attempt through
  1517. * a mtspr, then we set the inherit bit. This also allows
  1518. * the user to write or read the register directly in the
  1519. * future by setting via the FSCR DSCR bit. But in case it
  1520. * is a read DSCR attempt through a mfspr instruction, we
  1521. * just emulate the instruction instead. This code path will
  1522. * always emulate all the mfspr instructions till the user
  1523. * has attempted at least one mtspr instruction. This way it
  1524. * preserves the same behaviour when the user is accessing
  1525. * the DSCR through privilege level only SPR number (0x11)
  1526. * which is emulated through illegal instruction exception.
  1527. * We always leave HFSCR DSCR set.
  1528. */
  1529. if (get_user(instword, (u32 __user *)(regs->nip))) {
  1530. pr_err("Failed to fetch the user instruction\n");
  1531. return;
  1532. }
  1533. /* Write into DSCR (mtspr 0x03, RS) */
  1534. if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
  1535. == PPC_INST_MTSPR_DSCR_USER) {
  1536. rd = (instword >> 21) & 0x1f;
  1537. current->thread.dscr = regs->gpr[rd];
  1538. current->thread.dscr_inherit = 1;
  1539. current->thread.fscr |= FSCR_DSCR;
  1540. mtspr(SPRN_FSCR, current->thread.fscr);
  1541. }
  1542. /* Read from DSCR (mfspr RT, 0x03) */
  1543. if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
  1544. == PPC_INST_MFSPR_DSCR_USER) {
  1545. if (emulate_instruction(regs)) {
  1546. pr_err("DSCR based mfspr emulation failed\n");
  1547. return;
  1548. }
  1549. regs_add_return_ip(regs, 4);
  1550. emulate_single_step(regs);
  1551. }
  1552. return;
  1553. }
  1554. if (status == FSCR_TM_LG) {
  1555. /*
  1556. * If we're here then the hardware is TM aware because it
  1557. * generated an exception with FSRM_TM set.
  1558. *
  1559. * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
  1560. * told us not to do TM, or the kernel is not built with TM
  1561. * support.
  1562. *
  1563. * If both of those things are true, then userspace can spam the
  1564. * console by triggering the printk() below just by continually
  1565. * doing tbegin (or any TM instruction). So in that case just
  1566. * send the process a SIGILL immediately.
  1567. */
  1568. if (!cpu_has_feature(CPU_FTR_TM))
  1569. goto out;
  1570. tm_unavailable(regs);
  1571. return;
  1572. }
  1573. pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
  1574. hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
  1575. out:
  1576. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1577. }
  1578. #endif
  1579. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1580. DEFINE_INTERRUPT_HANDLER(fp_unavailable_tm)
  1581. {
  1582. /* Note: This does not handle any kind of FP laziness. */
  1583. TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
  1584. regs->nip, regs->msr);
  1585. /* We can only have got here if the task started using FP after
  1586. * beginning the transaction. So, the transactional regs are just a
  1587. * copy of the checkpointed ones. But, we still need to recheckpoint
  1588. * as we're enabling FP for the process; it will return, abort the
  1589. * transaction, and probably retry but now with FP enabled. So the
  1590. * checkpointed FP registers need to be loaded.
  1591. */
  1592. tm_reclaim_current(TM_CAUSE_FAC_UNAV);
  1593. /*
  1594. * Reclaim initially saved out bogus (lazy) FPRs to ckfp_state, and
  1595. * then it was overwrite by the thr->fp_state by tm_reclaim_thread().
  1596. *
  1597. * At this point, ck{fp,vr}_state contains the exact values we want to
  1598. * recheckpoint.
  1599. */
  1600. /* Enable FP for the task: */
  1601. current->thread.load_fp = 1;
  1602. /*
  1603. * Recheckpoint all the checkpointed ckpt, ck{fp, vr}_state registers.
  1604. */
  1605. tm_recheckpoint(&current->thread);
  1606. }
  1607. DEFINE_INTERRUPT_HANDLER(altivec_unavailable_tm)
  1608. {
  1609. /* See the comments in fp_unavailable_tm(). This function operates
  1610. * the same way.
  1611. */
  1612. TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
  1613. "MSR=%lx\n",
  1614. regs->nip, regs->msr);
  1615. tm_reclaim_current(TM_CAUSE_FAC_UNAV);
  1616. current->thread.load_vec = 1;
  1617. tm_recheckpoint(&current->thread);
  1618. current->thread.used_vr = 1;
  1619. }
  1620. DEFINE_INTERRUPT_HANDLER(vsx_unavailable_tm)
  1621. {
  1622. /* See the comments in fp_unavailable_tm(). This works similarly,
  1623. * though we're loading both FP and VEC registers in here.
  1624. *
  1625. * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
  1626. * regs. Either way, set MSR_VSX.
  1627. */
  1628. TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
  1629. "MSR=%lx\n",
  1630. regs->nip, regs->msr);
  1631. current->thread.used_vsr = 1;
  1632. /* This reclaims FP and/or VR regs if they're already enabled */
  1633. tm_reclaim_current(TM_CAUSE_FAC_UNAV);
  1634. current->thread.load_vec = 1;
  1635. current->thread.load_fp = 1;
  1636. tm_recheckpoint(&current->thread);
  1637. }
  1638. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1639. #ifdef CONFIG_PPC64
  1640. DECLARE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi);
  1641. DEFINE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi)
  1642. {
  1643. __this_cpu_inc(irq_stat.pmu_irqs);
  1644. perf_irq(regs);
  1645. return 0;
  1646. }
  1647. #endif
  1648. DECLARE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async);
  1649. DEFINE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async)
  1650. {
  1651. __this_cpu_inc(irq_stat.pmu_irqs);
  1652. perf_irq(regs);
  1653. }
  1654. DEFINE_INTERRUPT_HANDLER_RAW(performance_monitor_exception)
  1655. {
  1656. /*
  1657. * On 64-bit, if perf interrupts hit in a local_irq_disable
  1658. * (soft-masked) region, we consider them as NMIs. This is required to
  1659. * prevent hash faults on user addresses when reading callchains (and
  1660. * looks better from an irq tracing perspective).
  1661. */
  1662. if (IS_ENABLED(CONFIG_PPC64) && unlikely(arch_irq_disabled_regs(regs)))
  1663. performance_monitor_exception_nmi(regs);
  1664. else
  1665. performance_monitor_exception_async(regs);
  1666. return 0;
  1667. }
  1668. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1669. static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
  1670. {
  1671. int changed = 0;
  1672. /*
  1673. * Determine the cause of the debug event, clear the
  1674. * event flags and send a trap to the handler. Torez
  1675. */
  1676. if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
  1677. dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  1678. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1679. current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
  1680. #endif
  1681. do_send_trap(regs, mfspr(SPRN_DAC1), debug_status,
  1682. 5);
  1683. changed |= 0x01;
  1684. } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
  1685. dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  1686. do_send_trap(regs, mfspr(SPRN_DAC2), debug_status,
  1687. 6);
  1688. changed |= 0x01;
  1689. } else if (debug_status & DBSR_IAC1) {
  1690. current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
  1691. dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
  1692. do_send_trap(regs, mfspr(SPRN_IAC1), debug_status,
  1693. 1);
  1694. changed |= 0x01;
  1695. } else if (debug_status & DBSR_IAC2) {
  1696. current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
  1697. do_send_trap(regs, mfspr(SPRN_IAC2), debug_status,
  1698. 2);
  1699. changed |= 0x01;
  1700. } else if (debug_status & DBSR_IAC3) {
  1701. current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
  1702. dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
  1703. do_send_trap(regs, mfspr(SPRN_IAC3), debug_status,
  1704. 3);
  1705. changed |= 0x01;
  1706. } else if (debug_status & DBSR_IAC4) {
  1707. current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
  1708. do_send_trap(regs, mfspr(SPRN_IAC4), debug_status,
  1709. 4);
  1710. changed |= 0x01;
  1711. }
  1712. /*
  1713. * At the point this routine was called, the MSR(DE) was turned off.
  1714. * Check all other debug flags and see if that bit needs to be turned
  1715. * back on or not.
  1716. */
  1717. if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
  1718. current->thread.debug.dbcr1))
  1719. regs_set_return_msr(regs, regs->msr | MSR_DE);
  1720. else
  1721. /* Make sure the IDM flag is off */
  1722. current->thread.debug.dbcr0 &= ~DBCR0_IDM;
  1723. if (changed & 0x01)
  1724. mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
  1725. }
  1726. DEFINE_INTERRUPT_HANDLER(DebugException)
  1727. {
  1728. unsigned long debug_status = regs->dsisr;
  1729. current->thread.debug.dbsr = debug_status;
  1730. /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
  1731. * on server, it stops on the target of the branch. In order to simulate
  1732. * the server behaviour, we thus restart right away with a single step
  1733. * instead of stopping here when hitting a BT
  1734. */
  1735. if (debug_status & DBSR_BT) {
  1736. regs_set_return_msr(regs, regs->msr & ~MSR_DE);
  1737. /* Disable BT */
  1738. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
  1739. /* Clear the BT event */
  1740. mtspr(SPRN_DBSR, DBSR_BT);
  1741. /* Do the single step trick only when coming from userspace */
  1742. if (user_mode(regs)) {
  1743. current->thread.debug.dbcr0 &= ~DBCR0_BT;
  1744. current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  1745. regs_set_return_msr(regs, regs->msr | MSR_DE);
  1746. return;
  1747. }
  1748. if (kprobe_post_handler(regs))
  1749. return;
  1750. if (notify_die(DIE_SSTEP, "block_step", regs, 5,
  1751. 5, SIGTRAP) == NOTIFY_STOP) {
  1752. return;
  1753. }
  1754. if (debugger_sstep(regs))
  1755. return;
  1756. } else if (debug_status & DBSR_IC) { /* Instruction complete */
  1757. regs_set_return_msr(regs, regs->msr & ~MSR_DE);
  1758. /* Disable instruction completion */
  1759. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  1760. /* Clear the instruction completion event */
  1761. mtspr(SPRN_DBSR, DBSR_IC);
  1762. if (kprobe_post_handler(regs))
  1763. return;
  1764. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  1765. 5, SIGTRAP) == NOTIFY_STOP) {
  1766. return;
  1767. }
  1768. if (debugger_sstep(regs))
  1769. return;
  1770. if (user_mode(regs)) {
  1771. current->thread.debug.dbcr0 &= ~DBCR0_IC;
  1772. if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
  1773. current->thread.debug.dbcr1))
  1774. regs_set_return_msr(regs, regs->msr | MSR_DE);
  1775. else
  1776. /* Make sure the IDM bit is off */
  1777. current->thread.debug.dbcr0 &= ~DBCR0_IDM;
  1778. }
  1779. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  1780. } else
  1781. handle_debug(regs, debug_status);
  1782. }
  1783. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  1784. #ifdef CONFIG_ALTIVEC
  1785. DEFINE_INTERRUPT_HANDLER(altivec_assist_exception)
  1786. {
  1787. int err;
  1788. if (!user_mode(regs)) {
  1789. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  1790. " at %lx\n", regs->nip);
  1791. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  1792. }
  1793. flush_altivec_to_thread(current);
  1794. PPC_WARN_EMULATED(altivec, regs);
  1795. err = emulate_altivec(regs);
  1796. if (err == 0) {
  1797. regs_add_return_ip(regs, 4); /* skip emulated instruction */
  1798. emulate_single_step(regs);
  1799. return;
  1800. }
  1801. if (err == -EFAULT) {
  1802. /* got an error reading the instruction */
  1803. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1804. } else {
  1805. /* didn't recognize the instruction */
  1806. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  1807. printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
  1808. "in %s at %lx\n", current->comm, regs->nip);
  1809. current->thread.vr_state.vscr.u[3] |= 0x10000;
  1810. }
  1811. }
  1812. #endif /* CONFIG_ALTIVEC */
  1813. #ifdef CONFIG_PPC_85xx
  1814. DEFINE_INTERRUPT_HANDLER(CacheLockingException)
  1815. {
  1816. unsigned long error_code = regs->dsisr;
  1817. /* We treat cache locking instructions from the user
  1818. * as priv ops, in the future we could try to do
  1819. * something smarter
  1820. */
  1821. if (error_code & (ESR_DLK|ESR_ILK))
  1822. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1823. return;
  1824. }
  1825. #endif /* CONFIG_PPC_85xx */
  1826. #ifdef CONFIG_SPE
  1827. DEFINE_INTERRUPT_HANDLER(SPEFloatingPointException)
  1828. {
  1829. unsigned long spefscr;
  1830. int fpexc_mode;
  1831. int code = FPE_FLTUNK;
  1832. int err;
  1833. interrupt_cond_local_irq_enable(regs);
  1834. flush_spe_to_thread(current);
  1835. spefscr = current->thread.spefscr;
  1836. fpexc_mode = current->thread.fpexc_mode;
  1837. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  1838. code = FPE_FLTOVF;
  1839. }
  1840. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1841. code = FPE_FLTUND;
  1842. }
  1843. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1844. code = FPE_FLTDIV;
  1845. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1846. code = FPE_FLTINV;
  1847. }
  1848. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1849. code = FPE_FLTRES;
  1850. err = do_spe_mathemu(regs);
  1851. if (err == 0) {
  1852. regs_add_return_ip(regs, 4); /* skip emulated instruction */
  1853. emulate_single_step(regs);
  1854. return;
  1855. }
  1856. if (err == -EFAULT) {
  1857. /* got an error reading the instruction */
  1858. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1859. } else if (err == -EINVAL) {
  1860. /* didn't recognize the instruction */
  1861. printk(KERN_ERR "unrecognized spe instruction "
  1862. "in %s at %lx\n", current->comm, regs->nip);
  1863. } else {
  1864. _exception(SIGFPE, regs, code, regs->nip);
  1865. }
  1866. return;
  1867. }
  1868. DEFINE_INTERRUPT_HANDLER(SPEFloatingPointRoundException)
  1869. {
  1870. int err;
  1871. interrupt_cond_local_irq_enable(regs);
  1872. preempt_disable();
  1873. if (regs->msr & MSR_SPE)
  1874. giveup_spe(current);
  1875. preempt_enable();
  1876. regs_add_return_ip(regs, -4);
  1877. err = speround_handler(regs);
  1878. if (err == 0) {
  1879. regs_add_return_ip(regs, 4); /* skip emulated instruction */
  1880. emulate_single_step(regs);
  1881. return;
  1882. }
  1883. if (err == -EFAULT) {
  1884. /* got an error reading the instruction */
  1885. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1886. } else if (err == -EINVAL) {
  1887. /* didn't recognize the instruction */
  1888. printk(KERN_ERR "unrecognized spe instruction "
  1889. "in %s at %lx\n", current->comm, regs->nip);
  1890. } else {
  1891. _exception(SIGFPE, regs, FPE_FLTUNK, regs->nip);
  1892. return;
  1893. }
  1894. }
  1895. #endif
  1896. /*
  1897. * We enter here if we get an unrecoverable exception, that is, one
  1898. * that happened at a point where the RI (recoverable interrupt) bit
  1899. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1900. * we therefore lost state by taking this exception.
  1901. */
  1902. void __noreturn unrecoverable_exception(struct pt_regs *regs)
  1903. {
  1904. pr_emerg("Unrecoverable exception %lx at %lx (msr=%lx)\n",
  1905. regs->trap, regs->nip, regs->msr);
  1906. die("Unrecoverable exception", regs, SIGABRT);
  1907. /* die() should not return */
  1908. for (;;)
  1909. ;
  1910. }
  1911. #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
  1912. /*
  1913. * Default handler for a Watchdog exception,
  1914. * spins until a reboot occurs
  1915. */
  1916. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1917. {
  1918. /* Generic WatchdogHandler, implement your own */
  1919. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1920. return;
  1921. }
  1922. DEFINE_INTERRUPT_HANDLER_NMI(WatchdogException)
  1923. {
  1924. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1925. WatchdogHandler(regs);
  1926. return 0;
  1927. }
  1928. #endif
  1929. /*
  1930. * We enter here if we discover during exception entry that we are
  1931. * running in supervisor mode with a userspace value in the stack pointer.
  1932. */
  1933. DEFINE_INTERRUPT_HANDLER(kernel_bad_stack)
  1934. {
  1935. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1936. regs->gpr[1], regs->nip);
  1937. die("Bad kernel stack pointer", regs, SIGABRT);
  1938. }
  1939. #ifdef CONFIG_PPC_EMULATED_STATS
  1940. #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
  1941. struct ppc_emulated ppc_emulated = {
  1942. #ifdef CONFIG_ALTIVEC
  1943. WARN_EMULATED_SETUP(altivec),
  1944. #endif
  1945. WARN_EMULATED_SETUP(dcba),
  1946. WARN_EMULATED_SETUP(dcbz),
  1947. WARN_EMULATED_SETUP(fp_pair),
  1948. WARN_EMULATED_SETUP(isel),
  1949. WARN_EMULATED_SETUP(mcrxr),
  1950. WARN_EMULATED_SETUP(mfpvr),
  1951. WARN_EMULATED_SETUP(multiple),
  1952. WARN_EMULATED_SETUP(popcntb),
  1953. WARN_EMULATED_SETUP(spe),
  1954. WARN_EMULATED_SETUP(string),
  1955. WARN_EMULATED_SETUP(sync),
  1956. WARN_EMULATED_SETUP(unaligned),
  1957. #ifdef CONFIG_MATH_EMULATION
  1958. WARN_EMULATED_SETUP(math),
  1959. #endif
  1960. #ifdef CONFIG_VSX
  1961. WARN_EMULATED_SETUP(vsx),
  1962. #endif
  1963. #ifdef CONFIG_PPC64
  1964. WARN_EMULATED_SETUP(mfdscr),
  1965. WARN_EMULATED_SETUP(mtdscr),
  1966. WARN_EMULATED_SETUP(lq_stq),
  1967. WARN_EMULATED_SETUP(lxvw4x),
  1968. WARN_EMULATED_SETUP(lxvh8x),
  1969. WARN_EMULATED_SETUP(lxvd2x),
  1970. WARN_EMULATED_SETUP(lxvb16x),
  1971. #endif
  1972. };
  1973. u32 ppc_warn_emulated;
  1974. void ppc_warn_emulated_print(const char *type)
  1975. {
  1976. pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
  1977. type);
  1978. }
  1979. static int __init ppc_warn_emulated_init(void)
  1980. {
  1981. struct dentry *dir;
  1982. unsigned int i;
  1983. struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
  1984. dir = debugfs_create_dir("emulated_instructions",
  1985. arch_debugfs_dir);
  1986. debugfs_create_u32("do_warn", 0644, dir, &ppc_warn_emulated);
  1987. for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++)
  1988. debugfs_create_u32(entries[i].name, 0644, dir,
  1989. (u32 *)&entries[i].val.counter);
  1990. return 0;
  1991. }
  1992. device_initcall(ppc_warn_emulated_init);
  1993. #endif /* CONFIG_PPC_EMULATED_STATS */