traps.c 41 KB

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