]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - arch/x86/kernel/ftrace.c
Merge branch 'tracing/ftrace' into tracing/urgent
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / ftrace.c
index 498608c015fbe53bc6b4b6756f561b9ca94c0552..8821ceabf51df44239de2583f0e95bfe40c1d5f9 100644 (file)
 
 #include <linux/spinlock.h>
 #include <linux/hardirq.h>
+#include <linux/uaccess.h>
 #include <linux/ftrace.h>
 #include <linux/percpu.h>
 #include <linux/init.h>
 #include <linux/list.h>
 
-#include <asm/alternative.h>
+#include <asm/ftrace.h>
+#include <asm/nops.h>
 
-#define CALL_BACK              5
 
 /* 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[5];
+       char code[MCOUNT_INSN_SIZE];
        struct {
                char e8;
                int offset;
        } __attribute__((packed));
 };
 
-notrace int ftrace_ip_converted(unsigned long ip)
-{
-       unsigned long save;
-
-       ip -= CALL_BACK;
-       save = *(long *)ip;
-
-       return save == *ftrace_nop;
-}
 
 static int notrace ftrace_calc_offset(long ip, long addr)
 {
@@ -56,7 +48,7 @@ notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
        static union ftrace_code_union calc;
 
        calc.e8         = 0xe8;
-       calc.offset     = ftrace_calc_offset(ip, addr);
+       calc.offset     = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
 
        /*
         * No locking needed, this must be called via kstop_machine
@@ -69,14 +61,8 @@ 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;
-
-       /* move the IP back to the start of the call */
-       ip -= CALL_BACK;
+       unsigned char replaced[MCOUNT_INSN_SIZE];
+       int ret;
 
        /*
         * Note: Due to modules and __init, code can
@@ -84,40 +70,30 @@ ftrace_modify_code(unsigned long ip, unsigned char *old_code,
         *  as well as code changing.
         *
         * 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), "r"(newch),
-                 "0"(faulted), "a"(old)
-               : "memory");
-       sync_core();
+       if (__copy_from_user_inatomic(replaced, (char __user *)ip, MCOUNT_INSN_SIZE))
+               return 1;
+
+       if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
+               return 2;
 
-       if (replaced != old && replaced != new)
-               faulted = 2;
+       ret = __copy_to_user_inatomic((char __user *)ip, new_code,
+                                       MCOUNT_INSN_SIZE);
+       WARN_ON_ONCE(ret);
 
-       return faulted;
+       sync_core();
+
+       return 0;
 }
 
 notrace int ftrace_update_ftrace_func(ftrace_func_t func)
 {
        unsigned long ip = (unsigned long)(&ftrace_call);
-       unsigned char old[5], *new;
+       unsigned char old[MCOUNT_INSN_SIZE], *new;
        int ret;
 
-       ip += CALL_BACK;
-
-       memcpy(old, &ftrace_call, 5);
+       memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
        new = ftrace_call_replace(ip, (unsigned long)func);
        ret = ftrace_modify_code(ip, old, new);
 
@@ -126,34 +102,76 @@ notrace int ftrace_update_ftrace_func(ftrace_func_t func)
 
 notrace int ftrace_mcount_set(unsigned long *data)
 {
-       unsigned long ip = (long)(&mcount_call);
-       unsigned long *addr = data;
-       unsigned char old[5], *new;
-
-       /* ip is at the location, but modify code will subtact this */
-       ip += CALL_BACK;
-
-       /*
-        * Replace the mcount stub with a pointer to the
-        * ip recorder function.
-        */
-       memcpy(old, &mcount_call, 5);
-       new = ftrace_call_replace(ip, *addr);
-       *addr = ftrace_modify_code(ip, old, new);
-
+       /* mcount is initialized as a nop */
+       *data = 0;
        return 0;
 }
 
 int __init ftrace_dyn_arch_init(void *data)
 {
-       const unsigned char *const *noptable = find_nop_table();
-
-       /* This is running in kstop_machine */
-
-       ftrace_mcount_set(data);
+       extern const unsigned char ftrace_test_p6nop[];
+       extern const unsigned char ftrace_test_nop5[];
+       extern const unsigned char ftrace_test_jmp[];
+       int faulted = 0;
 
-       ftrace_nop = (unsigned long *)noptable[CALL_BACK];
+       /*
+        * There is no good nop for all x86 archs.
+        * We will default to using the P6_NOP5, but first we
+        * will test to make sure that the nop will actually
+        * work on this CPU. If it faults, we will then
+        * go to a lesser efficient 5 byte nop. If that fails
+        * we then just use a jmp as our nop. This isn't the most
+        * efficient nop, but we can not use a multi part nop
+        * since we would then risk being preempted in the middle
+        * of that nop, and if we enabled tracing then, it might
+        * cause a system crash.
+        *
+        * TODO: check the cpuid to determine the best nop.
+        */
+       asm volatile (
+               "jmp ftrace_test_jmp\n"
+               /* This code needs to stay around */
+               ".section .text, \"ax\"\n"
+               "ftrace_test_jmp:"
+               "jmp ftrace_test_p6nop\n"
+               "nop\n"
+               "nop\n"
+               "nop\n"  /* 2 byte jmp + 3 bytes */
+               "ftrace_test_p6nop:"
+               P6_NOP5
+               "jmp 1f\n"
+               "ftrace_test_nop5:"
+               ".byte 0x66,0x66,0x66,0x66,0x90\n"
+               "jmp 1f\n"
+               ".previous\n"
+               "1:"
+               ".section .fixup, \"ax\"\n"
+               "2:     movl $1, %0\n"
+               "       jmp ftrace_test_nop5\n"
+               "3:     movl $2, %0\n"
+               "       jmp 1b\n"
+               ".previous\n"
+               _ASM_EXTABLE(ftrace_test_p6nop, 2b)
+               _ASM_EXTABLE(ftrace_test_nop5, 3b)
+               : "=r"(faulted) : "0" (faulted));
+
+       switch (faulted) {
+       case 0:
+               pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
+               ftrace_nop = (unsigned long *)ftrace_test_p6nop;
+               break;
+       case 1:
+               pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
+               ftrace_nop = (unsigned long *)ftrace_test_nop5;
+               break;
+       case 2:
+               pr_info("ftrace: converting mcount calls to jmp . + 5\n");
+               ftrace_nop = (unsigned long *)ftrace_test_jmp;
+               break;
+       }
+
+       /* The return code is retured via data */
+       *(unsigned long *)data = 0;
 
        return 0;
 }
-