traps.c 41 KB

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