traps.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. #ifdef CONFIG_PPC_PMAC
  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 */
  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. #ifdef CONFIG_PPC64
  275. int recover = 0;
  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. #else
  282. unsigned long reason = get_mc_reason(regs);
  283. if (user_mode(regs)) {
  284. regs->msr |= MSR_RI;
  285. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  286. return;
  287. }
  288. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  289. /* the qspan pci read routines can cause machine checks -- Cort */
  290. bad_page_fault(regs, regs->dar, SIGBUS);
  291. return;
  292. #endif
  293. if (debugger_fault_handler(regs)) {
  294. regs->msr |= MSR_RI;
  295. return;
  296. }
  297. if (check_io_access(regs))
  298. return;
  299. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  300. if (reason & ESR_IMCP) {
  301. printk("Instruction");
  302. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  303. } else
  304. printk("Data");
  305. printk(" machine check in kernel mode.\n");
  306. #elif defined(CONFIG_440A)
  307. printk("Machine check in kernel mode.\n");
  308. if (reason & ESR_IMCP){
  309. printk("Instruction Synchronous Machine Check exception\n");
  310. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  311. }
  312. else {
  313. u32 mcsr = mfspr(SPRN_MCSR);
  314. if (mcsr & MCSR_IB)
  315. printk("Instruction Read PLB Error\n");
  316. if (mcsr & MCSR_DRB)
  317. printk("Data Read PLB Error\n");
  318. if (mcsr & MCSR_DWB)
  319. printk("Data Write PLB Error\n");
  320. if (mcsr & MCSR_TLBP)
  321. printk("TLB Parity Error\n");
  322. if (mcsr & MCSR_ICP){
  323. flush_instruction_cache();
  324. printk("I-Cache Parity Error\n");
  325. }
  326. if (mcsr & MCSR_DCSP)
  327. printk("D-Cache Search Parity Error\n");
  328. if (mcsr & MCSR_DCFP)
  329. printk("D-Cache Flush Parity Error\n");
  330. if (mcsr & MCSR_IMPE)
  331. printk("Machine Check exception is imprecise\n");
  332. /* Clear MCSR */
  333. mtspr(SPRN_MCSR, mcsr);
  334. }
  335. #elif defined (CONFIG_E500)
  336. printk("Machine check in kernel mode.\n");
  337. printk("Caused by (from MCSR=%lx): ", reason);
  338. if (reason & MCSR_MCP)
  339. printk("Machine Check Signal\n");
  340. if (reason & MCSR_ICPERR)
  341. printk("Instruction Cache Parity Error\n");
  342. if (reason & MCSR_DCP_PERR)
  343. printk("Data Cache Push Parity Error\n");
  344. if (reason & MCSR_DCPERR)
  345. printk("Data Cache Parity Error\n");
  346. if (reason & MCSR_GL_CI)
  347. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  348. if (reason & MCSR_BUS_IAERR)
  349. printk("Bus - Instruction Address Error\n");
  350. if (reason & MCSR_BUS_RAERR)
  351. printk("Bus - Read Address Error\n");
  352. if (reason & MCSR_BUS_WAERR)
  353. printk("Bus - Write Address Error\n");
  354. if (reason & MCSR_BUS_IBERR)
  355. printk("Bus - Instruction Data Error\n");
  356. if (reason & MCSR_BUS_RBERR)
  357. printk("Bus - Read Data Bus Error\n");
  358. if (reason & MCSR_BUS_WBERR)
  359. printk("Bus - Read Data Bus Error\n");
  360. if (reason & MCSR_BUS_IPERR)
  361. printk("Bus - Instruction Parity Error\n");
  362. if (reason & MCSR_BUS_RPERR)
  363. printk("Bus - Read Parity Error\n");
  364. #elif defined (CONFIG_E200)
  365. printk("Machine check in kernel mode.\n");
  366. printk("Caused by (from MCSR=%lx): ", reason);
  367. if (reason & MCSR_MCP)
  368. printk("Machine Check Signal\n");
  369. if (reason & MCSR_CP_PERR)
  370. printk("Cache Push Parity Error\n");
  371. if (reason & MCSR_CPERR)
  372. printk("Cache Parity Error\n");
  373. if (reason & MCSR_EXCP_ERR)
  374. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  375. if (reason & MCSR_BUS_IRERR)
  376. printk("Bus - Read Bus Error on instruction fetch\n");
  377. if (reason & MCSR_BUS_DRERR)
  378. printk("Bus - Read Bus Error on data load\n");
  379. if (reason & MCSR_BUS_WRERR)
  380. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  381. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  382. printk("Machine check in kernel mode.\n");
  383. printk("Caused by (from SRR1=%lx): ", reason);
  384. switch (reason & 0x601F0000) {
  385. case 0x80000:
  386. printk("Machine check signal\n");
  387. break;
  388. case 0: /* for 601 */
  389. case 0x40000:
  390. case 0x140000: /* 7450 MSS error and TEA */
  391. printk("Transfer error ack signal\n");
  392. break;
  393. case 0x20000:
  394. printk("Data parity error signal\n");
  395. break;
  396. case 0x10000:
  397. printk("Address parity error signal\n");
  398. break;
  399. case 0x20000000:
  400. printk("L1 Data Cache error\n");
  401. break;
  402. case 0x40000000:
  403. printk("L1 Instruction Cache error\n");
  404. break;
  405. case 0x00100000:
  406. printk("L2 data cache parity error\n");
  407. break;
  408. default:
  409. printk("Unknown values in msr\n");
  410. }
  411. #endif /* CONFIG_4xx */
  412. /*
  413. * Optional platform-provided routine to print out
  414. * additional info, e.g. bus error registers.
  415. */
  416. platform_machine_check(regs);
  417. #endif /* CONFIG_PPC64 */
  418. if (debugger_fault_handler(regs))
  419. return;
  420. die("Machine check", regs, SIGBUS);
  421. /* Must die if the interrupt is not recoverable */
  422. if (!(regs->msr & MSR_RI))
  423. panic("Unrecoverable Machine check");
  424. }
  425. void SMIException(struct pt_regs *regs)
  426. {
  427. die("System Management Interrupt", regs, SIGABRT);
  428. }
  429. void unknown_exception(struct pt_regs *regs)
  430. {
  431. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  432. regs->nip, regs->msr, regs->trap);
  433. _exception(SIGTRAP, regs, 0, 0);
  434. }
  435. void instruction_breakpoint_exception(struct pt_regs *regs)
  436. {
  437. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  438. 5, SIGTRAP) == NOTIFY_STOP)
  439. return;
  440. if (debugger_iabr_match(regs))
  441. return;
  442. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  443. }
  444. void RunModeException(struct pt_regs *regs)
  445. {
  446. _exception(SIGTRAP, regs, 0, 0);
  447. }
  448. void __kprobes single_step_exception(struct pt_regs *regs)
  449. {
  450. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  451. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  452. 5, SIGTRAP) == NOTIFY_STOP)
  453. return;
  454. if (debugger_sstep(regs))
  455. return;
  456. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  457. }
  458. /*
  459. * After we have successfully emulated an instruction, we have to
  460. * check if the instruction was being single-stepped, and if so,
  461. * pretend we got a single-step exception. This was pointed out
  462. * by Kumar Gala. -- paulus
  463. */
  464. static void emulate_single_step(struct pt_regs *regs)
  465. {
  466. if (single_stepping(regs)) {
  467. clear_single_step(regs);
  468. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  469. }
  470. }
  471. static void parse_fpe(struct pt_regs *regs)
  472. {
  473. int code = 0;
  474. unsigned long fpscr;
  475. flush_fp_to_thread(current);
  476. fpscr = current->thread.fpscr.val;
  477. /* Invalid operation */
  478. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  479. code = FPE_FLTINV;
  480. /* Overflow */
  481. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  482. code = FPE_FLTOVF;
  483. /* Underflow */
  484. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  485. code = FPE_FLTUND;
  486. /* Divide by zero */
  487. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  488. code = FPE_FLTDIV;
  489. /* Inexact result */
  490. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  491. code = FPE_FLTRES;
  492. _exception(SIGFPE, regs, code, regs->nip);
  493. }
  494. /*
  495. * Illegal instruction emulation support. Originally written to
  496. * provide the PVR to user applications using the mfspr rd, PVR.
  497. * Return non-zero if we can't emulate, or -EFAULT if the associated
  498. * memory access caused an access fault. Return zero on success.
  499. *
  500. * There are a couple of ways to do this, either "decode" the instruction
  501. * or directly match lots of bits. In this case, matching lots of
  502. * bits is faster and easier.
  503. *
  504. */
  505. #define INST_MFSPR_PVR 0x7c1f42a6
  506. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  507. #define INST_DCBA 0x7c0005ec
  508. #define INST_DCBA_MASK 0x7c0007fe
  509. #define INST_MCRXR 0x7c000400
  510. #define INST_MCRXR_MASK 0x7c0007fe
  511. #define INST_STRING 0x7c00042a
  512. #define INST_STRING_MASK 0x7c0007fe
  513. #define INST_STRING_GEN_MASK 0x7c00067e
  514. #define INST_LSWI 0x7c0004aa
  515. #define INST_LSWX 0x7c00042a
  516. #define INST_STSWI 0x7c0005aa
  517. #define INST_STSWX 0x7c00052a
  518. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  519. {
  520. u8 rT = (instword >> 21) & 0x1f;
  521. u8 rA = (instword >> 16) & 0x1f;
  522. u8 NB_RB = (instword >> 11) & 0x1f;
  523. u32 num_bytes;
  524. unsigned long EA;
  525. int pos = 0;
  526. /* Early out if we are an invalid form of lswx */
  527. if ((instword & INST_STRING_MASK) == INST_LSWX)
  528. if ((rT == rA) || (rT == NB_RB))
  529. return -EINVAL;
  530. EA = (rA == 0) ? 0 : regs->gpr[rA];
  531. switch (instword & INST_STRING_MASK) {
  532. case INST_LSWX:
  533. case INST_STSWX:
  534. EA += NB_RB;
  535. num_bytes = regs->xer & 0x7f;
  536. break;
  537. case INST_LSWI:
  538. case INST_STSWI:
  539. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. while (num_bytes != 0)
  545. {
  546. u8 val;
  547. u32 shift = 8 * (3 - (pos & 0x3));
  548. switch ((instword & INST_STRING_MASK)) {
  549. case INST_LSWX:
  550. case INST_LSWI:
  551. if (get_user(val, (u8 __user *)EA))
  552. return -EFAULT;
  553. /* first time updating this reg,
  554. * zero it out */
  555. if (pos == 0)
  556. regs->gpr[rT] = 0;
  557. regs->gpr[rT] |= val << shift;
  558. break;
  559. case INST_STSWI:
  560. case INST_STSWX:
  561. val = regs->gpr[rT] >> shift;
  562. if (put_user(val, (u8 __user *)EA))
  563. return -EFAULT;
  564. break;
  565. }
  566. /* move EA to next address */
  567. EA += 1;
  568. num_bytes--;
  569. /* manage our position within the register */
  570. if (++pos == 4) {
  571. pos = 0;
  572. if (++rT == 32)
  573. rT = 0;
  574. }
  575. }
  576. return 0;
  577. }
  578. static int emulate_instruction(struct pt_regs *regs)
  579. {
  580. u32 instword;
  581. u32 rd;
  582. if (!user_mode(regs))
  583. return -EINVAL;
  584. CHECK_FULL_REGS(regs);
  585. if (get_user(instword, (u32 __user *)(regs->nip)))
  586. return -EFAULT;
  587. /* Emulate the mfspr rD, PVR. */
  588. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  589. rd = (instword >> 21) & 0x1f;
  590. regs->gpr[rd] = mfspr(SPRN_PVR);
  591. return 0;
  592. }
  593. /* Emulating the dcba insn is just a no-op. */
  594. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  595. return 0;
  596. /* Emulate the mcrxr insn. */
  597. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  598. int shift = (instword >> 21) & 0x1c;
  599. unsigned long msk = 0xf0000000UL >> shift;
  600. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  601. regs->xer &= ~0xf0000000UL;
  602. return 0;
  603. }
  604. /* Emulate load/store string insn. */
  605. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  606. return emulate_string_inst(regs, instword);
  607. return -EINVAL;
  608. }
  609. /*
  610. * Look through the list of trap instructions that are used for BUG(),
  611. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  612. * that the exception was caused by a trap instruction of some kind.
  613. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  614. * otherwise.
  615. */
  616. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  617. #ifndef CONFIG_MODULES
  618. #define module_find_bug(x) NULL
  619. #endif
  620. struct bug_entry *find_bug(unsigned long bugaddr)
  621. {
  622. struct bug_entry *bug;
  623. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  624. if (bugaddr == bug->bug_addr)
  625. return bug;
  626. return module_find_bug(bugaddr);
  627. }
  628. static int check_bug_trap(struct pt_regs *regs)
  629. {
  630. struct bug_entry *bug;
  631. unsigned long addr;
  632. if (regs->msr & MSR_PR)
  633. return 0; /* not in kernel */
  634. addr = regs->nip; /* address of trap instruction */
  635. if (addr < PAGE_OFFSET)
  636. return 0;
  637. bug = find_bug(regs->nip);
  638. if (bug == NULL)
  639. return 0;
  640. if (bug->line & BUG_WARNING_TRAP) {
  641. /* this is a WARN_ON rather than BUG/BUG_ON */
  642. printk(KERN_ERR "Badness in %s at %s:%ld\n",
  643. bug->function, bug->file,
  644. bug->line & ~BUG_WARNING_TRAP);
  645. dump_stack();
  646. return 1;
  647. }
  648. printk(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",
  649. bug->function, bug->file, bug->line);
  650. return 0;
  651. }
  652. void __kprobes program_check_exception(struct pt_regs *regs)
  653. {
  654. unsigned int reason = get_reason(regs);
  655. extern int do_mathemu(struct pt_regs *regs);
  656. #ifdef CONFIG_MATH_EMULATION
  657. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  658. * but there seems to be a hardware bug on the 405GP (RevD)
  659. * that means ESR is sometimes set incorrectly - either to
  660. * ESR_DST (!?) or 0. In the process of chasing this with the
  661. * hardware people - not sure if it can happen on any illegal
  662. * instruction or only on FP instructions, whether there is a
  663. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  664. if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
  665. emulate_single_step(regs);
  666. return;
  667. }
  668. #endif /* CONFIG_MATH_EMULATION */
  669. if (reason & REASON_FP) {
  670. /* IEEE FP exception */
  671. parse_fpe(regs);
  672. return;
  673. }
  674. if (reason & REASON_TRAP) {
  675. /* trap exception */
  676. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  677. == NOTIFY_STOP)
  678. return;
  679. if (debugger_bpt(regs))
  680. return;
  681. if (check_bug_trap(regs)) {
  682. regs->nip += 4;
  683. return;
  684. }
  685. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  686. return;
  687. }
  688. local_irq_enable();
  689. /* Try to emulate it if we should. */
  690. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  691. switch (emulate_instruction(regs)) {
  692. case 0:
  693. regs->nip += 4;
  694. emulate_single_step(regs);
  695. return;
  696. case -EFAULT:
  697. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  698. return;
  699. }
  700. }
  701. if (reason & REASON_PRIVILEGED)
  702. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  703. else
  704. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  705. }
  706. void alignment_exception(struct pt_regs *regs)
  707. {
  708. int fixed;
  709. fixed = fix_alignment(regs);
  710. if (fixed == 1) {
  711. regs->nip += 4; /* skip over emulated instruction */
  712. emulate_single_step(regs);
  713. return;
  714. }
  715. /* Operand address was bad */
  716. if (fixed == -EFAULT) {
  717. if (user_mode(regs))
  718. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
  719. else
  720. /* Search exception table */
  721. bad_page_fault(regs, regs->dar, SIGSEGV);
  722. return;
  723. }
  724. _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
  725. }
  726. void StackOverflow(struct pt_regs *regs)
  727. {
  728. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  729. current, regs->gpr[1]);
  730. debugger(regs);
  731. show_regs(regs);
  732. panic("kernel stack overflow");
  733. }
  734. void nonrecoverable_exception(struct pt_regs *regs)
  735. {
  736. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  737. regs->nip, regs->msr);
  738. debugger(regs);
  739. die("nonrecoverable exception", regs, SIGKILL);
  740. }
  741. void trace_syscall(struct pt_regs *regs)
  742. {
  743. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  744. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  745. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  746. }
  747. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  748. {
  749. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  750. "%lx at %lx\n", regs->trap, regs->nip);
  751. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  752. }
  753. void altivec_unavailable_exception(struct pt_regs *regs)
  754. {
  755. #if !defined(CONFIG_ALTIVEC)
  756. if (user_mode(regs)) {
  757. /* A user program has executed an altivec instruction,
  758. but this kernel doesn't support altivec. */
  759. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  760. return;
  761. }
  762. #endif
  763. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  764. "%lx at %lx\n", regs->trap, regs->nip);
  765. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  766. }
  767. void performance_monitor_exception(struct pt_regs *regs)
  768. {
  769. perf_irq(regs);
  770. }
  771. #ifdef CONFIG_8xx
  772. void SoftwareEmulation(struct pt_regs *regs)
  773. {
  774. extern int do_mathemu(struct pt_regs *);
  775. extern int Soft_emulate_8xx(struct pt_regs *);
  776. int errcode;
  777. CHECK_FULL_REGS(regs);
  778. if (!user_mode(regs)) {
  779. debugger(regs);
  780. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  781. }
  782. #ifdef CONFIG_MATH_EMULATION
  783. errcode = do_mathemu(regs);
  784. #else
  785. errcode = Soft_emulate_8xx(regs);
  786. #endif
  787. if (errcode) {
  788. if (errcode > 0)
  789. _exception(SIGFPE, regs, 0, 0);
  790. else if (errcode == -EFAULT)
  791. _exception(SIGSEGV, regs, 0, 0);
  792. else
  793. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  794. } else
  795. emulate_single_step(regs);
  796. }
  797. #endif /* CONFIG_8xx */
  798. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  799. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  800. {
  801. if (debug_status & DBSR_IC) { /* instruction completion */
  802. regs->msr &= ~MSR_DE;
  803. if (user_mode(regs)) {
  804. current->thread.dbcr0 &= ~DBCR0_IC;
  805. } else {
  806. /* Disable instruction completion */
  807. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  808. /* Clear the instruction completion event */
  809. mtspr(SPRN_DBSR, DBSR_IC);
  810. if (debugger_sstep(regs))
  811. return;
  812. }
  813. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  814. }
  815. }
  816. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  817. #if !defined(CONFIG_TAU_INT)
  818. void TAUException(struct pt_regs *regs)
  819. {
  820. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  821. regs->nip, regs->msr, regs->trap, print_tainted());
  822. }
  823. #endif /* CONFIG_INT_TAU */
  824. #ifdef CONFIG_ALTIVEC
  825. void altivec_assist_exception(struct pt_regs *regs)
  826. {
  827. int err;
  828. if (!user_mode(regs)) {
  829. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  830. " at %lx\n", regs->nip);
  831. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  832. }
  833. flush_altivec_to_thread(current);
  834. err = emulate_altivec(regs);
  835. if (err == 0) {
  836. regs->nip += 4; /* skip emulated instruction */
  837. emulate_single_step(regs);
  838. return;
  839. }
  840. if (err == -EFAULT) {
  841. /* got an error reading the instruction */
  842. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  843. } else {
  844. /* didn't recognize the instruction */
  845. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  846. if (printk_ratelimit())
  847. printk(KERN_ERR "Unrecognized altivec instruction "
  848. "in %s at %lx\n", current->comm, regs->nip);
  849. current->thread.vscr.u[3] |= 0x10000;
  850. }
  851. }
  852. #endif /* CONFIG_ALTIVEC */
  853. #ifdef CONFIG_FSL_BOOKE
  854. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  855. unsigned long error_code)
  856. {
  857. /* We treat cache locking instructions from the user
  858. * as priv ops, in the future we could try to do
  859. * something smarter
  860. */
  861. if (error_code & (ESR_DLK|ESR_ILK))
  862. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  863. return;
  864. }
  865. #endif /* CONFIG_FSL_BOOKE */
  866. #ifdef CONFIG_SPE
  867. void SPEFloatingPointException(struct pt_regs *regs)
  868. {
  869. unsigned long spefscr;
  870. int fpexc_mode;
  871. int code = 0;
  872. spefscr = current->thread.spefscr;
  873. fpexc_mode = current->thread.fpexc_mode;
  874. /* Hardware does not neccessarily set sticky
  875. * underflow/overflow/invalid flags */
  876. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  877. code = FPE_FLTOVF;
  878. spefscr |= SPEFSCR_FOVFS;
  879. }
  880. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  881. code = FPE_FLTUND;
  882. spefscr |= SPEFSCR_FUNFS;
  883. }
  884. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  885. code = FPE_FLTDIV;
  886. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  887. code = FPE_FLTINV;
  888. spefscr |= SPEFSCR_FINVS;
  889. }
  890. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  891. code = FPE_FLTRES;
  892. current->thread.spefscr = spefscr;
  893. _exception(SIGFPE, regs, code, regs->nip);
  894. return;
  895. }
  896. #endif
  897. /*
  898. * We enter here if we get an unrecoverable exception, that is, one
  899. * that happened at a point where the RI (recoverable interrupt) bit
  900. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  901. * we therefore lost state by taking this exception.
  902. */
  903. void unrecoverable_exception(struct pt_regs *regs)
  904. {
  905. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  906. regs->trap, regs->nip);
  907. die("Unrecoverable exception", regs, SIGABRT);
  908. }
  909. #ifdef CONFIG_BOOKE_WDT
  910. /*
  911. * Default handler for a Watchdog exception,
  912. * spins until a reboot occurs
  913. */
  914. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  915. {
  916. /* Generic WatchdogHandler, implement your own */
  917. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  918. return;
  919. }
  920. void WatchdogException(struct pt_regs *regs)
  921. {
  922. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  923. WatchdogHandler(regs);
  924. }
  925. #endif
  926. /*
  927. * We enter here if we discover during exception entry that we are
  928. * running in supervisor mode with a userspace value in the stack pointer.
  929. */
  930. void kernel_bad_stack(struct pt_regs *regs)
  931. {
  932. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  933. regs->gpr[1], regs->nip);
  934. die("Bad kernel stack pointer", regs, SIGABRT);
  935. }
  936. void __init trap_init(void)
  937. {
  938. }