traps.c 56 KB

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