traps.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/prctl.h>
  28. #include <linux/delay.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/kexec.h>
  31. #include <linux/backlight.h>
  32. #include <linux/bug.h>
  33. #include <linux/kdebug.h>
  34. #include <linux/debugfs.h>
  35. #include <asm/emulated_ops.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/machdep.h>
  41. #include <asm/rtas.h>
  42. #include <asm/pmc.h>
  43. #ifdef CONFIG_PPC32
  44. #include <asm/reg.h>
  45. #endif
  46. #ifdef CONFIG_PMAC_BACKLIGHT
  47. #include <asm/backlight.h>
  48. #endif
  49. #ifdef CONFIG_PPC64
  50. #include <asm/firmware.h>
  51. #include <asm/processor.h>
  52. #endif
  53. #include <asm/kexec.h>
  54. #include <asm/ppc-opcode.h>
  55. #ifdef CONFIG_FSL_BOOKE
  56. #include <asm/dbell.h>
  57. #endif
  58. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  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. /*
  75. * Trap & Exception support
  76. */
  77. #ifdef CONFIG_PMAC_BACKLIGHT
  78. static void pmac_backlight_unblank(void)
  79. {
  80. mutex_lock(&pmac_backlight_mutex);
  81. if (pmac_backlight) {
  82. struct backlight_properties *props;
  83. props = &pmac_backlight->props;
  84. props->brightness = props->max_brightness;
  85. props->power = FB_BLANK_UNBLANK;
  86. backlight_update_status(pmac_backlight);
  87. }
  88. mutex_unlock(&pmac_backlight_mutex);
  89. }
  90. #else
  91. static inline void pmac_backlight_unblank(void) { }
  92. #endif
  93. int die(const char *str, struct pt_regs *regs, long err)
  94. {
  95. static struct {
  96. spinlock_t lock;
  97. u32 lock_owner;
  98. int lock_owner_depth;
  99. } die = {
  100. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  101. .lock_owner = -1,
  102. .lock_owner_depth = 0
  103. };
  104. static int die_counter;
  105. unsigned long flags;
  106. if (debugger(regs))
  107. return 1;
  108. oops_enter();
  109. if (die.lock_owner != raw_smp_processor_id()) {
  110. console_verbose();
  111. spin_lock_irqsave(&die.lock, flags);
  112. die.lock_owner = smp_processor_id();
  113. die.lock_owner_depth = 0;
  114. bust_spinlocks(1);
  115. if (machine_is(powermac))
  116. pmac_backlight_unblank();
  117. } else {
  118. local_save_flags(flags);
  119. }
  120. if (++die.lock_owner_depth < 3) {
  121. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  122. #ifdef CONFIG_PREEMPT
  123. printk("PREEMPT ");
  124. #endif
  125. #ifdef CONFIG_SMP
  126. printk("SMP NR_CPUS=%d ", NR_CPUS);
  127. #endif
  128. #ifdef CONFIG_DEBUG_PAGEALLOC
  129. printk("DEBUG_PAGEALLOC ");
  130. #endif
  131. #ifdef CONFIG_NUMA
  132. printk("NUMA ");
  133. #endif
  134. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  135. print_modules();
  136. show_regs(regs);
  137. } else {
  138. printk("Recursive die() failure, output suppressed\n");
  139. }
  140. bust_spinlocks(0);
  141. die.lock_owner = -1;
  142. add_taint(TAINT_DIE);
  143. spin_unlock_irqrestore(&die.lock, flags);
  144. if (kexec_should_crash(current) ||
  145. kexec_sr_activated(smp_processor_id()))
  146. crash_kexec(regs);
  147. crash_kexec_secondary(regs);
  148. if (in_interrupt())
  149. panic("Fatal exception in interrupt");
  150. if (panic_on_oops)
  151. panic("Fatal exception");
  152. oops_exit();
  153. do_exit(err);
  154. return 0;
  155. }
  156. void user_single_step_siginfo(struct task_struct *tsk,
  157. struct pt_regs *regs, siginfo_t *info)
  158. {
  159. memset(info, 0, sizeof(*info));
  160. info->si_signo = SIGTRAP;
  161. info->si_code = TRAP_TRACE;
  162. info->si_addr = (void __user *)regs->nip;
  163. }
  164. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  165. {
  166. siginfo_t info;
  167. const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  168. "at %08lx nip %08lx lr %08lx code %x\n";
  169. const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  170. "at %016lx nip %016lx lr %016lx code %x\n";
  171. if (!user_mode(regs)) {
  172. if (die("Exception in kernel mode", regs, signr))
  173. return;
  174. } else if (show_unhandled_signals &&
  175. unhandled_signal(current, signr) &&
  176. printk_ratelimit()) {
  177. printk(regs->msr & MSR_SF ? fmt64 : fmt32,
  178. current->comm, current->pid, signr,
  179. addr, regs->nip, regs->link, code);
  180. }
  181. memset(&info, 0, sizeof(info));
  182. info.si_signo = signr;
  183. info.si_code = code;
  184. info.si_addr = (void __user *) addr;
  185. force_sig_info(signr, &info, current);
  186. }
  187. #ifdef CONFIG_PPC64
  188. void system_reset_exception(struct pt_regs *regs)
  189. {
  190. /* See if any machine dependent calls */
  191. if (ppc_md.system_reset_exception) {
  192. if (ppc_md.system_reset_exception(regs))
  193. return;
  194. }
  195. #ifdef CONFIG_KEXEC
  196. cpu_set(smp_processor_id(), cpus_in_sr);
  197. #endif
  198. die("System Reset", regs, SIGABRT);
  199. /*
  200. * Some CPUs when released from the debugger will execute this path.
  201. * These CPUs entered the debugger via a soft-reset. If the CPU was
  202. * hung before entering the debugger it will return to the hung
  203. * state when exiting this function. This causes a problem in
  204. * kdump since the hung CPU(s) will not respond to the IPI sent
  205. * from kdump. To prevent the problem we call crash_kexec_secondary()
  206. * here. If a kdump had not been initiated or we exit the debugger
  207. * with the "exit and recover" command (x) crash_kexec_secondary()
  208. * will return after 5ms and the CPU returns to its previous state.
  209. */
  210. crash_kexec_secondary(regs);
  211. /* Must die if the interrupt is not recoverable */
  212. if (!(regs->msr & MSR_RI))
  213. panic("Unrecoverable System Reset");
  214. /* What should we do here? We could issue a shutdown or hard reset. */
  215. }
  216. #endif
  217. /*
  218. * I/O accesses can cause machine checks on powermacs.
  219. * Check if the NIP corresponds to the address of a sync
  220. * instruction for which there is an entry in the exception
  221. * table.
  222. * Note that the 601 only takes a machine check on TEA
  223. * (transfer error ack) signal assertion, and does not
  224. * set any of the top 16 bits of SRR1.
  225. * -- paulus.
  226. */
  227. static inline int check_io_access(struct pt_regs *regs)
  228. {
  229. #ifdef CONFIG_PPC32
  230. unsigned long msr = regs->msr;
  231. const struct exception_table_entry *entry;
  232. unsigned int *nip = (unsigned int *)regs->nip;
  233. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  234. && (entry = search_exception_tables(regs->nip)) != NULL) {
  235. /*
  236. * Check that it's a sync instruction, or somewhere
  237. * in the twi; isync; nop sequence that inb/inw/inl uses.
  238. * As the address is in the exception table
  239. * we should be able to read the instr there.
  240. * For the debug message, we look at the preceding
  241. * load or store.
  242. */
  243. if (*nip == 0x60000000) /* nop */
  244. nip -= 2;
  245. else if (*nip == 0x4c00012c) /* isync */
  246. --nip;
  247. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  248. /* sync or twi */
  249. unsigned int rb;
  250. --nip;
  251. rb = (*nip >> 11) & 0x1f;
  252. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  253. (*nip & 0x100)? "OUT to": "IN from",
  254. regs->gpr[rb] - _IO_BASE, nip);
  255. regs->msr |= MSR_RI;
  256. regs->nip = entry->fixup;
  257. return 1;
  258. }
  259. }
  260. #endif /* CONFIG_PPC32 */
  261. return 0;
  262. }
  263. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  264. /* On 4xx, the reason for the machine check or program exception
  265. is in the ESR. */
  266. #define get_reason(regs) ((regs)->dsisr)
  267. #ifndef CONFIG_FSL_BOOKE
  268. #define get_mc_reason(regs) ((regs)->dsisr)
  269. #else
  270. #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
  271. #endif
  272. #define REASON_FP ESR_FP
  273. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  274. #define REASON_PRIVILEGED ESR_PPR
  275. #define REASON_TRAP ESR_PTR
  276. /* single-step stuff */
  277. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  278. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  279. #else
  280. /* On non-4xx, the reason for the machine check or program
  281. exception is in the MSR. */
  282. #define get_reason(regs) ((regs)->msr)
  283. #define get_mc_reason(regs) ((regs)->msr)
  284. #define REASON_FP 0x100000
  285. #define REASON_ILLEGAL 0x80000
  286. #define REASON_PRIVILEGED 0x40000
  287. #define REASON_TRAP 0x20000
  288. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  289. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  290. #endif
  291. #if defined(CONFIG_4xx)
  292. int machine_check_4xx(struct pt_regs *regs)
  293. {
  294. unsigned long reason = get_mc_reason(regs);
  295. if (reason & ESR_IMCP) {
  296. printk("Instruction");
  297. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  298. } else
  299. printk("Data");
  300. printk(" machine check in kernel mode.\n");
  301. return 0;
  302. }
  303. int machine_check_440A(struct pt_regs *regs)
  304. {
  305. unsigned long reason = get_mc_reason(regs);
  306. printk("Machine check in kernel mode.\n");
  307. if (reason & ESR_IMCP){
  308. printk("Instruction Synchronous Machine Check exception\n");
  309. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  310. }
  311. else {
  312. u32 mcsr = mfspr(SPRN_MCSR);
  313. if (mcsr & MCSR_IB)
  314. printk("Instruction Read PLB Error\n");
  315. if (mcsr & MCSR_DRB)
  316. printk("Data Read PLB Error\n");
  317. if (mcsr & MCSR_DWB)
  318. printk("Data Write PLB Error\n");
  319. if (mcsr & MCSR_TLBP)
  320. printk("TLB Parity Error\n");
  321. if (mcsr & MCSR_ICP){
  322. flush_instruction_cache();
  323. printk("I-Cache Parity Error\n");
  324. }
  325. if (mcsr & MCSR_DCSP)
  326. printk("D-Cache Search Parity Error\n");
  327. if (mcsr & MCSR_DCFP)
  328. printk("D-Cache Flush Parity Error\n");
  329. if (mcsr & MCSR_IMPE)
  330. printk("Machine Check exception is imprecise\n");
  331. /* Clear MCSR */
  332. mtspr(SPRN_MCSR, mcsr);
  333. }
  334. return 0;
  335. }
  336. #elif defined(CONFIG_E500)
  337. int machine_check_e500(struct pt_regs *regs)
  338. {
  339. unsigned long reason = get_mc_reason(regs);
  340. printk("Machine check in kernel mode.\n");
  341. printk("Caused by (from MCSR=%lx): ", reason);
  342. if (reason & MCSR_MCP)
  343. printk("Machine Check Signal\n");
  344. if (reason & MCSR_ICPERR)
  345. printk("Instruction Cache Parity Error\n");
  346. if (reason & MCSR_DCP_PERR)
  347. printk("Data Cache Push Parity Error\n");
  348. if (reason & MCSR_DCPERR)
  349. printk("Data Cache Parity Error\n");
  350. if (reason & MCSR_BUS_IAERR)
  351. printk("Bus - Instruction Address Error\n");
  352. if (reason & MCSR_BUS_RAERR)
  353. printk("Bus - Read Address Error\n");
  354. if (reason & MCSR_BUS_WAERR)
  355. printk("Bus - Write Address Error\n");
  356. if (reason & MCSR_BUS_IBERR)
  357. printk("Bus - Instruction Data Error\n");
  358. if (reason & MCSR_BUS_RBERR)
  359. printk("Bus - Read Data Bus Error\n");
  360. if (reason & MCSR_BUS_WBERR)
  361. printk("Bus - Read Data Bus Error\n");
  362. if (reason & MCSR_BUS_IPERR)
  363. printk("Bus - Instruction Parity Error\n");
  364. if (reason & MCSR_BUS_RPERR)
  365. printk("Bus - Read Parity Error\n");
  366. return 0;
  367. }
  368. #elif defined(CONFIG_E200)
  369. int machine_check_e200(struct pt_regs *regs)
  370. {
  371. unsigned long reason = get_mc_reason(regs);
  372. printk("Machine check in kernel mode.\n");
  373. printk("Caused by (from MCSR=%lx): ", reason);
  374. if (reason & MCSR_MCP)
  375. printk("Machine Check Signal\n");
  376. if (reason & MCSR_CP_PERR)
  377. printk("Cache Push Parity Error\n");
  378. if (reason & MCSR_CPERR)
  379. printk("Cache Parity Error\n");
  380. if (reason & MCSR_EXCP_ERR)
  381. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  382. if (reason & MCSR_BUS_IRERR)
  383. printk("Bus - Read Bus Error on instruction fetch\n");
  384. if (reason & MCSR_BUS_DRERR)
  385. printk("Bus - Read Bus Error on data load\n");
  386. if (reason & MCSR_BUS_WRERR)
  387. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  388. return 0;
  389. }
  390. #else
  391. int machine_check_generic(struct pt_regs *regs)
  392. {
  393. unsigned long reason = get_mc_reason(regs);
  394. printk("Machine check in kernel mode.\n");
  395. printk("Caused by (from SRR1=%lx): ", reason);
  396. switch (reason & 0x601F0000) {
  397. case 0x80000:
  398. printk("Machine check signal\n");
  399. break;
  400. case 0: /* for 601 */
  401. case 0x40000:
  402. case 0x140000: /* 7450 MSS error and TEA */
  403. printk("Transfer error ack signal\n");
  404. break;
  405. case 0x20000:
  406. printk("Data parity error signal\n");
  407. break;
  408. case 0x10000:
  409. printk("Address parity error signal\n");
  410. break;
  411. case 0x20000000:
  412. printk("L1 Data Cache error\n");
  413. break;
  414. case 0x40000000:
  415. printk("L1 Instruction Cache error\n");
  416. break;
  417. case 0x00100000:
  418. printk("L2 data cache parity error\n");
  419. break;
  420. default:
  421. printk("Unknown values in msr\n");
  422. }
  423. return 0;
  424. }
  425. #endif /* everything else */
  426. void machine_check_exception(struct pt_regs *regs)
  427. {
  428. int recover = 0;
  429. /* See if any machine dependent calls. In theory, we would want
  430. * to call the CPU first, and call the ppc_md. one if the CPU
  431. * one returns a positive number. However there is existing code
  432. * that assumes the board gets a first chance, so let's keep it
  433. * that way for now and fix things later. --BenH.
  434. */
  435. if (ppc_md.machine_check_exception)
  436. recover = ppc_md.machine_check_exception(regs);
  437. else if (cur_cpu_spec->machine_check)
  438. recover = cur_cpu_spec->machine_check(regs);
  439. if (recover > 0)
  440. return;
  441. if (user_mode(regs)) {
  442. regs->msr |= MSR_RI;
  443. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  444. return;
  445. }
  446. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  447. /* the qspan pci read routines can cause machine checks -- Cort
  448. *
  449. * yuck !!! that totally needs to go away ! There are better ways
  450. * to deal with that than having a wart in the mcheck handler.
  451. * -- BenH
  452. */
  453. bad_page_fault(regs, regs->dar, SIGBUS);
  454. return;
  455. #endif
  456. if (debugger_fault_handler(regs)) {
  457. regs->msr |= MSR_RI;
  458. return;
  459. }
  460. if (check_io_access(regs))
  461. return;
  462. if (debugger_fault_handler(regs))
  463. return;
  464. die("Machine check", regs, SIGBUS);
  465. /* Must die if the interrupt is not recoverable */
  466. if (!(regs->msr & MSR_RI))
  467. panic("Unrecoverable Machine check");
  468. }
  469. void SMIException(struct pt_regs *regs)
  470. {
  471. die("System Management Interrupt", regs, SIGABRT);
  472. }
  473. void unknown_exception(struct pt_regs *regs)
  474. {
  475. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  476. regs->nip, regs->msr, regs->trap);
  477. _exception(SIGTRAP, regs, 0, 0);
  478. }
  479. void instruction_breakpoint_exception(struct pt_regs *regs)
  480. {
  481. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  482. 5, SIGTRAP) == NOTIFY_STOP)
  483. return;
  484. if (debugger_iabr_match(regs))
  485. return;
  486. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  487. }
  488. void RunModeException(struct pt_regs *regs)
  489. {
  490. _exception(SIGTRAP, regs, 0, 0);
  491. }
  492. void __kprobes single_step_exception(struct pt_regs *regs)
  493. {
  494. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  495. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  496. 5, SIGTRAP) == NOTIFY_STOP)
  497. return;
  498. if (debugger_sstep(regs))
  499. return;
  500. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  501. }
  502. /*
  503. * After we have successfully emulated an instruction, we have to
  504. * check if the instruction was being single-stepped, and if so,
  505. * pretend we got a single-step exception. This was pointed out
  506. * by Kumar Gala. -- paulus
  507. */
  508. static void emulate_single_step(struct pt_regs *regs)
  509. {
  510. if (single_stepping(regs)) {
  511. clear_single_step(regs);
  512. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  513. }
  514. }
  515. static inline int __parse_fpscr(unsigned long fpscr)
  516. {
  517. int ret = 0;
  518. /* Invalid operation */
  519. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  520. ret = FPE_FLTINV;
  521. /* Overflow */
  522. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  523. ret = FPE_FLTOVF;
  524. /* Underflow */
  525. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  526. ret = FPE_FLTUND;
  527. /* Divide by zero */
  528. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  529. ret = FPE_FLTDIV;
  530. /* Inexact result */
  531. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  532. ret = FPE_FLTRES;
  533. return ret;
  534. }
  535. static void parse_fpe(struct pt_regs *regs)
  536. {
  537. int code = 0;
  538. flush_fp_to_thread(current);
  539. code = __parse_fpscr(current->thread.fpscr.val);
  540. _exception(SIGFPE, regs, code, regs->nip);
  541. }
  542. /*
  543. * Illegal instruction emulation support. Originally written to
  544. * provide the PVR to user applications using the mfspr rd, PVR.
  545. * Return non-zero if we can't emulate, or -EFAULT if the associated
  546. * memory access caused an access fault. Return zero on success.
  547. *
  548. * There are a couple of ways to do this, either "decode" the instruction
  549. * or directly match lots of bits. In this case, matching lots of
  550. * bits is faster and easier.
  551. *
  552. */
  553. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  554. {
  555. u8 rT = (instword >> 21) & 0x1f;
  556. u8 rA = (instword >> 16) & 0x1f;
  557. u8 NB_RB = (instword >> 11) & 0x1f;
  558. u32 num_bytes;
  559. unsigned long EA;
  560. int pos = 0;
  561. /* Early out if we are an invalid form of lswx */
  562. if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
  563. if ((rT == rA) || (rT == NB_RB))
  564. return -EINVAL;
  565. EA = (rA == 0) ? 0 : regs->gpr[rA];
  566. switch (instword & PPC_INST_STRING_MASK) {
  567. case PPC_INST_LSWX:
  568. case PPC_INST_STSWX:
  569. EA += NB_RB;
  570. num_bytes = regs->xer & 0x7f;
  571. break;
  572. case PPC_INST_LSWI:
  573. case PPC_INST_STSWI:
  574. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  575. break;
  576. default:
  577. return -EINVAL;
  578. }
  579. while (num_bytes != 0)
  580. {
  581. u8 val;
  582. u32 shift = 8 * (3 - (pos & 0x3));
  583. switch ((instword & PPC_INST_STRING_MASK)) {
  584. case PPC_INST_LSWX:
  585. case PPC_INST_LSWI:
  586. if (get_user(val, (u8 __user *)EA))
  587. return -EFAULT;
  588. /* first time updating this reg,
  589. * zero it out */
  590. if (pos == 0)
  591. regs->gpr[rT] = 0;
  592. regs->gpr[rT] |= val << shift;
  593. break;
  594. case PPC_INST_STSWI:
  595. case PPC_INST_STSWX:
  596. val = regs->gpr[rT] >> shift;
  597. if (put_user(val, (u8 __user *)EA))
  598. return -EFAULT;
  599. break;
  600. }
  601. /* move EA to next address */
  602. EA += 1;
  603. num_bytes--;
  604. /* manage our position within the register */
  605. if (++pos == 4) {
  606. pos = 0;
  607. if (++rT == 32)
  608. rT = 0;
  609. }
  610. }
  611. return 0;
  612. }
  613. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  614. {
  615. u32 ra,rs;
  616. unsigned long tmp;
  617. ra = (instword >> 16) & 0x1f;
  618. rs = (instword >> 21) & 0x1f;
  619. tmp = regs->gpr[rs];
  620. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  621. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  622. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  623. regs->gpr[ra] = tmp;
  624. return 0;
  625. }
  626. static int emulate_isel(struct pt_regs *regs, u32 instword)
  627. {
  628. u8 rT = (instword >> 21) & 0x1f;
  629. u8 rA = (instword >> 16) & 0x1f;
  630. u8 rB = (instword >> 11) & 0x1f;
  631. u8 BC = (instword >> 6) & 0x1f;
  632. u8 bit;
  633. unsigned long tmp;
  634. tmp = (rA == 0) ? 0 : regs->gpr[rA];
  635. bit = (regs->ccr >> (31 - BC)) & 0x1;
  636. regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
  637. return 0;
  638. }
  639. static int emulate_instruction(struct pt_regs *regs)
  640. {
  641. u32 instword;
  642. u32 rd;
  643. if (!user_mode(regs) || (regs->msr & MSR_LE))
  644. return -EINVAL;
  645. CHECK_FULL_REGS(regs);
  646. if (get_user(instword, (u32 __user *)(regs->nip)))
  647. return -EFAULT;
  648. /* Emulate the mfspr rD, PVR. */
  649. if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
  650. PPC_WARN_EMULATED(mfpvr, regs);
  651. rd = (instword >> 21) & 0x1f;
  652. regs->gpr[rd] = mfspr(SPRN_PVR);
  653. return 0;
  654. }
  655. /* Emulating the dcba insn is just a no-op. */
  656. if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
  657. PPC_WARN_EMULATED(dcba, regs);
  658. return 0;
  659. }
  660. /* Emulate the mcrxr insn. */
  661. if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
  662. int shift = (instword >> 21) & 0x1c;
  663. unsigned long msk = 0xf0000000UL >> shift;
  664. PPC_WARN_EMULATED(mcrxr, regs);
  665. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  666. regs->xer &= ~0xf0000000UL;
  667. return 0;
  668. }
  669. /* Emulate load/store string insn. */
  670. if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
  671. PPC_WARN_EMULATED(string, regs);
  672. return emulate_string_inst(regs, instword);
  673. }
  674. /* Emulate the popcntb (Population Count Bytes) instruction. */
  675. if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
  676. PPC_WARN_EMULATED(popcntb, regs);
  677. return emulate_popcntb_inst(regs, instword);
  678. }
  679. /* Emulate isel (Integer Select) instruction */
  680. if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
  681. PPC_WARN_EMULATED(isel, regs);
  682. return emulate_isel(regs, instword);
  683. }
  684. return -EINVAL;
  685. }
  686. int is_valid_bugaddr(unsigned long addr)
  687. {
  688. return is_kernel_addr(addr);
  689. }
  690. void __kprobes program_check_exception(struct pt_regs *regs)
  691. {
  692. unsigned int reason = get_reason(regs);
  693. extern int do_mathemu(struct pt_regs *regs);
  694. /* We can now get here via a FP Unavailable exception if the core
  695. * has no FPU, in that case the reason flags will be 0 */
  696. if (reason & REASON_FP) {
  697. /* IEEE FP exception */
  698. parse_fpe(regs);
  699. return;
  700. }
  701. if (reason & REASON_TRAP) {
  702. /* trap exception */
  703. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  704. == NOTIFY_STOP)
  705. return;
  706. if (debugger_bpt(regs))
  707. return;
  708. if (!(regs->msr & MSR_PR) && /* not user-mode */
  709. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  710. regs->nip += 4;
  711. return;
  712. }
  713. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  714. return;
  715. }
  716. local_irq_enable();
  717. #ifdef CONFIG_MATH_EMULATION
  718. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  719. * but there seems to be a hardware bug on the 405GP (RevD)
  720. * that means ESR is sometimes set incorrectly - either to
  721. * ESR_DST (!?) or 0. In the process of chasing this with the
  722. * hardware people - not sure if it can happen on any illegal
  723. * instruction or only on FP instructions, whether there is a
  724. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  725. switch (do_mathemu(regs)) {
  726. case 0:
  727. emulate_single_step(regs);
  728. return;
  729. case 1: {
  730. int code = 0;
  731. code = __parse_fpscr(current->thread.fpscr.val);
  732. _exception(SIGFPE, regs, code, regs->nip);
  733. return;
  734. }
  735. case -EFAULT:
  736. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  737. return;
  738. }
  739. /* fall through on any other errors */
  740. #endif /* CONFIG_MATH_EMULATION */
  741. /* Try to emulate it if we should. */
  742. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  743. switch (emulate_instruction(regs)) {
  744. case 0:
  745. regs->nip += 4;
  746. emulate_single_step(regs);
  747. return;
  748. case -EFAULT:
  749. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  750. return;
  751. }
  752. }
  753. if (reason & REASON_PRIVILEGED)
  754. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  755. else
  756. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  757. }
  758. void alignment_exception(struct pt_regs *regs)
  759. {
  760. int sig, code, fixed = 0;
  761. /* we don't implement logging of alignment exceptions */
  762. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  763. fixed = fix_alignment(regs);
  764. if (fixed == 1) {
  765. regs->nip += 4; /* skip over emulated instruction */
  766. emulate_single_step(regs);
  767. return;
  768. }
  769. /* Operand address was bad */
  770. if (fixed == -EFAULT) {
  771. sig = SIGSEGV;
  772. code = SEGV_ACCERR;
  773. } else {
  774. sig = SIGBUS;
  775. code = BUS_ADRALN;
  776. }
  777. if (user_mode(regs))
  778. _exception(sig, regs, code, regs->dar);
  779. else
  780. bad_page_fault(regs, regs->dar, sig);
  781. }
  782. void StackOverflow(struct pt_regs *regs)
  783. {
  784. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  785. current, regs->gpr[1]);
  786. debugger(regs);
  787. show_regs(regs);
  788. panic("kernel stack overflow");
  789. }
  790. void nonrecoverable_exception(struct pt_regs *regs)
  791. {
  792. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  793. regs->nip, regs->msr);
  794. debugger(regs);
  795. die("nonrecoverable exception", regs, SIGKILL);
  796. }
  797. void trace_syscall(struct pt_regs *regs)
  798. {
  799. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  800. current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
  801. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  802. }
  803. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  804. {
  805. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  806. "%lx at %lx\n", regs->trap, regs->nip);
  807. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  808. }
  809. void altivec_unavailable_exception(struct pt_regs *regs)
  810. {
  811. if (user_mode(regs)) {
  812. /* A user program has executed an altivec instruction,
  813. but this kernel doesn't support altivec. */
  814. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  815. return;
  816. }
  817. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  818. "%lx at %lx\n", regs->trap, regs->nip);
  819. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  820. }
  821. void vsx_unavailable_exception(struct pt_regs *regs)
  822. {
  823. if (user_mode(regs)) {
  824. /* A user program has executed an vsx instruction,
  825. but this kernel doesn't support vsx. */
  826. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  827. return;
  828. }
  829. printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
  830. "%lx at %lx\n", regs->trap, regs->nip);
  831. die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
  832. }
  833. void performance_monitor_exception(struct pt_regs *regs)
  834. {
  835. perf_irq(regs);
  836. }
  837. #ifdef CONFIG_8xx
  838. void SoftwareEmulation(struct pt_regs *regs)
  839. {
  840. extern int do_mathemu(struct pt_regs *);
  841. extern int Soft_emulate_8xx(struct pt_regs *);
  842. #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
  843. int errcode;
  844. #endif
  845. CHECK_FULL_REGS(regs);
  846. if (!user_mode(regs)) {
  847. debugger(regs);
  848. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  849. }
  850. #ifdef CONFIG_MATH_EMULATION
  851. errcode = do_mathemu(regs);
  852. if (errcode >= 0)
  853. PPC_WARN_EMULATED(math, regs);
  854. switch (errcode) {
  855. case 0:
  856. emulate_single_step(regs);
  857. return;
  858. case 1: {
  859. int code = 0;
  860. code = __parse_fpscr(current->thread.fpscr.val);
  861. _exception(SIGFPE, regs, code, regs->nip);
  862. return;
  863. }
  864. case -EFAULT:
  865. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  866. return;
  867. default:
  868. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  869. return;
  870. }
  871. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  872. errcode = Soft_emulate_8xx(regs);
  873. if (errcode >= 0)
  874. PPC_WARN_EMULATED(8xx, regs);
  875. switch (errcode) {
  876. case 0:
  877. emulate_single_step(regs);
  878. return;
  879. case 1:
  880. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  881. return;
  882. case -EFAULT:
  883. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  884. return;
  885. }
  886. #else
  887. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  888. #endif
  889. }
  890. #endif /* CONFIG_8xx */
  891. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  892. void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
  893. {
  894. /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
  895. * on server, it stops on the target of the branch. In order to simulate
  896. * the server behaviour, we thus restart right away with a single step
  897. * instead of stopping here when hitting a BT
  898. */
  899. if (debug_status & DBSR_BT) {
  900. regs->msr &= ~MSR_DE;
  901. /* Disable BT */
  902. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
  903. /* Clear the BT event */
  904. mtspr(SPRN_DBSR, DBSR_BT);
  905. /* Do the single step trick only when coming from userspace */
  906. if (user_mode(regs)) {
  907. current->thread.dbcr0 &= ~DBCR0_BT;
  908. current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  909. regs->msr |= MSR_DE;
  910. return;
  911. }
  912. if (notify_die(DIE_SSTEP, "block_step", regs, 5,
  913. 5, SIGTRAP) == NOTIFY_STOP) {
  914. return;
  915. }
  916. if (debugger_sstep(regs))
  917. return;
  918. } else if (debug_status & DBSR_IC) { /* Instruction complete */
  919. regs->msr &= ~MSR_DE;
  920. /* Disable instruction completion */
  921. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  922. /* Clear the instruction completion event */
  923. mtspr(SPRN_DBSR, DBSR_IC);
  924. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  925. 5, SIGTRAP) == NOTIFY_STOP) {
  926. return;
  927. }
  928. if (debugger_sstep(regs))
  929. return;
  930. if (user_mode(regs))
  931. current->thread.dbcr0 &= ~(DBCR0_IC);
  932. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  933. } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
  934. regs->msr &= ~MSR_DE;
  935. if (user_mode(regs)) {
  936. current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
  937. DBCR0_IDM);
  938. } else {
  939. /* Disable DAC interupts */
  940. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
  941. DBSR_DAC1W | DBCR0_IDM));
  942. /* Clear the DAC event */
  943. mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
  944. }
  945. /* Setup and send the trap to the handler */
  946. do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
  947. }
  948. }
  949. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  950. #if !defined(CONFIG_TAU_INT)
  951. void TAUException(struct pt_regs *regs)
  952. {
  953. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  954. regs->nip, regs->msr, regs->trap, print_tainted());
  955. }
  956. #endif /* CONFIG_INT_TAU */
  957. #ifdef CONFIG_ALTIVEC
  958. void altivec_assist_exception(struct pt_regs *regs)
  959. {
  960. int err;
  961. if (!user_mode(regs)) {
  962. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  963. " at %lx\n", regs->nip);
  964. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  965. }
  966. flush_altivec_to_thread(current);
  967. PPC_WARN_EMULATED(altivec, regs);
  968. err = emulate_altivec(regs);
  969. if (err == 0) {
  970. regs->nip += 4; /* skip emulated instruction */
  971. emulate_single_step(regs);
  972. return;
  973. }
  974. if (err == -EFAULT) {
  975. /* got an error reading the instruction */
  976. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  977. } else {
  978. /* didn't recognize the instruction */
  979. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  980. if (printk_ratelimit())
  981. printk(KERN_ERR "Unrecognized altivec instruction "
  982. "in %s at %lx\n", current->comm, regs->nip);
  983. current->thread.vscr.u[3] |= 0x10000;
  984. }
  985. }
  986. #endif /* CONFIG_ALTIVEC */
  987. #ifdef CONFIG_VSX
  988. void vsx_assist_exception(struct pt_regs *regs)
  989. {
  990. if (!user_mode(regs)) {
  991. printk(KERN_EMERG "VSX assist exception in kernel mode"
  992. " at %lx\n", regs->nip);
  993. die("Kernel VSX assist exception", regs, SIGILL);
  994. }
  995. flush_vsx_to_thread(current);
  996. printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
  997. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  998. }
  999. #endif /* CONFIG_VSX */
  1000. #ifdef CONFIG_FSL_BOOKE
  1001. void doorbell_exception(struct pt_regs *regs)
  1002. {
  1003. #ifdef CONFIG_SMP
  1004. int cpu = smp_processor_id();
  1005. int msg;
  1006. if (num_online_cpus() < 2)
  1007. return;
  1008. for (msg = 0; msg < 4; msg++)
  1009. if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
  1010. smp_message_recv(msg);
  1011. #else
  1012. printk(KERN_WARNING "Received doorbell on non-smp system\n");
  1013. #endif
  1014. }
  1015. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  1016. unsigned long error_code)
  1017. {
  1018. /* We treat cache locking instructions from the user
  1019. * as priv ops, in the future we could try to do
  1020. * something smarter
  1021. */
  1022. if (error_code & (ESR_DLK|ESR_ILK))
  1023. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1024. return;
  1025. }
  1026. #endif /* CONFIG_FSL_BOOKE */
  1027. #ifdef CONFIG_SPE
  1028. void SPEFloatingPointException(struct pt_regs *regs)
  1029. {
  1030. extern int do_spe_mathemu(struct pt_regs *regs);
  1031. unsigned long spefscr;
  1032. int fpexc_mode;
  1033. int code = 0;
  1034. int err;
  1035. preempt_disable();
  1036. if (regs->msr & MSR_SPE)
  1037. giveup_spe(current);
  1038. preempt_enable();
  1039. spefscr = current->thread.spefscr;
  1040. fpexc_mode = current->thread.fpexc_mode;
  1041. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  1042. code = FPE_FLTOVF;
  1043. }
  1044. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1045. code = FPE_FLTUND;
  1046. }
  1047. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1048. code = FPE_FLTDIV;
  1049. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1050. code = FPE_FLTINV;
  1051. }
  1052. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1053. code = FPE_FLTRES;
  1054. err = do_spe_mathemu(regs);
  1055. if (err == 0) {
  1056. regs->nip += 4; /* skip emulated instruction */
  1057. emulate_single_step(regs);
  1058. return;
  1059. }
  1060. if (err == -EFAULT) {
  1061. /* got an error reading the instruction */
  1062. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1063. } else if (err == -EINVAL) {
  1064. /* didn't recognize the instruction */
  1065. printk(KERN_ERR "unrecognized spe instruction "
  1066. "in %s at %lx\n", current->comm, regs->nip);
  1067. } else {
  1068. _exception(SIGFPE, regs, code, regs->nip);
  1069. }
  1070. return;
  1071. }
  1072. void SPEFloatingPointRoundException(struct pt_regs *regs)
  1073. {
  1074. extern int speround_handler(struct pt_regs *regs);
  1075. int err;
  1076. preempt_disable();
  1077. if (regs->msr & MSR_SPE)
  1078. giveup_spe(current);
  1079. preempt_enable();
  1080. regs->nip -= 4;
  1081. err = speround_handler(regs);
  1082. if (err == 0) {
  1083. regs->nip += 4; /* skip emulated instruction */
  1084. emulate_single_step(regs);
  1085. return;
  1086. }
  1087. if (err == -EFAULT) {
  1088. /* got an error reading the instruction */
  1089. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1090. } else if (err == -EINVAL) {
  1091. /* didn't recognize the instruction */
  1092. printk(KERN_ERR "unrecognized spe instruction "
  1093. "in %s at %lx\n", current->comm, regs->nip);
  1094. } else {
  1095. _exception(SIGFPE, regs, 0, regs->nip);
  1096. return;
  1097. }
  1098. }
  1099. #endif
  1100. /*
  1101. * We enter here if we get an unrecoverable exception, that is, one
  1102. * that happened at a point where the RI (recoverable interrupt) bit
  1103. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1104. * we therefore lost state by taking this exception.
  1105. */
  1106. void unrecoverable_exception(struct pt_regs *regs)
  1107. {
  1108. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  1109. regs->trap, regs->nip);
  1110. die("Unrecoverable exception", regs, SIGABRT);
  1111. }
  1112. #ifdef CONFIG_BOOKE_WDT
  1113. /*
  1114. * Default handler for a Watchdog exception,
  1115. * spins until a reboot occurs
  1116. */
  1117. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1118. {
  1119. /* Generic WatchdogHandler, implement your own */
  1120. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1121. return;
  1122. }
  1123. void WatchdogException(struct pt_regs *regs)
  1124. {
  1125. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1126. WatchdogHandler(regs);
  1127. }
  1128. #endif
  1129. /*
  1130. * We enter here if we discover during exception entry that we are
  1131. * running in supervisor mode with a userspace value in the stack pointer.
  1132. */
  1133. void kernel_bad_stack(struct pt_regs *regs)
  1134. {
  1135. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1136. regs->gpr[1], regs->nip);
  1137. die("Bad kernel stack pointer", regs, SIGABRT);
  1138. }
  1139. void __init trap_init(void)
  1140. {
  1141. }
  1142. #ifdef CONFIG_PPC_EMULATED_STATS
  1143. #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
  1144. struct ppc_emulated ppc_emulated = {
  1145. #ifdef CONFIG_ALTIVEC
  1146. WARN_EMULATED_SETUP(altivec),
  1147. #endif
  1148. WARN_EMULATED_SETUP(dcba),
  1149. WARN_EMULATED_SETUP(dcbz),
  1150. WARN_EMULATED_SETUP(fp_pair),
  1151. WARN_EMULATED_SETUP(isel),
  1152. WARN_EMULATED_SETUP(mcrxr),
  1153. WARN_EMULATED_SETUP(mfpvr),
  1154. WARN_EMULATED_SETUP(multiple),
  1155. WARN_EMULATED_SETUP(popcntb),
  1156. WARN_EMULATED_SETUP(spe),
  1157. WARN_EMULATED_SETUP(string),
  1158. WARN_EMULATED_SETUP(unaligned),
  1159. #ifdef CONFIG_MATH_EMULATION
  1160. WARN_EMULATED_SETUP(math),
  1161. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  1162. WARN_EMULATED_SETUP(8xx),
  1163. #endif
  1164. #ifdef CONFIG_VSX
  1165. WARN_EMULATED_SETUP(vsx),
  1166. #endif
  1167. };
  1168. u32 ppc_warn_emulated;
  1169. void ppc_warn_emulated_print(const char *type)
  1170. {
  1171. if (printk_ratelimit())
  1172. pr_warning("%s used emulated %s instruction\n", current->comm,
  1173. type);
  1174. }
  1175. static int __init ppc_warn_emulated_init(void)
  1176. {
  1177. struct dentry *dir, *d;
  1178. unsigned int i;
  1179. struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
  1180. if (!powerpc_debugfs_root)
  1181. return -ENODEV;
  1182. dir = debugfs_create_dir("emulated_instructions",
  1183. powerpc_debugfs_root);
  1184. if (!dir)
  1185. return -ENOMEM;
  1186. d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
  1187. &ppc_warn_emulated);
  1188. if (!d)
  1189. goto fail;
  1190. for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
  1191. d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
  1192. (u32 *)&entries[i].val.counter);
  1193. if (!d)
  1194. goto fail;
  1195. }
  1196. return 0;
  1197. fail:
  1198. debugfs_remove_recursive(dir);
  1199. return -ENOMEM;
  1200. }
  1201. device_initcall(ppc_warn_emulated_init);
  1202. #endif /* CONFIG_PPC_EMULATED_STATS */