traps.c 56 KB

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