traps.c 50 KB

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