xref: /linux/arch/x86/include/asm/kprobes.h (revision f86fd32d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _ASM_X86_KPROBES_H
3 #define _ASM_X86_KPROBES_H
4 /*
5  *  Kernel Probes (KProbes)
6  *
7  * Copyright (C) IBM Corporation, 2002, 2004
8  *
9  * See arch/x86/kernel/kprobes.c for x86 kprobes history.
10  */
11 
12 #include <asm-generic/kprobes.h>
13 
14 #ifdef CONFIG_KPROBES
15 #include <linux/types.h>
16 #include <linux/ptrace.h>
17 #include <linux/percpu.h>
18 #include <asm/text-patching.h>
19 #include <asm/insn.h>
20 
21 #define  __ARCH_WANT_KPROBES_INSN_SLOT
22 
23 struct pt_regs;
24 struct kprobe;
25 
26 typedef u8 kprobe_opcode_t;
27 
28 #define MAX_STACK_SIZE 64
29 #define CUR_STACK_SIZE(ADDR) \
30 	(current_top_of_stack() - (unsigned long)(ADDR))
31 #define MIN_STACK_SIZE(ADDR)				\
32 	(MAX_STACK_SIZE < CUR_STACK_SIZE(ADDR) ?	\
33 	 MAX_STACK_SIZE : CUR_STACK_SIZE(ADDR))
34 
35 #define flush_insn_slot(p)	do { } while (0)
36 
37 /* optinsn template addresses */
38 extern __visible kprobe_opcode_t optprobe_template_entry[];
39 extern __visible kprobe_opcode_t optprobe_template_val[];
40 extern __visible kprobe_opcode_t optprobe_template_call[];
41 extern __visible kprobe_opcode_t optprobe_template_end[];
42 #define MAX_OPTIMIZED_LENGTH (MAX_INSN_SIZE + DISP32_SIZE)
43 #define MAX_OPTINSN_SIZE 				\
44 	(((unsigned long)optprobe_template_end -	\
45 	  (unsigned long)optprobe_template_entry) +	\
46 	 MAX_OPTIMIZED_LENGTH + JMP32_INSN_SIZE)
47 
48 extern const int kretprobe_blacklist_size;
49 
50 void arch_remove_kprobe(struct kprobe *p);
51 asmlinkage void kretprobe_trampoline(void);
52 
53 extern void arch_kprobe_override_function(struct pt_regs *regs);
54 
55 /* Architecture specific copy of original instruction*/
56 struct arch_specific_insn {
57 	/* copy of the original instruction */
58 	kprobe_opcode_t *insn;
59 	/*
60 	 * boostable = false: This instruction type is not boostable.
61 	 * boostable = true: This instruction has been boosted: we have
62 	 * added a relative jump after the instruction copy in insn,
63 	 * so no single-step and fixup are needed (unless there's
64 	 * a post_handler).
65 	 */
66 	bool boostable;
67 	bool if_modifier;
68 };
69 
70 struct arch_optimized_insn {
71 	/* copy of the original instructions */
72 	kprobe_opcode_t copied_insn[DISP32_SIZE];
73 	/* detour code buffer */
74 	kprobe_opcode_t *insn;
75 	/* the size of instructions copied to detour code buffer */
76 	size_t size;
77 };
78 
79 /* Return true (!0) if optinsn is prepared for optimization. */
80 static inline int arch_prepared_optinsn(struct arch_optimized_insn *optinsn)
81 {
82 	return optinsn->size;
83 }
84 
85 struct prev_kprobe {
86 	struct kprobe *kp;
87 	unsigned long status;
88 	unsigned long old_flags;
89 	unsigned long saved_flags;
90 };
91 
92 /* per-cpu kprobe control block */
93 struct kprobe_ctlblk {
94 	unsigned long kprobe_status;
95 	unsigned long kprobe_old_flags;
96 	unsigned long kprobe_saved_flags;
97 	struct prev_kprobe prev_kprobe;
98 };
99 
100 extern int kprobe_fault_handler(struct pt_regs *regs, int trapnr);
101 extern int kprobe_exceptions_notify(struct notifier_block *self,
102 				    unsigned long val, void *data);
103 extern int kprobe_int3_handler(struct pt_regs *regs);
104 extern int kprobe_debug_handler(struct pt_regs *regs);
105 
106 #endif /* CONFIG_KPROBES */
107 #endif /* _ASM_X86_KPROBES_H */
108