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