traps.c 27 KB

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