ARM: convert /proc/cpu/aligment to seq_file
[safe/jmp/linux-2.6] / arch / arm / mm / alignment.c
index 133e65d..28b7c27 100644 (file)
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
+#include <linux/moduleparam.h>
 #include <linux/compiler.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <linux/proc_fs.h>
+#include <linux/seq_file.h>
 #include <linux/init.h>
+#include <linux/sched.h>
 #include <linux/uaccess.h>
 
 #include <asm/unaligned.h>
 #define SHIFT_ASR      0x40
 #define SHIFT_RORRRX   0x60
 
+#define BAD_INSTR      0xdeadc0de
+
+/* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
+#define IS_T32(hi16) \
+       (((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
+
 static unsigned long ai_user;
 static unsigned long ai_sys;
 static unsigned long ai_skipped;
@@ -70,6 +79,12 @@ static unsigned long ai_dword;
 static unsigned long ai_multi;
 static int ai_usermode;
 
+core_param(alignment, ai_usermode, int, 0600);
+
+#define UM_WARN                (1 << 0)
+#define UM_FIXUP       (1 << 1)
+#define UM_SIGNAL      (1 << 2)
+
 #ifdef CONFIG_PROC_FS
 static const char *usermode_action[] = {
        "ignored",
@@ -80,36 +95,29 @@ static const char *usermode_action[] = {
        "signal+warn"
 };
 
-static int
-proc_alignment_read(char *page, char **start, off_t off, int count, int *eof,
-                   void *data)
+static int alignment_proc_show(struct seq_file *m, void *v)
 {
-       char *p = page;
-       int len;
-
-       p += sprintf(p, "User:\t\t%lu\n", ai_user);
-       p += sprintf(p, "System:\t\t%lu\n", ai_sys);
-       p += sprintf(p, "Skipped:\t%lu\n", ai_skipped);
-       p += sprintf(p, "Half:\t\t%lu\n", ai_half);
-       p += sprintf(p, "Word:\t\t%lu\n", ai_word);
+       seq_printf(m, "User:\t\t%lu\n", ai_user);
+       seq_printf(m, "System:\t\t%lu\n", ai_sys);
+       seq_printf(m, "Skipped:\t%lu\n", ai_skipped);
+       seq_printf(m, "Half:\t\t%lu\n", ai_half);
+       seq_printf(m, "Word:\t\t%lu\n", ai_word);
        if (cpu_architecture() >= CPU_ARCH_ARMv5TE)
-               p += sprintf(p, "DWord:\t\t%lu\n", ai_dword);
-       p += sprintf(p, "Multi:\t\t%lu\n", ai_multi);
-       p += sprintf(p, "User faults:\t%i (%s)\n", ai_usermode,
+               seq_printf(m, "DWord:\t\t%lu\n", ai_dword);
+       seq_printf(m, "Multi:\t\t%lu\n", ai_multi);
+       seq_printf(m, "User faults:\t%i (%s)\n", ai_usermode,
                        usermode_action[ai_usermode]);
 
-       len = (p - page) - off;
-       if (len < 0)
-               len = 0;
-
-       *eof = (len <= count) ? 1 : 0;
-       *start = page + off;
+       return 0;
+}
 
-       return len;
+static int alignment_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, alignment_proc_show, NULL);
 }
 
-static int proc_alignment_write(struct file *file, const char __user *buffer,
-                               unsigned long count, void *data)
+static ssize_t alignment_proc_write(struct file *file, const char __user *buffer,
+                                   size_t count, loff_t *pos)
 {
        char mode;
 
@@ -122,6 +130,13 @@ static int proc_alignment_write(struct file *file, const char __user *buffer,
        return count;
 }
 
+static const struct file_operations alignment_proc_fops = {
+       .open           = alignment_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = alignment_proc_write,
+};
 #endif /* CONFIG_PROC_FS */
 
 union offset_union {
@@ -148,7 +163,9 @@ union offset_union {
 
 #define __get8_unaligned_check(ins,val,addr,err)       \
        __asm__(                                        \
-       "1:     "ins"   %1, [%2], #1\n"                 \
+ ARM(  "1:     "ins"   %1, [%2], #1\n" )               \
+ THUMB(        "1:     "ins"   %1, [%2]\n"     )               \
+ THUMB(        "       add     %2, %2, #1\n"   )               \
        "2:\n"                                          \
        "       .section .fixup,\"ax\"\n"               \
        "       .align  2\n"                            \
@@ -204,7 +221,9 @@ union offset_union {
        do {                                                    \
                unsigned int err = 0, v = val, a = addr;        \
                __asm__( FIRST_BYTE_16                          \
-               "1:     "ins"   %1, [%2], #1\n"                 \
+        ARM(   "1:     "ins"   %1, [%2], #1\n" )               \
+        THUMB( "1:     "ins"   %1, [%2]\n"     )               \
+        THUMB( "       add     %2, %2, #1\n"   )               \
                "       mov     %1, %1, "NEXT_BYTE"\n"          \
                "2:     "ins"   %1, [%2]\n"                     \
                "3:\n"                                          \
@@ -234,11 +253,17 @@ union offset_union {
        do {                                                    \
                unsigned int err = 0, v = val, a = addr;        \
                __asm__( FIRST_BYTE_32                          \
-               "1:     "ins"   %1, [%2], #1\n"                 \
+        ARM(   "1:     "ins"   %1, [%2], #1\n" )               \
+        THUMB( "1:     "ins"   %1, [%2]\n"     )               \
+        THUMB( "       add     %2, %2, #1\n"   )               \
                "       mov     %1, %1, "NEXT_BYTE"\n"          \
-               "2:     "ins"   %1, [%2], #1\n"                 \
+        ARM(   "2:     "ins"   %1, [%2], #1\n" )               \
+        THUMB( "2:     "ins"   %1, [%2]\n"     )               \
+        THUMB( "       add     %2, %2, #1\n"   )               \
                "       mov     %1, %1, "NEXT_BYTE"\n"          \
-               "3:     "ins"   %1, [%2], #1\n"                 \
+        ARM(   "3:     "ins"   %1, [%2], #1\n" )               \
+        THUMB( "3:     "ins"   %1, [%2]\n"     )               \
+        THUMB( "       add     %2, %2, #1\n"   )               \
                "       mov     %1, %1, "NEXT_BYTE"\n"          \
                "4:     "ins"   %1, [%2]\n"                     \
                "5:\n"                                          \
@@ -327,38 +352,48 @@ do_alignment_ldrdstrd(unsigned long addr, unsigned long instr,
                      struct pt_regs *regs)
 {
        unsigned int rd = RD_BITS(instr);
-
-       if (((rd & 1) == 1) || (rd == 14))
+       unsigned int rd2;
+       int load;
+
+       if ((instr & 0xfe000000) == 0xe8000000) {
+               /* ARMv7 Thumb-2 32-bit LDRD/STRD */
+               rd2 = (instr >> 8) & 0xf;
+               load = !!(LDST_L_BIT(instr));
+       } else if (((rd & 1) == 1) || (rd == 14))
                goto bad;
+       else {
+               load = ((instr & 0xf0) == 0xd0);
+               rd2 = rd + 1;
+       }
 
        ai_dword += 1;
 
        if (user_mode(regs))
                goto user;
 
-       if ((instr & 0xf0) == 0xd0) {
+       if (load) {
                unsigned long val;
                get32_unaligned_check(val, addr);
                regs->uregs[rd] = val;
                get32_unaligned_check(val, addr + 4);
-               regs->uregs[rd + 1] = val;
+               regs->uregs[rd2] = val;
        } else {
                put32_unaligned_check(regs->uregs[rd], addr);
-               put32_unaligned_check(regs->uregs[rd + 1], addr + 4);
+               put32_unaligned_check(regs->uregs[rd2], addr + 4);
        }
 
        return TYPE_LDST;
 
  user:
-       if ((instr & 0xf0) == 0xd0) {
+       if (load) {
                unsigned long val;
                get32t_unaligned_check(val, addr);
                regs->uregs[rd] = val;
                get32t_unaligned_check(val, addr + 4);
-               regs->uregs[rd + 1] = val;
+               regs->uregs[rd2] = val;
        } else {
                put32t_unaligned_check(regs->uregs[rd], addr);
-               put32t_unaligned_check(regs->uregs[rd + 1], addr + 4);
+               put32t_unaligned_check(regs->uregs[rd2], addr + 4);
        }
 
        return TYPE_LDST;
@@ -611,8 +646,72 @@ thumb2arm(u16 tinstr)
                /* Else fall through for illegal instruction case */
 
        default:
-               return 0xdeadc0de;
+               return BAD_INSTR;
+       }
+}
+
+/*
+ * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
+ * handlable by ARM alignment handler, also find the corresponding handler,
+ * so that we can reuse ARM userland alignment fault fixups for Thumb.
+ *
+ * @pinstr: original Thumb-2 instruction; returns new handlable instruction
+ * @regs: register context.
+ * @poffset: return offset from faulted addr for later writeback
+ *
+ * NOTES:
+ * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
+ * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
+ */
+static void *
+do_alignment_t32_to_handler(unsigned long *pinstr, struct pt_regs *regs,
+                           union offset_union *poffset)
+{
+       unsigned long instr = *pinstr;
+       u16 tinst1 = (instr >> 16) & 0xffff;
+       u16 tinst2 = instr & 0xffff;
+       poffset->un = 0;
+
+       switch (tinst1 & 0xffe0) {
+       /* A6.3.5 Load/Store multiple */
+       case 0xe880:            /* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
+       case 0xe8a0:            /* ...above writeback version */
+       case 0xe900:            /* STMDB/STMFD, LDMDB/LDMEA */
+       case 0xe920:            /* ...above writeback version */
+               /* no need offset decision since handler calculates it */
+               return do_alignment_ldmstm;
+
+       case 0xf840:            /* POP/PUSH T3 (single register) */
+               if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
+                       u32 L = !!(LDST_L_BIT(instr));
+                       const u32 subset[2] = {
+                               0xe92d0000,     /* STMDB sp!,{registers} */
+                               0xe8bd0000,     /* LDMIA sp!,{registers} */
+                       };
+                       *pinstr = subset[L] | (1<<RD_BITS(instr));
+                       return do_alignment_ldmstm;
+               }
+               /* Else fall through for illegal instruction case */
+               break;
+
+       /* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
+       case 0xe860:
+       case 0xe960:
+       case 0xe8e0:
+       case 0xe9e0:
+               poffset->un = (tinst2 & 0xff) << 2;
+       case 0xe940:
+       case 0xe9c0:
+               return do_alignment_ldrdstrd;
+
+       /*
+        * No need to handle load/store instructions up to word size
+        * since ARMv6 and later CPUs can perform unaligned accesses.
+        */
+       default:
+               break;
        }
+       return NULL;
 }
 
 static int
@@ -625,6 +724,8 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
        mm_segment_t fs;
        unsigned int fault;
        u16 tinstr = 0;
+       int isize = 4;
+       int thumb2_32b = 0;
 
        instrptr = instruction_pointer(regs);
 
@@ -632,8 +733,19 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
        set_fs(KERNEL_DS);
        if (thumb_mode(regs)) {
                fault = __get_user(tinstr, (u16 *)(instrptr & ~1));
-               if (!(fault))
-                       instr = thumb2arm(tinstr);
+               if (!fault) {
+                       if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
+                           IS_T32(tinstr)) {
+                               /* Thumb-2 32-bit */
+                               u16 tinst2 = 0;
+                               fault = __get_user(tinst2, (u16 *)(instrptr+2));
+                               instr = (tinstr << 16) | tinst2;
+                               thumb2_32b = 1;
+                       } else {
+                               isize = 2;
+                               instr = thumb2arm(tinstr);
+                       }
+               }
        } else
                fault = __get_user(instr, (u32 *)instrptr);
        set_fs(fs);
@@ -650,7 +762,7 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
 
  fixup:
 
-       regs->ARM_pc += thumb_mode(regs) ? 2 : 4;
+       regs->ARM_pc += isize;
 
        switch (CODING_BITS(instr)) {
        case 0x00000000:        /* 3.13.4 load/store instruction extensions */
@@ -709,18 +821,25 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
                handler = do_alignment_ldrstr;
                break;
 
-       case 0x08000000:        /* ldm or stm */
-               handler = do_alignment_ldmstm;
+       case 0x08000000:        /* ldm or stm, or thumb-2 32bit instruction */
+               if (thumb2_32b)
+                       handler = do_alignment_t32_to_handler(&instr, regs, &offset);
+               else
+                       handler = do_alignment_ldmstm;
                break;
 
        default:
                goto bad;
        }
 
+       if (!handler)
+               goto bad;
        type = handler(addr, instr, regs);
 
-       if (type == TYPE_ERROR || type == TYPE_FAULT)
+       if (type == TYPE_ERROR || type == TYPE_FAULT) {
+               regs->ARM_pc -= isize;
                goto bad_or_fault;
+       }
 
        if (type == TYPE_LDST)
                do_alignment_finish_ldst(addr, instr, regs, offset);
@@ -730,7 +849,6 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  bad_or_fault:
        if (type == TYPE_ERROR)
                goto bad;
-       regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
        /*
         * We got a fault - fix it up, or die.
         */
@@ -746,26 +864,26 @@ do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
         */
        printk(KERN_ERR "Alignment trap: not handling instruction "
                "%0*lx at [<%08lx>]\n",
-               thumb_mode(regs) ? 4 : 8,
-               thumb_mode(regs) ? tinstr : instr, instrptr);
+               isize << 1,
+               isize == 2 ? tinstr : instr, instrptr);
        ai_skipped += 1;
        return 1;
 
  user:
        ai_user += 1;
 
-       if (ai_usermode & 1)
+       if (ai_usermode & UM_WARN)
                printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*lx "
                       "Address=0x%08lx FSR 0x%03x\n", current->comm,
                        task_pid_nr(current), instrptr,
-                       thumb_mode(regs) ? 4 : 8,
-                       thumb_mode(regs) ? tinstr : instr,
+                       isize << 1,
+                       isize == 2 ? tinstr : instr,
                        addr, fsr);
 
-       if (ai_usermode & 2)
+       if (ai_usermode & UM_FIXUP)
                goto fixup;
 
-       if (ai_usermode & 4)
+       if (ai_usermode & UM_SIGNAL)
                force_sig(SIGBUS, current);
        else
                set_cr(cr_no_alignment);
@@ -784,18 +902,28 @@ static int __init alignment_init(void)
 #ifdef CONFIG_PROC_FS
        struct proc_dir_entry *res;
 
-       res = proc_mkdir("cpu", NULL);
-       if (!res)
-               return -ENOMEM;
-
-       res = create_proc_entry("alignment", S_IWUSR | S_IRUGO, res);
+       res = proc_create("cpu/alignment", S_IWUSR | S_IRUGO, NULL,
+                         &alignment_proc_fops);
        if (!res)
                return -ENOMEM;
-
-       res->read_proc = proc_alignment_read;
-       res->write_proc = proc_alignment_write;
 #endif
 
+       /*
+        * ARMv6 and later CPUs can perform unaligned accesses for
+        * most single load and store instructions up to word size.
+        * LDM, STM, LDRD and STRD still need to be handled.
+        *
+        * Ignoring the alignment fault is not an option on these
+        * CPUs since we spin re-faulting the instruction without
+        * making any progress.
+        */
+       if (cpu_architecture() >= CPU_ARCH_ARMv6 && (cr_alignment & CR_U)) {
+               cr_alignment &= ~CR_A;
+               cr_no_alignment &= ~CR_A;
+               set_cr(cr_alignment);
+               ai_usermode = UM_FIXUP;
+       }
+
        hook_fault_code(1, do_alignment, SIGILL, "alignment exception");
        hook_fault_code(3, do_alignment, SIGILL, "alignment exception");