Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Dynamic function tracing support. |
| 3 | * |
| 4 | * Copyright (C) 2008 Abhishek Sagar <sagar.abhishek@gmail.com> |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 5 | * Copyright (C) 2010 Rabin Vincent <rabin@rab.in> |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 6 | * |
| 7 | * For licencing details, see COPYING. |
| 8 | * |
| 9 | * Defines low-level handling of mcount calls when the kernel |
| 10 | * is compiled with the -pg flag. When using dynamic ftrace, the |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 11 | * mcount call-sites get patched with NOP till they are enabled. |
| 12 | * All code mutation routines here are called under stop_machine(). |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/ftrace.h> |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Rabin Vincent | a672917 | 2014-04-03 18:46:45 +0100 | [diff] [blame] | 17 | #include <linux/module.h> |
Kees Cook | 80d6b0c | 2014-04-03 13:29:50 -0700 | [diff] [blame] | 18 | #include <linux/stop_machine.h> |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 19 | |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 20 | #include <asm/cacheflush.h> |
Rabin Vincent | 4394e28 | 2012-02-18 17:47:03 +0100 | [diff] [blame] | 21 | #include <asm/opcodes.h> |
Abhishek Sagar | 395a59d | 2008-06-21 23:47:27 +0530 | [diff] [blame] | 22 | #include <asm/ftrace.h> |
Wang Nan | 0dc016d | 2015-01-09 14:37:36 +0800 | [diff] [blame] | 23 | #include <asm/insn.h> |
Rabin Vincent | d82227c | 2012-02-18 17:50:06 +0100 | [diff] [blame] | 24 | |
Rabin Vincent | 72dc43a | 2010-08-10 19:52:35 +0100 | [diff] [blame] | 25 | #ifdef CONFIG_THUMB2_KERNEL |
Rabin Vincent | 4394e28 | 2012-02-18 17:47:03 +0100 | [diff] [blame] | 26 | #define NOP 0xf85deb04 /* pop.w {lr} */ |
Rabin Vincent | 72dc43a | 2010-08-10 19:52:35 +0100 | [diff] [blame] | 27 | #else |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 28 | #define NOP 0xe8bd4000 /* pop {lr} */ |
Rabin Vincent | 72dc43a | 2010-08-10 19:52:35 +0100 | [diff] [blame] | 29 | #endif |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 30 | |
Tim Bird | 376cfa8 | 2010-10-09 22:24:38 +0530 | [diff] [blame] | 31 | #ifdef CONFIG_DYNAMIC_FTRACE |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 32 | |
Kees Cook | 80d6b0c | 2014-04-03 13:29:50 -0700 | [diff] [blame] | 33 | static int __ftrace_modify_code(void *data) |
| 34 | { |
| 35 | int *command = data; |
| 36 | |
| 37 | set_kernel_text_rw(); |
| 38 | ftrace_modify_all_code(*command); |
| 39 | set_kernel_text_ro(); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | void arch_ftrace_update_code(int command) |
| 45 | { |
| 46 | stop_machine(__ftrace_modify_code, &command, NULL); |
| 47 | } |
| 48 | |
Abel Vesa | 84c9248 | 2017-04-03 23:58:54 +0100 | [diff] [blame] | 49 | #ifdef CONFIG_OLD_MCOUNT |
| 50 | #define OLD_MCOUNT_ADDR ((unsigned long) mcount) |
| 51 | #define OLD_FTRACE_ADDR ((unsigned long) ftrace_caller_old) |
| 52 | |
| 53 | #define OLD_NOP 0xe1a00000 /* mov r0, r0 */ |
| 54 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 55 | static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec) |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 56 | { |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 57 | return rec->arch.old_mcount ? OLD_NOP : NOP; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 58 | } |
| 59 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 60 | static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr) |
| 61 | { |
| 62 | if (!rec->arch.old_mcount) |
| 63 | return addr; |
| 64 | |
| 65 | if (addr == MCOUNT_ADDR) |
| 66 | addr = OLD_MCOUNT_ADDR; |
| 67 | else if (addr == FTRACE_ADDR) |
| 68 | addr = OLD_FTRACE_ADDR; |
| 69 | |
| 70 | return addr; |
| 71 | } |
| 72 | #else |
| 73 | static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec) |
| 74 | { |
| 75 | return NOP; |
| 76 | } |
| 77 | |
| 78 | static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr) |
| 79 | { |
| 80 | return addr; |
| 81 | } |
| 82 | #endif |
| 83 | |
Rabin Vincent | a672917 | 2014-04-03 18:46:45 +0100 | [diff] [blame] | 84 | int ftrace_arch_code_modify_prepare(void) |
| 85 | { |
| 86 | set_all_modules_text_rw(); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int ftrace_arch_code_modify_post_process(void) |
| 91 | { |
| 92 | set_all_modules_text_ro(); |
Kees Cook | 80d6b0c | 2014-04-03 13:29:50 -0700 | [diff] [blame] | 93 | /* Make sure any TLB misses during machine stop are cleared. */ |
| 94 | flush_tlb_all(); |
Rabin Vincent | a672917 | 2014-04-03 18:46:45 +0100 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
Rabin Vincent | dd686eb | 2010-11-06 23:03:21 +0530 | [diff] [blame] | 98 | static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr) |
| 99 | { |
Rabin Vincent | d82227c | 2012-02-18 17:50:06 +0100 | [diff] [blame] | 100 | return arm_gen_branch_link(pc, addr); |
Rabin Vincent | dd686eb | 2010-11-06 23:03:21 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 103 | static int ftrace_modify_code(unsigned long pc, unsigned long old, |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 104 | unsigned long new, bool validate) |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 105 | { |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 106 | unsigned long replaced; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 107 | |
Rabin Vincent | 4394e28 | 2012-02-18 17:47:03 +0100 | [diff] [blame] | 108 | if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { |
| 109 | old = __opcode_to_mem_thumb32(old); |
| 110 | new = __opcode_to_mem_thumb32(new); |
| 111 | } else { |
| 112 | old = __opcode_to_mem_arm(old); |
| 113 | new = __opcode_to_mem_arm(new); |
| 114 | } |
| 115 | |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 116 | if (validate) { |
| 117 | if (probe_kernel_read(&replaced, (void *)pc, MCOUNT_INSN_SIZE)) |
| 118 | return -EFAULT; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 119 | |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 120 | if (replaced != old) |
| 121 | return -EINVAL; |
| 122 | } |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 123 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 124 | if (probe_kernel_write((void *)pc, &new, MCOUNT_INSN_SIZE)) |
| 125 | return -EPERM; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 126 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 127 | flush_icache_range(pc, pc + MCOUNT_INSN_SIZE); |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 128 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 129 | return 0; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 133 | { |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 134 | unsigned long pc; |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 135 | unsigned long new; |
| 136 | int ret; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 137 | |
| 138 | pc = (unsigned long)&ftrace_call; |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 139 | new = ftrace_call_replace(pc, (unsigned long)func); |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 140 | |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 141 | ret = ftrace_modify_code(pc, 0, new, false); |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 142 | |
| 143 | #ifdef CONFIG_OLD_MCOUNT |
| 144 | if (!ret) { |
| 145 | pc = (unsigned long)&ftrace_call_old; |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 146 | new = ftrace_call_replace(pc, (unsigned long)func); |
| 147 | |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 148 | ret = ftrace_modify_code(pc, 0, new, false); |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 149 | } |
| 150 | #endif |
| 151 | |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 155 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 156 | { |
| 157 | unsigned long new, old; |
| 158 | unsigned long ip = rec->ip; |
| 159 | |
| 160 | old = ftrace_nop_replace(rec); |
| 161 | new = ftrace_call_replace(ip, adjust_address(rec, addr)); |
| 162 | |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 163 | return ftrace_modify_code(rec->ip, old, new, true); |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int ftrace_make_nop(struct module *mod, |
| 167 | struct dyn_ftrace *rec, unsigned long addr) |
| 168 | { |
| 169 | unsigned long ip = rec->ip; |
| 170 | unsigned long old; |
| 171 | unsigned long new; |
| 172 | int ret; |
| 173 | |
| 174 | old = ftrace_call_replace(ip, adjust_address(rec, addr)); |
| 175 | new = ftrace_nop_replace(rec); |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 176 | ret = ftrace_modify_code(ip, old, new, true); |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 177 | |
| 178 | #ifdef CONFIG_OLD_MCOUNT |
| 179 | if (ret == -EINVAL && addr == MCOUNT_ADDR) { |
| 180 | rec->arch.old_mcount = true; |
| 181 | |
| 182 | old = ftrace_call_replace(ip, adjust_address(rec, addr)); |
| 183 | new = ftrace_nop_replace(rec); |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 184 | ret = ftrace_modify_code(ip, old, new, true); |
Rabin Vincent | 3b6c223 | 2010-08-10 19:43:28 +0100 | [diff] [blame] | 185 | } |
| 186 | #endif |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 191 | int __init ftrace_dyn_arch_init(void) |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 192 | { |
Abhishek Sagar | 014c257 | 2008-05-31 14:23:50 +0530 | [diff] [blame] | 193 | return 0; |
| 194 | } |
Tim Bird | 376cfa8 | 2010-10-09 22:24:38 +0530 | [diff] [blame] | 195 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 196 | |
| 197 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 198 | void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, |
| 199 | unsigned long frame_pointer) |
| 200 | { |
| 201 | unsigned long return_hooker = (unsigned long) &return_to_handler; |
| 202 | struct ftrace_graph_ent trace; |
| 203 | unsigned long old; |
| 204 | int err; |
| 205 | |
| 206 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 207 | return; |
| 208 | |
| 209 | old = *parent; |
| 210 | *parent = return_hooker; |
| 211 | |
Colin Cross | 4c36595 | 2012-07-18 19:15:25 +0100 | [diff] [blame] | 212 | trace.func = self_addr; |
| 213 | trace.depth = current->curr_ret_stack + 1; |
| 214 | |
| 215 | /* Only trace if the calling function expects to */ |
| 216 | if (!ftrace_graph_entry(&trace)) { |
| 217 | *parent = old; |
| 218 | return; |
| 219 | } |
| 220 | |
Tim Bird | 376cfa8 | 2010-10-09 22:24:38 +0530 | [diff] [blame] | 221 | err = ftrace_push_return_trace(old, self_addr, &trace.depth, |
Josh Poimboeuf | 9a7c348 | 2016-08-19 06:52:57 -0500 | [diff] [blame] | 222 | frame_pointer, NULL); |
Tim Bird | 376cfa8 | 2010-10-09 22:24:38 +0530 | [diff] [blame] | 223 | if (err == -EBUSY) { |
| 224 | *parent = old; |
| 225 | return; |
| 226 | } |
Tim Bird | 376cfa8 | 2010-10-09 22:24:38 +0530 | [diff] [blame] | 227 | } |
Rabin Vincent | dd686eb | 2010-11-06 23:03:21 +0530 | [diff] [blame] | 228 | |
| 229 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 230 | extern unsigned long ftrace_graph_call; |
| 231 | extern unsigned long ftrace_graph_call_old; |
| 232 | extern void ftrace_graph_caller_old(void); |
| 233 | |
| 234 | static int __ftrace_modify_caller(unsigned long *callsite, |
| 235 | void (*func) (void), bool enable) |
| 236 | { |
| 237 | unsigned long caller_fn = (unsigned long) func; |
| 238 | unsigned long pc = (unsigned long) callsite; |
Rabin Vincent | d82227c | 2012-02-18 17:50:06 +0100 | [diff] [blame] | 239 | unsigned long branch = arm_gen_branch(pc, caller_fn); |
Rabin Vincent | dd686eb | 2010-11-06 23:03:21 +0530 | [diff] [blame] | 240 | unsigned long nop = 0xe1a00000; /* mov r0, r0 */ |
| 241 | unsigned long old = enable ? nop : branch; |
| 242 | unsigned long new = enable ? branch : nop; |
| 243 | |
Rabin Vincent | dc283d70 | 2012-02-29 15:59:07 +0100 | [diff] [blame] | 244 | return ftrace_modify_code(pc, old, new, true); |
Rabin Vincent | dd686eb | 2010-11-06 23:03:21 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int ftrace_modify_graph_caller(bool enable) |
| 248 | { |
| 249 | int ret; |
| 250 | |
| 251 | ret = __ftrace_modify_caller(&ftrace_graph_call, |
| 252 | ftrace_graph_caller, |
| 253 | enable); |
| 254 | |
| 255 | #ifdef CONFIG_OLD_MCOUNT |
| 256 | if (!ret) |
| 257 | ret = __ftrace_modify_caller(&ftrace_graph_call_old, |
| 258 | ftrace_graph_caller_old, |
| 259 | enable); |
| 260 | #endif |
| 261 | |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | int ftrace_enable_ftrace_graph_caller(void) |
| 266 | { |
| 267 | return ftrace_modify_graph_caller(true); |
| 268 | } |
| 269 | |
| 270 | int ftrace_disable_ftrace_graph_caller(void) |
| 271 | { |
| 272 | return ftrace_modify_graph_caller(false); |
| 273 | } |
| 274 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
Tim Bird | 376cfa8 | 2010-10-09 22:24:38 +0530 | [diff] [blame] | 275 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |