traps.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Modified by Cort Dougan (cort@cs.nmt.edu)
  10. * and Paul Mackerras (paulus@samba.org)
  11. */
  12. /*
  13. * This file handles the architecture-dependent parts of hardware exceptions
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/stddef.h>
  20. #include <linux/unistd.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/slab.h>
  23. #include <linux/user.h>
  24. #include <linux/a.out.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/prctl.h>
  29. #include <linux/delay.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kexec.h>
  32. #include <linux/backlight.h>
  33. #include <linux/bug.h>
  34. #include <asm/kdebug.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/system.h>
  38. #include <asm/io.h>
  39. #include <asm/machdep.h>
  40. #include <asm/rtas.h>
  41. #include <asm/pmc.h>
  42. #ifdef CONFIG_PPC32
  43. #include <asm/reg.h>
  44. #endif
  45. #ifdef CONFIG_PMAC_BACKLIGHT
  46. #include <asm/backlight.h>
  47. #endif
  48. #ifdef CONFIG_PPC64
  49. #include <asm/firmware.h>
  50. #include <asm/processor.h>
  51. #endif
  52. #include <asm/kexec.h>
  53. #ifdef CONFIG_DEBUGGER
  54. int (*__debugger)(struct pt_regs *regs);
  55. int (*__debugger_ipi)(struct pt_regs *regs);
  56. int (*__debugger_bpt)(struct pt_regs *regs);
  57. int (*__debugger_sstep)(struct pt_regs *regs);
  58. int (*__debugger_iabr_match)(struct pt_regs *regs);
  59. int (*__debugger_dabr_match)(struct pt_regs *regs);
  60. int (*__debugger_fault_handler)(struct pt_regs *regs);
  61. EXPORT_SYMBOL(__debugger);
  62. EXPORT_SYMBOL(__debugger_ipi);
  63. EXPORT_SYMBOL(__debugger_bpt);
  64. EXPORT_SYMBOL(__debugger_sstep);
  65. EXPORT_SYMBOL(__debugger_iabr_match);
  66. EXPORT_SYMBOL(__debugger_dabr_match);
  67. EXPORT_SYMBOL(__debugger_fault_handler);
  68. #endif
  69. ATOMIC_NOTIFIER_HEAD(powerpc_die_chain);
  70. int register_die_notifier(struct notifier_block *nb)
  71. {
  72. return atomic_notifier_chain_register(&powerpc_die_chain, nb);
  73. }
  74. EXPORT_SYMBOL(register_die_notifier);
  75. int unregister_die_notifier(struct notifier_block *nb)
  76. {
  77. return atomic_notifier_chain_unregister(&powerpc_die_chain, nb);
  78. }
  79. EXPORT_SYMBOL(unregister_die_notifier);
  80. /*
  81. * Trap & Exception support
  82. */
  83. #ifdef CONFIG_PMAC_BACKLIGHT
  84. static void pmac_backlight_unblank(void)
  85. {
  86. mutex_lock(&pmac_backlight_mutex);
  87. if (pmac_backlight) {
  88. struct backlight_properties *props;
  89. props = &pmac_backlight->props;
  90. props->brightness = props->max_brightness;
  91. props->power = FB_BLANK_UNBLANK;
  92. backlight_update_status(pmac_backlight);
  93. }
  94. mutex_unlock(&pmac_backlight_mutex);
  95. }
  96. #else
  97. static inline void pmac_backlight_unblank(void) { }
  98. #endif
  99. int die(const char *str, struct pt_regs *regs, long err)
  100. {
  101. static struct {
  102. spinlock_t lock;
  103. u32 lock_owner;
  104. int lock_owner_depth;
  105. } die = {
  106. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  107. .lock_owner = -1,
  108. .lock_owner_depth = 0
  109. };
  110. static int die_counter;
  111. unsigned long flags;
  112. if (debugger(regs))
  113. return 1;
  114. oops_enter();
  115. if (die.lock_owner != raw_smp_processor_id()) {
  116. console_verbose();
  117. spin_lock_irqsave(&die.lock, flags);
  118. die.lock_owner = smp_processor_id();
  119. die.lock_owner_depth = 0;
  120. bust_spinlocks(1);
  121. if (machine_is(powermac))
  122. pmac_backlight_unblank();
  123. } else {
  124. local_save_flags(flags);
  125. }
  126. if (++die.lock_owner_depth < 3) {
  127. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  128. #ifdef CONFIG_PREEMPT
  129. printk("PREEMPT ");
  130. #endif
  131. #ifdef CONFIG_SMP
  132. printk("SMP NR_CPUS=%d ", NR_CPUS);
  133. #endif
  134. #ifdef CONFIG_DEBUG_PAGEALLOC
  135. printk("DEBUG_PAGEALLOC ");
  136. #endif
  137. #ifdef CONFIG_NUMA
  138. printk("NUMA ");
  139. #endif
  140. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  141. print_modules();
  142. show_regs(regs);
  143. } else {
  144. printk("Recursive die() failure, output suppressed\n");
  145. }
  146. bust_spinlocks(0);
  147. die.lock_owner = -1;
  148. spin_unlock_irqrestore(&die.lock, flags);
  149. if (kexec_should_crash(current) ||
  150. kexec_sr_activated(smp_processor_id()))
  151. crash_kexec(regs);
  152. crash_kexec_secondary(regs);
  153. if (in_interrupt())
  154. panic("Fatal exception in interrupt");
  155. if (panic_on_oops)
  156. panic("Fatal exception");
  157. oops_exit();
  158. do_exit(err);
  159. return 0;
  160. }
  161. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  162. {
  163. siginfo_t info;
  164. if (!user_mode(regs)) {
  165. if (die("Exception in kernel mode", regs, signr))
  166. return;
  167. }
  168. memset(&info, 0, sizeof(info));
  169. info.si_signo = signr;
  170. info.si_code = code;
  171. info.si_addr = (void __user *) addr;
  172. force_sig_info(signr, &info, current);
  173. /*
  174. * Init gets no signals that it doesn't have a handler for.
  175. * That's all very well, but if it has caused a synchronous
  176. * exception and we ignore the resulting signal, it will just
  177. * generate the same exception over and over again and we get
  178. * nowhere. Better to kill it and let the kernel panic.
  179. */
  180. if (is_init(current)) {
  181. __sighandler_t handler;
  182. spin_lock_irq(&current->sighand->siglock);
  183. handler = current->sighand->action[signr-1].sa.sa_handler;
  184. spin_unlock_irq(&current->sighand->siglock);
  185. if (handler == SIG_DFL) {
  186. /* init has generated a synchronous exception
  187. and it doesn't have a handler for the signal */
  188. printk(KERN_CRIT "init has generated signal %d "
  189. "but has no handler for it\n", signr);
  190. do_exit(signr);
  191. }
  192. }
  193. }
  194. #ifdef CONFIG_PPC64
  195. void system_reset_exception(struct pt_regs *regs)
  196. {
  197. /* See if any machine dependent calls */
  198. if (ppc_md.system_reset_exception) {
  199. if (ppc_md.system_reset_exception(regs))
  200. return;
  201. }
  202. #ifdef CONFIG_KEXEC
  203. cpu_set(smp_processor_id(), cpus_in_sr);
  204. #endif
  205. die("System Reset", regs, SIGABRT);
  206. /*
  207. * Some CPUs when released from the debugger will execute this path.
  208. * These CPUs entered the debugger via a soft-reset. If the CPU was
  209. * hung before entering the debugger it will return to the hung
  210. * state when exiting this function. This causes a problem in
  211. * kdump since the hung CPU(s) will not respond to the IPI sent
  212. * from kdump. To prevent the problem we call crash_kexec_secondary()
  213. * here. If a kdump had not been initiated or we exit the debugger
  214. * with the "exit and recover" command (x) crash_kexec_secondary()
  215. * will return after 5ms and the CPU returns to its previous state.
  216. */
  217. crash_kexec_secondary(regs);
  218. /* Must die if the interrupt is not recoverable */
  219. if (!(regs->msr & MSR_RI))
  220. panic("Unrecoverable System Reset");
  221. /* What should we do here? We could issue a shutdown or hard reset. */
  222. }
  223. #endif
  224. /*
  225. * I/O accesses can cause machine checks on powermacs.
  226. * Check if the NIP corresponds to the address of a sync
  227. * instruction for which there is an entry in the exception
  228. * table.
  229. * Note that the 601 only takes a machine check on TEA
  230. * (transfer error ack) signal assertion, and does not
  231. * set any of the top 16 bits of SRR1.
  232. * -- paulus.
  233. */
  234. static inline int check_io_access(struct pt_regs *regs)
  235. {
  236. #ifdef CONFIG_PPC32
  237. unsigned long msr = regs->msr;
  238. const struct exception_table_entry *entry;
  239. unsigned int *nip = (unsigned int *)regs->nip;
  240. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  241. && (entry = search_exception_tables(regs->nip)) != NULL) {
  242. /*
  243. * Check that it's a sync instruction, or somewhere
  244. * in the twi; isync; nop sequence that inb/inw/inl uses.
  245. * As the address is in the exception table
  246. * we should be able to read the instr there.
  247. * For the debug message, we look at the preceding
  248. * load or store.
  249. */
  250. if (*nip == 0x60000000) /* nop */
  251. nip -= 2;
  252. else if (*nip == 0x4c00012c) /* isync */
  253. --nip;
  254. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  255. /* sync or twi */
  256. unsigned int rb;
  257. --nip;
  258. rb = (*nip >> 11) & 0x1f;
  259. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  260. (*nip & 0x100)? "OUT to": "IN from",
  261. regs->gpr[rb] - _IO_BASE, nip);
  262. regs->msr |= MSR_RI;
  263. regs->nip = entry->fixup;
  264. return 1;
  265. }
  266. }
  267. #endif /* CONFIG_PPC32 */
  268. return 0;
  269. }
  270. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  271. /* On 4xx, the reason for the machine check or program exception
  272. is in the ESR. */
  273. #define get_reason(regs) ((regs)->dsisr)
  274. #ifndef CONFIG_FSL_BOOKE
  275. #define get_mc_reason(regs) ((regs)->dsisr)
  276. #else
  277. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  278. #endif
  279. #define REASON_FP ESR_FP
  280. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  281. #define REASON_PRIVILEGED ESR_PPR
  282. #define REASON_TRAP ESR_PTR
  283. /* single-step stuff */
  284. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  285. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  286. #else
  287. /* On non-4xx, the reason for the machine check or program
  288. exception is in the MSR. */
  289. #define get_reason(regs) ((regs)->msr)
  290. #define get_mc_reason(regs) ((regs)->msr)
  291. #define REASON_FP 0x100000
  292. #define REASON_ILLEGAL 0x80000
  293. #define REASON_PRIVILEGED 0x40000
  294. #define REASON_TRAP 0x20000
  295. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  296. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  297. #endif
  298. /*
  299. * This is "fall-back" implementation for configurations
  300. * which don't provide platform-specific machine check info
  301. */
  302. void __attribute__ ((weak))
  303. platform_machine_check(struct pt_regs *regs)
  304. {
  305. }
  306. void machine_check_exception(struct pt_regs *regs)
  307. {
  308. int recover = 0;
  309. unsigned long reason = get_mc_reason(regs);
  310. /* See if any machine dependent calls */
  311. if (ppc_md.machine_check_exception)
  312. recover = ppc_md.machine_check_exception(regs);
  313. if (recover)
  314. return;
  315. if (user_mode(regs)) {
  316. regs->msr |= MSR_RI;
  317. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  318. return;
  319. }
  320. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  321. /* the qspan pci read routines can cause machine checks -- Cort */
  322. bad_page_fault(regs, regs->dar, SIGBUS);
  323. return;
  324. #endif
  325. if (debugger_fault_handler(regs)) {
  326. regs->msr |= MSR_RI;
  327. return;
  328. }
  329. if (check_io_access(regs))
  330. return;
  331. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  332. if (reason & ESR_IMCP) {
  333. printk("Instruction");
  334. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  335. } else
  336. printk("Data");
  337. printk(" machine check in kernel mode.\n");
  338. #elif defined(CONFIG_440A)
  339. printk("Machine check in kernel mode.\n");
  340. if (reason & ESR_IMCP){
  341. printk("Instruction Synchronous Machine Check exception\n");
  342. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  343. }
  344. else {
  345. u32 mcsr = mfspr(SPRN_MCSR);
  346. if (mcsr & MCSR_IB)
  347. printk("Instruction Read PLB Error\n");
  348. if (mcsr & MCSR_DRB)
  349. printk("Data Read PLB Error\n");
  350. if (mcsr & MCSR_DWB)
  351. printk("Data Write PLB Error\n");
  352. if (mcsr & MCSR_TLBP)
  353. printk("TLB Parity Error\n");
  354. if (mcsr & MCSR_ICP){
  355. flush_instruction_cache();
  356. printk("I-Cache Parity Error\n");
  357. }
  358. if (mcsr & MCSR_DCSP)
  359. printk("D-Cache Search Parity Error\n");
  360. if (mcsr & MCSR_DCFP)
  361. printk("D-Cache Flush Parity Error\n");
  362. if (mcsr & MCSR_IMPE)
  363. printk("Machine Check exception is imprecise\n");
  364. /* Clear MCSR */
  365. mtspr(SPRN_MCSR, mcsr);
  366. }
  367. #elif defined (CONFIG_E500)
  368. printk("Machine check in kernel mode.\n");
  369. printk("Caused by (from MCSR=%lx): ", reason);
  370. if (reason & MCSR_MCP)
  371. printk("Machine Check Signal\n");
  372. if (reason & MCSR_ICPERR)
  373. printk("Instruction Cache Parity Error\n");
  374. if (reason & MCSR_DCP_PERR)
  375. printk("Data Cache Push Parity Error\n");
  376. if (reason & MCSR_DCPERR)
  377. printk("Data Cache Parity Error\n");
  378. if (reason & MCSR_GL_CI)
  379. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  380. if (reason & MCSR_BUS_IAERR)
  381. printk("Bus - Instruction Address Error\n");
  382. if (reason & MCSR_BUS_RAERR)
  383. printk("Bus - Read Address Error\n");
  384. if (reason & MCSR_BUS_WAERR)
  385. printk("Bus - Write Address Error\n");
  386. if (reason & MCSR_BUS_IBERR)
  387. printk("Bus - Instruction Data Error\n");
  388. if (reason & MCSR_BUS_RBERR)
  389. printk("Bus - Read Data Bus Error\n");
  390. if (reason & MCSR_BUS_WBERR)
  391. printk("Bus - Read Data Bus Error\n");
  392. if (reason & MCSR_BUS_IPERR)
  393. printk("Bus - Instruction Parity Error\n");
  394. if (reason & MCSR_BUS_RPERR)
  395. printk("Bus - Read Parity Error\n");
  396. #elif defined (CONFIG_E200)
  397. printk("Machine check in kernel mode.\n");
  398. printk("Caused by (from MCSR=%lx): ", reason);
  399. if (reason & MCSR_MCP)
  400. printk("Machine Check Signal\n");
  401. if (reason & MCSR_CP_PERR)
  402. printk("Cache Push Parity Error\n");
  403. if (reason & MCSR_CPERR)
  404. printk("Cache Parity Error\n");
  405. if (reason & MCSR_EXCP_ERR)
  406. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  407. if (reason & MCSR_BUS_IRERR)
  408. printk("Bus - Read Bus Error on instruction fetch\n");
  409. if (reason & MCSR_BUS_DRERR)
  410. printk("Bus - Read Bus Error on data load\n");
  411. if (reason & MCSR_BUS_WRERR)
  412. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  413. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  414. printk("Machine check in kernel mode.\n");
  415. printk("Caused by (from SRR1=%lx): ", reason);
  416. switch (reason & 0x601F0000) {
  417. case 0x80000:
  418. printk("Machine check signal\n");
  419. break;
  420. case 0: /* for 601 */
  421. case 0x40000:
  422. case 0x140000: /* 7450 MSS error and TEA */
  423. printk("Transfer error ack signal\n");
  424. break;
  425. case 0x20000:
  426. printk("Data parity error signal\n");
  427. break;
  428. case 0x10000:
  429. printk("Address parity error signal\n");
  430. break;
  431. case 0x20000000:
  432. printk("L1 Data Cache error\n");
  433. break;
  434. case 0x40000000:
  435. printk("L1 Instruction Cache error\n");
  436. break;
  437. case 0x00100000:
  438. printk("L2 data cache parity error\n");
  439. break;
  440. default:
  441. printk("Unknown values in msr\n");
  442. }
  443. #endif /* CONFIG_4xx */
  444. /*
  445. * Optional platform-provided routine to print out
  446. * additional info, e.g. bus error registers.
  447. */
  448. platform_machine_check(regs);
  449. if (debugger_fault_handler(regs))
  450. return;
  451. die("Machine check", regs, SIGBUS);
  452. /* Must die if the interrupt is not recoverable */
  453. if (!(regs->msr & MSR_RI))
  454. panic("Unrecoverable Machine check");
  455. }
  456. void SMIException(struct pt_regs *regs)
  457. {
  458. die("System Management Interrupt", regs, SIGABRT);
  459. }
  460. void unknown_exception(struct pt_regs *regs)
  461. {
  462. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  463. regs->nip, regs->msr, regs->trap);
  464. _exception(SIGTRAP, regs, 0, 0);
  465. }
  466. void instruction_breakpoint_exception(struct pt_regs *regs)
  467. {
  468. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  469. 5, SIGTRAP) == NOTIFY_STOP)
  470. return;
  471. if (debugger_iabr_match(regs))
  472. return;
  473. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  474. }
  475. void RunModeException(struct pt_regs *regs)
  476. {
  477. _exception(SIGTRAP, regs, 0, 0);
  478. }
  479. void __kprobes single_step_exception(struct pt_regs *regs)
  480. {
  481. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  482. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  483. 5, SIGTRAP) == NOTIFY_STOP)
  484. return;
  485. if (debugger_sstep(regs))
  486. return;
  487. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  488. }
  489. /*
  490. * After we have successfully emulated an instruction, we have to
  491. * check if the instruction was being single-stepped, and if so,
  492. * pretend we got a single-step exception. This was pointed out
  493. * by Kumar Gala. -- paulus
  494. */
  495. static void emulate_single_step(struct pt_regs *regs)
  496. {
  497. if (single_stepping(regs)) {
  498. clear_single_step(regs);
  499. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  500. }
  501. }
  502. static inline int __parse_fpscr(unsigned long fpscr)
  503. {
  504. int ret = 0;
  505. /* Invalid operation */
  506. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  507. ret = FPE_FLTINV;
  508. /* Overflow */
  509. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  510. ret = FPE_FLTOVF;
  511. /* Underflow */
  512. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  513. ret = FPE_FLTUND;
  514. /* Divide by zero */
  515. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  516. ret = FPE_FLTDIV;
  517. /* Inexact result */
  518. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  519. ret = FPE_FLTRES;
  520. return ret;
  521. }
  522. static void parse_fpe(struct pt_regs *regs)
  523. {
  524. int code = 0;
  525. flush_fp_to_thread(current);
  526. code = __parse_fpscr(current->thread.fpscr.val);
  527. _exception(SIGFPE, regs, code, regs->nip);
  528. }
  529. /*
  530. * Illegal instruction emulation support. Originally written to
  531. * provide the PVR to user applications using the mfspr rd, PVR.
  532. * Return non-zero if we can't emulate, or -EFAULT if the associated
  533. * memory access caused an access fault. Return zero on success.
  534. *
  535. * There are a couple of ways to do this, either "decode" the instruction
  536. * or directly match lots of bits. In this case, matching lots of
  537. * bits is faster and easier.
  538. *
  539. */
  540. #define INST_MFSPR_PVR 0x7c1f42a6
  541. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  542. #define INST_DCBA 0x7c0005ec
  543. #define INST_DCBA_MASK 0xfc0007fe
  544. #define INST_MCRXR 0x7c000400
  545. #define INST_MCRXR_MASK 0xfc0007fe
  546. #define INST_STRING 0x7c00042a
  547. #define INST_STRING_MASK 0xfc0007fe
  548. #define INST_STRING_GEN_MASK 0xfc00067e
  549. #define INST_LSWI 0x7c0004aa
  550. #define INST_LSWX 0x7c00042a
  551. #define INST_STSWI 0x7c0005aa
  552. #define INST_STSWX 0x7c00052a
  553. #define INST_POPCNTB 0x7c0000f4
  554. #define INST_POPCNTB_MASK 0xfc0007fe
  555. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  556. {
  557. u8 rT = (instword >> 21) & 0x1f;
  558. u8 rA = (instword >> 16) & 0x1f;
  559. u8 NB_RB = (instword >> 11) & 0x1f;
  560. u32 num_bytes;
  561. unsigned long EA;
  562. int pos = 0;
  563. /* Early out if we are an invalid form of lswx */
  564. if ((instword & INST_STRING_MASK) == INST_LSWX)
  565. if ((rT == rA) || (rT == NB_RB))
  566. return -EINVAL;
  567. EA = (rA == 0) ? 0 : regs->gpr[rA];
  568. switch (instword & INST_STRING_MASK) {
  569. case INST_LSWX:
  570. case INST_STSWX:
  571. EA += NB_RB;
  572. num_bytes = regs->xer & 0x7f;
  573. break;
  574. case INST_LSWI:
  575. case INST_STSWI:
  576. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. while (num_bytes != 0)
  582. {
  583. u8 val;
  584. u32 shift = 8 * (3 - (pos & 0x3));
  585. switch ((instword & INST_STRING_MASK)) {
  586. case INST_LSWX:
  587. case INST_LSWI:
  588. if (get_user(val, (u8 __user *)EA))
  589. return -EFAULT;
  590. /* first time updating this reg,
  591. * zero it out */
  592. if (pos == 0)
  593. regs->gpr[rT] = 0;
  594. regs->gpr[rT] |= val << shift;
  595. break;
  596. case INST_STSWI:
  597. case INST_STSWX:
  598. val = regs->gpr[rT] >> shift;
  599. if (put_user(val, (u8 __user *)EA))
  600. return -EFAULT;
  601. break;
  602. }
  603. /* move EA to next address */
  604. EA += 1;
  605. num_bytes--;
  606. /* manage our position within the register */
  607. if (++pos == 4) {
  608. pos = 0;
  609. if (++rT == 32)
  610. rT = 0;
  611. }
  612. }
  613. return 0;
  614. }
  615. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  616. {
  617. u32 ra,rs;
  618. unsigned long tmp;
  619. ra = (instword >> 16) & 0x1f;
  620. rs = (instword >> 21) & 0x1f;
  621. tmp = regs->gpr[rs];
  622. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  623. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  624. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  625. regs->gpr[ra] = tmp;
  626. return 0;
  627. }
  628. static int emulate_instruction(struct pt_regs *regs)
  629. {
  630. u32 instword;
  631. u32 rd;
  632. if (!user_mode(regs) || (regs->msr & MSR_LE))
  633. return -EINVAL;
  634. CHECK_FULL_REGS(regs);
  635. if (get_user(instword, (u32 __user *)(regs->nip)))
  636. return -EFAULT;
  637. /* Emulate the mfspr rD, PVR. */
  638. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  639. rd = (instword >> 21) & 0x1f;
  640. regs->gpr[rd] = mfspr(SPRN_PVR);
  641. return 0;
  642. }
  643. /* Emulating the dcba insn is just a no-op. */
  644. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  645. return 0;
  646. /* Emulate the mcrxr insn. */
  647. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  648. int shift = (instword >> 21) & 0x1c;
  649. unsigned long msk = 0xf0000000UL >> shift;
  650. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  651. regs->xer &= ~0xf0000000UL;
  652. return 0;
  653. }
  654. /* Emulate load/store string insn. */
  655. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  656. return emulate_string_inst(regs, instword);
  657. /* Emulate the popcntb (Population Count Bytes) instruction. */
  658. if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
  659. return emulate_popcntb_inst(regs, instword);
  660. }
  661. return -EINVAL;
  662. }
  663. int is_valid_bugaddr(unsigned long addr)
  664. {
  665. return is_kernel_addr(addr);
  666. }
  667. void __kprobes program_check_exception(struct pt_regs *regs)
  668. {
  669. unsigned int reason = get_reason(regs);
  670. extern int do_mathemu(struct pt_regs *regs);
  671. /* We can now get here via a FP Unavailable exception if the core
  672. * has no FPU, in that case the reason flags will be 0 */
  673. if (reason & REASON_FP) {
  674. /* IEEE FP exception */
  675. parse_fpe(regs);
  676. return;
  677. }
  678. if (reason & REASON_TRAP) {
  679. /* trap exception */
  680. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  681. == NOTIFY_STOP)
  682. return;
  683. if (debugger_bpt(regs))
  684. return;
  685. if (!(regs->msr & MSR_PR) && /* not user-mode */
  686. report_bug(regs->nip) == BUG_TRAP_TYPE_WARN) {
  687. regs->nip += 4;
  688. return;
  689. }
  690. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  691. return;
  692. }
  693. local_irq_enable();
  694. #ifdef CONFIG_MATH_EMULATION
  695. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  696. * but there seems to be a hardware bug on the 405GP (RevD)
  697. * that means ESR is sometimes set incorrectly - either to
  698. * ESR_DST (!?) or 0. In the process of chasing this with the
  699. * hardware people - not sure if it can happen on any illegal
  700. * instruction or only on FP instructions, whether there is a
  701. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  702. switch (do_mathemu(regs)) {
  703. case 0:
  704. emulate_single_step(regs);
  705. return;
  706. case 1: {
  707. int code = 0;
  708. code = __parse_fpscr(current->thread.fpscr.val);
  709. _exception(SIGFPE, regs, code, regs->nip);
  710. return;
  711. }
  712. case -EFAULT:
  713. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  714. return;
  715. }
  716. /* fall through on any other errors */
  717. #endif /* CONFIG_MATH_EMULATION */
  718. /* Try to emulate it if we should. */
  719. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  720. switch (emulate_instruction(regs)) {
  721. case 0:
  722. regs->nip += 4;
  723. emulate_single_step(regs);
  724. return;
  725. case -EFAULT:
  726. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  727. return;
  728. }
  729. }
  730. if (reason & REASON_PRIVILEGED)
  731. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  732. else
  733. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  734. }
  735. void alignment_exception(struct pt_regs *regs)
  736. {
  737. int sig, code, fixed = 0;
  738. /* we don't implement logging of alignment exceptions */
  739. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  740. fixed = fix_alignment(regs);
  741. if (fixed == 1) {
  742. regs->nip += 4; /* skip over emulated instruction */
  743. emulate_single_step(regs);
  744. return;
  745. }
  746. /* Operand address was bad */
  747. if (fixed == -EFAULT) {
  748. sig = SIGSEGV;
  749. code = SEGV_ACCERR;
  750. } else {
  751. sig = SIGBUS;
  752. code = BUS_ADRALN;
  753. }
  754. if (user_mode(regs))
  755. _exception(sig, regs, code, regs->dar);
  756. else
  757. bad_page_fault(regs, regs->dar, sig);
  758. }
  759. void StackOverflow(struct pt_regs *regs)
  760. {
  761. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  762. current, regs->gpr[1]);
  763. debugger(regs);
  764. show_regs(regs);
  765. panic("kernel stack overflow");
  766. }
  767. void nonrecoverable_exception(struct pt_regs *regs)
  768. {
  769. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  770. regs->nip, regs->msr);
  771. debugger(regs);
  772. die("nonrecoverable exception", regs, SIGKILL);
  773. }
  774. void trace_syscall(struct pt_regs *regs)
  775. {
  776. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  777. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  778. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  779. }
  780. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  781. {
  782. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  783. "%lx at %lx\n", regs->trap, regs->nip);
  784. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  785. }
  786. void altivec_unavailable_exception(struct pt_regs *regs)
  787. {
  788. if (user_mode(regs)) {
  789. /* A user program has executed an altivec instruction,
  790. but this kernel doesn't support altivec. */
  791. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  792. return;
  793. }
  794. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  795. "%lx at %lx\n", regs->trap, regs->nip);
  796. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  797. }
  798. void performance_monitor_exception(struct pt_regs *regs)
  799. {
  800. perf_irq(regs);
  801. }
  802. #ifdef CONFIG_8xx
  803. void SoftwareEmulation(struct pt_regs *regs)
  804. {
  805. extern int do_mathemu(struct pt_regs *);
  806. extern int Soft_emulate_8xx(struct pt_regs *);
  807. int errcode;
  808. CHECK_FULL_REGS(regs);
  809. if (!user_mode(regs)) {
  810. debugger(regs);
  811. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  812. }
  813. #ifdef CONFIG_MATH_EMULATION
  814. errcode = do_mathemu(regs);
  815. switch (errcode) {
  816. case 0:
  817. emulate_single_step(regs);
  818. return;
  819. case 1: {
  820. int code = 0;
  821. code = __parse_fpscr(current->thread.fpscr.val);
  822. _exception(SIGFPE, regs, code, regs->nip);
  823. return;
  824. }
  825. case -EFAULT:
  826. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  827. return;
  828. default:
  829. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  830. return;
  831. }
  832. #else
  833. errcode = Soft_emulate_8xx(regs);
  834. switch (errcode) {
  835. case 0:
  836. emulate_single_step(regs);
  837. return;
  838. case 1:
  839. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  840. return;
  841. case -EFAULT:
  842. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  843. return;
  844. }
  845. #endif
  846. }
  847. #endif /* CONFIG_8xx */
  848. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  849. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  850. {
  851. if (debug_status & DBSR_IC) { /* instruction completion */
  852. regs->msr &= ~MSR_DE;
  853. if (user_mode(regs)) {
  854. current->thread.dbcr0 &= ~DBCR0_IC;
  855. } else {
  856. /* Disable instruction completion */
  857. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  858. /* Clear the instruction completion event */
  859. mtspr(SPRN_DBSR, DBSR_IC);
  860. if (debugger_sstep(regs))
  861. return;
  862. }
  863. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  864. }
  865. }
  866. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  867. #if !defined(CONFIG_TAU_INT)
  868. void TAUException(struct pt_regs *regs)
  869. {
  870. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  871. regs->nip, regs->msr, regs->trap, print_tainted());
  872. }
  873. #endif /* CONFIG_INT_TAU */
  874. #ifdef CONFIG_ALTIVEC
  875. void altivec_assist_exception(struct pt_regs *regs)
  876. {
  877. int err;
  878. if (!user_mode(regs)) {
  879. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  880. " at %lx\n", regs->nip);
  881. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  882. }
  883. flush_altivec_to_thread(current);
  884. err = emulate_altivec(regs);
  885. if (err == 0) {
  886. regs->nip += 4; /* skip emulated instruction */
  887. emulate_single_step(regs);
  888. return;
  889. }
  890. if (err == -EFAULT) {
  891. /* got an error reading the instruction */
  892. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  893. } else {
  894. /* didn't recognize the instruction */
  895. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  896. if (printk_ratelimit())
  897. printk(KERN_ERR "Unrecognized altivec instruction "
  898. "in %s at %lx\n", current->comm, regs->nip);
  899. current->thread.vscr.u[3] |= 0x10000;
  900. }
  901. }
  902. #endif /* CONFIG_ALTIVEC */
  903. #ifdef CONFIG_FSL_BOOKE
  904. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  905. unsigned long error_code)
  906. {
  907. /* We treat cache locking instructions from the user
  908. * as priv ops, in the future we could try to do
  909. * something smarter
  910. */
  911. if (error_code & (ESR_DLK|ESR_ILK))
  912. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  913. return;
  914. }
  915. #endif /* CONFIG_FSL_BOOKE */
  916. #ifdef CONFIG_SPE
  917. void SPEFloatingPointException(struct pt_regs *regs)
  918. {
  919. unsigned long spefscr;
  920. int fpexc_mode;
  921. int code = 0;
  922. spefscr = current->thread.spefscr;
  923. fpexc_mode = current->thread.fpexc_mode;
  924. /* Hardware does not neccessarily set sticky
  925. * underflow/overflow/invalid flags */
  926. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  927. code = FPE_FLTOVF;
  928. spefscr |= SPEFSCR_FOVFS;
  929. }
  930. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  931. code = FPE_FLTUND;
  932. spefscr |= SPEFSCR_FUNFS;
  933. }
  934. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  935. code = FPE_FLTDIV;
  936. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  937. code = FPE_FLTINV;
  938. spefscr |= SPEFSCR_FINVS;
  939. }
  940. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  941. code = FPE_FLTRES;
  942. current->thread.spefscr = spefscr;
  943. _exception(SIGFPE, regs, code, regs->nip);
  944. return;
  945. }
  946. #endif
  947. /*
  948. * We enter here if we get an unrecoverable exception, that is, one
  949. * that happened at a point where the RI (recoverable interrupt) bit
  950. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  951. * we therefore lost state by taking this exception.
  952. */
  953. void unrecoverable_exception(struct pt_regs *regs)
  954. {
  955. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  956. regs->trap, regs->nip);
  957. die("Unrecoverable exception", regs, SIGABRT);
  958. }
  959. #ifdef CONFIG_BOOKE_WDT
  960. /*
  961. * Default handler for a Watchdog exception,
  962. * spins until a reboot occurs
  963. */
  964. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  965. {
  966. /* Generic WatchdogHandler, implement your own */
  967. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  968. return;
  969. }
  970. void WatchdogException(struct pt_regs *regs)
  971. {
  972. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  973. WatchdogHandler(regs);
  974. }
  975. #endif
  976. /*
  977. * We enter here if we discover during exception entry that we are
  978. * running in supervisor mode with a userspace value in the stack pointer.
  979. */
  980. void kernel_bad_stack(struct pt_regs *regs)
  981. {
  982. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  983. regs->gpr[1], regs->nip);
  984. die("Bad kernel stack pointer", regs, SIGABRT);
  985. }
  986. void __init trap_init(void)
  987. {
  988. }