traps.c 40 KB

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