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. if (ppc_md.system_reset_exception(regs))
  211. return;
  212. }
  213. die("System Reset", regs, SIGABRT);
  214. /* Must die if the interrupt is not recoverable */
  215. if (!(regs->msr & MSR_RI))
  216. panic("Unrecoverable System Reset");
  217. /* What should we do here? We could issue a shutdown or hard reset. */
  218. }
  219. #endif
  220. /*
  221. * I/O accesses can cause machine checks on powermacs.
  222. * Check if the NIP corresponds to the address of a sync
  223. * instruction for which there is an entry in the exception
  224. * table.
  225. * Note that the 601 only takes a machine check on TEA
  226. * (transfer error ack) signal assertion, and does not
  227. * set any of the top 16 bits of SRR1.
  228. * -- paulus.
  229. */
  230. static inline int check_io_access(struct pt_regs *regs)
  231. {
  232. #ifdef CONFIG_PPC_PMAC
  233. unsigned long msr = regs->msr;
  234. const struct exception_table_entry *entry;
  235. unsigned int *nip = (unsigned int *)regs->nip;
  236. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  237. && (entry = search_exception_tables(regs->nip)) != NULL) {
  238. /*
  239. * Check that it's a sync instruction, or somewhere
  240. * in the twi; isync; nop sequence that inb/inw/inl uses.
  241. * As the address is in the exception table
  242. * we should be able to read the instr there.
  243. * For the debug message, we look at the preceding
  244. * load or store.
  245. */
  246. if (*nip == 0x60000000) /* nop */
  247. nip -= 2;
  248. else if (*nip == 0x4c00012c) /* isync */
  249. --nip;
  250. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  251. /* sync or twi */
  252. unsigned int rb;
  253. --nip;
  254. rb = (*nip >> 11) & 0x1f;
  255. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  256. (*nip & 0x100)? "OUT to": "IN from",
  257. regs->gpr[rb] - _IO_BASE, nip);
  258. regs->msr |= MSR_RI;
  259. regs->nip = entry->fixup;
  260. return 1;
  261. }
  262. }
  263. #endif /* CONFIG_PPC_PMAC */
  264. return 0;
  265. }
  266. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  267. /* On 4xx, the reason for the machine check or program exception
  268. is in the ESR. */
  269. #define get_reason(regs) ((regs)->dsisr)
  270. #ifndef CONFIG_FSL_BOOKE
  271. #define get_mc_reason(regs) ((regs)->dsisr)
  272. #else
  273. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  274. #endif
  275. #define REASON_FP ESR_FP
  276. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  277. #define REASON_PRIVILEGED ESR_PPR
  278. #define REASON_TRAP ESR_PTR
  279. /* single-step stuff */
  280. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  281. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  282. #else
  283. /* On non-4xx, the reason for the machine check or program
  284. exception is in the MSR. */
  285. #define get_reason(regs) ((regs)->msr)
  286. #define get_mc_reason(regs) ((regs)->msr)
  287. #define REASON_FP 0x100000
  288. #define REASON_ILLEGAL 0x80000
  289. #define REASON_PRIVILEGED 0x40000
  290. #define REASON_TRAP 0x20000
  291. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  292. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  293. #endif
  294. /*
  295. * This is "fall-back" implementation for configurations
  296. * which don't provide platform-specific machine check info
  297. */
  298. void __attribute__ ((weak))
  299. platform_machine_check(struct pt_regs *regs)
  300. {
  301. }
  302. void machine_check_exception(struct pt_regs *regs)
  303. {
  304. #ifdef CONFIG_PPC64
  305. int recover = 0;
  306. /* See if any machine dependent calls */
  307. if (ppc_md.machine_check_exception)
  308. recover = ppc_md.machine_check_exception(regs);
  309. if (recover)
  310. return;
  311. #else
  312. unsigned long reason = get_mc_reason(regs);
  313. if (user_mode(regs)) {
  314. regs->msr |= MSR_RI;
  315. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  316. return;
  317. }
  318. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  319. /* the qspan pci read routines can cause machine checks -- Cort */
  320. bad_page_fault(regs, regs->dar, SIGBUS);
  321. return;
  322. #endif
  323. if (debugger_fault_handler(regs)) {
  324. regs->msr |= MSR_RI;
  325. return;
  326. }
  327. if (check_io_access(regs))
  328. return;
  329. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  330. if (reason & ESR_IMCP) {
  331. printk("Instruction");
  332. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  333. } else
  334. printk("Data");
  335. printk(" machine check in kernel mode.\n");
  336. #elif defined(CONFIG_440A)
  337. printk("Machine check in kernel mode.\n");
  338. if (reason & ESR_IMCP){
  339. printk("Instruction Synchronous Machine Check exception\n");
  340. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  341. }
  342. else {
  343. u32 mcsr = mfspr(SPRN_MCSR);
  344. if (mcsr & MCSR_IB)
  345. printk("Instruction Read PLB Error\n");
  346. if (mcsr & MCSR_DRB)
  347. printk("Data Read PLB Error\n");
  348. if (mcsr & MCSR_DWB)
  349. printk("Data Write PLB Error\n");
  350. if (mcsr & MCSR_TLBP)
  351. printk("TLB Parity Error\n");
  352. if (mcsr & MCSR_ICP){
  353. flush_instruction_cache();
  354. printk("I-Cache Parity Error\n");
  355. }
  356. if (mcsr & MCSR_DCSP)
  357. printk("D-Cache Search Parity Error\n");
  358. if (mcsr & MCSR_DCFP)
  359. printk("D-Cache Flush Parity Error\n");
  360. if (mcsr & MCSR_IMPE)
  361. printk("Machine Check exception is imprecise\n");
  362. /* Clear MCSR */
  363. mtspr(SPRN_MCSR, mcsr);
  364. }
  365. #elif defined (CONFIG_E500)
  366. printk("Machine check in kernel mode.\n");
  367. printk("Caused by (from MCSR=%lx): ", reason);
  368. if (reason & MCSR_MCP)
  369. printk("Machine Check Signal\n");
  370. if (reason & MCSR_ICPERR)
  371. printk("Instruction Cache Parity Error\n");
  372. if (reason & MCSR_DCP_PERR)
  373. printk("Data Cache Push Parity Error\n");
  374. if (reason & MCSR_DCPERR)
  375. printk("Data Cache Parity Error\n");
  376. if (reason & MCSR_GL_CI)
  377. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  378. if (reason & MCSR_BUS_IAERR)
  379. printk("Bus - Instruction Address Error\n");
  380. if (reason & MCSR_BUS_RAERR)
  381. printk("Bus - Read Address Error\n");
  382. if (reason & MCSR_BUS_WAERR)
  383. printk("Bus - Write Address Error\n");
  384. if (reason & MCSR_BUS_IBERR)
  385. printk("Bus - Instruction Data Error\n");
  386. if (reason & MCSR_BUS_RBERR)
  387. printk("Bus - Read Data Bus Error\n");
  388. if (reason & MCSR_BUS_WBERR)
  389. printk("Bus - Read Data Bus Error\n");
  390. if (reason & MCSR_BUS_IPERR)
  391. printk("Bus - Instruction Parity Error\n");
  392. if (reason & MCSR_BUS_RPERR)
  393. printk("Bus - Read Parity Error\n");
  394. #elif defined (CONFIG_E200)
  395. printk("Machine check in kernel mode.\n");
  396. printk("Caused by (from MCSR=%lx): ", reason);
  397. if (reason & MCSR_MCP)
  398. printk("Machine Check Signal\n");
  399. if (reason & MCSR_CP_PERR)
  400. printk("Cache Push Parity Error\n");
  401. if (reason & MCSR_CPERR)
  402. printk("Cache Parity Error\n");
  403. if (reason & MCSR_EXCP_ERR)
  404. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  405. if (reason & MCSR_BUS_IRERR)
  406. printk("Bus - Read Bus Error on instruction fetch\n");
  407. if (reason & MCSR_BUS_DRERR)
  408. printk("Bus - Read Bus Error on data load\n");
  409. if (reason & MCSR_BUS_WRERR)
  410. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  411. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  412. printk("Machine check in kernel mode.\n");
  413. printk("Caused by (from SRR1=%lx): ", reason);
  414. switch (reason & 0x601F0000) {
  415. case 0x80000:
  416. printk("Machine check signal\n");
  417. break;
  418. case 0: /* for 601 */
  419. case 0x40000:
  420. case 0x140000: /* 7450 MSS error and TEA */
  421. printk("Transfer error ack signal\n");
  422. break;
  423. case 0x20000:
  424. printk("Data parity error signal\n");
  425. break;
  426. case 0x10000:
  427. printk("Address parity error signal\n");
  428. break;
  429. case 0x20000000:
  430. printk("L1 Data Cache error\n");
  431. break;
  432. case 0x40000000:
  433. printk("L1 Instruction Cache error\n");
  434. break;
  435. case 0x00100000:
  436. printk("L2 data cache parity error\n");
  437. break;
  438. default:
  439. printk("Unknown values in msr\n");
  440. }
  441. #endif /* CONFIG_4xx */
  442. /*
  443. * Optional platform-provided routine to print out
  444. * additional info, e.g. bus error registers.
  445. */
  446. platform_machine_check(regs);
  447. #endif /* CONFIG_PPC64 */
  448. if (debugger_fault_handler(regs))
  449. return;
  450. die("Machine check", regs, SIGBUS);
  451. /* Must die if the interrupt is not recoverable */
  452. if (!(regs->msr & MSR_RI))
  453. panic("Unrecoverable Machine check");
  454. }
  455. void SMIException(struct pt_regs *regs)
  456. {
  457. die("System Management Interrupt", regs, SIGABRT);
  458. }
  459. void unknown_exception(struct pt_regs *regs)
  460. {
  461. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  462. regs->nip, regs->msr, regs->trap);
  463. _exception(SIGTRAP, regs, 0, 0);
  464. }
  465. void instruction_breakpoint_exception(struct pt_regs *regs)
  466. {
  467. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  468. 5, SIGTRAP) == NOTIFY_STOP)
  469. return;
  470. if (debugger_iabr_match(regs))
  471. return;
  472. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  473. }
  474. void RunModeException(struct pt_regs *regs)
  475. {
  476. _exception(SIGTRAP, regs, 0, 0);
  477. }
  478. void __kprobes single_step_exception(struct pt_regs *regs)
  479. {
  480. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  481. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  482. 5, SIGTRAP) == NOTIFY_STOP)
  483. return;
  484. if (debugger_sstep(regs))
  485. return;
  486. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  487. }
  488. /*
  489. * After we have successfully emulated an instruction, we have to
  490. * check if the instruction was being single-stepped, and if so,
  491. * pretend we got a single-step exception. This was pointed out
  492. * by Kumar Gala. -- paulus
  493. */
  494. static void emulate_single_step(struct pt_regs *regs)
  495. {
  496. if (single_stepping(regs)) {
  497. clear_single_step(regs);
  498. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  499. }
  500. }
  501. static void parse_fpe(struct pt_regs *regs)
  502. {
  503. int code = 0;
  504. unsigned long fpscr;
  505. flush_fp_to_thread(current);
  506. fpscr = current->thread.fpscr.val;
  507. /* Invalid operation */
  508. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  509. code = FPE_FLTINV;
  510. /* Overflow */
  511. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  512. code = FPE_FLTOVF;
  513. /* Underflow */
  514. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  515. code = FPE_FLTUND;
  516. /* Divide by zero */
  517. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  518. code = FPE_FLTDIV;
  519. /* Inexact result */
  520. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  521. code = FPE_FLTRES;
  522. _exception(SIGFPE, regs, code, regs->nip);
  523. }
  524. /*
  525. * Illegal instruction emulation support. Originally written to
  526. * provide the PVR to user applications using the mfspr rd, PVR.
  527. * Return non-zero if we can't emulate, or -EFAULT if the associated
  528. * memory access caused an access fault. Return zero on success.
  529. *
  530. * There are a couple of ways to do this, either "decode" the instruction
  531. * or directly match lots of bits. In this case, matching lots of
  532. * bits is faster and easier.
  533. *
  534. */
  535. #define INST_MFSPR_PVR 0x7c1f42a6
  536. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  537. #define INST_DCBA 0x7c0005ec
  538. #define INST_DCBA_MASK 0x7c0007fe
  539. #define INST_MCRXR 0x7c000400
  540. #define INST_MCRXR_MASK 0x7c0007fe
  541. #define INST_STRING 0x7c00042a
  542. #define INST_STRING_MASK 0x7c0007fe
  543. #define INST_STRING_GEN_MASK 0x7c00067e
  544. #define INST_LSWI 0x7c0004aa
  545. #define INST_LSWX 0x7c00042a
  546. #define INST_STSWI 0x7c0005aa
  547. #define INST_STSWX 0x7c00052a
  548. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  549. {
  550. u8 rT = (instword >> 21) & 0x1f;
  551. u8 rA = (instword >> 16) & 0x1f;
  552. u8 NB_RB = (instword >> 11) & 0x1f;
  553. u32 num_bytes;
  554. unsigned long EA;
  555. int pos = 0;
  556. /* Early out if we are an invalid form of lswx */
  557. if ((instword & INST_STRING_MASK) == INST_LSWX)
  558. if ((rT == rA) || (rT == NB_RB))
  559. return -EINVAL;
  560. EA = (rA == 0) ? 0 : regs->gpr[rA];
  561. switch (instword & INST_STRING_MASK) {
  562. case INST_LSWX:
  563. case INST_STSWX:
  564. EA += NB_RB;
  565. num_bytes = regs->xer & 0x7f;
  566. break;
  567. case INST_LSWI:
  568. case INST_STSWI:
  569. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  570. break;
  571. default:
  572. return -EINVAL;
  573. }
  574. while (num_bytes != 0)
  575. {
  576. u8 val;
  577. u32 shift = 8 * (3 - (pos & 0x3));
  578. switch ((instword & INST_STRING_MASK)) {
  579. case INST_LSWX:
  580. case INST_LSWI:
  581. if (get_user(val, (u8 __user *)EA))
  582. return -EFAULT;
  583. /* first time updating this reg,
  584. * zero it out */
  585. if (pos == 0)
  586. regs->gpr[rT] = 0;
  587. regs->gpr[rT] |= val << shift;
  588. break;
  589. case INST_STSWI:
  590. case INST_STSWX:
  591. val = regs->gpr[rT] >> shift;
  592. if (put_user(val, (u8 __user *)EA))
  593. return -EFAULT;
  594. break;
  595. }
  596. /* move EA to next address */
  597. EA += 1;
  598. num_bytes--;
  599. /* manage our position within the register */
  600. if (++pos == 4) {
  601. pos = 0;
  602. if (++rT == 32)
  603. rT = 0;
  604. }
  605. }
  606. return 0;
  607. }
  608. static int emulate_instruction(struct pt_regs *regs)
  609. {
  610. u32 instword;
  611. u32 rd;
  612. if (!user_mode(regs))
  613. return -EINVAL;
  614. CHECK_FULL_REGS(regs);
  615. if (get_user(instword, (u32 __user *)(regs->nip)))
  616. return -EFAULT;
  617. /* Emulate the mfspr rD, PVR. */
  618. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  619. rd = (instword >> 21) & 0x1f;
  620. regs->gpr[rd] = mfspr(SPRN_PVR);
  621. return 0;
  622. }
  623. /* Emulating the dcba insn is just a no-op. */
  624. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  625. return 0;
  626. /* Emulate the mcrxr insn. */
  627. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  628. int shift = (instword >> 21) & 0x1c;
  629. unsigned long msk = 0xf0000000UL >> shift;
  630. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  631. regs->xer &= ~0xf0000000UL;
  632. return 0;
  633. }
  634. /* Emulate load/store string insn. */
  635. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  636. return emulate_string_inst(regs, instword);
  637. return -EINVAL;
  638. }
  639. /*
  640. * Look through the list of trap instructions that are used for BUG(),
  641. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  642. * that the exception was caused by a trap instruction of some kind.
  643. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  644. * otherwise.
  645. */
  646. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  647. #ifndef CONFIG_MODULES
  648. #define module_find_bug(x) NULL
  649. #endif
  650. struct bug_entry *find_bug(unsigned long bugaddr)
  651. {
  652. struct bug_entry *bug;
  653. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  654. if (bugaddr == bug->bug_addr)
  655. return bug;
  656. return module_find_bug(bugaddr);
  657. }
  658. static int check_bug_trap(struct pt_regs *regs)
  659. {
  660. struct bug_entry *bug;
  661. unsigned long addr;
  662. if (regs->msr & MSR_PR)
  663. return 0; /* not in kernel */
  664. addr = regs->nip; /* address of trap instruction */
  665. if (addr < PAGE_OFFSET)
  666. return 0;
  667. bug = find_bug(regs->nip);
  668. if (bug == NULL)
  669. return 0;
  670. if (bug->line & BUG_WARNING_TRAP) {
  671. /* this is a WARN_ON rather than BUG/BUG_ON */
  672. printk(KERN_ERR "Badness in %s at %s:%ld\n",
  673. bug->function, bug->file,
  674. bug->line & ~BUG_WARNING_TRAP);
  675. dump_stack();
  676. return 1;
  677. }
  678. printk(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",
  679. bug->function, bug->file, bug->line);
  680. return 0;
  681. }
  682. void __kprobes program_check_exception(struct pt_regs *regs)
  683. {
  684. unsigned int reason = get_reason(regs);
  685. extern int do_mathemu(struct pt_regs *regs);
  686. #ifdef CONFIG_MATH_EMULATION
  687. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  688. * but there seems to be a hardware bug on the 405GP (RevD)
  689. * that means ESR is sometimes set incorrectly - either to
  690. * ESR_DST (!?) or 0. In the process of chasing this with the
  691. * hardware people - not sure if it can happen on any illegal
  692. * instruction or only on FP instructions, whether there is a
  693. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  694. if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
  695. emulate_single_step(regs);
  696. return;
  697. }
  698. #endif /* CONFIG_MATH_EMULATION */
  699. if (reason & REASON_FP) {
  700. /* IEEE FP exception */
  701. parse_fpe(regs);
  702. return;
  703. }
  704. if (reason & REASON_TRAP) {
  705. /* trap exception */
  706. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  707. == NOTIFY_STOP)
  708. return;
  709. if (debugger_bpt(regs))
  710. return;
  711. if (check_bug_trap(regs)) {
  712. regs->nip += 4;
  713. return;
  714. }
  715. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  716. return;
  717. }
  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 fixed;
  738. fixed = fix_alignment(regs);
  739. if (fixed == 1) {
  740. regs->nip += 4; /* skip over emulated instruction */
  741. emulate_single_step(regs);
  742. return;
  743. }
  744. /* Operand address was bad */
  745. if (fixed == -EFAULT) {
  746. if (user_mode(regs))
  747. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
  748. else
  749. /* Search exception table */
  750. bad_page_fault(regs, regs->dar, SIGSEGV);
  751. return;
  752. }
  753. _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
  754. }
  755. void StackOverflow(struct pt_regs *regs)
  756. {
  757. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  758. current, regs->gpr[1]);
  759. debugger(regs);
  760. show_regs(regs);
  761. panic("kernel stack overflow");
  762. }
  763. void nonrecoverable_exception(struct pt_regs *regs)
  764. {
  765. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  766. regs->nip, regs->msr);
  767. debugger(regs);
  768. die("nonrecoverable exception", regs, SIGKILL);
  769. }
  770. void trace_syscall(struct pt_regs *regs)
  771. {
  772. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  773. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  774. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  775. }
  776. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  777. {
  778. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  779. "%lx at %lx\n", regs->trap, regs->nip);
  780. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  781. }
  782. void altivec_unavailable_exception(struct pt_regs *regs)
  783. {
  784. #if !defined(CONFIG_ALTIVEC)
  785. if (user_mode(regs)) {
  786. /* A user program has executed an altivec instruction,
  787. but this kernel doesn't support altivec. */
  788. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  789. return;
  790. }
  791. #endif
  792. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  793. "%lx at %lx\n", regs->trap, regs->nip);
  794. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  795. }
  796. void performance_monitor_exception(struct pt_regs *regs)
  797. {
  798. perf_irq(regs);
  799. }
  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. }