traps.c 27 KB

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