// SPDX-License-Identifier: GPL-2.0 /* * Dynamic function tracing support. * * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> * * Thanks goes to Ingo Molnar, for suggesting the idea. * Mathieu Desnoyers, for suggesting postponing the modifications. * Arjan van de Ven, for keeping me straight, and explaining to me * the dangers of modifying code on the run.
*/
void ftrace_arch_code_modify_prepare(void)
__acquires(&text_mutex)
{ /* * Need to grab text_mutex to prevent a race from module loading * and live kernel patching from changing the text permissions while * ftrace has it set to "read/write".
*/
mutex_lock(&text_mutex);
ftrace_poke_late = 1;
}
void ftrace_arch_code_modify_post_process(void)
__releases(&text_mutex)
{ /* * ftrace_make_{call,nop}() may be called during * module load, and we need to finish the smp_text_poke_batch_add() * that they do, here.
*/
smp_text_poke_batch_finish();
ftrace_poke_late = 0;
mutex_unlock(&text_mutex);
}
staticconstchar *ftrace_call_replace(unsignedlong ip, unsignedlong addr)
{ /* * No need to translate into a callthunk. The trampoline does * the depth accounting itself.
*/ return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
}
/* * Note: * We are paranoid about modifying text, as if a bug was to happen, it * could cause us to read or write to someplace that could cause harm. * Carefully read and modify the code with probe_kernel_*(), and make * sure what we read is what we expected it to be before modifying it.
*/ /* read the text we want to modify */ if (copy_from_kernel_nofault(cur_code, (void *)ip, MCOUNT_INSN_SIZE)) {
WARN_ON(1); return -EFAULT;
}
/* Make sure it is what we expect it to be */ if (memcmp(cur_code, old_code, MCOUNT_INSN_SIZE) != 0) {
ftrace_expected = old_code;
WARN_ON(1); return -EINVAL;
}
return 0;
}
/* * Marked __ref because it calls text_poke_early() which is .init.text. That is * ok because that call will happen early, during boot, when .init sections are * still present.
*/ staticint __ref
ftrace_modify_code_direct(unsignedlong ip, constchar *old_code, constchar *new_code)
{ int ret = ftrace_verify_code(ip, old_code); if (ret) return ret;
/* replace the text with the new text */ if (ftrace_poke_late)
smp_text_poke_batch_add((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL); else
text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE); return 0;
}
int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsignedlong addr)
{ unsignedlong ip = rec->ip; constchar *new, *old;
old = ftrace_call_replace(ip, addr); new = ftrace_nop_replace();
/* * On boot up, and when modules are loaded, the MCOUNT_ADDR * is converted to a nop, and will never become MCOUNT_ADDR * again. This code is either running before SMP (on boot up) * or before the code will ever be executed (module load). * We do not want to use the breakpoint version in this case, * just modify the code directly.
*/ if (addr == MCOUNT_ADDR) return ftrace_modify_code_direct(ip, old, new);
/* * x86 overrides ftrace_replace_code -- this function will never be used * in this case.
*/
WARN_ONCE(1, "invalid use of ftrace_make_nop"); return -EINVAL;
}
int ftrace_make_call(struct dyn_ftrace *rec, unsignedlong addr)
{ unsignedlong ip = rec->ip; constchar *new, *old;
old = ftrace_nop_replace(); new = ftrace_call_replace(ip, addr);
/* Should only be called when module is loaded */ return ftrace_modify_code_direct(rec->ip, old, new);
}
/* * Should never be called: * As it is only called by __ftrace_replace_code() which is called by * ftrace_replace_code() that x86 overrides, and by ftrace_update_code() * which is called to turn mcount into nops or nops into function calls * but not to convert a function from not using regs to one that uses * regs, which ftrace_modify_call() is for.
*/ int ftrace_modify_call(struct dyn_ftrace *rec, unsignedlong old_addr, unsignedlong addr)
{
WARN_ON(1); return -EINVAL;
}
int ftrace_update_ftrace_func(ftrace_func_t func)
{ unsignedlong ip; constchar *new;
ip = (unsignedlong)(&ftrace_call); new = ftrace_call_replace(ip, (unsignedlong)func);
smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
ip = (unsignedlong)(&ftrace_regs_call); new = ftrace_call_replace(ip, (unsignedlong)func);
smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
/* Defined as markers to the end of the ftrace default trampolines */ externvoid ftrace_regs_caller_end(void); externvoid ftrace_caller_end(void); externvoid ftrace_caller_op_ptr(void); externvoid ftrace_regs_caller_op_ptr(void); externvoid ftrace_regs_caller_jmp(void);
/* * The ftrace_ops is passed to the function callback. Since the * trampoline only services a single ftrace_ops, we can pass in * that ops directly. * * The ftrace_op_code_union is used to create a pointer to the * ftrace_ops that will be passed to the callback function.
*/ union ftrace_op_code_union { char code[OP_REF_SIZE]; struct { char op[3]; int offset;
} __attribute__((packed));
};
/* * Allocate enough size to store the ftrace_caller code, * the iret , as well as the address of the ftrace_ops this * trampoline is used for.
*/
trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *)); if (!trampoline) return 0;
/* Copy ftrace_caller onto the trampoline memory */
ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size); if (WARN_ON(ret < 0)) goto fail;
ip = trampoline + size; if (cpu_wants_rethunk_at(ip))
__text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE); else
memcpy(ip, retq, sizeof(retq));
/* No need to test direct calls on created trampolines */ if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) { /* NOP the jnz 1f; but make sure it's a 2 byte jnz */
ip = trampoline + (jmp_offset - start_offset); if (WARN_ON(*(char *)ip != 0x75)) goto fail;
ret = copy_from_kernel_nofault(ip, x86_nops[2], 2); if (ret < 0) goto fail;
}
/* * The address of the ftrace_ops that is used for this trampoline * is stored at the end of the trampoline. This will be used to * load the third parameter for the callback. Basically, that * location at the end of the trampoline takes the place of * the global function_trace_op variable.
*/
/* Are we pointing to the reference? */ if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0)) goto fail;
/* Load the contents of ptr into the callback parameter */
offset = (unsignedlong)ptr;
offset -= (unsignedlong)trampoline + op_offset + OP_REF_SIZE;
op_ptr.offset = offset;
/* put in the new offset to the ftrace_ops */
memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
/* put in the call to the function */
mutex_lock(&text_mutex);
call_offset -= start_offset; /* * No need to translate into a callthunk. The trampoline does * the depth accounting before the call already.
*/
dest = ftrace_ops_get_func(ops);
memcpy(trampoline + call_offset,
text_gen_insn(CALL_INSN_OPCODE, trampoline + call_offset, dest),
CALL_INSN_SIZE);
mutex_unlock(&text_mutex);
/* ALLOC_TRAMP flags lets us know we created it */
ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
if (!ops->trampoline) {
ops->trampoline = create_trampoline(ops, &size); if (!ops->trampoline) return;
ops->trampoline_size = size; return;
}
/* * The ftrace_ops caller may set up its own trampoline. * In such a case, this code must not modify it.
*/ if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) return;
mutex_lock(&text_mutex); /* Do a safe modify in case the trampoline is executing */ new = ftrace_call_replace(ip, (unsignedlong)func);
smp_text_poke_single((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
mutex_unlock(&text_mutex);
}
/* Return the address of the function the trampoline calls */ staticvoid *addr_from_call(void *ptr)
{ union text_poke_insn call; int ret;
ret = copy_from_kernel_nofault(&call, ptr, CALL_INSN_SIZE); if (WARN_ON_ONCE(ret < 0)) return NULL;
/* Make sure this is a call */ if (WARN_ON_ONCE(call.opcode != CALL_INSN_OPCODE)) {
pr_warn("Expected E8, got %x\n", call.opcode); return NULL;
}
return ptr + CALL_INSN_SIZE + call.disp;
}
/* * If the ops->trampoline was not allocated, then it probably * has a static trampoline func, or is the ftrace caller itself.
*/ staticvoid *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
{ unsignedlong offset; bool save_regs = rec->flags & FTRACE_FL_REGS_EN; void *ptr;
if (ops && ops->trampoline) { #if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) && \ defined(CONFIG_FUNCTION_GRAPH_TRACER) /* * We only know about function graph tracer setting as static * trampoline.
*/ if (ops->trampoline == FTRACE_GRAPH_ADDR) return (void *)prepare_ftrace_return; #endif return NULL;
}
/* If we didn't allocate this trampoline, consider it static */ if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) return static_tramp_func(ops, rec);
staticinlinebool skip_ftrace_return(void)
{ /* * When resuming from suspend-to-ram, this function can be indirectly * called from early CPU startup code while the CPU is in real mode, * which would fail miserably. Make sure the stack pointer is a * virtual address. * * This check isn't as accurate as virt_addr_valid(), but it should be * good enough for this purpose, and it's fast.
*/ if ((long)__builtin_frame_address(0) >= 0) returntrue;
if (ftrace_graph_is_dead()) returntrue;
if (atomic_read(¤t->tracing_graph_pause)) returntrue; returnfalse;
}
/* * Hook the return address and push it in the stack of return addrs * in current thread info.
*/ void prepare_ftrace_return(unsignedlong ip, unsignedlong *parent, unsignedlong frame_pointer)
{ unsignedlong return_hooker = (unsignedlong)&return_to_handler;
if (unlikely(skip_ftrace_return())) return;
if (!function_graph_enter(*parent, ip, frame_pointer, parent))
*parent = return_hooker;
}
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.