traps.c 27 KB

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