traps.c 28 KB

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