/*1* Code for replacing ftrace calls with jumps.2*3* Copyright (C) 2007-2008 Steven Rostedt <[email protected]>4* Copyright (C) 2009, 2010 DSLab, Lanzhou University, China5* Author: Wu Zhangjin <[email protected]>6*7* Thanks goes to Steven Rostedt for writing the original x86 version.8*/910#include <linux/uaccess.h>11#include <linux/init.h>12#include <linux/ftrace.h>1314#include <asm/asm.h>15#include <asm/asm-offsets.h>16#include <asm/cacheflush.h>17#include <asm/uasm.h>1819#include <asm-generic/sections.h>2021#ifdef CONFIG_DYNAMIC_FTRACE2223#define JAL 0x0c000000 /* jump & link: ip --> ra, jump to target */24#define ADDR_MASK 0x03ffffff /* op_code|addr : 31...26|25 ....0 */25#define JUMP_RANGE_MASK ((1UL << 28) - 1)2627#define INSN_NOP 0x00000000 /* nop */28#define INSN_JAL(addr) \29((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))3031static unsigned int insn_jal_ftrace_caller __read_mostly;32static unsigned int insn_lui_v1_hi16_mcount __read_mostly;33static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly;3435static inline void ftrace_dyn_arch_init_insns(void)36{37u32 *buf;38unsigned int v1;3940/* lui v1, hi16_mcount */41v1 = 3;42buf = (u32 *)&insn_lui_v1_hi16_mcount;43UASM_i_LA_mostly(&buf, v1, MCOUNT_ADDR);4445/* jal (ftrace_caller + 8), jump over the first two instruction */46buf = (u32 *)&insn_jal_ftrace_caller;47uasm_i_jal(&buf, (FTRACE_ADDR + 8) & JUMP_RANGE_MASK);4849#ifdef CONFIG_FUNCTION_GRAPH_TRACER50/* j ftrace_graph_caller */51buf = (u32 *)&insn_j_ftrace_graph_caller;52uasm_i_j(&buf, (unsigned long)ftrace_graph_caller & JUMP_RANGE_MASK);53#endif54}5556/*57* Check if the address is in kernel space58*59* Clone core_kernel_text() from kernel/extable.c, but doesn't call60* init_kernel_text() for Ftrace doesn't trace functions in init sections.61*/62static inline int in_kernel_space(unsigned long ip)63{64if (ip >= (unsigned long)_stext &&65ip <= (unsigned long)_etext)66return 1;67return 0;68}6970static int ftrace_modify_code(unsigned long ip, unsigned int new_code)71{72int faulted;7374/* *(unsigned int *)ip = new_code; */75safe_store_code(new_code, ip, faulted);7677if (unlikely(faulted))78return -EFAULT;7980flush_icache_range(ip, ip + 8);8182return 0;83}8485/*86* The details about the calling site of mcount on MIPS87*88* 1. For kernel:89*90* move at, ra91* jal _mcount --> nop92*93* 2. For modules:94*95* 2.1 For KBUILD_MCOUNT_RA_ADDRESS and CONFIG_32BIT96*97* lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)98* addiu v1, v1, low_16bit_of_mcount99* move at, ra100* move $12, ra_address101* jalr v1102* sub sp, sp, 8103* 1: offset = 5 instructions104* 2.2 For the Other situations105*106* lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)107* addiu v1, v1, low_16bit_of_mcount108* move at, ra109* jalr v1110* nop | move $12, ra_address | sub sp, sp, 8111* 1: offset = 4 instructions112*/113114#if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)115#define MCOUNT_OFFSET_INSNS 5116#else117#define MCOUNT_OFFSET_INSNS 4118#endif119#define INSN_B_1F (0x10000000 | MCOUNT_OFFSET_INSNS)120121int ftrace_make_nop(struct module *mod,122struct dyn_ftrace *rec, unsigned long addr)123{124unsigned int new;125unsigned long ip = rec->ip;126127/*128* If ip is in kernel space, no long call, otherwise, long call is129* needed.130*/131new = in_kernel_space(ip) ? INSN_NOP : INSN_B_1F;132133return ftrace_modify_code(ip, new);134}135136int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)137{138unsigned int new;139unsigned long ip = rec->ip;140141new = in_kernel_space(ip) ? insn_jal_ftrace_caller :142insn_lui_v1_hi16_mcount;143144return ftrace_modify_code(ip, new);145}146147#define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))148149int ftrace_update_ftrace_func(ftrace_func_t func)150{151unsigned int new;152153new = INSN_JAL((unsigned long)func);154155return ftrace_modify_code(FTRACE_CALL_IP, new);156}157158int __init ftrace_dyn_arch_init(void *data)159{160/* Encode the instructions when booting */161ftrace_dyn_arch_init_insns();162163/* Remove "b ftrace_stub" to ensure ftrace_caller() is executed */164ftrace_modify_code(MCOUNT_ADDR, INSN_NOP);165166/* The return code is retured via data */167*(unsigned long *)data = 0;168169return 0;170}171#endif /* CONFIG_DYNAMIC_FTRACE */172173#ifdef CONFIG_FUNCTION_GRAPH_TRACER174175#ifdef CONFIG_DYNAMIC_FTRACE176177extern void ftrace_graph_call(void);178#define FTRACE_GRAPH_CALL_IP ((unsigned long)(&ftrace_graph_call))179180int ftrace_enable_ftrace_graph_caller(void)181{182return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,183insn_j_ftrace_graph_caller);184}185186int ftrace_disable_ftrace_graph_caller(void)187{188return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);189}190191#endif /* CONFIG_DYNAMIC_FTRACE */192193#ifndef KBUILD_MCOUNT_RA_ADDRESS194195#define S_RA_SP (0xafbf << 16) /* s{d,w} ra, offset(sp) */196#define S_R_SP (0xafb0 << 16) /* s{d,w} R, offset(sp) */197#define OFFSET_MASK 0xffff /* stack offset range: 0 ~ PT_SIZE */198199unsigned long ftrace_get_parent_ra_addr(unsigned long self_ra, unsigned long200old_parent_ra, unsigned long parent_ra_addr, unsigned long fp)201{202unsigned long sp, ip, tmp;203unsigned int code;204int faulted;205206/*207* For module, move the ip from the return address after the208* instruction "lui v1, hi_16bit_of_mcount"(offset is 24), but for209* kernel, move after the instruction "move ra, at"(offset is 16)210*/211ip = self_ra - (in_kernel_space(self_ra) ? 16 : 24);212213/*214* search the text until finding the non-store instruction or "s{d,w}215* ra, offset(sp)" instruction216*/217do {218/* get the code at "ip": code = *(unsigned int *)ip; */219safe_load_code(code, ip, faulted);220221if (unlikely(faulted))222return 0;223/*224* If we hit the non-store instruction before finding where the225* ra is stored, then this is a leaf function and it does not226* store the ra on the stack227*/228if ((code & S_R_SP) != S_R_SP)229return parent_ra_addr;230231/* Move to the next instruction */232ip -= 4;233} while ((code & S_RA_SP) != S_RA_SP);234235sp = fp + (code & OFFSET_MASK);236237/* tmp = *(unsigned long *)sp; */238safe_load_stack(tmp, sp, faulted);239if (unlikely(faulted))240return 0;241242if (tmp == old_parent_ra)243return sp;244return 0;245}246247#endif /* !KBUILD_MCOUNT_RA_ADDRESS */248249/*250* Hook the return address and push it in the stack of return addrs251* in current thread info.252*/253void prepare_ftrace_return(unsigned long *parent_ra_addr, unsigned long self_ra,254unsigned long fp)255{256unsigned long old_parent_ra;257struct ftrace_graph_ent trace;258unsigned long return_hooker = (unsigned long)259&return_to_handler;260int faulted, insns;261262if (unlikely(atomic_read(¤t->tracing_graph_pause)))263return;264265/*266* "parent_ra_addr" is the stack address saved the return address of267* the caller of _mcount.268*269* if the gcc < 4.5, a leaf function does not save the return address270* in the stack address, so, we "emulate" one in _mcount's stack space,271* and hijack it directly, but for a non-leaf function, it save the272* return address to the its own stack space, we can not hijack it273* directly, but need to find the real stack address,274* ftrace_get_parent_addr() does it!275*276* if gcc>= 4.5, with the new -mmcount-ra-address option, for a277* non-leaf function, the location of the return address will be saved278* to $12 for us, and for a leaf function, only put a zero into $12. we279* do it in ftrace_graph_caller of mcount.S.280*/281282/* old_parent_ra = *parent_ra_addr; */283safe_load_stack(old_parent_ra, parent_ra_addr, faulted);284if (unlikely(faulted))285goto out;286#ifndef KBUILD_MCOUNT_RA_ADDRESS287parent_ra_addr = (unsigned long *)ftrace_get_parent_ra_addr(self_ra,288old_parent_ra, (unsigned long)parent_ra_addr, fp);289/*290* If fails when getting the stack address of the non-leaf function's291* ra, stop function graph tracer and return292*/293if (parent_ra_addr == 0)294goto out;295#endif296/* *parent_ra_addr = return_hooker; */297safe_store_stack(return_hooker, parent_ra_addr, faulted);298if (unlikely(faulted))299goto out;300301if (ftrace_push_return_trace(old_parent_ra, self_ra, &trace.depth, fp)302== -EBUSY) {303*parent_ra_addr = old_parent_ra;304return;305}306307/*308* Get the recorded ip of the current mcount calling site in the309* __mcount_loc section, which will be used to filter the function310* entries configured through the tracing/set_graph_function interface.311*/312313insns = in_kernel_space(self_ra) ? 2 : MCOUNT_OFFSET_INSNS + 1;314trace.func = self_ra - (MCOUNT_INSN_SIZE * insns);315316/* Only trace if the calling function expects to */317if (!ftrace_graph_entry(&trace)) {318current->curr_ret_stack--;319*parent_ra_addr = old_parent_ra;320}321return;322out:323ftrace_graph_stop();324WARN_ON(1);325}326#endif /* CONFIG_FUNCTION_GRAPH_TRACER */327328329