Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Dynamic function tracer architecture backend. |
| 4 | * |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2009,2014 |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 6 | * |
| 7 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>, |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 8 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 11 | #include <linux/moduleloader.h> |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 12 | #include <linux/hardirq.h> |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
| 14 | #include <linux/ftrace.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/types.h> |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 17 | #include <linux/kprobes.h> |
Heiko Carstens | 9bf1226 | 2009-06-12 10:26:47 +0200 | [diff] [blame] | 18 | #include <trace/syscall.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 19 | #include <asm/asm-offsets.h> |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 20 | #include <asm/cacheflush.h> |
Laura Abbott | e6c7c63 | 2017-05-08 15:58:08 -0700 | [diff] [blame] | 21 | #include <asm/set_memory.h> |
Heiko Carstens | 63df41d6 | 2013-09-06 19:10:48 +0200 | [diff] [blame] | 22 | #include "entry.h" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 23 | |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 24 | /* |
Heiko Carstens | 53255c9 | 2014-10-07 15:45:10 +0200 | [diff] [blame] | 25 | * The mcount code looks like this: |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 26 | * stg %r14,8(%r15) # offset 0 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 27 | * larl %r1,<&counter> # offset 6 |
| 28 | * brasl %r14,_mcount # offset 12 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 29 | * lg %r14,8(%r15) # offset 18 |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 30 | * Total length is 24 bytes. Only the first instruction will be patched |
| 31 | * by ftrace_make_call / ftrace_make_nop. |
Heiko Carstens | 53255c9 | 2014-10-07 15:45:10 +0200 | [diff] [blame] | 32 | * The enabled ftrace code block looks like this: |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 33 | * > brasl %r0,ftrace_caller # offset 0 |
| 34 | * larl %r1,<&counter> # offset 6 |
| 35 | * brasl %r14,_mcount # offset 12 |
| 36 | * lg %r14,8(%r15) # offset 18 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 37 | * The ftrace function gets called with a non-standard C function call ABI |
| 38 | * where r0 contains the return address. It is also expected that the called |
| 39 | * function only clobbers r0 and r1, but restores r2-r15. |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 40 | * For module code we can't directly jump to ftrace caller, but need a |
| 41 | * trampoline (ftrace_plt), which clobbers also r1. |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 42 | * The return point of the ftrace function has offset 24, so execution |
| 43 | * continues behind the mcount block. |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 44 | * The disabled ftrace code block looks like this: |
| 45 | * > jg .+24 # offset 0 |
| 46 | * larl %r1,<&counter> # offset 6 |
| 47 | * brasl %r14,_mcount # offset 12 |
| 48 | * lg %r14,8(%r15) # offset 18 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 49 | * The jg instruction branches to offset 24 to skip as many instructions |
| 50 | * as possible. |
Heiko Carstens | e6d60b3 | 2015-01-09 13:08:28 +0100 | [diff] [blame] | 51 | * In case we use gcc's hotpatch feature the original and also the disabled |
| 52 | * function prologue contains only a single six byte instruction and looks |
| 53 | * like this: |
| 54 | * > brcl 0,0 # offset 0 |
| 55 | * To enable ftrace the code gets patched like above and afterwards looks |
| 56 | * like this: |
| 57 | * > brasl %r0,ftrace_caller # offset 0 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 58 | */ |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 59 | |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 60 | unsigned long ftrace_plt; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 61 | |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 62 | static inline void ftrace_generate_orig_insn(struct ftrace_insn *insn) |
| 63 | { |
Vasily Gorbik | d983c89 | 2018-08-06 15:17:47 +0200 | [diff] [blame] | 64 | #if defined(CC_USING_HOTPATCH) || defined(CC_USING_NOP_MCOUNT) |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 65 | /* brcl 0,0 */ |
| 66 | insn->opc = 0xc004; |
| 67 | insn->disp = 0; |
| 68 | #else |
| 69 | /* stg r14,8(r15) */ |
| 70 | insn->opc = 0xe3e0; |
| 71 | insn->disp = 0xf0080024; |
| 72 | #endif |
| 73 | } |
| 74 | |
| 75 | static inline int is_kprobe_on_ftrace(struct ftrace_insn *insn) |
| 76 | { |
| 77 | #ifdef CONFIG_KPROBES |
| 78 | if (insn->opc == BREAKPOINT_INSTRUCTION) |
| 79 | return 1; |
| 80 | #endif |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static inline void ftrace_generate_kprobe_nop_insn(struct ftrace_insn *insn) |
| 85 | { |
| 86 | #ifdef CONFIG_KPROBES |
| 87 | insn->opc = BREAKPOINT_INSTRUCTION; |
| 88 | insn->disp = KPROBE_ON_FTRACE_NOP; |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | static inline void ftrace_generate_kprobe_call_insn(struct ftrace_insn *insn) |
| 93 | { |
| 94 | #ifdef CONFIG_KPROBES |
| 95 | insn->opc = BREAKPOINT_INSTRUCTION; |
| 96 | insn->disp = KPROBE_ON_FTRACE_CALL; |
| 97 | #endif |
| 98 | } |
| 99 | |
Heiko Carstens | 10dec7d | 2014-08-15 13:01:46 +0200 | [diff] [blame] | 100 | int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, |
| 101 | unsigned long addr) |
| 102 | { |
| 103 | return 0; |
| 104 | } |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 105 | |
| 106 | int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, |
| 107 | unsigned long addr) |
| 108 | { |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 109 | struct ftrace_insn orig, new, old; |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 110 | |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 111 | if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old))) |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 112 | return -EFAULT; |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 113 | if (addr == MCOUNT_ADDR) { |
Heiko Carstens | e6d60b3 | 2015-01-09 13:08:28 +0100 | [diff] [blame] | 114 | /* Initial code replacement */ |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 115 | ftrace_generate_orig_insn(&orig); |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 116 | ftrace_generate_nop_insn(&new); |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 117 | } else if (is_kprobe_on_ftrace(&old)) { |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 118 | /* |
| 119 | * If we find a breakpoint instruction, a kprobe has been |
| 120 | * placed at the beginning of the function. We write the |
| 121 | * constant KPROBE_ON_FTRACE_NOP into the remaining four |
| 122 | * bytes of the original instruction so that the kprobes |
| 123 | * handler can execute a nop, if it reaches this breakpoint. |
| 124 | */ |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 125 | ftrace_generate_kprobe_call_insn(&orig); |
| 126 | ftrace_generate_kprobe_nop_insn(&new); |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 127 | } else { |
| 128 | /* Replace ftrace call with a nop. */ |
| 129 | ftrace_generate_call_insn(&orig, rec->ip); |
| 130 | ftrace_generate_nop_insn(&new); |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 131 | } |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 132 | /* Verify that the to be replaced code matches what we expect. */ |
| 133 | if (memcmp(&orig, &old, sizeof(old))) |
| 134 | return -EINVAL; |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 135 | s390_kernel_write((void *) rec->ip, &new, sizeof(new)); |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 136 | return 0; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 140 | { |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 141 | struct ftrace_insn orig, new, old; |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 142 | |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 143 | if (probe_kernel_read(&old, (void *) rec->ip, sizeof(old))) |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 144 | return -EFAULT; |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 145 | if (is_kprobe_on_ftrace(&old)) { |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 146 | /* |
| 147 | * If we find a breakpoint instruction, a kprobe has been |
| 148 | * placed at the beginning of the function. We write the |
| 149 | * constant KPROBE_ON_FTRACE_CALL into the remaining four |
| 150 | * bytes of the original instruction so that the kprobes |
| 151 | * handler can execute a brasl if it reaches this breakpoint. |
| 152 | */ |
Heiko Carstens | 20e76ee | 2015-03-13 10:31:42 +0100 | [diff] [blame] | 153 | ftrace_generate_kprobe_nop_insn(&orig); |
| 154 | ftrace_generate_kprobe_call_insn(&new); |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 155 | } else { |
| 156 | /* Replace nop with an ftrace call. */ |
| 157 | ftrace_generate_nop_insn(&orig); |
| 158 | ftrace_generate_call_insn(&new, rec->ip); |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 159 | } |
Heiko Carstens | 58498ee | 2014-12-09 10:18:49 +0100 | [diff] [blame] | 160 | /* Verify that the to be replaced code matches what we expect. */ |
| 161 | if (memcmp(&orig, &old, sizeof(old))) |
| 162 | return -EINVAL; |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 163 | s390_kernel_write((void *) rec->ip, &new, sizeof(new)); |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 164 | return 0; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 168 | { |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 172 | int __init ftrace_dyn_arch_init(void) |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 173 | { |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 174 | return 0; |
| 175 | } |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 176 | |
Heiko Carstens | 085b6ba | 2017-05-02 12:38:57 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_MODULES |
| 178 | |
Heiko Carstens | c933146 | 2014-10-15 12:17:38 +0200 | [diff] [blame] | 179 | static int __init ftrace_plt_init(void) |
| 180 | { |
| 181 | unsigned int *ip; |
| 182 | |
| 183 | ftrace_plt = (unsigned long) module_alloc(PAGE_SIZE); |
| 184 | if (!ftrace_plt) |
| 185 | panic("cannot allocate ftrace plt\n"); |
| 186 | ip = (unsigned int *) ftrace_plt; |
| 187 | ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */ |
| 188 | ip[1] = 0x100a0004; |
| 189 | ip[2] = 0x07f10000; |
| 190 | ip[3] = FTRACE_ADDR >> 32; |
| 191 | ip[4] = FTRACE_ADDR & 0xffffffff; |
| 192 | set_memory_ro(ftrace_plt, 1); |
| 193 | return 0; |
| 194 | } |
| 195 | device_initcall(ftrace_plt_init); |
| 196 | |
Heiko Carstens | 085b6ba | 2017-05-02 12:38:57 +0200 | [diff] [blame] | 197 | #endif /* CONFIG_MODULES */ |
| 198 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Hook the return address and push it in the stack of return addresses |
| 202 | * in current thread info. |
| 203 | */ |
Heiko Carstens | 7a5388d | 2014-10-22 12:42:38 +0200 | [diff] [blame] | 204 | unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip) |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 205 | { |
| 206 | struct ftrace_graph_ent trace; |
| 207 | |
Heiko Carstens | 6ed15ea | 2014-10-27 15:49:06 +0100 | [diff] [blame] | 208 | if (unlikely(ftrace_graph_is_dead())) |
| 209 | goto out; |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 210 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 211 | goto out; |
Heiko Carstens | 9cb1cce | 2016-01-18 13:12:19 +0100 | [diff] [blame] | 212 | ip -= MCOUNT_INSN_SIZE; |
Heiko Carstens | 05e0baa | 2013-10-11 08:55:57 +0200 | [diff] [blame] | 213 | trace.func = ip; |
| 214 | trace.depth = current->curr_ret_stack + 1; |
| 215 | /* Only trace if the calling function expects to. */ |
| 216 | if (!ftrace_graph_entry(&trace)) |
| 217 | goto out; |
Josh Poimboeuf | 9a7c348 | 2016-08-19 06:52:57 -0500 | [diff] [blame] | 218 | if (ftrace_push_return_trace(parent, ip, &trace.depth, 0, |
| 219 | NULL) == -EBUSY) |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 220 | goto out; |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 221 | parent = (unsigned long) return_to_handler; |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 222 | out: |
| 223 | return parent; |
| 224 | } |
Heiko Carstens | 7a5388d | 2014-10-22 12:42:38 +0200 | [diff] [blame] | 225 | NOKPROBE_SYMBOL(prepare_ftrace_return); |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 226 | |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 227 | /* |
| 228 | * Patch the kernel code at ftrace_graph_caller location. The instruction |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 229 | * there is branch relative on condition. To enable the ftrace graph code |
| 230 | * block, we simply patch the mask field of the instruction to zero and |
| 231 | * turn the instruction into a nop. |
| 232 | * To disable the ftrace graph code the mask field will be patched to |
| 233 | * all ones, which turns the instruction into an unconditional branch. |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 234 | */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 235 | int ftrace_enable_ftrace_graph_caller(void) |
| 236 | { |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 237 | u8 op = 0x04; /* set mask field to zero */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 238 | |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 239 | s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op)); |
| 240 | return 0; |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | int ftrace_disable_ftrace_graph_caller(void) |
| 244 | { |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 245 | u8 op = 0xf4; /* set mask field to all ones */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 246 | |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 247 | s390_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op)); |
| 248 | return 0; |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 251 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |