traps.c 40 KB

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