traps.c 28 KB

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