Merge tag 'riscv-for-linus-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / include / linux / uprobes.h
CommitLineData
1a59d1b8 1/* SPDX-License-Identifier: GPL-2.0-or-later */
2b144498
SD
2#ifndef _LINUX_UPROBES_H
3#define _LINUX_UPROBES_H
4/*
7b2d81d4 5 * User-space Probes (UProbes)
2b144498 6 *
35aa621b 7 * Copyright (C) IBM Corporation, 2008-2012
2b144498
SD
8 * Authors:
9 * Srikar Dronamraju
10 * Jim Keniston
90eec103 11 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
2b144498
SD
12 */
13
14#include <linux/errno.h>
15#include <linux/rbtree.h>
32473431 16#include <linux/types.h>
e8f4aa60 17#include <linux/wait.h>
dd1a7567 18#include <linux/timer.h>
8622e45b 19#include <linux/seqlock.h>
2b144498 20
3c83a9ad 21struct uprobe;
2b144498 22struct vm_area_struct;
d4b3b638
SD
23struct mm_struct;
24struct inode;
3820b4d2 25struct notifier_block;
72e6ae28 26struct page;
0326f5a9 27
4d756095
JO
28/*
29 * Allowed return values from uprobe consumer's handler callback
30 * with following meaning:
31 *
32 * UPROBE_HANDLER_REMOVE
33 * - Remove the uprobe breakpoint from current->mm.
34 * UPROBE_HANDLER_IGNORE
35 * - Ignore ret_handler callback for this consumer.
36 */
da1816b1 37#define UPROBE_HANDLER_REMOVE 1
4d756095 38#define UPROBE_HANDLER_IGNORE 2
da1816b1 39
ded49c55
AA
40#define MAX_URETPROBE_DEPTH 64
41
fa6192ad
JO
42#define UPROBE_NO_TRAMPOLINE_VADDR (~0UL)
43
2b144498 44struct uprobe_consumer {
cc01bd04
AN
45 /*
46 * handler() can return UPROBE_HANDLER_REMOVE to signal the need to
47 * unregister uprobe for current process. If UPROBE_HANDLER_REMOVE is
48 * returned, filter() callback has to be implemented as well and it
49 * should return false to "confirm" the decision to uninstall uprobe
50 * for the current process. If filter() is omitted or returns true,
51 * UPROBE_HANDLER_REMOVE is effectively ignored.
52 */
da09a9e0 53 int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs, __u64 *data);
ea024870
AA
54 int (*ret_handler)(struct uprobe_consumer *self,
55 unsigned long func,
da09a9e0 56 struct pt_regs *regs, __u64 *data);
59da880a 57 bool (*filter)(struct uprobe_consumer *self, struct mm_struct *mm);
2b144498 58
cc01bd04 59 struct list_head cons_node;
4d756095
JO
60
61 __u64 id; /* set when uprobe_consumer is registered */
2b144498
SD
62};
63
2b144498 64#ifdef CONFIG_UPROBES
c912dae6
ON
65#include <asm/uprobes.h>
66
0326f5a9
SD
67enum uprobe_task_state {
68 UTASK_RUNNING,
0326f5a9
SD
69 UTASK_SSTEP,
70 UTASK_SSTEP_ACK,
71 UTASK_SSTEP_TRAPPED,
72};
73
dd1a7567
AN
74/* The state of hybrid-lifetime uprobe inside struct return_instance */
75enum hprobe_state {
76 HPROBE_LEASED, /* uretprobes_srcu-protected uprobe */
77 HPROBE_STABLE, /* refcounted uprobe */
78 HPROBE_GONE, /* NULL uprobe, SRCU expired, refcount failed */
79 HPROBE_CONSUMED, /* uprobe "consumed" by uretprobe handler */
80};
81
82/*
83 * Hybrid lifetime uprobe. Represents a uprobe instance that could be either
84 * SRCU protected (with SRCU protection eventually potentially timing out),
85 * refcounted using uprobe->ref, or there could be no valid uprobe (NULL).
86 *
87 * hprobe's internal state is setup such that background timer thread can
88 * atomically "downgrade" temporarily RCU-protected uprobe into refcounted one
89 * (or no uprobe, if refcounting failed).
90 *
91 * *stable* pointer always point to the uprobe (or could be NULL if there is
92 * was no valid underlying uprobe to begin with).
93 *
94 * *leased* pointer is the key to achieving race-free atomic lifetime state
95 * transition and can have three possible states:
96 * - either the same non-NULL value as *stable*, in which case uprobe is
97 * SRCU-protected;
98 * - NULL, in which case uprobe (if there is any) is refcounted;
99 * - special __UPROBE_DEAD value, which represents an uprobe that was SRCU
100 * protected initially, but SRCU period timed out and we attempted to
101 * convert it to refcounted, but refcount_inc_not_zero() failed, because
102 * uprobe effectively went away (the last consumer unsubscribed). In this
103 * case it's important to know that *stable* pointer (which still has
104 * non-NULL uprobe pointer) shouldn't be used, because lifetime of
105 * underlying uprobe is not guaranteed anymore. __UPROBE_DEAD is just an
106 * internal marker and is handled transparently by hprobe_fetch() helper.
107 *
108 * When uprobe is SRCU-protected, we also record srcu_idx value, necessary for
109 * SRCU unlocking.
110 *
111 * See hprobe_expire() and hprobe_fetch() for details of race-free uprobe
112 * state transitioning details. It all hinges on atomic xchg() over *leaded*
113 * pointer. *stable* pointer, once initially set, is not modified concurrently.
114 */
115struct hprobe {
116 enum hprobe_state state;
117 int srcu_idx;
118 struct uprobe *uprobe;
119};
120
0326f5a9
SD
121/*
122 * uprobe_task: Metadata of a task while it singlesteps.
123 */
124struct uprobe_task {
125 enum uprobe_task_state state;
0326f5a9 126
c554aa9c
CJ
127 unsigned int depth;
128 struct return_instance *return_instances;
129
8622e45b
AN
130 struct return_instance *ri_pool;
131 struct timer_list ri_timer;
132 seqcount_t ri_seqcount;
133
32473431
ON
134 union {
135 struct {
136 struct arch_uprobe_task autask;
137 unsigned long vaddr;
138 };
139
140 struct {
141 struct callback_head dup_xol_work;
142 unsigned long dup_xol_addr;
143 };
144 };
0326f5a9 145
32473431 146 struct uprobe *active_uprobe;
0326f5a9 147 unsigned long xol_vaddr;
83179cd6 148 bool signal_denied;
32473431 149
cfa7f3d2 150 struct arch_uprobe *auprobe;
0326f5a9
SD
151};
152
4d756095
JO
153struct return_consumer {
154 __u64 cookie;
155 __u64 id;
156};
157
97da8976 158struct return_instance {
dd1a7567 159 struct hprobe hprobe;
97da8976 160 unsigned long func;
7b868e48 161 unsigned long stack; /* stack pointer */
97da8976
ON
162 unsigned long orig_ret_vaddr; /* original return address */
163 bool chained; /* true, if instance is nested */
2ff913ab 164 int cons_cnt; /* total number of session consumers */
97da8976
ON
165
166 struct return_instance *next; /* keep as stack */
dd1a7567 167 struct rcu_head rcu;
4d756095 168
2ff913ab
AN
169 /* singular pre-allocated return_consumer instance for common case */
170 struct return_consumer consumer;
171 /*
172 * extra return_consumer instances for rare cases of multiple session consumers,
173 * contains (cons_cnt - 1) elements
174 */
175 struct return_consumer *extra_consumers;
dd1a7567 176} ____cacheline_aligned;
97da8976 177
86dcb702
ON
178enum rp_check {
179 RP_CHECK_CALL,
db087ef6 180 RP_CHECK_CHAIN_CALL,
86dcb702
ON
181 RP_CHECK_RET,
182};
183
c912dae6 184struct xol_area;
d4b3b638
SD
185
186struct uprobes_state {
187 struct xol_area *xol_area;
188};
647c42df 189
aad42dd4 190extern void __init uprobes_init(void);
8a557742
DH
191extern int set_swbp(struct arch_uprobe *aup, struct vm_area_struct *vma, unsigned long vaddr);
192extern int set_orig_insn(struct arch_uprobe *aup, struct vm_area_struct *vma, unsigned long vaddr);
271a9c35
BH
193extern bool is_swbp_insn(uprobe_opcode_t *insn);
194extern bool is_trap_insn(uprobe_opcode_t *insn);
195extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
b02ef20a 196extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
8a557742 197extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, unsigned long vaddr, uprobe_opcode_t);
3c83a9ad
ON
198extern struct uprobe *uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
199extern int uprobe_apply(struct uprobe *uprobe, struct uprobe_consumer *uc, bool);
04b01625
PZ
200extern void uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc);
201extern void uprobe_unregister_sync(void);
7b2d81d4 202extern int uprobe_mmap(struct vm_area_struct *vma);
cbc91f71 203extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
32cdba1e
ON
204extern void uprobe_start_dup_mmap(void);
205extern void uprobe_end_dup_mmap(void);
f8ac4ec9 206extern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
0326f5a9 207extern void uprobe_free_utask(struct task_struct *t);
3ab67966 208extern void uprobe_copy_process(struct task_struct *t, unsigned long flags);
0326f5a9
SD
209extern int uprobe_post_sstep_notifier(struct pt_regs *regs);
210extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
211extern void uprobe_notify_resume(struct pt_regs *regs);
212extern bool uprobe_deny_signal(void);
c2d3f25d 213extern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
d4b3b638 214extern void uprobe_clear_state(struct mm_struct *mm);
3820b4d2
DL
215extern int arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
216extern int arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
217extern int arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
218extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
219extern int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
220extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
221extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
86dcb702 222extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, struct pt_regs *regs);
271a9c35
BH
223extern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
224extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
72e6ae28 225 void *src, unsigned long len);
ff474a78
JO
226extern void uprobe_handle_trampoline(struct pt_regs *regs);
227extern void *arch_uprobe_trampoline(unsigned long *psize);
228extern unsigned long uprobe_get_trampoline_vaddr(void);
0326f5a9 229#else /* !CONFIG_UPROBES */
d4b3b638
SD
230struct uprobes_state {
231};
b02ef20a 232
aad42dd4
NA
233static inline void uprobes_init(void)
234{
235}
236
b02ef20a
ON
237#define uprobe_get_trap_addr(regs) instruction_pointer(regs)
238
3c83a9ad 239static inline struct uprobe *
e04332eb 240uprobe_register(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
2b144498 241{
3c83a9ad 242 return ERR_PTR(-ENOSYS);
2b144498 243}
bdf8647c 244static inline int
3c83a9ad 245uprobe_apply(struct uprobe* uprobe, struct uprobe_consumer *uc, bool add)
bdf8647c
ON
246{
247 return -ENOSYS;
248}
7b2d81d4 249static inline void
04b01625
PZ
250uprobe_unregister_nosync(struct uprobe *uprobe, struct uprobe_consumer *uc)
251{
252}
253static inline void uprobe_unregister_sync(void)
2b144498
SD
254{
255}
7b2d81d4 256static inline int uprobe_mmap(struct vm_area_struct *vma)
2b144498
SD
257{
258 return 0;
259}
cbc91f71
SD
260static inline void
261uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
682968e0
SD
262{
263}
32cdba1e
ON
264static inline void uprobe_start_dup_mmap(void)
265{
266}
267static inline void uprobe_end_dup_mmap(void)
268{
269}
f8ac4ec9
ON
270static inline void
271uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
272{
273}
0326f5a9
SD
274static inline void uprobe_notify_resume(struct pt_regs *regs)
275{
276}
277static inline bool uprobe_deny_signal(void)
278{
279 return false;
280}
0326f5a9
SD
281static inline void uprobe_free_utask(struct task_struct *t)
282{
283}
3ab67966 284static inline void uprobe_copy_process(struct task_struct *t, unsigned long flags)
0326f5a9
SD
285{
286}
d4b3b638
SD
287static inline void uprobe_clear_state(struct mm_struct *mm)
288{
289}
0326f5a9 290#endif /* !CONFIG_UPROBES */
2b144498 291#endif /* _LINUX_UPROBES_H */