1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_UPROBES_H
3 #define _LINUX_UPROBES_H
5 * User-space Probes (UProbes)
7 * Copyright (C) IBM Corporation, 2008-2012
11 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
14 #include <linux/errno.h>
15 #include <linux/rbtree.h>
16 #include <linux/types.h>
17 #include <linux/wait.h>
18 #include <linux/timer.h>
21 struct vm_area_struct
;
24 struct notifier_block
;
28 * Allowed return values from uprobe consumer's handler callback
29 * with following meaning:
31 * UPROBE_HANDLER_REMOVE
32 * - Remove the uprobe breakpoint from current->mm.
33 * UPROBE_HANDLER_IGNORE
34 * - Ignore ret_handler callback for this consumer.
36 #define UPROBE_HANDLER_REMOVE 1
37 #define UPROBE_HANDLER_IGNORE 2
39 #define MAX_URETPROBE_DEPTH 64
41 struct uprobe_consumer
{
43 * handler() can return UPROBE_HANDLER_REMOVE to signal the need to
44 * unregister uprobe for current process. If UPROBE_HANDLER_REMOVE is
45 * returned, filter() callback has to be implemented as well and it
46 * should return false to "confirm" the decision to uninstall uprobe
47 * for the current process. If filter() is omitted or returns true,
48 * UPROBE_HANDLER_REMOVE is effectively ignored.
50 int (*handler
)(struct uprobe_consumer
*self
, struct pt_regs
*regs
, __u64
*data
);
51 int (*ret_handler
)(struct uprobe_consumer
*self
,
53 struct pt_regs
*regs
, __u64
*data
);
54 bool (*filter
)(struct uprobe_consumer
*self
, struct mm_struct
*mm
);
56 struct list_head cons_node
;
58 __u64 id
; /* set when uprobe_consumer is registered */
62 #include <asm/uprobes.h>
64 enum uprobe_task_state
{
71 /* The state of hybrid-lifetime uprobe inside struct return_instance */
73 HPROBE_LEASED
, /* uretprobes_srcu-protected uprobe */
74 HPROBE_STABLE
, /* refcounted uprobe */
75 HPROBE_GONE
, /* NULL uprobe, SRCU expired, refcount failed */
76 HPROBE_CONSUMED
, /* uprobe "consumed" by uretprobe handler */
80 * Hybrid lifetime uprobe. Represents a uprobe instance that could be either
81 * SRCU protected (with SRCU protection eventually potentially timing out),
82 * refcounted using uprobe->ref, or there could be no valid uprobe (NULL).
84 * hprobe's internal state is setup such that background timer thread can
85 * atomically "downgrade" temporarily RCU-protected uprobe into refcounted one
86 * (or no uprobe, if refcounting failed).
88 * *stable* pointer always point to the uprobe (or could be NULL if there is
89 * was no valid underlying uprobe to begin with).
91 * *leased* pointer is the key to achieving race-free atomic lifetime state
92 * transition and can have three possible states:
93 * - either the same non-NULL value as *stable*, in which case uprobe is
95 * - NULL, in which case uprobe (if there is any) is refcounted;
96 * - special __UPROBE_DEAD value, which represents an uprobe that was SRCU
97 * protected initially, but SRCU period timed out and we attempted to
98 * convert it to refcounted, but refcount_inc_not_zero() failed, because
99 * uprobe effectively went away (the last consumer unsubscribed). In this
100 * case it's important to know that *stable* pointer (which still has
101 * non-NULL uprobe pointer) shouldn't be used, because lifetime of
102 * underlying uprobe is not guaranteed anymore. __UPROBE_DEAD is just an
103 * internal marker and is handled transparently by hprobe_fetch() helper.
105 * When uprobe is SRCU-protected, we also record srcu_idx value, necessary for
108 * See hprobe_expire() and hprobe_fetch() for details of race-free uprobe
109 * state transitioning details. It all hinges on atomic xchg() over *leaded*
110 * pointer. *stable* pointer, once initially set, is not modified concurrently.
113 enum hprobe_state state
;
115 struct uprobe
*uprobe
;
119 * uprobe_task: Metadata of a task while it singlesteps.
122 enum uprobe_task_state state
;
125 struct return_instance
*return_instances
;
129 struct arch_uprobe_task autask
;
134 struct callback_head dup_xol_work
;
135 unsigned long dup_xol_addr
;
139 struct uprobe
*active_uprobe
;
140 struct timer_list ri_timer
;
141 unsigned long xol_vaddr
;
143 struct arch_uprobe
*auprobe
;
146 struct return_consumer
{
151 struct return_instance
{
152 struct hprobe hprobe
;
154 unsigned long stack
; /* stack pointer */
155 unsigned long orig_ret_vaddr
; /* original return address */
156 bool chained
; /* true, if instance is nested */
159 struct return_instance
*next
; /* keep as stack */
162 struct return_consumer consumers
[] __counted_by(consumers_cnt
);
163 } ____cacheline_aligned
;
173 struct uprobes_state
{
174 struct xol_area
*xol_area
;
177 extern void __init
uprobes_init(void);
178 extern int set_swbp(struct arch_uprobe
*aup
, struct mm_struct
*mm
, unsigned long vaddr
);
179 extern int set_orig_insn(struct arch_uprobe
*aup
, struct mm_struct
*mm
, unsigned long vaddr
);
180 extern bool is_swbp_insn(uprobe_opcode_t
*insn
);
181 extern bool is_trap_insn(uprobe_opcode_t
*insn
);
182 extern unsigned long uprobe_get_swbp_addr(struct pt_regs
*regs
);
183 extern unsigned long uprobe_get_trap_addr(struct pt_regs
*regs
);
184 extern int uprobe_write_opcode(struct arch_uprobe
*auprobe
, struct mm_struct
*mm
, unsigned long vaddr
, uprobe_opcode_t
);
185 extern struct uprobe
*uprobe_register(struct inode
*inode
, loff_t offset
, loff_t ref_ctr_offset
, struct uprobe_consumer
*uc
);
186 extern int uprobe_apply(struct uprobe
*uprobe
, struct uprobe_consumer
*uc
, bool);
187 extern void uprobe_unregister_nosync(struct uprobe
*uprobe
, struct uprobe_consumer
*uc
);
188 extern void uprobe_unregister_sync(void);
189 extern int uprobe_mmap(struct vm_area_struct
*vma
);
190 extern void uprobe_munmap(struct vm_area_struct
*vma
, unsigned long start
, unsigned long end
);
191 extern void uprobe_start_dup_mmap(void);
192 extern void uprobe_end_dup_mmap(void);
193 extern void uprobe_dup_mmap(struct mm_struct
*oldmm
, struct mm_struct
*newmm
);
194 extern void uprobe_free_utask(struct task_struct
*t
);
195 extern void uprobe_copy_process(struct task_struct
*t
, unsigned long flags
);
196 extern int uprobe_post_sstep_notifier(struct pt_regs
*regs
);
197 extern int uprobe_pre_sstep_notifier(struct pt_regs
*regs
);
198 extern void uprobe_notify_resume(struct pt_regs
*regs
);
199 extern bool uprobe_deny_signal(void);
200 extern bool arch_uprobe_skip_sstep(struct arch_uprobe
*aup
, struct pt_regs
*regs
);
201 extern void uprobe_clear_state(struct mm_struct
*mm
);
202 extern int arch_uprobe_analyze_insn(struct arch_uprobe
*aup
, struct mm_struct
*mm
, unsigned long addr
);
203 extern int arch_uprobe_pre_xol(struct arch_uprobe
*aup
, struct pt_regs
*regs
);
204 extern int arch_uprobe_post_xol(struct arch_uprobe
*aup
, struct pt_regs
*regs
);
205 extern bool arch_uprobe_xol_was_trapped(struct task_struct
*tsk
);
206 extern int arch_uprobe_exception_notify(struct notifier_block
*self
, unsigned long val
, void *data
);
207 extern void arch_uprobe_abort_xol(struct arch_uprobe
*aup
, struct pt_regs
*regs
);
208 extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr
, struct pt_regs
*regs
);
209 extern bool arch_uretprobe_is_alive(struct return_instance
*ret
, enum rp_check ctx
, struct pt_regs
*regs
);
210 extern bool arch_uprobe_ignore(struct arch_uprobe
*aup
, struct pt_regs
*regs
);
211 extern void arch_uprobe_copy_ixol(struct page
*page
, unsigned long vaddr
,
212 void *src
, unsigned long len
);
213 extern void uprobe_handle_trampoline(struct pt_regs
*regs
);
214 extern void *arch_uprobe_trampoline(unsigned long *psize
);
215 extern unsigned long uprobe_get_trampoline_vaddr(void);
216 #else /* !CONFIG_UPROBES */
217 struct uprobes_state
{
220 static inline void uprobes_init(void)
224 #define uprobe_get_trap_addr(regs) instruction_pointer(regs)
226 static inline struct uprobe
*
227 uprobe_register(struct inode
*inode
, loff_t offset
, loff_t ref_ctr_offset
, struct uprobe_consumer
*uc
)
229 return ERR_PTR(-ENOSYS
);
232 uprobe_apply(struct uprobe
* uprobe
, struct uprobe_consumer
*uc
, bool add
)
237 uprobe_unregister_nosync(struct uprobe
*uprobe
, struct uprobe_consumer
*uc
)
240 static inline void uprobe_unregister_sync(void)
243 static inline int uprobe_mmap(struct vm_area_struct
*vma
)
248 uprobe_munmap(struct vm_area_struct
*vma
, unsigned long start
, unsigned long end
)
251 static inline void uprobe_start_dup_mmap(void)
254 static inline void uprobe_end_dup_mmap(void)
258 uprobe_dup_mmap(struct mm_struct
*oldmm
, struct mm_struct
*newmm
)
261 static inline void uprobe_notify_resume(struct pt_regs
*regs
)
264 static inline bool uprobe_deny_signal(void)
268 static inline void uprobe_free_utask(struct task_struct
*t
)
271 static inline void uprobe_copy_process(struct task_struct
*t
, unsigned long flags
)
274 static inline void uprobe_clear_state(struct mm_struct
*mm
)
277 #endif /* !CONFIG_UPROBES */
278 #endif /* _LINUX_UPROBES_H */