traps.c 56 KB

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