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 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 8 | #include <asm/ftrace.h> |
| 9 | |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 10 | #ifdef CONFIG_DYNAMIC_FTRACE |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 11 | static const u32 ftrace_nop = 0x01000000; |
| 12 | |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 13 | static u32 ftrace_call_replace(unsigned long ip, unsigned long addr) |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 14 | { |
| 15 | static u32 call; |
| 16 | s32 off; |
| 17 | |
| 18 | off = ((s32)addr - (s32)ip); |
| 19 | call = 0x40000000 | ((u32)off >> 2); |
| 20 | |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 21 | return call; |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 22 | } |
| 23 | |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 24 | static int ftrace_modify_code(unsigned long ip, u32 old, u32 new) |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 25 | { |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 26 | u32 replaced; |
| 27 | int faulted; |
| 28 | |
| 29 | __asm__ __volatile__( |
| 30 | "1: cas [%[ip]], %[old], %[new]\n" |
| 31 | " flush %[ip]\n" |
| 32 | " mov 0, %[faulted]\n" |
| 33 | "2:\n" |
| 34 | " .section .fixup,#alloc,#execinstr\n" |
| 35 | " .align 4\n" |
| 36 | "3: sethi %%hi(2b), %[faulted]\n" |
| 37 | " jmpl %[faulted] + %%lo(2b), %%g0\n" |
| 38 | " mov 1, %[faulted]\n" |
| 39 | " .previous\n" |
| 40 | " .section __ex_table,\"a\"\n" |
| 41 | " .align 4\n" |
| 42 | " .word 1b, 3b\n" |
| 43 | " .previous\n" |
| 44 | : "=r" (replaced), [faulted] "=r" (faulted) |
| 45 | : [new] "0" (new), [old] "r" (old), [ip] "r" (ip) |
| 46 | : "memory"); |
| 47 | |
| 48 | if (replaced != old && replaced != new) |
| 49 | faulted = 2; |
| 50 | |
| 51 | return faulted; |
| 52 | } |
| 53 | |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 54 | int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr) |
| 55 | { |
| 56 | unsigned long ip = rec->ip; |
| 57 | u32 old, new; |
| 58 | |
| 59 | old = ftrace_call_replace(ip, addr); |
| 60 | new = ftrace_nop; |
| 61 | return ftrace_modify_code(ip, old, new); |
| 62 | } |
| 63 | |
| 64 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 65 | { |
| 66 | unsigned long ip = rec->ip; |
| 67 | u32 old, new; |
| 68 | |
| 69 | old = ftrace_nop; |
| 70 | new = ftrace_call_replace(ip, addr); |
| 71 | return ftrace_modify_code(ip, old, new); |
| 72 | } |
| 73 | |
Steven Rostedt | 15adc04 | 2008-10-23 09:33:08 -0400 | [diff] [blame] | 74 | int ftrace_update_ftrace_func(ftrace_func_t func) |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 75 | { |
| 76 | unsigned long ip = (unsigned long)(&ftrace_call); |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 77 | u32 old, new; |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 78 | |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 79 | old = *(u32 *) &ftrace_call; |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 80 | new = ftrace_call_replace(ip, (unsigned long)func); |
| 81 | return ftrace_modify_code(ip, old, new); |
| 82 | } |
| 83 | |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 84 | int __init ftrace_dyn_arch_init(void *data) |
| 85 | { |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 86 | unsigned long *p = data; |
| 87 | |
| 88 | *p = 0; |
| 89 | |
David Miller | d05f5f9 | 2008-05-13 22:06:59 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |
David S. Miller | 9be12f9 | 2009-06-13 01:03:24 -0700 | [diff] [blame] | 92 | #endif |
| 93 | |