traps.c 40 KB

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