traps.c 27 KB

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