traps.c 41 KB

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