text-patching.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_TEXT_PATCHING_H
  3. #define _ASM_X86_TEXT_PATCHING_H
  4. #include <linux/types.h>
  5. #include <linux/stddef.h>
  6. #include <asm/ptrace.h>
  7. struct paravirt_patch_site;
  8. #ifdef CONFIG_PARAVIRT
  9. void apply_paravirt(struct paravirt_patch_site *start,
  10. struct paravirt_patch_site *end);
  11. #else
  12. static inline void apply_paravirt(struct paravirt_patch_site *start,
  13. struct paravirt_patch_site *end)
  14. {}
  15. #define __parainstructions NULL
  16. #define __parainstructions_end NULL
  17. #endif
  18. /*
  19. * Currently, the max observed size in the kernel code is
  20. * JUMP_LABEL_NOP_SIZE/RELATIVEJUMP_SIZE, which are 5.
  21. * Raise it if needed.
  22. */
  23. #define POKE_MAX_OPCODE_SIZE 5
  24. extern void text_poke_early(void *addr, const void *opcode, size_t len);
  25. /*
  26. * Clear and restore the kernel write-protection flag on the local CPU.
  27. * Allows the kernel to edit read-only pages.
  28. * Side-effect: any interrupt handler running between save and restore will have
  29. * the ability to write to read-only pages.
  30. *
  31. * Warning:
  32. * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
  33. * no thread can be preempted in the instructions being modified (no iret to an
  34. * invalid instruction possible) or if the instructions are changed from a
  35. * consistent state to another consistent state atomically.
  36. * On the local CPU you need to be protected against NMI or MCE handlers seeing
  37. * an inconsistent instruction while you patch.
  38. */
  39. extern void *text_poke(void *addr, const void *opcode, size_t len);
  40. extern void text_poke_sync(void);
  41. extern void *text_poke_kgdb(void *addr, const void *opcode, size_t len);
  42. extern void *text_poke_copy(void *addr, const void *opcode, size_t len);
  43. extern void *text_poke_set(void *addr, int c, size_t len);
  44. extern int poke_int3_handler(struct pt_regs *regs);
  45. extern void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate);
  46. extern void text_poke_queue(void *addr, const void *opcode, size_t len, const void *emulate);
  47. extern void text_poke_finish(void);
  48. #define INT3_INSN_SIZE 1
  49. #define INT3_INSN_OPCODE 0xCC
  50. #define RET_INSN_SIZE 1
  51. #define RET_INSN_OPCODE 0xC3
  52. #define CALL_INSN_SIZE 5
  53. #define CALL_INSN_OPCODE 0xE8
  54. #define JMP32_INSN_SIZE 5
  55. #define JMP32_INSN_OPCODE 0xE9
  56. #define JMP8_INSN_SIZE 2
  57. #define JMP8_INSN_OPCODE 0xEB
  58. #define DISP32_SIZE 4
  59. static __always_inline int text_opcode_size(u8 opcode)
  60. {
  61. int size = 0;
  62. #define __CASE(insn) \
  63. case insn##_INSN_OPCODE: size = insn##_INSN_SIZE; break
  64. switch(opcode) {
  65. __CASE(INT3);
  66. __CASE(RET);
  67. __CASE(CALL);
  68. __CASE(JMP32);
  69. __CASE(JMP8);
  70. }
  71. #undef __CASE
  72. return size;
  73. }
  74. union text_poke_insn {
  75. u8 text[POKE_MAX_OPCODE_SIZE];
  76. struct {
  77. u8 opcode;
  78. s32 disp;
  79. } __attribute__((packed));
  80. };
  81. static __always_inline
  82. void __text_gen_insn(void *buf, u8 opcode, const void *addr, const void *dest, int size)
  83. {
  84. union text_poke_insn *insn = buf;
  85. BUG_ON(size < text_opcode_size(opcode));
  86. /*
  87. * Hide the addresses to avoid the compiler folding in constants when
  88. * referencing code, these can mess up annotations like
  89. * ANNOTATE_NOENDBR.
  90. */
  91. OPTIMIZER_HIDE_VAR(insn);
  92. OPTIMIZER_HIDE_VAR(addr);
  93. OPTIMIZER_HIDE_VAR(dest);
  94. insn->opcode = opcode;
  95. if (size > 1) {
  96. insn->disp = (long)dest - (long)(addr + size);
  97. if (size == 2) {
  98. /*
  99. * Ensure that for JMP8 the displacement
  100. * actually fits the signed byte.
  101. */
  102. BUG_ON((insn->disp >> 31) != (insn->disp >> 7));
  103. }
  104. }
  105. }
  106. static __always_inline
  107. void *text_gen_insn(u8 opcode, const void *addr, const void *dest)
  108. {
  109. static union text_poke_insn insn; /* per instance */
  110. __text_gen_insn(&insn, opcode, addr, dest, text_opcode_size(opcode));
  111. return &insn.text;
  112. }
  113. extern int after_bootmem;
  114. extern __ro_after_init struct mm_struct *poking_mm;
  115. extern __ro_after_init unsigned long poking_addr;
  116. #ifndef CONFIG_UML_X86
  117. static __always_inline
  118. void int3_emulate_jmp(struct pt_regs *regs, unsigned long ip)
  119. {
  120. regs->ip = ip;
  121. }
  122. static __always_inline
  123. void int3_emulate_push(struct pt_regs *regs, unsigned long val)
  124. {
  125. /*
  126. * The int3 handler in entry_64.S adds a gap between the
  127. * stack where the break point happened, and the saving of
  128. * pt_regs. We can extend the original stack because of
  129. * this gap. See the idtentry macro's create_gap option.
  130. *
  131. * Similarly entry_32.S will have a gap on the stack for (any) hardware
  132. * exception and pt_regs; see FIXUP_FRAME.
  133. */
  134. regs->sp -= sizeof(unsigned long);
  135. *(unsigned long *)regs->sp = val;
  136. }
  137. static __always_inline
  138. unsigned long int3_emulate_pop(struct pt_regs *regs)
  139. {
  140. unsigned long val = *(unsigned long *)regs->sp;
  141. regs->sp += sizeof(unsigned long);
  142. return val;
  143. }
  144. static __always_inline
  145. void int3_emulate_call(struct pt_regs *regs, unsigned long func)
  146. {
  147. int3_emulate_push(regs, regs->ip - INT3_INSN_SIZE + CALL_INSN_SIZE);
  148. int3_emulate_jmp(regs, func);
  149. }
  150. static __always_inline
  151. void int3_emulate_ret(struct pt_regs *regs)
  152. {
  153. unsigned long ip = int3_emulate_pop(regs);
  154. int3_emulate_jmp(regs, ip);
  155. }
  156. static __always_inline
  157. void int3_emulate_jcc(struct pt_regs *regs, u8 cc, unsigned long ip, unsigned long disp)
  158. {
  159. static const unsigned long jcc_mask[6] = {
  160. [0] = X86_EFLAGS_OF,
  161. [1] = X86_EFLAGS_CF,
  162. [2] = X86_EFLAGS_ZF,
  163. [3] = X86_EFLAGS_CF | X86_EFLAGS_ZF,
  164. [4] = X86_EFLAGS_SF,
  165. [5] = X86_EFLAGS_PF,
  166. };
  167. bool invert = cc & 1;
  168. bool match;
  169. if (cc < 0xc) {
  170. match = regs->flags & jcc_mask[cc >> 1];
  171. } else {
  172. match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^
  173. ((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT);
  174. if (cc >= 0xe)
  175. match = match || (regs->flags & X86_EFLAGS_ZF);
  176. }
  177. if ((match && !invert) || (!match && invert))
  178. ip += disp;
  179. int3_emulate_jmp(regs, ip);
  180. }
  181. #endif /* !CONFIG_UML_X86 */
  182. #endif /* _ASM_X86_TEXT_PATCHING_H */