traps.c 52 KB

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