162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci#ifndef _LINUX_UPROBES_H
362306a36Sopenharmony_ci#define _LINUX_UPROBES_H
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci * User-space Probes (UProbes)
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) IBM Corporation, 2008-2012
862306a36Sopenharmony_ci * Authors:
962306a36Sopenharmony_ci *	Srikar Dronamraju
1062306a36Sopenharmony_ci *	Jim Keniston
1162306a36Sopenharmony_ci * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/errno.h>
1562306a36Sopenharmony_ci#include <linux/rbtree.h>
1662306a36Sopenharmony_ci#include <linux/types.h>
1762306a36Sopenharmony_ci#include <linux/wait.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct vm_area_struct;
2062306a36Sopenharmony_cistruct mm_struct;
2162306a36Sopenharmony_cistruct inode;
2262306a36Sopenharmony_cistruct notifier_block;
2362306a36Sopenharmony_cistruct page;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define UPROBE_HANDLER_REMOVE		1
2662306a36Sopenharmony_ci#define UPROBE_HANDLER_MASK		1
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define MAX_URETPROBE_DEPTH		64
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cienum uprobe_filter_ctx {
3162306a36Sopenharmony_ci	UPROBE_FILTER_REGISTER,
3262306a36Sopenharmony_ci	UPROBE_FILTER_UNREGISTER,
3362306a36Sopenharmony_ci	UPROBE_FILTER_MMAP,
3462306a36Sopenharmony_ci};
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistruct uprobe_consumer {
3762306a36Sopenharmony_ci	int (*handler)(struct uprobe_consumer *self, struct pt_regs *regs);
3862306a36Sopenharmony_ci	int (*ret_handler)(struct uprobe_consumer *self,
3962306a36Sopenharmony_ci				unsigned long func,
4062306a36Sopenharmony_ci				struct pt_regs *regs);
4162306a36Sopenharmony_ci	bool (*filter)(struct uprobe_consumer *self,
4262306a36Sopenharmony_ci				enum uprobe_filter_ctx ctx,
4362306a36Sopenharmony_ci				struct mm_struct *mm);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	struct uprobe_consumer *next;
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#ifdef CONFIG_UPROBES
4962306a36Sopenharmony_ci#include <asm/uprobes.h>
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cienum uprobe_task_state {
5262306a36Sopenharmony_ci	UTASK_RUNNING,
5362306a36Sopenharmony_ci	UTASK_SSTEP,
5462306a36Sopenharmony_ci	UTASK_SSTEP_ACK,
5562306a36Sopenharmony_ci	UTASK_SSTEP_TRAPPED,
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/*
5962306a36Sopenharmony_ci * uprobe_task: Metadata of a task while it singlesteps.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_cistruct uprobe_task {
6262306a36Sopenharmony_ci	enum uprobe_task_state		state;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	union {
6562306a36Sopenharmony_ci		struct {
6662306a36Sopenharmony_ci			struct arch_uprobe_task	autask;
6762306a36Sopenharmony_ci			unsigned long		vaddr;
6862306a36Sopenharmony_ci		};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci		struct {
7162306a36Sopenharmony_ci			struct callback_head	dup_xol_work;
7262306a36Sopenharmony_ci			unsigned long		dup_xol_addr;
7362306a36Sopenharmony_ci		};
7462306a36Sopenharmony_ci	};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	struct uprobe			*active_uprobe;
7762306a36Sopenharmony_ci	unsigned long			xol_vaddr;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	struct return_instance		*return_instances;
8062306a36Sopenharmony_ci	unsigned int			depth;
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistruct return_instance {
8462306a36Sopenharmony_ci	struct uprobe		*uprobe;
8562306a36Sopenharmony_ci	unsigned long		func;
8662306a36Sopenharmony_ci	unsigned long		stack;		/* stack pointer */
8762306a36Sopenharmony_ci	unsigned long		orig_ret_vaddr; /* original return address */
8862306a36Sopenharmony_ci	bool			chained;	/* true, if instance is nested */
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	struct return_instance	*next;		/* keep as stack */
9162306a36Sopenharmony_ci};
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cienum rp_check {
9462306a36Sopenharmony_ci	RP_CHECK_CALL,
9562306a36Sopenharmony_ci	RP_CHECK_CHAIN_CALL,
9662306a36Sopenharmony_ci	RP_CHECK_RET,
9762306a36Sopenharmony_ci};
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistruct xol_area;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistruct uprobes_state {
10262306a36Sopenharmony_ci	struct xol_area		*xol_area;
10362306a36Sopenharmony_ci};
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciextern void __init uprobes_init(void);
10662306a36Sopenharmony_ciextern int set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
10762306a36Sopenharmony_ciextern int set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
10862306a36Sopenharmony_ciextern bool is_swbp_insn(uprobe_opcode_t *insn);
10962306a36Sopenharmony_ciextern bool is_trap_insn(uprobe_opcode_t *insn);
11062306a36Sopenharmony_ciextern unsigned long uprobe_get_swbp_addr(struct pt_regs *regs);
11162306a36Sopenharmony_ciextern unsigned long uprobe_get_trap_addr(struct pt_regs *regs);
11262306a36Sopenharmony_ciextern int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t);
11362306a36Sopenharmony_ciextern int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
11462306a36Sopenharmony_ciextern int uprobe_register_refctr(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc);
11562306a36Sopenharmony_ciextern int uprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool);
11662306a36Sopenharmony_ciextern void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc);
11762306a36Sopenharmony_ciextern int uprobe_mmap(struct vm_area_struct *vma);
11862306a36Sopenharmony_ciextern void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end);
11962306a36Sopenharmony_ciextern void uprobe_start_dup_mmap(void);
12062306a36Sopenharmony_ciextern void uprobe_end_dup_mmap(void);
12162306a36Sopenharmony_ciextern void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm);
12262306a36Sopenharmony_ciextern void uprobe_free_utask(struct task_struct *t);
12362306a36Sopenharmony_ciextern void uprobe_copy_process(struct task_struct *t, unsigned long flags);
12462306a36Sopenharmony_ciextern int uprobe_post_sstep_notifier(struct pt_regs *regs);
12562306a36Sopenharmony_ciextern int uprobe_pre_sstep_notifier(struct pt_regs *regs);
12662306a36Sopenharmony_ciextern void uprobe_notify_resume(struct pt_regs *regs);
12762306a36Sopenharmony_ciextern bool uprobe_deny_signal(void);
12862306a36Sopenharmony_ciextern bool arch_uprobe_skip_sstep(struct arch_uprobe *aup, struct pt_regs *regs);
12962306a36Sopenharmony_ciextern void uprobe_clear_state(struct mm_struct *mm);
13062306a36Sopenharmony_ciextern int  arch_uprobe_analyze_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long addr);
13162306a36Sopenharmony_ciextern int  arch_uprobe_pre_xol(struct arch_uprobe *aup, struct pt_regs *regs);
13262306a36Sopenharmony_ciextern int  arch_uprobe_post_xol(struct arch_uprobe *aup, struct pt_regs *regs);
13362306a36Sopenharmony_ciextern bool arch_uprobe_xol_was_trapped(struct task_struct *tsk);
13462306a36Sopenharmony_ciextern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned long val, void *data);
13562306a36Sopenharmony_ciextern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
13662306a36Sopenharmony_ciextern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
13762306a36Sopenharmony_ciextern bool arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, struct pt_regs *regs);
13862306a36Sopenharmony_ciextern bool arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
13962306a36Sopenharmony_ciextern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
14062306a36Sopenharmony_ci					 void *src, unsigned long len);
14162306a36Sopenharmony_ci#else /* !CONFIG_UPROBES */
14262306a36Sopenharmony_cistruct uprobes_state {
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistatic inline void uprobes_init(void)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci#define uprobe_get_trap_addr(regs)	instruction_pointer(regs)
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_cistatic inline int
15262306a36Sopenharmony_ciuprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	return -ENOSYS;
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_cistatic inline int uprobe_register_refctr(struct inode *inode, loff_t offset, loff_t ref_ctr_offset, struct uprobe_consumer *uc)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	return -ENOSYS;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_cistatic inline int
16162306a36Sopenharmony_ciuprobe_apply(struct inode *inode, loff_t offset, struct uprobe_consumer *uc, bool add)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	return -ENOSYS;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_cistatic inline void
16662306a36Sopenharmony_ciuprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
16762306a36Sopenharmony_ci{
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_cistatic inline int uprobe_mmap(struct vm_area_struct *vma)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	return 0;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_cistatic inline void
17462306a36Sopenharmony_ciuprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci}
17762306a36Sopenharmony_cistatic inline void uprobe_start_dup_mmap(void)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_cistatic inline void uprobe_end_dup_mmap(void)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_cistatic inline void
18462306a36Sopenharmony_ciuprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
18562306a36Sopenharmony_ci{
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_cistatic inline void uprobe_notify_resume(struct pt_regs *regs)
18862306a36Sopenharmony_ci{
18962306a36Sopenharmony_ci}
19062306a36Sopenharmony_cistatic inline bool uprobe_deny_signal(void)
19162306a36Sopenharmony_ci{
19262306a36Sopenharmony_ci	return false;
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_cistatic inline void uprobe_free_utask(struct task_struct *t)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_cistatic inline void uprobe_copy_process(struct task_struct *t, unsigned long flags)
19862306a36Sopenharmony_ci{
19962306a36Sopenharmony_ci}
20062306a36Sopenharmony_cistatic inline void uprobe_clear_state(struct mm_struct *mm)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci#endif /* !CONFIG_UPROBES */
20462306a36Sopenharmony_ci#endif	/* _LINUX_UPROBES_H */
205