// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2008 Matt Fleming <[email protected]>3* Copyright (C) 2008 Paul Mundt <[email protected]>4*5* Code for replacing ftrace calls with jumps.6*7* Copyright (C) 2007-2008 Steven Rostedt <[email protected]>8*9* Thanks goes to Ingo Molnar, for suggesting the idea.10* Mathieu Desnoyers, for suggesting postponing the modifications.11* Arjan van de Ven, for keeping me straight, and explaining to me12* the dangers of modifying code on the run.13*/14#include <linux/uaccess.h>15#include <linux/ftrace.h>16#include <linux/string.h>17#include <linux/init.h>18#include <linux/io.h>19#include <linux/kernel.h>20#include <asm/ftrace.h>21#include <asm/cacheflush.h>22#include <asm/unistd.h>23#include <trace/syscall.h>2425#ifdef CONFIG_DYNAMIC_FTRACE26static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];2728static unsigned char ftrace_nop[4];29/*30* If we're trying to nop out a call to a function, we instead31* place a call to the address after the memory table.32*33* 8c011060 <a>:34* 8c011060: 02 d1 mov.l 8c01106c <a+0xc>,r135* 8c011062: 22 4f sts.l pr,@-r1536* 8c011064: 02 c7 mova 8c011070 <a+0x10>,r037* 8c011066: 2b 41 jmp @r138* 8c011068: 2a 40 lds r0,pr39* 8c01106a: 09 00 nop40* 8c01106c: 68 24 .word 0x2468 <--- ip41* 8c01106e: 1d 8c .word 0x8c1d42* 8c011070: 26 4f lds.l @r15+,pr <--- ip + MCOUNT_INSN_SIZE43*44* We write 0x8c011070 to 0x8c01106c so that on entry to a() we branch45* past the _mcount call and continue executing code like normal.46*/47static unsigned char *ftrace_nop_replace(unsigned long ip)48{49__raw_writel(ip + MCOUNT_INSN_SIZE, ftrace_nop);50return ftrace_nop;51}5253static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)54{55/* Place the address in the memory table. */56__raw_writel(addr, ftrace_replaced_code);5758/*59* No locking needed, this must be called via kstop_machine60* which in essence is like running on a uniprocessor machine.61*/62return ftrace_replaced_code;63}6465/*66* Modifying code must take extra care. On an SMP machine, if67* the code being modified is also being executed on another CPU68* that CPU will have undefined results and possibly take a GPF.69* We use kstop_machine to stop other CPUS from executing code.70* But this does not stop NMIs from happening. We still need71* to protect against that. We separate out the modification of72* the code to take care of this.73*74* Two buffers are added: An IP buffer and a "code" buffer.75*76* 1) Put the instruction pointer into the IP buffer77* and the new code into the "code" buffer.78* 2) Wait for any running NMIs to finish and set a flag that says79* we are modifying code, it is done in an atomic operation.80* 3) Write the code81* 4) clear the flag.82* 5) Wait for any running NMIs to finish.83*84* If an NMI is executed, the first thing it does is to call85* "ftrace_nmi_enter". This will check if the flag is set to write86* and if it is, it will write what is in the IP and "code" buffers.87*88* The trick is, it does not matter if everyone is writing the same89* content to the code location. Also, if a CPU is executing code90* it is OK to write to that code location if the contents being written91* are the same as what exists.92*/93#define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */94static atomic_t nmi_running = ATOMIC_INIT(0);95static int mod_code_status; /* holds return value of text write */96static void *mod_code_ip; /* holds the IP to write to */97static void *mod_code_newcode; /* holds the text to write to the IP */9899static void clear_mod_flag(void)100{101int old = atomic_read(&nmi_running);102103for (;;) {104int new = old & ~MOD_CODE_WRITE_FLAG;105106if (old == new)107break;108109old = atomic_cmpxchg(&nmi_running, old, new);110}111}112113static void ftrace_mod_code(void)114{115/*116* Yes, more than one CPU process can be writing to mod_code_status.117* (and the code itself)118* But if one were to fail, then they all should, and if one were119* to succeed, then they all should.120*/121mod_code_status = copy_to_kernel_nofault(mod_code_ip, mod_code_newcode,122MCOUNT_INSN_SIZE);123124/* if we fail, then kill any new writers */125if (mod_code_status)126clear_mod_flag();127}128129void arch_ftrace_nmi_enter(void)130{131if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {132smp_rmb();133ftrace_mod_code();134}135/* Must have previous changes seen before executions */136smp_mb();137}138139void arch_ftrace_nmi_exit(void)140{141/* Finish all executions before clearing nmi_running */142smp_mb();143atomic_dec(&nmi_running);144}145146static void wait_for_nmi_and_set_mod_flag(void)147{148if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))149return;150151do {152cpu_relax();153} while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));154}155156static void wait_for_nmi(void)157{158if (!atomic_read(&nmi_running))159return;160161do {162cpu_relax();163} while (atomic_read(&nmi_running));164}165166static int167do_ftrace_mod_code(unsigned long ip, void *new_code)168{169mod_code_ip = (void *)ip;170mod_code_newcode = new_code;171172/* The buffers need to be visible before we let NMIs write them */173smp_mb();174175wait_for_nmi_and_set_mod_flag();176177/* Make sure all running NMIs have finished before we write the code */178smp_mb();179180ftrace_mod_code();181182/* Make sure the write happens before clearing the bit */183smp_mb();184185clear_mod_flag();186wait_for_nmi();187188return mod_code_status;189}190191static int ftrace_modify_code(unsigned long ip, unsigned char *old_code,192unsigned char *new_code)193{194unsigned char replaced[MCOUNT_INSN_SIZE];195196/*197* Note:198* We are paranoid about modifying text, as if a bug was to happen, it199* could cause us to read or write to someplace that could cause harm.200* Carefully read and modify the code with probe_kernel_*(), and make201* sure what we read is what we expected it to be before modifying it.202*/203204/* read the text we want to modify */205if (copy_from_kernel_nofault(replaced, (void *)ip, MCOUNT_INSN_SIZE))206return -EFAULT;207208/* Make sure it is what we expect it to be */209if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)210return -EINVAL;211212/* replace the text with the new text */213if (do_ftrace_mod_code(ip, new_code))214return -EPERM;215216flush_icache_range(ip, ip + MCOUNT_INSN_SIZE);217218return 0;219}220221int ftrace_update_ftrace_func(ftrace_func_t func)222{223unsigned long ip = (unsigned long)(&ftrace_call) + MCOUNT_INSN_OFFSET;224unsigned char old[MCOUNT_INSN_SIZE], *new;225226memcpy(old, (unsigned char *)ip, MCOUNT_INSN_SIZE);227new = ftrace_call_replace(ip, (unsigned long)func);228229return ftrace_modify_code(ip, old, new);230}231232int ftrace_make_nop(struct module *mod,233struct dyn_ftrace *rec, unsigned long addr)234{235unsigned char *new, *old;236unsigned long ip = rec->ip;237238old = ftrace_call_replace(ip, addr);239new = ftrace_nop_replace(ip);240241return ftrace_modify_code(rec->ip, old, new);242}243244int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)245{246unsigned char *new, *old;247unsigned long ip = rec->ip;248249old = ftrace_nop_replace(ip);250new = ftrace_call_replace(ip, addr);251252return ftrace_modify_code(rec->ip, old, new);253}254#endif /* CONFIG_DYNAMIC_FTRACE */255256#ifdef CONFIG_FUNCTION_GRAPH_TRACER257#ifdef CONFIG_DYNAMIC_FTRACE258extern void ftrace_graph_call(void);259260static int ftrace_mod(unsigned long ip, unsigned long old_addr,261unsigned long new_addr)262{263unsigned char code[MCOUNT_INSN_SIZE];264265if (copy_from_kernel_nofault(code, (void *)ip, MCOUNT_INSN_SIZE))266return -EFAULT;267268if (old_addr != __raw_readl((unsigned long *)code))269return -EINVAL;270271__raw_writel(new_addr, ip);272return 0;273}274275int ftrace_enable_ftrace_graph_caller(void)276{277unsigned long ip, old_addr, new_addr;278279ip = (unsigned long)(&ftrace_graph_call) + GRAPH_INSN_OFFSET;280old_addr = (unsigned long)(&skip_trace);281new_addr = (unsigned long)(&ftrace_graph_caller);282283return ftrace_mod(ip, old_addr, new_addr);284}285286int ftrace_disable_ftrace_graph_caller(void)287{288unsigned long ip, old_addr, new_addr;289290ip = (unsigned long)(&ftrace_graph_call) + GRAPH_INSN_OFFSET;291old_addr = (unsigned long)(&ftrace_graph_caller);292new_addr = (unsigned long)(&skip_trace);293294return ftrace_mod(ip, old_addr, new_addr);295}296#endif /* CONFIG_DYNAMIC_FTRACE */297298/*299* Hook the return address and push it in the stack of return addrs300* in the current thread info.301*302* This is the main routine for the function graph tracer. The function303* graph tracer essentially works like this:304*305* parent is the stack address containing self_addr's return address.306* We pull the real return address out of parent and store it in307* current's ret_stack. Then, we replace the return address on the stack308* with the address of return_to_handler. self_addr is the function that309* called mcount.310*311* When self_addr returns, it will jump to return_to_handler which calls312* ftrace_return_to_handler. ftrace_return_to_handler will pull the real313* return address off of current's ret_stack and jump to it.314*/315void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)316{317unsigned long old;318int faulted;319unsigned long return_hooker = (unsigned long)&return_to_handler;320321if (unlikely(ftrace_graph_is_dead()))322return;323324if (unlikely(atomic_read(¤t->tracing_graph_pause)))325return;326327/*328* Protect against fault, even if it shouldn't329* happen. This tool is too much intrusive to330* ignore such a protection.331*/332__asm__ __volatile__(333"1: \n\t"334"mov.l @%2, %0 \n\t"335"2: \n\t"336"mov.l %3, @%2 \n\t"337"mov #0, %1 \n\t"338"3: \n\t"339".section .fixup, \"ax\" \n\t"340"4: \n\t"341"mov.l 5f, %0 \n\t"342"jmp @%0 \n\t"343" mov #1, %1 \n\t"344".balign 4 \n\t"345"5: .long 3b \n\t"346".previous \n\t"347".section __ex_table,\"a\" \n\t"348".long 1b, 4b \n\t"349".long 2b, 4b \n\t"350".previous \n\t"351: "=&r" (old), "=r" (faulted)352: "r" (parent), "r" (return_hooker)353);354355if (unlikely(faulted)) {356ftrace_graph_stop();357WARN_ON(1);358return;359}360361if (function_graph_enter(old, self_addr, 0, NULL))362__raw_writel(old, parent);363}364#endif /* CONFIG_FUNCTION_GRAPH_TRACER */365366367