traps.c 40 KB

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