Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Dynamic function tracing support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Thanks goes to Ingo Molnar, for suggesting the idea.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Mathieu Desnoyers, for suggesting postponing the modifications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Arjan van de Ven, for keeping me straight, and explaining to me
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * the dangers of modifying code on the run.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/hardirq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <trace/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <asm/set_memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <asm/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <asm/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <asm/nops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <asm/text-patching.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static int ftrace_poke_late = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) int ftrace_arch_code_modify_prepare(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)     __acquires(&text_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 * Need to grab text_mutex to prevent a race from module loading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	 * and live kernel patching from changing the text permissions while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 * ftrace has it set to "read/write".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	mutex_lock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	ftrace_poke_late = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) int ftrace_arch_code_modify_post_process(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)     __releases(&text_mutex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 * ftrace_make_{call,nop}() may be called during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * module load, and we need to finish the text_poke_queue()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 * that they do, here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	text_poke_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ftrace_poke_late = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	mutex_unlock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static const char *ftrace_nop_replace(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return ideal_nops[NOP_ATOMIC5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static const char *ftrace_call_replace(unsigned long ip, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static int ftrace_verify_code(unsigned long ip, const char *old_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	char cur_code[MCOUNT_INSN_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * Note:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * We are paranoid about modifying text, as if a bug was to happen, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 * could cause us to read or write to someplace that could cause harm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	 * Carefully read and modify the code with probe_kernel_*(), and make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 * sure what we read is what we expected it to be before modifying it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* read the text we want to modify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (copy_from_kernel_nofault(cur_code, (void *)ip, MCOUNT_INSN_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/* Make sure it is what we expect it to be */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (memcmp(cur_code, old_code, MCOUNT_INSN_SIZE) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * Marked __ref because it calls text_poke_early() which is .init.text. That is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * ok because that call will happen early, during boot, when .init sections are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * still present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int __ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ftrace_modify_code_direct(unsigned long ip, const char *old_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			  const char *new_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int ret = ftrace_verify_code(ip, old_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	/* replace the text with the new text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (ftrace_poke_late)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		text_poke_queue((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	unsigned long ip = rec->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	const char *new, *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	old = ftrace_call_replace(ip, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	new = ftrace_nop_replace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * On boot up, and when modules are loaded, the MCOUNT_ADDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 * is converted to a nop, and will never become MCOUNT_ADDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * again. This code is either running before SMP (on boot up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * or before the code will ever be executed (module load).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * We do not want to use the breakpoint version in this case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * just modify the code directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (addr == MCOUNT_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return ftrace_modify_code_direct(ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	 * x86 overrides ftrace_replace_code -- this function will never be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	WARN_ONCE(1, "invalid use of ftrace_make_nop");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	unsigned long ip = rec->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	const char *new, *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	old = ftrace_nop_replace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	new = ftrace_call_replace(ip, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	/* Should only be called when module is loaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return ftrace_modify_code_direct(rec->ip, old, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * Should never be called:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  *  As it is only called by __ftrace_replace_code() which is called by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  *  ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *  which is called to turn mcount into nops or nops into function calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *  but not to convert a function from not using regs to one that uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *  regs, which ftrace_modify_call() is for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				 unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int ftrace_update_ftrace_func(ftrace_func_t func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	unsigned long ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	const char *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	ip = (unsigned long)(&ftrace_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	new = ftrace_call_replace(ip, (unsigned long)func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	ip = (unsigned long)(&ftrace_regs_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	new = ftrace_call_replace(ip, (unsigned long)func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) void ftrace_replace_code(int enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct ftrace_rec_iter *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct dyn_ftrace *rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	const char *new, *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	for_ftrace_rec_iter(iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		rec = ftrace_rec_iter_record(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		switch (ftrace_test_record(rec, enable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		case FTRACE_UPDATE_IGNORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		case FTRACE_UPDATE_MAKE_CALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			old = ftrace_nop_replace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		case FTRACE_UPDATE_MODIFY_CALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		case FTRACE_UPDATE_MAKE_NOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			old = ftrace_call_replace(rec->ip, ftrace_get_addr_curr(rec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		ret = ftrace_verify_code(rec->ip, old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			ftrace_bug(ret, rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	for_ftrace_rec_iter(iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		rec = ftrace_rec_iter_record(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		switch (ftrace_test_record(rec, enable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		case FTRACE_UPDATE_IGNORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		case FTRACE_UPDATE_MAKE_CALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		case FTRACE_UPDATE_MODIFY_CALL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			new = ftrace_call_replace(rec->ip, ftrace_get_addr_new(rec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		case FTRACE_UPDATE_MAKE_NOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			new = ftrace_nop_replace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		text_poke_queue((void *)rec->ip, new, MCOUNT_INSN_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		ftrace_update_record(rec, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	text_poke_finish();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) void arch_ftrace_update_code(int command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	ftrace_modify_all_code(command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int __init ftrace_dyn_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* Currently only x86_64 supports dynamic trampolines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) #ifdef CONFIG_MODULES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* Module allocation simplifies allocating memory for code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static inline void *alloc_tramp(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return module_alloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static inline void tramp_free(void *tramp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	module_memfree(tramp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Trampolines can only be created if modules are supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static inline void *alloc_tramp(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static inline void tramp_free(void *tramp) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* Defined as markers to the end of the ftrace default trampolines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) extern void ftrace_regs_caller_end(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) extern void ftrace_regs_caller_ret(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) extern void ftrace_caller_end(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) extern void ftrace_caller_op_ptr(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) extern void ftrace_regs_caller_op_ptr(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) extern void ftrace_regs_caller_jmp(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* movq function_trace_op(%rip), %rdx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #define OP_REF_SIZE	7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  * The ftrace_ops is passed to the function callback. Since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * trampoline only services a single ftrace_ops, we can pass in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * that ops directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * The ftrace_op_code_union is used to create a pointer to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  * ftrace_ops that will be passed to the callback function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) union ftrace_op_code_union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	char code[OP_REF_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		char op[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	} __attribute__((packed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #define RET_SIZE		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	unsigned long start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	unsigned long end_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	unsigned long op_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	unsigned long call_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	unsigned long jmp_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	unsigned long npages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	unsigned long retq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	unsigned long *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	void *trampoline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	void *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	/* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	union ftrace_op_code_union op_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		start_offset = (unsigned long)ftrace_regs_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		end_offset = (unsigned long)ftrace_regs_caller_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		call_offset = (unsigned long)ftrace_regs_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		jmp_offset = (unsigned long)ftrace_regs_caller_jmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		start_offset = (unsigned long)ftrace_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		end_offset = (unsigned long)ftrace_caller_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		op_offset = (unsigned long)ftrace_caller_op_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		call_offset = (unsigned long)ftrace_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		jmp_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	size = end_offset - start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	 * Allocate enough size to store the ftrace_caller code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	 * the iret , as well as the address of the ftrace_ops this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	 * trampoline is used for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (!trampoline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	*tramp_size = size + RET_SIZE + sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	/* Copy ftrace_caller onto the trampoline memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (WARN_ON(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	ip = trampoline + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* The trampoline ends with ret(q) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	retq = (unsigned long)ftrace_stub;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	ret = copy_from_kernel_nofault(ip, (void *)retq, RET_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (WARN_ON(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/* No need to test direct calls on created trampolines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		/* NOP the jnz 1f; but make sure it's a 2 byte jnz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		ip = trampoline + (jmp_offset - start_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		if (WARN_ON(*(char *)ip != 0x75))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		ret = copy_from_kernel_nofault(ip, ideal_nops[2], 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 * The address of the ftrace_ops that is used for this trampoline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 * is stored at the end of the trampoline. This will be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * load the third parameter for the callback. Basically, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 * location at the end of the trampoline takes the place of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	 * the global function_trace_op variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	ptr = (unsigned long *)(trampoline + size + RET_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	*ptr = (unsigned long)ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	op_offset -= start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	/* Are we pointing to the reference? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	/* Load the contents of ptr into the callback parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	offset = (unsigned long)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	op_ptr.offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/* put in the new offset to the ftrace_ops */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	/* put in the call to the function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	mutex_lock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	call_offset -= start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	memcpy(trampoline + call_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	       text_gen_insn(CALL_INSN_OPCODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			     trampoline + call_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			     ftrace_ops_get_func(ops)), CALL_INSN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	mutex_unlock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	/* ALLOC_TRAMP flags lets us know we created it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	set_vm_flush_reset_perms(trampoline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (likely(system_state != SYSTEM_BOOTING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		set_memory_ro((unsigned long)trampoline, npages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	set_memory_x((unsigned long)trampoline, npages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	return (unsigned long)trampoline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	tramp_free(trampoline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) void set_ftrace_ops_ro(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	struct ftrace_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	unsigned long start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	unsigned long end_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	unsigned long npages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	do_for_each_ftrace_op(ops, ftrace_ops_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 			start_offset = (unsigned long)ftrace_regs_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			end_offset = (unsigned long)ftrace_regs_caller_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			start_offset = (unsigned long)ftrace_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			end_offset = (unsigned long)ftrace_caller_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		size = end_offset - start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		size = size + RET_SIZE + sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		npages = DIV_ROUND_UP(size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		set_memory_ro((unsigned long)ops->trampoline, npages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	} while_for_each_ftrace_op(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static unsigned long calc_trampoline_call_offset(bool save_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	unsigned long start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	unsigned long call_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (save_regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		start_offset = (unsigned long)ftrace_regs_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		call_offset = (unsigned long)ftrace_regs_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		start_offset = (unsigned long)ftrace_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		call_offset = (unsigned long)ftrace_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return call_offset - start_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	ftrace_func_t func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	unsigned long ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	const char *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	if (!ops->trampoline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		ops->trampoline = create_trampoline(ops, &size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (!ops->trampoline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		ops->trampoline_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	 * The ftrace_ops caller may set up its own trampoline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	 * In such a case, this code must not modify it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	ip = ops->trampoline + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	func = ftrace_ops_get_func(ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	mutex_lock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	/* Do a safe modify in case the trampoline is executing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	new = ftrace_call_replace(ip, (unsigned long)func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	mutex_unlock(&text_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /* Return the address of the function the trampoline calls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static void *addr_from_call(void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	union text_poke_insn call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	ret = copy_from_kernel_nofault(&call, ptr, CALL_INSN_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (WARN_ON_ONCE(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	/* Make sure this is a call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	if (WARN_ON_ONCE(call.opcode != CALL_INSN_OPCODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		pr_warn("Expected E8, got %x\n", call.opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	return ptr + CALL_INSN_SIZE + call.disp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			   unsigned long frame_pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  * If the ops->trampoline was not allocated, then it probably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  * has a static trampoline func, or is the ftrace caller itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	if (ops && ops->trampoline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) #ifdef CONFIG_FUNCTION_GRAPH_TRACER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		 * We only know about function graph tracer setting as static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		 * trampoline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		if (ops->trampoline == FTRACE_GRAPH_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			return (void *)prepare_ftrace_return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	offset = calc_trampoline_call_offset(save_regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	if (save_regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		ptr = (void *)FTRACE_REGS_ADDR + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		ptr = (void *)FTRACE_ADDR + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	return addr_from_call(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	/* If we didn't allocate this trampoline, consider it static */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		return static_tramp_func(ops, rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	return addr_from_call((void *)ops->trampoline + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	tramp_free((void *)ops->trampoline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	ops->trampoline = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) #endif /* CONFIG_X86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) #endif /* CONFIG_DYNAMIC_FTRACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) #ifdef CONFIG_FUNCTION_GRAPH_TRACER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) extern void ftrace_graph_call(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) static const char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static int ftrace_mod_jmp(unsigned long ip, void *func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	const char *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	new = ftrace_jmp_replace(ip, (unsigned long)func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) int ftrace_enable_ftrace_graph_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	return ftrace_mod_jmp(ip, &ftrace_graph_caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) int ftrace_disable_ftrace_graph_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	return ftrace_mod_jmp(ip, &ftrace_stub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) #endif /* !CONFIG_DYNAMIC_FTRACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)  * Hook the return address and push it in the stack of return addrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)  * in current thread info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 			   unsigned long frame_pointer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	unsigned long return_hooker = (unsigned long)&return_to_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	unsigned long old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	int faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	 * When resuming from suspend-to-ram, this function can be indirectly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	 * called from early CPU startup code while the CPU is in real mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	 * which would fail miserably.  Make sure the stack pointer is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	 * virtual address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	 * This check isn't as accurate as virt_addr_valid(), but it should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	 * good enough for this purpose, and it's fast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	if (unlikely((long)__builtin_frame_address(0) >= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	if (unlikely(ftrace_graph_is_dead()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	 * Protect against fault, even if it shouldn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	 * happen. This tool is too much intrusive to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	 * ignore such a protection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	asm volatile(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		"1: " _ASM_MOV " (%[parent]), %[old]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		"2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		"   movl $0, %[faulted]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		"3:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		".section .fixup, \"ax\"\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		"4: movl $1, %[faulted]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		"   jmp 3b\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		".previous\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		_ASM_EXTABLE(1b, 4b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		_ASM_EXTABLE(2b, 4b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		: [old] "=&r" (old), [faulted] "=r" (faulted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		: [parent] "r" (parent), [return_hooker] "r" (return_hooker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		: "memory"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	if (unlikely(faulted)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		ftrace_graph_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	if (function_graph_enter(old, self_addr, frame_pointer, parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		*parent = old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) #endif /* CONFIG_FUNCTION_GRAPH_TRACER */