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 | |
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 | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 30 | * Total length is 24 bytes. The complete mcount block initially gets replaced |
| 31 | * by ftrace_make_nop. Subsequent calls to ftrace_make_call / ftrace_make_nop |
| 32 | * only patch the jg/lg instruction within the block. |
| 33 | * Note: we do not patch the first instruction to an unconditional branch, |
| 34 | * since that would break kprobes/jprobes. It is easier to leave the larl |
| 35 | * instruction in and only modify the second instruction. |
Heiko Carstens | 53255c9 | 2014-10-07 15:45:10 +0200 | [diff] [blame] | 36 | * The enabled ftrace code block looks like this: |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 37 | * larl %r0,.+24 # offset 0 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 38 | * > lg %r1,__LC_FTRACE_FUNC # offset 6 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 39 | * br %r1 # offset 12 |
| 40 | * brcl 0,0 # offset 14 |
| 41 | * brc 0,0 # offset 20 |
| 42 | * The ftrace function gets called with a non-standard C function call ABI |
| 43 | * where r0 contains the return address. It is also expected that the called |
| 44 | * function only clobbers r0 and r1, but restores r2-r15. |
| 45 | * The return point of the ftrace function has offset 24, so execution |
| 46 | * continues behind the mcount block. |
| 47 | * larl %r0,.+24 # offset 0 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 48 | * > jg .+18 # offset 6 |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 49 | * br %r1 # offset 12 |
| 50 | * brcl 0,0 # offset 14 |
| 51 | * brc 0,0 # offset 20 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 52 | * The jg instruction branches to offset 24 to skip as many instructions |
| 53 | * as possible. |
| 54 | */ |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 55 | asm( |
| 56 | " .align 4\n" |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 57 | "mcount_replace_code:\n" |
| 58 | " larl %r0,0f\n" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 59 | "ftrace_disable_code:\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 60 | " jg 0f\n" |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 61 | " br %r1\n" |
| 62 | " brcl 0,0\n" |
| 63 | " brc 0,0\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 64 | "0:\n" |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 65 | " .align 4\n" |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 66 | "ftrace_enable_insn:\n" |
| 67 | " lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n"); |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 68 | |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 69 | #define MCOUNT_BLOCK_SIZE 24 |
| 70 | #define MCOUNT_INSN_OFFSET 6 |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 71 | #define FTRACE_INSN_SIZE 6 |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 72 | |
Heiko Carstens | 10dec7d | 2014-08-15 13:01:46 +0200 | [diff] [blame] | 73 | int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, |
| 74 | unsigned long addr) |
| 75 | { |
| 76 | return 0; |
| 77 | } |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 78 | |
| 79 | int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, |
| 80 | unsigned long addr) |
| 81 | { |
Heiko Carstens | 3d1e220 | 2014-09-03 13:26:23 +0200 | [diff] [blame] | 82 | /* Initial replacement of the whole mcount block */ |
| 83 | if (addr == MCOUNT_ADDR) { |
| 84 | if (probe_kernel_write((void *) rec->ip - MCOUNT_INSN_OFFSET, |
| 85 | mcount_replace_code, |
| 86 | MCOUNT_BLOCK_SIZE)) |
| 87 | return -EPERM; |
| 88 | return 0; |
| 89 | } |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 90 | if (probe_kernel_write((void *) rec->ip, ftrace_disable_code, |
| 91 | MCOUNT_INSN_SIZE)) |
| 92 | return -EPERM; |
| 93 | return 0; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 97 | { |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 98 | if (probe_kernel_write((void *) rec->ip, ftrace_enable_insn, |
| 99 | FTRACE_INSN_SIZE)) |
| 100 | return -EPERM; |
| 101 | return 0; |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 105 | { |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Jiri Slaby | 3a36cb1 | 2014-02-24 19:59:59 +0100 | [diff] [blame] | 109 | int __init ftrace_dyn_arch_init(void) |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 110 | { |
Heiko Carstens | dfd9f7a | 2009-06-12 10:26:44 +0200 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 113 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 115 | /* |
| 116 | * Hook the return address and push it in the stack of return addresses |
| 117 | * in current thread info. |
| 118 | */ |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 119 | unsigned long __kprobes prepare_ftrace_return(unsigned long parent, |
| 120 | unsigned long ip) |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 121 | { |
| 122 | struct ftrace_graph_ent trace; |
| 123 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 124 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 125 | goto out; |
Heiko Carstens | aca9120 | 2013-05-13 14:48:52 +0200 | [diff] [blame] | 126 | ip = (ip & PSW_ADDR_INSN) - MCOUNT_INSN_SIZE; |
Heiko Carstens | 05e0baa | 2013-10-11 08:55:57 +0200 | [diff] [blame] | 127 | trace.func = ip; |
| 128 | trace.depth = current->curr_ret_stack + 1; |
| 129 | /* Only trace if the calling function expects to. */ |
| 130 | if (!ftrace_graph_entry(&trace)) |
| 131 | goto out; |
Steven Rostedt | 71e308a | 2009-06-18 12:45:08 -0400 | [diff] [blame] | 132 | if (ftrace_push_return_trace(parent, ip, &trace.depth, 0) == -EBUSY) |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 133 | goto out; |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 134 | parent = (unsigned long) return_to_handler; |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 135 | out: |
| 136 | return parent; |
| 137 | } |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 138 | |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Patch the kernel code at ftrace_graph_caller location. The instruction |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 141 | * there is branch relative on condition. To enable the ftrace graph code |
| 142 | * block, we simply patch the mask field of the instruction to zero and |
| 143 | * turn the instruction into a nop. |
| 144 | * To disable the ftrace graph code the mask field will be patched to |
| 145 | * all ones, which turns the instruction into an unconditional branch. |
Martin Schwidefsky | 4cc9bed | 2011-01-05 12:48:11 +0100 | [diff] [blame] | 146 | */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 147 | int ftrace_enable_ftrace_graph_caller(void) |
| 148 | { |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 149 | u8 op = 0x04; /* set mask field to zero */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 150 | |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 151 | return probe_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op)); |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int ftrace_disable_ftrace_graph_caller(void) |
| 155 | { |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 156 | u8 op = 0xf4; /* set mask field to all ones */ |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 157 | |
Heiko Carstens | 0cccdda | 2014-10-08 10:03:08 +0200 | [diff] [blame] | 158 | return probe_kernel_write(__va(ftrace_graph_caller)+1, &op, sizeof(op)); |
Heiko Carstens | 2481a87 | 2014-08-15 12:33:46 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Heiko Carstens | 88dbd20 | 2009-06-12 10:26:46 +0200 | [diff] [blame] | 161 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |