traps.c 55 KB

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