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