traps.c 40 KB

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