traps.c 49 KB

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