1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_TEXT_PATCHING_H
3 #define _ASM_X86_TEXT_PATCHING_H
5 #include <linux/types.h>
6 #include <linux/stddef.h>
7 #include <asm/ptrace.h>
9 struct paravirt_patch_site;
10 #ifdef CONFIG_PARAVIRT
11 void apply_paravirt(struct paravirt_patch_site *start,
12 struct paravirt_patch_site *end);
14 static inline void apply_paravirt(struct paravirt_patch_site *start,
15 struct paravirt_patch_site *end)
17 #define __parainstructions NULL
18 #define __parainstructions_end NULL
22 * Currently, the max observed size in the kernel code is
23 * JUMP_LABEL_NOP_SIZE/RELATIVEJUMP_SIZE, which are 5.
26 #define POKE_MAX_OPCODE_SIZE 5
28 extern void text_poke_early(void *addr, const void *opcode, size_t len);
31 * Clear and restore the kernel write-protection flag on the local CPU.
32 * Allows the kernel to edit read-only pages.
33 * Side-effect: any interrupt handler running between save and restore will have
34 * the ability to write to read-only pages.
37 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
38 * no thread can be preempted in the instructions being modified (no iret to an
39 * invalid instruction possible) or if the instructions are changed from a
40 * consistent state to another consistent state atomically.
41 * On the local CPU you need to be protected against NMI or MCE handlers seeing
42 * an inconsistent instruction while you patch.
44 extern void *text_poke(void *addr, const void *opcode, size_t len);
45 extern void text_poke_sync(void);
46 extern void *text_poke_kgdb(void *addr, const void *opcode, size_t len);
47 extern void *text_poke_copy(void *addr, const void *opcode, size_t len);
48 extern int poke_int3_handler(struct pt_regs *regs);
49 extern void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate);
51 extern void text_poke_queue(void *addr, const void *opcode, size_t len, const void *emulate);
52 extern void text_poke_finish(void);
54 #define INT3_INSN_SIZE 1
55 #define INT3_INSN_OPCODE 0xCC
57 #define RET_INSN_SIZE 1
58 #define RET_INSN_OPCODE 0xC3
60 #define CALL_INSN_SIZE 5
61 #define CALL_INSN_OPCODE 0xE8
63 #define JMP32_INSN_SIZE 5
64 #define JMP32_INSN_OPCODE 0xE9
66 #define JMP8_INSN_SIZE 2
67 #define JMP8_INSN_OPCODE 0xEB
71 static __always_inline int text_opcode_size(u8 opcode)
75 #define __CASE(insn) \
76 case insn##_INSN_OPCODE: size = insn##_INSN_SIZE; break
91 union text_poke_insn {
92 u8 text[POKE_MAX_OPCODE_SIZE];
96 } __attribute__((packed));
99 static __always_inline
100 void *text_gen_insn(u8 opcode, const void *addr, const void *dest)
102 static union text_poke_insn insn; /* per instance */
103 int size = text_opcode_size(opcode);
105 insn.opcode = opcode;
108 insn.disp = (long)dest - (long)(addr + size);
111 * Ensure that for JMP9 the displacement
112 * actually fits the signed byte.
114 BUG_ON((insn.disp >> 31) != (insn.disp >> 7));
121 extern int after_bootmem;
122 extern __ro_after_init struct mm_struct *poking_mm;
123 extern __ro_after_init unsigned long poking_addr;
125 #ifndef CONFIG_UML_X86
126 static __always_inline
127 void int3_emulate_jmp(struct pt_regs *regs, unsigned long ip)
132 static __always_inline
133 void int3_emulate_push(struct pt_regs *regs, unsigned long val)
136 * The int3 handler in entry_64.S adds a gap between the
137 * stack where the break point happened, and the saving of
138 * pt_regs. We can extend the original stack because of
139 * this gap. See the idtentry macro's create_gap option.
141 * Similarly entry_32.S will have a gap on the stack for (any) hardware
142 * exception and pt_regs; see FIXUP_FRAME.
144 regs->sp -= sizeof(unsigned long);
145 *(unsigned long *)regs->sp = val;
148 static __always_inline
149 unsigned long int3_emulate_pop(struct pt_regs *regs)
151 unsigned long val = *(unsigned long *)regs->sp;
152 regs->sp += sizeof(unsigned long);
156 static __always_inline
157 void int3_emulate_call(struct pt_regs *regs, unsigned long func)
159 int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE);
160 int3_emulate_jmp(regs, func);
163 static __always_inline
164 void int3_emulate_ret(struct pt_regs *regs)
166 unsigned long ip = int3_emulate_pop(regs);
167 int3_emulate_jmp(regs, ip);
169 #endif /* !CONFIG_UML_X86 */
171 #endif /* _ASM_X86_TEXT_PATCHING_H */