David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame^] | 1 | #include <linux/spinlock.h> |
| 2 | #include <linux/hardirq.h> |
| 3 | #include <linux/ftrace.h> |
| 4 | #include <linux/percpu.h> |
| 5 | #include <linux/init.h> |
| 6 | #include <linux/list.h> |
| 7 | |
| 8 | static const u32 ftrace_nop = 0x01000000; |
| 9 | |
| 10 | notrace int ftrace_ip_converted(unsigned long ip) |
| 11 | { |
| 12 | u32 insn = *(u32 *) ip; |
| 13 | |
| 14 | return (insn == ftrace_nop); |
| 15 | } |
| 16 | |
| 17 | notrace unsigned char *ftrace_nop_replace(void) |
| 18 | { |
| 19 | return (char *)&ftrace_nop; |
| 20 | } |
| 21 | |
| 22 | notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) |
| 23 | { |
| 24 | static u32 call; |
| 25 | s32 off; |
| 26 | |
| 27 | off = ((s32)addr - (s32)ip); |
| 28 | call = 0x40000000 | ((u32)off >> 2); |
| 29 | |
| 30 | return (unsigned char *) &call; |
| 31 | } |
| 32 | |
| 33 | notrace int |
| 34 | ftrace_modify_code(unsigned long ip, unsigned char *old_code, |
| 35 | unsigned char *new_code) |
| 36 | { |
| 37 | u32 old = *(u32 *)old_code; |
| 38 | u32 new = *(u32 *)new_code; |
| 39 | u32 replaced; |
| 40 | int faulted; |
| 41 | |
| 42 | __asm__ __volatile__( |
| 43 | "1: cas [%[ip]], %[old], %[new]\n" |
| 44 | " flush %[ip]\n" |
| 45 | " mov 0, %[faulted]\n" |
| 46 | "2:\n" |
| 47 | " .section .fixup,#alloc,#execinstr\n" |
| 48 | " .align 4\n" |
| 49 | "3: sethi %%hi(2b), %[faulted]\n" |
| 50 | " jmpl %[faulted] + %%lo(2b), %%g0\n" |
| 51 | " mov 1, %[faulted]\n" |
| 52 | " .previous\n" |
| 53 | " .section __ex_table,\"a\"\n" |
| 54 | " .align 4\n" |
| 55 | " .word 1b, 3b\n" |
| 56 | " .previous\n" |
| 57 | : "=r" (replaced), [faulted] "=r" (faulted) |
| 58 | : [new] "0" (new), [old] "r" (old), [ip] "r" (ip) |
| 59 | : "memory"); |
| 60 | |
| 61 | if (replaced != old && replaced != new) |
| 62 | faulted = 2; |
| 63 | |
| 64 | return faulted; |
| 65 | } |
| 66 | |
| 67 | notrace int ftrace_update_ftrace_func(ftrace_func_t func) |
| 68 | { |
| 69 | unsigned long ip = (unsigned long)(&ftrace_call); |
| 70 | unsigned char old[4], *new; |
| 71 | |
| 72 | memcpy(old, &ftrace_call, 4); |
| 73 | new = ftrace_call_replace(ip, (unsigned long)func); |
| 74 | return ftrace_modify_code(ip, old, new); |
| 75 | } |
| 76 | |
| 77 | notrace int ftrace_mcount_set(unsigned long *data) |
| 78 | { |
| 79 | unsigned long ip = (long)(&mcount_call); |
| 80 | unsigned long *addr = data; |
| 81 | unsigned char old[4], *new; |
| 82 | |
| 83 | /* |
| 84 | * Replace the mcount stub with a pointer to the |
| 85 | * ip recorder function. |
| 86 | */ |
| 87 | memcpy(old, &mcount_call, 4); |
| 88 | new = ftrace_call_replace(ip, *addr); |
| 89 | *addr = ftrace_modify_code(ip, old, new); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | int __init ftrace_dyn_arch_init(void *data) |
| 96 | { |
| 97 | ftrace_mcount_set(data); |
| 98 | return 0; |
| 99 | } |