@@ -63,18 +63,18 @@ static inline int in_kernel_space(unsigned long ip)
6363 ((unsigned int)(JAL | (((addr) >> 2) & ADDR_MASK)))
6464
6565static unsigned int insn_jal_ftrace_caller __read_mostly ;
66- static unsigned int insn_lui_v1_hi16_mcount __read_mostly ;
66+ static unsigned int insn_la_mcount [ 2 ] __read_mostly ;
6767static unsigned int insn_j_ftrace_graph_caller __maybe_unused __read_mostly ;
6868
6969static inline void ftrace_dyn_arch_init_insns (void )
7070{
7171 u32 * buf ;
7272 unsigned int v1 ;
7373
74- /* lui v1, hi16_mcount */
74+ /* la v1, _mcount */
7575 v1 = 3 ;
76- buf = (u32 * )& insn_lui_v1_hi16_mcount ;
77- UASM_i_LA_mostly (& buf , v1 , MCOUNT_ADDR );
76+ buf = (u32 * )& insn_la_mcount [ 0 ] ;
77+ UASM_i_LA (& buf , v1 , MCOUNT_ADDR );
7878
7979 /* jal (ftrace_caller + 8), jump over the first two instruction */
8080 buf = (u32 * )& insn_jal_ftrace_caller ;
@@ -111,14 +111,47 @@ static int ftrace_modify_code_2(unsigned long ip, unsigned int new_code1,
111111 unsigned int new_code2 )
112112{
113113 int faulted ;
114+ mm_segment_t old_fs ;
114115
115116 safe_store_code (new_code1 , ip , faulted );
116117 if (unlikely (faulted ))
117118 return - EFAULT ;
118- safe_store_code (new_code2 , ip + 4 , faulted );
119+
120+ ip += 4 ;
121+ safe_store_code (new_code2 , ip , faulted );
119122 if (unlikely (faulted ))
120123 return - EFAULT ;
124+
125+ ip -= 4 ;
126+ old_fs = get_fs ();
127+ set_fs (get_ds ());
121128 flush_icache_range (ip , ip + 8 );
129+ set_fs (old_fs );
130+
131+ return 0 ;
132+ }
133+
134+ static int ftrace_modify_code_2r (unsigned long ip , unsigned int new_code1 ,
135+ unsigned int new_code2 )
136+ {
137+ int faulted ;
138+ mm_segment_t old_fs ;
139+
140+ ip += 4 ;
141+ safe_store_code (new_code2 , ip , faulted );
142+ if (unlikely (faulted ))
143+ return - EFAULT ;
144+
145+ ip -= 4 ;
146+ safe_store_code (new_code1 , ip , faulted );
147+ if (unlikely (faulted ))
148+ return - EFAULT ;
149+
150+ old_fs = get_fs ();
151+ set_fs (get_ds ());
152+ flush_icache_range (ip , ip + 8 );
153+ set_fs (old_fs );
154+
122155 return 0 ;
123156}
124157#endif
@@ -130,13 +163,14 @@ static int ftrace_modify_code_2(unsigned long ip, unsigned int new_code1,
130163 *
131164 * move at, ra
132165 * jal _mcount --> nop
166+ * sub sp, sp, 8 --> nop (CONFIG_32BIT)
133167 *
134168 * 2. For modules:
135169 *
136170 * 2.1 For KBUILD_MCOUNT_RA_ADDRESS and CONFIG_32BIT
137171 *
138172 * lui v1, hi_16bit_of_mcount --> b 1f (0x10000005)
139- * addiu v1, v1, low_16bit_of_mcount
173+ * addiu v1, v1, low_16bit_of_mcount --> nop (CONFIG_32BIT)
140174 * move at, ra
141175 * move $12, ra_address
142176 * jalr v1
@@ -145,7 +179,7 @@ static int ftrace_modify_code_2(unsigned long ip, unsigned int new_code1,
145179 * 2.2 For the Other situations
146180 *
147181 * lui v1, hi_16bit_of_mcount --> b 1f (0x10000004)
148- * addiu v1, v1, low_16bit_of_mcount
182+ * addiu v1, v1, low_16bit_of_mcount --> nop (CONFIG_32BIT)
149183 * move at, ra
150184 * jalr v1
151185 * nop | move $12, ra_address | sub sp, sp, 8
@@ -184,10 +218,14 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
184218 unsigned int new ;
185219 unsigned long ip = rec -> ip ;
186220
187- new = in_kernel_space (ip ) ? insn_jal_ftrace_caller :
188- insn_lui_v1_hi16_mcount ;
221+ new = in_kernel_space (ip ) ? insn_jal_ftrace_caller : insn_la_mcount [0 ];
189222
223+ #ifdef CONFIG_64BIT
190224 return ftrace_modify_code (ip , new );
225+ #else
226+ return ftrace_modify_code_2r (ip , new , in_kernel_space (ip ) ?
227+ INSN_NOP : insn_la_mcount [1 ]);
228+ #endif
191229}
192230
193231#define FTRACE_CALL_IP ((unsigned long)(&ftrace_call))
0 commit comments