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> 203d083395SSteven Rostedt #include <linux/init.h> 213d083395SSteven Rostedt #include <linux/list.h> 2284e1c6bbSmatthieu castet #include <linux/module.h> 233d083395SSteven Rostedt 2447788c58SFrederic Weisbecker #include <trace/syscall.h> 2547788c58SFrederic Weisbecker 2616239630SSteven Rostedt #include <asm/cacheflush.h> 2759a094c9SSteven Rostedt #include <asm/kprobes.h> 28395a59d0SAbhishek Sagar #include <asm/ftrace.h> 29732f3ca7SSteven Rostedt #include <asm/nops.h> 303d083395SSteven Rostedt 31caf4b323SFrederic Weisbecker #ifdef CONFIG_DYNAMIC_FTRACE 323d083395SSteven Rostedt 3316239630SSteven Rostedt int ftrace_arch_code_modify_prepare(void) 3416239630SSteven Rostedt { 3516239630SSteven Rostedt set_kernel_text_rw(); 3684e1c6bbSmatthieu castet set_all_modules_text_rw(); 3716239630SSteven Rostedt return 0; 3816239630SSteven Rostedt } 3916239630SSteven Rostedt 4016239630SSteven Rostedt int ftrace_arch_code_modify_post_process(void) 4116239630SSteven Rostedt { 4284e1c6bbSmatthieu castet set_all_modules_text_ro(); 4316239630SSteven Rostedt set_kernel_text_ro(); 4416239630SSteven Rostedt return 0; 4516239630SSteven Rostedt } 4616239630SSteven Rostedt 473d083395SSteven Rostedt union ftrace_code_union { 48395a59d0SAbhishek Sagar char code[MCOUNT_INSN_SIZE]; 493d083395SSteven Rostedt struct { 503d083395SSteven Rostedt char e8; 513d083395SSteven Rostedt int offset; 523d083395SSteven Rostedt } __attribute__((packed)); 533d083395SSteven Rostedt }; 543d083395SSteven Rostedt 5515adc048SSteven Rostedt static int ftrace_calc_offset(long ip, long addr) 563c1720f0SSteven Rostedt { 573c1720f0SSteven Rostedt return (int)(addr - ip); 583d083395SSteven Rostedt } 593d083395SSteven Rostedt 6031e88909SSteven Rostedt static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr) 613c1720f0SSteven Rostedt { 623c1720f0SSteven Rostedt static union ftrace_code_union calc; 633c1720f0SSteven Rostedt 643c1720f0SSteven Rostedt calc.e8 = 0xe8; 65395a59d0SAbhishek Sagar calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); 663c1720f0SSteven Rostedt 673c1720f0SSteven Rostedt /* 683c1720f0SSteven Rostedt * No locking needed, this must be called via kstop_machine 693c1720f0SSteven Rostedt * which in essence is like running on a uniprocessor machine. 703c1720f0SSteven Rostedt */ 713c1720f0SSteven Rostedt return calc.code; 723c1720f0SSteven Rostedt } 733c1720f0SSteven Rostedt 7455ca3cc1SSuresh Siddha static inline int 7555ca3cc1SSuresh Siddha within(unsigned long addr, unsigned long start, unsigned long end) 7655ca3cc1SSuresh Siddha { 7755ca3cc1SSuresh Siddha return addr >= start && addr < end; 7855ca3cc1SSuresh Siddha } 7955ca3cc1SSuresh Siddha 8087fbb2acSSteven Rostedt (Red Hat) static unsigned long text_ip_addr(unsigned long ip) 8117666f02SSteven Rostedt { 8255ca3cc1SSuresh Siddha /* 8355ca3cc1SSuresh Siddha * On x86_64, kernel text mappings are mapped read-only with 8455ca3cc1SSuresh Siddha * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead 8555ca3cc1SSuresh Siddha * of the kernel text mapping to modify the kernel text. 8655ca3cc1SSuresh Siddha * 8755ca3cc1SSuresh Siddha * For 32bit kernels, these mappings are same and we can use 8855ca3cc1SSuresh Siddha * kernel identity mapping to modify code. 8955ca3cc1SSuresh Siddha */ 9055ca3cc1SSuresh Siddha if (within(ip, (unsigned long)_text, (unsigned long)_etext)) 91217f155eSAlexander Duyck ip = (unsigned long)__va(__pa_symbol(ip)); 9255ca3cc1SSuresh Siddha 9387fbb2acSSteven Rostedt (Red Hat) return ip; 9417666f02SSteven Rostedt } 9517666f02SSteven Rostedt 96dc326fcaSH. Peter Anvin static const unsigned char *ftrace_nop_replace(void) 97caf4b323SFrederic Weisbecker { 98dc326fcaSH. Peter Anvin return ideal_nops[NOP_ATOMIC5]; 99caf4b323SFrederic Weisbecker } 100caf4b323SFrederic Weisbecker 10131e88909SSteven Rostedt static int 1028a4d0a68SSteven Rostedt ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code, 1030d098a7dSRakib Mullick unsigned const char *new_code) 1043d083395SSteven Rostedt { 1056f93fc07SSteven Rostedt unsigned char replaced[MCOUNT_INSN_SIZE]; 1063d083395SSteven Rostedt 1073d083395SSteven Rostedt /* 1083d083395SSteven Rostedt * Note: Due to modules and __init, code can 1093d083395SSteven Rostedt * disappear and change, we need to protect against faulting 11076aefee5SSteven Rostedt * as well as code changing. We do this by using the 111ab9a0918SSteven Rostedt * probe_kernel_* functions. 1123d083395SSteven Rostedt * 1133d083395SSteven Rostedt * No real locking needed, this code is run through 1146f93fc07SSteven Rostedt * kstop_machine, or before SMP starts. 1153d083395SSteven Rostedt */ 11676aefee5SSteven Rostedt 11776aefee5SSteven Rostedt /* read the text we want to modify */ 118ab9a0918SSteven Rostedt if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) 119593eb8a2SSteven Rostedt return -EFAULT; 1206f93fc07SSteven Rostedt 12176aefee5SSteven Rostedt /* Make sure it is what we expect it to be */ 1226f93fc07SSteven Rostedt if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0) 123593eb8a2SSteven Rostedt return -EINVAL; 1246f93fc07SSteven Rostedt 12587fbb2acSSteven Rostedt (Red Hat) ip = text_ip_addr(ip); 12687fbb2acSSteven Rostedt (Red Hat) 12776aefee5SSteven Rostedt /* replace the text with the new text */ 12887fbb2acSSteven Rostedt (Red Hat) if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE)) 129593eb8a2SSteven Rostedt return -EPERM; 1306f93fc07SSteven Rostedt 1313d083395SSteven Rostedt sync_core(); 1323d083395SSteven Rostedt 1336f93fc07SSteven Rostedt return 0; 1343d083395SSteven Rostedt } 1353d083395SSteven Rostedt 13631e88909SSteven Rostedt int ftrace_make_nop(struct module *mod, 13731e88909SSteven Rostedt struct dyn_ftrace *rec, unsigned long addr) 13831e88909SSteven Rostedt { 1390d098a7dSRakib Mullick unsigned const char *new, *old; 14031e88909SSteven Rostedt unsigned long ip = rec->ip; 14131e88909SSteven Rostedt 14231e88909SSteven Rostedt old = ftrace_call_replace(ip, addr); 14331e88909SSteven Rostedt new = ftrace_nop_replace(); 14431e88909SSteven Rostedt 1458a4d0a68SSteven Rostedt /* 1468a4d0a68SSteven Rostedt * On boot up, and when modules are loaded, the MCOUNT_ADDR 1478a4d0a68SSteven Rostedt * is converted to a nop, and will never become MCOUNT_ADDR 1488a4d0a68SSteven Rostedt * again. This code is either running before SMP (on boot up) 1498a4d0a68SSteven Rostedt * or before the code will ever be executed (module load). 1508a4d0a68SSteven Rostedt * We do not want to use the breakpoint version in this case, 1518a4d0a68SSteven Rostedt * just modify the code directly. 1528a4d0a68SSteven Rostedt */ 1538a4d0a68SSteven Rostedt if (addr == MCOUNT_ADDR) 1548a4d0a68SSteven Rostedt return ftrace_modify_code_direct(rec->ip, old, new); 1558a4d0a68SSteven Rostedt 1568a4d0a68SSteven Rostedt /* Normal cases use add_brk_on_nop */ 1578a4d0a68SSteven Rostedt WARN_ONCE(1, "invalid use of ftrace_make_nop"); 1588a4d0a68SSteven Rostedt return -EINVAL; 15931e88909SSteven Rostedt } 16031e88909SSteven Rostedt 16131e88909SSteven Rostedt int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) 16231e88909SSteven Rostedt { 1630d098a7dSRakib Mullick unsigned const char *new, *old; 16431e88909SSteven Rostedt unsigned long ip = rec->ip; 16531e88909SSteven Rostedt 16631e88909SSteven Rostedt old = ftrace_nop_replace(); 16731e88909SSteven Rostedt new = ftrace_call_replace(ip, addr); 16831e88909SSteven Rostedt 1698a4d0a68SSteven Rostedt /* Should only be called when module is loaded */ 1708a4d0a68SSteven Rostedt return ftrace_modify_code_direct(rec->ip, old, new); 171d61f82d0SSteven Rostedt } 172d61f82d0SSteven Rostedt 173a192cd04SSteven Rostedt /* 174a192cd04SSteven Rostedt * The modifying_ftrace_code is used to tell the breakpoint 175a192cd04SSteven Rostedt * handler to call ftrace_int3_handler(). If it fails to 176a192cd04SSteven Rostedt * call this handler for a breakpoint added by ftrace, then 177a192cd04SSteven Rostedt * the kernel may crash. 178a192cd04SSteven Rostedt * 179a192cd04SSteven Rostedt * As atomic_writes on x86 do not need a barrier, we do not 180a192cd04SSteven Rostedt * need to add smp_mb()s for this to work. It is also considered 181a192cd04SSteven Rostedt * that we can not read the modifying_ftrace_code before 182a192cd04SSteven Rostedt * executing the breakpoint. That would be quite remarkable if 183a192cd04SSteven Rostedt * it could do that. Here's the flow that is required: 184a192cd04SSteven Rostedt * 185a192cd04SSteven Rostedt * CPU-0 CPU-1 186a192cd04SSteven Rostedt * 187a192cd04SSteven Rostedt * atomic_inc(mfc); 188a192cd04SSteven Rostedt * write int3s 189a192cd04SSteven Rostedt * <trap-int3> // implicit (r)mb 190a192cd04SSteven Rostedt * if (atomic_read(mfc)) 191a192cd04SSteven Rostedt * call ftrace_int3_handler() 192a192cd04SSteven Rostedt * 193a192cd04SSteven Rostedt * Then when we are finished: 194a192cd04SSteven Rostedt * 195a192cd04SSteven Rostedt * atomic_dec(mfc); 196a192cd04SSteven Rostedt * 197a192cd04SSteven Rostedt * If we hit a breakpoint that was not set by ftrace, it does not 198a192cd04SSteven Rostedt * matter if ftrace_int3_handler() is called or not. It will 199a192cd04SSteven Rostedt * simply be ignored. But it is crucial that a ftrace nop/caller 200a192cd04SSteven Rostedt * breakpoint is handled. No other user should ever place a 201a192cd04SSteven Rostedt * breakpoint on an ftrace nop/caller location. It must only 202a192cd04SSteven Rostedt * be done by this code. 203a192cd04SSteven Rostedt */ 204a192cd04SSteven Rostedt atomic_t modifying_ftrace_code __read_mostly; 20508d636b6SSteven Rostedt 2068a4d0a68SSteven Rostedt static int 2078a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code, 2088a4d0a68SSteven Rostedt unsigned const char *new_code); 2098a4d0a68SSteven Rostedt 21008f6fba5SSteven Rostedt /* 21108f6fba5SSteven Rostedt * Should never be called: 21208f6fba5SSteven Rostedt * As it is only called by __ftrace_replace_code() which is called by 21308f6fba5SSteven Rostedt * ftrace_replace_code() that x86 overrides, and by ftrace_update_code() 21408f6fba5SSteven Rostedt * which is called to turn mcount into nops or nops into function calls 21508f6fba5SSteven Rostedt * but not to convert a function from not using regs to one that uses 21608f6fba5SSteven Rostedt * regs, which ftrace_modify_call() is for. 21708f6fba5SSteven Rostedt */ 21808f6fba5SSteven Rostedt int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, 21908f6fba5SSteven Rostedt unsigned long addr) 22008f6fba5SSteven Rostedt { 22108f6fba5SSteven Rostedt WARN_ON(1); 22208f6fba5SSteven Rostedt return -EINVAL; 22308f6fba5SSteven Rostedt } 22408f6fba5SSteven Rostedt 22587fbb2acSSteven Rostedt (Red Hat) static unsigned long ftrace_update_func; 22687fbb2acSSteven Rostedt (Red Hat) 22787fbb2acSSteven Rostedt (Red Hat) static int update_ftrace_func(unsigned long ip, void *new) 2288a4d0a68SSteven Rostedt { 22987fbb2acSSteven Rostedt (Red Hat) unsigned char old[MCOUNT_INSN_SIZE]; 2308a4d0a68SSteven Rostedt int ret; 2318a4d0a68SSteven Rostedt 23287fbb2acSSteven Rostedt (Red Hat) memcpy(old, (void *)ip, MCOUNT_INSN_SIZE); 23387fbb2acSSteven Rostedt (Red Hat) 23487fbb2acSSteven Rostedt (Red Hat) ftrace_update_func = ip; 23587fbb2acSSteven Rostedt (Red Hat) /* Make sure the breakpoints see the ftrace_update_func update */ 23687fbb2acSSteven Rostedt (Red Hat) smp_wmb(); 2378a4d0a68SSteven Rostedt 2388a4d0a68SSteven Rostedt /* See comment above by declaration of modifying_ftrace_code */ 2398a4d0a68SSteven Rostedt atomic_inc(&modifying_ftrace_code); 2408a4d0a68SSteven Rostedt 2418a4d0a68SSteven Rostedt ret = ftrace_modify_code(ip, old, new); 2428a4d0a68SSteven Rostedt 24387fbb2acSSteven Rostedt (Red Hat) atomic_dec(&modifying_ftrace_code); 24487fbb2acSSteven Rostedt (Red Hat) 24587fbb2acSSteven Rostedt (Red Hat) return ret; 24687fbb2acSSteven Rostedt (Red Hat) } 24787fbb2acSSteven Rostedt (Red Hat) 24887fbb2acSSteven Rostedt (Red Hat) int ftrace_update_ftrace_func(ftrace_func_t func) 24987fbb2acSSteven Rostedt (Red Hat) { 25087fbb2acSSteven Rostedt (Red Hat) unsigned long ip = (unsigned long)(&ftrace_call); 25187fbb2acSSteven Rostedt (Red Hat) unsigned char *new; 25287fbb2acSSteven Rostedt (Red Hat) int ret; 25387fbb2acSSteven Rostedt (Red Hat) 25487fbb2acSSteven Rostedt (Red Hat) new = ftrace_call_replace(ip, (unsigned long)func); 25587fbb2acSSteven Rostedt (Red Hat) ret = update_ftrace_func(ip, new); 25687fbb2acSSteven Rostedt (Red Hat) 25708f6fba5SSteven Rostedt /* Also update the regs callback function */ 25808f6fba5SSteven Rostedt if (!ret) { 25908f6fba5SSteven Rostedt ip = (unsigned long)(&ftrace_regs_call); 26008f6fba5SSteven Rostedt new = ftrace_call_replace(ip, (unsigned long)func); 26187fbb2acSSteven Rostedt (Red Hat) ret = update_ftrace_func(ip, new); 26208f6fba5SSteven Rostedt } 26308f6fba5SSteven Rostedt 2648a4d0a68SSteven Rostedt return ret; 2658a4d0a68SSteven Rostedt } 2668a4d0a68SSteven Rostedt 267ab4ead02SKevin Hao static int is_ftrace_caller(unsigned long ip) 268ab4ead02SKevin Hao { 26987fbb2acSSteven Rostedt (Red Hat) if (ip == ftrace_update_func) 270ab4ead02SKevin Hao return 1; 271ab4ead02SKevin Hao 272ab4ead02SKevin Hao return 0; 273ab4ead02SKevin Hao } 274ab4ead02SKevin Hao 27508d636b6SSteven Rostedt /* 27608d636b6SSteven Rostedt * A breakpoint was added to the code address we are about to 27708d636b6SSteven Rostedt * modify, and this is the handle that will just skip over it. 27808d636b6SSteven Rostedt * We are either changing a nop into a trace call, or a trace 27908d636b6SSteven Rostedt * call to a nop. While the change is taking place, we treat 28008d636b6SSteven Rostedt * it just like it was a nop. 28108d636b6SSteven Rostedt */ 28208d636b6SSteven Rostedt int ftrace_int3_handler(struct pt_regs *regs) 28308d636b6SSteven Rostedt { 284ab4ead02SKevin Hao unsigned long ip; 285ab4ead02SKevin Hao 28608d636b6SSteven Rostedt if (WARN_ON_ONCE(!regs)) 28708d636b6SSteven Rostedt return 0; 28808d636b6SSteven Rostedt 289ab4ead02SKevin Hao ip = regs->ip - 1; 290ab4ead02SKevin Hao if (!ftrace_location(ip) && !is_ftrace_caller(ip)) 29108d636b6SSteven Rostedt return 0; 29208d636b6SSteven Rostedt 29308d636b6SSteven Rostedt regs->ip += MCOUNT_INSN_SIZE - 1; 29408d636b6SSteven Rostedt 29508d636b6SSteven Rostedt return 1; 29608d636b6SSteven Rostedt } 29708d636b6SSteven Rostedt 29808d636b6SSteven Rostedt static int ftrace_write(unsigned long ip, const char *val, int size) 29908d636b6SSteven Rostedt { 300964f7b6bSPetr Mladek ip = text_ip_addr(ip); 30108d636b6SSteven Rostedt 30292550405SSteven Rostedt (Red Hat) if (probe_kernel_write((void *)ip, val, size)) 30392550405SSteven Rostedt (Red Hat) return -EPERM; 30492550405SSteven Rostedt (Red Hat) 30592550405SSteven Rostedt (Red Hat) return 0; 30608d636b6SSteven Rostedt } 30708d636b6SSteven Rostedt 30808d636b6SSteven Rostedt static int add_break(unsigned long ip, const char *old) 30908d636b6SSteven Rostedt { 31008d636b6SSteven Rostedt unsigned char replaced[MCOUNT_INSN_SIZE]; 31108d636b6SSteven Rostedt unsigned char brk = BREAKPOINT_INSTRUCTION; 31208d636b6SSteven Rostedt 31308d636b6SSteven Rostedt if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) 31408d636b6SSteven Rostedt return -EFAULT; 31508d636b6SSteven Rostedt 31608d636b6SSteven Rostedt /* Make sure it is what we expect it to be */ 31708d636b6SSteven Rostedt if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0) 31808d636b6SSteven Rostedt return -EINVAL; 31908d636b6SSteven Rostedt 32092550405SSteven Rostedt (Red Hat) return ftrace_write(ip, &brk, 1); 32108d636b6SSteven Rostedt } 32208d636b6SSteven Rostedt 32308d636b6SSteven Rostedt static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr) 32408d636b6SSteven Rostedt { 32508d636b6SSteven Rostedt unsigned const char *old; 32608d636b6SSteven Rostedt unsigned long ip = rec->ip; 32708d636b6SSteven Rostedt 32808d636b6SSteven Rostedt old = ftrace_call_replace(ip, addr); 32908d636b6SSteven Rostedt 33008d636b6SSteven Rostedt return add_break(rec->ip, old); 33108d636b6SSteven Rostedt } 33208d636b6SSteven Rostedt 33308d636b6SSteven Rostedt 33408d636b6SSteven Rostedt static int add_brk_on_nop(struct dyn_ftrace *rec) 33508d636b6SSteven Rostedt { 33608d636b6SSteven Rostedt unsigned const char *old; 33708d636b6SSteven Rostedt 33808d636b6SSteven Rostedt old = ftrace_nop_replace(); 33908d636b6SSteven Rostedt 34008d636b6SSteven Rostedt return add_break(rec->ip, old); 34108d636b6SSteven Rostedt } 34208d636b6SSteven Rostedt 34308d636b6SSteven Rostedt static int add_breakpoints(struct dyn_ftrace *rec, int enable) 34408d636b6SSteven Rostedt { 34508d636b6SSteven Rostedt unsigned long ftrace_addr; 34608d636b6SSteven Rostedt int ret; 34708d636b6SSteven Rostedt 3487413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_curr(rec); 34908d636b6SSteven Rostedt 35094792ea0SSteven Rostedt (Red Hat) ret = ftrace_test_record(rec, enable); 35108d636b6SSteven Rostedt 35208d636b6SSteven Rostedt switch (ret) { 35308d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 35408d636b6SSteven Rostedt return 0; 35508d636b6SSteven Rostedt 35608d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 35708d636b6SSteven Rostedt /* converting nop to call */ 35808d636b6SSteven Rostedt return add_brk_on_nop(rec); 35908d636b6SSteven Rostedt 36008f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 36108d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 36208d636b6SSteven Rostedt /* converting a call to a nop */ 36308d636b6SSteven Rostedt return add_brk_on_call(rec, ftrace_addr); 36408d636b6SSteven Rostedt } 36508d636b6SSteven Rostedt return 0; 36608d636b6SSteven Rostedt } 36708d636b6SSteven Rostedt 36808d636b6SSteven Rostedt /* 36908d636b6SSteven Rostedt * On error, we need to remove breakpoints. This needs to 37008d636b6SSteven Rostedt * be done caefully. If the address does not currently have a 37108d636b6SSteven Rostedt * breakpoint, we know we are done. Otherwise, we look at the 37208d636b6SSteven Rostedt * remaining 4 bytes of the instruction. If it matches a nop 37308d636b6SSteven Rostedt * we replace the breakpoint with the nop. Otherwise we replace 37408d636b6SSteven Rostedt * it with the call instruction. 37508d636b6SSteven Rostedt */ 37608d636b6SSteven Rostedt static int remove_breakpoint(struct dyn_ftrace *rec) 37708d636b6SSteven Rostedt { 37808d636b6SSteven Rostedt unsigned char ins[MCOUNT_INSN_SIZE]; 37908d636b6SSteven Rostedt unsigned char brk = BREAKPOINT_INSTRUCTION; 38008d636b6SSteven Rostedt const unsigned char *nop; 38108d636b6SSteven Rostedt unsigned long ftrace_addr; 38208d636b6SSteven Rostedt unsigned long ip = rec->ip; 38308d636b6SSteven Rostedt 38408d636b6SSteven Rostedt /* If we fail the read, just give up */ 38508d636b6SSteven Rostedt if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE)) 38608d636b6SSteven Rostedt return -EFAULT; 38708d636b6SSteven Rostedt 38808d636b6SSteven Rostedt /* If this does not have a breakpoint, we are done */ 38908d636b6SSteven Rostedt if (ins[0] != brk) 3907f11f5ecSPetr Mladek return 0; 39108d636b6SSteven Rostedt 39208d636b6SSteven Rostedt nop = ftrace_nop_replace(); 39308d636b6SSteven Rostedt 39408d636b6SSteven Rostedt /* 39508d636b6SSteven Rostedt * If the last 4 bytes of the instruction do not match 39608d636b6SSteven Rostedt * a nop, then we assume that this is a call to ftrace_addr. 39708d636b6SSteven Rostedt */ 39808d636b6SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) { 39908d636b6SSteven Rostedt /* 40008d636b6SSteven Rostedt * For extra paranoidism, we check if the breakpoint is on 40108d636b6SSteven Rostedt * a call that would actually jump to the ftrace_addr. 40208d636b6SSteven Rostedt * If not, don't touch the breakpoint, we make just create 40308d636b6SSteven Rostedt * a disaster. 40408d636b6SSteven Rostedt */ 4057413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_new(rec); 40608f6fba5SSteven Rostedt nop = ftrace_call_replace(ip, ftrace_addr); 40708f6fba5SSteven Rostedt 40808f6fba5SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0) 40908f6fba5SSteven Rostedt goto update; 41008f6fba5SSteven Rostedt 41108f6fba5SSteven Rostedt /* Check both ftrace_addr and ftrace_old_addr */ 4127413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_curr(rec); 41308d636b6SSteven Rostedt nop = ftrace_call_replace(ip, ftrace_addr); 41408d636b6SSteven Rostedt 41508d636b6SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) 41608d636b6SSteven Rostedt return -EINVAL; 41708d636b6SSteven Rostedt } 41808d636b6SSteven Rostedt 41908f6fba5SSteven Rostedt update: 420c932c6b7SSteven Rostedt (Red Hat) return ftrace_write(ip, nop, 1); 42108d636b6SSteven Rostedt } 42208d636b6SSteven Rostedt 42308d636b6SSteven Rostedt static int add_update_code(unsigned long ip, unsigned const char *new) 42408d636b6SSteven Rostedt { 42508d636b6SSteven Rostedt /* skip breakpoint */ 42608d636b6SSteven Rostedt ip++; 42708d636b6SSteven Rostedt new++; 42892550405SSteven Rostedt (Red Hat) return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1); 42908d636b6SSteven Rostedt } 43008d636b6SSteven Rostedt 43108d636b6SSteven Rostedt static int add_update_call(struct dyn_ftrace *rec, unsigned long addr) 43208d636b6SSteven Rostedt { 43308d636b6SSteven Rostedt unsigned long ip = rec->ip; 43408d636b6SSteven Rostedt unsigned const char *new; 43508d636b6SSteven Rostedt 43608d636b6SSteven Rostedt new = ftrace_call_replace(ip, addr); 43708d636b6SSteven Rostedt return add_update_code(ip, new); 43808d636b6SSteven Rostedt } 43908d636b6SSteven Rostedt 44008d636b6SSteven Rostedt static int add_update_nop(struct dyn_ftrace *rec) 44108d636b6SSteven Rostedt { 44208d636b6SSteven Rostedt unsigned long ip = rec->ip; 44308d636b6SSteven Rostedt unsigned const char *new; 44408d636b6SSteven Rostedt 44508d636b6SSteven Rostedt new = ftrace_nop_replace(); 44608d636b6SSteven Rostedt return add_update_code(ip, new); 44708d636b6SSteven Rostedt } 44808d636b6SSteven Rostedt 44908d636b6SSteven Rostedt static int add_update(struct dyn_ftrace *rec, int enable) 45008d636b6SSteven Rostedt { 45108d636b6SSteven Rostedt unsigned long ftrace_addr; 45208d636b6SSteven Rostedt int ret; 45308d636b6SSteven Rostedt 45408d636b6SSteven Rostedt ret = ftrace_test_record(rec, enable); 45508d636b6SSteven Rostedt 4567413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_new(rec); 45708d636b6SSteven Rostedt 45808d636b6SSteven Rostedt switch (ret) { 45908d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 46008d636b6SSteven Rostedt return 0; 46108d636b6SSteven Rostedt 46208f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 46308d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 46408d636b6SSteven Rostedt /* converting nop to call */ 46508d636b6SSteven Rostedt return add_update_call(rec, ftrace_addr); 46608d636b6SSteven Rostedt 46708d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 46808d636b6SSteven Rostedt /* converting a call to a nop */ 46908d636b6SSteven Rostedt return add_update_nop(rec); 47008d636b6SSteven Rostedt } 47108d636b6SSteven Rostedt 47208d636b6SSteven Rostedt return 0; 47308d636b6SSteven Rostedt } 47408d636b6SSteven Rostedt 47508d636b6SSteven Rostedt static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr) 47608d636b6SSteven Rostedt { 47708d636b6SSteven Rostedt unsigned long ip = rec->ip; 47808d636b6SSteven Rostedt unsigned const char *new; 47908d636b6SSteven Rostedt 48008d636b6SSteven Rostedt new = ftrace_call_replace(ip, addr); 48108d636b6SSteven Rostedt 48292550405SSteven Rostedt (Red Hat) return ftrace_write(ip, new, 1); 48308d636b6SSteven Rostedt } 48408d636b6SSteven Rostedt 48508d636b6SSteven Rostedt static int finish_update_nop(struct dyn_ftrace *rec) 48608d636b6SSteven Rostedt { 48708d636b6SSteven Rostedt unsigned long ip = rec->ip; 48808d636b6SSteven Rostedt unsigned const char *new; 48908d636b6SSteven Rostedt 49008d636b6SSteven Rostedt new = ftrace_nop_replace(); 49108d636b6SSteven Rostedt 49292550405SSteven Rostedt (Red Hat) return ftrace_write(ip, new, 1); 49308d636b6SSteven Rostedt } 49408d636b6SSteven Rostedt 49508d636b6SSteven Rostedt static int finish_update(struct dyn_ftrace *rec, int enable) 49608d636b6SSteven Rostedt { 49708d636b6SSteven Rostedt unsigned long ftrace_addr; 49808d636b6SSteven Rostedt int ret; 49908d636b6SSteven Rostedt 50008d636b6SSteven Rostedt ret = ftrace_update_record(rec, enable); 50108d636b6SSteven Rostedt 5027413af1fSSteven Rostedt (Red Hat) ftrace_addr = ftrace_get_addr_new(rec); 50308d636b6SSteven Rostedt 50408d636b6SSteven Rostedt switch (ret) { 50508d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 50608d636b6SSteven Rostedt return 0; 50708d636b6SSteven Rostedt 50808f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 50908d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 51008d636b6SSteven Rostedt /* converting nop to call */ 51108d636b6SSteven Rostedt return finish_update_call(rec, ftrace_addr); 51208d636b6SSteven Rostedt 51308d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 51408d636b6SSteven Rostedt /* converting a call to a nop */ 51508d636b6SSteven Rostedt return finish_update_nop(rec); 51608d636b6SSteven Rostedt } 51708d636b6SSteven Rostedt 51808d636b6SSteven Rostedt return 0; 51908d636b6SSteven Rostedt } 52008d636b6SSteven Rostedt 52108d636b6SSteven Rostedt static void do_sync_core(void *data) 52208d636b6SSteven Rostedt { 52308d636b6SSteven Rostedt sync_core(); 52408d636b6SSteven Rostedt } 52508d636b6SSteven Rostedt 52608d636b6SSteven Rostedt static void run_sync(void) 52708d636b6SSteven Rostedt { 52808d636b6SSteven Rostedt int enable_irqs = irqs_disabled(); 52908d636b6SSteven Rostedt 53008d636b6SSteven Rostedt /* We may be called with interrupts disbled (on bootup). */ 53108d636b6SSteven Rostedt if (enable_irqs) 53208d636b6SSteven Rostedt local_irq_enable(); 53308d636b6SSteven Rostedt on_each_cpu(do_sync_core, NULL, 1); 53408d636b6SSteven Rostedt if (enable_irqs) 53508d636b6SSteven Rostedt local_irq_disable(); 53608d636b6SSteven Rostedt } 53708d636b6SSteven Rostedt 538e4f5d544SSteven Rostedt void ftrace_replace_code(int enable) 53908d636b6SSteven Rostedt { 54008d636b6SSteven Rostedt struct ftrace_rec_iter *iter; 54108d636b6SSteven Rostedt struct dyn_ftrace *rec; 54208d636b6SSteven Rostedt const char *report = "adding breakpoints"; 54308d636b6SSteven Rostedt int count = 0; 54408d636b6SSteven Rostedt int ret; 54508d636b6SSteven Rostedt 54608d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 54708d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 54808d636b6SSteven Rostedt 54908d636b6SSteven Rostedt ret = add_breakpoints(rec, enable); 55008d636b6SSteven Rostedt if (ret) 55108d636b6SSteven Rostedt goto remove_breakpoints; 55208d636b6SSteven Rostedt count++; 55308d636b6SSteven Rostedt } 55408d636b6SSteven Rostedt 55508d636b6SSteven Rostedt run_sync(); 55608d636b6SSteven Rostedt 55708d636b6SSteven Rostedt report = "updating code"; 55808d636b6SSteven Rostedt 55908d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 56008d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 56108d636b6SSteven Rostedt 56208d636b6SSteven Rostedt ret = add_update(rec, enable); 56308d636b6SSteven Rostedt if (ret) 56408d636b6SSteven Rostedt goto remove_breakpoints; 56508d636b6SSteven Rostedt } 56608d636b6SSteven Rostedt 56708d636b6SSteven Rostedt run_sync(); 56808d636b6SSteven Rostedt 56908d636b6SSteven Rostedt report = "removing breakpoints"; 57008d636b6SSteven Rostedt 57108d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 57208d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 57308d636b6SSteven Rostedt 57408d636b6SSteven Rostedt ret = finish_update(rec, enable); 57508d636b6SSteven Rostedt if (ret) 57608d636b6SSteven Rostedt goto remove_breakpoints; 57708d636b6SSteven Rostedt } 57808d636b6SSteven Rostedt 57908d636b6SSteven Rostedt run_sync(); 58008d636b6SSteven Rostedt 58108d636b6SSteven Rostedt return; 58208d636b6SSteven Rostedt 58308d636b6SSteven Rostedt remove_breakpoints: 58474bb8c45SPetr Mladek pr_warn("Failed on %s (%d):\n", report, count); 58508d636b6SSteven Rostedt ftrace_bug(ret, rec ? rec->ip : 0); 58608d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 58708d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 5887f11f5ecSPetr Mladek /* 5897f11f5ecSPetr Mladek * Breakpoints are handled only when this function is in 5907f11f5ecSPetr Mladek * progress. The system could not work with them. 5917f11f5ecSPetr Mladek */ 5927f11f5ecSPetr Mladek if (remove_breakpoint(rec)) 5937f11f5ecSPetr Mladek BUG(); 59408d636b6SSteven Rostedt } 595c932c6b7SSteven Rostedt (Red Hat) run_sync(); 59608d636b6SSteven Rostedt } 59708d636b6SSteven Rostedt 5988a4d0a68SSteven Rostedt static int 5998a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code, 6008a4d0a68SSteven Rostedt unsigned const char *new_code) 6018a4d0a68SSteven Rostedt { 6028a4d0a68SSteven Rostedt int ret; 6038a4d0a68SSteven Rostedt 6048a4d0a68SSteven Rostedt ret = add_break(ip, old_code); 6058a4d0a68SSteven Rostedt if (ret) 6068a4d0a68SSteven Rostedt goto out; 6078a4d0a68SSteven Rostedt 6088a4d0a68SSteven Rostedt run_sync(); 6098a4d0a68SSteven Rostedt 6108a4d0a68SSteven Rostedt ret = add_update_code(ip, new_code); 6118a4d0a68SSteven Rostedt if (ret) 6128a4d0a68SSteven Rostedt goto fail_update; 6138a4d0a68SSteven Rostedt 6148a4d0a68SSteven Rostedt run_sync(); 6158a4d0a68SSteven Rostedt 6168a4d0a68SSteven Rostedt ret = ftrace_write(ip, new_code, 1); 6177f11f5ecSPetr Mladek /* 6187f11f5ecSPetr Mladek * The breakpoint is handled only when this function is in progress. 6197f11f5ecSPetr Mladek * The system could not work if we could not remove it. 6207f11f5ecSPetr Mladek */ 6217f11f5ecSPetr Mladek BUG_ON(ret); 6228a4d0a68SSteven Rostedt out: 62312729f14SPetr Mladek run_sync(); 6248a4d0a68SSteven Rostedt return ret; 6258a4d0a68SSteven Rostedt 6268a4d0a68SSteven Rostedt fail_update: 6277f11f5ecSPetr Mladek /* Also here the system could not work with the breakpoint */ 6287f11f5ecSPetr Mladek if (ftrace_write(ip, old_code, 1)) 6297f11f5ecSPetr Mladek BUG(); 6308a4d0a68SSteven Rostedt goto out; 6318a4d0a68SSteven Rostedt } 6328a4d0a68SSteven Rostedt 63308d636b6SSteven Rostedt void arch_ftrace_update_code(int command) 63408d636b6SSteven Rostedt { 635a192cd04SSteven Rostedt /* See comment above by declaration of modifying_ftrace_code */ 636a192cd04SSteven Rostedt atomic_inc(&modifying_ftrace_code); 63708d636b6SSteven Rostedt 638e4f5d544SSteven Rostedt ftrace_modify_all_code(command); 63908d636b6SSteven Rostedt 640a192cd04SSteven Rostedt atomic_dec(&modifying_ftrace_code); 64108d636b6SSteven Rostedt } 64208d636b6SSteven Rostedt 6433a36cb11SJiri Slaby int __init ftrace_dyn_arch_init(void) 6443d083395SSteven Rostedt { 6453d083395SSteven Rostedt return 0; 6463d083395SSteven Rostedt } 647caf4b323SFrederic Weisbecker #endif 648e7d3737eSFrederic Weisbecker 649fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER 650e7d3737eSFrederic Weisbecker 6515a45cfe1SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 6525a45cfe1SSteven Rostedt extern void ftrace_graph_call(void); 6535a45cfe1SSteven Rostedt 65487fbb2acSSteven Rostedt (Red Hat) static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr) 6555a45cfe1SSteven Rostedt { 65687fbb2acSSteven Rostedt (Red Hat) static union ftrace_code_union calc; 6575a45cfe1SSteven Rostedt 65887fbb2acSSteven Rostedt (Red Hat) /* Jmp not a call (ignore the .e8) */ 65987fbb2acSSteven Rostedt (Red Hat) calc.e8 = 0xe9; 66087fbb2acSSteven Rostedt (Red Hat) calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); 6615a45cfe1SSteven Rostedt 66287fbb2acSSteven Rostedt (Red Hat) /* 66387fbb2acSSteven Rostedt (Red Hat) * ftrace external locks synchronize the access to the static variable. 66487fbb2acSSteven Rostedt (Red Hat) */ 66587fbb2acSSteven Rostedt (Red Hat) return calc.code; 66687fbb2acSSteven Rostedt (Red Hat) } 6675a45cfe1SSteven Rostedt 66887fbb2acSSteven Rostedt (Red Hat) static int ftrace_mod_jmp(unsigned long ip, void *func) 66987fbb2acSSteven Rostedt (Red Hat) { 67087fbb2acSSteven Rostedt (Red Hat) unsigned char *new; 6715a45cfe1SSteven Rostedt 67287fbb2acSSteven Rostedt (Red Hat) new = ftrace_jmp_replace(ip, (unsigned long)func); 6735a45cfe1SSteven Rostedt 67487fbb2acSSteven Rostedt (Red Hat) return update_ftrace_func(ip, new); 6755a45cfe1SSteven Rostedt } 6765a45cfe1SSteven Rostedt 6775a45cfe1SSteven Rostedt int ftrace_enable_ftrace_graph_caller(void) 6785a45cfe1SSteven Rostedt { 6795a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call); 6805a45cfe1SSteven Rostedt 68187fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_graph_caller); 6825a45cfe1SSteven Rostedt } 6835a45cfe1SSteven Rostedt 6845a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void) 6855a45cfe1SSteven Rostedt { 6865a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call); 6875a45cfe1SSteven Rostedt 68887fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_stub); 6895a45cfe1SSteven Rostedt } 6905a45cfe1SSteven Rostedt 691e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */ 692e7d3737eSFrederic Weisbecker 693e7d3737eSFrederic Weisbecker /* 694e7d3737eSFrederic Weisbecker * Hook the return address and push it in the stack of return addrs 695e7d3737eSFrederic Weisbecker * in current thread info. 696e7d3737eSFrederic Weisbecker */ 69771e308a2SSteven Rostedt void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, 69871e308a2SSteven Rostedt unsigned long frame_pointer) 699e7d3737eSFrederic Weisbecker { 700e7d3737eSFrederic Weisbecker unsigned long old; 701e7d3737eSFrederic Weisbecker int faulted; 702287b6e68SFrederic Weisbecker struct ftrace_graph_ent trace; 703e7d3737eSFrederic Weisbecker unsigned long return_hooker = (unsigned long) 704e7d3737eSFrederic Weisbecker &return_to_handler; 705e7d3737eSFrederic Weisbecker 706*84b2bc7fSSteven Rostedt (Red Hat) if (unlikely(ftrace_graph_is_dead())) 707*84b2bc7fSSteven Rostedt (Red Hat) return; 708*84b2bc7fSSteven Rostedt (Red Hat) 709380c4b14SFrederic Weisbecker if (unlikely(atomic_read(¤t->tracing_graph_pause))) 710e7d3737eSFrederic Weisbecker return; 711e7d3737eSFrederic Weisbecker 712e7d3737eSFrederic Weisbecker /* 713e7d3737eSFrederic Weisbecker * Protect against fault, even if it shouldn't 714e7d3737eSFrederic Weisbecker * happen. This tool is too much intrusive to 715e7d3737eSFrederic Weisbecker * ignore such a protection. 716e7d3737eSFrederic Weisbecker */ 717e7d3737eSFrederic Weisbecker asm volatile( 71896665788SSteven Rostedt "1: " _ASM_MOV " (%[parent]), %[old]\n" 71996665788SSteven Rostedt "2: " _ASM_MOV " %[return_hooker], (%[parent])\n" 720e7d3737eSFrederic Weisbecker " movl $0, %[faulted]\n" 721e3944bfaSSteven Rostedt "3:\n" 722e7d3737eSFrederic Weisbecker 723e7d3737eSFrederic Weisbecker ".section .fixup, \"ax\"\n" 724e3944bfaSSteven Rostedt "4: movl $1, %[faulted]\n" 725e3944bfaSSteven Rostedt " jmp 3b\n" 726e7d3737eSFrederic Weisbecker ".previous\n" 727e7d3737eSFrederic Weisbecker 728e3944bfaSSteven Rostedt _ASM_EXTABLE(1b, 4b) 729e3944bfaSSteven Rostedt _ASM_EXTABLE(2b, 4b) 730e7d3737eSFrederic Weisbecker 731aa512a27SSteven Rostedt : [old] "=&r" (old), [faulted] "=r" (faulted) 73296665788SSteven Rostedt : [parent] "r" (parent), [return_hooker] "r" (return_hooker) 733e7d3737eSFrederic Weisbecker : "memory" 734e7d3737eSFrederic Weisbecker ); 735e7d3737eSFrederic Weisbecker 73614a866c5SSteven Rostedt if (unlikely(faulted)) { 73714a866c5SSteven Rostedt ftrace_graph_stop(); 73814a866c5SSteven Rostedt WARN_ON(1); 739e7d3737eSFrederic Weisbecker return; 740e7d3737eSFrederic Weisbecker } 741e7d3737eSFrederic Weisbecker 742722b3c74SSteven Rostedt trace.func = self_addr; 743722b3c74SSteven Rostedt trace.depth = current->curr_ret_stack + 1; 744722b3c74SSteven Rostedt 745722b3c74SSteven Rostedt /* Only trace if the calling function expects to */ 746722b3c74SSteven Rostedt if (!ftrace_graph_entry(&trace)) { 747e7d3737eSFrederic Weisbecker *parent = old; 748287b6e68SFrederic Weisbecker return; 749287b6e68SFrederic Weisbecker } 750287b6e68SFrederic Weisbecker 751722b3c74SSteven Rostedt if (ftrace_push_return_trace(old, self_addr, &trace.depth, 752722b3c74SSteven Rostedt frame_pointer) == -EBUSY) { 753e49dc19cSSteven Rostedt *parent = old; 754722b3c74SSteven Rostedt return; 755e49dc19cSSteven Rostedt } 756e7d3737eSFrederic Weisbecker } 757fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 758