spi: spi-fsl-qspi: Fix return value check of devm_ioremap() in probe
[linux/fpc-iii.git] / arch / riscv / kernel / ftrace.c
blobce69b34ff55d092ae51baf338f522109481ea3d6
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2013 Linaro Limited
4 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
5 * Copyright (C) 2017 Andes Technology Corporation
6 */
8 #include <linux/ftrace.h>
9 #include <linux/uaccess.h>
10 #include <asm/cacheflush.h>
11 #include <asm/patch.h>
13 #ifdef CONFIG_DYNAMIC_FTRACE
14 static int ftrace_check_current_call(unsigned long hook_pos,
15 unsigned int *expected)
17 unsigned int replaced[2];
18 unsigned int nops[2] = {NOP4, NOP4};
20 /* we expect nops at the hook position */
21 if (!expected)
22 expected = nops;
25 * Read the text we want to modify;
26 * return must be -EFAULT on read error
28 if (probe_kernel_read(replaced, (void *)hook_pos, MCOUNT_INSN_SIZE))
29 return -EFAULT;
32 * Make sure it is what we expect it to be;
33 * return must be -EINVAL on failed comparison
35 if (memcmp(expected, replaced, sizeof(replaced))) {
36 pr_err("%p: expected (%08x %08x) but got (%08x %08x)\n",
37 (void *)hook_pos, expected[0], expected[1], replaced[0],
38 replaced[1]);
39 return -EINVAL;
42 return 0;
45 static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target,
46 bool enable)
48 unsigned int call[2];
49 unsigned int nops[2] = {NOP4, NOP4};
51 make_call(hook_pos, target, call);
53 /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */
54 if (riscv_patch_text_nosync
55 ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE))
56 return -EPERM;
58 return 0;
61 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
63 int ret = ftrace_check_current_call(rec->ip, NULL);
65 if (ret)
66 return ret;
68 return __ftrace_modify_call(rec->ip, addr, true);
71 int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
72 unsigned long addr)
74 unsigned int call[2];
75 int ret;
77 make_call(rec->ip, addr, call);
78 ret = ftrace_check_current_call(rec->ip, call);
80 if (ret)
81 return ret;
83 return __ftrace_modify_call(rec->ip, addr, false);
86 int ftrace_update_ftrace_func(ftrace_func_t func)
88 int ret = __ftrace_modify_call((unsigned long)&ftrace_call,
89 (unsigned long)func, true);
90 if (!ret) {
91 ret = __ftrace_modify_call((unsigned long)&ftrace_regs_call,
92 (unsigned long)func, true);
95 return ret;
98 int __init ftrace_dyn_arch_init(void)
100 return 0;
102 #endif
104 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
105 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
106 unsigned long addr)
108 unsigned int call[2];
109 int ret;
111 make_call(rec->ip, old_addr, call);
112 ret = ftrace_check_current_call(rec->ip, call);
114 if (ret)
115 return ret;
117 return __ftrace_modify_call(rec->ip, addr, true);
119 #endif
121 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
123 * Most of this function is copied from arm64.
125 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
126 unsigned long frame_pointer)
128 unsigned long return_hooker = (unsigned long)&return_to_handler;
129 unsigned long old;
131 if (unlikely(atomic_read(&current->tracing_graph_pause)))
132 return;
135 * We don't suffer access faults, so no extra fault-recovery assembly
136 * is needed here.
138 old = *parent;
140 if (!function_graph_enter(old, self_addr, frame_pointer, parent))
141 *parent = return_hooker;
144 #ifdef CONFIG_DYNAMIC_FTRACE
145 extern void ftrace_graph_call(void);
146 int ftrace_enable_ftrace_graph_caller(void)
148 unsigned int call[2];
149 static int init_graph = 1;
150 int ret;
152 make_call(&ftrace_graph_call, &ftrace_stub, call);
155 * When enabling graph tracer for the first time, ftrace_graph_call
156 * should contains a call to ftrace_stub. Once it has been disabled,
157 * the 8-bytes at the position becomes NOPs.
159 if (init_graph) {
160 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
161 call);
162 init_graph = 0;
163 } else {
164 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
165 NULL);
168 if (ret)
169 return ret;
171 return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
172 (unsigned long)&prepare_ftrace_return, true);
175 int ftrace_disable_ftrace_graph_caller(void)
177 unsigned int call[2];
178 int ret;
180 make_call(&ftrace_graph_call, &prepare_ftrace_return, call);
183 * This is to make sure that ftrace_enable_ftrace_graph_caller
184 * did the right thing.
186 ret = ftrace_check_current_call((unsigned long)&ftrace_graph_call,
187 call);
189 if (ret)
190 return ret;
192 return __ftrace_modify_call((unsigned long)&ftrace_graph_call,
193 (unsigned long)&prepare_ftrace_return, false);
195 #endif /* CONFIG_DYNAMIC_FTRACE */
196 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */