13d083395SSteven Rostedt /* 23d083395SSteven Rostedt * Code for replacing ftrace calls with jumps. 33d083395SSteven Rostedt * 43d083395SSteven Rostedt * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 53d083395SSteven Rostedt * 63d083395SSteven Rostedt * Thanks goes to Ingo Molnar, for suggesting the idea. 73d083395SSteven Rostedt * Mathieu Desnoyers, for suggesting postponing the modifications. 83d083395SSteven Rostedt * Arjan van de Ven, for keeping me straight, and explaining to me 93d083395SSteven Rostedt * the dangers of modifying code on the run. 103d083395SSteven Rostedt */ 113d083395SSteven Rostedt 123bb258bfSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 133bb258bfSJoe Perches 143d083395SSteven Rostedt #include <linux/spinlock.h> 153d083395SSteven Rostedt #include <linux/hardirq.h> 166f93fc07SSteven Rostedt #include <linux/uaccess.h> 173d083395SSteven Rostedt #include <linux/ftrace.h> 183d083395SSteven Rostedt #include <linux/percpu.h> 1919b3e967SIngo Molnar #include <linux/sched.h> 20f3bea491SSteven Rostedt (Red Hat) #include <linux/slab.h> 213d083395SSteven Rostedt #include <linux/init.h> 223d083395SSteven Rostedt #include <linux/list.h> 2384e1c6bbSmatthieu castet #include <linux/module.h> 243d083395SSteven Rostedt 2547788c58SFrederic Weisbecker #include <trace/syscall.h> 2647788c58SFrederic Weisbecker 2716239630SSteven Rostedt #include <asm/cacheflush.h> 2859a094c9SSteven Rostedt #include <asm/kprobes.h> 29395a59d0SAbhishek Sagar #include <asm/ftrace.h> 30732f3ca7SSteven Rostedt #include <asm/nops.h> 313d083395SSteven Rostedt 32caf4b323SFrederic Weisbecker #ifdef CONFIG_DYNAMIC_FTRACE 333d083395SSteven Rostedt 3416239630SSteven Rostedt int ftrace_arch_code_modify_prepare(void) 3516239630SSteven Rostedt { 3616239630SSteven Rostedt set_kernel_text_rw(); 3784e1c6bbSmatthieu castet set_all_modules_text_rw(); 3816239630SSteven Rostedt return 0; 3916239630SSteven Rostedt } 4016239630SSteven Rostedt 4116239630SSteven Rostedt int ftrace_arch_code_modify_post_process(void) 4216239630SSteven Rostedt { 4384e1c6bbSmatthieu castet set_all_modules_text_ro(); 4416239630SSteven Rostedt set_kernel_text_ro(); 4516239630SSteven Rostedt return 0; 4616239630SSteven Rostedt } 4716239630SSteven Rostedt 483d083395SSteven Rostedt union ftrace_code_union { 49395a59d0SAbhishek Sagar char code[MCOUNT_INSN_SIZE]; 503d083395SSteven Rostedt struct { 51*15d5b02cSSteven Rostedt (Red Hat) unsigned char e8; 523d083395SSteven Rostedt int offset; 533d083395SSteven Rostedt } __attribute__((packed)); 543d083395SSteven Rostedt }; 553d083395SSteven Rostedt 5615adc048SSteven Rostedt static int ftrace_calc_offset(long ip, long addr) 573c1720f0SSteven Rostedt { 583c1720f0SSteven Rostedt return (int)(addr - ip); 593d083395SSteven Rostedt } 603d083395SSteven Rostedt 6131e88909SSteven Rostedt static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) 623c1720f0SSteven Rostedt { 633c1720f0SSteven Rostedt static union ftrace_code_union calc; 643c1720f0SSteven Rostedt 653c1720f0SSteven Rostedt calc.e8 = 0xe8; 66395a59d0SAbhishek Sagar calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); 673c1720f0SSteven Rostedt 683c1720f0SSteven Rostedt /* 693c1720f0SSteven Rostedt * No locking needed, this must be called via kstop_machine 703c1720f0SSteven Rostedt * which in essence is like running on a uniprocessor machine. 713c1720f0SSteven Rostedt */ 723c1720f0SSteven Rostedt return calc.code; 733c1720f0SSteven Rostedt } 743c1720f0SSteven Rostedt 7555ca3cc1SSuresh Siddha static inline int 7655ca3cc1SSuresh Siddha within(unsigned long addr, unsigned long start, unsigned long end) 7755ca3cc1SSuresh Siddha { 7855ca3cc1SSuresh Siddha return addr >= start && addr < end; 7955ca3cc1SSuresh Siddha } 8055ca3cc1SSuresh Siddha 8187fbb2acSSteven Rostedt (Red Hat) static unsigned long text_ip_addr(unsigned long ip) 8217666f02SSteven Rostedt { 8355ca3cc1SSuresh Siddha /* 8455ca3cc1SSuresh Siddha * On x86_64, kernel text mappings are mapped read-only with 8555ca3cc1SSuresh Siddha * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead 8655ca3cc1SSuresh Siddha * of the kernel text mapping to modify the kernel text. 8755ca3cc1SSuresh Siddha * 8855ca3cc1SSuresh Siddha * For 32bit kernels, these mappings are same and we can use 8955ca3cc1SSuresh Siddha * kernel identity mapping to modify code. 9055ca3cc1SSuresh Siddha */ 9155ca3cc1SSuresh Siddha if (within(ip, (unsigned long)_text, (unsigned long)_etext)) 92217f155eSAlexander Duyck ip = (unsigned long)__va(__pa_symbol(ip)); 9355ca3cc1SSuresh Siddha 9487fbb2acSSteven Rostedt (Red Hat) return ip; 9517666f02SSteven Rostedt } 9617666f02SSteven Rostedt 97dc326fcaSH. Peter Anvin static const unsigned char *ftrace_nop_replace(void) 98caf4b323SFrederic Weisbecker { 99dc326fcaSH. Peter Anvin return ideal_nops[NOP_ATOMIC5]; 100caf4b323SFrederic Weisbecker } 101caf4b323SFrederic Weisbecker 10231e88909SSteven Rostedt static int 1038a4d0a68SSteven Rostedt ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code, 1040d098a7dSRakib Mullick unsigned const char *new_code) 1053d083395SSteven Rostedt { 1066f93fc07SSteven Rostedt unsigned char replaced[MCOUNT_INSN_SIZE]; 1073d083395SSteven Rostedt 1083d083395SSteven Rostedt /* 1093d083395SSteven Rostedt * Note: Due to modules and __init, code can 1103d083395SSteven Rostedt * disappear and change, we need to protect against faulting 11176aefee5SSteven Rostedt * as well as code changing. We do this by using the 112ab9a0918SSteven Rostedt * probe_kernel_* functions. 1133d083395SSteven Rostedt * 1143d083395SSteven Rostedt * No real locking needed, this code is run through 1156f93fc07SSteven Rostedt * kstop_machine, or before SMP starts. 1163d083395SSteven Rostedt */ 11776aefee5SSteven Rostedt 11876aefee5SSteven Rostedt /* read the text we want to modify */ 119ab9a0918SSteven Rostedt if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) 120593eb8a2SSteven Rostedt return -EFAULT; 1216f93fc07SSteven Rostedt 12276aefee5SSteven Rostedt /* Make sure it is what we expect it to be */ 1236f93fc07SSteven Rostedt if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0) 124593eb8a2SSteven Rostedt return -EINVAL; 1256f93fc07SSteven Rostedt 12687fbb2acSSteven Rostedt (Red Hat) ip = text_ip_addr(ip); 12787fbb2acSSteven Rostedt (Red Hat) 12876aefee5SSteven Rostedt /* replace the text with the new text */ 12987fbb2acSSteven Rostedt (Red Hat) if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE)) 130593eb8a2SSteven Rostedt return -EPERM; 1316f93fc07SSteven Rostedt 1323d083395SSteven Rostedt sync_core(); 1333d083395SSteven Rostedt 1346f93fc07SSteven Rostedt return 0; 1353d083395SSteven Rostedt } 1363d083395SSteven Rostedt 13731e88909SSteven Rostedt int ftrace_make_nop(struct module *mod, 13831e88909SSteven Rostedt struct dyn_ftrace *rec, unsigned long addr) 13931e88909SSteven Rostedt { 1400d098a7dSRakib Mullick unsigned const char *new, *old; 14131e88909SSteven Rostedt unsigned long ip = rec->ip; 14231e88909SSteven Rostedt 14331e88909SSteven Rostedt old = ftrace_call_replace(ip, addr); 14431e88909SSteven Rostedt new = ftrace_nop_replace(); 14531e88909SSteven Rostedt 1468a4d0a68SSteven Rostedt /* 1478a4d0a68SSteven Rostedt * On boot up, and when modules are loaded, the MCOUNT_ADDR 1488a4d0a68SSteven Rostedt * is converted to a nop, and will never become MCOUNT_ADDR 1498a4d0a68SSteven Rostedt * again. This code is either running before SMP (on boot up) 1508a4d0a68SSteven Rostedt * or before the code will ever be executed (module load). 1518a4d0a68SSteven Rostedt * We do not want to use the breakpoint version in this case, 1528a4d0a68SSteven Rostedt * just modify the code directly. 1538a4d0a68SSteven Rostedt */ 1548a4d0a68SSteven Rostedt if (addr == MCOUNT_ADDR) 1558a4d0a68SSteven Rostedt return ftrace_modify_code_direct(rec->ip, old, new); 1568a4d0a68SSteven Rostedt 1578a4d0a68SSteven Rostedt /* Normal cases use add_brk_on_nop */ 1588a4d0a68SSteven Rostedt WARN_ONCE(1, "invalid use of ftrace_make_nop"); 1598a4d0a68SSteven Rostedt return -EINVAL; 16031e88909SSteven Rostedt } 16131e88909SSteven Rostedt 16231e88909SSteven Rostedt int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) 16331e88909SSteven Rostedt { 1640d098a7dSRakib Mullick unsigned const char *new, *old; 16531e88909SSteven Rostedt unsigned long ip = rec->ip; 16631e88909SSteven Rostedt 16731e88909SSteven Rostedt old = ftrace_nop_replace(); 16831e88909SSteven Rostedt new = ftrace_call_replace(ip, addr); 16931e88909SSteven Rostedt 1708a4d0a68SSteven Rostedt /* Should only be called when module is loaded */ 1718a4d0a68SSteven Rostedt return ftrace_modify_code_direct(rec->ip, old, new); 172d61f82d0SSteven Rostedt } 173d61f82d0SSteven Rostedt 174a192cd04SSteven Rostedt /* 175a192cd04SSteven Rostedt * The modifying_ftrace_code is used to tell the breakpoint 176a192cd04SSteven Rostedt * handler to call ftrace_int3_handler(). If it fails to 177a192cd04SSteven Rostedt * call this handler for a breakpoint added by ftrace, then 178a192cd04SSteven Rostedt * the kernel may crash. 179a192cd04SSteven Rostedt * 180a192cd04SSteven Rostedt * As atomic_writes on x86 do not need a barrier, we do not 181a192cd04SSteven Rostedt * need to add smp_mb()s for this to work. It is also considered 182a192cd04SSteven Rostedt * that we can not read the modifying_ftrace_code before 183a192cd04SSteven Rostedt * executing the breakpoint. That would be quite remarkable if 184a192cd04SSteven Rostedt * it could do that. Here's the flow that is required: 185a192cd04SSteven Rostedt * 186a192cd04SSteven Rostedt * CPU-0 CPU-1 187a192cd04SSteven Rostedt * 188a192cd04SSteven Rostedt * atomic_inc(mfc); 189a192cd04SSteven Rostedt * write int3s 190a192cd04SSteven Rostedt * <trap-int3> // implicit (r)mb 191a192cd04SSteven Rostedt * if (atomic_read(mfc)) 192a192cd04SSteven Rostedt * call ftrace_int3_handler() 193a192cd04SSteven Rostedt * 194a192cd04SSteven Rostedt * Then when we are finished: 195a192cd04SSteven Rostedt * 196a192cd04SSteven Rostedt * atomic_dec(mfc); 197a192cd04SSteven Rostedt * 198a192cd04SSteven Rostedt * If we hit a breakpoint that was not set by ftrace, it does not 199a192cd04SSteven Rostedt * matter if ftrace_int3_handler() is called or not. It will 200a192cd04SSteven Rostedt * simply be ignored. But it is crucial that a ftrace nop/caller 201a192cd04SSteven Rostedt * breakpoint is handled. No other user should ever place a 202a192cd04SSteven Rostedt * breakpoint on an ftrace nop/caller location. It must only 203a192cd04SSteven Rostedt * be done by this code. 204a192cd04SSteven Rostedt */ 205a192cd04SSteven Rostedt atomic_t modifying_ftrace_code __read_mostly; 20608d636b6SSteven Rostedt 2078a4d0a68SSteven Rostedt static int 2088a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code, 2098a4d0a68SSteven Rostedt unsigned const char *new_code); 2108a4d0a68SSteven Rostedt 21108f6fba5SSteven Rostedt /* 21208f6fba5SSteven Rostedt * Should never be called: 21308f6fba5SSteven Rostedt * As it is only called by __ftrace_replace_code() which is called by 21408f6fba5SSteven Rostedt * ftrace_replace_code() that x86 overrides, and by ftrace_update_code() 21508f6fba5SSteven Rostedt * which is called to turn mcount into nops or nops into function calls 21608f6fba5SSteven Rostedt * but not to convert a function from not using regs to one that uses 21708f6fba5SSteven Rostedt * regs, which ftrace_modify_call() is for. 21808f6fba5SSteven Rostedt */ 21908f6fba5SSteven Rostedt int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, 22008f6fba5SSteven Rostedt unsigned long addr) 22108f6fba5SSteven Rostedt { 22208f6fba5SSteven Rostedt WARN_ON(1); 22308f6fba5SSteven Rostedt return -EINVAL; 22408f6fba5SSteven Rostedt } 22508f6fba5SSteven Rostedt 22687fbb2acSSteven Rostedt (Red Hat) static unsigned long ftrace_update_func; 22787fbb2acSSteven Rostedt (Red Hat) 22887fbb2acSSteven Rostedt (Red Hat) static int update_ftrace_func(unsigned long ip, void *new) 2298a4d0a68SSteven Rostedt { 23087fbb2acSSteven Rostedt (Red Hat) unsigned char old[MCOUNT_INSN_SIZE]; 2318a4d0a68SSteven Rostedt int ret; 2328a4d0a68SSteven Rostedt 23387fbb2acSSteven Rostedt (Red Hat) memcpy(old, (void *)ip, MCOUNT_INSN_SIZE); 23487fbb2acSSteven Rostedt (Red Hat) 23587fbb2acSSteven Rostedt (Red Hat) ftrace_update_func = ip; 23687fbb2acSSteven Rostedt (Red Hat) /* Make sure the breakpoints see the ftrace_update_func update */ 23787fbb2acSSteven Rostedt (Red Hat) smp_wmb(); 2388a4d0a68SSteven Rostedt 2398a4d0a68SSteven Rostedt /* See comment above by declaration of modifying_ftrace_code */ 2408a4d0a68SSteven Rostedt atomic_inc(&modifying_ftrace_code); 2418a4d0a68SSteven Rostedt 2428a4d0a68SSteven Rostedt ret = ftrace_modify_code(ip, old, new); 2438a4d0a68SSteven Rostedt 24487fbb2acSSteven Rostedt (Red Hat) atomic_dec(&modifying_ftrace_code); 24587fbb2acSSteven Rostedt (Red Hat) 24687fbb2acSSteven Rostedt (Red Hat) return ret; 24787fbb2acSSteven Rostedt (Red Hat) } 24887fbb2acSSteven Rostedt (Red Hat) 24987fbb2acSSteven Rostedt (Red Hat) int ftrace_update_ftrace_func(ftrace_func_t func) 25087fbb2acSSteven Rostedt (Red Hat) { 25187fbb2acSSteven Rostedt (Red Hat) unsigned long ip = (unsigned long)(&ftrace_call); 25287fbb2acSSteven Rostedt (Red Hat) unsigned char *new; 25387fbb2acSSteven Rostedt (Red Hat) int ret; 25487fbb2acSSteven Rostedt (Red Hat) 25587fbb2acSSteven Rostedt (Red Hat) new = ftrace_call_replace(ip, (unsigned long)func); 25687fbb2acSSteven Rostedt (Red Hat) ret = update_ftrace_func(ip, new); 25787fbb2acSSteven Rostedt (Red Hat) 25808f6fba5SSteven Rostedt /* Also update the regs callback function */ 25908f6fba5SSteven Rostedt if (!ret) { 26008f6fba5SSteven Rostedt ip = (unsigned long)(&ftrace_regs_call); 26108f6fba5SSteven Rostedt new = ftrace_call_replace(ip, (unsigned long)func); 26287fbb2acSSteven Rostedt (Red Hat) ret = update_ftrace_func(ip, new); 26308f6fba5SSteven Rostedt } 26408f6fba5SSteven Rostedt 2658a4d0a68SSteven Rostedt return ret; 2668a4d0a68SSteven Rostedt } 2678a4d0a68SSteven Rostedt 268ab4ead02SKevin Hao static int is_ftrace_caller(unsigned long ip) 269ab4ead02SKevin Hao { 27087fbb2acSSteven Rostedt (Red Hat) if (ip == ftrace_update_func) 271ab4ead02SKevin Hao return 1; 272ab4ead02SKevin Hao 273ab4ead02SKevin Hao return 0; 274ab4ead02SKevin Hao } 275ab4ead02SKevin Hao 27608d636b6SSteven Rostedt /* 27708d636b6SSteven Rostedt * A breakpoint was added to the code address we are about to 27808d636b6SSteven Rostedt * modify, and this is the handle that will just skip over it. 27908d636b6SSteven Rostedt * We are either changing a nop into a trace call, or a trace 28008d636b6SSteven Rostedt * call to a nop. While the change is taking place, we treat 28108d636b6SSteven Rostedt * it just like it was a nop. 28208d636b6SSteven Rostedt */ 28308d636b6SSteven Rostedt int ftrace_int3_handler(struct pt_regs *regs) 28408d636b6SSteven Rostedt { 285ab4ead02SKevin Hao unsigned long ip; 286ab4ead02SKevin Hao 28708d636b6SSteven Rostedt if (WARN_ON_ONCE(!regs)) 28808d636b6SSteven Rostedt return 0; 28908d636b6SSteven Rostedt 290ab4ead02SKevin Hao ip = regs->ip - 1; 291ab4ead02SKevin Hao if (!ftrace_location(ip) && !is_ftrace_caller(ip)) 29208d636b6SSteven Rostedt return 0; 29308d636b6SSteven Rostedt 29408d636b6SSteven Rostedt regs->ip += MCOUNT_INSN_SIZE - 1; 29508d636b6SSteven Rostedt 29608d636b6SSteven Rostedt return 1; 29708d636b6SSteven Rostedt } 29808d636b6SSteven Rostedt 29908d636b6SSteven Rostedt static int ftrace_write(unsigned long ip, const char *val, int size) 30008d636b6SSteven Rostedt { 301964f7b6bSPetr Mladek ip = text_ip_addr(ip); 30208d636b6SSteven Rostedt 30392550405SSteven Rostedt (Red Hat) if (probe_kernel_write((void *)ip, val, size)) 30492550405SSteven Rostedt (Red Hat) return -EPERM; 30592550405SSteven Rostedt (Red Hat) 30692550405SSteven Rostedt (Red Hat) return 0; 30708d636b6SSteven Rostedt } 30808d636b6SSteven Rostedt 30908d636b6SSteven Rostedt static int add_break(unsigned long ip, const char *old) 31008d636b6SSteven Rostedt { 31108d636b6SSteven Rostedt unsigned char replaced[MCOUNT_INSN_SIZE]; 31208d636b6SSteven Rostedt unsigned char brk = BREAKPOINT_INSTRUCTION; 31308d636b6SSteven Rostedt 31408d636b6SSteven Rostedt if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) 31508d636b6SSteven Rostedt return -EFAULT; 31608d636b6SSteven Rostedt 31708d636b6SSteven Rostedt /* Make sure it is what we expect it to be */ 31808d636b6SSteven Rostedt if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0) 31908d636b6SSteven Rostedt return -EINVAL; 32008d636b6SSteven Rostedt 32192550405SSteven Rostedt (Red Hat) return ftrace_write(ip, &brk, 1); 32208d636b6SSteven Rostedt } 32308d636b6SSteven Rostedt 32408d636b6SSteven Rostedt static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr) 32508d636b6SSteven Rostedt { 32608d636b6SSteven Rostedt unsigned const char *old; 32708d636b6SSteven Rostedt unsigned long ip = rec->ip; 32808d636b6SSteven Rostedt 32908d636b6SSteven Rostedt old = ftrace_call_replace(ip, addr); 33008d636b6SSteven Rostedt 33108d636b6SSteven Rostedt return add_break(rec->ip, old); 33208d636b6SSteven Rostedt } 33308d636b6SSteven Rostedt 33408d636b6SSteven Rostedt 33508d636b6SSteven Rostedt static int add_brk_on_nop(struct dyn_ftrace *rec) 33608d636b6SSteven Rostedt { 33708d636b6SSteven Rostedt unsigned const char *old; 33808d636b6SSteven Rostedt 33908d636b6SSteven Rostedt old = ftrace_nop_replace(); 34008d636b6SSteven Rostedt 34108d636b6SSteven Rostedt return add_break(rec->ip, old); 34208d636b6SSteven Rostedt } 34308d636b6SSteven Rostedt 34408d636b6SSteven Rostedt static int add_breakpoints(struct dyn_ftrace *rec, int enable) 34508d636b6SSteven Rostedt { 34608d636b6SSteven Rostedt unsigned long ftrace_addr; 34708d636b6SSteven Rostedt int ret; 34808d636b6SSteven Rostedt 3497413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_curr(rec); 35008d636b6SSteven Rostedt 35194792ea0SSteven Rostedt (Red Hat) ret = ftrace_test_record(rec, enable); 35208d636b6SSteven Rostedt 35308d636b6SSteven Rostedt switch (ret) { 35408d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 35508d636b6SSteven Rostedt return 0; 35608d636b6SSteven Rostedt 35708d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 35808d636b6SSteven Rostedt /* converting nop to call */ 35908d636b6SSteven Rostedt return add_brk_on_nop(rec); 36008d636b6SSteven Rostedt 36108f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 36208d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 36308d636b6SSteven Rostedt /* converting a call to a nop */ 36408d636b6SSteven Rostedt return add_brk_on_call(rec, ftrace_addr); 36508d636b6SSteven Rostedt } 36608d636b6SSteven Rostedt return 0; 36708d636b6SSteven Rostedt } 36808d636b6SSteven Rostedt 36908d636b6SSteven Rostedt /* 37008d636b6SSteven Rostedt * On error, we need to remove breakpoints. This needs to 37108d636b6SSteven Rostedt * be done caefully. If the address does not currently have a 37208d636b6SSteven Rostedt * breakpoint, we know we are done. Otherwise, we look at the 37308d636b6SSteven Rostedt * remaining 4 bytes of the instruction. If it matches a nop 37408d636b6SSteven Rostedt * we replace the breakpoint with the nop. Otherwise we replace 37508d636b6SSteven Rostedt * it with the call instruction. 37608d636b6SSteven Rostedt */ 37708d636b6SSteven Rostedt static int remove_breakpoint(struct dyn_ftrace *rec) 37808d636b6SSteven Rostedt { 37908d636b6SSteven Rostedt unsigned char ins[MCOUNT_INSN_SIZE]; 38008d636b6SSteven Rostedt unsigned char brk = BREAKPOINT_INSTRUCTION; 38108d636b6SSteven Rostedt const unsigned char *nop; 38208d636b6SSteven Rostedt unsigned long ftrace_addr; 38308d636b6SSteven Rostedt unsigned long ip = rec->ip; 38408d636b6SSteven Rostedt 38508d636b6SSteven Rostedt /* If we fail the read, just give up */ 38608d636b6SSteven Rostedt if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE)) 38708d636b6SSteven Rostedt return -EFAULT; 38808d636b6SSteven Rostedt 38908d636b6SSteven Rostedt /* If this does not have a breakpoint, we are done */ 39008d636b6SSteven Rostedt if (ins[0] != brk) 3917f11f5ecSPetr Mladek return 0; 39208d636b6SSteven Rostedt 39308d636b6SSteven Rostedt nop = ftrace_nop_replace(); 39408d636b6SSteven Rostedt 39508d636b6SSteven Rostedt /* 39608d636b6SSteven Rostedt * If the last 4 bytes of the instruction do not match 39708d636b6SSteven Rostedt * a nop, then we assume that this is a call to ftrace_addr. 39808d636b6SSteven Rostedt */ 39908d636b6SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) { 40008d636b6SSteven Rostedt /* 40108d636b6SSteven Rostedt * For extra paranoidism, we check if the breakpoint is on 40208d636b6SSteven Rostedt * a call that would actually jump to the ftrace_addr. 40308d636b6SSteven Rostedt * If not, don't touch the breakpoint, we make just create 40408d636b6SSteven Rostedt * a disaster. 40508d636b6SSteven Rostedt */ 4067413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_new(rec); 40708f6fba5SSteven Rostedt nop = ftrace_call_replace(ip, ftrace_addr); 40808f6fba5SSteven Rostedt 40908f6fba5SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0) 41008f6fba5SSteven Rostedt goto update; 41108f6fba5SSteven Rostedt 41208f6fba5SSteven Rostedt /* Check both ftrace_addr and ftrace_old_addr */ 4137413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_curr(rec); 41408d636b6SSteven Rostedt nop = ftrace_call_replace(ip, ftrace_addr); 41508d636b6SSteven Rostedt 41608d636b6SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) 41708d636b6SSteven Rostedt return -EINVAL; 41808d636b6SSteven Rostedt } 41908d636b6SSteven Rostedt 42008f6fba5SSteven Rostedt update: 421c932c6b7SSteven Rostedt (Red Hat) return ftrace_write(ip, nop, 1); 42208d636b6SSteven Rostedt } 42308d636b6SSteven Rostedt 42408d636b6SSteven Rostedt static int add_update_code(unsigned long ip, unsigned const char *new) 42508d636b6SSteven Rostedt { 42608d636b6SSteven Rostedt /* skip breakpoint */ 42708d636b6SSteven Rostedt ip++; 42808d636b6SSteven Rostedt new++; 42992550405SSteven Rostedt (Red Hat) return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1); 43008d636b6SSteven Rostedt } 43108d636b6SSteven Rostedt 43208d636b6SSteven Rostedt static int add_update_call(struct dyn_ftrace *rec, unsigned long addr) 43308d636b6SSteven Rostedt { 43408d636b6SSteven Rostedt unsigned long ip = rec->ip; 43508d636b6SSteven Rostedt unsigned const char *new; 43608d636b6SSteven Rostedt 43708d636b6SSteven Rostedt new = ftrace_call_replace(ip, addr); 43808d636b6SSteven Rostedt return add_update_code(ip, new); 43908d636b6SSteven Rostedt } 44008d636b6SSteven Rostedt 44108d636b6SSteven Rostedt static int add_update_nop(struct dyn_ftrace *rec) 44208d636b6SSteven Rostedt { 44308d636b6SSteven Rostedt unsigned long ip = rec->ip; 44408d636b6SSteven Rostedt unsigned const char *new; 44508d636b6SSteven Rostedt 44608d636b6SSteven Rostedt new = ftrace_nop_replace(); 44708d636b6SSteven Rostedt return add_update_code(ip, new); 44808d636b6SSteven Rostedt } 44908d636b6SSteven Rostedt 45008d636b6SSteven Rostedt static int add_update(struct dyn_ftrace *rec, int enable) 45108d636b6SSteven Rostedt { 45208d636b6SSteven Rostedt unsigned long ftrace_addr; 45308d636b6SSteven Rostedt int ret; 45408d636b6SSteven Rostedt 45508d636b6SSteven Rostedt ret = ftrace_test_record(rec, enable); 45608d636b6SSteven Rostedt 4577413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_new(rec); 45808d636b6SSteven Rostedt 45908d636b6SSteven Rostedt switch (ret) { 46008d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 46108d636b6SSteven Rostedt return 0; 46208d636b6SSteven Rostedt 46308f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 46408d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 46508d636b6SSteven Rostedt /* converting nop to call */ 46608d636b6SSteven Rostedt return add_update_call(rec, ftrace_addr); 46708d636b6SSteven Rostedt 46808d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 46908d636b6SSteven Rostedt /* converting a call to a nop */ 47008d636b6SSteven Rostedt return add_update_nop(rec); 47108d636b6SSteven Rostedt } 47208d636b6SSteven Rostedt 47308d636b6SSteven Rostedt return 0; 47408d636b6SSteven Rostedt } 47508d636b6SSteven Rostedt 47608d636b6SSteven Rostedt static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr) 47708d636b6SSteven Rostedt { 47808d636b6SSteven Rostedt unsigned long ip = rec->ip; 47908d636b6SSteven Rostedt unsigned const char *new; 48008d636b6SSteven Rostedt 48108d636b6SSteven Rostedt new = ftrace_call_replace(ip, addr); 48208d636b6SSteven Rostedt 48392550405SSteven Rostedt (Red Hat) return ftrace_write(ip, new, 1); 48408d636b6SSteven Rostedt } 48508d636b6SSteven Rostedt 48608d636b6SSteven Rostedt static int finish_update_nop(struct dyn_ftrace *rec) 48708d636b6SSteven Rostedt { 48808d636b6SSteven Rostedt unsigned long ip = rec->ip; 48908d636b6SSteven Rostedt unsigned const char *new; 49008d636b6SSteven Rostedt 49108d636b6SSteven Rostedt new = ftrace_nop_replace(); 49208d636b6SSteven Rostedt 49392550405SSteven Rostedt (Red Hat) return ftrace_write(ip, new, 1); 49408d636b6SSteven Rostedt } 49508d636b6SSteven Rostedt 49608d636b6SSteven Rostedt static int finish_update(struct dyn_ftrace *rec, int enable) 49708d636b6SSteven Rostedt { 49808d636b6SSteven Rostedt unsigned long ftrace_addr; 49908d636b6SSteven Rostedt int ret; 50008d636b6SSteven Rostedt 50108d636b6SSteven Rostedt ret = ftrace_update_record(rec, enable); 50208d636b6SSteven Rostedt 5037413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_new(rec); 50408d636b6SSteven Rostedt 50508d636b6SSteven Rostedt switch (ret) { 50608d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 50708d636b6SSteven Rostedt return 0; 50808d636b6SSteven Rostedt 50908f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 51008d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 51108d636b6SSteven Rostedt /* converting nop to call */ 51208d636b6SSteven Rostedt return finish_update_call(rec, ftrace_addr); 51308d636b6SSteven Rostedt 51408d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 51508d636b6SSteven Rostedt /* converting a call to a nop */ 51608d636b6SSteven Rostedt return finish_update_nop(rec); 51708d636b6SSteven Rostedt } 51808d636b6SSteven Rostedt 51908d636b6SSteven Rostedt return 0; 52008d636b6SSteven Rostedt } 52108d636b6SSteven Rostedt 52208d636b6SSteven Rostedt static void do_sync_core(void *data) 52308d636b6SSteven Rostedt { 52408d636b6SSteven Rostedt sync_core(); 52508d636b6SSteven Rostedt } 52608d636b6SSteven Rostedt 52708d636b6SSteven Rostedt static void run_sync(void) 52808d636b6SSteven Rostedt { 52908d636b6SSteven Rostedt int enable_irqs = irqs_disabled(); 53008d636b6SSteven Rostedt 53108d636b6SSteven Rostedt /* We may be called with interrupts disbled (on bootup). */ 53208d636b6SSteven Rostedt if (enable_irqs) 53308d636b6SSteven Rostedt local_irq_enable(); 53408d636b6SSteven Rostedt on_each_cpu(do_sync_core, NULL, 1); 53508d636b6SSteven Rostedt if (enable_irqs) 53608d636b6SSteven Rostedt local_irq_disable(); 53708d636b6SSteven Rostedt } 53808d636b6SSteven Rostedt 539e4f5d544SSteven Rostedt void ftrace_replace_code(int enable) 54008d636b6SSteven Rostedt { 54108d636b6SSteven Rostedt struct ftrace_rec_iter *iter; 54208d636b6SSteven Rostedt struct dyn_ftrace *rec; 54308d636b6SSteven Rostedt const char *report = "adding breakpoints"; 54408d636b6SSteven Rostedt int count = 0; 54508d636b6SSteven Rostedt int ret; 54608d636b6SSteven Rostedt 54708d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 54808d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 54908d636b6SSteven Rostedt 55008d636b6SSteven Rostedt ret = add_breakpoints(rec, enable); 55108d636b6SSteven Rostedt if (ret) 55208d636b6SSteven Rostedt goto remove_breakpoints; 55308d636b6SSteven Rostedt count++; 55408d636b6SSteven Rostedt } 55508d636b6SSteven Rostedt 55608d636b6SSteven Rostedt run_sync(); 55708d636b6SSteven Rostedt 55808d636b6SSteven Rostedt report = "updating code"; 55908d636b6SSteven Rostedt 56008d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 56108d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 56208d636b6SSteven Rostedt 56308d636b6SSteven Rostedt ret = add_update(rec, enable); 56408d636b6SSteven Rostedt if (ret) 56508d636b6SSteven Rostedt goto remove_breakpoints; 56608d636b6SSteven Rostedt } 56708d636b6SSteven Rostedt 56808d636b6SSteven Rostedt run_sync(); 56908d636b6SSteven Rostedt 57008d636b6SSteven Rostedt report = "removing breakpoints"; 57108d636b6SSteven Rostedt 57208d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 57308d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 57408d636b6SSteven Rostedt 57508d636b6SSteven Rostedt ret = finish_update(rec, enable); 57608d636b6SSteven Rostedt if (ret) 57708d636b6SSteven Rostedt goto remove_breakpoints; 57808d636b6SSteven Rostedt } 57908d636b6SSteven Rostedt 58008d636b6SSteven Rostedt run_sync(); 58108d636b6SSteven Rostedt 58208d636b6SSteven Rostedt return; 58308d636b6SSteven Rostedt 58408d636b6SSteven Rostedt remove_breakpoints: 58574bb8c45SPetr Mladek pr_warn("Failed on %s (%d):\n", report, count); 58608d636b6SSteven Rostedt ftrace_bug(ret, rec ? rec->ip : 0); 58708d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 58808d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 5897f11f5ecSPetr Mladek /* 5907f11f5ecSPetr Mladek * Breakpoints are handled only when this function is in 5917f11f5ecSPetr Mladek * progress. The system could not work with them. 5927f11f5ecSPetr Mladek */ 5937f11f5ecSPetr Mladek if (remove_breakpoint(rec)) 5947f11f5ecSPetr Mladek BUG(); 59508d636b6SSteven Rostedt } 596c932c6b7SSteven Rostedt (Red Hat) run_sync(); 59708d636b6SSteven Rostedt } 59808d636b6SSteven Rostedt 5998a4d0a68SSteven Rostedt static int 6008a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code, 6018a4d0a68SSteven Rostedt unsigned const char *new_code) 6028a4d0a68SSteven Rostedt { 6038a4d0a68SSteven Rostedt int ret; 6048a4d0a68SSteven Rostedt 6058a4d0a68SSteven Rostedt ret = add_break(ip, old_code); 6068a4d0a68SSteven Rostedt if (ret) 6078a4d0a68SSteven Rostedt goto out; 6088a4d0a68SSteven Rostedt 6098a4d0a68SSteven Rostedt run_sync(); 6108a4d0a68SSteven Rostedt 6118a4d0a68SSteven Rostedt ret = add_update_code(ip, new_code); 6128a4d0a68SSteven Rostedt if (ret) 6138a4d0a68SSteven Rostedt goto fail_update; 6148a4d0a68SSteven Rostedt 6158a4d0a68SSteven Rostedt run_sync(); 6168a4d0a68SSteven Rostedt 6178a4d0a68SSteven Rostedt ret = ftrace_write(ip, new_code, 1); 6187f11f5ecSPetr Mladek /* 6197f11f5ecSPetr Mladek * The breakpoint is handled only when this function is in progress. 6207f11f5ecSPetr Mladek * The system could not work if we could not remove it. 6217f11f5ecSPetr Mladek */ 6227f11f5ecSPetr Mladek BUG_ON(ret); 6238a4d0a68SSteven Rostedt out: 62412729f14SPetr Mladek run_sync(); 6258a4d0a68SSteven Rostedt return ret; 6268a4d0a68SSteven Rostedt 6278a4d0a68SSteven Rostedt fail_update: 6287f11f5ecSPetr Mladek /* Also here the system could not work with the breakpoint */ 6297f11f5ecSPetr Mladek if (ftrace_write(ip, old_code, 1)) 6307f11f5ecSPetr Mladek BUG(); 6318a4d0a68SSteven Rostedt goto out; 6328a4d0a68SSteven Rostedt } 6338a4d0a68SSteven Rostedt 63408d636b6SSteven Rostedt void arch_ftrace_update_code(int command) 63508d636b6SSteven Rostedt { 636a192cd04SSteven Rostedt /* See comment above by declaration of modifying_ftrace_code */ 637a192cd04SSteven Rostedt atomic_inc(&modifying_ftrace_code); 63808d636b6SSteven Rostedt 639e4f5d544SSteven Rostedt ftrace_modify_all_code(command); 64008d636b6SSteven Rostedt 641a192cd04SSteven Rostedt atomic_dec(&modifying_ftrace_code); 64208d636b6SSteven Rostedt } 64308d636b6SSteven Rostedt 6443a36cb11SJiri Slaby int __init ftrace_dyn_arch_init(void) 6453d083395SSteven Rostedt { 6463d083395SSteven Rostedt return 0; 6473d083395SSteven Rostedt } 648e7d3737eSFrederic Weisbecker 649f3bea491SSteven Rostedt (Red Hat) #if defined(CONFIG_X86_64) || defined(CONFIG_FUNCTION_GRAPH_TRACER) 65087fbb2acSSteven Rostedt (Red Hat) static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr) 6515a45cfe1SSteven Rostedt { 65287fbb2acSSteven Rostedt (Red Hat) static union ftrace_code_union calc; 6535a45cfe1SSteven Rostedt 65487fbb2acSSteven Rostedt (Red Hat) /* Jmp not a call (ignore the .e8) */ 65587fbb2acSSteven Rostedt (Red Hat) calc.e8 = 0xe9; 65687fbb2acSSteven Rostedt (Red Hat) calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); 6575a45cfe1SSteven Rostedt 65887fbb2acSSteven Rostedt (Red Hat) /* 65987fbb2acSSteven Rostedt (Red Hat) * ftrace external locks synchronize the access to the static variable. 66087fbb2acSSteven Rostedt (Red Hat) */ 66187fbb2acSSteven Rostedt (Red Hat) return calc.code; 66287fbb2acSSteven Rostedt (Red Hat) } 663f3bea491SSteven Rostedt (Red Hat) #endif 664f3bea491SSteven Rostedt (Red Hat) 665f3bea491SSteven Rostedt (Red Hat) /* Currently only x86_64 supports dynamic trampolines */ 666f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_X86_64 667f3bea491SSteven Rostedt (Red Hat) 668f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_MODULES 669f3bea491SSteven Rostedt (Red Hat) #include <linux/moduleloader.h> 670f3bea491SSteven Rostedt (Red Hat) /* Module allocation simplifies allocating memory for code */ 671f3bea491SSteven Rostedt (Red Hat) static inline void *alloc_tramp(unsigned long size) 672f3bea491SSteven Rostedt (Red Hat) { 673f3bea491SSteven Rostedt (Red Hat) return module_alloc(size); 674f3bea491SSteven Rostedt (Red Hat) } 675f3bea491SSteven Rostedt (Red Hat) static inline void tramp_free(void *tramp) 676f3bea491SSteven Rostedt (Red Hat) { 677f3bea491SSteven Rostedt (Red Hat) module_free(NULL, tramp); 678f3bea491SSteven Rostedt (Red Hat) } 679f3bea491SSteven Rostedt (Red Hat) #else 680f3bea491SSteven Rostedt (Red Hat) /* Trampolines can only be created if modules are supported */ 681f3bea491SSteven Rostedt (Red Hat) static inline void *alloc_tramp(unsigned long size) 682f3bea491SSteven Rostedt (Red Hat) { 683f3bea491SSteven Rostedt (Red Hat) return NULL; 684f3bea491SSteven Rostedt (Red Hat) } 685f3bea491SSteven Rostedt (Red Hat) static inline void tramp_free(void *tramp) { } 686f3bea491SSteven Rostedt (Red Hat) #endif 687f3bea491SSteven Rostedt (Red Hat) 688f3bea491SSteven Rostedt (Red Hat) /* Defined as markers to the end of the ftrace default trampolines */ 689f3bea491SSteven Rostedt (Red Hat) extern void ftrace_caller_end(void); 690f3bea491SSteven Rostedt (Red Hat) extern void ftrace_regs_caller_end(void); 691f3bea491SSteven Rostedt (Red Hat) extern void ftrace_return(void); 692f3bea491SSteven Rostedt (Red Hat) extern void ftrace_caller_op_ptr(void); 693f3bea491SSteven Rostedt (Red Hat) extern void ftrace_regs_caller_op_ptr(void); 694f3bea491SSteven Rostedt (Red Hat) 695f3bea491SSteven Rostedt (Red Hat) /* movq function_trace_op(%rip), %rdx */ 696f3bea491SSteven Rostedt (Red Hat) /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */ 697f3bea491SSteven Rostedt (Red Hat) #define OP_REF_SIZE 7 698f3bea491SSteven Rostedt (Red Hat) 699f3bea491SSteven Rostedt (Red Hat) /* 700f3bea491SSteven Rostedt (Red Hat) * The ftrace_ops is passed to the function callback. Since the 701f3bea491SSteven Rostedt (Red Hat) * trampoline only services a single ftrace_ops, we can pass in 702f3bea491SSteven Rostedt (Red Hat) * that ops directly. 703f3bea491SSteven Rostedt (Red Hat) * 704f3bea491SSteven Rostedt (Red Hat) * The ftrace_op_code_union is used to create a pointer to the 705f3bea491SSteven Rostedt (Red Hat) * ftrace_ops that will be passed to the callback function. 706f3bea491SSteven Rostedt (Red Hat) */ 707f3bea491SSteven Rostedt (Red Hat) union ftrace_op_code_union { 708f3bea491SSteven Rostedt (Red Hat) char code[OP_REF_SIZE]; 709f3bea491SSteven Rostedt (Red Hat) struct { 710f3bea491SSteven Rostedt (Red Hat) char op[3]; 711f3bea491SSteven Rostedt (Red Hat) int offset; 712f3bea491SSteven Rostedt (Red Hat) } __attribute__((packed)); 713f3bea491SSteven Rostedt (Red Hat) }; 714f3bea491SSteven Rostedt (Red Hat) 715f3bea491SSteven Rostedt (Red Hat) static unsigned long create_trampoline(struct ftrace_ops *ops) 716f3bea491SSteven Rostedt (Red Hat) { 717f3bea491SSteven Rostedt (Red Hat) unsigned const char *jmp; 718f3bea491SSteven Rostedt (Red Hat) unsigned long start_offset; 719f3bea491SSteven Rostedt (Red Hat) unsigned long end_offset; 720f3bea491SSteven Rostedt (Red Hat) unsigned long op_offset; 721f3bea491SSteven Rostedt (Red Hat) unsigned long offset; 722f3bea491SSteven Rostedt (Red Hat) unsigned long size; 723f3bea491SSteven Rostedt (Red Hat) unsigned long ip; 724f3bea491SSteven Rostedt (Red Hat) unsigned long *ptr; 725f3bea491SSteven Rostedt (Red Hat) void *trampoline; 726f3bea491SSteven Rostedt (Red Hat) /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */ 727f3bea491SSteven Rostedt (Red Hat) unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 }; 728f3bea491SSteven Rostedt (Red Hat) union ftrace_op_code_union op_ptr; 729f3bea491SSteven Rostedt (Red Hat) int ret; 730f3bea491SSteven Rostedt (Red Hat) 731f3bea491SSteven Rostedt (Red Hat) if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { 732f3bea491SSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_regs_caller; 733f3bea491SSteven Rostedt (Red Hat) end_offset = (unsigned long)ftrace_regs_caller_end; 734f3bea491SSteven Rostedt (Red Hat) op_offset = (unsigned long)ftrace_regs_caller_op_ptr; 735f3bea491SSteven Rostedt (Red Hat) } else { 736f3bea491SSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_caller; 737f3bea491SSteven Rostedt (Red Hat) end_offset = (unsigned long)ftrace_caller_end; 738f3bea491SSteven Rostedt (Red Hat) op_offset = (unsigned long)ftrace_caller_op_ptr; 739f3bea491SSteven Rostedt (Red Hat) } 740f3bea491SSteven Rostedt (Red Hat) 741f3bea491SSteven Rostedt (Red Hat) size = end_offset - start_offset; 742f3bea491SSteven Rostedt (Red Hat) 743f3bea491SSteven Rostedt (Red Hat) /* 744f3bea491SSteven Rostedt (Red Hat) * Allocate enough size to store the ftrace_caller code, 745f3bea491SSteven Rostedt (Red Hat) * the jmp to ftrace_return, as well as the address of 746f3bea491SSteven Rostedt (Red Hat) * the ftrace_ops this trampoline is used for. 747f3bea491SSteven Rostedt (Red Hat) */ 748f3bea491SSteven Rostedt (Red Hat) trampoline = alloc_tramp(size + MCOUNT_INSN_SIZE + sizeof(void *)); 749f3bea491SSteven Rostedt (Red Hat) if (!trampoline) 750f3bea491SSteven Rostedt (Red Hat) return 0; 751f3bea491SSteven Rostedt (Red Hat) 752f3bea491SSteven Rostedt (Red Hat) /* Copy ftrace_caller onto the trampoline memory */ 753f3bea491SSteven Rostedt (Red Hat) ret = probe_kernel_read(trampoline, (void *)start_offset, size); 754f3bea491SSteven Rostedt (Red Hat) if (WARN_ON(ret < 0)) { 755f3bea491SSteven Rostedt (Red Hat) tramp_free(trampoline); 756f3bea491SSteven Rostedt (Red Hat) return 0; 757f3bea491SSteven Rostedt (Red Hat) } 758f3bea491SSteven Rostedt (Red Hat) 759f3bea491SSteven Rostedt (Red Hat) ip = (unsigned long)trampoline + size; 760f3bea491SSteven Rostedt (Red Hat) 761f3bea491SSteven Rostedt (Red Hat) /* The trampoline ends with a jmp to ftrace_return */ 762f3bea491SSteven Rostedt (Red Hat) jmp = ftrace_jmp_replace(ip, (unsigned long)ftrace_return); 763f3bea491SSteven Rostedt (Red Hat) memcpy(trampoline + size, jmp, MCOUNT_INSN_SIZE); 764f3bea491SSteven Rostedt (Red Hat) 765f3bea491SSteven Rostedt (Red Hat) /* 766f3bea491SSteven Rostedt (Red Hat) * The address of the ftrace_ops that is used for this trampoline 767f3bea491SSteven Rostedt (Red Hat) * is stored at the end of the trampoline. This will be used to 768f3bea491SSteven Rostedt (Red Hat) * load the third parameter for the callback. Basically, that 769f3bea491SSteven Rostedt (Red Hat) * location at the end of the trampoline takes the place of 770f3bea491SSteven Rostedt (Red Hat) * the global function_trace_op variable. 771f3bea491SSteven Rostedt (Red Hat) */ 772f3bea491SSteven Rostedt (Red Hat) 773f3bea491SSteven Rostedt (Red Hat) ptr = (unsigned long *)(trampoline + size + MCOUNT_INSN_SIZE); 774f3bea491SSteven Rostedt (Red Hat) *ptr = (unsigned long)ops; 775f3bea491SSteven Rostedt (Red Hat) 776f3bea491SSteven Rostedt (Red Hat) op_offset -= start_offset; 777f3bea491SSteven Rostedt (Red Hat) memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE); 778f3bea491SSteven Rostedt (Red Hat) 779f3bea491SSteven Rostedt (Red Hat) /* Are we pointing to the reference? */ 780f3bea491SSteven Rostedt (Red Hat) if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0)) { 781f3bea491SSteven Rostedt (Red Hat) tramp_free(trampoline); 782f3bea491SSteven Rostedt (Red Hat) return 0; 783f3bea491SSteven Rostedt (Red Hat) } 784f3bea491SSteven Rostedt (Red Hat) 785f3bea491SSteven Rostedt (Red Hat) /* Load the contents of ptr into the callback parameter */ 786f3bea491SSteven Rostedt (Red Hat) offset = (unsigned long)ptr; 787f3bea491SSteven Rostedt (Red Hat) offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE; 788f3bea491SSteven Rostedt (Red Hat) 789f3bea491SSteven Rostedt (Red Hat) op_ptr.offset = offset; 790f3bea491SSteven Rostedt (Red Hat) 791f3bea491SSteven Rostedt (Red Hat) /* put in the new offset to the ftrace_ops */ 792f3bea491SSteven Rostedt (Red Hat) memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE); 793f3bea491SSteven Rostedt (Red Hat) 794f3bea491SSteven Rostedt (Red Hat) /* ALLOC_TRAMP flags lets us know we created it */ 795f3bea491SSteven Rostedt (Red Hat) ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP; 796f3bea491SSteven Rostedt (Red Hat) 797f3bea491SSteven Rostedt (Red Hat) return (unsigned long)trampoline; 798f3bea491SSteven Rostedt (Red Hat) } 799f3bea491SSteven Rostedt (Red Hat) 800*15d5b02cSSteven Rostedt (Red Hat) static unsigned long calc_trampoline_call_offset(bool save_regs) 801*15d5b02cSSteven Rostedt (Red Hat) { 802*15d5b02cSSteven Rostedt (Red Hat) unsigned long start_offset; 803*15d5b02cSSteven Rostedt (Red Hat) unsigned long call_offset; 804*15d5b02cSSteven Rostedt (Red Hat) 805*15d5b02cSSteven Rostedt (Red Hat) if (save_regs) { 806*15d5b02cSSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_regs_caller; 807*15d5b02cSSteven Rostedt (Red Hat) call_offset = (unsigned long)ftrace_regs_call; 808*15d5b02cSSteven Rostedt (Red Hat) } else { 809*15d5b02cSSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_caller; 810*15d5b02cSSteven Rostedt (Red Hat) call_offset = (unsigned long)ftrace_call; 811*15d5b02cSSteven Rostedt (Red Hat) } 812*15d5b02cSSteven Rostedt (Red Hat) 813*15d5b02cSSteven Rostedt (Red Hat) return call_offset - start_offset; 814*15d5b02cSSteven Rostedt (Red Hat) } 815*15d5b02cSSteven Rostedt (Red Hat) 816f3bea491SSteven Rostedt (Red Hat) void arch_ftrace_update_trampoline(struct ftrace_ops *ops) 817f3bea491SSteven Rostedt (Red Hat) { 818f3bea491SSteven Rostedt (Red Hat) ftrace_func_t func; 819f3bea491SSteven Rostedt (Red Hat) unsigned char *new; 820f3bea491SSteven Rostedt (Red Hat) unsigned long offset; 821f3bea491SSteven Rostedt (Red Hat) unsigned long ip; 822f3bea491SSteven Rostedt (Red Hat) int ret; 823f3bea491SSteven Rostedt (Red Hat) 824f3bea491SSteven Rostedt (Red Hat) if (ops->trampoline) { 825f3bea491SSteven Rostedt (Red Hat) /* 826f3bea491SSteven Rostedt (Red Hat) * The ftrace_ops caller may set up its own trampoline. 827f3bea491SSteven Rostedt (Red Hat) * In such a case, this code must not modify it. 828f3bea491SSteven Rostedt (Red Hat) */ 829f3bea491SSteven Rostedt (Red Hat) if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) 830f3bea491SSteven Rostedt (Red Hat) return; 831f3bea491SSteven Rostedt (Red Hat) } else { 832f3bea491SSteven Rostedt (Red Hat) ops->trampoline = create_trampoline(ops); 833f3bea491SSteven Rostedt (Red Hat) if (!ops->trampoline) 834f3bea491SSteven Rostedt (Red Hat) return; 835f3bea491SSteven Rostedt (Red Hat) } 836f3bea491SSteven Rostedt (Red Hat) 837*15d5b02cSSteven Rostedt (Red Hat) offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS); 838f3bea491SSteven Rostedt (Red Hat) ip = ops->trampoline + offset; 839f3bea491SSteven Rostedt (Red Hat) 840f3bea491SSteven Rostedt (Red Hat) func = ftrace_ops_get_func(ops); 841f3bea491SSteven Rostedt (Red Hat) 842f3bea491SSteven Rostedt (Red Hat) /* Do a safe modify in case the trampoline is executing */ 843f3bea491SSteven Rostedt (Red Hat) new = ftrace_call_replace(ip, (unsigned long)func); 844f3bea491SSteven Rostedt (Red Hat) ret = update_ftrace_func(ip, new); 845f3bea491SSteven Rostedt (Red Hat) 846f3bea491SSteven Rostedt (Red Hat) /* The update should never fail */ 847f3bea491SSteven Rostedt (Red Hat) WARN_ON(ret); 848f3bea491SSteven Rostedt (Red Hat) } 849*15d5b02cSSteven Rostedt (Red Hat) 850*15d5b02cSSteven Rostedt (Red Hat) /* Return the address of the function the trampoline calls */ 851*15d5b02cSSteven Rostedt (Red Hat) static void *addr_from_call(void *ptr) 852*15d5b02cSSteven Rostedt (Red Hat) { 853*15d5b02cSSteven Rostedt (Red Hat) union ftrace_code_union calc; 854*15d5b02cSSteven Rostedt (Red Hat) int ret; 855*15d5b02cSSteven Rostedt (Red Hat) 856*15d5b02cSSteven Rostedt (Red Hat) ret = probe_kernel_read(&calc, ptr, MCOUNT_INSN_SIZE); 857*15d5b02cSSteven Rostedt (Red Hat) if (WARN_ON_ONCE(ret < 0)) 858*15d5b02cSSteven Rostedt (Red Hat) return NULL; 859*15d5b02cSSteven Rostedt (Red Hat) 860*15d5b02cSSteven Rostedt (Red Hat) /* Make sure this is a call */ 861*15d5b02cSSteven Rostedt (Red Hat) if (WARN_ON_ONCE(calc.e8 != 0xe8)) { 862*15d5b02cSSteven Rostedt (Red Hat) pr_warn("Expected e8, got %x\n", calc.e8); 863*15d5b02cSSteven Rostedt (Red Hat) return NULL; 864*15d5b02cSSteven Rostedt (Red Hat) } 865*15d5b02cSSteven Rostedt (Red Hat) 866*15d5b02cSSteven Rostedt (Red Hat) return ptr + MCOUNT_INSN_SIZE + calc.offset; 867*15d5b02cSSteven Rostedt (Red Hat) } 868*15d5b02cSSteven Rostedt (Red Hat) 869*15d5b02cSSteven Rostedt (Red Hat) void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, 870*15d5b02cSSteven Rostedt (Red Hat) unsigned long frame_pointer); 871*15d5b02cSSteven Rostedt (Red Hat) 872*15d5b02cSSteven Rostedt (Red Hat) /* 873*15d5b02cSSteven Rostedt (Red Hat) * If the ops->trampoline was not allocated, then it probably 874*15d5b02cSSteven Rostedt (Red Hat) * has a static trampoline func, or is the ftrace caller itself. 875*15d5b02cSSteven Rostedt (Red Hat) */ 876*15d5b02cSSteven Rostedt (Red Hat) static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) 877*15d5b02cSSteven Rostedt (Red Hat) { 878*15d5b02cSSteven Rostedt (Red Hat) unsigned long offset; 879*15d5b02cSSteven Rostedt (Red Hat) bool save_regs = rec->flags & FTRACE_FL_REGS_EN; 880*15d5b02cSSteven Rostedt (Red Hat) void *ptr; 881*15d5b02cSSteven Rostedt (Red Hat) 882*15d5b02cSSteven Rostedt (Red Hat) if (ops && ops->trampoline) { 883*15d5b02cSSteven Rostedt (Red Hat) #ifdef CONFIG_FUNCTION_GRAPH_TRACER 884*15d5b02cSSteven Rostedt (Red Hat) /* 885*15d5b02cSSteven Rostedt (Red Hat) * We only know about function graph tracer setting as static 886*15d5b02cSSteven Rostedt (Red Hat) * trampoline. 887*15d5b02cSSteven Rostedt (Red Hat) */ 888*15d5b02cSSteven Rostedt (Red Hat) if (ops->trampoline == FTRACE_GRAPH_ADDR) 889*15d5b02cSSteven Rostedt (Red Hat) return (void *)prepare_ftrace_return; 890*15d5b02cSSteven Rostedt (Red Hat) #endif 891*15d5b02cSSteven Rostedt (Red Hat) return NULL; 892*15d5b02cSSteven Rostedt (Red Hat) } 893*15d5b02cSSteven Rostedt (Red Hat) 894*15d5b02cSSteven Rostedt (Red Hat) offset = calc_trampoline_call_offset(save_regs); 895*15d5b02cSSteven Rostedt (Red Hat) 896*15d5b02cSSteven Rostedt (Red Hat) if (save_regs) 897*15d5b02cSSteven Rostedt (Red Hat) ptr = (void *)FTRACE_REGS_ADDR + offset; 898*15d5b02cSSteven Rostedt (Red Hat) else 899*15d5b02cSSteven Rostedt (Red Hat) ptr = (void *)FTRACE_ADDR + offset; 900*15d5b02cSSteven Rostedt (Red Hat) 901*15d5b02cSSteven Rostedt (Red Hat) return addr_from_call(ptr); 902*15d5b02cSSteven Rostedt (Red Hat) } 903*15d5b02cSSteven Rostedt (Red Hat) 904*15d5b02cSSteven Rostedt (Red Hat) void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec) 905*15d5b02cSSteven Rostedt (Red Hat) { 906*15d5b02cSSteven Rostedt (Red Hat) unsigned long offset; 907*15d5b02cSSteven Rostedt (Red Hat) 908*15d5b02cSSteven Rostedt (Red Hat) /* If we didn't allocate this trampoline, consider it static */ 909*15d5b02cSSteven Rostedt (Red Hat) if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) 910*15d5b02cSSteven Rostedt (Red Hat) return static_tramp_func(ops, rec); 911*15d5b02cSSteven Rostedt (Red Hat) 912*15d5b02cSSteven Rostedt (Red Hat) offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS); 913*15d5b02cSSteven Rostedt (Red Hat) return addr_from_call((void *)ops->trampoline + offset); 914*15d5b02cSSteven Rostedt (Red Hat) } 915*15d5b02cSSteven Rostedt (Red Hat) 916*15d5b02cSSteven Rostedt (Red Hat) 917f3bea491SSteven Rostedt (Red Hat) #endif /* CONFIG_X86_64 */ 918f3bea491SSteven Rostedt (Red Hat) #endif /* CONFIG_DYNAMIC_FTRACE */ 919f3bea491SSteven Rostedt (Red Hat) 920f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_FUNCTION_GRAPH_TRACER 921f3bea491SSteven Rostedt (Red Hat) 922f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_DYNAMIC_FTRACE 923f3bea491SSteven Rostedt (Red Hat) extern void ftrace_graph_call(void); 9245a45cfe1SSteven Rostedt 92587fbb2acSSteven Rostedt (Red Hat) static int ftrace_mod_jmp(unsigned long ip, void *func) 92687fbb2acSSteven Rostedt (Red Hat) { 92787fbb2acSSteven Rostedt (Red Hat) unsigned char *new; 9285a45cfe1SSteven Rostedt 92987fbb2acSSteven Rostedt (Red Hat) new = ftrace_jmp_replace(ip, (unsigned long)func); 9305a45cfe1SSteven Rostedt 93187fbb2acSSteven Rostedt (Red Hat) return update_ftrace_func(ip, new); 9325a45cfe1SSteven Rostedt } 9335a45cfe1SSteven Rostedt 9345a45cfe1SSteven Rostedt int ftrace_enable_ftrace_graph_caller(void) 9355a45cfe1SSteven Rostedt { 9365a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call); 9375a45cfe1SSteven Rostedt 93887fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_graph_caller); 9395a45cfe1SSteven Rostedt } 9405a45cfe1SSteven Rostedt 9415a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void) 9425a45cfe1SSteven Rostedt { 9435a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call); 9445a45cfe1SSteven Rostedt 94587fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_stub); 9465a45cfe1SSteven Rostedt } 9475a45cfe1SSteven Rostedt 948e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */ 949e7d3737eSFrederic Weisbecker 950e7d3737eSFrederic Weisbecker /* 951e7d3737eSFrederic Weisbecker * Hook the return address and push it in the stack of return addrs 952e7d3737eSFrederic Weisbecker * in current thread info. 953e7d3737eSFrederic Weisbecker */ 95471e308a2SSteven Rostedt void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, 95571e308a2SSteven Rostedt unsigned long frame_pointer) 956e7d3737eSFrederic Weisbecker { 957e7d3737eSFrederic Weisbecker unsigned long old; 958e7d3737eSFrederic Weisbecker int faulted; 959287b6e68SFrederic Weisbecker struct ftrace_graph_ent trace; 960e7d3737eSFrederic Weisbecker unsigned long return_hooker = (unsigned long) 961e7d3737eSFrederic Weisbecker &return_to_handler; 962e7d3737eSFrederic Weisbecker 96384b2bc7fSSteven Rostedt (Red Hat) if (unlikely(ftrace_graph_is_dead())) 96484b2bc7fSSteven Rostedt (Red Hat) return; 96584b2bc7fSSteven Rostedt (Red Hat) 966380c4b14SFrederic Weisbecker if (unlikely(atomic_read(¤t->tracing_graph_pause))) 967e7d3737eSFrederic Weisbecker return; 968e7d3737eSFrederic Weisbecker 969e7d3737eSFrederic Weisbecker /* 970e7d3737eSFrederic Weisbecker * Protect against fault, even if it shouldn't 971e7d3737eSFrederic Weisbecker * happen. This tool is too much intrusive to 972e7d3737eSFrederic Weisbecker * ignore such a protection. 973e7d3737eSFrederic Weisbecker */ 974e7d3737eSFrederic Weisbecker asm volatile( 97596665788SSteven Rostedt "1: " _ASM_MOV " (%[parent]), %[old]\n" 97696665788SSteven Rostedt "2: " _ASM_MOV " %[return_hooker], (%[parent])\n" 977e7d3737eSFrederic Weisbecker " movl $0, %[faulted]\n" 978e3944bfaSSteven Rostedt "3:\n" 979e7d3737eSFrederic Weisbecker 980e7d3737eSFrederic Weisbecker ".section .fixup, \"ax\"\n" 981e3944bfaSSteven Rostedt "4: movl $1, %[faulted]\n" 982e3944bfaSSteven Rostedt " jmp 3b\n" 983e7d3737eSFrederic Weisbecker ".previous\n" 984e7d3737eSFrederic Weisbecker 985e3944bfaSSteven Rostedt _ASM_EXTABLE(1b, 4b) 986e3944bfaSSteven Rostedt _ASM_EXTABLE(2b, 4b) 987e7d3737eSFrederic Weisbecker 988aa512a27SSteven Rostedt : [old] "=&r" (old), [faulted] "=r" (faulted) 98996665788SSteven Rostedt : [parent] "r" (parent), [return_hooker] "r" (return_hooker) 990e7d3737eSFrederic Weisbecker : "memory" 991e7d3737eSFrederic Weisbecker ); 992e7d3737eSFrederic Weisbecker 99314a866c5SSteven Rostedt if (unlikely(faulted)) { 99414a866c5SSteven Rostedt ftrace_graph_stop(); 99514a866c5SSteven Rostedt WARN_ON(1); 996e7d3737eSFrederic Weisbecker return; 997e7d3737eSFrederic Weisbecker } 998e7d3737eSFrederic Weisbecker 999722b3c74SSteven Rostedt trace.func = self_addr; 1000722b3c74SSteven Rostedt trace.depth = current->curr_ret_stack + 1; 1001722b3c74SSteven Rostedt 1002722b3c74SSteven Rostedt /* Only trace if the calling function expects to */ 1003722b3c74SSteven Rostedt if (!ftrace_graph_entry(&trace)) { 1004e7d3737eSFrederic Weisbecker *parent = old; 1005287b6e68SFrederic Weisbecker return; 1006287b6e68SFrederic Weisbecker } 1007287b6e68SFrederic Weisbecker 1008722b3c74SSteven Rostedt if (ftrace_push_return_trace(old, self_addr, &trace.depth, 1009722b3c74SSteven Rostedt frame_pointer) == -EBUSY) { 1010e49dc19cSSteven Rostedt *parent = old; 1011722b3c74SSteven Rostedt return; 1012e49dc19cSSteven Rostedt } 1013e7d3737eSFrederic Weisbecker } 1014fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 1015