traps.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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_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_MASK) == PPC_INST_MFSPR_DSCR) &&
  825. cpu_has_feature(CPU_FTR_DSCR)) {
  826. PPC_WARN_EMULATED(mfdscr, regs);
  827. rd = (instword >> 21) & 0x1f;
  828. regs->gpr[rd] = mfspr(SPRN_DSCR);
  829. return 0;
  830. }
  831. /* Emulate the mtspr DSCR, rD. */
  832. if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
  833. cpu_has_feature(CPU_FTR_DSCR)) {
  834. PPC_WARN_EMULATED(mtdscr, regs);
  835. rd = (instword >> 21) & 0x1f;
  836. mtspr(SPRN_DSCR, regs->gpr[rd]);
  837. current->thread.dscr_inherit = 1;
  838. return 0;
  839. }
  840. #endif
  841. return -EINVAL;
  842. }
  843. int is_valid_bugaddr(unsigned long addr)
  844. {
  845. return is_kernel_addr(addr);
  846. }
  847. void __kprobes program_check_exception(struct pt_regs *regs)
  848. {
  849. unsigned int reason = get_reason(regs);
  850. extern int do_mathemu(struct pt_regs *regs);
  851. /* We can now get here via a FP Unavailable exception if the core
  852. * has no FPU, in that case the reason flags will be 0 */
  853. if (reason & REASON_FP) {
  854. /* IEEE FP exception */
  855. parse_fpe(regs);
  856. return;
  857. }
  858. if (reason & REASON_TRAP) {
  859. /* Debugger is first in line to stop recursive faults in
  860. * rcu_lock, notify_die, or atomic_notifier_call_chain */
  861. if (debugger_bpt(regs))
  862. return;
  863. /* trap exception */
  864. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  865. == NOTIFY_STOP)
  866. return;
  867. if (!(regs->msr & MSR_PR) && /* not user-mode */
  868. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  869. regs->nip += 4;
  870. return;
  871. }
  872. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  873. return;
  874. }
  875. local_irq_enable();
  876. #ifdef CONFIG_MATH_EMULATION
  877. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  878. * but there seems to be a hardware bug on the 405GP (RevD)
  879. * that means ESR is sometimes set incorrectly - either to
  880. * ESR_DST (!?) or 0. In the process of chasing this with the
  881. * hardware people - not sure if it can happen on any illegal
  882. * instruction or only on FP instructions, whether there is a
  883. * pattern to occurrences etc. -dgibson 31/Mar/2003 */
  884. switch (do_mathemu(regs)) {
  885. case 0:
  886. emulate_single_step(regs);
  887. return;
  888. case 1: {
  889. int code = 0;
  890. code = __parse_fpscr(current->thread.fpscr.val);
  891. _exception(SIGFPE, regs, code, regs->nip);
  892. return;
  893. }
  894. case -EFAULT:
  895. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  896. return;
  897. }
  898. /* fall through on any other errors */
  899. #endif /* CONFIG_MATH_EMULATION */
  900. /* Try to emulate it if we should. */
  901. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  902. switch (emulate_instruction(regs)) {
  903. case 0:
  904. regs->nip += 4;
  905. emulate_single_step(regs);
  906. return;
  907. case -EFAULT:
  908. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  909. return;
  910. }
  911. }
  912. if (reason & REASON_PRIVILEGED)
  913. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  914. else
  915. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  916. }
  917. void alignment_exception(struct pt_regs *regs)
  918. {
  919. int sig, code, fixed = 0;
  920. /* we don't implement logging of alignment exceptions */
  921. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  922. fixed = fix_alignment(regs);
  923. if (fixed == 1) {
  924. regs->nip += 4; /* skip over emulated instruction */
  925. emulate_single_step(regs);
  926. return;
  927. }
  928. /* Operand address was bad */
  929. if (fixed == -EFAULT) {
  930. sig = SIGSEGV;
  931. code = SEGV_ACCERR;
  932. } else {
  933. sig = SIGBUS;
  934. code = BUS_ADRALN;
  935. }
  936. if (user_mode(regs))
  937. _exception(sig, regs, code, regs->dar);
  938. else
  939. bad_page_fault(regs, regs->dar, sig);
  940. }
  941. void StackOverflow(struct pt_regs *regs)
  942. {
  943. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  944. current, regs->gpr[1]);
  945. debugger(regs);
  946. show_regs(regs);
  947. panic("kernel stack overflow");
  948. }
  949. void nonrecoverable_exception(struct pt_regs *regs)
  950. {
  951. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  952. regs->nip, regs->msr);
  953. debugger(regs);
  954. die("nonrecoverable exception", regs, SIGKILL);
  955. }
  956. void trace_syscall(struct pt_regs *regs)
  957. {
  958. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  959. current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
  960. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  961. }
  962. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  963. {
  964. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  965. "%lx at %lx\n", regs->trap, regs->nip);
  966. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  967. }
  968. void altivec_unavailable_exception(struct pt_regs *regs)
  969. {
  970. if (user_mode(regs)) {
  971. /* A user program has executed an altivec instruction,
  972. but this kernel doesn't support altivec. */
  973. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  974. return;
  975. }
  976. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  977. "%lx at %lx\n", regs->trap, regs->nip);
  978. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  979. }
  980. void vsx_unavailable_exception(struct pt_regs *regs)
  981. {
  982. if (user_mode(regs)) {
  983. /* A user program has executed an vsx instruction,
  984. but this kernel doesn't support vsx. */
  985. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  986. return;
  987. }
  988. printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
  989. "%lx at %lx\n", regs->trap, regs->nip);
  990. die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
  991. }
  992. void performance_monitor_exception(struct pt_regs *regs)
  993. {
  994. __get_cpu_var(irq_stat).pmu_irqs++;
  995. perf_irq(regs);
  996. }
  997. #ifdef CONFIG_8xx
  998. void SoftwareEmulation(struct pt_regs *regs)
  999. {
  1000. extern int do_mathemu(struct pt_regs *);
  1001. extern int Soft_emulate_8xx(struct pt_regs *);
  1002. #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
  1003. int errcode;
  1004. #endif
  1005. CHECK_FULL_REGS(regs);
  1006. if (!user_mode(regs)) {
  1007. debugger(regs);
  1008. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  1009. }
  1010. #ifdef CONFIG_MATH_EMULATION
  1011. errcode = do_mathemu(regs);
  1012. if (errcode >= 0)
  1013. PPC_WARN_EMULATED(math, regs);
  1014. switch (errcode) {
  1015. case 0:
  1016. emulate_single_step(regs);
  1017. return;
  1018. case 1: {
  1019. int code = 0;
  1020. code = __parse_fpscr(current->thread.fpscr.val);
  1021. _exception(SIGFPE, regs, code, regs->nip);
  1022. return;
  1023. }
  1024. case -EFAULT:
  1025. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  1026. return;
  1027. default:
  1028. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1029. return;
  1030. }
  1031. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  1032. errcode = Soft_emulate_8xx(regs);
  1033. if (errcode >= 0)
  1034. PPC_WARN_EMULATED(8xx, regs);
  1035. switch (errcode) {
  1036. case 0:
  1037. emulate_single_step(regs);
  1038. return;
  1039. case 1:
  1040. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1041. return;
  1042. case -EFAULT:
  1043. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  1044. return;
  1045. }
  1046. #else
  1047. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1048. #endif
  1049. }
  1050. #endif /* CONFIG_8xx */
  1051. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1052. static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
  1053. {
  1054. int changed = 0;
  1055. /*
  1056. * Determine the cause of the debug event, clear the
  1057. * event flags and send a trap to the handler. Torez
  1058. */
  1059. if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
  1060. dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  1061. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1062. current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
  1063. #endif
  1064. do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
  1065. 5);
  1066. changed |= 0x01;
  1067. } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
  1068. dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  1069. do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
  1070. 6);
  1071. changed |= 0x01;
  1072. } else if (debug_status & DBSR_IAC1) {
  1073. current->thread.dbcr0 &= ~DBCR0_IAC1;
  1074. dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
  1075. do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
  1076. 1);
  1077. changed |= 0x01;
  1078. } else if (debug_status & DBSR_IAC2) {
  1079. current->thread.dbcr0 &= ~DBCR0_IAC2;
  1080. do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
  1081. 2);
  1082. changed |= 0x01;
  1083. } else if (debug_status & DBSR_IAC3) {
  1084. current->thread.dbcr0 &= ~DBCR0_IAC3;
  1085. dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
  1086. do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
  1087. 3);
  1088. changed |= 0x01;
  1089. } else if (debug_status & DBSR_IAC4) {
  1090. current->thread.dbcr0 &= ~DBCR0_IAC4;
  1091. do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
  1092. 4);
  1093. changed |= 0x01;
  1094. }
  1095. /*
  1096. * At the point this routine was called, the MSR(DE) was turned off.
  1097. * Check all other debug flags and see if that bit needs to be turned
  1098. * back on or not.
  1099. */
  1100. if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
  1101. regs->msr |= MSR_DE;
  1102. else
  1103. /* Make sure the IDM flag is off */
  1104. current->thread.dbcr0 &= ~DBCR0_IDM;
  1105. if (changed & 0x01)
  1106. mtspr(SPRN_DBCR0, current->thread.dbcr0);
  1107. }
  1108. void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
  1109. {
  1110. current->thread.dbsr = debug_status;
  1111. /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
  1112. * on server, it stops on the target of the branch. In order to simulate
  1113. * the server behaviour, we thus restart right away with a single step
  1114. * instead of stopping here when hitting a BT
  1115. */
  1116. if (debug_status & DBSR_BT) {
  1117. regs->msr &= ~MSR_DE;
  1118. /* Disable BT */
  1119. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
  1120. /* Clear the BT event */
  1121. mtspr(SPRN_DBSR, DBSR_BT);
  1122. /* Do the single step trick only when coming from userspace */
  1123. if (user_mode(regs)) {
  1124. current->thread.dbcr0 &= ~DBCR0_BT;
  1125. current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  1126. regs->msr |= MSR_DE;
  1127. return;
  1128. }
  1129. if (notify_die(DIE_SSTEP, "block_step", regs, 5,
  1130. 5, SIGTRAP) == NOTIFY_STOP) {
  1131. return;
  1132. }
  1133. if (debugger_sstep(regs))
  1134. return;
  1135. } else if (debug_status & DBSR_IC) { /* Instruction complete */
  1136. regs->msr &= ~MSR_DE;
  1137. /* Disable instruction completion */
  1138. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  1139. /* Clear the instruction completion event */
  1140. mtspr(SPRN_DBSR, DBSR_IC);
  1141. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  1142. 5, SIGTRAP) == NOTIFY_STOP) {
  1143. return;
  1144. }
  1145. if (debugger_sstep(regs))
  1146. return;
  1147. if (user_mode(regs)) {
  1148. current->thread.dbcr0 &= ~DBCR0_IC;
  1149. if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
  1150. current->thread.dbcr1))
  1151. regs->msr |= MSR_DE;
  1152. else
  1153. /* Make sure the IDM bit is off */
  1154. current->thread.dbcr0 &= ~DBCR0_IDM;
  1155. }
  1156. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  1157. } else
  1158. handle_debug(regs, debug_status);
  1159. }
  1160. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  1161. #if !defined(CONFIG_TAU_INT)
  1162. void TAUException(struct pt_regs *regs)
  1163. {
  1164. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  1165. regs->nip, regs->msr, regs->trap, print_tainted());
  1166. }
  1167. #endif /* CONFIG_INT_TAU */
  1168. #ifdef CONFIG_ALTIVEC
  1169. void altivec_assist_exception(struct pt_regs *regs)
  1170. {
  1171. int err;
  1172. if (!user_mode(regs)) {
  1173. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  1174. " at %lx\n", regs->nip);
  1175. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  1176. }
  1177. flush_altivec_to_thread(current);
  1178. PPC_WARN_EMULATED(altivec, regs);
  1179. err = emulate_altivec(regs);
  1180. if (err == 0) {
  1181. regs->nip += 4; /* skip emulated instruction */
  1182. emulate_single_step(regs);
  1183. return;
  1184. }
  1185. if (err == -EFAULT) {
  1186. /* got an error reading the instruction */
  1187. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1188. } else {
  1189. /* didn't recognize the instruction */
  1190. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  1191. printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
  1192. "in %s at %lx\n", current->comm, regs->nip);
  1193. current->thread.vscr.u[3] |= 0x10000;
  1194. }
  1195. }
  1196. #endif /* CONFIG_ALTIVEC */
  1197. #ifdef CONFIG_VSX
  1198. void vsx_assist_exception(struct pt_regs *regs)
  1199. {
  1200. if (!user_mode(regs)) {
  1201. printk(KERN_EMERG "VSX assist exception in kernel mode"
  1202. " at %lx\n", regs->nip);
  1203. die("Kernel VSX assist exception", regs, SIGILL);
  1204. }
  1205. flush_vsx_to_thread(current);
  1206. printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
  1207. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1208. }
  1209. #endif /* CONFIG_VSX */
  1210. #ifdef CONFIG_FSL_BOOKE
  1211. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  1212. unsigned long error_code)
  1213. {
  1214. /* We treat cache locking instructions from the user
  1215. * as priv ops, in the future we could try to do
  1216. * something smarter
  1217. */
  1218. if (error_code & (ESR_DLK|ESR_ILK))
  1219. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1220. return;
  1221. }
  1222. #endif /* CONFIG_FSL_BOOKE */
  1223. #ifdef CONFIG_SPE
  1224. void SPEFloatingPointException(struct pt_regs *regs)
  1225. {
  1226. extern int do_spe_mathemu(struct pt_regs *regs);
  1227. unsigned long spefscr;
  1228. int fpexc_mode;
  1229. int code = 0;
  1230. int err;
  1231. flush_spe_to_thread(current);
  1232. spefscr = current->thread.spefscr;
  1233. fpexc_mode = current->thread.fpexc_mode;
  1234. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  1235. code = FPE_FLTOVF;
  1236. }
  1237. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1238. code = FPE_FLTUND;
  1239. }
  1240. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1241. code = FPE_FLTDIV;
  1242. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1243. code = FPE_FLTINV;
  1244. }
  1245. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1246. code = FPE_FLTRES;
  1247. err = do_spe_mathemu(regs);
  1248. if (err == 0) {
  1249. regs->nip += 4; /* skip emulated instruction */
  1250. emulate_single_step(regs);
  1251. return;
  1252. }
  1253. if (err == -EFAULT) {
  1254. /* got an error reading the instruction */
  1255. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1256. } else if (err == -EINVAL) {
  1257. /* didn't recognize the instruction */
  1258. printk(KERN_ERR "unrecognized spe instruction "
  1259. "in %s at %lx\n", current->comm, regs->nip);
  1260. } else {
  1261. _exception(SIGFPE, regs, code, regs->nip);
  1262. }
  1263. return;
  1264. }
  1265. void SPEFloatingPointRoundException(struct pt_regs *regs)
  1266. {
  1267. extern int speround_handler(struct pt_regs *regs);
  1268. int err;
  1269. preempt_disable();
  1270. if (regs->msr & MSR_SPE)
  1271. giveup_spe(current);
  1272. preempt_enable();
  1273. regs->nip -= 4;
  1274. err = speround_handler(regs);
  1275. if (err == 0) {
  1276. regs->nip += 4; /* skip emulated instruction */
  1277. emulate_single_step(regs);
  1278. return;
  1279. }
  1280. if (err == -EFAULT) {
  1281. /* got an error reading the instruction */
  1282. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1283. } else if (err == -EINVAL) {
  1284. /* didn't recognize the instruction */
  1285. printk(KERN_ERR "unrecognized spe instruction "
  1286. "in %s at %lx\n", current->comm, regs->nip);
  1287. } else {
  1288. _exception(SIGFPE, regs, 0, regs->nip);
  1289. return;
  1290. }
  1291. }
  1292. #endif
  1293. /*
  1294. * We enter here if we get an unrecoverable exception, that is, one
  1295. * that happened at a point where the RI (recoverable interrupt) bit
  1296. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1297. * we therefore lost state by taking this exception.
  1298. */
  1299. void unrecoverable_exception(struct pt_regs *regs)
  1300. {
  1301. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  1302. regs->trap, regs->nip);
  1303. die("Unrecoverable exception", regs, SIGABRT);
  1304. }
  1305. #ifdef CONFIG_BOOKE_WDT
  1306. /*
  1307. * Default handler for a Watchdog exception,
  1308. * spins until a reboot occurs
  1309. */
  1310. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1311. {
  1312. /* Generic WatchdogHandler, implement your own */
  1313. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1314. return;
  1315. }
  1316. void WatchdogException(struct pt_regs *regs)
  1317. {
  1318. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1319. WatchdogHandler(regs);
  1320. }
  1321. #endif
  1322. /*
  1323. * We enter here if we discover during exception entry that we are
  1324. * running in supervisor mode with a userspace value in the stack pointer.
  1325. */
  1326. void kernel_bad_stack(struct pt_regs *regs)
  1327. {
  1328. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1329. regs->gpr[1], regs->nip);
  1330. die("Bad kernel stack pointer", regs, SIGABRT);
  1331. }
  1332. void __init trap_init(void)
  1333. {
  1334. }
  1335. #ifdef CONFIG_PPC_EMULATED_STATS
  1336. #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
  1337. struct ppc_emulated ppc_emulated = {
  1338. #ifdef CONFIG_ALTIVEC
  1339. WARN_EMULATED_SETUP(altivec),
  1340. #endif
  1341. WARN_EMULATED_SETUP(dcba),
  1342. WARN_EMULATED_SETUP(dcbz),
  1343. WARN_EMULATED_SETUP(fp_pair),
  1344. WARN_EMULATED_SETUP(isel),
  1345. WARN_EMULATED_SETUP(mcrxr),
  1346. WARN_EMULATED_SETUP(mfpvr),
  1347. WARN_EMULATED_SETUP(multiple),
  1348. WARN_EMULATED_SETUP(popcntb),
  1349. WARN_EMULATED_SETUP(spe),
  1350. WARN_EMULATED_SETUP(string),
  1351. WARN_EMULATED_SETUP(unaligned),
  1352. #ifdef CONFIG_MATH_EMULATION
  1353. WARN_EMULATED_SETUP(math),
  1354. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  1355. WARN_EMULATED_SETUP(8xx),
  1356. #endif
  1357. #ifdef CONFIG_VSX
  1358. WARN_EMULATED_SETUP(vsx),
  1359. #endif
  1360. #ifdef CONFIG_PPC64
  1361. WARN_EMULATED_SETUP(mfdscr),
  1362. WARN_EMULATED_SETUP(mtdscr),
  1363. #endif
  1364. };
  1365. u32 ppc_warn_emulated;
  1366. void ppc_warn_emulated_print(const char *type)
  1367. {
  1368. pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
  1369. type);
  1370. }
  1371. static int __init ppc_warn_emulated_init(void)
  1372. {
  1373. struct dentry *dir, *d;
  1374. unsigned int i;
  1375. struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
  1376. if (!powerpc_debugfs_root)
  1377. return -ENODEV;
  1378. dir = debugfs_create_dir("emulated_instructions",
  1379. powerpc_debugfs_root);
  1380. if (!dir)
  1381. return -ENOMEM;
  1382. d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
  1383. &ppc_warn_emulated);
  1384. if (!d)
  1385. goto fail;
  1386. for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
  1387. d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
  1388. (u32 *)&entries[i].val.counter);
  1389. if (!d)
  1390. goto fail;
  1391. }
  1392. return 0;
  1393. fail:
  1394. debugfs_remove_recursive(dir);
  1395. return -ENOMEM;
  1396. }
  1397. device_initcall(ppc_warn_emulated_init);
  1398. #endif /* CONFIG_PPC_EMULATED_STATS */