Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 2 | #ifndef _ASM_X86_KPROBES_H |
| 3 | #define _ASM_X86_KPROBES_H |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 4 | /* |
| 5 | * Kernel Probes (KProbes) |
| 6 | * |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 7 | * Copyright (C) IBM Corporation, 2002, 2004 |
| 8 | * |
| 9 | * See arch/x86/kernel/kprobes.c for x86 kprobes history. |
| 10 | */ |
Luis R. Rodriguez | 7d134b2 | 2017-02-27 14:26:56 -0800 | [diff] [blame] | 11 | |
| 12 | #include <asm-generic/kprobes.h> |
| 13 | |
Luis R. Rodriguez | 7d134b2 | 2017-02-27 14:26:56 -0800 | [diff] [blame] | 14 | #ifdef CONFIG_KPROBES |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <linux/percpu.h> |
Peter Zijlstra | ab09e95 | 2019-10-09 13:57:17 +0200 | [diff] [blame] | 18 | #include <asm/text-patching.h> |
Peter Zijlstra | 30a813a | 2010-03-04 13:49:21 +0100 | [diff] [blame] | 19 | #include <asm/insn.h> |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 20 | |
| 21 | #define __ARCH_WANT_KPROBES_INSN_SLOT |
| 22 | |
| 23 | struct pt_regs; |
| 24 | struct kprobe; |
| 25 | |
| 26 | typedef u8 kprobe_opcode_t; |
Peter Zijlstra | ab09e95 | 2019-10-09 13:57:17 +0200 | [diff] [blame] | 27 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 28 | #define MAX_STACK_SIZE 64 |
Linus Torvalds | aca9c29 | 2016-06-24 16:55:53 -0700 | [diff] [blame] | 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)) |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 34 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 35 | #define flush_insn_slot(p) do { } while (0) |
| 36 | |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 37 | /* optinsn template addresses */ |
Linus Torvalds | 54a7d50 | 2017-07-20 11:34:47 -0700 | [diff] [blame] | 38 | extern __visible kprobe_opcode_t optprobe_template_entry[]; |
Peter Zijlstra | d8a7386 | 2020-03-05 10:21:30 +0100 | [diff] [blame] | 39 | extern __visible kprobe_opcode_t optprobe_template_clac[]; |
Linus Torvalds | 54a7d50 | 2017-07-20 11:34:47 -0700 | [diff] [blame] | 40 | extern __visible kprobe_opcode_t optprobe_template_val[]; |
| 41 | extern __visible kprobe_opcode_t optprobe_template_call[]; |
| 42 | extern __visible kprobe_opcode_t optprobe_template_end[]; |
Peter Zijlstra | ab09e95 | 2019-10-09 13:57:17 +0200 | [diff] [blame] | 43 | #define MAX_OPTIMIZED_LENGTH (MAX_INSN_SIZE + DISP32_SIZE) |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 44 | #define MAX_OPTINSN_SIZE \ |
Masami Hiramatsu | a8976fc | 2017-08-18 17:25:08 +0900 | [diff] [blame] | 45 | (((unsigned long)optprobe_template_end - \ |
| 46 | (unsigned long)optprobe_template_entry) + \ |
Peter Zijlstra | ab09e95 | 2019-10-09 13:57:17 +0200 | [diff] [blame] | 47 | MAX_OPTIMIZED_LENGTH + JMP32_INSN_SIZE) |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 48 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 49 | extern const int kretprobe_blacklist_size; |
| 50 | |
| 51 | void arch_remove_kprobe(struct kprobe *p); |
Andi Kleen | 04bb591 | 2013-08-05 15:02:41 -0700 | [diff] [blame] | 52 | asmlinkage void kretprobe_trampoline(void); |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 53 | |
Masami Hiramatsu | b4da334 | 2018-01-13 02:54:04 +0900 | [diff] [blame] | 54 | extern void arch_kprobe_override_function(struct pt_regs *regs); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 55 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 56 | /* Architecture specific copy of original instruction*/ |
| 57 | struct arch_specific_insn { |
| 58 | /* copy of the original instruction */ |
| 59 | kprobe_opcode_t *insn; |
| 60 | /* |
Masami Hiramatsu | abd82e5 | 2020-12-18 23:12:05 +0900 | [diff] [blame] | 61 | * boostable = 0: This instruction type is not boostable. |
| 62 | * boostable = 1: This instruction has been boosted: we have |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 63 | * added a relative jump after the instruction copy in insn, |
| 64 | * so no single-step and fixup are needed (unless there's |
Masami Hiramatsu | e704e34 | 2018-06-20 01:10:55 +0900 | [diff] [blame] | 65 | * a post_handler). |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 66 | */ |
Masami Hiramatsu | abd82e5 | 2020-12-18 23:12:05 +0900 | [diff] [blame] | 67 | unsigned boostable:1; |
Masami Hiramatsu | 6256e66 | 2021-03-03 00:25:46 +0900 | [diff] [blame] | 68 | unsigned char size; /* The size of insn */ |
| 69 | union { |
| 70 | unsigned char opcode; |
| 71 | struct { |
| 72 | unsigned char type; |
| 73 | } jcc; |
| 74 | struct { |
| 75 | unsigned char type; |
| 76 | unsigned char asize; |
| 77 | } loop; |
| 78 | struct { |
| 79 | unsigned char reg; |
| 80 | } indirect; |
| 81 | }; |
| 82 | s32 rel32; /* relative offset must be s32, s16, or s8 */ |
| 83 | void (*emulate_op)(struct kprobe *p, struct pt_regs *regs); |
Adrian Hunter | 3e46bb4 | 2020-05-12 15:19:12 +0300 | [diff] [blame] | 84 | /* Number of bytes of text poked */ |
| 85 | int tp_len; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 88 | struct arch_optimized_insn { |
| 89 | /* copy of the original instructions */ |
Peter Zijlstra | ab09e95 | 2019-10-09 13:57:17 +0200 | [diff] [blame] | 90 | kprobe_opcode_t copied_insn[DISP32_SIZE]; |
Masami Hiramatsu | c0f7ac3 | 2010-02-25 08:34:46 -0500 | [diff] [blame] | 91 | /* detour code buffer */ |
| 92 | kprobe_opcode_t *insn; |
| 93 | /* the size of instructions copied to detour code buffer */ |
| 94 | size_t size; |
| 95 | }; |
| 96 | |
| 97 | /* Return true (!0) if optinsn is prepared for optimization. */ |
| 98 | static inline int arch_prepared_optinsn(struct arch_optimized_insn *optinsn) |
| 99 | { |
| 100 | return optinsn->size; |
| 101 | } |
| 102 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 103 | struct prev_kprobe { |
| 104 | struct kprobe *kp; |
| 105 | unsigned long status; |
| 106 | unsigned long old_flags; |
| 107 | unsigned long saved_flags; |
| 108 | }; |
| 109 | |
| 110 | /* per-cpu kprobe control block */ |
| 111 | struct kprobe_ctlblk { |
| 112 | unsigned long kprobe_status; |
| 113 | unsigned long kprobe_old_flags; |
| 114 | unsigned long kprobe_saved_flags; |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 115 | struct prev_kprobe prev_kprobe; |
| 116 | }; |
| 117 | |
Masami Hiramatsu | d6be29b | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 118 | extern int kprobe_fault_handler(struct pt_regs *regs, int trapnr); |
| 119 | extern int kprobe_exceptions_notify(struct notifier_block *self, |
| 120 | unsigned long val, void *data); |
Masami Hiramatsu | 6f6343f | 2014-04-17 17:17:33 +0900 | [diff] [blame] | 121 | extern int kprobe_int3_handler(struct pt_regs *regs); |
Luis R. Rodriguez | 7d134b2 | 2017-02-27 14:26:56 -0800 | [diff] [blame] | 122 | |
Peter Zijlstra | 20a6e35 | 2020-09-02 15:25:53 +0200 | [diff] [blame] | 123 | #else |
| 124 | |
| 125 | static inline int kprobe_debug_handler(struct pt_regs *regs) { return 0; } |
| 126 | |
Luis R. Rodriguez | 7d134b2 | 2017-02-27 14:26:56 -0800 | [diff] [blame] | 127 | #endif /* CONFIG_KPROBES */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 128 | #endif /* _ASM_X86_KPROBES_H */ |