mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / tile / kernel / ftrace.c
blobb827a418b155021104d27de2d821cf81e59babce
1 /*
2 * Copyright 2012 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
14 * TILE-Gx specific ftrace support
17 #include <linux/ftrace.h>
18 #include <linux/uaccess.h>
20 #include <asm/cacheflush.h>
21 #include <asm/ftrace.h>
22 #include <asm/sections.h>
23 #include <asm/insn.h>
25 #include <arch/opcode.h>
27 #ifdef CONFIG_DYNAMIC_FTRACE
29 static int machine_stopped __read_mostly;
31 int ftrace_arch_code_modify_prepare(void)
33 machine_stopped = 1;
34 return 0;
37 int ftrace_arch_code_modify_post_process(void)
39 flush_icache_range(0, CHIP_L1I_CACHE_SIZE());
40 machine_stopped = 0;
41 return 0;
45 * Put { move r10, lr; jal ftrace_caller } in a bundle, this lets dynamic
46 * tracer just add one cycle overhead to every kernel function when disabled.
48 static unsigned long ftrace_gen_branch(unsigned long pc, unsigned long addr,
49 bool link)
51 tilegx_bundle_bits opcode_x0, opcode_x1;
52 long pcrel_by_instr = (addr - pc) >> TILEGX_LOG2_BUNDLE_SIZE_IN_BYTES;
54 if (link) {
55 /* opcode: jal addr */
56 opcode_x1 =
57 create_Opcode_X1(JUMP_OPCODE_X1) |
58 create_JumpOpcodeExtension_X1(JAL_JUMP_OPCODE_X1) |
59 create_JumpOff_X1(pcrel_by_instr);
60 } else {
61 /* opcode: j addr */
62 opcode_x1 =
63 create_Opcode_X1(JUMP_OPCODE_X1) |
64 create_JumpOpcodeExtension_X1(J_JUMP_OPCODE_X1) |
65 create_JumpOff_X1(pcrel_by_instr);
69 * Also put { move r10, lr; jal ftrace_stub } in a bundle, which
70 * is used to replace the instruction in address ftrace_call.
72 if (addr == FTRACE_ADDR || addr == (unsigned long)ftrace_stub) {
73 /* opcode: or r10, lr, zero */
74 opcode_x0 =
75 create_Dest_X0(10) |
76 create_SrcA_X0(TREG_LR) |
77 create_SrcB_X0(TREG_ZERO) |
78 create_RRROpcodeExtension_X0(OR_RRR_0_OPCODE_X0) |
79 create_Opcode_X0(RRR_0_OPCODE_X0);
80 } else {
81 /* opcode: fnop */
82 opcode_x0 =
83 create_UnaryOpcodeExtension_X0(FNOP_UNARY_OPCODE_X0) |
84 create_RRROpcodeExtension_X0(UNARY_RRR_0_OPCODE_X0) |
85 create_Opcode_X0(RRR_0_OPCODE_X0);
88 return opcode_x1 | opcode_x0;
91 static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
93 return NOP();
96 static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
98 return ftrace_gen_branch(pc, addr, true);
101 static int ftrace_modify_code(unsigned long pc, unsigned long old,
102 unsigned long new)
104 unsigned long pc_wr;
106 /* Check if the address is in kernel text space and module space. */
107 if (!kernel_text_address(pc))
108 return -EINVAL;
110 /* Operate on writable kernel text mapping. */
111 pc_wr = ktext_writable_addr(pc);
113 if (probe_kernel_write((void *)pc_wr, &new, MCOUNT_INSN_SIZE))
114 return -EPERM;
116 smp_wmb();
118 if (!machine_stopped && num_online_cpus() > 1)
119 flush_icache_range(pc, pc + MCOUNT_INSN_SIZE);
121 return 0;
124 int ftrace_update_ftrace_func(ftrace_func_t func)
126 unsigned long pc, old;
127 unsigned long new;
128 int ret;
130 pc = (unsigned long)&ftrace_call;
131 memcpy(&old, &ftrace_call, MCOUNT_INSN_SIZE);
132 new = ftrace_call_replace(pc, (unsigned long)func);
134 ret = ftrace_modify_code(pc, old, new);
136 return ret;
139 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
141 unsigned long new, old;
142 unsigned long ip = rec->ip;
144 old = ftrace_nop_replace(rec);
145 new = ftrace_call_replace(ip, addr);
147 return ftrace_modify_code(rec->ip, old, new);
150 int ftrace_make_nop(struct module *mod,
151 struct dyn_ftrace *rec, unsigned long addr)
153 unsigned long ip = rec->ip;
154 unsigned long old;
155 unsigned long new;
156 int ret;
158 old = ftrace_call_replace(ip, addr);
159 new = ftrace_nop_replace(rec);
160 ret = ftrace_modify_code(ip, old, new);
162 return ret;
165 int __init ftrace_dyn_arch_init(void)
167 return 0;
169 #endif /* CONFIG_DYNAMIC_FTRACE */
171 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
172 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
173 unsigned long frame_pointer)
175 unsigned long return_hooker = (unsigned long) &return_to_handler;
176 struct ftrace_graph_ent trace;
177 unsigned long old;
178 int err;
180 if (unlikely(atomic_read(&current->tracing_graph_pause)))
181 return;
183 old = *parent;
184 *parent = return_hooker;
186 err = ftrace_push_return_trace(old, self_addr, &trace.depth,
187 frame_pointer, NULL);
188 if (err == -EBUSY) {
189 *parent = old;
190 return;
193 trace.func = self_addr;
195 /* Only trace if the calling function expects to */
196 if (!ftrace_graph_entry(&trace)) {
197 current->curr_ret_stack--;
198 *parent = old;
202 #ifdef CONFIG_DYNAMIC_FTRACE
203 extern unsigned long ftrace_graph_call;
205 static int __ftrace_modify_caller(unsigned long *callsite,
206 void (*func) (void), bool enable)
208 unsigned long caller_fn = (unsigned long) func;
209 unsigned long pc = (unsigned long) callsite;
210 unsigned long branch = ftrace_gen_branch(pc, caller_fn, false);
211 unsigned long nop = NOP();
212 unsigned long old = enable ? nop : branch;
213 unsigned long new = enable ? branch : nop;
215 return ftrace_modify_code(pc, old, new);
218 static int ftrace_modify_graph_caller(bool enable)
220 int ret;
222 ret = __ftrace_modify_caller(&ftrace_graph_call,
223 ftrace_graph_caller,
224 enable);
226 return ret;
229 int ftrace_enable_ftrace_graph_caller(void)
231 return ftrace_modify_graph_caller(true);
234 int ftrace_disable_ftrace_graph_caller(void)
236 return ftrace_modify_graph_caller(false);
238 #endif /* CONFIG_DYNAMIC_FTRACE */
239 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */