1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27853f9c0SNaveen N. Rao /*
37853f9c0SNaveen N. Rao * Code for replacing ftrace calls with jumps.
47853f9c0SNaveen N. Rao *
57853f9c0SNaveen N. Rao * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
67853f9c0SNaveen N. Rao *
77853f9c0SNaveen N. Rao * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
87853f9c0SNaveen N. Rao *
97853f9c0SNaveen N. Rao * Added function graph tracer code, taken from x86 that was written
107853f9c0SNaveen N. Rao * by Frederic Weisbecker, and ported to PPC by Steven Rostedt.
117853f9c0SNaveen N. Rao *
127853f9c0SNaveen N. Rao */
137853f9c0SNaveen N. Rao
147853f9c0SNaveen N. Rao #define pr_fmt(fmt) "ftrace-powerpc: " fmt
157853f9c0SNaveen N. Rao
167853f9c0SNaveen N. Rao #include <linux/spinlock.h>
177853f9c0SNaveen N. Rao #include <linux/hardirq.h>
187853f9c0SNaveen N. Rao #include <linux/uaccess.h>
197853f9c0SNaveen N. Rao #include <linux/module.h>
207853f9c0SNaveen N. Rao #include <linux/ftrace.h>
217853f9c0SNaveen N. Rao #include <linux/percpu.h>
227853f9c0SNaveen N. Rao #include <linux/init.h>
237853f9c0SNaveen N. Rao #include <linux/list.h>
247853f9c0SNaveen N. Rao
257853f9c0SNaveen N. Rao #include <asm/cacheflush.h>
267853f9c0SNaveen N. Rao #include <asm/code-patching.h>
277853f9c0SNaveen N. Rao #include <asm/ftrace.h>
287853f9c0SNaveen N. Rao #include <asm/syscall.h>
2975346251SJordan Niethe #include <asm/inst.h>
30*ea73179eSNaveen N Rao #include <asm/sections.h>
317853f9c0SNaveen N. Rao
3233bb8a0bSNaveen N Rao #define NUM_FTRACE_TRAMPS 2
3367361cf8SNaveen N. Rao static unsigned long ftrace_tramps[NUM_FTRACE_TRAMPS];
3467361cf8SNaveen N. Rao
ftrace_call_adjust(unsigned long addr)35*ea73179eSNaveen N Rao unsigned long ftrace_call_adjust(unsigned long addr)
36*ea73179eSNaveen N Rao {
37*ea73179eSNaveen N Rao if (addr >= (unsigned long)__exittext_begin && addr < (unsigned long)__exittext_end)
38*ea73179eSNaveen N Rao return 0;
39*ea73179eSNaveen N Rao
40*ea73179eSNaveen N Rao if (IS_ENABLED(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY))
41*ea73179eSNaveen N Rao addr += MCOUNT_INSN_SIZE;
42*ea73179eSNaveen N Rao
43*ea73179eSNaveen N Rao return addr;
44*ea73179eSNaveen N Rao }
45*ea73179eSNaveen N Rao
ftrace_create_branch_inst(unsigned long ip,unsigned long addr,int link)46cc93b923SNaveen N Rao static ppc_inst_t ftrace_create_branch_inst(unsigned long ip, unsigned long addr, int link)
47cc93b923SNaveen N Rao {
48cc93b923SNaveen N Rao ppc_inst_t op;
49cc93b923SNaveen N Rao
50cc93b923SNaveen N Rao WARN_ON(!is_offset_in_branch_range(addr - ip));
51cc93b923SNaveen N Rao create_branch(&op, (u32 *)ip, addr, link ? BRANCH_SET_LINK : 0);
52cc93b923SNaveen N Rao
53cc93b923SNaveen N Rao return op;
54cc93b923SNaveen N Rao }
55cc93b923SNaveen N Rao
ftrace_read_inst(unsigned long ip,ppc_inst_t * op)56f4fcbf2eSNaveen N Rao static inline int ftrace_read_inst(unsigned long ip, ppc_inst_t *op)
577853f9c0SNaveen N. Rao {
58f4fcbf2eSNaveen N Rao if (copy_inst_from_kernel_nofault(op, (void *)ip)) {
59f4fcbf2eSNaveen N Rao pr_err("0x%lx: fetching instruction failed\n", ip);
607853f9c0SNaveen N. Rao return -EFAULT;
617853f9c0SNaveen N. Rao }
627853f9c0SNaveen N. Rao
63f4fcbf2eSNaveen N Rao return 0;
64f4fcbf2eSNaveen N Rao }
65f4fcbf2eSNaveen N Rao
ftrace_validate_inst(unsigned long ip,ppc_inst_t inst)66f4fcbf2eSNaveen N Rao static inline int ftrace_validate_inst(unsigned long ip, ppc_inst_t inst)
67f4fcbf2eSNaveen N Rao {
68f4fcbf2eSNaveen N Rao ppc_inst_t op;
69f4fcbf2eSNaveen N Rao int ret;
70f4fcbf2eSNaveen N Rao
71f4fcbf2eSNaveen N Rao ret = ftrace_read_inst(ip, &op);
72f4fcbf2eSNaveen N Rao if (!ret && !ppc_inst_equal(op, inst)) {
73f4fcbf2eSNaveen N Rao pr_err("0x%lx: expected (%08lx) != found (%08lx)\n",
74f4fcbf2eSNaveen N Rao ip, ppc_inst_as_ulong(inst), ppc_inst_as_ulong(op));
75f4fcbf2eSNaveen N Rao ret = -EINVAL;
76f4fcbf2eSNaveen N Rao }
77f4fcbf2eSNaveen N Rao
78f4fcbf2eSNaveen N Rao return ret;
79f4fcbf2eSNaveen N Rao }
80f4fcbf2eSNaveen N Rao
ftrace_modify_code(unsigned long ip,ppc_inst_t old,ppc_inst_t new)81f4fcbf2eSNaveen N Rao static inline int ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
82f4fcbf2eSNaveen N Rao {
83f4fcbf2eSNaveen N Rao int ret = ftrace_validate_inst(ip, old);
84f4fcbf2eSNaveen N Rao
85f4fcbf2eSNaveen N Rao if (!ret)
86f4fcbf2eSNaveen N Rao ret = patch_instruction((u32 *)ip, new);
87f4fcbf2eSNaveen N Rao
88f4fcbf2eSNaveen N Rao return ret;
897853f9c0SNaveen N. Rao }
907853f9c0SNaveen N. Rao
is_bl_op(ppc_inst_t op)91c545b9f0SChristophe Leroy static int is_bl_op(ppc_inst_t op)
927853f9c0SNaveen N. Rao {
93e89aa642SChristophe Leroy return (ppc_inst_val(op) & ~PPC_LI_MASK) == PPC_RAW_BL(0);
947853f9c0SNaveen N. Rao }
957853f9c0SNaveen N. Rao
find_ftrace_tramp(unsigned long ip)9667361cf8SNaveen N. Rao static unsigned long find_ftrace_tramp(unsigned long ip)
9767361cf8SNaveen N. Rao {
9867361cf8SNaveen N. Rao int i;
9967361cf8SNaveen N. Rao
10033bb8a0bSNaveen N Rao for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
10167361cf8SNaveen N. Rao if (!ftrace_tramps[i])
10267361cf8SNaveen N. Rao continue;
103a1facd25SChristophe Leroy else if (is_offset_in_branch_range(ftrace_tramps[i] - ip))
10467361cf8SNaveen N. Rao return ftrace_tramps[i];
10567361cf8SNaveen N. Rao
10667361cf8SNaveen N. Rao return 0;
10767361cf8SNaveen N. Rao }
10867361cf8SNaveen N. Rao
ftrace_get_call_inst(struct dyn_ftrace * rec,unsigned long addr,ppc_inst_t * call_inst)109c91c5a82SNaveen N Rao static int ftrace_get_call_inst(struct dyn_ftrace *rec, unsigned long addr, ppc_inst_t *call_inst)
110c91c5a82SNaveen N Rao {
111c91c5a82SNaveen N Rao unsigned long ip = rec->ip;
112c91c5a82SNaveen N Rao unsigned long stub;
113c91c5a82SNaveen N Rao
114c91c5a82SNaveen N Rao if (is_offset_in_branch_range(addr - ip)) {
115c91c5a82SNaveen N Rao /* Within range */
116c91c5a82SNaveen N Rao stub = addr;
117c91c5a82SNaveen N Rao #ifdef CONFIG_MODULES
118c91c5a82SNaveen N Rao } else if (rec->arch.mod) {
119c91c5a82SNaveen N Rao /* Module code would be going to one of the module stubs */
120c91c5a82SNaveen N Rao stub = (addr == (unsigned long)ftrace_caller ? rec->arch.mod->arch.tramp :
121c91c5a82SNaveen N Rao rec->arch.mod->arch.tramp_regs);
122c91c5a82SNaveen N Rao #endif
123c91c5a82SNaveen N Rao } else if (core_kernel_text(ip)) {
124c91c5a82SNaveen N Rao /* We would be branching to one of our ftrace stubs */
125c91c5a82SNaveen N Rao stub = find_ftrace_tramp(ip);
126c91c5a82SNaveen N Rao if (!stub) {
127c91c5a82SNaveen N Rao pr_err("0x%lx: No ftrace stubs reachable\n", ip);
128c91c5a82SNaveen N Rao return -EINVAL;
129c91c5a82SNaveen N Rao }
130c91c5a82SNaveen N Rao } else {
131c91c5a82SNaveen N Rao return -EINVAL;
132c91c5a82SNaveen N Rao }
133c91c5a82SNaveen N Rao
134c91c5a82SNaveen N Rao *call_inst = ftrace_create_branch_inst(ip, stub, 1);
135c91c5a82SNaveen N Rao return 0;
136c91c5a82SNaveen N Rao }
137c91c5a82SNaveen N Rao
138ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
ftrace_modify_call(struct dyn_ftrace * rec,unsigned long old_addr,unsigned long addr)13967385738SNaveen N Rao int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, unsigned long addr)
140ae30cc05SNaveen N. Rao {
141c91c5a82SNaveen N Rao /* This should never be called since we override ftrace_replace_code() */
142c91c5a82SNaveen N Rao WARN_ON(1);
143ae30cc05SNaveen N. Rao return -EINVAL;
144ae30cc05SNaveen N. Rao }
145ae30cc05SNaveen N. Rao #endif
146ae30cc05SNaveen N. Rao
ftrace_make_call(struct dyn_ftrace * rec,unsigned long addr)1479365e23bSNaveen N Rao int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
1489365e23bSNaveen N Rao {
1499365e23bSNaveen N Rao ppc_inst_t old, new;
150c91c5a82SNaveen N Rao int ret;
151c91c5a82SNaveen N Rao
152c91c5a82SNaveen N Rao /* This can only ever be called during module load */
153c91c5a82SNaveen N Rao if (WARN_ON(!IS_ENABLED(CONFIG_MODULES) || core_kernel_text(rec->ip)))
154c91c5a82SNaveen N Rao return -EINVAL;
1559365e23bSNaveen N Rao
1569365e23bSNaveen N Rao old = ppc_inst(PPC_RAW_NOP());
157c91c5a82SNaveen N Rao ret = ftrace_get_call_inst(rec, addr, &new);
158c91c5a82SNaveen N Rao if (ret)
159c91c5a82SNaveen N Rao return ret;
1609365e23bSNaveen N Rao
161c91c5a82SNaveen N Rao return ftrace_modify_code(rec->ip, old, new);
1629365e23bSNaveen N Rao }
1639365e23bSNaveen N Rao
ftrace_make_nop(struct module * mod,struct dyn_ftrace * rec,unsigned long addr)164562bde0bSNaveen N Rao int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
165562bde0bSNaveen N Rao {
166c91c5a82SNaveen N Rao /*
167c91c5a82SNaveen N Rao * This should never be called since we override ftrace_replace_code(),
168c91c5a82SNaveen N Rao * as well as ftrace_init_nop()
169c91c5a82SNaveen N Rao */
170c91c5a82SNaveen N Rao WARN_ON(1);
171562bde0bSNaveen N Rao return -EINVAL;
172562bde0bSNaveen N Rao }
173c91c5a82SNaveen N Rao
ftrace_replace_code(int enable)174c91c5a82SNaveen N Rao void ftrace_replace_code(int enable)
175c91c5a82SNaveen N Rao {
176c91c5a82SNaveen N Rao ppc_inst_t old, new, call_inst, new_call_inst;
177c91c5a82SNaveen N Rao ppc_inst_t nop_inst = ppc_inst(PPC_RAW_NOP());
178c91c5a82SNaveen N Rao unsigned long ip, new_addr, addr;
179c91c5a82SNaveen N Rao struct ftrace_rec_iter *iter;
180c91c5a82SNaveen N Rao struct dyn_ftrace *rec;
181c91c5a82SNaveen N Rao int ret = 0, update;
182c91c5a82SNaveen N Rao
183c91c5a82SNaveen N Rao for_ftrace_rec_iter(iter) {
184c91c5a82SNaveen N Rao rec = ftrace_rec_iter_record(iter);
185c91c5a82SNaveen N Rao ip = rec->ip;
186c91c5a82SNaveen N Rao
187c91c5a82SNaveen N Rao if (rec->flags & FTRACE_FL_DISABLED && !(rec->flags & FTRACE_FL_ENABLED))
188c91c5a82SNaveen N Rao continue;
189c91c5a82SNaveen N Rao
190c91c5a82SNaveen N Rao addr = ftrace_get_addr_curr(rec);
191c91c5a82SNaveen N Rao new_addr = ftrace_get_addr_new(rec);
192c91c5a82SNaveen N Rao update = ftrace_update_record(rec, enable);
193c91c5a82SNaveen N Rao
194c91c5a82SNaveen N Rao switch (update) {
195c91c5a82SNaveen N Rao case FTRACE_UPDATE_IGNORE:
196c91c5a82SNaveen N Rao default:
197c91c5a82SNaveen N Rao continue;
198c91c5a82SNaveen N Rao case FTRACE_UPDATE_MODIFY_CALL:
199c91c5a82SNaveen N Rao ret = ftrace_get_call_inst(rec, new_addr, &new_call_inst);
200c91c5a82SNaveen N Rao ret |= ftrace_get_call_inst(rec, addr, &call_inst);
201c91c5a82SNaveen N Rao old = call_inst;
202c91c5a82SNaveen N Rao new = new_call_inst;
203c91c5a82SNaveen N Rao break;
204c91c5a82SNaveen N Rao case FTRACE_UPDATE_MAKE_NOP:
205c91c5a82SNaveen N Rao ret = ftrace_get_call_inst(rec, addr, &call_inst);
206c91c5a82SNaveen N Rao old = call_inst;
207c91c5a82SNaveen N Rao new = nop_inst;
208c91c5a82SNaveen N Rao break;
209c91c5a82SNaveen N Rao case FTRACE_UPDATE_MAKE_CALL:
210c91c5a82SNaveen N Rao ret = ftrace_get_call_inst(rec, new_addr, &call_inst);
211c91c5a82SNaveen N Rao old = nop_inst;
212c91c5a82SNaveen N Rao new = call_inst;
213c91c5a82SNaveen N Rao break;
214562bde0bSNaveen N Rao }
215562bde0bSNaveen N Rao
216c91c5a82SNaveen N Rao if (!ret)
217c91c5a82SNaveen N Rao ret = ftrace_modify_code(ip, old, new);
218c91c5a82SNaveen N Rao if (ret)
219c91c5a82SNaveen N Rao goto out;
220c91c5a82SNaveen N Rao }
221c91c5a82SNaveen N Rao
222c91c5a82SNaveen N Rao out:
223c91c5a82SNaveen N Rao if (ret)
224c91c5a82SNaveen N Rao ftrace_bug(ret, rec);
225c91c5a82SNaveen N Rao return;
226562bde0bSNaveen N Rao }
227562bde0bSNaveen N Rao
ftrace_init_nop(struct module * mod,struct dyn_ftrace * rec)228cc93b923SNaveen N Rao int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
229cc93b923SNaveen N Rao {
230cc93b923SNaveen N Rao unsigned long addr, ip = rec->ip;
231cc93b923SNaveen N Rao ppc_inst_t old, new;
232cc93b923SNaveen N Rao int ret = 0;
233cc93b923SNaveen N Rao
234cc93b923SNaveen N Rao /* Verify instructions surrounding the ftrace location */
2350f71dcfbSNaveen N Rao if (IS_ENABLED(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY)) {
2360f71dcfbSNaveen N Rao /* Expect nops */
2370f71dcfbSNaveen N Rao ret = ftrace_validate_inst(ip - 4, ppc_inst(PPC_RAW_NOP()));
2380f71dcfbSNaveen N Rao if (!ret)
2390f71dcfbSNaveen N Rao ret = ftrace_validate_inst(ip, ppc_inst(PPC_RAW_NOP()));
2400f71dcfbSNaveen N Rao } else if (IS_ENABLED(CONFIG_PPC32)) {
241cc93b923SNaveen N Rao /* Expected sequence: 'mflr r0', 'stw r0,4(r1)', 'bl _mcount' */
242cc93b923SNaveen N Rao ret = ftrace_validate_inst(ip - 8, ppc_inst(PPC_RAW_MFLR(_R0)));
243cc93b923SNaveen N Rao if (!ret)
244cc93b923SNaveen N Rao ret = ftrace_validate_inst(ip - 4, ppc_inst(PPC_RAW_STW(_R0, _R1, 4)));
245cc93b923SNaveen N Rao } else if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) {
246cc93b923SNaveen N Rao /* Expected sequence: 'mflr r0', ['std r0,16(r1)'], 'bl _mcount' */
247cc93b923SNaveen N Rao ret = ftrace_read_inst(ip - 4, &old);
248cc93b923SNaveen N Rao if (!ret && !ppc_inst_equal(old, ppc_inst(PPC_RAW_MFLR(_R0)))) {
249cc93b923SNaveen N Rao ret = ftrace_validate_inst(ip - 8, ppc_inst(PPC_RAW_MFLR(_R0)));
250cc93b923SNaveen N Rao ret |= ftrace_validate_inst(ip - 4, ppc_inst(PPC_RAW_STD(_R0, _R1, 16)));
251cc93b923SNaveen N Rao }
252cc93b923SNaveen N Rao } else {
253cc93b923SNaveen N Rao return -EINVAL;
254cc93b923SNaveen N Rao }
255cc93b923SNaveen N Rao
256cc93b923SNaveen N Rao if (ret)
257cc93b923SNaveen N Rao return ret;
258cc93b923SNaveen N Rao
259cc93b923SNaveen N Rao if (!core_kernel_text(ip)) {
260cc93b923SNaveen N Rao if (!mod) {
261cc93b923SNaveen N Rao pr_err("0x%lx: No module provided for non-kernel address\n", ip);
262cc93b923SNaveen N Rao return -EFAULT;
263cc93b923SNaveen N Rao }
264cc93b923SNaveen N Rao rec->arch.mod = mod;
265cc93b923SNaveen N Rao }
266cc93b923SNaveen N Rao
267cc93b923SNaveen N Rao /* Nop-out the ftrace location */
268cc93b923SNaveen N Rao new = ppc_inst(PPC_RAW_NOP());
269cc93b923SNaveen N Rao addr = MCOUNT_ADDR;
2700f71dcfbSNaveen N Rao if (IS_ENABLED(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY)) {
2710f71dcfbSNaveen N Rao /* we instead patch-in the 'mflr r0' */
2720f71dcfbSNaveen N Rao old = ppc_inst(PPC_RAW_NOP());
2730f71dcfbSNaveen N Rao new = ppc_inst(PPC_RAW_MFLR(_R0));
2740f71dcfbSNaveen N Rao ret = ftrace_modify_code(ip - 4, old, new);
2750f71dcfbSNaveen N Rao } else if (is_offset_in_branch_range(addr - ip)) {
276cc93b923SNaveen N Rao /* Within range */
277cc93b923SNaveen N Rao old = ftrace_create_branch_inst(ip, addr, 1);
278cc93b923SNaveen N Rao ret = ftrace_modify_code(ip, old, new);
279cc93b923SNaveen N Rao } else if (core_kernel_text(ip) || (IS_ENABLED(CONFIG_MODULES) && mod)) {
280cc93b923SNaveen N Rao /*
281cc93b923SNaveen N Rao * We would be branching to a linker-generated stub, or to the module _mcount
282cc93b923SNaveen N Rao * stub. Let's just confirm we have a 'bl' here.
283cc93b923SNaveen N Rao */
284cc93b923SNaveen N Rao ret = ftrace_read_inst(ip, &old);
285cc93b923SNaveen N Rao if (ret)
286cc93b923SNaveen N Rao return ret;
287cc93b923SNaveen N Rao if (!is_bl_op(old)) {
288cc93b923SNaveen N Rao pr_err("0x%lx: expected (bl) != found (%08lx)\n", ip, ppc_inst_as_ulong(old));
289cc93b923SNaveen N Rao return -EINVAL;
290cc93b923SNaveen N Rao }
291cc93b923SNaveen N Rao ret = patch_instruction((u32 *)ip, new);
292cc93b923SNaveen N Rao } else {
293cc93b923SNaveen N Rao return -EINVAL;
294cc93b923SNaveen N Rao }
295cc93b923SNaveen N Rao
296cc93b923SNaveen N Rao return ret;
297cc93b923SNaveen N Rao }
298cc93b923SNaveen N Rao
ftrace_update_ftrace_func(ftrace_func_t func)2997853f9c0SNaveen N. Rao int ftrace_update_ftrace_func(ftrace_func_t func)
3007853f9c0SNaveen N. Rao {
3017853f9c0SNaveen N. Rao unsigned long ip = (unsigned long)(&ftrace_call);
302c545b9f0SChristophe Leroy ppc_inst_t old, new;
3037853f9c0SNaveen N. Rao int ret;
3047853f9c0SNaveen N. Rao
30569d4d6e5SChristophe Leroy old = ppc_inst_read((u32 *)&ftrace_call);
306a26ce427SNaveen N Rao new = ftrace_create_branch_inst(ip, ppc_function_entry(func), 1);
3077853f9c0SNaveen N. Rao ret = ftrace_modify_code(ip, old, new);
3087853f9c0SNaveen N. Rao
309ae30cc05SNaveen N. Rao /* Also update the regs callback function */
310af8b9f35SChristophe Leroy if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && !ret) {
311ae30cc05SNaveen N. Rao ip = (unsigned long)(&ftrace_regs_call);
31269d4d6e5SChristophe Leroy old = ppc_inst_read((u32 *)&ftrace_regs_call);
313a26ce427SNaveen N Rao new = ftrace_create_branch_inst(ip, ppc_function_entry(func), 1);
314ae30cc05SNaveen N. Rao ret = ftrace_modify_code(ip, old, new);
315ae30cc05SNaveen N. Rao }
316ae30cc05SNaveen N. Rao
3177853f9c0SNaveen N. Rao return ret;
3187853f9c0SNaveen N. Rao }
3197853f9c0SNaveen N. Rao
3207853f9c0SNaveen N. Rao /*
3217853f9c0SNaveen N. Rao * Use the default ftrace_modify_all_code, but without
3227853f9c0SNaveen N. Rao * stop_machine().
3237853f9c0SNaveen N. Rao */
arch_ftrace_update_code(int command)3247853f9c0SNaveen N. Rao void arch_ftrace_update_code(int command)
3257853f9c0SNaveen N. Rao {
3267853f9c0SNaveen N. Rao ftrace_modify_all_code(command);
3277853f9c0SNaveen N. Rao }
3287853f9c0SNaveen N. Rao
ftrace_free_init_tramp(void)32984ade0a6SNaveen N. Rao void ftrace_free_init_tramp(void)
33084ade0a6SNaveen N. Rao {
33184ade0a6SNaveen N. Rao int i;
33284ade0a6SNaveen N. Rao
33384ade0a6SNaveen N. Rao for (i = 0; i < NUM_FTRACE_TRAMPS && ftrace_tramps[i]; i++)
33484ade0a6SNaveen N. Rao if (ftrace_tramps[i] == (unsigned long)ftrace_tramp_init) {
33584ade0a6SNaveen N. Rao ftrace_tramps[i] = 0;
33684ade0a6SNaveen N. Rao return;
33784ade0a6SNaveen N. Rao }
33884ade0a6SNaveen N. Rao }
33984ade0a6SNaveen N. Rao
add_ftrace_tramp(unsigned long tramp)34033bb8a0bSNaveen N Rao static void __init add_ftrace_tramp(unsigned long tramp)
34133bb8a0bSNaveen N Rao {
34233bb8a0bSNaveen N Rao int i;
34333bb8a0bSNaveen N Rao
34433bb8a0bSNaveen N Rao for (i = 0; i < NUM_FTRACE_TRAMPS; i++)
34533bb8a0bSNaveen N Rao if (!ftrace_tramps[i]) {
34633bb8a0bSNaveen N Rao ftrace_tramps[i] = tramp;
34733bb8a0bSNaveen N Rao return;
34833bb8a0bSNaveen N Rao }
34933bb8a0bSNaveen N Rao }
35033bb8a0bSNaveen N Rao
ftrace_dyn_arch_init(void)35167361cf8SNaveen N. Rao int __init ftrace_dyn_arch_init(void)
35267361cf8SNaveen N. Rao {
35367361cf8SNaveen N. Rao unsigned int *tramp[] = { ftrace_tramp_text, ftrace_tramp_init };
354f3993a03SNaveen N Rao unsigned long addr = FTRACE_REGS_ADDR;
355f3993a03SNaveen N Rao long reladdr;
356f3993a03SNaveen N Rao int i;
3577e3a68beSNicholas Piggin u32 stub_insns[] = {
358f3993a03SNaveen N Rao #ifdef CONFIG_PPC_KERNEL_PCREL
3597e3a68beSNicholas Piggin /* pla r12,addr */
3607e3a68beSNicholas Piggin PPC_PREFIX_MLS | __PPC_PRFX_R(1),
3617e3a68beSNicholas Piggin PPC_INST_PADDI | ___PPC_RT(_R12),
3627e3a68beSNicholas Piggin PPC_RAW_MTCTR(_R12),
3637e3a68beSNicholas Piggin PPC_RAW_BCTR()
364f3993a03SNaveen N Rao #elif defined(CONFIG_PPC64)
365f3993a03SNaveen N Rao PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)),
366e89aa642SChristophe Leroy PPC_RAW_ADDIS(_R12, _R12, 0),
367e89aa642SChristophe Leroy PPC_RAW_ADDI(_R12, _R12, 0),
368e89aa642SChristophe Leroy PPC_RAW_MTCTR(_R12),
369e89aa642SChristophe Leroy PPC_RAW_BCTR()
370f3993a03SNaveen N Rao #else
371f3993a03SNaveen N Rao PPC_RAW_LIS(_R12, 0),
372f3993a03SNaveen N Rao PPC_RAW_ADDI(_R12, _R12, 0),
373f3993a03SNaveen N Rao PPC_RAW_MTCTR(_R12),
374f3993a03SNaveen N Rao PPC_RAW_BCTR()
3757e3a68beSNicholas Piggin #endif
376f3993a03SNaveen N Rao };
377af8b9f35SChristophe Leroy
3787e3a68beSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) {
3797e3a68beSNicholas Piggin for (i = 0; i < 2; i++) {
3807e3a68beSNicholas Piggin reladdr = addr - (unsigned long)tramp[i];
3817e3a68beSNicholas Piggin
3827e3a68beSNicholas Piggin if (reladdr >= (long)SZ_8G || reladdr < -(long)SZ_8G) {
3837e3a68beSNicholas Piggin pr_err("Address of %ps out of range of pcrel address.\n",
3847e3a68beSNicholas Piggin (void *)addr);
3857e3a68beSNicholas Piggin return -1;
3867e3a68beSNicholas Piggin }
3877e3a68beSNicholas Piggin
3887e3a68beSNicholas Piggin memcpy(tramp[i], stub_insns, sizeof(stub_insns));
3897e3a68beSNicholas Piggin tramp[i][0] |= IMM_H18(reladdr);
3907e3a68beSNicholas Piggin tramp[i][1] |= IMM_L(reladdr);
3917e3a68beSNicholas Piggin add_ftrace_tramp((unsigned long)tramp[i]);
3927e3a68beSNicholas Piggin }
393f3993a03SNaveen N Rao } else if (IS_ENABLED(CONFIG_PPC64)) {
394af8b9f35SChristophe Leroy reladdr = addr - kernel_toc_addr();
39567361cf8SNaveen N. Rao
396f3993a03SNaveen N Rao if (reladdr >= (long)SZ_2G || reladdr < -(long long)SZ_2G) {
39767361cf8SNaveen N. Rao pr_err("Address of %ps out of range of kernel_toc.\n",
39867361cf8SNaveen N. Rao (void *)addr);
39967361cf8SNaveen N. Rao return -1;
40067361cf8SNaveen N. Rao }
40167361cf8SNaveen N. Rao
40267361cf8SNaveen N. Rao for (i = 0; i < 2; i++) {
40367361cf8SNaveen N. Rao memcpy(tramp[i], stub_insns, sizeof(stub_insns));
40467361cf8SNaveen N. Rao tramp[i][1] |= PPC_HA(reladdr);
40567361cf8SNaveen N. Rao tramp[i][2] |= PPC_LO(reladdr);
40667361cf8SNaveen N. Rao add_ftrace_tramp((unsigned long)tramp[i]);
40767361cf8SNaveen N. Rao }
408f3993a03SNaveen N Rao } else {
409f3993a03SNaveen N Rao for (i = 0; i < 2; i++) {
410f3993a03SNaveen N Rao memcpy(tramp[i], stub_insns, sizeof(stub_insns));
411f3993a03SNaveen N Rao tramp[i][0] |= PPC_HA(addr);
412f3993a03SNaveen N Rao tramp[i][1] |= PPC_LO(addr);
413f3993a03SNaveen N Rao add_ftrace_tramp((unsigned long)tramp[i]);
414f3993a03SNaveen N Rao }
4157e3a68beSNicholas Piggin }
41667361cf8SNaveen N. Rao
41767361cf8SNaveen N. Rao return 0;
41867361cf8SNaveen N. Rao }
4197853f9c0SNaveen N. Rao
4207853f9c0SNaveen N. Rao #ifdef CONFIG_FUNCTION_GRAPH_TRACER
ftrace_graph_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct ftrace_regs * fregs)42196d7a136SNaveen N Rao void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
42296d7a136SNaveen N Rao struct ftrace_ops *op, struct ftrace_regs *fregs)
4237853f9c0SNaveen N. Rao {
42496d7a136SNaveen N Rao unsigned long sp = fregs->regs.gpr[1];
425df45a557SChristophe Leroy int bit;
4267853f9c0SNaveen N. Rao
4277853f9c0SNaveen N. Rao if (unlikely(ftrace_graph_is_dead()))
4287853f9c0SNaveen N. Rao goto out;
4297853f9c0SNaveen N. Rao
4307853f9c0SNaveen N. Rao if (unlikely(atomic_read(¤t->tracing_graph_pause)))
4317853f9c0SNaveen N. Rao goto out;
4327853f9c0SNaveen N. Rao
43396d7a136SNaveen N Rao bit = ftrace_test_recursion_trylock(ip, parent_ip);
434df45a557SChristophe Leroy if (bit < 0)
435df45a557SChristophe Leroy goto out;
436df45a557SChristophe Leroy
43796d7a136SNaveen N Rao if (!function_graph_enter(parent_ip, ip, 0, (unsigned long *)sp))
43896d7a136SNaveen N Rao parent_ip = ppc_function_entry(return_to_handler);
439df45a557SChristophe Leroy
440df45a557SChristophe Leroy ftrace_test_recursion_unlock(bit);
4417853f9c0SNaveen N. Rao out:
44296d7a136SNaveen N Rao fregs->regs.link = parent_ip;
4437853f9c0SNaveen N. Rao }
4447853f9c0SNaveen N. Rao #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
445