Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Code for replacing ftrace calls with jumps. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | * |
| 6 | * Thanks goes to Ingo Molnar, for suggesting the idea. |
| 7 | * Mathieu Desnoyers, for suggesting postponing the modifications. |
| 8 | * Arjan van de Ven, for keeping me straight, and explaining to me |
| 9 | * the dangers of modifying code on the run. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/hardirq.h> |
| 14 | #include <linux/ftrace.h> |
| 15 | #include <linux/percpu.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/list.h> |
| 18 | |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 19 | #include <asm/alternative.h> |
| 20 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 21 | #define CALL_BACK 5 |
| 22 | |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 23 | /* Long is fine, even if it is only 4 bytes ;-) */ |
| 24 | static long *ftrace_nop; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 25 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 26 | union ftrace_code_union { |
| 27 | char code[5]; |
| 28 | struct { |
| 29 | char e8; |
| 30 | int offset; |
| 31 | } __attribute__((packed)); |
| 32 | }; |
| 33 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 34 | notrace int ftrace_ip_converted(unsigned long ip) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 35 | { |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 36 | unsigned long save; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 37 | |
| 38 | ip -= CALL_BACK; |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 39 | save = *(long *)ip; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 40 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 41 | return save == *ftrace_nop; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 44 | static int notrace ftrace_calc_offset(long ip, long addr) |
| 45 | { |
| 46 | return (int)(addr - ip); |
| 47 | } |
| 48 | |
| 49 | notrace unsigned char *ftrace_nop_replace(void) |
| 50 | { |
| 51 | return (char *)ftrace_nop; |
| 52 | } |
| 53 | |
| 54 | notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) |
| 55 | { |
| 56 | static union ftrace_code_union calc; |
| 57 | |
| 58 | calc.e8 = 0xe8; |
| 59 | calc.offset = ftrace_calc_offset(ip, addr); |
| 60 | |
| 61 | /* |
| 62 | * No locking needed, this must be called via kstop_machine |
| 63 | * which in essence is like running on a uniprocessor machine. |
| 64 | */ |
| 65 | return calc.code; |
| 66 | } |
| 67 | |
| 68 | notrace int |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 69 | ftrace_modify_code(unsigned long ip, unsigned char *old_code, |
| 70 | unsigned char *new_code) |
| 71 | { |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 72 | unsigned replaced; |
| 73 | unsigned old = *(unsigned *)old_code; /* 4 bytes */ |
| 74 | unsigned new = *(unsigned *)new_code; /* 4 bytes */ |
| 75 | unsigned char newch = new_code[4]; |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 76 | int faulted = 0; |
| 77 | |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 78 | /* move the IP back to the start of the call */ |
| 79 | ip -= CALL_BACK; |
| 80 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 81 | /* |
| 82 | * Note: Due to modules and __init, code can |
| 83 | * disappear and change, we need to protect against faulting |
| 84 | * as well as code changing. |
| 85 | * |
| 86 | * No real locking needed, this code is run through |
| 87 | * kstop_machine. |
| 88 | */ |
| 89 | asm volatile ( |
| 90 | "1: lock\n" |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 91 | " cmpxchg %3, (%2)\n" |
| 92 | " jnz 2f\n" |
| 93 | " movb %b4, 4(%2)\n" |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 94 | "2:\n" |
| 95 | ".section .fixup, \"ax\"\n" |
| 96 | " movl $1, %0\n" |
| 97 | "3: jmp 2b\n" |
| 98 | ".previous\n" |
| 99 | _ASM_EXTABLE(1b, 3b) |
| 100 | : "=r"(faulted), "=a"(replaced) |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 101 | : "r"(ip), "r"(new), "r"(newch), |
| 102 | "0"(faulted), "a"(old) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 103 | : "memory"); |
| 104 | sync_core(); |
| 105 | |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 106 | if (replaced != old && replaced != new) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 107 | faulted = 2; |
| 108 | |
| 109 | return faulted; |
| 110 | } |
| 111 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame^] | 112 | notrace int ftrace_update_ftrace_func(ftrace_func_t func) |
| 113 | { |
| 114 | unsigned long ip = (unsigned long)(&ftrace_call); |
| 115 | unsigned char old[5], *new; |
| 116 | int ret; |
| 117 | |
| 118 | ip += CALL_BACK; |
| 119 | |
| 120 | memcpy(old, &ftrace_call, 5); |
| 121 | new = ftrace_call_replace(ip, (unsigned long)func); |
| 122 | ret = ftrace_modify_code(ip, old, new); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | notrace int ftrace_mcount_set(unsigned long *data) |
| 128 | { |
| 129 | unsigned long ip = (long)(&mcount_call); |
| 130 | unsigned long *addr = data; |
| 131 | unsigned char old[5], *new; |
| 132 | |
| 133 | /* ip is at the location, but modify code will subtact this */ |
| 134 | ip += CALL_BACK; |
| 135 | |
| 136 | /* |
| 137 | * Replace the mcount stub with a pointer to the |
| 138 | * ip recorder function. |
| 139 | */ |
| 140 | memcpy(old, &mcount_call, 5); |
| 141 | new = ftrace_call_replace(ip, *addr); |
| 142 | *addr = ftrace_modify_code(ip, old, new); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int __init ftrace_dyn_arch_init(void *data) |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 148 | { |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 149 | const unsigned char *const *noptable = find_nop_table(); |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 150 | |
Steven Rostedt | d61f82d | 2008-05-12 21:20:43 +0200 | [diff] [blame^] | 151 | /* This is running in kstop_machine */ |
| 152 | |
| 153 | ftrace_mcount_set(data); |
| 154 | |
Steven Rostedt | dfa60ab | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 155 | ftrace_nop = (unsigned long *)noptable[CALL_BACK]; |
| 156 | |
Steven Rostedt | 3d08339 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 157 | return 0; |
| 158 | } |
Steven Rostedt | 3c1720f | 2008-05-12 21:20:43 +0200 | [diff] [blame] | 159 | |