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