traps.c 28 KB

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