sh: convert /proc/cpu/aligmnent, /proc/cpu/kernel_alignment to seq_file
[safe/jmp/linux-2.6] / arch / sh / kernel / traps_32.c
index c0aa3d8..3da5a12 100644 (file)
@@ -24,6 +24,9 @@
 #include <linux/kdebug.h>
 #include <linux/kexec.h>
 #include <linux/limits.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+#include <linux/sysfs.h>
 #include <asm/system.h>
 #include <asm/uaccess.h>
 #include <asm/fpu.h>
@@ -34,6 +37,7 @@
 # define TRAP_ILLEGAL_SLOT_INST        6
 # define TRAP_ADDRESS_ERROR    9
 # ifdef CONFIG_CPU_SH2A
+#  define TRAP_UBC             12
 #  define TRAP_FPU_ERROR       13
 #  define TRAP_DIVZERO_ERROR   17
 #  define TRAP_DIVOVF_ERROR    18
 #define TRAP_ILLEGAL_SLOT_INST 13
 #endif
 
+static unsigned long se_user;
+static unsigned long se_sys;
+static unsigned long se_half;
+static unsigned long se_word;
+static unsigned long se_dword;
+static unsigned long se_multi;
+/* bitfield: 1: warn 2: fixup 4: signal -> combinations 2|4 && 1|2|4 are not
+   valid! */
+static int se_usermode = 3;
+/* 0: no warning 1: print a warning message, disabled by default */
+static int se_kernmode_warn;
+
+#ifdef CONFIG_PROC_FS
+static const char *se_usermode_action[] = {
+       "ignored",
+       "warn",
+       "fixup",
+       "fixup+warn",
+       "signal",
+       "signal+warn"
+};
+
+static int alignment_proc_show(struct seq_file *m, void *v)
+{
+       seq_printf(m, "User:\t\t%lu\n", se_user);
+       seq_printf(m, "System:\t\t%lu\n", se_sys);
+       seq_printf(m, "Half:\t\t%lu\n", se_half);
+       seq_printf(m, "Word:\t\t%lu\n", se_word);
+       seq_printf(m, "DWord:\t\t%lu\n", se_dword);
+       seq_printf(m, "Multi:\t\t%lu\n", se_multi);
+       seq_printf(m, "User faults:\t%i (%s)\n", se_usermode,
+                       se_usermode_action[se_usermode]);
+       seq_printf(m, "Kernel faults:\t%i (fixup%s)\n", se_kernmode_warn,
+                       se_kernmode_warn ? "+warn" : "");
+       return 0;
+}
+
+static int alignment_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, alignment_proc_show, NULL);
+}
+
+static ssize_t alignment_proc_write(struct file *file,
+               const char __user *buffer, size_t count, loff_t *pos)
+{
+       int *data = PDE(file->f_path.dentry->d_inode)->data;
+       char mode;
+
+       if (count > 0) {
+               if (get_user(mode, buffer))
+                       return -EFAULT;
+               if (mode >= '0' && mode <= '5')
+                       *data = mode - '0';
+       }
+       return count;
+}
+
+static const struct file_operations alignment_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = alignment_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = alignment_proc_write,
+};
+#endif
+
 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
 {
        unsigned long p;
@@ -78,12 +149,12 @@ void die(const char * str, struct pt_regs * regs, long err)
 
        oops_enter();
 
-       console_verbose();
        spin_lock_irq(&die_lock);
+       console_verbose();
        bust_spinlocks(1);
 
        printk("%s: %04lx [#%d]\n", str, err & 0xffff, ++die_counter);
-
+       sysfs_printk_last_file();
        print_modules();
        show_regs(regs);
 
@@ -99,6 +170,7 @@ void die(const char * str, struct pt_regs * regs, long err)
        bust_spinlocks(0);
        add_taint(TAINT_DIE);
        spin_unlock_irq(&die_lock);
+       oops_exit();
 
        if (kexec_should_crash(current))
                crash_kexec(regs);
@@ -109,7 +181,6 @@ void die(const char * str, struct pt_regs * regs, long err)
        if (panic_on_oops)
                panic("Fatal exception");
 
-       oops_exit();
        do_exit(SIGSEGV);
 }
 
@@ -125,20 +196,19 @@ static inline void die_if_kernel(const char *str, struct pt_regs *regs,
  * - userspace errors just cause EFAULT to be returned, resulting in SEGV
  * - kernel/userspace interfaces cause a jump to an appropriate handler
  * - other kernel errors are bad
- * - return 0 if fixed-up, -EFAULT if non-fatal (to the kernel) fault
  */
-static int die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
+static void die_if_no_fixup(const char * str, struct pt_regs * regs, long err)
 {
        if (!user_mode(regs)) {
                const struct exception_table_entry *fixup;
                fixup = search_exception_tables(regs->pc);
                if (fixup) {
                        regs->pc = fixup->fixup;
-                       return 0;
+                       return;
                }
+
                die(str, regs, err);
        }
-       return -EFAULT;
 }
 
 static inline void sign_extend(unsigned int count, unsigned char *dst)
@@ -178,7 +248,7 @@ static struct mem_access user_mem_access = {
  *   (if that instruction is in a branch delay slot)
  * - return 0 if emulation okay, -EFAULT on existential error
  */
-static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
+static int handle_unaligned_ins(insn_size_t instruction, struct pt_regs *regs,
                                struct mem_access *ma)
 {
        int ret, index, count;
@@ -194,6 +264,13 @@ static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
 
        count = 1<<(instruction&3);
 
+       switch (count) {
+       case 1: se_half  += 1; break;
+       case 2: se_word  += 1; break;
+       case 4: se_dword += 1; break;
+       case 8: se_multi += 1; break; /* ??? */
+       }
+
        ret = -EFAULT;
        switch (instruction>>12) {
        case 0: /* mov.[bwl] to/from memory via r0+rn */
@@ -314,7 +391,8 @@ static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
        /* Argh. Address not only misaligned but also non-existent.
         * Raise an EFAULT and see if it's trapped
         */
-       return die_if_no_fixup("Fault in unaligned fixup", regs, 0);
+       die_if_no_fixup("Fault in unaligned fixup", regs, 0);
+       return -EFAULT;
 }
 
 /*
@@ -322,10 +400,10 @@ static int handle_unaligned_ins(opcode_t instruction, struct pt_regs *regs,
  * - fetches the instruction from PC+2
  */
 static inline int handle_delayslot(struct pt_regs *regs,
-                                  opcode_t old_instruction,
+                                  insn_size_t old_instruction,
                                   struct mem_access *ma)
 {
-       opcode_t instruction;
+       insn_size_t instruction;
        void __user *addr = (void __user *)(regs->pc +
                instruction_size(old_instruction));
 
@@ -358,31 +436,28 @@ static inline int handle_delayslot(struct pt_regs *regs,
 #define SH_PC_8BIT_OFFSET(instr) ((((signed char)(instr))*2) + 4)
 #define SH_PC_12BIT_OFFSET(instr) ((((signed short)(instr<<4))>>3) + 4)
 
-/*
- * XXX: SH-2A needs this too, but it needs an overhaul thanks to mixed 32-bit
- * opcodes..
- */
-
-static int handle_unaligned_notify_count = 10;
-
-int handle_unaligned_access(opcode_t instruction, struct pt_regs *regs,
-                           struct mem_access *ma)
+int handle_unaligned_access(insn_size_t instruction, struct pt_regs *regs,
+                           struct mem_access *ma, int expected)
 {
        u_int rm;
        int ret, index;
 
+       /*
+        * XXX: We can't handle mixed 16/32-bit instructions yet
+        */
+       if (instruction_size(instruction) != 2)
+               return -EINVAL;
+
        index = (instruction>>8)&15;    /* 0x0F00 */
        rm = regs->regs[index];
 
-       /* shout about the first ten userspace fixups */
-       if (user_mode(regs) && handle_unaligned_notify_count>0) {
-               handle_unaligned_notify_count--;
-
-               printk(KERN_NOTICE "Fixing up unaligned userspace access "
+       /* shout about fixups */
+       if (!expected && printk_ratelimit())
+               printk(KERN_NOTICE "Fixing up unaligned %s access "
                       "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
+                      user_mode(regs) ? "userspace" : "kernel",
                       current->comm, task_pid_nr(current),
                       (void *)regs->pc, instruction);
-       }
 
        ret = -EFAULT;
        switch (instruction&0xF000) {
@@ -523,7 +598,7 @@ asmlinkage void do_address_error(struct pt_regs *regs,
        unsigned long error_code = 0;
        mm_segment_t oldfs;
        siginfo_t info;
-       opcode_t instruction;
+       insn_size_t instruction;
        int tmp;
 
        /* Intentional ifdef */
@@ -538,24 +613,44 @@ asmlinkage void do_address_error(struct pt_regs *regs,
 
                local_irq_enable();
 
-               /* bad PC is not something we can fix */
-               if (regs->pc & 1) {
-                       si_code = BUS_ADRALN;
-                       goto uspace_segv;
-               }
+               se_user += 1;
 
                set_fs(USER_DS);
-               if (copy_from_user(&instruction, (void __user *)(regs->pc),
+               if (copy_from_user(&instruction, (insn_size_t *)(regs->pc & ~1),
                                   sizeof(instruction))) {
-                       /* Argh. Fault on the instruction itself.
-                          This should never happen non-SMP
-                       */
                        set_fs(oldfs);
                        goto uspace_segv;
                }
+               set_fs(oldfs);
+
+               /* shout about userspace fixups */
+               if (se_usermode & 1)
+                       printk(KERN_NOTICE "Unaligned userspace access "
+                              "in \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
+                              current->comm, current->pid, (void *)regs->pc,
+                              instruction);
+
+               if (se_usermode & 2)
+                       goto fixup;
+
+               if (se_usermode & 4)
+                       goto uspace_segv;
+               else {
+                       /* ignore */
+                       regs->pc += instruction_size(instruction);
+                       return;
+               }
 
+fixup:
+               /* bad PC is not something we can fix */
+               if (regs->pc & 1) {
+                       si_code = BUS_ADRALN;
+                       goto uspace_segv;
+               }
+
+               set_fs(USER_DS);
                tmp = handle_unaligned_access(instruction, regs,
-                                             &user_mem_access);
+                                             &user_mem_access, 0);
                set_fs(oldfs);
 
                if (tmp==0)
@@ -571,6 +666,8 @@ uspace_segv:
                info.si_addr = (void __user *)address;
                force_sig_info(SIGBUS, &info, current);
        } else {
+               se_sys += 1;
+
                if (regs->pc & 1)
                        die("unaligned program counter", regs, error_code);
 
@@ -584,7 +681,14 @@ uspace_segv:
                        die("insn faulting in do_address_error", regs, 0);
                }
 
-               handle_unaligned_access(instruction, regs, &user_mem_access);
+               if (se_kernmode_warn)
+                       printk(KERN_NOTICE "Unaligned kernel access "
+                              "on behalf of \"%s\" pid=%d pc=0x%p ins=0x%04hx\n",
+                              current->comm, current->pid, (void *)regs->pc,
+                              instruction);
+
+               handle_unaligned_access(instruction, regs,
+                                       &user_mem_access, 0);
                set_fs(oldfs);
        }
 }
@@ -665,6 +769,8 @@ asmlinkage void do_reserved_inst(unsigned long r4, unsigned long r5,
        if (is_dsp_inst(regs)) {
                /* Enable DSP mode, and restart instruction. */
                regs->sr |= SR_DSP;
+               /* Save DSP mode */
+               tsk->thread.dsp_status.status |= SR_DSP;
                return;
        }
 #endif
@@ -828,14 +934,9 @@ void __init trap_init(void)
        set_exception_table_evt(0x800, do_reserved_inst);
        set_exception_table_evt(0x820, do_illegal_slot_inst);
 #elif defined(CONFIG_SH_FPU)
-#ifdef CONFIG_CPU_SUBTYPE_SHX3
-       set_exception_table_evt(0xd80, fpu_state_restore_trap_handler);
-       set_exception_table_evt(0xda0, fpu_state_restore_trap_handler);
-#else
        set_exception_table_evt(0x800, fpu_state_restore_trap_handler);
        set_exception_table_evt(0x820, fpu_state_restore_trap_handler);
 #endif
-#endif
 
 #ifdef CONFIG_CPU_SH2
        set_exception_table_vec(TRAP_ADDRESS_ERROR, address_error_trap_handler);
@@ -848,34 +949,14 @@ void __init trap_init(void)
 #endif
 #endif
 
+#ifdef TRAP_UBC
+       set_exception_table_vec(TRAP_UBC, break_point_trap);
+#endif
+
        /* Setup VBR for boot cpu */
        per_cpu_trap_init();
 }
 
-void show_trace(struct task_struct *tsk, unsigned long *sp,
-               struct pt_regs *regs)
-{
-       unsigned long addr;
-
-       if (regs && user_mode(regs))
-               return;
-
-       printk("\nCall trace:\n");
-
-       while (!kstack_end(sp)) {
-               addr = *sp++;
-               if (kernel_text_address(addr))
-                       print_ip_sym(addr);
-       }
-
-       printk("\n");
-
-       if (!tsk)
-               tsk = current;
-
-       debug_show_held_locks(tsk);
-}
-
 void show_stack(struct task_struct *tsk, unsigned long *sp)
 {
        unsigned long stack;
@@ -898,3 +979,34 @@ void dump_stack(void)
        show_stack(NULL, NULL);
 }
 EXPORT_SYMBOL(dump_stack);
+
+#ifdef CONFIG_PROC_FS
+/*
+ * This needs to be done after sysctl_init, otherwise sys/ will be
+ * overwritten.  Actually, this shouldn't be in sys/ at all since
+ * it isn't a sysctl, and it doesn't contain sysctl information.
+ * We now locate it in /proc/cpu/alignment instead.
+ */
+static int __init alignment_init(void)
+{
+       struct proc_dir_entry *dir, *res;
+
+       dir = proc_mkdir("cpu", NULL);
+       if (!dir)
+               return -ENOMEM;
+
+       res = proc_create_data("alignment", S_IWUSR | S_IRUGO, dir,
+                              &alignment_proc_fops, &se_usermode);
+       if (!res)
+               return -ENOMEM;
+
+        res = proc_create_data("kernel_alignment", S_IWUSR | S_IRUGO, dir,
+                              &alignment_proc_fops, &se_kernmode_warn);
+        if (!res)
+                return -ENOMEM;
+
+       return 0;
+}
+
+fs_initcall(alignment_init);
+#endif