Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Dynamic function tracer architecture backend. |
| 3 | * |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 4 | * Copyright IBM Corp. 2009,2014 |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 5 | * |
| 6 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>, |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 7 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 10 | #include <linux/hardirq.h> |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
| 12 | #include <linux/ftrace.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/types.h> |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 15 | #include <linux/kprobes.h> |
Heiko Carstens | 9bf1226 | 2009-06-12 10:26:47 +0200 | [diff] [blame] | 16 | #include <trace/syscall.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 17 | #include <asm/asm-offsets.h> |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 18 | #include "entry.h" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 19 | |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 20 | void mcount_replace_code(void); |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 21 | void ftrace_disable_code(void); |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 22 | void ftrace_enable_insn(void); |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 23 | |
| 24 | #ifdef CONFIG_64BIT |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 25 | /* |
| 26 | * The 64-bit mcount code looks like this: |
| 27 | * stg %r14,8(%r15) # offset 0 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 28 | * larl %r1,<&counter> # offset 6 |
| 29 | * brasl %r14,_mcount # offset 12 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 30 | * lg %r14,8(%r15) # offset 18 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 31 | * Total length is 24 bytes. The complete mcount block initially gets replaced |
| 32 | * by ftrace_make_nop. Subsequent calls to ftrace_make_call / ftrace_make_nop |
| 33 | * only patch the jg/lg instruction within the block. |
| 34 | * Note: we do not patch the first instruction to an unconditional branch, |
| 35 | * since that would break kprobes/jprobes. It is easier to leave the larl |
| 36 | * instruction in and only modify the second instruction. |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 37 | * The 64-bit enabled ftrace code block looks like this: |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 38 | * larl %r0,.+24 # offset 0 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 39 | * > lg %r1,__LC_FTRACE_FUNC # offset 6 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 40 | * br %r1 # offset 12 |
| 41 | * brcl 0,0 # offset 14 |
| 42 | * brc 0,0 # offset 20 |
| 43 | * The ftrace function gets called with a non-standard C function call ABI |
| 44 | * where r0 contains the return address. It is also expected that the called |
| 45 | * function only clobbers r0 and r1, but restores r2-r15. |
| 46 | * The return point of the ftrace function has offset 24, so execution |
| 47 | * continues behind the mcount block. |
| 48 | * larl %r0,.+24 # offset 0 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 49 | * > jg .+18 # offset 6 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 50 | * br %r1 # offset 12 |
| 51 | * brcl 0,0 # offset 14 |
| 52 | * brc 0,0 # offset 20 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 53 | * The jg instruction branches to offset 24 to skip as many instructions |
| 54 | * as possible. |
| 55 | */ |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 56 | asm( |
| 57 | " .align 4\n" |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 58 | "mcount_replace_code:\n" |
| 59 | " larl %r0,0f\n" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 60 | "ftrace_disable_code:\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 61 | " jg 0f\n" |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 62 | " br %r1\n" |
| 63 | " brcl 0,0\n" |
| 64 | " brc 0,0\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 65 | "0:\n" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 66 | " .align 4\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 67 | "ftrace_enable_insn:\n" |
| 68 | " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n"); |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 69 | |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 70 | #define MCOUNT_BLOCK_SIZE 24 |
| 71 | #define MCOUNT_INSN_OFFSET 6 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 72 | #define FTRACE_INSN_SIZE 6 |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 73 | |
| 74 | #else /* CONFIG_64BIT */ |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 75 | /* |
| 76 | * The 31-bit mcount code looks like this: |
| 77 | * st %r14,4(%r15) # offset 0 |
| 78 | * > bras %r1,0f # offset 4 |
| 79 | * > .long _mcount # offset 8 |
| 80 | * > .long <&counter> # offset 12 |
| 81 | * > 0: l %r14,0(%r1) # offset 16 |
| 82 | * > l %r1,4(%r1) # offset 20 |
| 83 | * basr %r14,%r14 # offset 24 |
| 84 | * l %r14,4(%r15) # offset 26 |
| 85 | * Total length is 30 bytes. The twenty bytes starting from offset 4 |
| 86 | * to offset 24 get overwritten by ftrace_make_nop / ftrace_make_call. |
| 87 | * The 31-bit enabled ftrace code block looks like this: |
| 88 | * st %r14,4(%r15) # offset 0 |
| 89 | * > l %r14,__LC_FTRACE_FUNC # offset 4 |
| 90 | * > j 0f # offset 8 |
| 91 | * > .fill 12,1,0x07 # offset 12 |
| 92 | * 0: basr %r14,%r14 # offset 24 |
| 93 | * l %r14,4(%r14) # offset 26 |
| 94 | * The return points of the mcount/ftrace function have the same offset 26. |
| 95 | * The 31-bit disabled ftrace code block looks like this: |
| 96 | * st %r14,4(%r15) # offset 0 |
| 97 | * > j .+26 # offset 4 |
| 98 | * > j 0f # offset 8 |
| 99 | * > .fill 12,1,0x07 # offset 12 |
| 100 | * 0: basr %r14,%r14 # offset 24 |
| 101 | * l %r14,4(%r14) # offset 26 |
| 102 | * The j instruction branches to offset 30 to skip as many instructions |
| 103 | * as possible. |
| 104 | */ |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 105 | asm( |
| 106 | " .align 4\n" |
| 107 | "ftrace_disable_code:\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 108 | " j 1f\n" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 109 | " j 0f\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 110 | " .fill 12,1,0x07\n" |
| 111 | "0: basr %r14,%r14\n" |
| 112 | "1:\n" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 113 | " .align 4\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 114 | "ftrace_enable_insn:\n" |
| 115 | " l %r14,"__stringify(__LC_FTRACE_FUNC)"\n"); |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 116 | |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 117 | #define FTRACE_INSN_SIZE 4 |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 118 | |
| 119 | #endif /* CONFIG_64BIT */ |
| 120 | |
Heiko Carstens | 10dec7d | 2014-08-15 13:01:46 +0200 | [diff] [blame] | 121 | #ifdef CONFIG_64BIT |
| 122 | int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, |
| 123 | unsigned long addr) |
| 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | #endif |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 128 | |
| 129 | int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, |
| 130 | unsigned long addr) |
| 131 | { |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame^] | 132 | #ifdef CONFIG_64BIT |
| 133 | /* Initial replacement of the whole mcount block */ |
| 134 | if (addr == MCOUNT_ADDR) { |
| 135 | if (probe_kernel_write((void *) rec->ip - MCOUNT_INSN_OFFSET, |
| 136 | mcount_replace_code, |
| 137 | MCOUNT_BLOCK_SIZE)) |
| 138 | return -EPERM; |
| 139 | return 0; |
| 140 | } |
| 141 | #endif |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 142 | if (probe_kernel_write((void *) rec->ip, ftrace_disable_code, |
| 143 | MCOUNT_INSN_SIZE)) |
| 144 | return -EPERM; |
| 145 | return 0; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 149 | { |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 150 | if (probe_kernel_write((void *) rec->ip, ftrace_enable_insn, |
| 151 | FTRACE_INSN_SIZE)) |
| 152 | return -EPERM; |
| 153 | return 0; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 157 | { |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 161 | int __init ftrace_dyn_arch_init(void) |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 162 | { |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 165 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 166 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 167 | /* |
| 168 | * Hook the return address and push it in the stack of return addresses |
| 169 | * in current thread info. |
| 170 | */ |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 171 | unsigned long __kprobes prepare_ftrace_return(unsigned long parent, |
| 172 | unsigned long ip) |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 173 | { |
| 174 | struct ftrace_graph_ent trace; |
| 175 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 176 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 177 | goto out; |
Heiko Carstens | aca9120 | 2013-05-13 14:48:52 +0200 | [diff] [blame] | 178 | ip = (ip & PSW_ADDR_INSN) - MCOUNT_INSN_SIZE; |
Heiko Carstens | 05e0baa | 2013-10-11 08:55:57 +0200 | [diff] [blame] | 179 | trace.func = ip; |
| 180 | trace.depth = current->curr_ret_stack + 1; |
| 181 | /* Only trace if the calling function expects to. */ |
| 182 | if (!ftrace_graph_entry(&trace)) |
| 183 | goto out; |
Steven Rostedt | 71e308a | 2009-06-18 12:45:08 -0400 | [diff] [blame] | 184 | if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY) |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 185 | goto out; |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 186 | parent = (unsigned long) return_to_handler; |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 187 | out: |
| 188 | return parent; |
| 189 | } |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 190 | |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 191 | /* |
| 192 | * Patch the kernel code at ftrace_graph_caller location. The instruction |
| 193 | * there is branch relative and save to prepare_ftrace_return. To disable |
| 194 | * the call to prepare_ftrace_return we patch the bras offset to point |
| 195 | * directly after the instructions. To enable the call we calculate |
| 196 | * the original offset to prepare_ftrace_return and put it back. |
| 197 | */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 198 | |
| 199 | #ifdef CONFIG_64BIT |
| 200 | |
| 201 | int ftrace_enable_ftrace_graph_caller(void) |
| 202 | { |
| 203 | static unsigned short offset = 0x0002; |
| 204 | |
| 205 | return probe_kernel_write((void *) ftrace_graph_caller + 2, |
| 206 | &offset, sizeof(offset)); |
| 207 | } |
| 208 | |
| 209 | int ftrace_disable_ftrace_graph_caller(void) |
| 210 | { |
| 211 | unsigned short offset; |
| 212 | |
| 213 | offset = ((void *) &ftrace_graph_caller_end - |
| 214 | (void *) ftrace_graph_caller) / 2; |
| 215 | return probe_kernel_write((void *) ftrace_graph_caller + 2, |
| 216 | &offset, sizeof(offset)); |
| 217 | } |
| 218 | |
| 219 | #else /* CONFIG_64BIT */ |
| 220 | |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 221 | int ftrace_enable_ftrace_graph_caller(void) |
| 222 | { |
| 223 | unsigned short offset; |
| 224 | |
| 225 | offset = ((void *) prepare_ftrace_return - |
| 226 | (void *) ftrace_graph_caller) / 2; |
Heiko Carstens | 5eb8ae5 | 2013-09-06 19:16:14 +0200 | [diff] [blame] | 227 | return probe_kernel_write((void *) ftrace_graph_caller + 2, |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 228 | &offset, sizeof(offset)); |
| 229 | } |
| 230 | |
| 231 | int ftrace_disable_ftrace_graph_caller(void) |
| 232 | { |
| 233 | static unsigned short offset = 0x0002; |
| 234 | |
Heiko Carstens | 5eb8ae5 | 2013-09-06 19:16:14 +0200 | [diff] [blame] | 235 | return probe_kernel_write((void *) ftrace_graph_caller + 2, |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 236 | &offset, sizeof(offset)); |
| 237 | } |
| 238 | |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 239 | #endif /* CONFIG_64BIT */ |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 240 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |