traps.c 28 KB

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