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 
19 struct uprobe;
20 struct vm_area_struct;
21 struct mm_struct;
22 struct inode;
23 struct notifier_block;
24 struct page;
25 
26 #define UPROBE_HANDLER_REMOVE		1
27 #define UPROBE_HANDLER_MASK		1
28 
29 #define MAX_URETPROBE_DEPTH		64
30 
31 struct uprobe_consumer {
32 	/*
33 	 * handler() can return UPROBE_HANDLER_REMOVE to signal the need to
34 	 * unregister uprobe for current process. If UPROBE_HANDLER_REMOVE is
35 	 * returned, filter() callback has to be implemented as well and it
36 	 * should return false to "confirm" the decision to uninstall uprobe
37 	 * for the current process. If filter() is omitted or returns true,
38 	 * UPROBE_HANDLER_REMOVE is effectively ignored.
39 	 */
40 	int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
41 	int (*ret_handler)(struct uprobe_consumer *self,
42 				unsigned long func,
43 				struct pt_regs *regs);
44 	bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm);
45 
46 	struct list_head cons_node;
47 };
48 
49 #ifdef CONFIG_UPROBES
50 #include <asm/uprobes.h>
51 
52 enum uprobe_task_state {
53 	UTASK_RUNNING,
54 	UTASK_SSTEP,
55 	UTASK_SSTEP_ACK,
56 	UTASK_SSTEP_TRAPPED,
57 };
58 
59 /*
60  * uprobe_task: Metadata of a task while it singlesteps.
61  */
62 struct uprobe_task {
63 	enum uprobe_task_state		state;
64 
65 	union {
66 		struct {
67 			struct arch_uprobe_task	autask;
68 			unsigned long		vaddr;
69 		};
70 
71 		struct {
72 			struct callback_head	dup_xol_work;
73 			unsigned long		dup_xol_addr;
74 		};
75 	};
76 
77 	struct uprobe			*active_uprobe;
78 	unsigned long			xol_vaddr;
79 
80 	struct arch_uprobe              *auprobe;
81 
82 	struct return_instance		*return_instances;
83 	unsigned int			depth;
84 };
85 
86 struct return_instance {
87 	struct uprobe		*uprobe;
88 	unsigned long		func;
89 	unsigned long		stack;		/* stack pointer */
90 	unsigned long		orig_ret_vaddr; /* original return address */
91 	bool			chained;	/* true, if instance is nested */
92 
93 	struct return_instance	*next;		/* keep as stack */
94 };
95 
96 enum rp_check {
97 	RP_CHECK_CALL,
98 	RP_CHECK_CHAIN_CALL,
99 	RP_CHECK_RET,
100 };
101 
102 struct xol_area;
103 
104 struct uprobes_state {
105 	struct xol_area		*xol_area;
106 };
107 
108 extern void __init uprobes_init(void);
109 extern int set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
110 extern int set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
111 extern bool is_swbp_insn(uprobe_opcode_t *insn);
112 extern bool is_trap_insn(uprobe_opcode_t *insn);
113 extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
114 extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
115 extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t);
116 extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
117 extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool);
118 extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc);
119 extern void uprobe_unregister_sync(void);
120 extern int uprobe_mmap(struct vm_area_struct *vma);
121 extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
122 extern void uprobe_start_dup_mmap(void);
123 extern void uprobe_end_dup_mmap(void);
124 extern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
125 extern void uprobe_free_utask(struct task_struct *t);
126 extern void uprobe_copy_process(struct task_struct *t, unsigned long flags);
127 extern int uprobe_post_sstep_notifier(struct pt_regs *regs);
128 extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
129 extern void uprobe_notify_resume(struct pt_regs *regs);
130 extern bool uprobe_deny_signal(void);
131 extern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
132 extern void uprobe_clear_state(struct mm_struct *mm);
133 extern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
134 extern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
135 extern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
136 extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
137 extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
138 extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
139 extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
140 extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, struct pt_regs *regs);
141 extern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
142 extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
143 					 void *src, unsigned long len);
144 extern void uprobe_handle_trampoline(struct pt_regs *regs);
145 extern void *arch_uprobe_trampoline(unsigned long *psize);
146 extern unsigned long uprobe_get_trampoline_vaddr(void);
147 #else /* !CONFIG_UPROBES */
148 struct uprobes_state {
149 };
150 
uprobes_init(void)151 static inline void uprobes_init(void)
152 {
153 }
154 
155 #define uprobe_get_trap_addr(regs)	instruction_pointer(regs)
156 
157 static inline struct uprobe *
uprobe_register(struct inode * inode,loff_t offset,loff_t ref_ctr_offset,struct uprobe_consumer * uc)158 uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
159 {
160 	return ERR_PTR(-ENOSYS);
161 }
162 static inline int
uprobe_apply(struct uprobe * uprobe,struct uprobe_consumer * uc,bool add)163 uprobe_apply(struct uprobe* uprobe, struct uprobe_consumer *uc, bool add)
164 {
165 	return -ENOSYS;
166 }
167 static inline void
uprobe_unregister_nosync(struct uprobe * uprobe,struct uprobe_consumer * uc)168 uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc)
169 {
170 }
uprobe_unregister_sync(void)171 static inline void uprobe_unregister_sync(void)
172 {
173 }
uprobe_mmap(struct vm_area_struct * vma)174 static inline int uprobe_mmap(struct vm_area_struct *vma)
175 {
176 	return 0;
177 }
178 static inline void
uprobe_munmap(struct vm_area_struct * vma,unsigned long start,unsigned long end)179 uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
180 {
181 }
uprobe_start_dup_mmap(void)182 static inline void uprobe_start_dup_mmap(void)
183 {
184 }
uprobe_end_dup_mmap(void)185 static inline void uprobe_end_dup_mmap(void)
186 {
187 }
188 static inline void
uprobe_dup_mmap(struct mm_struct * oldmm,struct mm_struct * newmm)189 uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
190 {
191 }
uprobe_notify_resume(struct pt_regs * regs)192 static inline void uprobe_notify_resume(struct pt_regs *regs)
193 {
194 }
uprobe_deny_signal(void)195 static inline bool uprobe_deny_signal(void)
196 {
197 	return false;
198 }
uprobe_free_utask(struct task_struct * t)199 static inline void uprobe_free_utask(struct task_struct *t)
200 {
201 }
uprobe_copy_process(struct task_struct * t,unsigned long flags)202 static inline void uprobe_copy_process(struct task_struct *t, unsigned long flags)
203 {
204 }
uprobe_clear_state(struct mm_struct * mm)205 static inline void uprobe_clear_state(struct mm_struct *mm)
206 {
207 }
208 #endif /* !CONFIG_UPROBES */
209 #endif	/* _LINUX_UPROBES_H */
210