traps.c 34 KB

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