traps.c 41 KB

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