traps.c 40 KB

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