traps.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  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/extable.h>
  27. #include <linux/module.h> /* print_modules */
  28. #include <linux/prctl.h>
  29. #include <linux/delay.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kexec.h>
  32. #include <linux/backlight.h>
  33. #include <linux/bug.h>
  34. #include <linux/kdebug.h>
  35. #include <linux/debugfs.h>
  36. #include <linux/ratelimit.h>
  37. #include <linux/context_tracking.h>
  38. #include <asm/emulated_ops.h>
  39. #include <asm/pgtable.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/io.h>
  42. #include <asm/machdep.h>
  43. #include <asm/rtas.h>
  44. #include <asm/pmc.h>
  45. #include <asm/reg.h>
  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. #include <asm/tm.h>
  53. #endif
  54. #include <asm/kexec.h>
  55. #include <asm/ppc-opcode.h>
  56. #include <asm/rio.h>
  57. #include <asm/fadump.h>
  58. #include <asm/switch_to.h>
  59. #include <asm/tm.h>
  60. #include <asm/debug.h>
  61. #include <asm/asm-prototypes.h>
  62. #include <asm/hmi.h>
  63. #include <sysdev/fsl_pci.h>
  64. #include <asm/kprobes.h>
  65. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
  66. int (*__debugger)(struct pt_regs *regs) __read_mostly;
  67. int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
  68. int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
  69. int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
  70. int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
  71. int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
  72. int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
  73. EXPORT_SYMBOL(__debugger);
  74. EXPORT_SYMBOL(__debugger_ipi);
  75. EXPORT_SYMBOL(__debugger_bpt);
  76. EXPORT_SYMBOL(__debugger_sstep);
  77. EXPORT_SYMBOL(__debugger_iabr_match);
  78. EXPORT_SYMBOL(__debugger_break_match);
  79. EXPORT_SYMBOL(__debugger_fault_handler);
  80. #endif
  81. /* Transactional Memory trap debug */
  82. #ifdef TM_DEBUG_SW
  83. #define TM_DEBUG(x...) printk(KERN_INFO x)
  84. #else
  85. #define TM_DEBUG(x...) do { } while(0)
  86. #endif
  87. /*
  88. * Trap & Exception support
  89. */
  90. #ifdef CONFIG_PMAC_BACKLIGHT
  91. static void pmac_backlight_unblank(void)
  92. {
  93. mutex_lock(&pmac_backlight_mutex);
  94. if (pmac_backlight) {
  95. struct backlight_properties *props;
  96. props = &pmac_backlight->props;
  97. props->brightness = props->max_brightness;
  98. props->power = FB_BLANK_UNBLANK;
  99. backlight_update_status(pmac_backlight);
  100. }
  101. mutex_unlock(&pmac_backlight_mutex);
  102. }
  103. #else
  104. static inline void pmac_backlight_unblank(void) { }
  105. #endif
  106. static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  107. static int die_owner = -1;
  108. static unsigned int die_nest_count;
  109. static int die_counter;
  110. static unsigned long oops_begin(struct pt_regs *regs)
  111. {
  112. int cpu;
  113. unsigned long flags;
  114. oops_enter();
  115. /* racy, but better than risking deadlock. */
  116. raw_local_irq_save(flags);
  117. cpu = smp_processor_id();
  118. if (!arch_spin_trylock(&die_lock)) {
  119. if (cpu == die_owner)
  120. /* nested oops. should stop eventually */;
  121. else
  122. arch_spin_lock(&die_lock);
  123. }
  124. die_nest_count++;
  125. die_owner = cpu;
  126. console_verbose();
  127. bust_spinlocks(1);
  128. if (machine_is(powermac))
  129. pmac_backlight_unblank();
  130. return flags;
  131. }
  132. NOKPROBE_SYMBOL(oops_begin);
  133. static void oops_end(unsigned long flags, struct pt_regs *regs,
  134. int signr)
  135. {
  136. bust_spinlocks(0);
  137. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  138. die_nest_count--;
  139. oops_exit();
  140. printk("\n");
  141. if (!die_nest_count) {
  142. /* Nest count reaches zero, release the lock. */
  143. die_owner = -1;
  144. arch_spin_unlock(&die_lock);
  145. }
  146. raw_local_irq_restore(flags);
  147. crash_fadump(regs, "die oops");
  148. /*
  149. * A system reset (0x100) is a request to dump, so we always send
  150. * it through the crashdump code.
  151. */
  152. if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
  153. crash_kexec(regs);
  154. /*
  155. * We aren't the primary crash CPU. We need to send it
  156. * to a holding pattern to avoid it ending up in the panic
  157. * code.
  158. */
  159. crash_kexec_secondary(regs);
  160. }
  161. if (!signr)
  162. return;
  163. /*
  164. * While our oops output is serialised by a spinlock, output
  165. * from panic() called below can race and corrupt it. If we
  166. * know we are going to panic, delay for 1 second so we have a
  167. * chance to get clean backtraces from all CPUs that are oopsing.
  168. */
  169. if (in_interrupt() || panic_on_oops || !current->pid ||
  170. is_global_init(current)) {
  171. mdelay(MSEC_PER_SEC);
  172. }
  173. if (in_interrupt())
  174. panic("Fatal exception in interrupt");
  175. if (panic_on_oops)
  176. panic("Fatal exception");
  177. do_exit(signr);
  178. }
  179. NOKPROBE_SYMBOL(oops_end);
  180. static int __die(const char *str, struct pt_regs *regs, long err)
  181. {
  182. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  183. #ifdef CONFIG_PREEMPT
  184. printk("PREEMPT ");
  185. #endif
  186. #ifdef CONFIG_SMP
  187. printk("SMP NR_CPUS=%d ", NR_CPUS);
  188. #endif
  189. if (debug_pagealloc_enabled())
  190. printk("DEBUG_PAGEALLOC ");
  191. #ifdef CONFIG_NUMA
  192. printk("NUMA ");
  193. #endif
  194. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  195. if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
  196. return 1;
  197. print_modules();
  198. show_regs(regs);
  199. return 0;
  200. }
  201. NOKPROBE_SYMBOL(__die);
  202. void die(const char *str, struct pt_regs *regs, long err)
  203. {
  204. unsigned long flags;
  205. if (debugger(regs))
  206. return;
  207. flags = oops_begin(regs);
  208. if (__die(str, regs, err))
  209. err = 0;
  210. oops_end(flags, regs, err);
  211. }
  212. void user_single_step_siginfo(struct task_struct *tsk,
  213. struct pt_regs *regs, siginfo_t *info)
  214. {
  215. memset(info, 0, sizeof(*info));
  216. info->si_signo = SIGTRAP;
  217. info->si_code = TRAP_TRACE;
  218. info->si_addr = (void __user *)regs->nip;
  219. }
  220. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  221. {
  222. siginfo_t info;
  223. const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  224. "at %08lx nip %08lx lr %08lx code %x\n";
  225. const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  226. "at %016lx nip %016lx lr %016lx code %x\n";
  227. if (!user_mode(regs)) {
  228. die("Exception in kernel mode", regs, signr);
  229. return;
  230. }
  231. if (show_unhandled_signals && unhandled_signal(current, signr)) {
  232. printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
  233. current->comm, current->pid, signr,
  234. addr, regs->nip, regs->link, code);
  235. }
  236. if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
  237. local_irq_enable();
  238. current->thread.trap_nr = code;
  239. memset(&info, 0, sizeof(info));
  240. info.si_signo = signr;
  241. info.si_code = code;
  242. info.si_addr = (void __user *) addr;
  243. force_sig_info(signr, &info, current);
  244. }
  245. void system_reset_exception(struct pt_regs *regs)
  246. {
  247. /* See if any machine dependent calls */
  248. if (ppc_md.system_reset_exception) {
  249. if (ppc_md.system_reset_exception(regs))
  250. return;
  251. }
  252. die("System Reset", regs, SIGABRT);
  253. /* Must die if the interrupt is not recoverable */
  254. if (!(regs->msr & MSR_RI))
  255. panic("Unrecoverable System Reset");
  256. /* What should we do here? We could issue a shutdown or hard reset. */
  257. }
  258. #ifdef CONFIG_PPC64
  259. /*
  260. * This function is called in real mode. Strictly no printk's please.
  261. *
  262. * regs->nip and regs->msr contains srr0 and ssr1.
  263. */
  264. long machine_check_early(struct pt_regs *regs)
  265. {
  266. long handled = 0;
  267. __this_cpu_inc(irq_stat.mce_exceptions);
  268. add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
  269. if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
  270. handled = cur_cpu_spec->machine_check_early(regs);
  271. return handled;
  272. }
  273. long hmi_exception_realmode(struct pt_regs *regs)
  274. {
  275. __this_cpu_inc(irq_stat.hmi_exceptions);
  276. wait_for_subcore_guest_exit();
  277. if (ppc_md.hmi_exception_early)
  278. ppc_md.hmi_exception_early(regs);
  279. wait_for_tb_resync();
  280. return 0;
  281. }
  282. #endif
  283. /*
  284. * I/O accesses can cause machine checks on powermacs.
  285. * Check if the NIP corresponds to the address of a sync
  286. * instruction for which there is an entry in the exception
  287. * table.
  288. * Note that the 601 only takes a machine check on TEA
  289. * (transfer error ack) signal assertion, and does not
  290. * set any of the top 16 bits of SRR1.
  291. * -- paulus.
  292. */
  293. static inline int check_io_access(struct pt_regs *regs)
  294. {
  295. #ifdef CONFIG_PPC32
  296. unsigned long msr = regs->msr;
  297. const struct exception_table_entry *entry;
  298. unsigned int *nip = (unsigned int *)regs->nip;
  299. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  300. && (entry = search_exception_tables(regs->nip)) != NULL) {
  301. /*
  302. * Check that it's a sync instruction, or somewhere
  303. * in the twi; isync; nop sequence that inb/inw/inl uses.
  304. * As the address is in the exception table
  305. * we should be able to read the instr there.
  306. * For the debug message, we look at the preceding
  307. * load or store.
  308. */
  309. if (*nip == PPC_INST_NOP)
  310. nip -= 2;
  311. else if (*nip == PPC_INST_ISYNC)
  312. --nip;
  313. if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
  314. unsigned int rb;
  315. --nip;
  316. rb = (*nip >> 11) & 0x1f;
  317. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  318. (*nip & 0x100)? "OUT to": "IN from",
  319. regs->gpr[rb] - _IO_BASE, nip);
  320. regs->msr |= MSR_RI;
  321. regs->nip = extable_fixup(entry);
  322. return 1;
  323. }
  324. }
  325. #endif /* CONFIG_PPC32 */
  326. return 0;
  327. }
  328. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  329. /* On 4xx, the reason for the machine check or program exception
  330. is in the ESR. */
  331. #define get_reason(regs) ((regs)->dsisr)
  332. #ifndef CONFIG_FSL_BOOKE
  333. #define get_mc_reason(regs) ((regs)->dsisr)
  334. #else
  335. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  336. #endif
  337. #define REASON_FP ESR_FP
  338. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  339. #define REASON_PRIVILEGED ESR_PPR
  340. #define REASON_TRAP ESR_PTR
  341. /* single-step stuff */
  342. #define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
  343. #define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
  344. #else
  345. /* On non-4xx, the reason for the machine check or program
  346. exception is in the MSR. */
  347. #define get_reason(regs) ((regs)->msr)
  348. #define get_mc_reason(regs) ((regs)->msr)
  349. #define REASON_TM 0x200000
  350. #define REASON_FP 0x100000
  351. #define REASON_ILLEGAL 0x80000
  352. #define REASON_PRIVILEGED 0x40000
  353. #define REASON_TRAP 0x20000
  354. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  355. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  356. #endif
  357. #if defined(CONFIG_4xx)
  358. int machine_check_4xx(struct pt_regs *regs)
  359. {
  360. unsigned long reason = get_mc_reason(regs);
  361. if (reason & ESR_IMCP) {
  362. printk("Instruction");
  363. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  364. } else
  365. printk("Data");
  366. printk(" machine check in kernel mode.\n");
  367. return 0;
  368. }
  369. int machine_check_440A(struct pt_regs *regs)
  370. {
  371. unsigned long reason = get_mc_reason(regs);
  372. printk("Machine check in kernel mode.\n");
  373. if (reason & ESR_IMCP){
  374. printk("Instruction Synchronous Machine Check exception\n");
  375. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  376. }
  377. else {
  378. u32 mcsr = mfspr(SPRN_MCSR);
  379. if (mcsr & MCSR_IB)
  380. printk("Instruction Read PLB Error\n");
  381. if (mcsr & MCSR_DRB)
  382. printk("Data Read PLB Error\n");
  383. if (mcsr & MCSR_DWB)
  384. printk("Data Write PLB Error\n");
  385. if (mcsr & MCSR_TLBP)
  386. printk("TLB Parity Error\n");
  387. if (mcsr & MCSR_ICP){
  388. flush_instruction_cache();
  389. printk("I-Cache Parity Error\n");
  390. }
  391. if (mcsr & MCSR_DCSP)
  392. printk("D-Cache Search Parity Error\n");
  393. if (mcsr & MCSR_DCFP)
  394. printk("D-Cache Flush Parity Error\n");
  395. if (mcsr & MCSR_IMPE)
  396. printk("Machine Check exception is imprecise\n");
  397. /* Clear MCSR */
  398. mtspr(SPRN_MCSR, mcsr);
  399. }
  400. return 0;
  401. }
  402. int machine_check_47x(struct pt_regs *regs)
  403. {
  404. unsigned long reason = get_mc_reason(regs);
  405. u32 mcsr;
  406. printk(KERN_ERR "Machine check in kernel mode.\n");
  407. if (reason & ESR_IMCP) {
  408. printk(KERN_ERR
  409. "Instruction Synchronous Machine Check exception\n");
  410. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  411. return 0;
  412. }
  413. mcsr = mfspr(SPRN_MCSR);
  414. if (mcsr & MCSR_IB)
  415. printk(KERN_ERR "Instruction Read PLB Error\n");
  416. if (mcsr & MCSR_DRB)
  417. printk(KERN_ERR "Data Read PLB Error\n");
  418. if (mcsr & MCSR_DWB)
  419. printk(KERN_ERR "Data Write PLB Error\n");
  420. if (mcsr & MCSR_TLBP)
  421. printk(KERN_ERR "TLB Parity Error\n");
  422. if (mcsr & MCSR_ICP) {
  423. flush_instruction_cache();
  424. printk(KERN_ERR "I-Cache Parity Error\n");
  425. }
  426. if (mcsr & MCSR_DCSP)
  427. printk(KERN_ERR "D-Cache Search Parity Error\n");
  428. if (mcsr & PPC47x_MCSR_GPR)
  429. printk(KERN_ERR "GPR Parity Error\n");
  430. if (mcsr & PPC47x_MCSR_FPR)
  431. printk(KERN_ERR "FPR Parity Error\n");
  432. if (mcsr & PPC47x_MCSR_IPR)
  433. printk(KERN_ERR "Machine Check exception is imprecise\n");
  434. /* Clear MCSR */
  435. mtspr(SPRN_MCSR, mcsr);
  436. return 0;
  437. }
  438. #elif defined(CONFIG_E500)
  439. int machine_check_e500mc(struct pt_regs *regs)
  440. {
  441. unsigned long mcsr = mfspr(SPRN_MCSR);
  442. unsigned long reason = mcsr;
  443. int recoverable = 1;
  444. if (reason & MCSR_LD) {
  445. recoverable = fsl_rio_mcheck_exception(regs);
  446. if (recoverable == 1)
  447. goto silent_out;
  448. }
  449. printk("Machine check in kernel mode.\n");
  450. printk("Caused by (from MCSR=%lx): ", reason);
  451. if (reason & MCSR_MCP)
  452. printk("Machine Check Signal\n");
  453. if (reason & MCSR_ICPERR) {
  454. printk("Instruction Cache Parity Error\n");
  455. /*
  456. * This is recoverable by invalidating the i-cache.
  457. */
  458. mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
  459. while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
  460. ;
  461. /*
  462. * This will generally be accompanied by an instruction
  463. * fetch error report -- only treat MCSR_IF as fatal
  464. * if it wasn't due to an L1 parity error.
  465. */
  466. reason &= ~MCSR_IF;
  467. }
  468. if (reason & MCSR_DCPERR_MC) {
  469. printk("Data Cache Parity Error\n");
  470. /*
  471. * In write shadow mode we auto-recover from the error, but it
  472. * may still get logged and cause a machine check. We should
  473. * only treat the non-write shadow case as non-recoverable.
  474. */
  475. if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
  476. recoverable = 0;
  477. }
  478. if (reason & MCSR_L2MMU_MHIT) {
  479. printk("Hit on multiple TLB entries\n");
  480. recoverable = 0;
  481. }
  482. if (reason & MCSR_NMI)
  483. printk("Non-maskable interrupt\n");
  484. if (reason & MCSR_IF) {
  485. printk("Instruction Fetch Error Report\n");
  486. recoverable = 0;
  487. }
  488. if (reason & MCSR_LD) {
  489. printk("Load Error Report\n");
  490. recoverable = 0;
  491. }
  492. if (reason & MCSR_ST) {
  493. printk("Store Error Report\n");
  494. recoverable = 0;
  495. }
  496. if (reason & MCSR_LDG) {
  497. printk("Guarded Load Error Report\n");
  498. recoverable = 0;
  499. }
  500. if (reason & MCSR_TLBSYNC)
  501. printk("Simultaneous tlbsync operations\n");
  502. if (reason & MCSR_BSL2_ERR) {
  503. printk("Level 2 Cache Error\n");
  504. recoverable = 0;
  505. }
  506. if (reason & MCSR_MAV) {
  507. u64 addr;
  508. addr = mfspr(SPRN_MCAR);
  509. addr |= (u64)mfspr(SPRN_MCARU) << 32;
  510. printk("Machine Check %s Address: %#llx\n",
  511. reason & MCSR_MEA ? "Effective" : "Physical", addr);
  512. }
  513. silent_out:
  514. mtspr(SPRN_MCSR, mcsr);
  515. return mfspr(SPRN_MCSR) == 0 && recoverable;
  516. }
  517. int machine_check_e500(struct pt_regs *regs)
  518. {
  519. unsigned long reason = get_mc_reason(regs);
  520. if (reason & MCSR_BUS_RBERR) {
  521. if (fsl_rio_mcheck_exception(regs))
  522. return 1;
  523. if (fsl_pci_mcheck_exception(regs))
  524. return 1;
  525. }
  526. printk("Machine check in kernel mode.\n");
  527. printk("Caused by (from MCSR=%lx): ", reason);
  528. if (reason & MCSR_MCP)
  529. printk("Machine Check Signal\n");
  530. if (reason & MCSR_ICPERR)
  531. printk("Instruction Cache Parity Error\n");
  532. if (reason & MCSR_DCP_PERR)
  533. printk("Data Cache Push Parity Error\n");
  534. if (reason & MCSR_DCPERR)
  535. printk("Data Cache Parity Error\n");
  536. if (reason & MCSR_BUS_IAERR)
  537. printk("Bus - Instruction Address Error\n");
  538. if (reason & MCSR_BUS_RAERR)
  539. printk("Bus - Read Address Error\n");
  540. if (reason & MCSR_BUS_WAERR)
  541. printk("Bus - Write Address Error\n");
  542. if (reason & MCSR_BUS_IBERR)
  543. printk("Bus - Instruction Data Error\n");
  544. if (reason & MCSR_BUS_RBERR)
  545. printk("Bus - Read Data Bus Error\n");
  546. if (reason & MCSR_BUS_WBERR)
  547. printk("Bus - Write Data Bus Error\n");
  548. if (reason & MCSR_BUS_IPERR)
  549. printk("Bus - Instruction Parity Error\n");
  550. if (reason & MCSR_BUS_RPERR)
  551. printk("Bus - Read Parity Error\n");
  552. return 0;
  553. }
  554. int machine_check_generic(struct pt_regs *regs)
  555. {
  556. return 0;
  557. }
  558. #elif defined(CONFIG_E200)
  559. int machine_check_e200(struct pt_regs *regs)
  560. {
  561. unsigned long reason = get_mc_reason(regs);
  562. printk("Machine check in kernel mode.\n");
  563. printk("Caused by (from MCSR=%lx): ", reason);
  564. if (reason & MCSR_MCP)
  565. printk("Machine Check Signal\n");
  566. if (reason & MCSR_CP_PERR)
  567. printk("Cache Push Parity Error\n");
  568. if (reason & MCSR_CPERR)
  569. printk("Cache Parity Error\n");
  570. if (reason & MCSR_EXCP_ERR)
  571. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  572. if (reason & MCSR_BUS_IRERR)
  573. printk("Bus - Read Bus Error on instruction fetch\n");
  574. if (reason & MCSR_BUS_DRERR)
  575. printk("Bus - Read Bus Error on data load\n");
  576. if (reason & MCSR_BUS_WRERR)
  577. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  578. return 0;
  579. }
  580. #elif defined(CONFIG_PPC_8xx)
  581. int machine_check_8xx(struct pt_regs *regs)
  582. {
  583. unsigned long reason = get_mc_reason(regs);
  584. pr_err("Machine check in kernel mode.\n");
  585. pr_err("Caused by (from SRR1=%lx): ", reason);
  586. if (reason & 0x40000000)
  587. pr_err("Fetch error at address %lx\n", regs->nip);
  588. else
  589. pr_err("Data access error at address %lx\n", regs->dar);
  590. #ifdef CONFIG_PCI
  591. /* the qspan pci read routines can cause machine checks -- Cort
  592. *
  593. * yuck !!! that totally needs to go away ! There are better ways
  594. * to deal with that than having a wart in the mcheck handler.
  595. * -- BenH
  596. */
  597. bad_page_fault(regs, regs->dar, SIGBUS);
  598. return 1;
  599. #else
  600. return 0;
  601. #endif
  602. }
  603. #else
  604. int machine_check_generic(struct pt_regs *regs)
  605. {
  606. unsigned long reason = get_mc_reason(regs);
  607. printk("Machine check in kernel mode.\n");
  608. printk("Caused by (from SRR1=%lx): ", reason);
  609. switch (reason & 0x601F0000) {
  610. case 0x80000:
  611. printk("Machine check signal\n");
  612. break;
  613. case 0: /* for 601 */
  614. case 0x40000:
  615. case 0x140000: /* 7450 MSS error and TEA */
  616. printk("Transfer error ack signal\n");
  617. break;
  618. case 0x20000:
  619. printk("Data parity error signal\n");
  620. break;
  621. case 0x10000:
  622. printk("Address parity error signal\n");
  623. break;
  624. case 0x20000000:
  625. printk("L1 Data Cache error\n");
  626. break;
  627. case 0x40000000:
  628. printk("L1 Instruction Cache error\n");
  629. break;
  630. case 0x00100000:
  631. printk("L2 data cache parity error\n");
  632. break;
  633. default:
  634. printk("Unknown values in msr\n");
  635. }
  636. return 0;
  637. }
  638. #endif /* everything else */
  639. void machine_check_exception(struct pt_regs *regs)
  640. {
  641. enum ctx_state prev_state = exception_enter();
  642. int recover = 0;
  643. __this_cpu_inc(irq_stat.mce_exceptions);
  644. /* See if any machine dependent calls. In theory, we would want
  645. * to call the CPU first, and call the ppc_md. one if the CPU
  646. * one returns a positive number. However there is existing code
  647. * that assumes the board gets a first chance, so let's keep it
  648. * that way for now and fix things later. --BenH.
  649. */
  650. if (ppc_md.machine_check_exception)
  651. recover = ppc_md.machine_check_exception(regs);
  652. else if (cur_cpu_spec->machine_check)
  653. recover = cur_cpu_spec->machine_check(regs);
  654. if (recover > 0)
  655. goto bail;
  656. if (debugger_fault_handler(regs))
  657. goto bail;
  658. if (check_io_access(regs))
  659. goto bail;
  660. die("Machine check", regs, SIGBUS);
  661. /* Must die if the interrupt is not recoverable */
  662. if (!(regs->msr & MSR_RI))
  663. panic("Unrecoverable Machine check");
  664. bail:
  665. exception_exit(prev_state);
  666. }
  667. void SMIException(struct pt_regs *regs)
  668. {
  669. die("System Management Interrupt", regs, SIGABRT);
  670. }
  671. void handle_hmi_exception(struct pt_regs *regs)
  672. {
  673. struct pt_regs *old_regs;
  674. old_regs = set_irq_regs(regs);
  675. irq_enter();
  676. if (ppc_md.handle_hmi_exception)
  677. ppc_md.handle_hmi_exception(regs);
  678. irq_exit();
  679. set_irq_regs(old_regs);
  680. }
  681. void unknown_exception(struct pt_regs *regs)
  682. {
  683. enum ctx_state prev_state = exception_enter();
  684. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  685. regs->nip, regs->msr, regs->trap);
  686. _exception(SIGTRAP, regs, 0, 0);
  687. exception_exit(prev_state);
  688. }
  689. void instruction_breakpoint_exception(struct pt_regs *regs)
  690. {
  691. enum ctx_state prev_state = exception_enter();
  692. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  693. 5, SIGTRAP) == NOTIFY_STOP)
  694. goto bail;
  695. if (debugger_iabr_match(regs))
  696. goto bail;
  697. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  698. bail:
  699. exception_exit(prev_state);
  700. }
  701. void RunModeException(struct pt_regs *regs)
  702. {
  703. _exception(SIGTRAP, regs, 0, 0);
  704. }
  705. void single_step_exception(struct pt_regs *regs)
  706. {
  707. enum ctx_state prev_state = exception_enter();
  708. clear_single_step(regs);
  709. if (kprobe_post_handler(regs))
  710. return;
  711. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  712. 5, SIGTRAP) == NOTIFY_STOP)
  713. goto bail;
  714. if (debugger_sstep(regs))
  715. goto bail;
  716. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  717. bail:
  718. exception_exit(prev_state);
  719. }
  720. NOKPROBE_SYMBOL(single_step_exception);
  721. /*
  722. * After we have successfully emulated an instruction, we have to
  723. * check if the instruction was being single-stepped, and if so,
  724. * pretend we got a single-step exception. This was pointed out
  725. * by Kumar Gala. -- paulus
  726. */
  727. static void emulate_single_step(struct pt_regs *regs)
  728. {
  729. if (single_stepping(regs))
  730. single_step_exception(regs);
  731. }
  732. static inline int __parse_fpscr(unsigned long fpscr)
  733. {
  734. int ret = 0;
  735. /* Invalid operation */
  736. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  737. ret = FPE_FLTINV;
  738. /* Overflow */
  739. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  740. ret = FPE_FLTOVF;
  741. /* Underflow */
  742. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  743. ret = FPE_FLTUND;
  744. /* Divide by zero */
  745. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  746. ret = FPE_FLTDIV;
  747. /* Inexact result */
  748. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  749. ret = FPE_FLTRES;
  750. return ret;
  751. }
  752. static void parse_fpe(struct pt_regs *regs)
  753. {
  754. int code = 0;
  755. flush_fp_to_thread(current);
  756. code = __parse_fpscr(current->thread.fp_state.fpscr);
  757. _exception(SIGFPE, regs, code, regs->nip);
  758. }
  759. /*
  760. * Illegal instruction emulation support. Originally written to
  761. * provide the PVR to user applications using the mfspr rd, PVR.
  762. * Return non-zero if we can't emulate, or -EFAULT if the associated
  763. * memory access caused an access fault. Return zero on success.
  764. *
  765. * There are a couple of ways to do this, either "decode" the instruction
  766. * or directly match lots of bits. In this case, matching lots of
  767. * bits is faster and easier.
  768. *
  769. */
  770. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  771. {
  772. u8 rT = (instword >> 21) & 0x1f;
  773. u8 rA = (instword >> 16) & 0x1f;
  774. u8 NB_RB = (instword >> 11) & 0x1f;
  775. u32 num_bytes;
  776. unsigned long EA;
  777. int pos = 0;
  778. /* Early out if we are an invalid form of lswx */
  779. if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
  780. if ((rT == rA) || (rT == NB_RB))
  781. return -EINVAL;
  782. EA = (rA == 0) ? 0 : regs->gpr[rA];
  783. switch (instword & PPC_INST_STRING_MASK) {
  784. case PPC_INST_LSWX:
  785. case PPC_INST_STSWX:
  786. EA += NB_RB;
  787. num_bytes = regs->xer & 0x7f;
  788. break;
  789. case PPC_INST_LSWI:
  790. case PPC_INST_STSWI:
  791. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  792. break;
  793. default:
  794. return -EINVAL;
  795. }
  796. while (num_bytes != 0)
  797. {
  798. u8 val;
  799. u32 shift = 8 * (3 - (pos & 0x3));
  800. /* if process is 32-bit, clear upper 32 bits of EA */
  801. if ((regs->msr & MSR_64BIT) == 0)
  802. EA &= 0xFFFFFFFF;
  803. switch ((instword & PPC_INST_STRING_MASK)) {
  804. case PPC_INST_LSWX:
  805. case PPC_INST_LSWI:
  806. if (get_user(val, (u8 __user *)EA))
  807. return -EFAULT;
  808. /* first time updating this reg,
  809. * zero it out */
  810. if (pos == 0)
  811. regs->gpr[rT] = 0;
  812. regs->gpr[rT] |= val << shift;
  813. break;
  814. case PPC_INST_STSWI:
  815. case PPC_INST_STSWX:
  816. val = regs->gpr[rT] >> shift;
  817. if (put_user(val, (u8 __user *)EA))
  818. return -EFAULT;
  819. break;
  820. }
  821. /* move EA to next address */
  822. EA += 1;
  823. num_bytes--;
  824. /* manage our position within the register */
  825. if (++pos == 4) {
  826. pos = 0;
  827. if (++rT == 32)
  828. rT = 0;
  829. }
  830. }
  831. return 0;
  832. }
  833. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  834. {
  835. u32 ra,rs;
  836. unsigned long tmp;
  837. ra = (instword >> 16) & 0x1f;
  838. rs = (instword >> 21) & 0x1f;
  839. tmp = regs->gpr[rs];
  840. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  841. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  842. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  843. regs->gpr[ra] = tmp;
  844. return 0;
  845. }
  846. static int emulate_isel(struct pt_regs *regs, u32 instword)
  847. {
  848. u8 rT = (instword >> 21) & 0x1f;
  849. u8 rA = (instword >> 16) & 0x1f;
  850. u8 rB = (instword >> 11) & 0x1f;
  851. u8 BC = (instword >> 6) & 0x1f;
  852. u8 bit;
  853. unsigned long tmp;
  854. tmp = (rA == 0) ? 0 : regs->gpr[rA];
  855. bit = (regs->ccr >> (31 - BC)) & 0x1;
  856. regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
  857. return 0;
  858. }
  859. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  860. static inline bool tm_abort_check(struct pt_regs *regs, int cause)
  861. {
  862. /* If we're emulating a load/store in an active transaction, we cannot
  863. * emulate it as the kernel operates in transaction suspended context.
  864. * We need to abort the transaction. This creates a persistent TM
  865. * abort so tell the user what caused it with a new code.
  866. */
  867. if (MSR_TM_TRANSACTIONAL(regs->msr)) {
  868. tm_enable();
  869. tm_abort(cause);
  870. return true;
  871. }
  872. return false;
  873. }
  874. #else
  875. static inline bool tm_abort_check(struct pt_regs *regs, int reason)
  876. {
  877. return false;
  878. }
  879. #endif
  880. static int emulate_instruction(struct pt_regs *regs)
  881. {
  882. u32 instword;
  883. u32 rd;
  884. if (!user_mode(regs))
  885. return -EINVAL;
  886. CHECK_FULL_REGS(regs);
  887. if (get_user(instword, (u32 __user *)(regs->nip)))
  888. return -EFAULT;
  889. /* Emulate the mfspr rD, PVR. */
  890. if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
  891. PPC_WARN_EMULATED(mfpvr, regs);
  892. rd = (instword >> 21) & 0x1f;
  893. regs->gpr[rd] = mfspr(SPRN_PVR);
  894. return 0;
  895. }
  896. /* Emulating the dcba insn is just a no-op. */
  897. if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
  898. PPC_WARN_EMULATED(dcba, regs);
  899. return 0;
  900. }
  901. /* Emulate the mcrxr insn. */
  902. if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
  903. int shift = (instword >> 21) & 0x1c;
  904. unsigned long msk = 0xf0000000UL >> shift;
  905. PPC_WARN_EMULATED(mcrxr, regs);
  906. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  907. regs->xer &= ~0xf0000000UL;
  908. return 0;
  909. }
  910. /* Emulate load/store string insn. */
  911. if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
  912. if (tm_abort_check(regs,
  913. TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
  914. return -EINVAL;
  915. PPC_WARN_EMULATED(string, regs);
  916. return emulate_string_inst(regs, instword);
  917. }
  918. /* Emulate the popcntb (Population Count Bytes) instruction. */
  919. if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
  920. PPC_WARN_EMULATED(popcntb, regs);
  921. return emulate_popcntb_inst(regs, instword);
  922. }
  923. /* Emulate isel (Integer Select) instruction */
  924. if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
  925. PPC_WARN_EMULATED(isel, regs);
  926. return emulate_isel(regs, instword);
  927. }
  928. /* Emulate sync instruction variants */
  929. if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
  930. PPC_WARN_EMULATED(sync, regs);
  931. asm volatile("sync");
  932. return 0;
  933. }
  934. #ifdef CONFIG_PPC64
  935. /* Emulate the mfspr rD, DSCR. */
  936. if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
  937. PPC_INST_MFSPR_DSCR_USER) ||
  938. ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
  939. PPC_INST_MFSPR_DSCR)) &&
  940. cpu_has_feature(CPU_FTR_DSCR)) {
  941. PPC_WARN_EMULATED(mfdscr, regs);
  942. rd = (instword >> 21) & 0x1f;
  943. regs->gpr[rd] = mfspr(SPRN_DSCR);
  944. return 0;
  945. }
  946. /* Emulate the mtspr DSCR, rD. */
  947. if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
  948. PPC_INST_MTSPR_DSCR_USER) ||
  949. ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
  950. PPC_INST_MTSPR_DSCR)) &&
  951. cpu_has_feature(CPU_FTR_DSCR)) {
  952. PPC_WARN_EMULATED(mtdscr, regs);
  953. rd = (instword >> 21) & 0x1f;
  954. current->thread.dscr = regs->gpr[rd];
  955. current->thread.dscr_inherit = 1;
  956. mtspr(SPRN_DSCR, current->thread.dscr);
  957. return 0;
  958. }
  959. #endif
  960. return -EINVAL;
  961. }
  962. int is_valid_bugaddr(unsigned long addr)
  963. {
  964. return is_kernel_addr(addr);
  965. }
  966. #ifdef CONFIG_MATH_EMULATION
  967. static int emulate_math(struct pt_regs *regs)
  968. {
  969. int ret;
  970. extern int do_mathemu(struct pt_regs *regs);
  971. ret = do_mathemu(regs);
  972. if (ret >= 0)
  973. PPC_WARN_EMULATED(math, regs);
  974. switch (ret) {
  975. case 0:
  976. emulate_single_step(regs);
  977. return 0;
  978. case 1: {
  979. int code = 0;
  980. code = __parse_fpscr(current->thread.fp_state.fpscr);
  981. _exception(SIGFPE, regs, code, regs->nip);
  982. return 0;
  983. }
  984. case -EFAULT:
  985. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  986. return 0;
  987. }
  988. return -1;
  989. }
  990. #else
  991. static inline int emulate_math(struct pt_regs *regs) { return -1; }
  992. #endif
  993. void program_check_exception(struct pt_regs *regs)
  994. {
  995. enum ctx_state prev_state = exception_enter();
  996. unsigned int reason = get_reason(regs);
  997. /* We can now get here via a FP Unavailable exception if the core
  998. * has no FPU, in that case the reason flags will be 0 */
  999. if (reason & REASON_FP) {
  1000. /* IEEE FP exception */
  1001. parse_fpe(regs);
  1002. goto bail;
  1003. }
  1004. if (reason & REASON_TRAP) {
  1005. unsigned long bugaddr;
  1006. /* Debugger is first in line to stop recursive faults in
  1007. * rcu_lock, notify_die, or atomic_notifier_call_chain */
  1008. if (debugger_bpt(regs))
  1009. goto bail;
  1010. if (kprobe_handler(regs))
  1011. goto bail;
  1012. /* trap exception */
  1013. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  1014. == NOTIFY_STOP)
  1015. goto bail;
  1016. bugaddr = regs->nip;
  1017. /*
  1018. * Fixup bugaddr for BUG_ON() in real mode
  1019. */
  1020. if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
  1021. bugaddr += PAGE_OFFSET;
  1022. if (!(regs->msr & MSR_PR) && /* not user-mode */
  1023. report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
  1024. regs->nip += 4;
  1025. goto bail;
  1026. }
  1027. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  1028. goto bail;
  1029. }
  1030. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1031. if (reason & REASON_TM) {
  1032. /* This is a TM "Bad Thing Exception" program check.
  1033. * This occurs when:
  1034. * - An rfid/hrfid/mtmsrd attempts to cause an illegal
  1035. * transition in TM states.
  1036. * - A trechkpt is attempted when transactional.
  1037. * - A treclaim is attempted when non transactional.
  1038. * - A tend is illegally attempted.
  1039. * - writing a TM SPR when transactional.
  1040. */
  1041. if (!user_mode(regs) &&
  1042. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  1043. regs->nip += 4;
  1044. goto bail;
  1045. }
  1046. /* If usermode caused this, it's done something illegal and
  1047. * gets a SIGILL slap on the wrist. We call it an illegal
  1048. * operand to distinguish from the instruction just being bad
  1049. * (e.g. executing a 'tend' on a CPU without TM!); it's an
  1050. * illegal /placement/ of a valid instruction.
  1051. */
  1052. if (user_mode(regs)) {
  1053. _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
  1054. goto bail;
  1055. } else {
  1056. printk(KERN_EMERG "Unexpected TM Bad Thing exception "
  1057. "at %lx (msr 0x%x)\n", regs->nip, reason);
  1058. die("Unrecoverable exception", regs, SIGABRT);
  1059. }
  1060. }
  1061. #endif
  1062. /*
  1063. * If we took the program check in the kernel skip down to sending a
  1064. * SIGILL. The subsequent cases all relate to emulating instructions
  1065. * which we should only do for userspace. We also do not want to enable
  1066. * interrupts for kernel faults because that might lead to further
  1067. * faults, and loose the context of the original exception.
  1068. */
  1069. if (!user_mode(regs))
  1070. goto sigill;
  1071. /* We restore the interrupt state now */
  1072. if (!arch_irq_disabled_regs(regs))
  1073. local_irq_enable();
  1074. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  1075. * but there seems to be a hardware bug on the 405GP (RevD)
  1076. * that means ESR is sometimes set incorrectly - either to
  1077. * ESR_DST (!?) or 0. In the process of chasing this with the
  1078. * hardware people - not sure if it can happen on any illegal
  1079. * instruction or only on FP instructions, whether there is a
  1080. * pattern to occurrences etc. -dgibson 31/Mar/2003
  1081. */
  1082. if (!emulate_math(regs))
  1083. goto bail;
  1084. /* Try to emulate it if we should. */
  1085. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  1086. switch (emulate_instruction(regs)) {
  1087. case 0:
  1088. regs->nip += 4;
  1089. emulate_single_step(regs);
  1090. goto bail;
  1091. case -EFAULT:
  1092. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  1093. goto bail;
  1094. }
  1095. }
  1096. sigill:
  1097. if (reason & REASON_PRIVILEGED)
  1098. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1099. else
  1100. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1101. bail:
  1102. exception_exit(prev_state);
  1103. }
  1104. NOKPROBE_SYMBOL(program_check_exception);
  1105. /*
  1106. * This occurs when running in hypervisor mode on POWER6 or later
  1107. * and an illegal instruction is encountered.
  1108. */
  1109. void emulation_assist_interrupt(struct pt_regs *regs)
  1110. {
  1111. regs->msr |= REASON_ILLEGAL;
  1112. program_check_exception(regs);
  1113. }
  1114. NOKPROBE_SYMBOL(emulation_assist_interrupt);
  1115. void alignment_exception(struct pt_regs *regs)
  1116. {
  1117. enum ctx_state prev_state = exception_enter();
  1118. int sig, code, fixed = 0;
  1119. /* We restore the interrupt state now */
  1120. if (!arch_irq_disabled_regs(regs))
  1121. local_irq_enable();
  1122. if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
  1123. goto bail;
  1124. /* we don't implement logging of alignment exceptions */
  1125. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  1126. fixed = fix_alignment(regs);
  1127. if (fixed == 1) {
  1128. regs->nip += 4; /* skip over emulated instruction */
  1129. emulate_single_step(regs);
  1130. goto bail;
  1131. }
  1132. /* Operand address was bad */
  1133. if (fixed == -EFAULT) {
  1134. sig = SIGSEGV;
  1135. code = SEGV_ACCERR;
  1136. } else {
  1137. sig = SIGBUS;
  1138. code = BUS_ADRALN;
  1139. }
  1140. if (user_mode(regs))
  1141. _exception(sig, regs, code, regs->dar);
  1142. else
  1143. bad_page_fault(regs, regs->dar, sig);
  1144. bail:
  1145. exception_exit(prev_state);
  1146. }
  1147. void slb_miss_bad_addr(struct pt_regs *regs)
  1148. {
  1149. enum ctx_state prev_state = exception_enter();
  1150. if (user_mode(regs))
  1151. _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
  1152. else
  1153. bad_page_fault(regs, regs->dar, SIGSEGV);
  1154. exception_exit(prev_state);
  1155. }
  1156. void StackOverflow(struct pt_regs *regs)
  1157. {
  1158. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  1159. current, regs->gpr[1]);
  1160. debugger(regs);
  1161. show_regs(regs);
  1162. panic("kernel stack overflow");
  1163. }
  1164. void nonrecoverable_exception(struct pt_regs *regs)
  1165. {
  1166. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  1167. regs->nip, regs->msr);
  1168. debugger(regs);
  1169. die("nonrecoverable exception", regs, SIGKILL);
  1170. }
  1171. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  1172. {
  1173. enum ctx_state prev_state = exception_enter();
  1174. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  1175. "%lx at %lx\n", regs->trap, regs->nip);
  1176. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  1177. exception_exit(prev_state);
  1178. }
  1179. void altivec_unavailable_exception(struct pt_regs *regs)
  1180. {
  1181. enum ctx_state prev_state = exception_enter();
  1182. if (user_mode(regs)) {
  1183. /* A user program has executed an altivec instruction,
  1184. but this kernel doesn't support altivec. */
  1185. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1186. goto bail;
  1187. }
  1188. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  1189. "%lx at %lx\n", regs->trap, regs->nip);
  1190. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  1191. bail:
  1192. exception_exit(prev_state);
  1193. }
  1194. void vsx_unavailable_exception(struct pt_regs *regs)
  1195. {
  1196. if (user_mode(regs)) {
  1197. /* A user program has executed an vsx instruction,
  1198. but this kernel doesn't support vsx. */
  1199. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1200. return;
  1201. }
  1202. printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
  1203. "%lx at %lx\n", regs->trap, regs->nip);
  1204. die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
  1205. }
  1206. #ifdef CONFIG_PPC64
  1207. static void tm_unavailable(struct pt_regs *regs)
  1208. {
  1209. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1210. if (user_mode(regs)) {
  1211. current->thread.load_tm++;
  1212. regs->msr |= MSR_TM;
  1213. tm_enable();
  1214. tm_restore_sprs(&current->thread);
  1215. return;
  1216. }
  1217. #endif
  1218. pr_emerg("Unrecoverable TM Unavailable Exception "
  1219. "%lx at %lx\n", regs->trap, regs->nip);
  1220. die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
  1221. }
  1222. void facility_unavailable_exception(struct pt_regs *regs)
  1223. {
  1224. static char *facility_strings[] = {
  1225. [FSCR_FP_LG] = "FPU",
  1226. [FSCR_VECVSX_LG] = "VMX/VSX",
  1227. [FSCR_DSCR_LG] = "DSCR",
  1228. [FSCR_PM_LG] = "PMU SPRs",
  1229. [FSCR_BHRB_LG] = "BHRB",
  1230. [FSCR_TM_LG] = "TM",
  1231. [FSCR_EBB_LG] = "EBB",
  1232. [FSCR_TAR_LG] = "TAR",
  1233. };
  1234. char *facility = "unknown";
  1235. u64 value;
  1236. u32 instword, rd;
  1237. u8 status;
  1238. bool hv;
  1239. hv = (regs->trap == 0xf80);
  1240. if (hv)
  1241. value = mfspr(SPRN_HFSCR);
  1242. else
  1243. value = mfspr(SPRN_FSCR);
  1244. status = value >> 56;
  1245. if (status == FSCR_DSCR_LG) {
  1246. /*
  1247. * User is accessing the DSCR register using the problem
  1248. * state only SPR number (0x03) either through a mfspr or
  1249. * a mtspr instruction. If it is a write attempt through
  1250. * a mtspr, then we set the inherit bit. This also allows
  1251. * the user to write or read the register directly in the
  1252. * future by setting via the FSCR DSCR bit. But in case it
  1253. * is a read DSCR attempt through a mfspr instruction, we
  1254. * just emulate the instruction instead. This code path will
  1255. * always emulate all the mfspr instructions till the user
  1256. * has attempted at least one mtspr instruction. This way it
  1257. * preserves the same behaviour when the user is accessing
  1258. * the DSCR through privilege level only SPR number (0x11)
  1259. * which is emulated through illegal instruction exception.
  1260. * We always leave HFSCR DSCR set.
  1261. */
  1262. if (get_user(instword, (u32 __user *)(regs->nip))) {
  1263. pr_err("Failed to fetch the user instruction\n");
  1264. return;
  1265. }
  1266. /* Write into DSCR (mtspr 0x03, RS) */
  1267. if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
  1268. == PPC_INST_MTSPR_DSCR_USER) {
  1269. rd = (instword >> 21) & 0x1f;
  1270. current->thread.dscr = regs->gpr[rd];
  1271. current->thread.dscr_inherit = 1;
  1272. current->thread.fscr |= FSCR_DSCR;
  1273. mtspr(SPRN_FSCR, current->thread.fscr);
  1274. }
  1275. /* Read from DSCR (mfspr RT, 0x03) */
  1276. if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
  1277. == PPC_INST_MFSPR_DSCR_USER) {
  1278. if (emulate_instruction(regs)) {
  1279. pr_err("DSCR based mfspr emulation failed\n");
  1280. return;
  1281. }
  1282. regs->nip += 4;
  1283. emulate_single_step(regs);
  1284. }
  1285. return;
  1286. }
  1287. if (status == FSCR_TM_LG) {
  1288. /*
  1289. * If we're here then the hardware is TM aware because it
  1290. * generated an exception with FSRM_TM set.
  1291. *
  1292. * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
  1293. * told us not to do TM, or the kernel is not built with TM
  1294. * support.
  1295. *
  1296. * If both of those things are true, then userspace can spam the
  1297. * console by triggering the printk() below just by continually
  1298. * doing tbegin (or any TM instruction). So in that case just
  1299. * send the process a SIGILL immediately.
  1300. */
  1301. if (!cpu_has_feature(CPU_FTR_TM))
  1302. goto out;
  1303. tm_unavailable(regs);
  1304. return;
  1305. }
  1306. if ((hv || status >= 2) &&
  1307. (status < ARRAY_SIZE(facility_strings)) &&
  1308. facility_strings[status])
  1309. facility = facility_strings[status];
  1310. /* We restore the interrupt state now */
  1311. if (!arch_irq_disabled_regs(regs))
  1312. local_irq_enable();
  1313. pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
  1314. hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
  1315. out:
  1316. if (user_mode(regs)) {
  1317. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1318. return;
  1319. }
  1320. die("Unexpected facility unavailable exception", regs, SIGABRT);
  1321. }
  1322. #endif
  1323. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1324. void fp_unavailable_tm(struct pt_regs *regs)
  1325. {
  1326. /* Note: This does not handle any kind of FP laziness. */
  1327. TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
  1328. regs->nip, regs->msr);
  1329. /* We can only have got here if the task started using FP after
  1330. * beginning the transaction. So, the transactional regs are just a
  1331. * copy of the checkpointed ones. But, we still need to recheckpoint
  1332. * as we're enabling FP for the process; it will return, abort the
  1333. * transaction, and probably retry but now with FP enabled. So the
  1334. * checkpointed FP registers need to be loaded.
  1335. */
  1336. tm_reclaim_current(TM_CAUSE_FAC_UNAV);
  1337. /* Reclaim didn't save out any FPRs to transact_fprs. */
  1338. /* Enable FP for the task: */
  1339. regs->msr |= (MSR_FP | current->thread.fpexc_mode);
  1340. /* This loads and recheckpoints the FP registers from
  1341. * thread.fpr[]. They will remain in registers after the
  1342. * checkpoint so we don't need to reload them after.
  1343. * If VMX is in use, the VRs now hold checkpointed values,
  1344. * so we don't want to load the VRs from the thread_struct.
  1345. */
  1346. tm_recheckpoint(&current->thread, MSR_FP);
  1347. /* If VMX is in use, get the transactional values back */
  1348. if (regs->msr & MSR_VEC) {
  1349. msr_check_and_set(MSR_VEC);
  1350. load_vr_state(&current->thread.vr_state);
  1351. /* At this point all the VSX state is loaded, so enable it */
  1352. regs->msr |= MSR_VSX;
  1353. }
  1354. }
  1355. void altivec_unavailable_tm(struct pt_regs *regs)
  1356. {
  1357. /* See the comments in fp_unavailable_tm(). This function operates
  1358. * the same way.
  1359. */
  1360. TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
  1361. "MSR=%lx\n",
  1362. regs->nip, regs->msr);
  1363. tm_reclaim_current(TM_CAUSE_FAC_UNAV);
  1364. regs->msr |= MSR_VEC;
  1365. tm_recheckpoint(&current->thread, MSR_VEC);
  1366. current->thread.used_vr = 1;
  1367. if (regs->msr & MSR_FP) {
  1368. msr_check_and_set(MSR_FP);
  1369. load_fp_state(&current->thread.fp_state);
  1370. regs->msr |= MSR_VSX;
  1371. }
  1372. }
  1373. void vsx_unavailable_tm(struct pt_regs *regs)
  1374. {
  1375. unsigned long orig_msr = regs->msr;
  1376. /* See the comments in fp_unavailable_tm(). This works similarly,
  1377. * though we're loading both FP and VEC registers in here.
  1378. *
  1379. * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
  1380. * regs. Either way, set MSR_VSX.
  1381. */
  1382. TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
  1383. "MSR=%lx\n",
  1384. regs->nip, regs->msr);
  1385. current->thread.used_vsr = 1;
  1386. /* If FP and VMX are already loaded, we have all the state we need */
  1387. if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
  1388. regs->msr |= MSR_VSX;
  1389. return;
  1390. }
  1391. /* This reclaims FP and/or VR regs if they're already enabled */
  1392. tm_reclaim_current(TM_CAUSE_FAC_UNAV);
  1393. regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
  1394. MSR_VSX;
  1395. /* This loads & recheckpoints FP and VRs; but we have
  1396. * to be sure not to overwrite previously-valid state.
  1397. */
  1398. tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
  1399. msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
  1400. if (orig_msr & MSR_FP)
  1401. load_fp_state(&current->thread.fp_state);
  1402. if (orig_msr & MSR_VEC)
  1403. load_vr_state(&current->thread.vr_state);
  1404. }
  1405. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  1406. void performance_monitor_exception(struct pt_regs *regs)
  1407. {
  1408. __this_cpu_inc(irq_stat.pmu_irqs);
  1409. perf_irq(regs);
  1410. }
  1411. #ifdef CONFIG_8xx
  1412. void SoftwareEmulation(struct pt_regs *regs)
  1413. {
  1414. CHECK_FULL_REGS(regs);
  1415. if (!user_mode(regs)) {
  1416. debugger(regs);
  1417. die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
  1418. regs, SIGFPE);
  1419. }
  1420. if (!emulate_math(regs))
  1421. return;
  1422. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1423. }
  1424. #endif /* CONFIG_8xx */
  1425. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  1426. static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
  1427. {
  1428. int changed = 0;
  1429. /*
  1430. * Determine the cause of the debug event, clear the
  1431. * event flags and send a trap to the handler. Torez
  1432. */
  1433. if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
  1434. dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  1435. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  1436. current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
  1437. #endif
  1438. do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
  1439. 5);
  1440. changed |= 0x01;
  1441. } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
  1442. dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  1443. do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
  1444. 6);
  1445. changed |= 0x01;
  1446. } else if (debug_status & DBSR_IAC1) {
  1447. current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
  1448. dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
  1449. do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
  1450. 1);
  1451. changed |= 0x01;
  1452. } else if (debug_status & DBSR_IAC2) {
  1453. current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
  1454. do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
  1455. 2);
  1456. changed |= 0x01;
  1457. } else if (debug_status & DBSR_IAC3) {
  1458. current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
  1459. dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
  1460. do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
  1461. 3);
  1462. changed |= 0x01;
  1463. } else if (debug_status & DBSR_IAC4) {
  1464. current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
  1465. do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
  1466. 4);
  1467. changed |= 0x01;
  1468. }
  1469. /*
  1470. * At the point this routine was called, the MSR(DE) was turned off.
  1471. * Check all other debug flags and see if that bit needs to be turned
  1472. * back on or not.
  1473. */
  1474. if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
  1475. current->thread.debug.dbcr1))
  1476. regs->msr |= MSR_DE;
  1477. else
  1478. /* Make sure the IDM flag is off */
  1479. current->thread.debug.dbcr0 &= ~DBCR0_IDM;
  1480. if (changed & 0x01)
  1481. mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
  1482. }
  1483. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  1484. {
  1485. current->thread.debug.dbsr = debug_status;
  1486. /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
  1487. * on server, it stops on the target of the branch. In order to simulate
  1488. * the server behaviour, we thus restart right away with a single step
  1489. * instead of stopping here when hitting a BT
  1490. */
  1491. if (debug_status & DBSR_BT) {
  1492. regs->msr &= ~MSR_DE;
  1493. /* Disable BT */
  1494. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
  1495. /* Clear the BT event */
  1496. mtspr(SPRN_DBSR, DBSR_BT);
  1497. /* Do the single step trick only when coming from userspace */
  1498. if (user_mode(regs)) {
  1499. current->thread.debug.dbcr0 &= ~DBCR0_BT;
  1500. current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  1501. regs->msr |= MSR_DE;
  1502. return;
  1503. }
  1504. if (kprobe_post_handler(regs))
  1505. return;
  1506. if (notify_die(DIE_SSTEP, "block_step", regs, 5,
  1507. 5, SIGTRAP) == NOTIFY_STOP) {
  1508. return;
  1509. }
  1510. if (debugger_sstep(regs))
  1511. return;
  1512. } else if (debug_status & DBSR_IC) { /* Instruction complete */
  1513. regs->msr &= ~MSR_DE;
  1514. /* Disable instruction completion */
  1515. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  1516. /* Clear the instruction completion event */
  1517. mtspr(SPRN_DBSR, DBSR_IC);
  1518. if (kprobe_post_handler(regs))
  1519. return;
  1520. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  1521. 5, SIGTRAP) == NOTIFY_STOP) {
  1522. return;
  1523. }
  1524. if (debugger_sstep(regs))
  1525. return;
  1526. if (user_mode(regs)) {
  1527. current->thread.debug.dbcr0 &= ~DBCR0_IC;
  1528. if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
  1529. current->thread.debug.dbcr1))
  1530. regs->msr |= MSR_DE;
  1531. else
  1532. /* Make sure the IDM bit is off */
  1533. current->thread.debug.dbcr0 &= ~DBCR0_IDM;
  1534. }
  1535. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  1536. } else
  1537. handle_debug(regs, debug_status);
  1538. }
  1539. NOKPROBE_SYMBOL(DebugException);
  1540. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  1541. #if !defined(CONFIG_TAU_INT)
  1542. void TAUException(struct pt_regs *regs)
  1543. {
  1544. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  1545. regs->nip, regs->msr, regs->trap, print_tainted());
  1546. }
  1547. #endif /* CONFIG_INT_TAU */
  1548. #ifdef CONFIG_ALTIVEC
  1549. void altivec_assist_exception(struct pt_regs *regs)
  1550. {
  1551. int err;
  1552. if (!user_mode(regs)) {
  1553. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  1554. " at %lx\n", regs->nip);
  1555. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  1556. }
  1557. flush_altivec_to_thread(current);
  1558. PPC_WARN_EMULATED(altivec, regs);
  1559. err = emulate_altivec(regs);
  1560. if (err == 0) {
  1561. regs->nip += 4; /* skip emulated instruction */
  1562. emulate_single_step(regs);
  1563. return;
  1564. }
  1565. if (err == -EFAULT) {
  1566. /* got an error reading the instruction */
  1567. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1568. } else {
  1569. /* didn't recognize the instruction */
  1570. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  1571. printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
  1572. "in %s at %lx\n", current->comm, regs->nip);
  1573. current->thread.vr_state.vscr.u[3] |= 0x10000;
  1574. }
  1575. }
  1576. #endif /* CONFIG_ALTIVEC */
  1577. #ifdef CONFIG_FSL_BOOKE
  1578. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  1579. unsigned long error_code)
  1580. {
  1581. /* We treat cache locking instructions from the user
  1582. * as priv ops, in the future we could try to do
  1583. * something smarter
  1584. */
  1585. if (error_code & (ESR_DLK|ESR_ILK))
  1586. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1587. return;
  1588. }
  1589. #endif /* CONFIG_FSL_BOOKE */
  1590. #ifdef CONFIG_SPE
  1591. void SPEFloatingPointException(struct pt_regs *regs)
  1592. {
  1593. extern int do_spe_mathemu(struct pt_regs *regs);
  1594. unsigned long spefscr;
  1595. int fpexc_mode;
  1596. int code = 0;
  1597. int err;
  1598. flush_spe_to_thread(current);
  1599. spefscr = current->thread.spefscr;
  1600. fpexc_mode = current->thread.fpexc_mode;
  1601. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  1602. code = FPE_FLTOVF;
  1603. }
  1604. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1605. code = FPE_FLTUND;
  1606. }
  1607. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1608. code = FPE_FLTDIV;
  1609. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1610. code = FPE_FLTINV;
  1611. }
  1612. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1613. code = FPE_FLTRES;
  1614. err = do_spe_mathemu(regs);
  1615. if (err == 0) {
  1616. regs->nip += 4; /* skip emulated instruction */
  1617. emulate_single_step(regs);
  1618. return;
  1619. }
  1620. if (err == -EFAULT) {
  1621. /* got an error reading the instruction */
  1622. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1623. } else if (err == -EINVAL) {
  1624. /* didn't recognize the instruction */
  1625. printk(KERN_ERR "unrecognized spe instruction "
  1626. "in %s at %lx\n", current->comm, regs->nip);
  1627. } else {
  1628. _exception(SIGFPE, regs, code, regs->nip);
  1629. }
  1630. return;
  1631. }
  1632. void SPEFloatingPointRoundException(struct pt_regs *regs)
  1633. {
  1634. extern int speround_handler(struct pt_regs *regs);
  1635. int err;
  1636. preempt_disable();
  1637. if (regs->msr & MSR_SPE)
  1638. giveup_spe(current);
  1639. preempt_enable();
  1640. regs->nip -= 4;
  1641. err = speround_handler(regs);
  1642. if (err == 0) {
  1643. regs->nip += 4; /* skip emulated instruction */
  1644. emulate_single_step(regs);
  1645. return;
  1646. }
  1647. if (err == -EFAULT) {
  1648. /* got an error reading the instruction */
  1649. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1650. } else if (err == -EINVAL) {
  1651. /* didn't recognize the instruction */
  1652. printk(KERN_ERR "unrecognized spe instruction "
  1653. "in %s at %lx\n", current->comm, regs->nip);
  1654. } else {
  1655. _exception(SIGFPE, regs, 0, regs->nip);
  1656. return;
  1657. }
  1658. }
  1659. #endif
  1660. /*
  1661. * We enter here if we get an unrecoverable exception, that is, one
  1662. * that happened at a point where the RI (recoverable interrupt) bit
  1663. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1664. * we therefore lost state by taking this exception.
  1665. */
  1666. void unrecoverable_exception(struct pt_regs *regs)
  1667. {
  1668. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  1669. regs->trap, regs->nip);
  1670. die("Unrecoverable exception", regs, SIGABRT);
  1671. }
  1672. #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
  1673. /*
  1674. * Default handler for a Watchdog exception,
  1675. * spins until a reboot occurs
  1676. */
  1677. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1678. {
  1679. /* Generic WatchdogHandler, implement your own */
  1680. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1681. return;
  1682. }
  1683. void WatchdogException(struct pt_regs *regs)
  1684. {
  1685. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1686. WatchdogHandler(regs);
  1687. }
  1688. #endif
  1689. /*
  1690. * We enter here if we discover during exception entry that we are
  1691. * running in supervisor mode with a userspace value in the stack pointer.
  1692. */
  1693. void kernel_bad_stack(struct pt_regs *regs)
  1694. {
  1695. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1696. regs->gpr[1], regs->nip);
  1697. die("Bad kernel stack pointer", regs, SIGABRT);
  1698. }
  1699. void __init trap_init(void)
  1700. {
  1701. }
  1702. #ifdef CONFIG_PPC_EMULATED_STATS
  1703. #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
  1704. struct ppc_emulated ppc_emulated = {
  1705. #ifdef CONFIG_ALTIVEC
  1706. WARN_EMULATED_SETUP(altivec),
  1707. #endif
  1708. WARN_EMULATED_SETUP(dcba),
  1709. WARN_EMULATED_SETUP(dcbz),
  1710. WARN_EMULATED_SETUP(fp_pair),
  1711. WARN_EMULATED_SETUP(isel),
  1712. WARN_EMULATED_SETUP(mcrxr),
  1713. WARN_EMULATED_SETUP(mfpvr),
  1714. WARN_EMULATED_SETUP(multiple),
  1715. WARN_EMULATED_SETUP(popcntb),
  1716. WARN_EMULATED_SETUP(spe),
  1717. WARN_EMULATED_SETUP(string),
  1718. WARN_EMULATED_SETUP(sync),
  1719. WARN_EMULATED_SETUP(unaligned),
  1720. #ifdef CONFIG_MATH_EMULATION
  1721. WARN_EMULATED_SETUP(math),
  1722. #endif
  1723. #ifdef CONFIG_VSX
  1724. WARN_EMULATED_SETUP(vsx),
  1725. #endif
  1726. #ifdef CONFIG_PPC64
  1727. WARN_EMULATED_SETUP(mfdscr),
  1728. WARN_EMULATED_SETUP(mtdscr),
  1729. WARN_EMULATED_SETUP(lq_stq),
  1730. #endif
  1731. };
  1732. u32 ppc_warn_emulated;
  1733. void ppc_warn_emulated_print(const char *type)
  1734. {
  1735. pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
  1736. type);
  1737. }
  1738. static int __init ppc_warn_emulated_init(void)
  1739. {
  1740. struct dentry *dir, *d;
  1741. unsigned int i;
  1742. struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
  1743. if (!powerpc_debugfs_root)
  1744. return -ENODEV;
  1745. dir = debugfs_create_dir("emulated_instructions",
  1746. powerpc_debugfs_root);
  1747. if (!dir)
  1748. return -ENOMEM;
  1749. d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
  1750. &ppc_warn_emulated);
  1751. if (!d)
  1752. goto fail;
  1753. for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
  1754. d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
  1755. (u32 *)&entries[i].val.counter);
  1756. if (!d)
  1757. goto fail;
  1758. }
  1759. return 0;
  1760. fail:
  1761. debugfs_remove_recursive(dir);
  1762. return -ENOMEM;
  1763. }
  1764. device_initcall(ppc_warn_emulated_init);
  1765. #endif /* CONFIG_PPC_EMULATED_STATS */