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)  * Code for replacing ftrace calls with jumps.
^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)  * Copyright (C) 2009, 2010 DSLab, Lanzhou University, China
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Wu Zhangjin <wuzhangjin@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Thanks goes to Steven Rostedt for writing the original x86 version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/asm-offsets.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/uasm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm-generic/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #if defined(KBUILD_MCOUNT_RA_ADDRESS) && defined(CONFIG_32BIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define MCOUNT_OFFSET_INSNS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define MCOUNT_OFFSET_INSNS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /* Arch override because MIPS doesn't need to run this from stop_machine() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) void arch_ftrace_update_code(int command)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	ftrace_modify_all_code(command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define JAL 0x0c000000		/* jump & link: ip --> ra, jump to target */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define ADDR_MASK 0x03ffffff	/*  op_code|addr : 31...26|25 ....0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define JUMP_RANGE_MASK ((1UL << 28) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define INSN_NOP 0x00000000	/* nop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define INSN_JAL(addr)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static unsigned int insn_jal_ftrace_caller __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static unsigned int insn_la_mcount[2] __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static inline void ftrace_dyn_arch_init_insns(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u32 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	unsigned int v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/* la v1, _mcount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	v1 = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	buf = (u32 *)&insn_la_mcount[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	UASM_i_LA(&buf, v1, MCOUNT_ADDR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	/* jal (ftrace_caller + 8), jump over the first two instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	buf = (u32 *)&insn_jal_ftrace_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	uasm_i_jal(&buf, (FTRACE_ADDR + 8) & JUMP_RANGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #ifdef CONFIG_FUNCTION_GRAPH_TRACER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/* j ftrace_graph_caller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	buf = (u32 *)&insn_j_ftrace_graph_caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	uasm_i_j(&buf, (unsigned long)ftrace_graph_caller & JUMP_RANGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int ftrace_modify_code(unsigned long ip, unsigned int new_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	mm_segment_t old_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	/* *(unsigned int *)ip = new_code; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	safe_store_code(new_code, ip, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	old_fs = get_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	set_fs(KERNEL_DS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	flush_icache_range(ip, ip + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	set_fs(old_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #ifndef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static int ftrace_modify_code_2(unsigned long ip, unsigned int new_code1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				unsigned int new_code2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	mm_segment_t old_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	safe_store_code(new_code1, ip, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ip += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	safe_store_code(new_code2, ip, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ip -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	old_fs = get_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	set_fs(KERNEL_DS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	flush_icache_range(ip, ip + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	set_fs(old_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int ftrace_modify_code_2r(unsigned long ip, unsigned int new_code1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				 unsigned int new_code2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	mm_segment_t old_fs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ip += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	safe_store_code(new_code2, ip, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ip -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	safe_store_code(new_code1, ip, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	old_fs = get_fs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	set_fs(KERNEL_DS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	flush_icache_range(ip, ip + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	set_fs(old_fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * The details about the calling site of mcount on MIPS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * 1. For kernel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * move at, ra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * jal _mcount		--> nop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  *  sub sp, sp, 8	--> nop  (CONFIG_32BIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * 2. For modules:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * 2.1 For KBUILD_MCOUNT_RA_ADDRESS and CONFIG_32BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * lui v1, hi_16bit_of_mcount	     --> b 1f (0x10000005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * addiu v1, v1, low_16bit_of_mcount --> nop  (CONFIG_32BIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * move at, ra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * move $12, ra_address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * jalr v1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  *  sub sp, sp, 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  *				    1: offset = 5 instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * 2.2 For the Other situations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * lui v1, hi_16bit_of_mcount	     --> b 1f (0x10000004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * addiu v1, v1, low_16bit_of_mcount --> nop  (CONFIG_32BIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * move at, ra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * jalr v1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *  nop | move $12, ra_address | sub sp, sp, 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *				    1: offset = 4 instructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define INSN_B_1F (0x10000000 | MCOUNT_OFFSET_INSNS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) int ftrace_make_nop(struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		    struct dyn_ftrace *rec, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	unsigned int new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	unsigned long ip = rec->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 * If ip is in kernel space, no long call, otherwise, long call is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 * needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	new = core_kernel_text(ip) ? INSN_NOP : INSN_B_1F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	return ftrace_modify_code(ip, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * On 32 bit MIPS platforms, gcc adds a stack adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 * instruction in the delay slot after the branch to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 * mcount and expects mcount to restore the sp on return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	 * This is based on a legacy API and does nothing but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 * waste instructions so it's being removed at runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return ftrace_modify_code_2(ip, new, INSN_NOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	unsigned int new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	unsigned long ip = rec->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	new = core_kernel_text(ip) ? insn_jal_ftrace_caller : insn_la_mcount[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return ftrace_modify_code(ip, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return ftrace_modify_code_2r(ip, new, core_kernel_text(ip) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 						INSN_NOP : insn_la_mcount[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int ftrace_update_ftrace_func(ftrace_func_t func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	unsigned int new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	new = INSN_JAL((unsigned long)func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	return ftrace_modify_code(FTRACE_CALL_IP, new);
^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) int __init ftrace_dyn_arch_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	/* Encode the instructions when booting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	ftrace_dyn_arch_init_insns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	/* Remove "b ftrace_stub" to ensure ftrace_caller() is executed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ftrace_modify_code(MCOUNT_ADDR, INSN_NOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) #endif	/* CONFIG_DYNAMIC_FTRACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #ifdef CONFIG_FUNCTION_GRAPH_TRACER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) #ifdef CONFIG_DYNAMIC_FTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) extern void ftrace_graph_call(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #define FTRACE_GRAPH_CALL_IP	((unsigned long)(&ftrace_graph_call))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int ftrace_enable_ftrace_graph_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return ftrace_modify_code(FTRACE_GRAPH_CALL_IP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			insn_j_ftrace_graph_caller);
^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) int ftrace_disable_ftrace_graph_caller(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return ftrace_modify_code(FTRACE_GRAPH_CALL_IP, INSN_NOP);
^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) #endif	/* CONFIG_DYNAMIC_FTRACE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) #ifndef KBUILD_MCOUNT_RA_ADDRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #define S_RA_SP (0xafbf << 16)	/* s{d,w} ra, offset(sp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define S_R_SP	(0xafb0 << 16)	/* s{d,w} R, offset(sp) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) #define OFFSET_MASK	0xffff	/* stack offset range: 0 ~ PT_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) unsigned long ftrace_get_parent_ra_addr(unsigned long self_ra, unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		old_parent_ra, unsigned long parent_ra_addr, unsigned long fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	unsigned long sp, ip, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	unsigned int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int faulted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 * For module, move the ip from the return address after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	 * instruction "lui v1, hi_16bit_of_mcount"(offset is 24), but for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 * kernel, move after the instruction "move ra, at"(offset is 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	ip = self_ra - (core_kernel_text(self_ra) ? 16 : 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	 * search the text until finding the non-store instruction or "s{d,w}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 * ra, offset(sp)" instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		/* get the code at "ip": code = *(unsigned int *)ip; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		safe_load_code(code, ip, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		 * If we hit the non-store instruction before finding where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		 * ra is stored, then this is a leaf function and it does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		 * store the ra on the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if ((code & S_R_SP) != S_R_SP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			return parent_ra_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		/* Move to the next instruction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		ip -= 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	} while ((code & S_RA_SP) != S_RA_SP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	sp = fp + (code & OFFSET_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/* tmp = *(unsigned long *)sp; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	safe_load_stack(tmp, sp, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (tmp == old_parent_ra)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		return sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return 0;
^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) #endif	/* !KBUILD_MCOUNT_RA_ADDRESS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * Hook the return address and push it in the stack of return addrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  * in current thread info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) void prepare_ftrace_return(unsigned long *parent_ra_addr, unsigned long self_ra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			   unsigned long fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	unsigned long old_parent_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	unsigned long return_hooker = (unsigned long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	    &return_to_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	int faulted, insns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (unlikely(ftrace_graph_is_dead()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	 * "parent_ra_addr" is the stack address where the return address of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	 * the caller of _mcount is saved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * If gcc < 4.5, a leaf function does not save the return address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 * in the stack address, so we "emulate" one in _mcount's stack space,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 * and hijack it directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	 * For a non-leaf function, it does save the return address to its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	 * stack space, so we can not hijack it directly, but need to find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	 * real stack address, which is done by ftrace_get_parent_addr().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 * If gcc >= 4.5, with the new -mmcount-ra-address option, for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * non-leaf function, the location of the return address will be saved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 * to $12 for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	 * For a leaf function, it just puts a zero into $12, so we handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	 * it in ftrace_graph_caller() of mcount.S.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* old_parent_ra = *parent_ra_addr; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	safe_load_stack(old_parent_ra, parent_ra_addr, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) #ifndef KBUILD_MCOUNT_RA_ADDRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	parent_ra_addr = (unsigned long *)ftrace_get_parent_ra_addr(self_ra,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			old_parent_ra, (unsigned long)parent_ra_addr, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * If fails when getting the stack address of the non-leaf function's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * ra, stop function graph tracer and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (parent_ra_addr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	/* *parent_ra_addr = return_hooker; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	safe_store_stack(return_hooker, parent_ra_addr, faulted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (unlikely(faulted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	 * Get the recorded ip of the current mcount calling site in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	 * __mcount_loc section, which will be used to filter the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	 * entries configured through the tracing/set_graph_function interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	insns = core_kernel_text(self_ra) ? 2 : MCOUNT_OFFSET_INSNS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	self_ra -= (MCOUNT_INSN_SIZE * insns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (function_graph_enter(old_parent_ra, self_ra, fp, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		*parent_ra_addr = old_parent_ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	ftrace_graph_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) #endif	/* CONFIG_FUNCTION_GRAPH_TRACER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #ifdef CONFIG_FTRACE_SYSCALLS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) #ifdef CONFIG_32BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) unsigned long __init arch_syscall_addr(int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	return (unsigned long)sys_call_table[nr - __NR_O32_Linux];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) #ifdef CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) unsigned long __init arch_syscall_addr(int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) #ifdef CONFIG_MIPS32_N32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (nr >= __NR_N32_Linux && nr < __NR_N32_Linux + __NR_N32_Linux_syscalls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		return (unsigned long)sysn32_call_table[nr - __NR_N32_Linux];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (nr >= __NR_64_Linux  && nr < __NR_64_Linux + __NR_64_Linux_syscalls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return (unsigned long)sys_call_table[nr - __NR_64_Linux];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) #ifdef CONFIG_MIPS32_O32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	if (nr >= __NR_O32_Linux && nr < __NR_O32_Linux + __NR_O32_Linux_syscalls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		return (unsigned long)sys32_call_table[nr - __NR_O32_Linux];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return (unsigned long) &sys_ni_syscall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) #endif /* CONFIG_FTRACE_SYSCALLS */