Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2#ifndef _LINUX_UPROBES_H
3#define _LINUX_UPROBES_H
4/*
5 * User-space Probes (UProbes)
6 *
7 * Copyright (C) IBM Corporation, 2008-2012
8 * Authors:
9 * Srikar Dronamraju
10 * Jim Keniston
11 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
12 */
13
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>
19#include <linux/seqlock.h>
20#include <linux/mutex.h>
21
22struct uprobe;
23struct vm_area_struct;
24struct mm_struct;
25struct inode;
26struct notifier_block;
27struct page;
28
29/*
30 * Allowed return values from uprobe consumer's handler callback
31 * with following meaning:
32 *
33 * UPROBE_HANDLER_REMOVE
34 * - Remove the uprobe breakpoint from current->mm.
35 * UPROBE_HANDLER_IGNORE
36 * - Ignore ret_handler callback for this consumer.
37 */
38#define UPROBE_HANDLER_REMOVE 1
39#define UPROBE_HANDLER_IGNORE 2
40
41#define MAX_URETPROBE_DEPTH 64
42
43#define UPROBE_NO_TRAMPOLINE_VADDR (~0UL)
44
45struct uprobe_consumer {
46 /*
47 * handler() can return UPROBE_HANDLER_REMOVE to signal the need to
48 * unregister uprobe for current process. If UPROBE_HANDLER_REMOVE is
49 * returned, filter() callback has to be implemented as well and it
50 * should return false to "confirm" the decision to uninstall uprobe
51 * for the current process. If filter() is omitted or returns true,
52 * UPROBE_HANDLER_REMOVE is effectively ignored.
53 */
54 int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs, __u64 *data);
55 int (*ret_handler)(struct uprobe_consumer *self,
56 unsigned long func,
57 struct pt_regs *regs, __u64 *data);
58 bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm);
59
60 struct list_head cons_node;
61
62 __u64 id; /* set when uprobe_consumer is registered */
63};
64
65#ifdef CONFIG_UPROBES
66#include <asm/uprobes.h>
67
68enum uprobe_task_state {
69 UTASK_RUNNING,
70 UTASK_SSTEP,
71 UTASK_SSTEP_ACK,
72 UTASK_SSTEP_TRAPPED,
73};
74
75/* The state of hybrid-lifetime uprobe inside struct return_instance */
76enum hprobe_state {
77 HPROBE_LEASED, /* uretprobes_srcu-protected uprobe */
78 HPROBE_STABLE, /* refcounted uprobe */
79 HPROBE_GONE, /* NULL uprobe, SRCU expired, refcount failed */
80 HPROBE_CONSUMED, /* uprobe "consumed" by uretprobe handler */
81};
82
83/*
84 * Hybrid lifetime uprobe. Represents a uprobe instance that could be either
85 * SRCU protected (with SRCU protection eventually potentially timing out),
86 * refcounted using uprobe->ref, or there could be no valid uprobe (NULL).
87 *
88 * hprobe's internal state is setup such that background timer thread can
89 * atomically "downgrade" temporarily RCU-protected uprobe into refcounted one
90 * (or no uprobe, if refcounting failed).
91 *
92 * *stable* pointer always point to the uprobe (or could be NULL if there is
93 * was no valid underlying uprobe to begin with).
94 *
95 * *leased* pointer is the key to achieving race-free atomic lifetime state
96 * transition and can have three possible states:
97 * - either the same non-NULL value as *stable*, in which case uprobe is
98 * SRCU-protected;
99 * - NULL, in which case uprobe (if there is any) is refcounted;
100 * - special __UPROBE_DEAD value, which represents an uprobe that was SRCU
101 * protected initially, but SRCU period timed out and we attempted to
102 * convert it to refcounted, but refcount_inc_not_zero() failed, because
103 * uprobe effectively went away (the last consumer unsubscribed). In this
104 * case it's important to know that *stable* pointer (which still has
105 * non-NULL uprobe pointer) shouldn't be used, because lifetime of
106 * underlying uprobe is not guaranteed anymore. __UPROBE_DEAD is just an
107 * internal marker and is handled transparently by hprobe_fetch() helper.
108 *
109 * When uprobe is SRCU-protected, we also record srcu_idx value, necessary for
110 * SRCU unlocking.
111 *
112 * See hprobe_expire() and hprobe_fetch() for details of race-free uprobe
113 * state transitioning details. It all hinges on atomic xchg() over *leaded*
114 * pointer. *stable* pointer, once initially set, is not modified concurrently.
115 */
116struct hprobe {
117 enum hprobe_state state;
118 int srcu_idx;
119 struct uprobe *uprobe;
120};
121
122/*
123 * uprobe_task: Metadata of a task while it singlesteps.
124 */
125struct uprobe_task {
126 enum uprobe_task_state state;
127
128 unsigned int depth;
129 struct return_instance *return_instances;
130
131 struct return_instance *ri_pool;
132 struct timer_list ri_timer;
133 seqcount_t ri_seqcount;
134
135 union {
136 struct {
137 struct arch_uprobe_task autask;
138 unsigned long vaddr;
139 };
140
141 struct {
142 struct callback_head dup_xol_work;
143 unsigned long dup_xol_addr;
144 };
145 };
146
147 struct uprobe *active_uprobe;
148 unsigned long xol_vaddr;
149 bool signal_denied;
150
151 struct arch_uprobe *auprobe;
152};
153
154struct return_consumer {
155 __u64 cookie;
156 __u64 id;
157};
158
159struct return_instance {
160 struct hprobe hprobe;
161 unsigned long func;
162 unsigned long stack; /* stack pointer */
163 unsigned long orig_ret_vaddr; /* original return address */
164 bool chained; /* true, if instance is nested */
165 int cons_cnt; /* total number of session consumers */
166
167 struct return_instance *next; /* keep as stack */
168 struct rcu_head rcu;
169
170 /* singular pre-allocated return_consumer instance for common case */
171 struct return_consumer consumer;
172 /*
173 * extra return_consumer instances for rare cases of multiple session consumers,
174 * contains (cons_cnt - 1) elements
175 */
176 struct return_consumer *extra_consumers;
177} ____cacheline_aligned;
178
179enum rp_check {
180 RP_CHECK_CALL,
181 RP_CHECK_CHAIN_CALL,
182 RP_CHECK_RET,
183};
184
185struct xol_area;
186
187struct uprobes_state {
188 struct xol_area *xol_area;
189#ifdef CONFIG_X86_64
190 struct hlist_head head_tramps;
191#endif
192};
193
194typedef int (*uprobe_write_verify_t)(struct page *page, unsigned long vaddr,
195 uprobe_opcode_t *insn, int nbytes, void *data);
196
197extern void __init uprobes_init(void);
198extern int set_swbp(struct arch_uprobe *aup, struct vm_area_struct *vma, unsigned long vaddr);
199extern int set_orig_insn(struct arch_uprobe *aup, struct vm_area_struct *vma, unsigned long vaddr);
200extern bool is_swbp_insn(uprobe_opcode_t *insn);
201extern bool is_trap_insn(uprobe_opcode_t *insn);
202extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
203extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
204extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, unsigned long vaddr, uprobe_opcode_t,
205 bool is_register);
206extern int uprobe_write(struct arch_uprobe *auprobe, struct vm_area_struct *vma, const unsigned long opcode_vaddr,
207 uprobe_opcode_t *insn, int nbytes, uprobe_write_verify_t verify, bool is_register, bool do_update_ref_ctr,
208 void *data);
209extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
210extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool);
211extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc);
212extern void uprobe_unregister_sync(void);
213extern int uprobe_mmap(struct vm_area_struct *vma);
214extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
215extern void uprobe_start_dup_mmap(void);
216extern void uprobe_end_dup_mmap(void);
217extern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
218extern void uprobe_free_utask(struct task_struct *t);
219extern void uprobe_copy_process(struct task_struct *t, u64 flags);
220extern int uprobe_post_sstep_notifier(struct pt_regs *regs);
221extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
222extern void uprobe_notify_resume(struct pt_regs *regs);
223extern bool uprobe_deny_signal(void);
224extern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
225extern void uprobe_clear_state(struct mm_struct *mm);
226extern int arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
227extern int arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
228extern int arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
229extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
230extern int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
231extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
232extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
233extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, struct pt_regs *regs);
234extern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
235extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
236 void *src, unsigned long len);
237extern void uprobe_handle_trampoline(struct pt_regs *regs);
238extern void *arch_uretprobe_trampoline(unsigned long *psize);
239extern unsigned long uprobe_get_trampoline_vaddr(void);
240extern void uprobe_copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len);
241extern void arch_uprobe_clear_state(struct mm_struct *mm);
242extern void arch_uprobe_init_state(struct mm_struct *mm);
243extern void handle_syscall_uprobe(struct pt_regs *regs, unsigned long bp_vaddr);
244extern void arch_uprobe_optimize(struct arch_uprobe *auprobe, unsigned long vaddr);
245#else /* !CONFIG_UPROBES */
246struct uprobes_state {
247};
248
249static inline void uprobes_init(void)
250{
251}
252
253#define uprobe_get_trap_addr(regs) instruction_pointer(regs)
254
255static inline struct uprobe *
256uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
257{
258 return ERR_PTR(-ENOSYS);
259}
260static inline int
261uprobe_apply(struct uprobe* uprobe, struct uprobe_consumer *uc, bool add)
262{
263 return -ENOSYS;
264}
265static inline void
266uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc)
267{
268}
269static inline void uprobe_unregister_sync(void)
270{
271}
272static inline int uprobe_mmap(struct vm_area_struct *vma)
273{
274 return 0;
275}
276static inline void
277uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
278{
279}
280static inline void uprobe_start_dup_mmap(void)
281{
282}
283static inline void uprobe_end_dup_mmap(void)
284{
285}
286static inline void
287uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
288{
289}
290static inline void uprobe_notify_resume(struct pt_regs *regs)
291{
292}
293static inline bool uprobe_deny_signal(void)
294{
295 return false;
296}
297static inline void uprobe_free_utask(struct task_struct *t)
298{
299}
300static inline void uprobe_copy_process(struct task_struct *t, u64 flags)
301{
302}
303static inline void uprobe_clear_state(struct mm_struct *mm)
304{
305}
306#endif /* !CONFIG_UPROBES */
307#endif /* _LINUX_UPROBES_H */