traps.c 45 KB

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