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