// SPDX-License-Identifier: GPL-2.0 /* * Code for replacing ftrace calls with jumps. * * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> * * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box. * * Added function graph tracer code, taken from x86 that was written * by Frederic Weisbecker, and ported to PPC by Steven Rostedt. *
*/
staticunsignedlong find_ftrace_tramp(unsignedlong ip)
{ int i;
for (i = 0; i < NUM_FTRACE_TRAMPS; i++) if (!ftrace_tramps[i]) continue; elseif (is_offset_in_branch_range(ftrace_tramps[i] - ip)) return ftrace_tramps[i];
if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE))
ip = ftrace_get_ool_stub(rec) + MCOUNT_INSN_SIZE; /* second instruction in stub */ else
ip = rec->ip;
if (!is_offset_in_branch_range(addr - ip) && addr != FTRACE_ADDR &&
addr != FTRACE_REGS_ADDR) { /* This can only happen with ftrace direct */ if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)) {
pr_err("0x%lx (0x%lx): Unexpected target address 0x%lx\n",
ip, rec->ip, addr); return -EINVAL;
}
addr = FTRACE_ADDR;
}
if (is_offset_in_branch_range(addr - ip)) /* Within range */
stub = addr; elseif (core_kernel_text(ip)) /* We would be branching to one of our ftrace stubs */
stub = find_ftrace_tramp(ip); else
stub = ftrace_lookup_module_stub(ip, addr);
if (!stub) {
pr_err("0x%lx (0x%lx): No ftrace stubs reachable\n", ip, rec->ip); return -EINVAL;
}
staticint ftrace_init_ool_stub(struct module *mod, struct dyn_ftrace *rec)
{ #ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE staticint ool_stub_text_index, ool_stub_text_end_index, ool_stub_inittext_index; int ret = 0, ool_stub_count, *ool_stub_index;
ppc_inst_t inst; /* * See ftrace_entry.S if changing the below instruction sequence, as we rely on * decoding the last branch instruction here to recover the correct function ip.
*/ struct ftrace_ool_stub *ool_stub, ool_stub_template = {
.insn = {
PPC_RAW_MFLR(_R0),
PPC_RAW_NOP(), /* bl ftrace_caller */
PPC_RAW_MTLR(_R0),
PPC_RAW_NOP() /* b rec->ip + 4 */
}
};
WARN_ON(rec->arch.ool_stub);
if (is_kernel_inittext(rec->ip)) {
ool_stub = ftrace_ool_stub_inittext;
ool_stub_index = &ool_stub_inittext_index;
ool_stub_count = ftrace_ool_stub_inittext_count;
} elseif (is_kernel_text(rec->ip)) { /* * ftrace records are sorted, so we first use up the stub area within .text * (ftrace_ool_stub_text) before using the area at the end of .text * (ftrace_ool_stub_text_end), unless the stub is out of range of the record.
*/ if (ool_stub_text_index >= ftrace_ool_stub_text_count ||
!is_offset_in_branch_range((long)rec->ip -
(long)&ftrace_ool_stub_text[ool_stub_text_index])) {
ool_stub = ftrace_ool_stub_text_end;
ool_stub_index = &ool_stub_text_end_index;
ool_stub_count = ftrace_ool_stub_text_end_count;
} else {
ool_stub = ftrace_ool_stub_text;
ool_stub_index = &ool_stub_text_index;
ool_stub_count = ftrace_ool_stub_text_count;
} #ifdef CONFIG_MODULES
} elseif (mod) {
ool_stub = mod->arch.ool_stubs;
ool_stub_index = &mod->arch.ool_stub_index;
ool_stub_count = mod->arch.ool_stub_count; #endif
} else { return -EINVAL;
}
ool_stub += (*ool_stub_index)++;
if (WARN_ON(*ool_stub_index > ool_stub_count)) return -EINVAL;
if (!is_offset_in_branch_range((long)rec->ip - (long)&ool_stub->insn[0]) ||
!is_offset_in_branch_range((long)(rec->ip + MCOUNT_INSN_SIZE) -
(long)&ool_stub->insn[3])) {
pr_err("%s: ftrace ool stub out of range (%p -> %p).\n",
__func__, (void *)rec->ip, (void *)&ool_stub->insn[0]); return -EINVAL;
}
/* bl ftrace_caller */ if (!mod)
ret = ftrace_get_call_inst(rec, (unsignedlong)ftrace_caller, &inst); #ifdef CONFIG_MODULES else /* * We can't use ftrace_get_call_inst() since that uses * __module_text_address(rec->ip) to look up the module. * But, since the module is not fully formed at this stage, * the lookup fails. We know the target though, so generate * the branch inst directly.
*/
inst = ftrace_create_branch_inst(ftrace_get_ool_stub(rec) + MCOUNT_INSN_SIZE,
mod->arch.tramp, 1); #endif
ool_stub_template.insn[1] = ppc_inst_val(inst);
/* b rec->ip + 4 */ if (!ret && create_branch(&inst, &ool_stub->insn[3], rec->ip + MCOUNT_INSN_SIZE, 0)) return -EINVAL;
ool_stub_template.insn[3] = ppc_inst_val(inst);
if (!ret)
ret = patch_instructions((u32 *)ool_stub, (u32 *)&ool_stub_template, sizeof(ool_stub_template), false);
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS int ftrace_modify_call(struct dyn_ftrace *rec, unsignedlong old_addr, unsignedlong addr)
{ /* This should never be called since we override ftrace_replace_code() */
WARN_ON(1); return -EINVAL;
} #endif
int ftrace_make_call(struct dyn_ftrace *rec, unsignedlong addr)
{
ppc_inst_t old, new; unsignedlong ip = rec->ip; int ret = 0;
/* This can only ever be called during module load */ if (WARN_ON(!IS_ENABLED(CONFIG_MODULES) || core_kernel_text(ip))) return -EINVAL;
old = ppc_inst(PPC_RAW_NOP()); if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE)) {
ip = ftrace_get_ool_stub(rec) + MCOUNT_INSN_SIZE; /* second instruction in stub */
ret = ftrace_get_call_inst(rec, (unsignedlong)ftrace_caller, &old);
}
ret |= ftrace_get_call_inst(rec, addr, &new);
if (!ret)
ret = ftrace_modify_code(ip, old, new);
ret = ftrace_rec_update_ops(rec); if (ret) return ret;
if (!ret && IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE))
ret = ftrace_modify_code(rec->ip, ppc_inst(PPC_RAW_NOP()),
ppc_inst(PPC_RAW_BRANCH((long)ftrace_get_ool_stub(rec) - (long)rec->ip)));
return ret;
}
int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsignedlong addr)
{ /* * This should never be called since we override ftrace_replace_code(), * as well as ftrace_init_nop()
*/
WARN_ON(1); return -EINVAL;
}
if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE) && update != FTRACE_UPDATE_IGNORE) {
ip = ftrace_get_ool_stub(rec) + MCOUNT_INSN_SIZE;
ret = ftrace_get_call_inst(rec, (unsignedlong)ftrace_caller, &nop_inst); if (ret) goto out;
}
switch (update) { case FTRACE_UPDATE_IGNORE: default: continue; case FTRACE_UPDATE_MODIFY_CALL:
ret = ftrace_get_call_inst(rec, new_addr, &new_call_inst);
ret |= ftrace_get_call_inst(rec, addr, &call_inst);
ret |= ftrace_rec_update_ops(rec);
old = call_inst; new = new_call_inst; break; case FTRACE_UPDATE_MAKE_NOP:
ret = ftrace_get_call_inst(rec, addr, &call_inst);
ret |= ftrace_rec_set_nop_ops(rec);
old = call_inst; new = nop_inst; break; case FTRACE_UPDATE_MAKE_CALL:
ret = ftrace_get_call_inst(rec, new_addr, &call_inst);
ret |= ftrace_rec_update_ops(rec);
old = nop_inst; new = call_inst; break;
}
if (!ret)
ret = ftrace_modify_code(ip, old, new);
if (!ret && IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE) &&
(update == FTRACE_UPDATE_MAKE_NOP || update == FTRACE_UPDATE_MAKE_CALL)) { /* Update the actual ftrace location */
call_inst = ppc_inst(PPC_RAW_BRANCH((long)ftrace_get_ool_stub(rec) -
(long)rec->ip));
nop_inst = ppc_inst(PPC_RAW_NOP());
ip = rec->ip;
if (update == FTRACE_UPDATE_MAKE_NOP)
ret = ftrace_modify_code(ip, call_inst, nop_inst); else
ret = ftrace_modify_code(ip, nop_inst, call_inst);
if (ret) goto out;
}
if (ret) goto out;
}
out: if (ret)
ftrace_bug(ret, rec); return;
}
int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
{ unsignedlong addr, ip = rec->ip;
ppc_inst_t old, new; int ret = 0;
/* Verify instructions surrounding the ftrace location */ if (IS_ENABLED(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY)) { /* Expect nops */ if (!IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE))
ret = ftrace_validate_inst(ip - 4, ppc_inst(PPC_RAW_NOP())); if (!ret)
ret = ftrace_validate_inst(ip, ppc_inst(PPC_RAW_NOP()));
} elseif (IS_ENABLED(CONFIG_PPC32)) { /* Expected sequence: 'mflr r0', 'stw r0,4(r1)', 'bl _mcount' */
ret = ftrace_validate_inst(ip - 8, ppc_inst(PPC_RAW_MFLR(_R0))); if (ret) return ret;
ret = ftrace_modify_code(ip - 4, ppc_inst(PPC_RAW_STW(_R0, _R1, 4)),
ppc_inst(PPC_RAW_NOP()));
} elseif (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) { /* Expected sequence: 'mflr r0', ['std r0,16(r1)'], 'bl _mcount' */
ret = ftrace_read_inst(ip - 4, &old); if (!ret && !ppc_inst_equal(old, ppc_inst(PPC_RAW_MFLR(_R0)))) { /* Gcc v5.x emit the additional 'std' instruction, gcc v6.x don't */
ret = ftrace_validate_inst(ip - 8, ppc_inst(PPC_RAW_MFLR(_R0))); if (ret) return ret;
ret = ftrace_modify_code(ip - 4, ppc_inst(PPC_RAW_STD(_R0, _R1, 16)),
ppc_inst(PPC_RAW_NOP()));
}
} else { return -EINVAL;
}
if (ret) return ret;
/* Set up out-of-line stub */ if (IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE)) {
ret = ftrace_init_ool_stub(mod, rec); goto out;
}
/* Nop-out the ftrace location */ new = ppc_inst(PPC_RAW_NOP());
addr = MCOUNT_ADDR; if (IS_ENABLED(CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY)) { /* we instead patch-in the 'mflr r0' */
old = ppc_inst(PPC_RAW_NOP()); new = ppc_inst(PPC_RAW_MFLR(_R0));
ret = ftrace_modify_code(ip - 4, old, new);
} elseif (is_offset_in_branch_range(addr - ip)) { /* Within range */
old = ftrace_create_branch_inst(ip, addr, 1);
ret = ftrace_modify_code(ip, old, new);
} elseif (core_kernel_text(ip) || (IS_ENABLED(CONFIG_MODULES) && mod)) { /* * We would be branching to a linker-generated stub, or to the module _mcount * stub. Let's just confirm we have a 'bl' here.
*/
ret = ftrace_read_inst(ip, &old); if (ret) return ret; if (!is_bl_op(old)) {
pr_err("0x%lx: expected (bl) != found (%08lx)\n", ip, ppc_inst_as_ulong(old)); return -EINVAL;
}
ret = patch_instruction((u32 *)ip, new);
} else { return -EINVAL;
}
out: if (!ret)
ret = ftrace_rec_set_nop_ops(rec);
return ret;
}
int ftrace_update_ftrace_func(ftrace_func_t func)
{ unsignedlong ip = (unsignedlong)(&ftrace_call);
ppc_inst_t old, new; int ret;
/* * When using CALL_OPS, the function to call is associated with the * call site, and we don't have a global function pointer to update.
*/ if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS)) return 0;
old = ppc_inst_read((u32 *)&ftrace_call); new = ftrace_create_branch_inst(ip, ppc_function_entry(func), 1);
ret = ftrace_modify_code(ip, old, new);
/* Also update the regs callback function */ if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && !ret) {
ip = (unsignedlong)(&ftrace_regs_call);
old = ppc_inst_read((u32 *)&ftrace_regs_call); new = ftrace_create_branch_inst(ip, ppc_function_entry(func), 1);
ret = ftrace_modify_code(ip, old, new);
}
return ret;
}
/* * Use the default ftrace_modify_all_code, but without * stop_machine().
*/ void arch_ftrace_update_code(int command)
{
ftrace_modify_all_code(command);
}
void ftrace_free_init_tramp(void)
{ int i;
for (i = 0; i < NUM_FTRACE_TRAMPS && ftrace_tramps[i]; i++) if (ftrace_tramps[i] == (unsignedlong)ftrace_tramp_init) {
ftrace_tramps[i] = 0; return;
}
}
staticvoid __init add_ftrace_tramp(unsignedlong tramp)
{ int i;
for (i = 0; i < NUM_FTRACE_TRAMPS; i++) if (!ftrace_tramps[i]) {
ftrace_tramps[i] = tramp; return;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.