traps.c 27 KB

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