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
20struct uprobe;
21struct vm_area_struct;
22struct mm_struct;
23struct inode;
24struct notifier_block;
25struct page;
26
27/*
28 * Allowed return values from uprobe consumer's handler callback
29 * with following meaning:
30 *
31 * UPROBE_HANDLER_REMOVE
32 * - Remove the uprobe breakpoint from current->mm.
33 * UPROBE_HANDLER_IGNORE
34 * - Ignore ret_handler callback for this consumer.
35 */
36#define UPROBE_HANDLER_REMOVE 1
37#define UPROBE_HANDLER_IGNORE 2
38
39#define MAX_URETPROBE_DEPTH 64
40
41struct uprobe_consumer {
42 /*
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.
49 */
50 int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs, __u64 *data);
51 int (*ret_handler)(struct uprobe_consumer *self,
52 unsigned long func,
53 struct pt_regs *regs, __u64 *data);
54 bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm);
55
56 struct list_head cons_node;
57
58 __u64 id; /* set when uprobe_consumer is registered */
59};
60
61#ifdef CONFIG_UPROBES
62#include <asm/uprobes.h>
63
64enum uprobe_task_state {
65 UTASK_RUNNING,
66 UTASK_SSTEP,
67 UTASK_SSTEP_ACK,
68 UTASK_SSTEP_TRAPPED,
69};
70
71/* The state of hybrid-lifetime uprobe inside struct return_instance */
72enum hprobe_state {
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 */
77};
78
79/*
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).
83 *
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).
87 *
88 * *stable* pointer always point to the uprobe (or could be NULL if there is
89 * was no valid underlying uprobe to begin with).
90 *
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
94 * SRCU-protected;
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.
104 *
105 * When uprobe is SRCU-protected, we also record srcu_idx value, necessary for
106 * SRCU unlocking.
107 *
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.
111 */
112struct hprobe {
113 enum hprobe_state state;
114 int srcu_idx;
115 struct uprobe *uprobe;
116};
117
118/*
119 * uprobe_task: Metadata of a task while it singlesteps.
120 */
121struct uprobe_task {
122 enum uprobe_task_state state;
123
124 unsigned int depth;
125 struct return_instance *return_instances;
126
127 union {
128 struct {
129 struct arch_uprobe_task autask;
130 unsigned long vaddr;
131 };
132
133 struct {
134 struct callback_head dup_xol_work;
135 unsigned long dup_xol_addr;
136 };
137 };
138
139 struct uprobe *active_uprobe;
140 struct timer_list ri_timer;
141 unsigned long xol_vaddr;
142
143 struct arch_uprobe *auprobe;
144};
145
146struct return_consumer {
147 __u64 cookie;
148 __u64 id;
149};
150
151struct return_instance {
152 struct hprobe hprobe;
153 unsigned long func;
154 unsigned long stack; /* stack pointer */
155 unsigned long orig_ret_vaddr; /* original return address */
156 bool chained; /* true, if instance is nested */
157 int consumers_cnt;
158
159 struct return_instance *next; /* keep as stack */
160 struct rcu_head rcu;
161
162 struct return_consumer consumers[] __counted_by(consumers_cnt);
163} ____cacheline_aligned;
164
165enum rp_check {
166 RP_CHECK_CALL,
167 RP_CHECK_CHAIN_CALL,
168 RP_CHECK_RET,
169};
170
171struct xol_area;
172
173struct uprobes_state {
174 struct xol_area *xol_area;
175};
176
177extern void __init uprobes_init(void);
178extern int set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
179extern int set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
180extern bool is_swbp_insn(uprobe_opcode_t *insn);
181extern bool is_trap_insn(uprobe_opcode_t *insn);
182extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
183extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
184extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t);
185extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
186extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool);
187extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc);
188extern void uprobe_unregister_sync(void);
189extern int uprobe_mmap(struct vm_area_struct *vma);
190extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
191extern void uprobe_start_dup_mmap(void);
192extern void uprobe_end_dup_mmap(void);
193extern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
194extern void uprobe_free_utask(struct task_struct *t);
195extern void uprobe_copy_process(struct task_struct *t, unsigned long flags);
196extern int uprobe_post_sstep_notifier(struct pt_regs *regs);
197extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
198extern void uprobe_notify_resume(struct pt_regs *regs);
199extern bool uprobe_deny_signal(void);
200extern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
201extern void uprobe_clear_state(struct mm_struct *mm);
202extern int arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
203extern int arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
204extern int arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
205extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
206extern int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
207extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
208extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
209extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, struct pt_regs *regs);
210extern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
211extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
212 void *src, unsigned long len);
213extern void uprobe_handle_trampoline(struct pt_regs *regs);
214extern void *arch_uprobe_trampoline(unsigned long *psize);
215extern unsigned long uprobe_get_trampoline_vaddr(void);
216#else /* !CONFIG_UPROBES */
217struct uprobes_state {
218};
219
220static inline void uprobes_init(void)
221{
222}
223
224#define uprobe_get_trap_addr(regs) instruction_pointer(regs)
225
226static inline struct uprobe *
227uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
228{
229 return ERR_PTR(-ENOSYS);
230}
231static inline int
232uprobe_apply(struct uprobe* uprobe, struct uprobe_consumer *uc, bool add)
233{
234 return -ENOSYS;
235}
236static inline void
237uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc)
238{
239}
240static inline void uprobe_unregister_sync(void)
241{
242}
243static inline int uprobe_mmap(struct vm_area_struct *vma)
244{
245 return 0;
246}
247static inline void
248uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
249{
250}
251static inline void uprobe_start_dup_mmap(void)
252{
253}
254static inline void uprobe_end_dup_mmap(void)
255{
256}
257static inline void
258uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
259{
260}
261static inline void uprobe_notify_resume(struct pt_regs *regs)
262{
263}
264static inline bool uprobe_deny_signal(void)
265{
266 return false;
267}
268static inline void uprobe_free_utask(struct task_struct *t)
269{
270}
271static inline void uprobe_copy_process(struct task_struct *t, unsigned long flags)
272{
273}
274static inline void uprobe_clear_state(struct mm_struct *mm)
275{
276}
277#endif /* !CONFIG_UPROBES */
278#endif /* _LINUX_UPROBES_H */