ftrace: remove mcount set
[safe/jmp/linux-2.6] / arch / x86 / kernel / ftrace.c
index 4151c91..b399eed 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <linux/spinlock.h>
 #include <linux/hardirq.h>
+#include <linux/uaccess.h>
 #include <linux/ftrace.h>
 #include <linux/percpu.h>
 #include <linux/init.h>
@@ -21,7 +22,7 @@
 
 
 /* Long is fine, even if it is only 4 bytes ;-) */
-static long *ftrace_nop;
+static unsigned long *ftrace_nop;
 
 union ftrace_code_union {
        char code[MCOUNT_INSN_SIZE];
@@ -60,41 +61,33 @@ notrace int
 ftrace_modify_code(unsigned long ip, unsigned char *old_code,
                   unsigned char *new_code)
 {
-       unsigned replaced;
-       unsigned old = *(unsigned *)old_code; /* 4 bytes */
-       unsigned new = *(unsigned *)new_code; /* 4 bytes */
-       unsigned char newch = new_code[4];
-       int faulted = 0;
+       unsigned char replaced[MCOUNT_INSN_SIZE];
 
        /*
         * Note: Due to modules and __init, code can
         *  disappear and change, we need to protect against faulting
-        *  as well as code changing.
+        *  as well as code changing. We do this by using the
+        *  probe_kernel_* functions.
         *
         * No real locking needed, this code is run through
-        * kstop_machine.
+        * kstop_machine, or before SMP starts.
         */
-       asm volatile (
-               "1: lock\n"
-               "   cmpxchg %3, (%2)\n"
-               "   jnz 2f\n"
-               "   movb %b4, 4(%2)\n"
-               "2:\n"
-               ".section .fixup, \"ax\"\n"
-               "3:     movl $1, %0\n"
-               "       jmp 2b\n"
-               ".previous\n"
-               _ASM_EXTABLE(1b, 3b)
-               : "=r"(faulted), "=a"(replaced)
-               : "r"(ip), "r"(new), "c"(newch),
-                 "0"(faulted), "a"(old)
-               : "memory");
-       sync_core();
 
-       if (replaced != old && replaced != new)
-               faulted = 2;
+       /* read the text we want to modify */
+       if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
+               return -EFAULT;
+
+       /* Make sure it is what we expect it to be */
+       if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
+               return -EINVAL;
+
+       /* replace the text with the new text */
+       if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
+               return -EPERM;
 
-       return faulted;
+       sync_core();
+
+       return 0;
 }
 
 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
@@ -110,13 +103,6 @@ notrace int ftrace_update_ftrace_func(ftrace_func_t func)
        return ret;
 }
 
-notrace int ftrace_mcount_set(unsigned long *data)
-{
-       /* mcount is initialized as a nop */
-       *data = 0;
-       return 0;
-}
-
 int __init ftrace_dyn_arch_init(void *data)
 {
        extern const unsigned char ftrace_test_p6nop[];
@@ -144,7 +130,9 @@ int __init ftrace_dyn_arch_init(void *data)
                ".section .text, \"ax\"\n"
                "ftrace_test_jmp:"
                "jmp ftrace_test_p6nop\n"
-               ".byte 0x00,0x00,0x00\n"  /* 2 byte jmp + 3 bytes */
+               "nop\n"
+               "nop\n"
+               "nop\n"  /* 2 byte jmp + 3 bytes */
                "ftrace_test_p6nop:"
                P6_NOP5
                "jmp 1f\n"
@@ -173,7 +161,7 @@ int __init ftrace_dyn_arch_init(void *data)
                ftrace_nop = (unsigned long *)ftrace_test_nop5;
                break;
        case 2:
-               pr_info("ftrace: converting mcount calls to jmp 1f\n");
+               pr_info("ftrace: converting mcount calls to jmp . + 5\n");
                ftrace_nop = (unsigned long *)ftrace_test_jmp;
                break;
        }