blob: f46e0ca0169c722b4d5b749b2c1b0b04ab79c380 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Srikar Dronamraju2b144492012-02-09 14:56:42 +05302#ifndef _LINUX_UPROBES_H
3#define _LINUX_UPROBES_H
4/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01005 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05306 *
Ingo Molnar35aa6212012-02-22 11:37:29 +01007 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +05308 * Authors:
9 * Srikar Dronamraju
10 * Jim Keniston
Peter Zijlstra90eec102015-11-16 11:08:45 +010011 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
Srikar Dronamraju2b144492012-02-09 14:56:42 +053012 */
13
14#include <linux/errno.h>
15#include <linux/rbtree.h>
Oleg Nesterov32473432013-11-08 18:52:21 +010016#include <linux/types.h>
Allen Paise8f4aa62016-10-13 10:06:13 +053017#include <linux/wait.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053018
19struct vm_area_struct;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053020struct mm_struct;
21struct inode;
David A. Long3820b4d2013-10-15 17:04:16 -040022struct notifier_block;
Victor Kamensky72e6ae22014-04-29 04:20:52 +010023struct page;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053024
Oleg Nesterovda1816b2012-12-29 17:49:11 +010025#define UPROBE_HANDLER_REMOVE 1
26#define UPROBE_HANDLER_MASK 1
27
Anton Arapovded49c52013-04-03 18:00:37 +020028#define MAX_URETPROBE_DEPTH 64
29
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +010030enum uprobe_filter_ctx {
31 UPROBE_FILTER_REGISTER,
32 UPROBE_FILTER_UNREGISTER,
33 UPROBE_FILTER_MMAP,
34};
35
Srikar Dronamraju2b144492012-02-09 14:56:42 +053036struct uprobe_consumer {
37 int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
Anton Arapovea024872013-04-03 18:00:31 +020038 int (*ret_handler)(struct uprobe_consumer *self,
39 unsigned long func,
40 struct pt_regs *regs);
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +010041 bool (*filter)(struct uprobe_consumer *self,
42 enum uprobe_filter_ctx ctx,
43 struct mm_struct *mm);
Srikar Dronamraju2b144492012-02-09 14:56:42 +053044
45 struct uprobe_consumer *next;
46};
47
Srikar Dronamraju2b144492012-02-09 14:56:42 +053048#ifdef CONFIG_UPROBES
Oleg Nesterovc912dae2013-11-09 19:49:39 +010049#include <asm/uprobes.h>
50
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053051enum uprobe_task_state {
52 UTASK_RUNNING,
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053053 UTASK_SSTEP,
54 UTASK_SSTEP_ACK,
55 UTASK_SSTEP_TRAPPED,
56};
57
58/*
59 * uprobe_task: Metadata of a task while it singlesteps.
60 */
61struct uprobe_task {
62 enum uprobe_task_state state;
Oleg Nesterov32473432013-11-08 18:52:21 +010063
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;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053078
Anton Arapov0dfd0eb2013-04-03 18:00:35 +020079 struct return_instance *return_instances;
Anton Arapovded49c52013-04-03 18:00:37 +020080 unsigned int depth;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053081};
82
Oleg Nesterov97da8972015-07-21 15:40:16 +020083struct return_instance {
84 struct uprobe *uprobe;
85 unsigned long func;
Oleg Nesterov7b868e42015-07-21 15:40:18 +020086 unsigned long stack; /* stack pointer */
Oleg Nesterov97da8972015-07-21 15:40:16 +020087 unsigned long orig_ret_vaddr; /* original return address */
88 bool chained; /* true, if instance is nested */
89
90 struct return_instance *next; /* keep as stack */
91};
92
Oleg Nesterov86dcb702015-07-21 15:40:26 +020093enum rp_check {
94 RP_CHECK_CALL,
Oleg Nesterovdb087ef2015-07-21 15:40:28 +020095 RP_CHECK_CHAIN_CALL,
Oleg Nesterov86dcb702015-07-21 15:40:26 +020096 RP_CHECK_RET,
97};
98
Oleg Nesterovc912dae2013-11-09 19:49:39 +010099struct xol_area;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +0530100
101struct uprobes_state {
102 struct xol_area *xol_area;
103};
Oleg Nesterov647c42d2012-08-06 13:15:09 +0200104
Nadav Amitaad42dd2019-04-26 16:22:44 -0700105extern void __init uprobes_init(void);
Bjorn Helgaas271a9c32014-10-13 19:01:03 -0600106extern int set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
107extern int set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
108extern bool is_swbp_insn(uprobe_opcode_t *insn);
109extern bool is_trap_insn(uprobe_opcode_t *insn);
110extern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200111extern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
Ravi Bangoria6d437432018-08-09 09:48:52 +0530112extern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530113extern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530114extern int uprobe_register_refctr(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100115extern int uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530116extern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100117extern int uprobe_mmap(struct vm_area_struct *vma);
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530118extern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100119extern void uprobe_start_dup_mmap(void);
120extern void uprobe_end_dup_mmap(void);
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200121extern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530122extern void uprobe_free_utask(struct task_struct *t);
Oleg Nesterov3ab67962013-10-16 19:39:37 +0200123extern void uprobe_copy_process(struct task_struct *t, unsigned long flags);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530124extern int uprobe_post_sstep_notifier(struct pt_regs *regs);
125extern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
126extern void uprobe_notify_resume(struct pt_regs *regs);
127extern bool uprobe_deny_signal(void);
Ralf Baechlec2d3f252013-10-09 14:08:09 +0200128extern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +0530129extern void uprobe_clear_state(struct mm_struct *mm);
David A. Long3820b4d2013-10-15 17:04:16 -0400130extern int arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
131extern int arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
132extern int arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
133extern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
134extern int arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
135extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
136extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
Oleg Nesterov86dcb702015-07-21 15:40:26 +0200137extern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, struct pt_regs *regs);
Bjorn Helgaas271a9c32014-10-13 19:01:03 -0600138extern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
139extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
Victor Kamensky72e6ae22014-04-29 04:20:52 +0100140 void *src, unsigned long len);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530141#else /* !CONFIG_UPROBES */
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +0530142struct uprobes_state {
143};
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200144
Nadav Amitaad42dd2019-04-26 16:22:44 -0700145static inline void uprobes_init(void)
146{
147}
148
Oleg Nesterovb02ef202014-05-12 18:24:45 +0200149#define uprobe_get_trap_addr(regs) instruction_pointer(regs)
150
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100151static inline int
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530152uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530153{
154 return -ENOSYS;
155}
Ravi Bangoria1cc33162018-08-20 10:12:47 +0530156static inline int uprobe_register_refctr(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
157{
158 return -ENOSYS;
159}
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100160static inline int
161uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool add)
162{
163 return -ENOSYS;
164}
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100165static inline void
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530166uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530167{
168}
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100169static inline int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530170{
171 return 0;
172}
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +0530173static inline void
174uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530175{
176}
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100177static inline void uprobe_start_dup_mmap(void)
178{
179}
180static inline void uprobe_end_dup_mmap(void)
181{
182}
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200183static inline void
184uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
185{
186}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530187static inline void uprobe_notify_resume(struct pt_regs *regs)
188{
189}
190static inline bool uprobe_deny_signal(void)
191{
192 return false;
193}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530194static inline void uprobe_free_utask(struct task_struct *t)
195{
196}
Oleg Nesterov3ab67962013-10-16 19:39:37 +0200197static inline void uprobe_copy_process(struct task_struct *t, unsigned long flags)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530198{
199}
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +0530200static inline void uprobe_clear_state(struct mm_struct *mm)
201{
202}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530203#endif /* !CONFIG_UPROBES */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530204#endif /* _LINUX_UPROBES_H */