@@ -172,12 +172,120 @@ int __kprobes parisc_kprobe_ss_handler(struct pt_regs *regs)
172172 return 1 ;
173173}
174174
175+ static inline void kretprobe_trampoline (void )
176+ {
177+ asm volatile ("nop" );
178+ asm volatile ("nop" );
179+ }
180+
181+ static int __kprobes trampoline_probe_handler (struct kprobe * p ,
182+ struct pt_regs * regs );
183+
184+ static struct kprobe trampoline_p = {
185+ .pre_handler = trampoline_probe_handler
186+ };
187+
188+ static int __kprobes trampoline_probe_handler (struct kprobe * p ,
189+ struct pt_regs * regs )
190+ {
191+ struct kretprobe_instance * ri = NULL ;
192+ struct hlist_head * head , empty_rp ;
193+ struct hlist_node * tmp ;
194+ unsigned long flags , orig_ret_address = 0 ;
195+ unsigned long trampoline_address = (unsigned long )trampoline_p .addr ;
196+ kprobe_opcode_t * correct_ret_addr = NULL ;
197+
198+ INIT_HLIST_HEAD (& empty_rp );
199+ kretprobe_hash_lock (current , & head , & flags );
200+
201+ /*
202+ * It is possible to have multiple instances associated with a given
203+ * task either because multiple functions in the call path have
204+ * a return probe installed on them, and/or more than one return
205+ * probe was registered for a target function.
206+ *
207+ * We can handle this because:
208+ * - instances are always inserted at the head of the list
209+ * - when multiple return probes are registered for the same
210+ * function, the first instance's ret_addr will point to the
211+ * real return address, and all the rest will point to
212+ * kretprobe_trampoline
213+ */
214+ hlist_for_each_entry_safe (ri , tmp , head , hlist ) {
215+ if (ri -> task != current )
216+ /* another task is sharing our hash bucket */
217+ continue ;
218+
219+ orig_ret_address = (unsigned long )ri -> ret_addr ;
220+
221+ if (orig_ret_address != trampoline_address )
222+ /*
223+ * This is the real return address. Any other
224+ * instances associated with this task are for
225+ * other calls deeper on the call stack
226+ */
227+ break ;
228+ }
229+
230+ kretprobe_assert (ri , orig_ret_address , trampoline_address );
231+
232+ correct_ret_addr = ri -> ret_addr ;
233+ hlist_for_each_entry_safe (ri , tmp , head , hlist ) {
234+ if (ri -> task != current )
235+ /* another task is sharing our hash bucket */
236+ continue ;
237+
238+ orig_ret_address = (unsigned long )ri -> ret_addr ;
239+ if (ri -> rp && ri -> rp -> handler ) {
240+ __this_cpu_write (current_kprobe , & ri -> rp -> kp );
241+ get_kprobe_ctlblk ()-> kprobe_status = KPROBE_HIT_ACTIVE ;
242+ ri -> ret_addr = correct_ret_addr ;
243+ ri -> rp -> handler (ri , regs );
244+ __this_cpu_write (current_kprobe , NULL );
245+ }
246+
247+ recycle_rp_inst (ri , & empty_rp );
248+
249+ if (orig_ret_address != trampoline_address )
250+ /*
251+ * This is the real return address. Any other
252+ * instances associated with this task are for
253+ * other calls deeper on the call stack
254+ */
255+ break ;
256+ }
257+
258+ kretprobe_hash_unlock (current , & flags );
259+
260+ hlist_for_each_entry_safe (ri , tmp , & empty_rp , hlist ) {
261+ hlist_del (& ri -> hlist );
262+ kfree (ri );
263+ }
264+ instruction_pointer_set (regs , orig_ret_address );
265+ return 1 ;
266+ }
267+
268+ void __kprobes arch_prepare_kretprobe (struct kretprobe_instance * ri ,
269+ struct pt_regs * regs )
270+ {
271+ ri -> ret_addr = (kprobe_opcode_t * )regs -> gr [2 ];
272+
273+ /* Replace the return addr with trampoline addr. */
274+ regs -> gr [2 ] = (unsigned long )trampoline_p .addr ;
275+ }
276+
277+ int __kprobes arch_trampoline_kprobe (struct kprobe * p )
278+ {
279+ return p -> addr == trampoline_p .addr ;
280+ }
175281bool arch_kprobe_on_func_entry (unsigned long offset )
176282{
177283 return !offset ;
178284}
179285
180286int __init arch_init_kprobes (void )
181287{
182- return 0 ;
288+ trampoline_p .addr = (kprobe_opcode_t * )
289+ dereference_function_descriptor (kretprobe_trampoline );
290+ return register_kprobe (& trampoline_p );
183291}
0 commit comments