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