traps.c 28 KB

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