xref: /linux-6.15/include/linux/uprobes.h (revision a22b8f4b)
1 #ifndef _LINUX_UPROBES_H
2 #define _LINUX_UPROBES_H
3 /*
4  * User-space Probes (UProbes)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Copyright (C) IBM Corporation, 2008-2012
21  * Authors:
22  *	Srikar Dronamraju
23  *	Jim Keniston
24  * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <[email protected]>
25  */
26 
27 #include <linux/errno.h>
28 #include <linux/rbtree.h>
29 
30 struct vm_area_struct;
31 struct mm_struct;
32 struct inode;
33 
34 #ifdef CONFIG_ARCH_SUPPORTS_UPROBES
35 # include <asm/uprobes.h>
36 #endif
37 
38 #define UPROBE_HANDLER_REMOVE		1
39 #define UPROBE_HANDLER_MASK		1
40 
41 #define MAX_URETPROBE_DEPTH		64
42 
43 enum uprobe_filter_ctx {
44 	UPROBE_FILTER_REGISTER,
45 	UPROBE_FILTER_UNREGISTER,
46 	UPROBE_FILTER_MMAP,
47 };
48 
49 struct uprobe_consumer {
50 	int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
51 	int (*ret_handler)(struct uprobe_consumer *self,
52 				unsigned long func,
53 				struct pt_regs *regs);
54 	bool (*filter)(struct uprobe_consumer *self,
55 				enum uprobe_filter_ctx ctx,
56 				struct mm_struct *mm);
57 
58 	struct uprobe_consumer *next;
59 };
60 
61 #ifdef CONFIG_UPROBES
62 enum uprobe_task_state {
63 	UTASK_RUNNING,
64 	UTASK_SSTEP,
65 	UTASK_SSTEP_ACK,
66 	UTASK_SSTEP_TRAPPED,
67 };
68 
69 /*
70  * uprobe_task: Metadata of a task while it singlesteps.
71  */
72 struct uprobe_task {
73 	enum uprobe_task_state		state;
74 	struct arch_uprobe_task		autask;
75 
76 	struct return_instance		*return_instances;
77 	unsigned int			depth;
78 	struct uprobe			*active_uprobe;
79 
80 	unsigned long			xol_vaddr;
81 	unsigned long			vaddr;
82 };
83 
84 /*
85  * On a breakpoint hit, thread contests for a slot.  It frees the
86  * slot after singlestep. Currently a fixed number of slots are
87  * allocated.
88  */
89 struct xol_area {
90 	wait_queue_head_t 	wq;		/* if all slots are busy */
91 	atomic_t 		slot_count;	/* number of in-use slots */
92 	unsigned long 		*bitmap;	/* 0 = free slot */
93 	struct page 		*page;
94 
95 	/*
96 	 * We keep the vma's vm_start rather than a pointer to the vma
97 	 * itself.  The probed process or a naughty kernel module could make
98 	 * the vma go away, and we must handle that reasonably gracefully.
99 	 */
100 	unsigned long 		vaddr;		/* Page(s) of instruction slots */
101 };
102 
103 struct uprobes_state {
104 	struct xol_area		*xol_area;
105 };
106 
107 extern int __weak set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
108 extern int __weak set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
109 extern bool __weak is_swbp_insn(uprobe_opcode_t *insn);
110 extern bool __weak is_trap_insn(uprobe_opcode_t *insn);
111 extern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
112 extern int uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool);
113 extern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
114 extern int uprobe_mmap(struct vm_area_struct *vma);
115 extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
116 extern void uprobe_start_dup_mmap(void);
117 extern void uprobe_end_dup_mmap(void);
118 extern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
119 extern void uprobe_free_utask(struct task_struct *t);
120 extern void uprobe_copy_process(struct task_struct *t);
121 extern unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs);
122 extern int uprobe_post_sstep_notifier(struct pt_regs *regs);
123 extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
124 extern void uprobe_notify_resume(struct pt_regs *regs);
125 extern bool uprobe_deny_signal(void);
126 extern bool __weak arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
127 extern void uprobe_clear_state(struct mm_struct *mm);
128 #else /* !CONFIG_UPROBES */
129 struct uprobes_state {
130 };
131 static inline int
132 uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
133 {
134 	return -ENOSYS;
135 }
136 static inline int
137 uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool add)
138 {
139 	return -ENOSYS;
140 }
141 static inline void
142 uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
143 {
144 }
145 static inline int uprobe_mmap(struct vm_area_struct *vma)
146 {
147 	return 0;
148 }
149 static inline void
150 uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
151 {
152 }
153 static inline void uprobe_start_dup_mmap(void)
154 {
155 }
156 static inline void uprobe_end_dup_mmap(void)
157 {
158 }
159 static inline void
160 uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
161 {
162 }
163 static inline void uprobe_notify_resume(struct pt_regs *regs)
164 {
165 }
166 static inline bool uprobe_deny_signal(void)
167 {
168 	return false;
169 }
170 static inline unsigned long uprobe_get_swbp_addr(struct pt_regs *regs)
171 {
172 	return 0;
173 }
174 static inline void uprobe_free_utask(struct task_struct *t)
175 {
176 }
177 static inline void uprobe_copy_process(struct task_struct *t)
178 {
179 }
180 static inline void uprobe_clear_state(struct mm_struct *mm)
181 {
182 }
183 #endif /* !CONFIG_UPROBES */
184 #endif	/* _LINUX_UPROBES_H */
185