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 { 30008d636b6SSteven Rostedt /* 30108d636b6SSteven Rostedt * On x86_64, kernel text mappings are mapped read-only with 30208d636b6SSteven Rostedt * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead 30308d636b6SSteven Rostedt * of the kernel text mapping to modify the kernel text. 30408d636b6SSteven Rostedt * 30508d636b6SSteven Rostedt * For 32bit kernels, these mappings are same and we can use 30608d636b6SSteven Rostedt * kernel identity mapping to modify code. 30708d636b6SSteven Rostedt */ 30808d636b6SSteven Rostedt if (within(ip, (unsigned long)_text, (unsigned long)_etext)) 309217f155eSAlexander Duyck ip = (unsigned long)__va(__pa_symbol(ip)); 31008d636b6SSteven Rostedt 31108d636b6SSteven Rostedt return probe_kernel_write((void *)ip, val, size); 31208d636b6SSteven Rostedt } 31308d636b6SSteven Rostedt 31408d636b6SSteven Rostedt static int add_break(unsigned long ip, const char *old) 31508d636b6SSteven Rostedt { 31608d636b6SSteven Rostedt unsigned char replaced[MCOUNT_INSN_SIZE]; 31708d636b6SSteven Rostedt unsigned char brk = BREAKPOINT_INSTRUCTION; 31808d636b6SSteven Rostedt 31908d636b6SSteven Rostedt if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE)) 32008d636b6SSteven Rostedt return -EFAULT; 32108d636b6SSteven Rostedt 32208d636b6SSteven Rostedt /* Make sure it is what we expect it to be */ 32308d636b6SSteven Rostedt if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0) 32408d636b6SSteven Rostedt return -EINVAL; 32508d636b6SSteven Rostedt 32608d636b6SSteven Rostedt if (ftrace_write(ip, &brk, 1)) 32708d636b6SSteven Rostedt return -EPERM; 32808d636b6SSteven Rostedt 32908d636b6SSteven Rostedt return 0; 33008d636b6SSteven Rostedt } 33108d636b6SSteven Rostedt 33208d636b6SSteven Rostedt static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr) 33308d636b6SSteven Rostedt { 33408d636b6SSteven Rostedt unsigned const char *old; 33508d636b6SSteven Rostedt unsigned long ip = rec->ip; 33608d636b6SSteven Rostedt 33708d636b6SSteven Rostedt old = ftrace_call_replace(ip, addr); 33808d636b6SSteven Rostedt 33908d636b6SSteven Rostedt return add_break(rec->ip, old); 34008d636b6SSteven Rostedt } 34108d636b6SSteven Rostedt 34208d636b6SSteven Rostedt 34308d636b6SSteven Rostedt static int add_brk_on_nop(struct dyn_ftrace *rec) 34408d636b6SSteven Rostedt { 34508d636b6SSteven Rostedt unsigned const char *old; 34608d636b6SSteven Rostedt 34708d636b6SSteven Rostedt old = ftrace_nop_replace(); 34808d636b6SSteven Rostedt 34908d636b6SSteven Rostedt return add_break(rec->ip, old); 35008d636b6SSteven Rostedt } 35108d636b6SSteven Rostedt 35208f6fba5SSteven Rostedt /* 35308f6fba5SSteven Rostedt * If the record has the FTRACE_FL_REGS set, that means that it 35408f6fba5SSteven Rostedt * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS 35508f6fba5SSteven Rostedt * is not not set, then it wants to convert to the normal callback. 35608f6fba5SSteven Rostedt */ 35708f6fba5SSteven Rostedt static unsigned long get_ftrace_addr(struct dyn_ftrace *rec) 35808f6fba5SSteven Rostedt { 35908f6fba5SSteven Rostedt if (rec->flags & FTRACE_FL_REGS) 36008f6fba5SSteven Rostedt return (unsigned long)FTRACE_REGS_ADDR; 36108f6fba5SSteven Rostedt else 36208f6fba5SSteven Rostedt return (unsigned long)FTRACE_ADDR; 36308f6fba5SSteven Rostedt } 36408f6fba5SSteven Rostedt 36508f6fba5SSteven Rostedt /* 36608f6fba5SSteven Rostedt * The FTRACE_FL_REGS_EN is set when the record already points to 36708f6fba5SSteven Rostedt * a function that saves all the regs. Basically the '_EN' version 36808f6fba5SSteven Rostedt * represents the current state of the function. 36908f6fba5SSteven Rostedt */ 37008f6fba5SSteven Rostedt static unsigned long get_ftrace_old_addr(struct dyn_ftrace *rec) 37108f6fba5SSteven Rostedt { 37208f6fba5SSteven Rostedt if (rec->flags & FTRACE_FL_REGS_EN) 37308f6fba5SSteven Rostedt return (unsigned long)FTRACE_REGS_ADDR; 37408f6fba5SSteven Rostedt else 37508f6fba5SSteven Rostedt return (unsigned long)FTRACE_ADDR; 37608f6fba5SSteven Rostedt } 37708f6fba5SSteven Rostedt 37808d636b6SSteven Rostedt static int add_breakpoints(struct dyn_ftrace *rec, int enable) 37908d636b6SSteven Rostedt { 38008d636b6SSteven Rostedt unsigned long ftrace_addr; 38108d636b6SSteven Rostedt int ret; 38208d636b6SSteven Rostedt 38308d636b6SSteven Rostedt ret = ftrace_test_record(rec, enable); 38408d636b6SSteven Rostedt 38508f6fba5SSteven Rostedt ftrace_addr = get_ftrace_addr(rec); 38608d636b6SSteven Rostedt 38708d636b6SSteven Rostedt switch (ret) { 38808d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 38908d636b6SSteven Rostedt return 0; 39008d636b6SSteven Rostedt 39108d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 39208d636b6SSteven Rostedt /* converting nop to call */ 39308d636b6SSteven Rostedt return add_brk_on_nop(rec); 39408d636b6SSteven Rostedt 39508f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL_REGS: 39608f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 39708f6fba5SSteven Rostedt ftrace_addr = get_ftrace_old_addr(rec); 39808f6fba5SSteven Rostedt /* fall through */ 39908d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 40008d636b6SSteven Rostedt /* converting a call to a nop */ 40108d636b6SSteven Rostedt return add_brk_on_call(rec, ftrace_addr); 40208d636b6SSteven Rostedt } 40308d636b6SSteven Rostedt return 0; 40408d636b6SSteven Rostedt } 40508d636b6SSteven Rostedt 40608d636b6SSteven Rostedt /* 40708d636b6SSteven Rostedt * On error, we need to remove breakpoints. This needs to 40808d636b6SSteven Rostedt * be done caefully. If the address does not currently have a 40908d636b6SSteven Rostedt * breakpoint, we know we are done. Otherwise, we look at the 41008d636b6SSteven Rostedt * remaining 4 bytes of the instruction. If it matches a nop 41108d636b6SSteven Rostedt * we replace the breakpoint with the nop. Otherwise we replace 41208d636b6SSteven Rostedt * it with the call instruction. 41308d636b6SSteven Rostedt */ 41408d636b6SSteven Rostedt static int remove_breakpoint(struct dyn_ftrace *rec) 41508d636b6SSteven Rostedt { 41608d636b6SSteven Rostedt unsigned char ins[MCOUNT_INSN_SIZE]; 41708d636b6SSteven Rostedt unsigned char brk = BREAKPOINT_INSTRUCTION; 41808d636b6SSteven Rostedt const unsigned char *nop; 41908d636b6SSteven Rostedt unsigned long ftrace_addr; 42008d636b6SSteven Rostedt unsigned long ip = rec->ip; 42108d636b6SSteven Rostedt 42208d636b6SSteven Rostedt /* If we fail the read, just give up */ 42308d636b6SSteven Rostedt if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE)) 42408d636b6SSteven Rostedt return -EFAULT; 42508d636b6SSteven Rostedt 42608d636b6SSteven Rostedt /* If this does not have a breakpoint, we are done */ 42708d636b6SSteven Rostedt if (ins[0] != brk) 42808d636b6SSteven Rostedt return -1; 42908d636b6SSteven Rostedt 43008d636b6SSteven Rostedt nop = ftrace_nop_replace(); 43108d636b6SSteven Rostedt 43208d636b6SSteven Rostedt /* 43308d636b6SSteven Rostedt * If the last 4 bytes of the instruction do not match 43408d636b6SSteven Rostedt * a nop, then we assume that this is a call to ftrace_addr. 43508d636b6SSteven Rostedt */ 43608d636b6SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) { 43708d636b6SSteven Rostedt /* 43808d636b6SSteven Rostedt * For extra paranoidism, we check if the breakpoint is on 43908d636b6SSteven Rostedt * a call that would actually jump to the ftrace_addr. 44008d636b6SSteven Rostedt * If not, don't touch the breakpoint, we make just create 44108d636b6SSteven Rostedt * a disaster. 44208d636b6SSteven Rostedt */ 44308f6fba5SSteven Rostedt ftrace_addr = get_ftrace_addr(rec); 44408f6fba5SSteven Rostedt nop = ftrace_call_replace(ip, ftrace_addr); 44508f6fba5SSteven Rostedt 44608f6fba5SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0) 44708f6fba5SSteven Rostedt goto update; 44808f6fba5SSteven Rostedt 44908f6fba5SSteven Rostedt /* Check both ftrace_addr and ftrace_old_addr */ 45008f6fba5SSteven Rostedt ftrace_addr = get_ftrace_old_addr(rec); 45108d636b6SSteven Rostedt nop = ftrace_call_replace(ip, ftrace_addr); 45208d636b6SSteven Rostedt 45308d636b6SSteven Rostedt if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) 45408d636b6SSteven Rostedt return -EINVAL; 45508d636b6SSteven Rostedt } 45608d636b6SSteven Rostedt 45708f6fba5SSteven Rostedt update: 458*c932c6b7SSteven Rostedt (Red Hat) return ftrace_write(ip, nop, 1); 45908d636b6SSteven Rostedt } 46008d636b6SSteven Rostedt 46108d636b6SSteven Rostedt static int add_update_code(unsigned long ip, unsigned const char *new) 46208d636b6SSteven Rostedt { 46308d636b6SSteven Rostedt /* skip breakpoint */ 46408d636b6SSteven Rostedt ip++; 46508d636b6SSteven Rostedt new++; 46608d636b6SSteven Rostedt if (ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1)) 46708d636b6SSteven Rostedt return -EPERM; 46808d636b6SSteven Rostedt return 0; 46908d636b6SSteven Rostedt } 47008d636b6SSteven Rostedt 47108d636b6SSteven Rostedt static int add_update_call(struct dyn_ftrace *rec, unsigned long addr) 47208d636b6SSteven Rostedt { 47308d636b6SSteven Rostedt unsigned long ip = rec->ip; 47408d636b6SSteven Rostedt unsigned const char *new; 47508d636b6SSteven Rostedt 47608d636b6SSteven Rostedt new = ftrace_call_replace(ip, addr); 47708d636b6SSteven Rostedt return add_update_code(ip, new); 47808d636b6SSteven Rostedt } 47908d636b6SSteven Rostedt 48008d636b6SSteven Rostedt static int add_update_nop(struct dyn_ftrace *rec) 48108d636b6SSteven Rostedt { 48208d636b6SSteven Rostedt unsigned long ip = rec->ip; 48308d636b6SSteven Rostedt unsigned const char *new; 48408d636b6SSteven Rostedt 48508d636b6SSteven Rostedt new = ftrace_nop_replace(); 48608d636b6SSteven Rostedt return add_update_code(ip, new); 48708d636b6SSteven Rostedt } 48808d636b6SSteven Rostedt 48908d636b6SSteven Rostedt static int add_update(struct dyn_ftrace *rec, int enable) 49008d636b6SSteven Rostedt { 49108d636b6SSteven Rostedt unsigned long ftrace_addr; 49208d636b6SSteven Rostedt int ret; 49308d636b6SSteven Rostedt 49408d636b6SSteven Rostedt ret = ftrace_test_record(rec, enable); 49508d636b6SSteven Rostedt 49608f6fba5SSteven Rostedt ftrace_addr = get_ftrace_addr(rec); 49708d636b6SSteven Rostedt 49808d636b6SSteven Rostedt switch (ret) { 49908d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 50008d636b6SSteven Rostedt return 0; 50108d636b6SSteven Rostedt 50208f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL_REGS: 50308f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 50408d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 50508d636b6SSteven Rostedt /* converting nop to call */ 50608d636b6SSteven Rostedt return add_update_call(rec, ftrace_addr); 50708d636b6SSteven Rostedt 50808d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 50908d636b6SSteven Rostedt /* converting a call to a nop */ 51008d636b6SSteven Rostedt return add_update_nop(rec); 51108d636b6SSteven Rostedt } 51208d636b6SSteven Rostedt 51308d636b6SSteven Rostedt return 0; 51408d636b6SSteven Rostedt } 51508d636b6SSteven Rostedt 51608d636b6SSteven Rostedt static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr) 51708d636b6SSteven Rostedt { 51808d636b6SSteven Rostedt unsigned long ip = rec->ip; 51908d636b6SSteven Rostedt unsigned const char *new; 52008d636b6SSteven Rostedt 52108d636b6SSteven Rostedt new = ftrace_call_replace(ip, addr); 52208d636b6SSteven Rostedt 52308d636b6SSteven Rostedt if (ftrace_write(ip, new, 1)) 52408d636b6SSteven Rostedt return -EPERM; 52508d636b6SSteven Rostedt 52608d636b6SSteven Rostedt return 0; 52708d636b6SSteven Rostedt } 52808d636b6SSteven Rostedt 52908d636b6SSteven Rostedt static int finish_update_nop(struct dyn_ftrace *rec) 53008d636b6SSteven Rostedt { 53108d636b6SSteven Rostedt unsigned long ip = rec->ip; 53208d636b6SSteven Rostedt unsigned const char *new; 53308d636b6SSteven Rostedt 53408d636b6SSteven Rostedt new = ftrace_nop_replace(); 53508d636b6SSteven Rostedt 53608d636b6SSteven Rostedt if (ftrace_write(ip, new, 1)) 53708d636b6SSteven Rostedt return -EPERM; 53808d636b6SSteven Rostedt return 0; 53908d636b6SSteven Rostedt } 54008d636b6SSteven Rostedt 54108d636b6SSteven Rostedt static int finish_update(struct dyn_ftrace *rec, int enable) 54208d636b6SSteven Rostedt { 54308d636b6SSteven Rostedt unsigned long ftrace_addr; 54408d636b6SSteven Rostedt int ret; 54508d636b6SSteven Rostedt 54608d636b6SSteven Rostedt ret = ftrace_update_record(rec, enable); 54708d636b6SSteven Rostedt 54808f6fba5SSteven Rostedt ftrace_addr = get_ftrace_addr(rec); 54908d636b6SSteven Rostedt 55008d636b6SSteven Rostedt switch (ret) { 55108d636b6SSteven Rostedt case FTRACE_UPDATE_IGNORE: 55208d636b6SSteven Rostedt return 0; 55308d636b6SSteven Rostedt 55408f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL_REGS: 55508f6fba5SSteven Rostedt case FTRACE_UPDATE_MODIFY_CALL: 55608d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_CALL: 55708d636b6SSteven Rostedt /* converting nop to call */ 55808d636b6SSteven Rostedt return finish_update_call(rec, ftrace_addr); 55908d636b6SSteven Rostedt 56008d636b6SSteven Rostedt case FTRACE_UPDATE_MAKE_NOP: 56108d636b6SSteven Rostedt /* converting a call to a nop */ 56208d636b6SSteven Rostedt return finish_update_nop(rec); 56308d636b6SSteven Rostedt } 56408d636b6SSteven Rostedt 56508d636b6SSteven Rostedt return 0; 56608d636b6SSteven Rostedt } 56708d636b6SSteven Rostedt 56808d636b6SSteven Rostedt static void do_sync_core(void *data) 56908d636b6SSteven Rostedt { 57008d636b6SSteven Rostedt sync_core(); 57108d636b6SSteven Rostedt } 57208d636b6SSteven Rostedt 57308d636b6SSteven Rostedt static void run_sync(void) 57408d636b6SSteven Rostedt { 57508d636b6SSteven Rostedt int enable_irqs = irqs_disabled(); 57608d636b6SSteven Rostedt 57708d636b6SSteven Rostedt /* We may be called with interrupts disbled (on bootup). */ 57808d636b6SSteven Rostedt if (enable_irqs) 57908d636b6SSteven Rostedt local_irq_enable(); 58008d636b6SSteven Rostedt on_each_cpu(do_sync_core, NULL, 1); 58108d636b6SSteven Rostedt if (enable_irqs) 58208d636b6SSteven Rostedt local_irq_disable(); 58308d636b6SSteven Rostedt } 58408d636b6SSteven Rostedt 585e4f5d544SSteven Rostedt void ftrace_replace_code(int enable) 58608d636b6SSteven Rostedt { 58708d636b6SSteven Rostedt struct ftrace_rec_iter *iter; 58808d636b6SSteven Rostedt struct dyn_ftrace *rec; 58908d636b6SSteven Rostedt const char *report = "adding breakpoints"; 59008d636b6SSteven Rostedt int count = 0; 59108d636b6SSteven Rostedt int ret; 59208d636b6SSteven Rostedt 59308d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 59408d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 59508d636b6SSteven Rostedt 59608d636b6SSteven Rostedt ret = add_breakpoints(rec, enable); 59708d636b6SSteven Rostedt if (ret) 59808d636b6SSteven Rostedt goto remove_breakpoints; 59908d636b6SSteven Rostedt count++; 60008d636b6SSteven Rostedt } 60108d636b6SSteven Rostedt 60208d636b6SSteven Rostedt run_sync(); 60308d636b6SSteven Rostedt 60408d636b6SSteven Rostedt report = "updating code"; 60508d636b6SSteven Rostedt 60608d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 60708d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 60808d636b6SSteven Rostedt 60908d636b6SSteven Rostedt ret = add_update(rec, enable); 61008d636b6SSteven Rostedt if (ret) 61108d636b6SSteven Rostedt goto remove_breakpoints; 61208d636b6SSteven Rostedt } 61308d636b6SSteven Rostedt 61408d636b6SSteven Rostedt run_sync(); 61508d636b6SSteven Rostedt 61608d636b6SSteven Rostedt report = "removing breakpoints"; 61708d636b6SSteven Rostedt 61808d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 61908d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 62008d636b6SSteven Rostedt 62108d636b6SSteven Rostedt ret = finish_update(rec, enable); 62208d636b6SSteven Rostedt if (ret) 62308d636b6SSteven Rostedt goto remove_breakpoints; 62408d636b6SSteven Rostedt } 62508d636b6SSteven Rostedt 62608d636b6SSteven Rostedt run_sync(); 62708d636b6SSteven Rostedt 62808d636b6SSteven Rostedt return; 62908d636b6SSteven Rostedt 63008d636b6SSteven Rostedt remove_breakpoints: 63108d636b6SSteven Rostedt ftrace_bug(ret, rec ? rec->ip : 0); 63208d636b6SSteven Rostedt printk(KERN_WARNING "Failed on %s (%d):\n", report, count); 63308d636b6SSteven Rostedt for_ftrace_rec_iter(iter) { 63408d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter); 63508d636b6SSteven Rostedt remove_breakpoint(rec); 63608d636b6SSteven Rostedt } 637*c932c6b7SSteven Rostedt (Red Hat) run_sync(); 63808d636b6SSteven Rostedt } 63908d636b6SSteven Rostedt 6408a4d0a68SSteven Rostedt static int 6418a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code, 6428a4d0a68SSteven Rostedt unsigned const char *new_code) 6438a4d0a68SSteven Rostedt { 6448a4d0a68SSteven Rostedt int ret; 6458a4d0a68SSteven Rostedt 6468a4d0a68SSteven Rostedt ret = add_break(ip, old_code); 6478a4d0a68SSteven Rostedt if (ret) 6488a4d0a68SSteven Rostedt goto out; 6498a4d0a68SSteven Rostedt 6508a4d0a68SSteven Rostedt run_sync(); 6518a4d0a68SSteven Rostedt 6528a4d0a68SSteven Rostedt ret = add_update_code(ip, new_code); 6538a4d0a68SSteven Rostedt if (ret) 6548a4d0a68SSteven Rostedt goto fail_update; 6558a4d0a68SSteven Rostedt 6568a4d0a68SSteven Rostedt run_sync(); 6578a4d0a68SSteven Rostedt 6588a4d0a68SSteven Rostedt ret = ftrace_write(ip, new_code, 1); 6598a4d0a68SSteven Rostedt if (ret) { 6608a4d0a68SSteven Rostedt ret = -EPERM; 6618a4d0a68SSteven Rostedt goto out; 6628a4d0a68SSteven Rostedt } 6638a4d0a68SSteven Rostedt run_sync(); 6648a4d0a68SSteven Rostedt out: 6658a4d0a68SSteven Rostedt return ret; 6668a4d0a68SSteven Rostedt 6678a4d0a68SSteven Rostedt fail_update: 668*c932c6b7SSteven Rostedt (Red Hat) ftrace_write(ip, old_code, 1); 6698a4d0a68SSteven Rostedt goto out; 6708a4d0a68SSteven Rostedt } 6718a4d0a68SSteven Rostedt 67208d636b6SSteven Rostedt void arch_ftrace_update_code(int command) 67308d636b6SSteven Rostedt { 674a192cd04SSteven Rostedt /* See comment above by declaration of modifying_ftrace_code */ 675a192cd04SSteven Rostedt atomic_inc(&modifying_ftrace_code); 67608d636b6SSteven Rostedt 677e4f5d544SSteven Rostedt ftrace_modify_all_code(command); 67808d636b6SSteven Rostedt 679a192cd04SSteven Rostedt atomic_dec(&modifying_ftrace_code); 68008d636b6SSteven Rostedt } 68108d636b6SSteven Rostedt 682d61f82d0SSteven Rostedt int __init ftrace_dyn_arch_init(void *data) 6833d083395SSteven Rostedt { 684732f3ca7SSteven Rostedt /* The return code is retured via data */ 685732f3ca7SSteven Rostedt *(unsigned long *)data = 0; 686dfa60abaSSteven Rostedt 6873d083395SSteven Rostedt return 0; 6883d083395SSteven Rostedt } 689caf4b323SFrederic Weisbecker #endif 690e7d3737eSFrederic Weisbecker 691fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER 692e7d3737eSFrederic Weisbecker 6935a45cfe1SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 6945a45cfe1SSteven Rostedt extern void ftrace_graph_call(void); 6955a45cfe1SSteven Rostedt 69687fbb2acSSteven Rostedt (Red Hat) static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr) 6975a45cfe1SSteven Rostedt { 69887fbb2acSSteven Rostedt (Red Hat) static union ftrace_code_union calc; 6995a45cfe1SSteven Rostedt 70087fbb2acSSteven Rostedt (Red Hat) /* Jmp not a call (ignore the .e8) */ 70187fbb2acSSteven Rostedt (Red Hat) calc.e8 = 0xe9; 70287fbb2acSSteven Rostedt (Red Hat) calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr); 7035a45cfe1SSteven Rostedt 70487fbb2acSSteven Rostedt (Red Hat) /* 70587fbb2acSSteven Rostedt (Red Hat) * ftrace external locks synchronize the access to the static variable. 70687fbb2acSSteven Rostedt (Red Hat) */ 70787fbb2acSSteven Rostedt (Red Hat) return calc.code; 70887fbb2acSSteven Rostedt (Red Hat) } 7095a45cfe1SSteven Rostedt 71087fbb2acSSteven Rostedt (Red Hat) static int ftrace_mod_jmp(unsigned long ip, void *func) 71187fbb2acSSteven Rostedt (Red Hat) { 71287fbb2acSSteven Rostedt (Red Hat) unsigned char *new; 7135a45cfe1SSteven Rostedt 71487fbb2acSSteven Rostedt (Red Hat) new = ftrace_jmp_replace(ip, (unsigned long)func); 7155a45cfe1SSteven Rostedt 71687fbb2acSSteven Rostedt (Red Hat) return update_ftrace_func(ip, new); 7175a45cfe1SSteven Rostedt } 7185a45cfe1SSteven Rostedt 7195a45cfe1SSteven Rostedt int ftrace_enable_ftrace_graph_caller(void) 7205a45cfe1SSteven Rostedt { 7215a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call); 7225a45cfe1SSteven Rostedt 72387fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_graph_caller); 7245a45cfe1SSteven Rostedt } 7255a45cfe1SSteven Rostedt 7265a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void) 7275a45cfe1SSteven Rostedt { 7285a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call); 7295a45cfe1SSteven Rostedt 73087fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_stub); 7315a45cfe1SSteven Rostedt } 7325a45cfe1SSteven Rostedt 733e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */ 734e7d3737eSFrederic Weisbecker 735e7d3737eSFrederic Weisbecker /* 736e7d3737eSFrederic Weisbecker * Hook the return address and push it in the stack of return addrs 737e7d3737eSFrederic Weisbecker * in current thread info. 738e7d3737eSFrederic Weisbecker */ 73971e308a2SSteven Rostedt void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr, 74071e308a2SSteven Rostedt unsigned long frame_pointer) 741e7d3737eSFrederic Weisbecker { 742e7d3737eSFrederic Weisbecker unsigned long old; 743e7d3737eSFrederic Weisbecker int faulted; 744287b6e68SFrederic Weisbecker struct ftrace_graph_ent trace; 745e7d3737eSFrederic Weisbecker unsigned long return_hooker = (unsigned long) 746e7d3737eSFrederic Weisbecker &return_to_handler; 747e7d3737eSFrederic Weisbecker 748380c4b14SFrederic Weisbecker if (unlikely(atomic_read(¤t->tracing_graph_pause))) 749e7d3737eSFrederic Weisbecker return; 750e7d3737eSFrederic Weisbecker 751e7d3737eSFrederic Weisbecker /* 752e7d3737eSFrederic Weisbecker * Protect against fault, even if it shouldn't 753e7d3737eSFrederic Weisbecker * happen. This tool is too much intrusive to 754e7d3737eSFrederic Weisbecker * ignore such a protection. 755e7d3737eSFrederic Weisbecker */ 756e7d3737eSFrederic Weisbecker asm volatile( 75796665788SSteven Rostedt "1: " _ASM_MOV " (%[parent]), %[old]\n" 75896665788SSteven Rostedt "2: " _ASM_MOV " %[return_hooker], (%[parent])\n" 759e7d3737eSFrederic Weisbecker " movl $0, %[faulted]\n" 760e3944bfaSSteven Rostedt "3:\n" 761e7d3737eSFrederic Weisbecker 762e7d3737eSFrederic Weisbecker ".section .fixup, \"ax\"\n" 763e3944bfaSSteven Rostedt "4: movl $1, %[faulted]\n" 764e3944bfaSSteven Rostedt " jmp 3b\n" 765e7d3737eSFrederic Weisbecker ".previous\n" 766e7d3737eSFrederic Weisbecker 767e3944bfaSSteven Rostedt _ASM_EXTABLE(1b, 4b) 768e3944bfaSSteven Rostedt _ASM_EXTABLE(2b, 4b) 769e7d3737eSFrederic Weisbecker 770aa512a27SSteven Rostedt : [old] "=&r" (old), [faulted] "=r" (faulted) 77196665788SSteven Rostedt : [parent] "r" (parent), [return_hooker] "r" (return_hooker) 772e7d3737eSFrederic Weisbecker : "memory" 773e7d3737eSFrederic Weisbecker ); 774e7d3737eSFrederic Weisbecker 77514a866c5SSteven Rostedt if (unlikely(faulted)) { 77614a866c5SSteven Rostedt ftrace_graph_stop(); 77714a866c5SSteven Rostedt WARN_ON(1); 778e7d3737eSFrederic Weisbecker return; 779e7d3737eSFrederic Weisbecker } 780e7d3737eSFrederic Weisbecker 781722b3c74SSteven Rostedt trace.func = self_addr; 782722b3c74SSteven Rostedt trace.depth = current->curr_ret_stack + 1; 783722b3c74SSteven Rostedt 784722b3c74SSteven Rostedt /* Only trace if the calling function expects to */ 785722b3c74SSteven Rostedt if (!ftrace_graph_entry(&trace)) { 786e7d3737eSFrederic Weisbecker *parent = old; 787287b6e68SFrederic Weisbecker return; 788287b6e68SFrederic Weisbecker } 789287b6e68SFrederic Weisbecker 790722b3c74SSteven Rostedt if (ftrace_push_return_trace(old, self_addr, &trace.depth, 791722b3c74SSteven Rostedt frame_pointer) == -EBUSY) { 792e49dc19cSSteven Rostedt *parent = old; 793722b3c74SSteven Rostedt return; 794e49dc19cSSteven Rostedt } 795e7d3737eSFrederic Weisbecker } 796fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 797