AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm64/kernel/ftrace.c |
| 3 | * |
| 4 | * Copyright (C) 2013 Linaro Limited |
| 5 | * Author: AKASHI Takahiro <takahiro.akashi@linaro.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/ftrace.h> |
| 13 | #include <linux/swab.h> |
| 14 | #include <linux/uaccess.h> |
| 15 | |
| 16 | #include <asm/cacheflush.h> |
| 17 | #include <asm/ftrace.h> |
| 18 | #include <asm/insn.h> |
| 19 | |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 20 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 21 | /* |
| 22 | * Replace a single instruction, which may be a branch or NOP. |
| 23 | * If @validate == true, a replaced instruction is checked against 'old'. |
| 24 | */ |
| 25 | static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, |
| 26 | bool validate) |
| 27 | { |
| 28 | u32 replaced; |
| 29 | |
| 30 | /* |
| 31 | * Note: |
Li Bin | 004ab58 | 2015-12-04 11:38:40 +0800 | [diff] [blame] | 32 | * We are paranoid about modifying text, as if a bug were to happen, it |
| 33 | * could cause us to read or write to someplace that could cause harm. |
| 34 | * Carefully read and modify the code with aarch64_insn_*() which uses |
| 35 | * probe_kernel_*(), and make sure what we read is what we expected it |
| 36 | * to be before modifying it. |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 37 | */ |
| 38 | if (validate) { |
| 39 | if (aarch64_insn_read((void *)pc, &replaced)) |
| 40 | return -EFAULT; |
| 41 | |
| 42 | if (replaced != old) |
| 43 | return -EINVAL; |
| 44 | } |
| 45 | if (aarch64_insn_patch_text_nosync((void *)pc, new)) |
| 46 | return -EPERM; |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Replace tracer function in ftrace_caller() |
| 53 | */ |
| 54 | int ftrace_update_ftrace_func(ftrace_func_t func) |
| 55 | { |
| 56 | unsigned long pc; |
| 57 | u32 new; |
| 58 | |
| 59 | pc = (unsigned long)&ftrace_call; |
Catalin Marinas | 9f1ae75 | 2014-09-19 12:05:45 +0100 | [diff] [blame] | 60 | new = aarch64_insn_gen_branch_imm(pc, (unsigned long)func, |
| 61 | AARCH64_INSN_BRANCH_LINK); |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 62 | |
| 63 | return ftrace_modify_code(pc, 0, new, false); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Turn on the call to ftrace_caller() in instrumented function |
| 68 | */ |
| 69 | int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 70 | { |
| 71 | unsigned long pc = rec->ip; |
| 72 | u32 old, new; |
| 73 | |
| 74 | old = aarch64_insn_gen_nop(); |
Catalin Marinas | 9f1ae75 | 2014-09-19 12:05:45 +0100 | [diff] [blame] | 75 | new = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK); |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 76 | |
| 77 | return ftrace_modify_code(pc, old, new, true); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Turn off the call to ftrace_caller() in instrumented function |
| 82 | */ |
| 83 | int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, |
| 84 | unsigned long addr) |
| 85 | { |
| 86 | unsigned long pc = rec->ip; |
| 87 | u32 old, new; |
| 88 | |
Catalin Marinas | 9f1ae75 | 2014-09-19 12:05:45 +0100 | [diff] [blame] | 89 | old = aarch64_insn_gen_branch_imm(pc, addr, AARCH64_INSN_BRANCH_LINK); |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 90 | new = aarch64_insn_gen_nop(); |
| 91 | |
| 92 | return ftrace_modify_code(pc, old, new, true); |
| 93 | } |
| 94 | |
Li Bin | 81a6a14 | 2015-12-04 11:38:39 +0800 | [diff] [blame] | 95 | void arch_ftrace_update_code(int command) |
| 96 | { |
| 97 | ftrace_modify_all_code(command); |
| 98 | } |
| 99 | |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 100 | int __init ftrace_dyn_arch_init(void) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
| 105 | |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 106 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
| 107 | /* |
| 108 | * function_graph tracer expects ftrace_return_to_handler() to be called |
| 109 | * on the way back to parent. For this purpose, this function is called |
| 110 | * in _mcount() or ftrace_caller() to replace return address (*parent) on |
| 111 | * the call stack to return_to_handler. |
| 112 | * |
| 113 | * Note that @frame_pointer is used only for sanity check later. |
| 114 | */ |
| 115 | void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, |
| 116 | unsigned long frame_pointer) |
| 117 | { |
| 118 | unsigned long return_hooker = (unsigned long)&return_to_handler; |
| 119 | unsigned long old; |
| 120 | struct ftrace_graph_ent trace; |
| 121 | int err; |
| 122 | |
| 123 | if (unlikely(atomic_read(¤t->tracing_graph_pause))) |
| 124 | return; |
| 125 | |
| 126 | /* |
| 127 | * Note: |
| 128 | * No protection against faulting at *parent, which may be seen |
| 129 | * on other archs. It's unlikely on AArch64. |
| 130 | */ |
| 131 | old = *parent; |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 132 | |
| 133 | trace.func = self_addr; |
| 134 | trace.depth = current->curr_ret_stack + 1; |
| 135 | |
| 136 | /* Only trace if the calling function expects to */ |
AKASHI Takahiro | 79fdee9 | 2015-12-15 17:33:39 +0900 | [diff] [blame] | 137 | if (!ftrace_graph_entry(&trace)) |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 138 | return; |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 139 | |
| 140 | err = ftrace_push_return_trace(old, self_addr, &trace.depth, |
Josh Poimboeuf | 9a7c348 | 2016-08-19 06:52:57 -0500 | [diff] [blame^] | 141 | frame_pointer, NULL); |
AKASHI Takahiro | 79fdee9 | 2015-12-15 17:33:39 +0900 | [diff] [blame] | 142 | if (err == -EBUSY) |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 143 | return; |
AKASHI Takahiro | 79fdee9 | 2015-12-15 17:33:39 +0900 | [diff] [blame] | 144 | else |
| 145 | *parent = return_hooker; |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 146 | } |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 149 | /* |
| 150 | * Turn on/off the call to ftrace_graph_caller() in ftrace_caller() |
| 151 | * depending on @enable. |
| 152 | */ |
| 153 | static int ftrace_modify_graph_caller(bool enable) |
| 154 | { |
| 155 | unsigned long pc = (unsigned long)&ftrace_graph_call; |
| 156 | u32 branch, nop; |
| 157 | |
| 158 | branch = aarch64_insn_gen_branch_imm(pc, |
Catalin Marinas | 9f1ae75 | 2014-09-19 12:05:45 +0100 | [diff] [blame] | 159 | (unsigned long)ftrace_graph_caller, |
Pratyush Anand | d0d6223 | 2015-02-13 04:06:21 +0000 | [diff] [blame] | 160 | AARCH64_INSN_BRANCH_NOLINK); |
AKASHI Takahiro | bd7d38d | 2014-04-30 10:54:34 +0100 | [diff] [blame] | 161 | nop = aarch64_insn_gen_nop(); |
| 162 | |
| 163 | if (enable) |
| 164 | return ftrace_modify_code(pc, nop, branch, true); |
| 165 | else |
| 166 | return ftrace_modify_code(pc, branch, nop, true); |
| 167 | } |
| 168 | |
| 169 | int ftrace_enable_ftrace_graph_caller(void) |
| 170 | { |
| 171 | return ftrace_modify_graph_caller(true); |
| 172 | } |
| 173 | |
| 174 | int ftrace_disable_ftrace_graph_caller(void) |
| 175 | { |
| 176 | return ftrace_modify_graph_caller(false); |
| 177 | } |
| 178 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
AKASHI Takahiro | 819e50e | 2014-04-30 18:54:33 +0900 | [diff] [blame] | 179 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |