blob: 991a7ad540c72e1dc01f1b35d727b1ced21f3798 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002#ifndef _ASM_X86_KPROBES_H
3#define _ASM_X86_KPROBES_H
4/*
5 * Kernel Probes (KProbes)
6 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 * 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#ifdef CONFIG_KPROBES
15#include <linux/types.h>
16#include <linux/ptrace.h>
17#include <linux/percpu.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020018#include <asm/text-patching.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include <asm/insn.h>
20
21#define __ARCH_WANT_KPROBES_INSN_SLOT
22
23struct pt_regs;
24struct kprobe;
25
26typedef u8 kprobe_opcode_t;
Olivier Deprez157378f2022-04-04 15:47:50 +020027
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028#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 */
38extern __visible kprobe_opcode_t optprobe_template_entry[];
Olivier Deprez157378f2022-04-04 15:47:50 +020039extern __visible kprobe_opcode_t optprobe_template_clac[];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040extern __visible kprobe_opcode_t optprobe_template_val[];
41extern __visible kprobe_opcode_t optprobe_template_call[];
42extern __visible kprobe_opcode_t optprobe_template_end[];
Olivier Deprez157378f2022-04-04 15:47:50 +020043#define MAX_OPTIMIZED_LENGTH (MAX_INSN_SIZE + DISP32_SIZE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044#define MAX_OPTINSN_SIZE \
45 (((unsigned long)optprobe_template_end - \
46 (unsigned long)optprobe_template_entry) + \
Olivier Deprez157378f2022-04-04 15:47:50 +020047 MAX_OPTIMIZED_LENGTH + JMP32_INSN_SIZE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
49extern const int kretprobe_blacklist_size;
50
51void arch_remove_kprobe(struct kprobe *p);
52asmlinkage void kretprobe_trampoline(void);
53
54extern void arch_kprobe_override_function(struct pt_regs *regs);
55
56/* Architecture specific copy of original instruction*/
57struct arch_specific_insn {
58 /* copy of the original instruction */
59 kprobe_opcode_t *insn;
60 /*
61 * boostable = false: This instruction type is not boostable.
62 * boostable = true: This instruction has been boosted: we have
63 * added a relative jump after the instruction copy in insn,
64 * so no single-step and fixup are needed (unless there's
65 * a post_handler).
66 */
67 bool boostable;
68 bool if_modifier;
Olivier Deprez157378f2022-04-04 15:47:50 +020069 /* Number of bytes of text poked */
70 int tp_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071};
72
73struct arch_optimized_insn {
74 /* copy of the original instructions */
Olivier Deprez157378f2022-04-04 15:47:50 +020075 kprobe_opcode_t copied_insn[DISP32_SIZE];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 /* detour code buffer */
77 kprobe_opcode_t *insn;
78 /* the size of instructions copied to detour code buffer */
79 size_t size;
80};
81
82/* Return true (!0) if optinsn is prepared for optimization. */
83static inline int arch_prepared_optinsn(struct arch_optimized_insn *optinsn)
84{
85 return optinsn->size;
86}
87
88struct prev_kprobe {
89 struct kprobe *kp;
90 unsigned long status;
91 unsigned long old_flags;
92 unsigned long saved_flags;
93};
94
95/* per-cpu kprobe control block */
96struct kprobe_ctlblk {
97 unsigned long kprobe_status;
98 unsigned long kprobe_old_flags;
99 unsigned long kprobe_saved_flags;
100 struct prev_kprobe prev_kprobe;
101};
102
103extern int kprobe_fault_handler(struct pt_regs *regs, int trapnr);
104extern int kprobe_exceptions_notify(struct notifier_block *self,
105 unsigned long val, void *data);
106extern int kprobe_int3_handler(struct pt_regs *regs);
107extern int kprobe_debug_handler(struct pt_regs *regs);
108
Olivier Deprez157378f2022-04-04 15:47:50 +0200109#else
110
111static inline int kprobe_debug_handler(struct pt_regs *regs) { return 0; }
112
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113#endif /* CONFIG_KPROBES */
114#endif /* _ASM_X86_KPROBES_H */