[SPARC64]: Don't be picky about virtual-dma values on sun4v.
[safe/jmp/linux-2.6] / arch / sparc64 / kernel / unaligned.c
index 9d6be20..953be81 100644 (file)
@@ -18,8 +18,8 @@
 #include <asm/system.h>
 #include <asm/uaccess.h>
 #include <linux/smp.h>
-#include <linux/smp_lock.h>
 #include <linux/bitops.h>
+#include <linux/kallsyms.h>
 #include <asm/fpumacro.h>
 
 /* #define DEBUG_MNA */
@@ -180,14 +180,14 @@ static void __attribute_used__ unaligned_panic(char *str, struct pt_regs *regs)
        die_if_kernel(str, regs);
 }
 
-extern void do_int_load(unsigned long *dest_reg, int size,
-                       unsigned long *saddr, int is_signed, int asi);
+extern int do_int_load(unsigned long *dest_reg, int size,
+                      unsigned long *saddr, int is_signed, int asi);
        
-extern void __do_int_store(unsigned long *dst_addr, int size,
-                          unsigned long src_val, int asi);
+extern int __do_int_store(unsigned long *dst_addr, int size,
+                         unsigned long src_val, int asi);
 
-static inline void do_int_store(int reg_num, int size, unsigned long *dst_addr,
-                               struct pt_regs *regs, int asi, int orig_asi)
+static inline int do_int_store(int reg_num, int size, unsigned long *dst_addr,
+                              struct pt_regs *regs, int asi, int orig_asi)
 {
        unsigned long zero = 0;
        unsigned long *src_val_p = &zero;
@@ -219,7 +219,7 @@ static inline void do_int_store(int reg_num, int size, unsigned long *dst_addr,
                        break;
                };
        }
-       __do_int_store(dst_addr, size, src_val, asi);
+       return __do_int_store(dst_addr, size, src_val, asi);
 }
 
 static inline void advance(struct pt_regs *regs)
@@ -242,7 +242,7 @@ static inline int ok_for_kernel(unsigned int insn)
        return !floating_point_load_or_store_p(insn);
 }
 
-void kernel_mna_trap_fault(void)
+static void kernel_mna_trap_fault(int fixup_tstate_asi)
 {
        struct pt_regs *regs = current_thread_info()->kern_una_regs;
        unsigned int insn = current_thread_info()->kern_una_insn;
@@ -273,28 +273,57 @@ void kernel_mna_trap_fault(void)
        regs->tpc = entry->fixup;
        regs->tnpc = regs->tpc + 4;
 
-       regs->tstate &= ~TSTATE_ASI;
-       regs->tstate |= (ASI_AIUS << 24UL);
+       if (fixup_tstate_asi) {
+               regs->tstate &= ~TSTATE_ASI;
+               regs->tstate |= (ASI_AIUS << 24UL);
+       }
+}
+
+static void log_unaligned(struct pt_regs *regs)
+{
+       static unsigned long count, last_time;
+
+       if (jiffies - last_time > 5 * HZ)
+               count = 0;
+       if (count < 5) {
+               last_time = jiffies;
+               count++;
+               printk("Kernel unaligned access at TPC[%lx] ", regs->tpc);
+               print_symbol("%s\n", regs->tpc);
+       }
 }
 
-asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn, unsigned long sfar, unsigned long sfsr)
+asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
 {
        enum direction dir = decode_direction(insn);
        int size = decode_access_size(insn);
+       int orig_asi, asi;
 
        current_thread_info()->kern_una_regs = regs;
        current_thread_info()->kern_una_insn = insn;
 
+       orig_asi = asi = decode_asi(insn, regs);
+
+       /* If this is a {get,put}_user() on an unaligned userspace pointer,
+        * just signal a fault and do not log the event.
+        */
+       if (asi == ASI_AIUS) {
+               kernel_mna_trap_fault(0);
+               return;
+       }
+
+       log_unaligned(regs);
+
        if (!ok_for_kernel(insn) || dir == both) {
                printk("Unsupported unaligned load/store trap for kernel "
                       "at <%016lx>.\n", regs->tpc);
                unaligned_panic("Kernel does fpu/atomic "
                                "unaligned load/store.", regs);
 
-               kernel_mna_trap_fault();
+               kernel_mna_trap_fault(0);
        } else {
                unsigned long addr, *reg_addr;
-               int orig_asi, asi;
+               int err;
 
                addr = compute_effective_address(regs, insn,
                                                 ((insn >> 25) & 0x1f));
@@ -304,7 +333,6 @@ asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn, u
                       regs->tpc, dirstrings[dir], addr, size,
                       regs->u_regs[UREG_RETPC]);
 #endif
-               orig_asi = asi = decode_asi(insn, regs);
                switch (asi) {
                case ASI_NL:
                case ASI_AIUPL:
@@ -319,9 +347,10 @@ asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn, u
                switch (dir) {
                case load:
                        reg_addr = fetch_reg_addr(((insn>>25)&0x1f), regs);
-                       do_int_load(reg_addr, size, (unsigned long *) addr,
-                                   decode_signedness(insn), asi);
-                       if (unlikely(asi != orig_asi)) {
+                       err = do_int_load(reg_addr, size,
+                                         (unsigned long *) addr,
+                                         decode_signedness(insn), asi);
+                       if (likely(!err) && unlikely(asi != orig_asi)) {
                                unsigned long val_in = *reg_addr;
                                switch (size) {
                                case 2:
@@ -343,16 +372,19 @@ asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn, u
                        break;
 
                case store:
-                       do_int_store(((insn>>25)&0x1f), size,
-                                    (unsigned long *) addr, regs,
-                                    asi, orig_asi);
+                       err = do_int_store(((insn>>25)&0x1f), size,
+                                          (unsigned long *) addr, regs,
+                                          asi, orig_asi);
                        break;
 
                default:
                        panic("Impossible kernel unaligned trap.");
                        /* Not reached... */
                }
-               advance(regs);
+               if (unlikely(err))
+                       kernel_mna_trap_fault(1);
+               else
+                       advance(regs);
        }
 }
 
@@ -401,6 +433,9 @@ extern void do_privact(struct pt_regs *regs);
 extern void spitfire_data_access_exception(struct pt_regs *regs,
                                           unsigned long sfsr,
                                           unsigned long sfar);
+extern void sun4v_data_access_exception(struct pt_regs *regs,
+                                       unsigned long addr,
+                                       unsigned long type_ctx);
 
 int handle_ldf_stq(u32 insn, struct pt_regs *regs)
 {
@@ -443,14 +478,20 @@ int handle_ldf_stq(u32 insn, struct pt_regs *regs)
                                break;
                        }
                default:
-                       spitfire_data_access_exception(regs, 0, addr);
+                       if (tlb_type == hypervisor)
+                               sun4v_data_access_exception(regs, addr, 0);
+                       else
+                               spitfire_data_access_exception(regs, 0, addr);
                        return 1;
                }
                if (put_user (first >> 32, (u32 __user *)addr) ||
                    __put_user ((u32)first, (u32 __user *)(addr + 4)) ||
                    __put_user (second >> 32, (u32 __user *)(addr + 8)) ||
                    __put_user ((u32)second, (u32 __user *)(addr + 12))) {
-                       spitfire_data_access_exception(regs, 0, addr);
+                       if (tlb_type == hypervisor)
+                               sun4v_data_access_exception(regs, addr, 0);
+                       else
+                               spitfire_data_access_exception(regs, 0, addr);
                        return 1;
                }
        } else {
@@ -463,7 +504,10 @@ int handle_ldf_stq(u32 insn, struct pt_regs *regs)
                        do_privact(regs);
                        return 1;
                } else if (asi > ASI_SNFL) {
-                       spitfire_data_access_exception(regs, 0, addr);
+                       if (tlb_type == hypervisor)
+                               sun4v_data_access_exception(regs, addr, 0);
+                       else
+                               spitfire_data_access_exception(regs, 0, addr);
                        return 1;
                }
                switch (insn & 0x180000) {
@@ -480,7 +524,10 @@ int handle_ldf_stq(u32 insn, struct pt_regs *regs)
                                err |= __get_user (data[i], (u32 __user *)(addr + 4*i));
                }
                if (err && !(asi & 0x2 /* NF */)) {
-                       spitfire_data_access_exception(regs, 0, addr);
+                       if (tlb_type == hypervisor)
+                               sun4v_data_access_exception(regs, addr, 0);
+                       else
+                               spitfire_data_access_exception(regs, 0, addr);
                        return 1;
                }
                if (asi & 0x8) /* Little */ {
@@ -544,7 +591,7 @@ void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr
        u32 insn;
        u32 first, second;
        u64 value;
-       u8 asi, freg;
+       u8 freg;
        int flag;
        struct fpustate *f = FPUSTATE;
 
@@ -553,7 +600,7 @@ void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr
        if (test_thread_flag(TIF_32BIT))
                pc = (u32)pc;
        if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
-               asi = sfsr >> 16;
+               int asi = decode_asi(insn, regs);
                if ((asi > ASI_SNFL) ||
                    (asi < ASI_P))
                        goto daex;
@@ -583,7 +630,11 @@ void handle_lddfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr
                *(u64 *)(f->regs + freg) = value;
                current_thread_info()->fpsaved[0] |= flag;
        } else {
-daex:          spitfire_data_access_exception(regs, sfsr, sfar);
+daex:
+               if (tlb_type == hypervisor)
+                       sun4v_data_access_exception(regs, sfar, sfsr);
+               else
+                       spitfire_data_access_exception(regs, sfsr, sfar);
                return;
        }
        advance(regs);
@@ -596,7 +647,7 @@ void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr
        unsigned long tstate = regs->tstate;
        u32 insn;
        u64 value;
-       u8 asi, freg;
+       u8 freg;
        int flag;
        struct fpustate *f = FPUSTATE;
 
@@ -605,8 +656,8 @@ void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr
        if (test_thread_flag(TIF_32BIT))
                pc = (u32)pc;
        if (get_user(insn, (u32 __user *) pc) != -EFAULT) {
+               int asi = decode_asi(insn, regs);
                freg = ((insn >> 25) & 0x1e) | ((insn >> 20) & 0x20);
-               asi = sfsr >> 16;
                value = 0;
                flag = (freg < 32) ? FPRS_DL : FPRS_DU;
                if ((asi > ASI_SNFL) ||
@@ -627,7 +678,11 @@ void handle_stdfmna(struct pt_regs *regs, unsigned long sfar, unsigned long sfsr
                    __put_user ((u32)value, (u32 __user *)(sfar + 4)))
                        goto daex;
        } else {
-daex:          spitfire_data_access_exception(regs, sfsr, sfar);
+daex:
+               if (tlb_type == hypervisor)
+                       sun4v_data_access_exception(regs, sfar, sfsr);
+               else
+                       spitfire_data_access_exception(regs, sfsr, sfar);
                return;
        }
        advance(regs);