GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / powerpc / kernel / traps.c
1 /*
2  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License
7  *  as published by the Free Software Foundation; either version
8  *  2 of the License, or (at your option) any later version.
9  *
10  *  Modified by Cort Dougan (cort@cs.nmt.edu)
11  *  and Paul Mackerras (paulus@samba.org)
12  */
13
14 /*
15  * This file handles the architecture-dependent parts of hardware exceptions
16  */
17
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/sched/debug.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/stddef.h>
24 #include <linux/unistd.h>
25 #include <linux/ptrace.h>
26 #include <linux/user.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/extable.h>
30 #include <linux/module.h>       /* print_modules */
31 #include <linux/prctl.h>
32 #include <linux/delay.h>
33 #include <linux/kprobes.h>
34 #include <linux/kexec.h>
35 #include <linux/backlight.h>
36 #include <linux/bug.h>
37 #include <linux/kdebug.h>
38 #include <linux/ratelimit.h>
39 #include <linux/context_tracking.h>
40
41 #include <asm/emulated_ops.h>
42 #include <asm/pgtable.h>
43 #include <linux/uaccess.h>
44 #include <asm/debugfs.h>
45 #include <asm/io.h>
46 #include <asm/machdep.h>
47 #include <asm/rtas.h>
48 #include <asm/pmc.h>
49 #include <asm/reg.h>
50 #ifdef CONFIG_PMAC_BACKLIGHT
51 #include <asm/backlight.h>
52 #endif
53 #ifdef CONFIG_PPC64
54 #include <asm/firmware.h>
55 #include <asm/processor.h>
56 #include <asm/tm.h>
57 #endif
58 #include <asm/kexec.h>
59 #include <asm/ppc-opcode.h>
60 #include <asm/rio.h>
61 #include <asm/fadump.h>
62 #include <asm/switch_to.h>
63 #include <asm/tm.h>
64 #include <asm/debug.h>
65 #include <asm/asm-prototypes.h>
66 #include <asm/hmi.h>
67 #include <sysdev/fsl_pci.h>
68 #include <asm/kprobes.h>
69
70 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
71 int (*__debugger)(struct pt_regs *regs) __read_mostly;
72 int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
73 int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
74 int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
75 int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
76 int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
77 int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
78
79 EXPORT_SYMBOL(__debugger);
80 EXPORT_SYMBOL(__debugger_ipi);
81 EXPORT_SYMBOL(__debugger_bpt);
82 EXPORT_SYMBOL(__debugger_sstep);
83 EXPORT_SYMBOL(__debugger_iabr_match);
84 EXPORT_SYMBOL(__debugger_break_match);
85 EXPORT_SYMBOL(__debugger_fault_handler);
86 #endif
87
88 /* Transactional Memory trap debug */
89 #ifdef TM_DEBUG_SW
90 #define TM_DEBUG(x...) printk(KERN_INFO x)
91 #else
92 #define TM_DEBUG(x...) do { } while(0)
93 #endif
94
95 /*
96  * Trap & Exception support
97  */
98
99 #ifdef CONFIG_PMAC_BACKLIGHT
100 static void pmac_backlight_unblank(void)
101 {
102         mutex_lock(&pmac_backlight_mutex);
103         if (pmac_backlight) {
104                 struct backlight_properties *props;
105
106                 props = &pmac_backlight->props;
107                 props->brightness = props->max_brightness;
108                 props->power = FB_BLANK_UNBLANK;
109                 backlight_update_status(pmac_backlight);
110         }
111         mutex_unlock(&pmac_backlight_mutex);
112 }
113 #else
114 static inline void pmac_backlight_unblank(void) { }
115 #endif
116
117 /*
118  * If oops/die is expected to crash the machine, return true here.
119  *
120  * This should not be expected to be 100% accurate, there may be
121  * notifiers registered or other unexpected conditions that may bring
122  * down the kernel. Or if the current process in the kernel is holding
123  * locks or has other critical state, the kernel may become effectively
124  * unusable anyway.
125  */
126 bool die_will_crash(void)
127 {
128         if (should_fadump_crash())
129                 return true;
130         if (kexec_should_crash(current))
131                 return true;
132         if (in_interrupt() || panic_on_oops ||
133                         !current->pid || is_global_init(current))
134                 return true;
135
136         return false;
137 }
138
139 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
140 static int die_owner = -1;
141 static unsigned int die_nest_count;
142 static int die_counter;
143
144 static unsigned long oops_begin(struct pt_regs *regs)
145 {
146         int cpu;
147         unsigned long flags;
148
149         oops_enter();
150
151         /* racy, but better than risking deadlock. */
152         raw_local_irq_save(flags);
153         cpu = smp_processor_id();
154         if (!arch_spin_trylock(&die_lock)) {
155                 if (cpu == die_owner)
156                         /* nested oops. should stop eventually */;
157                 else
158                         arch_spin_lock(&die_lock);
159         }
160         die_nest_count++;
161         die_owner = cpu;
162         console_verbose();
163         bust_spinlocks(1);
164         if (machine_is(powermac))
165                 pmac_backlight_unblank();
166         return flags;
167 }
168 NOKPROBE_SYMBOL(oops_begin);
169
170 static void oops_end(unsigned long flags, struct pt_regs *regs,
171                                int signr)
172 {
173         bust_spinlocks(0);
174         add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
175         die_nest_count--;
176         oops_exit();
177         printk("\n");
178         if (!die_nest_count) {
179                 /* Nest count reaches zero, release the lock. */
180                 die_owner = -1;
181                 arch_spin_unlock(&die_lock);
182         }
183         raw_local_irq_restore(flags);
184
185         /*
186          * system_reset_excption handles debugger, crash dump, panic, for 0x100
187          */
188         if (TRAP(regs) == 0x100)
189                 return;
190
191         crash_fadump(regs, "die oops");
192
193         if (kexec_should_crash(current))
194                 crash_kexec(regs);
195
196         if (!signr)
197                 return;
198
199         /*
200          * While our oops output is serialised by a spinlock, output
201          * from panic() called below can race and corrupt it. If we
202          * know we are going to panic, delay for 1 second so we have a
203          * chance to get clean backtraces from all CPUs that are oopsing.
204          */
205         if (in_interrupt() || panic_on_oops || !current->pid ||
206             is_global_init(current)) {
207                 mdelay(MSEC_PER_SEC);
208         }
209
210         if (in_interrupt())
211                 panic("Fatal exception in interrupt");
212         if (panic_on_oops)
213                 panic("Fatal exception");
214         do_exit(signr);
215 }
216 NOKPROBE_SYMBOL(oops_end);
217
218 static int __die(const char *str, struct pt_regs *regs, long err)
219 {
220         printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
221
222         if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
223                 printk("LE ");
224         else
225                 printk("BE ");
226
227         if (IS_ENABLED(CONFIG_PREEMPT))
228                 pr_cont("PREEMPT ");
229
230         if (IS_ENABLED(CONFIG_SMP))
231                 pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
232
233         if (debug_pagealloc_enabled())
234                 pr_cont("DEBUG_PAGEALLOC ");
235
236         if (IS_ENABLED(CONFIG_NUMA))
237                 pr_cont("NUMA ");
238
239         pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
240
241         if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
242                 return 1;
243
244         print_modules();
245         show_regs(regs);
246
247         return 0;
248 }
249 NOKPROBE_SYMBOL(__die);
250
251 void die(const char *str, struct pt_regs *regs, long err)
252 {
253         unsigned long flags;
254
255         /*
256          * system_reset_excption handles debugger, crash dump, panic, for 0x100
257          */
258         if (TRAP(regs) != 0x100) {
259                 if (debugger(regs))
260                         return;
261         }
262
263         flags = oops_begin(regs);
264         if (__die(str, regs, err))
265                 err = 0;
266         oops_end(flags, regs, err);
267 }
268 NOKPROBE_SYMBOL(die);
269
270 void user_single_step_siginfo(struct task_struct *tsk,
271                                 struct pt_regs *regs, siginfo_t *info)
272 {
273         memset(info, 0, sizeof(*info));
274         info->si_signo = SIGTRAP;
275         info->si_code = TRAP_TRACE;
276         info->si_addr = (void __user *)regs->nip;
277 }
278
279 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
280 {
281         siginfo_t info;
282         const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
283                         "at %08lx nip %08lx lr %08lx code %x\n";
284         const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
285                         "at %016lx nip %016lx lr %016lx code %x\n";
286
287         if (!user_mode(regs)) {
288                 die("Exception in kernel mode", regs, signr);
289                 return;
290         }
291
292         if (show_unhandled_signals && unhandled_signal(current, signr)) {
293                 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
294                                    current->comm, current->pid, signr,
295                                    addr, regs->nip, regs->link, code);
296         }
297
298         if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
299                 local_irq_enable();
300
301         current->thread.trap_nr = code;
302         memset(&info, 0, sizeof(info));
303         info.si_signo = signr;
304         info.si_code = code;
305         info.si_addr = (void __user *) addr;
306         force_sig_info(signr, &info, current);
307 }
308
309 void system_reset_exception(struct pt_regs *regs)
310 {
311         /*
312          * Avoid crashes in case of nested NMI exceptions. Recoverability
313          * is determined by RI and in_nmi
314          */
315         bool nested = in_nmi();
316         if (!nested)
317                 nmi_enter();
318
319         __this_cpu_inc(irq_stat.sreset_irqs);
320
321         /* See if any machine dependent calls */
322         if (ppc_md.system_reset_exception) {
323                 if (ppc_md.system_reset_exception(regs))
324                         goto out;
325         }
326
327         if (debugger(regs))
328                 goto out;
329
330         /*
331          * A system reset is a request to dump, so we always send
332          * it through the crashdump code (if fadump or kdump are
333          * registered).
334          */
335         crash_fadump(regs, "System Reset");
336
337         crash_kexec(regs);
338
339         /*
340          * We aren't the primary crash CPU. We need to send it
341          * to a holding pattern to avoid it ending up in the panic
342          * code.
343          */
344         crash_kexec_secondary(regs);
345
346         /*
347          * No debugger or crash dump registered, print logs then
348          * panic.
349          */
350         die("System Reset", regs, SIGABRT);
351
352         mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
353         add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
354         nmi_panic(regs, "System Reset");
355
356 out:
357 #ifdef CONFIG_PPC_BOOK3S_64
358         BUG_ON(get_paca()->in_nmi == 0);
359         if (get_paca()->in_nmi > 1)
360                 die("Unrecoverable nested System Reset", regs, SIGABRT);
361 #endif
362         /* Must die if the interrupt is not recoverable */
363         if (!(regs->msr & MSR_RI)) {
364                 /* For the reason explained in die_mce, nmi_exit before die */
365                 nmi_exit();
366                 die("Unrecoverable System Reset", regs, SIGABRT);
367         }
368
369         if (!nested)
370                 nmi_exit();
371
372         /* What should we do here? We could issue a shutdown or hard reset. */
373 }
374
375 /*
376  * I/O accesses can cause machine checks on powermacs.
377  * Check if the NIP corresponds to the address of a sync
378  * instruction for which there is an entry in the exception
379  * table.
380  * Note that the 601 only takes a machine check on TEA
381  * (transfer error ack) signal assertion, and does not
382  * set any of the top 16 bits of SRR1.
383  *  -- paulus.
384  */
385 static inline int check_io_access(struct pt_regs *regs)
386 {
387 #ifdef CONFIG_PPC32
388         unsigned long msr = regs->msr;
389         const struct exception_table_entry *entry;
390         unsigned int *nip = (unsigned int *)regs->nip;
391
392         if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
393             && (entry = search_exception_tables(regs->nip)) != NULL) {
394                 /*
395                  * Check that it's a sync instruction, or somewhere
396                  * in the twi; isync; nop sequence that inb/inw/inl uses.
397                  * As the address is in the exception table
398                  * we should be able to read the instr there.
399                  * For the debug message, we look at the preceding
400                  * load or store.
401                  */
402                 if (*nip == PPC_INST_NOP)
403                         nip -= 2;
404                 else if (*nip == PPC_INST_ISYNC)
405                         --nip;
406                 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
407                         unsigned int rb;
408
409                         --nip;
410                         rb = (*nip >> 11) & 0x1f;
411                         printk(KERN_DEBUG "%s bad port %lx at %p\n",
412                                (*nip & 0x100)? "OUT to": "IN from",
413                                regs->gpr[rb] - _IO_BASE, nip);
414                         regs->msr |= MSR_RI;
415                         regs->nip = extable_fixup(entry);
416                         return 1;
417                 }
418         }
419 #endif /* CONFIG_PPC32 */
420         return 0;
421 }
422
423 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
424 /* On 4xx, the reason for the machine check or program exception
425    is in the ESR. */
426 #define get_reason(regs)        ((regs)->dsisr)
427 #define REASON_FP               ESR_FP
428 #define REASON_ILLEGAL          (ESR_PIL | ESR_PUO)
429 #define REASON_PRIVILEGED       ESR_PPR
430 #define REASON_TRAP             ESR_PTR
431
432 /* single-step stuff */
433 #define single_stepping(regs)   (current->thread.debug.dbcr0 & DBCR0_IC)
434 #define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
435
436 #else
437 /* On non-4xx, the reason for the machine check or program
438    exception is in the MSR. */
439 #define get_reason(regs)        ((regs)->msr)
440 #define REASON_TM               SRR1_PROGTM
441 #define REASON_FP               SRR1_PROGFPE
442 #define REASON_ILLEGAL          SRR1_PROGILL
443 #define REASON_PRIVILEGED       SRR1_PROGPRIV
444 #define REASON_TRAP             SRR1_PROGTRAP
445
446 #define single_stepping(regs)   ((regs)->msr & MSR_SE)
447 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
448 #endif
449
450 #if defined(CONFIG_E500)
451 int machine_check_e500mc(struct pt_regs *regs)
452 {
453         unsigned long mcsr = mfspr(SPRN_MCSR);
454         unsigned long pvr = mfspr(SPRN_PVR);
455         unsigned long reason = mcsr;
456         int recoverable = 1;
457
458         if (reason & MCSR_LD) {
459                 recoverable = fsl_rio_mcheck_exception(regs);
460                 if (recoverable == 1)
461                         goto silent_out;
462         }
463
464         printk("Machine check in kernel mode.\n");
465         printk("Caused by (from MCSR=%lx): ", reason);
466
467         if (reason & MCSR_MCP)
468                 printk("Machine Check Signal\n");
469
470         if (reason & MCSR_ICPERR) {
471                 printk("Instruction Cache Parity Error\n");
472
473                 /*
474                  * This is recoverable by invalidating the i-cache.
475                  */
476                 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
477                 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
478                         ;
479
480                 /*
481                  * This will generally be accompanied by an instruction
482                  * fetch error report -- only treat MCSR_IF as fatal
483                  * if it wasn't due to an L1 parity error.
484                  */
485                 reason &= ~MCSR_IF;
486         }
487
488         if (reason & MCSR_DCPERR_MC) {
489                 printk("Data Cache Parity Error\n");
490
491                 /*
492                  * In write shadow mode we auto-recover from the error, but it
493                  * may still get logged and cause a machine check.  We should
494                  * only treat the non-write shadow case as non-recoverable.
495                  */
496                 /* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
497                  * is not implemented but L1 data cache always runs in write
498                  * shadow mode. Hence on data cache parity errors HW will
499                  * automatically invalidate the L1 Data Cache.
500                  */
501                 if (PVR_VER(pvr) != PVR_VER_E6500) {
502                         if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
503                                 recoverable = 0;
504                 }
505         }
506
507         if (reason & MCSR_L2MMU_MHIT) {
508                 printk("Hit on multiple TLB entries\n");
509                 recoverable = 0;
510         }
511
512         if (reason & MCSR_NMI)
513                 printk("Non-maskable interrupt\n");
514
515         if (reason & MCSR_IF) {
516                 printk("Instruction Fetch Error Report\n");
517                 recoverable = 0;
518         }
519
520         if (reason & MCSR_LD) {
521                 printk("Load Error Report\n");
522                 recoverable = 0;
523         }
524
525         if (reason & MCSR_ST) {
526                 printk("Store Error Report\n");
527                 recoverable = 0;
528         }
529
530         if (reason & MCSR_LDG) {
531                 printk("Guarded Load Error Report\n");
532                 recoverable = 0;
533         }
534
535         if (reason & MCSR_TLBSYNC)
536                 printk("Simultaneous tlbsync operations\n");
537
538         if (reason & MCSR_BSL2_ERR) {
539                 printk("Level 2 Cache Error\n");
540                 recoverable = 0;
541         }
542
543         if (reason & MCSR_MAV) {
544                 u64 addr;
545
546                 addr = mfspr(SPRN_MCAR);
547                 addr |= (u64)mfspr(SPRN_MCARU) << 32;
548
549                 printk("Machine Check %s Address: %#llx\n",
550                        reason & MCSR_MEA ? "Effective" : "Physical", addr);
551         }
552
553 silent_out:
554         mtspr(SPRN_MCSR, mcsr);
555         return mfspr(SPRN_MCSR) == 0 && recoverable;
556 }
557
558 int machine_check_e500(struct pt_regs *regs)
559 {
560         unsigned long reason = mfspr(SPRN_MCSR);
561
562         if (reason & MCSR_BUS_RBERR) {
563                 if (fsl_rio_mcheck_exception(regs))
564                         return 1;
565                 if (fsl_pci_mcheck_exception(regs))
566                         return 1;
567         }
568
569         printk("Machine check in kernel mode.\n");
570         printk("Caused by (from MCSR=%lx): ", reason);
571
572         if (reason & MCSR_MCP)
573                 printk("Machine Check Signal\n");
574         if (reason & MCSR_ICPERR)
575                 printk("Instruction Cache Parity Error\n");
576         if (reason & MCSR_DCP_PERR)
577                 printk("Data Cache Push Parity Error\n");
578         if (reason & MCSR_DCPERR)
579                 printk("Data Cache Parity Error\n");
580         if (reason & MCSR_BUS_IAERR)
581                 printk("Bus - Instruction Address Error\n");
582         if (reason & MCSR_BUS_RAERR)
583                 printk("Bus - Read Address Error\n");
584         if (reason & MCSR_BUS_WAERR)
585                 printk("Bus - Write Address Error\n");
586         if (reason & MCSR_BUS_IBERR)
587                 printk("Bus - Instruction Data Error\n");
588         if (reason & MCSR_BUS_RBERR)
589                 printk("Bus - Read Data Bus Error\n");
590         if (reason & MCSR_BUS_WBERR)
591                 printk("Bus - Write Data Bus Error\n");
592         if (reason & MCSR_BUS_IPERR)
593                 printk("Bus - Instruction Parity Error\n");
594         if (reason & MCSR_BUS_RPERR)
595                 printk("Bus - Read Parity Error\n");
596
597         return 0;
598 }
599
600 int machine_check_generic(struct pt_regs *regs)
601 {
602         return 0;
603 }
604 #elif defined(CONFIG_E200)
605 int machine_check_e200(struct pt_regs *regs)
606 {
607         unsigned long reason = mfspr(SPRN_MCSR);
608
609         printk("Machine check in kernel mode.\n");
610         printk("Caused by (from MCSR=%lx): ", reason);
611
612         if (reason & MCSR_MCP)
613                 printk("Machine Check Signal\n");
614         if (reason & MCSR_CP_PERR)
615                 printk("Cache Push Parity Error\n");
616         if (reason & MCSR_CPERR)
617                 printk("Cache Parity Error\n");
618         if (reason & MCSR_EXCP_ERR)
619                 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
620         if (reason & MCSR_BUS_IRERR)
621                 printk("Bus - Read Bus Error on instruction fetch\n");
622         if (reason & MCSR_BUS_DRERR)
623                 printk("Bus - Read Bus Error on data load\n");
624         if (reason & MCSR_BUS_WRERR)
625                 printk("Bus - Write Bus Error on buffered store or cache line push\n");
626
627         return 0;
628 }
629 #elif defined(CONFIG_PPC32)
630 int machine_check_generic(struct pt_regs *regs)
631 {
632         unsigned long reason = regs->msr;
633
634         printk("Machine check in kernel mode.\n");
635         printk("Caused by (from SRR1=%lx): ", reason);
636         switch (reason & 0x601F0000) {
637         case 0x80000:
638                 printk("Machine check signal\n");
639                 break;
640         case 0:         /* for 601 */
641         case 0x40000:
642         case 0x140000:  /* 7450 MSS error and TEA */
643                 printk("Transfer error ack signal\n");
644                 break;
645         case 0x20000:
646                 printk("Data parity error signal\n");
647                 break;
648         case 0x10000:
649                 printk("Address parity error signal\n");
650                 break;
651         case 0x20000000:
652                 printk("L1 Data Cache error\n");
653                 break;
654         case 0x40000000:
655                 printk("L1 Instruction Cache error\n");
656                 break;
657         case 0x00100000:
658                 printk("L2 data cache parity error\n");
659                 break;
660         default:
661                 printk("Unknown values in msr\n");
662         }
663         return 0;
664 }
665 #endif /* everything else */
666
667 void machine_check_exception(struct pt_regs *regs)
668 {
669         int recover = 0;
670         bool nested = in_nmi();
671         if (!nested)
672                 nmi_enter();
673
674         /* 64s accounts the mce in machine_check_early when in HVMODE */
675         if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
676                 __this_cpu_inc(irq_stat.mce_exceptions);
677
678         add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
679
680         /* See if any machine dependent calls. In theory, we would want
681          * to call the CPU first, and call the ppc_md. one if the CPU
682          * one returns a positive number. However there is existing code
683          * that assumes the board gets a first chance, so let's keep it
684          * that way for now and fix things later. --BenH.
685          */
686         if (ppc_md.machine_check_exception)
687                 recover = ppc_md.machine_check_exception(regs);
688         else if (cur_cpu_spec->machine_check)
689                 recover = cur_cpu_spec->machine_check(regs);
690
691         if (recover > 0)
692                 goto bail;
693
694         if (debugger_fault_handler(regs))
695                 goto bail;
696
697         if (check_io_access(regs))
698                 goto bail;
699
700         if (!nested)
701                 nmi_exit();
702
703         die("Machine check", regs, SIGBUS);
704
705         /* Must die if the interrupt is not recoverable */
706         if (!(regs->msr & MSR_RI))
707                 die("Unrecoverable Machine check", regs, SIGBUS);
708
709         return;
710
711 bail:
712         if (!nested)
713                 nmi_exit();
714 }
715
716 void SMIException(struct pt_regs *regs)
717 {
718         die("System Management Interrupt", regs, SIGABRT);
719 }
720
721 void handle_hmi_exception(struct pt_regs *regs)
722 {
723         struct pt_regs *old_regs;
724
725         old_regs = set_irq_regs(regs);
726         irq_enter();
727
728         if (ppc_md.handle_hmi_exception)
729                 ppc_md.handle_hmi_exception(regs);
730
731         irq_exit();
732         set_irq_regs(old_regs);
733 }
734
735 void unknown_exception(struct pt_regs *regs)
736 {
737         enum ctx_state prev_state = exception_enter();
738
739         printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
740                regs->nip, regs->msr, regs->trap);
741
742         _exception(SIGTRAP, regs, 0, 0);
743
744         exception_exit(prev_state);
745 }
746
747 void instruction_breakpoint_exception(struct pt_regs *regs)
748 {
749         enum ctx_state prev_state = exception_enter();
750
751         if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
752                                         5, SIGTRAP) == NOTIFY_STOP)
753                 goto bail;
754         if (debugger_iabr_match(regs))
755                 goto bail;
756         _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
757
758 bail:
759         exception_exit(prev_state);
760 }
761
762 void RunModeException(struct pt_regs *regs)
763 {
764         _exception(SIGTRAP, regs, 0, 0);
765 }
766
767 void single_step_exception(struct pt_regs *regs)
768 {
769         enum ctx_state prev_state = exception_enter();
770
771         clear_single_step(regs);
772
773         if (kprobe_post_handler(regs))
774                 return;
775
776         if (notify_die(DIE_SSTEP, "single_step", regs, 5,
777                                         5, SIGTRAP) == NOTIFY_STOP)
778                 goto bail;
779         if (debugger_sstep(regs))
780                 goto bail;
781
782         _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
783
784 bail:
785         exception_exit(prev_state);
786 }
787 NOKPROBE_SYMBOL(single_step_exception);
788
789 /*
790  * After we have successfully emulated an instruction, we have to
791  * check if the instruction was being single-stepped, and if so,
792  * pretend we got a single-step exception.  This was pointed out
793  * by Kumar Gala.  -- paulus
794  */
795 static void emulate_single_step(struct pt_regs *regs)
796 {
797         if (single_stepping(regs))
798                 single_step_exception(regs);
799 }
800
801 static inline int __parse_fpscr(unsigned long fpscr)
802 {
803         int ret = 0;
804
805         /* Invalid operation */
806         if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
807                 ret = FPE_FLTINV;
808
809         /* Overflow */
810         else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
811                 ret = FPE_FLTOVF;
812
813         /* Underflow */
814         else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
815                 ret = FPE_FLTUND;
816
817         /* Divide by zero */
818         else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
819                 ret = FPE_FLTDIV;
820
821         /* Inexact result */
822         else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
823                 ret = FPE_FLTRES;
824
825         return ret;
826 }
827
828 static void parse_fpe(struct pt_regs *regs)
829 {
830         int code = 0;
831
832         flush_fp_to_thread(current);
833
834         code = __parse_fpscr(current->thread.fp_state.fpscr);
835
836         _exception(SIGFPE, regs, code, regs->nip);
837 }
838
839 /*
840  * Illegal instruction emulation support.  Originally written to
841  * provide the PVR to user applications using the mfspr rd, PVR.
842  * Return non-zero if we can't emulate, or -EFAULT if the associated
843  * memory access caused an access fault.  Return zero on success.
844  *
845  * There are a couple of ways to do this, either "decode" the instruction
846  * or directly match lots of bits.  In this case, matching lots of
847  * bits is faster and easier.
848  *
849  */
850 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
851 {
852         u8 rT = (instword >> 21) & 0x1f;
853         u8 rA = (instword >> 16) & 0x1f;
854         u8 NB_RB = (instword >> 11) & 0x1f;
855         u32 num_bytes;
856         unsigned long EA;
857         int pos = 0;
858
859         /* Early out if we are an invalid form of lswx */
860         if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
861                 if ((rT == rA) || (rT == NB_RB))
862                         return -EINVAL;
863
864         EA = (rA == 0) ? 0 : regs->gpr[rA];
865
866         switch (instword & PPC_INST_STRING_MASK) {
867                 case PPC_INST_LSWX:
868                 case PPC_INST_STSWX:
869                         EA += NB_RB;
870                         num_bytes = regs->xer & 0x7f;
871                         break;
872                 case PPC_INST_LSWI:
873                 case PPC_INST_STSWI:
874                         num_bytes = (NB_RB == 0) ? 32 : NB_RB;
875                         break;
876                 default:
877                         return -EINVAL;
878         }
879
880         while (num_bytes != 0)
881         {
882                 u8 val;
883                 u32 shift = 8 * (3 - (pos & 0x3));
884
885                 /* if process is 32-bit, clear upper 32 bits of EA */
886                 if ((regs->msr & MSR_64BIT) == 0)
887                         EA &= 0xFFFFFFFF;
888
889                 switch ((instword & PPC_INST_STRING_MASK)) {
890                         case PPC_INST_LSWX:
891                         case PPC_INST_LSWI:
892                                 if (get_user(val, (u8 __user *)EA))
893                                         return -EFAULT;
894                                 /* first time updating this reg,
895                                  * zero it out */
896                                 if (pos == 0)
897                                         regs->gpr[rT] = 0;
898                                 regs->gpr[rT] |= val << shift;
899                                 break;
900                         case PPC_INST_STSWI:
901                         case PPC_INST_STSWX:
902                                 val = regs->gpr[rT] >> shift;
903                                 if (put_user(val, (u8 __user *)EA))
904                                         return -EFAULT;
905                                 break;
906                 }
907                 /* move EA to next address */
908                 EA += 1;
909                 num_bytes--;
910
911                 /* manage our position within the register */
912                 if (++pos == 4) {
913                         pos = 0;
914                         if (++rT == 32)
915                                 rT = 0;
916                 }
917         }
918
919         return 0;
920 }
921
922 static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
923 {
924         u32 ra,rs;
925         unsigned long tmp;
926
927         ra = (instword >> 16) & 0x1f;
928         rs = (instword >> 21) & 0x1f;
929
930         tmp = regs->gpr[rs];
931         tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
932         tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
933         tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
934         regs->gpr[ra] = tmp;
935
936         return 0;
937 }
938
939 static int emulate_isel(struct pt_regs *regs, u32 instword)
940 {
941         u8 rT = (instword >> 21) & 0x1f;
942         u8 rA = (instword >> 16) & 0x1f;
943         u8 rB = (instword >> 11) & 0x1f;
944         u8 BC = (instword >> 6) & 0x1f;
945         u8 bit;
946         unsigned long tmp;
947
948         tmp = (rA == 0) ? 0 : regs->gpr[rA];
949         bit = (regs->ccr >> (31 - BC)) & 0x1;
950
951         regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
952
953         return 0;
954 }
955
956 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
957 static inline bool tm_abort_check(struct pt_regs *regs, int cause)
958 {
959         /* If we're emulating a load/store in an active transaction, we cannot
960          * emulate it as the kernel operates in transaction suspended context.
961          * We need to abort the transaction.  This creates a persistent TM
962          * abort so tell the user what caused it with a new code.
963          */
964         if (MSR_TM_TRANSACTIONAL(regs->msr)) {
965                 tm_enable();
966                 tm_abort(cause);
967                 return true;
968         }
969         return false;
970 }
971 #else
972 static inline bool tm_abort_check(struct pt_regs *regs, int reason)
973 {
974         return false;
975 }
976 #endif
977
978 static int emulate_instruction(struct pt_regs *regs)
979 {
980         u32 instword;
981         u32 rd;
982
983         if (!user_mode(regs))
984                 return -EINVAL;
985         CHECK_FULL_REGS(regs);
986
987         if (get_user(instword, (u32 __user *)(regs->nip)))
988                 return -EFAULT;
989
990         /* Emulate the mfspr rD, PVR. */
991         if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
992                 PPC_WARN_EMULATED(mfpvr, regs);
993                 rd = (instword >> 21) & 0x1f;
994                 regs->gpr[rd] = mfspr(SPRN_PVR);
995                 return 0;
996         }
997
998         /* Emulating the dcba insn is just a no-op.  */
999         if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1000                 PPC_WARN_EMULATED(dcba, regs);
1001                 return 0;
1002         }
1003
1004         /* Emulate the mcrxr insn.  */
1005         if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
1006                 int shift = (instword >> 21) & 0x1c;
1007                 unsigned long msk = 0xf0000000UL >> shift;
1008
1009                 PPC_WARN_EMULATED(mcrxr, regs);
1010                 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
1011                 regs->xer &= ~0xf0000000UL;
1012                 return 0;
1013         }
1014
1015         /* Emulate load/store string insn. */
1016         if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
1017                 if (tm_abort_check(regs,
1018                                    TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
1019                         return -EINVAL;
1020                 PPC_WARN_EMULATED(string, regs);
1021                 return emulate_string_inst(regs, instword);
1022         }
1023
1024         /* Emulate the popcntb (Population Count Bytes) instruction. */
1025         if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1026                 PPC_WARN_EMULATED(popcntb, regs);
1027                 return emulate_popcntb_inst(regs, instword);
1028         }
1029
1030         /* Emulate isel (Integer Select) instruction */
1031         if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1032                 PPC_WARN_EMULATED(isel, regs);
1033                 return emulate_isel(regs, instword);
1034         }
1035
1036         /* Emulate sync instruction variants */
1037         if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
1038                 PPC_WARN_EMULATED(sync, regs);
1039                 asm volatile("sync");
1040                 return 0;
1041         }
1042
1043 #ifdef CONFIG_PPC64
1044         /* Emulate the mfspr rD, DSCR. */
1045         if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
1046                 PPC_INST_MFSPR_DSCR_USER) ||
1047              ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
1048                 PPC_INST_MFSPR_DSCR)) &&
1049                         cpu_has_feature(CPU_FTR_DSCR)) {
1050                 PPC_WARN_EMULATED(mfdscr, regs);
1051                 rd = (instword >> 21) & 0x1f;
1052                 regs->gpr[rd] = mfspr(SPRN_DSCR);
1053                 return 0;
1054         }
1055         /* Emulate the mtspr DSCR, rD. */
1056         if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1057                 PPC_INST_MTSPR_DSCR_USER) ||
1058              ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1059                 PPC_INST_MTSPR_DSCR)) &&
1060                         cpu_has_feature(CPU_FTR_DSCR)) {
1061                 PPC_WARN_EMULATED(mtdscr, regs);
1062                 rd = (instword >> 21) & 0x1f;
1063                 current->thread.dscr = regs->gpr[rd];
1064                 current->thread.dscr_inherit = 1;
1065                 mtspr(SPRN_DSCR, current->thread.dscr);
1066                 return 0;
1067         }
1068 #endif
1069
1070         return -EINVAL;
1071 }
1072
1073 int is_valid_bugaddr(unsigned long addr)
1074 {
1075         return is_kernel_addr(addr);
1076 }
1077
1078 #ifdef CONFIG_MATH_EMULATION
1079 static int emulate_math(struct pt_regs *regs)
1080 {
1081         int ret;
1082         extern int do_mathemu(struct pt_regs *regs);
1083
1084         ret = do_mathemu(regs);
1085         if (ret >= 0)
1086                 PPC_WARN_EMULATED(math, regs);
1087
1088         switch (ret) {
1089         case 0:
1090                 emulate_single_step(regs);
1091                 return 0;
1092         case 1: {
1093                         int code = 0;
1094                         code = __parse_fpscr(current->thread.fp_state.fpscr);
1095                         _exception(SIGFPE, regs, code, regs->nip);
1096                         return 0;
1097                 }
1098         case -EFAULT:
1099                 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1100                 return 0;
1101         }
1102
1103         return -1;
1104 }
1105 #else
1106 static inline int emulate_math(struct pt_regs *regs) { return -1; }
1107 #endif
1108
1109 void program_check_exception(struct pt_regs *regs)
1110 {
1111         enum ctx_state prev_state = exception_enter();
1112         unsigned int reason = get_reason(regs);
1113
1114         /* We can now get here via a FP Unavailable exception if the core
1115          * has no FPU, in that case the reason flags will be 0 */
1116
1117         if (reason & REASON_FP) {
1118                 /* IEEE FP exception */
1119                 parse_fpe(regs);
1120                 goto bail;
1121         }
1122         if (reason & REASON_TRAP) {
1123                 unsigned long bugaddr;
1124                 /* Debugger is first in line to stop recursive faults in
1125                  * rcu_lock, notify_die, or atomic_notifier_call_chain */
1126                 if (debugger_bpt(regs))
1127                         goto bail;
1128
1129                 if (kprobe_handler(regs))
1130                         goto bail;
1131
1132                 /* trap exception */
1133                 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1134                                 == NOTIFY_STOP)
1135                         goto bail;
1136
1137                 bugaddr = regs->nip;
1138                 /*
1139                  * Fixup bugaddr for BUG_ON() in real mode
1140                  */
1141                 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1142                         bugaddr += PAGE_OFFSET;
1143
1144                 if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1145                     report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
1146                         regs->nip += 4;
1147                         goto bail;
1148                 }
1149                 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1150                 goto bail;
1151         }
1152 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1153         if (reason & REASON_TM) {
1154                 /* This is a TM "Bad Thing Exception" program check.
1155                  * This occurs when:
1156                  * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1157                  *    transition in TM states.
1158                  * -  A trechkpt is attempted when transactional.
1159                  * -  A treclaim is attempted when non transactional.
1160                  * -  A tend is illegally attempted.
1161                  * -  writing a TM SPR when transactional.
1162                  */
1163                 if (!user_mode(regs) &&
1164                     report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1165                         regs->nip += 4;
1166                         goto bail;
1167                 }
1168                 /* If usermode caused this, it's done something illegal and
1169                  * gets a SIGILL slap on the wrist.  We call it an illegal
1170                  * operand to distinguish from the instruction just being bad
1171                  * (e.g. executing a 'tend' on a CPU without TM!); it's an
1172                  * illegal /placement/ of a valid instruction.
1173                  */
1174                 if (user_mode(regs)) {
1175                         _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1176                         goto bail;
1177                 } else {
1178                         printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1179                                "at %lx (msr 0x%x)\n", regs->nip, reason);
1180                         die("Unrecoverable exception", regs, SIGABRT);
1181                 }
1182         }
1183 #endif
1184
1185         /*
1186          * If we took the program check in the kernel skip down to sending a
1187          * SIGILL. The subsequent cases all relate to emulating instructions
1188          * which we should only do for userspace. We also do not want to enable
1189          * interrupts for kernel faults because that might lead to further
1190          * faults, and loose the context of the original exception.
1191          */
1192         if (!user_mode(regs))
1193                 goto sigill;
1194
1195         /* We restore the interrupt state now */
1196         if (!arch_irq_disabled_regs(regs))
1197                 local_irq_enable();
1198
1199         /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1200          * but there seems to be a hardware bug on the 405GP (RevD)
1201          * that means ESR is sometimes set incorrectly - either to
1202          * ESR_DST (!?) or 0.  In the process of chasing this with the
1203          * hardware people - not sure if it can happen on any illegal
1204          * instruction or only on FP instructions, whether there is a
1205          * pattern to occurrences etc. -dgibson 31/Mar/2003
1206          */
1207         if (!emulate_math(regs))
1208                 goto bail;
1209
1210         /* Try to emulate it if we should. */
1211         if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
1212                 switch (emulate_instruction(regs)) {
1213                 case 0:
1214                         regs->nip += 4;
1215                         emulate_single_step(regs);
1216                         goto bail;
1217                 case -EFAULT:
1218                         _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1219                         goto bail;
1220                 }
1221         }
1222
1223 sigill:
1224         if (reason & REASON_PRIVILEGED)
1225                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1226         else
1227                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1228
1229 bail:
1230         exception_exit(prev_state);
1231 }
1232 NOKPROBE_SYMBOL(program_check_exception);
1233
1234 /*
1235  * This occurs when running in hypervisor mode on POWER6 or later
1236  * and an illegal instruction is encountered.
1237  */
1238 void emulation_assist_interrupt(struct pt_regs *regs)
1239 {
1240         regs->msr |= REASON_ILLEGAL;
1241         program_check_exception(regs);
1242 }
1243 NOKPROBE_SYMBOL(emulation_assist_interrupt);
1244
1245 void alignment_exception(struct pt_regs *regs)
1246 {
1247         enum ctx_state prev_state = exception_enter();
1248         int sig, code, fixed = 0;
1249
1250         /* We restore the interrupt state now */
1251         if (!arch_irq_disabled_regs(regs))
1252                 local_irq_enable();
1253
1254         if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1255                 goto bail;
1256
1257         /* we don't implement logging of alignment exceptions */
1258         if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1259                 fixed = fix_alignment(regs);
1260
1261         if (fixed == 1) {
1262                 regs->nip += 4; /* skip over emulated instruction */
1263                 emulate_single_step(regs);
1264                 goto bail;
1265         }
1266
1267         /* Operand address was bad */
1268         if (fixed == -EFAULT) {
1269                 sig = SIGSEGV;
1270                 code = SEGV_ACCERR;
1271         } else {
1272                 sig = SIGBUS;
1273                 code = BUS_ADRALN;
1274         }
1275         if (user_mode(regs))
1276                 _exception(sig, regs, code, regs->dar);
1277         else
1278                 bad_page_fault(regs, regs->dar, sig);
1279
1280 bail:
1281         exception_exit(prev_state);
1282 }
1283
1284 void slb_miss_bad_addr(struct pt_regs *regs)
1285 {
1286         enum ctx_state prev_state = exception_enter();
1287
1288         if (user_mode(regs))
1289                 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1290         else
1291                 bad_page_fault(regs, regs->dar, SIGSEGV);
1292
1293         exception_exit(prev_state);
1294 }
1295
1296 void StackOverflow(struct pt_regs *regs)
1297 {
1298         pr_crit("Kernel stack overflow in process %s[%d], r1=%lx\n",
1299                 current->comm, task_pid_nr(current), regs->gpr[1]);
1300         debugger(regs);
1301         show_regs(regs);
1302         panic("kernel stack overflow");
1303 }
1304
1305 void nonrecoverable_exception(struct pt_regs *regs)
1306 {
1307         printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1308                regs->nip, regs->msr);
1309         debugger(regs);
1310         die("nonrecoverable exception", regs, SIGKILL);
1311 }
1312
1313 void kernel_fp_unavailable_exception(struct pt_regs *regs)
1314 {
1315         enum ctx_state prev_state = exception_enter();
1316
1317         printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1318                           "%lx at %lx\n", regs->trap, regs->nip);
1319         die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1320
1321         exception_exit(prev_state);
1322 }
1323
1324 void altivec_unavailable_exception(struct pt_regs *regs)
1325 {
1326         enum ctx_state prev_state = exception_enter();
1327
1328         if (user_mode(regs)) {
1329                 /* A user program has executed an altivec instruction,
1330                    but this kernel doesn't support altivec. */
1331                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1332                 goto bail;
1333         }
1334
1335         printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1336                         "%lx at %lx\n", regs->trap, regs->nip);
1337         die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1338
1339 bail:
1340         exception_exit(prev_state);
1341 }
1342
1343 void vsx_unavailable_exception(struct pt_regs *regs)
1344 {
1345         if (user_mode(regs)) {
1346                 /* A user program has executed an vsx instruction,
1347                    but this kernel doesn't support vsx. */
1348                 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1349                 return;
1350         }
1351
1352         printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1353                         "%lx at %lx\n", regs->trap, regs->nip);
1354         die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1355 }
1356
1357 #ifdef CONFIG_PPC64
1358 static void tm_unavailable(struct pt_regs *regs)
1359 {
1360 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1361         if (user_mode(regs)) {
1362                 current->thread.load_tm++;
1363                 regs->msr |= MSR_TM;
1364                 tm_enable();
1365                 tm_restore_sprs(&current->thread);
1366                 return;
1367         }
1368 #endif
1369         pr_emerg("Unrecoverable TM Unavailable Exception "
1370                         "%lx at %lx\n", regs->trap, regs->nip);
1371         die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1372 }
1373
1374 void facility_unavailable_exception(struct pt_regs *regs)
1375 {
1376         static char *facility_strings[] = {
1377                 [FSCR_FP_LG] = "FPU",
1378                 [FSCR_VECVSX_LG] = "VMX/VSX",
1379                 [FSCR_DSCR_LG] = "DSCR",
1380                 [FSCR_PM_LG] = "PMU SPRs",
1381                 [FSCR_BHRB_LG] = "BHRB",
1382                 [FSCR_TM_LG] = "TM",
1383                 [FSCR_EBB_LG] = "EBB",
1384                 [FSCR_TAR_LG] = "TAR",
1385                 [FSCR_MSGP_LG] = "MSGP",
1386                 [FSCR_SCV_LG] = "SCV",
1387         };
1388         char *facility = "unknown";
1389         u64 value;
1390         u32 instword, rd;
1391         u8 status;
1392         bool hv;
1393
1394         hv = (regs->trap == 0xf80);
1395         if (hv)
1396                 value = mfspr(SPRN_HFSCR);
1397         else
1398                 value = mfspr(SPRN_FSCR);
1399
1400         status = value >> 56;
1401         if ((hv || status >= 2) &&
1402             (status < ARRAY_SIZE(facility_strings)) &&
1403             facility_strings[status])
1404                 facility = facility_strings[status];
1405
1406         /* We should not have taken this interrupt in kernel */
1407         if (!user_mode(regs)) {
1408                 pr_emerg("Facility '%s' unavailable (%d) exception in kernel mode at %lx\n",
1409                          facility, status, regs->nip);
1410                 die("Unexpected facility unavailable exception", regs, SIGABRT);
1411         }
1412
1413         /* We restore the interrupt state now */
1414         if (!arch_irq_disabled_regs(regs))
1415                 local_irq_enable();
1416
1417         if (status == FSCR_DSCR_LG) {
1418                 /*
1419                  * User is accessing the DSCR register using the problem
1420                  * state only SPR number (0x03) either through a mfspr or
1421                  * a mtspr instruction. If it is a write attempt through
1422                  * a mtspr, then we set the inherit bit. This also allows
1423                  * the user to write or read the register directly in the
1424                  * future by setting via the FSCR DSCR bit. But in case it
1425                  * is a read DSCR attempt through a mfspr instruction, we
1426                  * just emulate the instruction instead. This code path will
1427                  * always emulate all the mfspr instructions till the user
1428                  * has attempted at least one mtspr instruction. This way it
1429                  * preserves the same behaviour when the user is accessing
1430                  * the DSCR through privilege level only SPR number (0x11)
1431                  * which is emulated through illegal instruction exception.
1432                  * We always leave HFSCR DSCR set.
1433                  */
1434                 if (get_user(instword, (u32 __user *)(regs->nip))) {
1435                         pr_err("Failed to fetch the user instruction\n");
1436                         return;
1437                 }
1438
1439                 /* Write into DSCR (mtspr 0x03, RS) */
1440                 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1441                                 == PPC_INST_MTSPR_DSCR_USER) {
1442                         rd = (instword >> 21) & 0x1f;
1443                         current->thread.dscr = regs->gpr[rd];
1444                         current->thread.dscr_inherit = 1;
1445                         current->thread.fscr |= FSCR_DSCR;
1446                         mtspr(SPRN_FSCR, current->thread.fscr);
1447                 }
1448
1449                 /* Read from DSCR (mfspr RT, 0x03) */
1450                 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1451                                 == PPC_INST_MFSPR_DSCR_USER) {
1452                         if (emulate_instruction(regs)) {
1453                                 pr_err("DSCR based mfspr emulation failed\n");
1454                                 return;
1455                         }
1456                         regs->nip += 4;
1457                         emulate_single_step(regs);
1458                 }
1459                 return;
1460         }
1461
1462         if (status == FSCR_TM_LG) {
1463                 /*
1464                  * If we're here then the hardware is TM aware because it
1465                  * generated an exception with FSRM_TM set.
1466                  *
1467                  * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1468                  * told us not to do TM, or the kernel is not built with TM
1469                  * support.
1470                  *
1471                  * If both of those things are true, then userspace can spam the
1472                  * console by triggering the printk() below just by continually
1473                  * doing tbegin (or any TM instruction). So in that case just
1474                  * send the process a SIGILL immediately.
1475                  */
1476                 if (!cpu_has_feature(CPU_FTR_TM))
1477                         goto out;
1478
1479                 tm_unavailable(regs);
1480                 return;
1481         }
1482
1483         pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
1484                 hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1485
1486 out:
1487         _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1488 }
1489 #endif
1490
1491 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1492
1493 void fp_unavailable_tm(struct pt_regs *regs)
1494 {
1495         /* Note:  This does not handle any kind of FP laziness. */
1496
1497         TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1498                  regs->nip, regs->msr);
1499
1500         /* We can only have got here if the task started using FP after
1501          * beginning the transaction.  So, the transactional regs are just a
1502          * copy of the checkpointed ones.  But, we still need to recheckpoint
1503          * as we're enabling FP for the process; it will return, abort the
1504          * transaction, and probably retry but now with FP enabled.  So the
1505          * checkpointed FP registers need to be loaded.
1506          */
1507         tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1508         /* Reclaim didn't save out any FPRs to transact_fprs. */
1509
1510         /* Enable FP for the task: */
1511         regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1512
1513         /* This loads and recheckpoints the FP registers from
1514          * thread.fpr[].  They will remain in registers after the
1515          * checkpoint so we don't need to reload them after.
1516          * If VMX is in use, the VRs now hold checkpointed values,
1517          * so we don't want to load the VRs from the thread_struct.
1518          */
1519         tm_recheckpoint(&current->thread, MSR_FP);
1520
1521         /* If VMX is in use, get the transactional values back */
1522         if (regs->msr & MSR_VEC) {
1523                 msr_check_and_set(MSR_VEC);
1524                 load_vr_state(&current->thread.vr_state);
1525                 /* At this point all the VSX state is loaded, so enable it */
1526                 regs->msr |= MSR_VSX;
1527         }
1528 }
1529
1530 void altivec_unavailable_tm(struct pt_regs *regs)
1531 {
1532         /* See the comments in fp_unavailable_tm().  This function operates
1533          * the same way.
1534          */
1535
1536         TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1537                  "MSR=%lx\n",
1538                  regs->nip, regs->msr);
1539         tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1540         regs->msr |= MSR_VEC;
1541         tm_recheckpoint(&current->thread, MSR_VEC);
1542         current->thread.used_vr = 1;
1543
1544         if (regs->msr & MSR_FP) {
1545                 msr_check_and_set(MSR_FP);
1546                 load_fp_state(&current->thread.fp_state);
1547                 regs->msr |= MSR_VSX;
1548         }
1549 }
1550
1551 void vsx_unavailable_tm(struct pt_regs *regs)
1552 {
1553         unsigned long orig_msr = regs->msr;
1554
1555         /* See the comments in fp_unavailable_tm().  This works similarly,
1556          * though we're loading both FP and VEC registers in here.
1557          *
1558          * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1559          * regs.  Either way, set MSR_VSX.
1560          */
1561
1562         TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1563                  "MSR=%lx\n",
1564                  regs->nip, regs->msr);
1565
1566         current->thread.used_vsr = 1;
1567
1568         /* If FP and VMX are already loaded, we have all the state we need */
1569         if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1570                 regs->msr |= MSR_VSX;
1571                 return;
1572         }
1573
1574         /* This reclaims FP and/or VR regs if they're already enabled */
1575         tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1576
1577         regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1578                 MSR_VSX;
1579
1580         /* This loads & recheckpoints FP and VRs; but we have
1581          * to be sure not to overwrite previously-valid state.
1582          */
1583         tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
1584
1585         msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1586
1587         if (orig_msr & MSR_FP)
1588                 load_fp_state(&current->thread.fp_state);
1589         if (orig_msr & MSR_VEC)
1590                 load_vr_state(&current->thread.vr_state);
1591 }
1592 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1593
1594 void performance_monitor_exception(struct pt_regs *regs)
1595 {
1596         __this_cpu_inc(irq_stat.pmu_irqs);
1597
1598         perf_irq(regs);
1599 }
1600
1601 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1602 static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1603 {
1604         int changed = 0;
1605         /*
1606          * Determine the cause of the debug event, clear the
1607          * event flags and send a trap to the handler. Torez
1608          */
1609         if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1610                 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1611 #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1612                 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
1613 #endif
1614                 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1615                              5);
1616                 changed |= 0x01;
1617         }  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1618                 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1619                 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1620                              6);
1621                 changed |= 0x01;
1622         }  else if (debug_status & DBSR_IAC1) {
1623                 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
1624                 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1625                 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1626                              1);
1627                 changed |= 0x01;
1628         }  else if (debug_status & DBSR_IAC2) {
1629                 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
1630                 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1631                              2);
1632                 changed |= 0x01;
1633         }  else if (debug_status & DBSR_IAC3) {
1634                 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
1635                 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1636                 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1637                              3);
1638                 changed |= 0x01;
1639         }  else if (debug_status & DBSR_IAC4) {
1640                 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
1641                 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1642                              4);
1643                 changed |= 0x01;
1644         }
1645         /*
1646          * At the point this routine was called, the MSR(DE) was turned off.
1647          * Check all other debug flags and see if that bit needs to be turned
1648          * back on or not.
1649          */
1650         if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1651                                current->thread.debug.dbcr1))
1652                 regs->msr |= MSR_DE;
1653         else
1654                 /* Make sure the IDM flag is off */
1655                 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
1656
1657         if (changed & 0x01)
1658                 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
1659 }
1660
1661 void DebugException(struct pt_regs *regs, unsigned long debug_status)
1662 {
1663         current->thread.debug.dbsr = debug_status;
1664
1665         /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1666          * on server, it stops on the target of the branch. In order to simulate
1667          * the server behaviour, we thus restart right away with a single step
1668          * instead of stopping here when hitting a BT
1669          */
1670         if (debug_status & DBSR_BT) {
1671                 regs->msr &= ~MSR_DE;
1672
1673                 /* Disable BT */
1674                 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1675                 /* Clear the BT event */
1676                 mtspr(SPRN_DBSR, DBSR_BT);
1677
1678                 /* Do the single step trick only when coming from userspace */
1679                 if (user_mode(regs)) {
1680                         current->thread.debug.dbcr0 &= ~DBCR0_BT;
1681                         current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1682                         regs->msr |= MSR_DE;
1683                         return;
1684                 }
1685
1686                 if (kprobe_post_handler(regs))
1687                         return;
1688
1689                 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1690                                5, SIGTRAP) == NOTIFY_STOP) {
1691                         return;
1692                 }
1693                 if (debugger_sstep(regs))
1694                         return;
1695         } else if (debug_status & DBSR_IC) {    /* Instruction complete */
1696                 regs->msr &= ~MSR_DE;
1697
1698                 /* Disable instruction completion */
1699                 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1700                 /* Clear the instruction completion event */
1701                 mtspr(SPRN_DBSR, DBSR_IC);
1702
1703                 if (kprobe_post_handler(regs))
1704                         return;
1705
1706                 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1707                                5, SIGTRAP) == NOTIFY_STOP) {
1708                         return;
1709                 }
1710
1711                 if (debugger_sstep(regs))
1712                         return;
1713
1714                 if (user_mode(regs)) {
1715                         current->thread.debug.dbcr0 &= ~DBCR0_IC;
1716                         if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1717                                                current->thread.debug.dbcr1))
1718                                 regs->msr |= MSR_DE;
1719                         else
1720                                 /* Make sure the IDM bit is off */
1721                                 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
1722                 }
1723
1724                 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1725         } else
1726                 handle_debug(regs, debug_status);
1727 }
1728 NOKPROBE_SYMBOL(DebugException);
1729 #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
1730
1731 #if !defined(CONFIG_TAU_INT)
1732 void TAUException(struct pt_regs *regs)
1733 {
1734         printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
1735                regs->nip, regs->msr, regs->trap, print_tainted());
1736 }
1737 #endif /* CONFIG_INT_TAU */
1738
1739 #ifdef CONFIG_ALTIVEC
1740 void altivec_assist_exception(struct pt_regs *regs)
1741 {
1742         int err;
1743
1744         if (!user_mode(regs)) {
1745                 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1746                        " at %lx\n", regs->nip);
1747                 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
1748         }
1749
1750         flush_altivec_to_thread(current);
1751
1752         PPC_WARN_EMULATED(altivec, regs);
1753         err = emulate_altivec(regs);
1754         if (err == 0) {
1755                 regs->nip += 4;         /* skip emulated instruction */
1756                 emulate_single_step(regs);
1757                 return;
1758         }
1759
1760         if (err == -EFAULT) {
1761                 /* got an error reading the instruction */
1762                 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1763         } else {
1764                 /* didn't recognize the instruction */
1765                 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1766                 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1767                                    "in %s at %lx\n", current->comm, regs->nip);
1768                 current->thread.vr_state.vscr.u[3] |= 0x10000;
1769         }
1770 }
1771 #endif /* CONFIG_ALTIVEC */
1772
1773 #ifdef CONFIG_FSL_BOOKE
1774 void CacheLockingException(struct pt_regs *regs, unsigned long address,
1775                            unsigned long error_code)
1776 {
1777         /* We treat cache locking instructions from the user
1778          * as priv ops, in the future we could try to do
1779          * something smarter
1780          */
1781         if (error_code & (ESR_DLK|ESR_ILK))
1782                 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1783         return;
1784 }
1785 #endif /* CONFIG_FSL_BOOKE */
1786
1787 #ifdef CONFIG_SPE
1788 void SPEFloatingPointException(struct pt_regs *regs)
1789 {
1790         extern int do_spe_mathemu(struct pt_regs *regs);
1791         unsigned long spefscr;
1792         int fpexc_mode;
1793         int code = 0;
1794         int err;
1795
1796         flush_spe_to_thread(current);
1797
1798         spefscr = current->thread.spefscr;
1799         fpexc_mode = current->thread.fpexc_mode;
1800
1801         if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1802                 code = FPE_FLTOVF;
1803         }
1804         else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1805                 code = FPE_FLTUND;
1806         }
1807         else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1808                 code = FPE_FLTDIV;
1809         else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1810                 code = FPE_FLTINV;
1811         }
1812         else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1813                 code = FPE_FLTRES;
1814
1815         err = do_spe_mathemu(regs);
1816         if (err == 0) {
1817                 regs->nip += 4;         /* skip emulated instruction */
1818                 emulate_single_step(regs);
1819                 return;
1820         }
1821
1822         if (err == -EFAULT) {
1823                 /* got an error reading the instruction */
1824                 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1825         } else if (err == -EINVAL) {
1826                 /* didn't recognize the instruction */
1827                 printk(KERN_ERR "unrecognized spe instruction "
1828                        "in %s at %lx\n", current->comm, regs->nip);
1829         } else {
1830                 _exception(SIGFPE, regs, code, regs->nip);
1831         }
1832
1833         return;
1834 }
1835
1836 void SPEFloatingPointRoundException(struct pt_regs *regs)
1837 {
1838         extern int speround_handler(struct pt_regs *regs);
1839         int err;
1840
1841         preempt_disable();
1842         if (regs->msr & MSR_SPE)
1843                 giveup_spe(current);
1844         preempt_enable();
1845
1846         regs->nip -= 4;
1847         err = speround_handler(regs);
1848         if (err == 0) {
1849                 regs->nip += 4;         /* skip emulated instruction */
1850                 emulate_single_step(regs);
1851                 return;
1852         }
1853
1854         if (err == -EFAULT) {
1855                 /* got an error reading the instruction */
1856                 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1857         } else if (err == -EINVAL) {
1858                 /* didn't recognize the instruction */
1859                 printk(KERN_ERR "unrecognized spe instruction "
1860                        "in %s at %lx\n", current->comm, regs->nip);
1861         } else {
1862                 _exception(SIGFPE, regs, 0, regs->nip);
1863                 return;
1864         }
1865 }
1866 #endif
1867
1868 /*
1869  * We enter here if we get an unrecoverable exception, that is, one
1870  * that happened at a point where the RI (recoverable interrupt) bit
1871  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1872  * we therefore lost state by taking this exception.
1873  */
1874 void unrecoverable_exception(struct pt_regs *regs)
1875 {
1876         printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1877                regs->trap, regs->nip);
1878         die("Unrecoverable exception", regs, SIGABRT);
1879 }
1880 NOKPROBE_SYMBOL(unrecoverable_exception);
1881
1882 #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
1883 /*
1884  * Default handler for a Watchdog exception,
1885  * spins until a reboot occurs
1886  */
1887 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1888 {
1889         /* Generic WatchdogHandler, implement your own */
1890         mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1891         return;
1892 }
1893
1894 void WatchdogException(struct pt_regs *regs)
1895 {
1896         printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1897         WatchdogHandler(regs);
1898 }
1899 #endif
1900
1901 /*
1902  * We enter here if we discover during exception entry that we are
1903  * running in supervisor mode with a userspace value in the stack pointer.
1904  */
1905 void kernel_bad_stack(struct pt_regs *regs)
1906 {
1907         printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1908                regs->gpr[1], regs->nip);
1909         die("Bad kernel stack pointer", regs, SIGABRT);
1910 }
1911 NOKPROBE_SYMBOL(kernel_bad_stack);
1912
1913 void __init trap_init(void)
1914 {
1915 }
1916
1917
1918 #ifdef CONFIG_PPC_EMULATED_STATS
1919
1920 #define WARN_EMULATED_SETUP(type)       .type = { .name = #type }
1921
1922 struct ppc_emulated ppc_emulated = {
1923 #ifdef CONFIG_ALTIVEC
1924         WARN_EMULATED_SETUP(altivec),
1925 #endif
1926         WARN_EMULATED_SETUP(dcba),
1927         WARN_EMULATED_SETUP(dcbz),
1928         WARN_EMULATED_SETUP(fp_pair),
1929         WARN_EMULATED_SETUP(isel),
1930         WARN_EMULATED_SETUP(mcrxr),
1931         WARN_EMULATED_SETUP(mfpvr),
1932         WARN_EMULATED_SETUP(multiple),
1933         WARN_EMULATED_SETUP(popcntb),
1934         WARN_EMULATED_SETUP(spe),
1935         WARN_EMULATED_SETUP(string),
1936         WARN_EMULATED_SETUP(sync),
1937         WARN_EMULATED_SETUP(unaligned),
1938 #ifdef CONFIG_MATH_EMULATION
1939         WARN_EMULATED_SETUP(math),
1940 #endif
1941 #ifdef CONFIG_VSX
1942         WARN_EMULATED_SETUP(vsx),
1943 #endif
1944 #ifdef CONFIG_PPC64
1945         WARN_EMULATED_SETUP(mfdscr),
1946         WARN_EMULATED_SETUP(mtdscr),
1947         WARN_EMULATED_SETUP(lq_stq),
1948 #endif
1949 };
1950
1951 u32 ppc_warn_emulated;
1952
1953 void ppc_warn_emulated_print(const char *type)
1954 {
1955         pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1956                             type);
1957 }
1958
1959 static int __init ppc_warn_emulated_init(void)
1960 {
1961         struct dentry *dir, *d;
1962         unsigned int i;
1963         struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1964
1965         if (!powerpc_debugfs_root)
1966                 return -ENODEV;
1967
1968         dir = debugfs_create_dir("emulated_instructions",
1969                                  powerpc_debugfs_root);
1970         if (!dir)
1971                 return -ENOMEM;
1972
1973         d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1974                                &ppc_warn_emulated);
1975         if (!d)
1976                 goto fail;
1977
1978         for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1979                 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1980                                        (u32 *)&entries[i].val.counter);
1981                 if (!d)
1982                         goto fail;
1983         }
1984
1985         return 0;
1986
1987 fail:
1988         debugfs_remove_recursive(dir);
1989         return -ENOMEM;
1990 }
1991
1992 device_initcall(ppc_warn_emulated_init);
1993
1994 #endif /* CONFIG_PPC_EMULATED_STATS */