traps.c 39 KB

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