alternative.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef _ASM_X86_ALTERNATIVE_H
  2. #define _ASM_X86_ALTERNATIVE_H
  3. #include <linux/types.h>
  4. #include <linux/stddef.h>
  5. #include <linux/stringify.h>
  6. #include <asm/asm.h>
  7. #include <asm/ptrace.h>
  8. /*
  9. * Alternative inline assembly for SMP.
  10. *
  11. * The LOCK_PREFIX macro defined here replaces the LOCK and
  12. * LOCK_PREFIX macros used everywhere in the source tree.
  13. *
  14. * SMP alternatives use the same data structures as the other
  15. * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  16. * UP system running a SMP kernel. The existing apply_alternatives()
  17. * works fine for patching a SMP kernel for UP.
  18. *
  19. * The SMP alternative tables can be kept after boot and contain both
  20. * UP and SMP versions of the instructions to allow switching back to
  21. * SMP at runtime, when hotplugging in a new CPU, which is especially
  22. * useful in virtualized environments.
  23. *
  24. * The very common lock prefix is handled as special case in a
  25. * separate table which is a pure address list without replacement ptr
  26. * and size information. That keeps the table sizes small.
  27. */
  28. #ifdef CONFIG_SMP
  29. #define LOCK_PREFIX_HERE \
  30. ".pushsection .smp_locks,\"a\"\n" \
  31. ".balign 4\n" \
  32. ".long 671f - .\n" /* offset */ \
  33. ".popsection\n" \
  34. "671:"
  35. #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
  36. #else /* ! CONFIG_SMP */
  37. #define LOCK_PREFIX_HERE ""
  38. #define LOCK_PREFIX ""
  39. #endif
  40. struct alt_instr {
  41. s32 instr_offset; /* original instruction */
  42. s32 repl_offset; /* offset to replacement instruction */
  43. u16 cpuid; /* cpuid bit set for replacement */
  44. u8 instrlen; /* length of original instruction */
  45. u8 replacementlen; /* length of new instruction, <= instrlen */
  46. };
  47. extern void alternative_instructions(void);
  48. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  49. struct module;
  50. #ifdef CONFIG_SMP
  51. extern void alternatives_smp_module_add(struct module *mod, char *name,
  52. void *locks, void *locks_end,
  53. void *text, void *text_end);
  54. extern void alternatives_smp_module_del(struct module *mod);
  55. extern void alternatives_enable_smp(void);
  56. extern int alternatives_text_reserved(void *start, void *end);
  57. extern bool skip_smp_alternatives;
  58. #else
  59. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  60. void *locks, void *locks_end,
  61. void *text, void *text_end) {}
  62. static inline void alternatives_smp_module_del(struct module *mod) {}
  63. static inline void alternatives_enable_smp(void) {}
  64. static inline int alternatives_text_reserved(void *start, void *end)
  65. {
  66. return 0;
  67. }
  68. #endif /* CONFIG_SMP */
  69. #define OLDINSTR(oldinstr) "661:\n\t" oldinstr "\n662:\n"
  70. #define b_replacement(number) "663"#number
  71. #define e_replacement(number) "664"#number
  72. #define alt_slen "662b-661b"
  73. #define alt_rlen(number) e_replacement(number)"f-"b_replacement(number)"f"
  74. #define ALTINSTR_ENTRY(feature, number) \
  75. " .long 661b - .\n" /* label */ \
  76. " .long " b_replacement(number)"f - .\n" /* new instruction */ \
  77. " .word " __stringify(feature) "\n" /* feature bit */ \
  78. " .byte " alt_slen "\n" /* source len */ \
  79. " .byte " alt_rlen(number) "\n" /* replacement len */
  80. #define DISCARD_ENTRY(number) /* rlen <= slen */ \
  81. " .byte 0xff + (" alt_rlen(number) ") - (" alt_slen ")\n"
  82. #define ALTINSTR_REPLACEMENT(newinstr, feature, number) /* replacement */ \
  83. b_replacement(number)":\n\t" newinstr "\n" e_replacement(number) ":\n\t"
  84. /* alternative assembly primitive: */
  85. #define ALTERNATIVE(oldinstr, newinstr, feature) \
  86. OLDINSTR(oldinstr) \
  87. ".pushsection .altinstructions,\"a\"\n" \
  88. ALTINSTR_ENTRY(feature, 1) \
  89. ".popsection\n" \
  90. ".pushsection .discard,\"aw\",@progbits\n" \
  91. DISCARD_ENTRY(1) \
  92. ".popsection\n" \
  93. ".pushsection .altinstr_replacement, \"ax\"\n" \
  94. ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
  95. ".popsection"
  96. #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
  97. OLDINSTR(oldinstr) \
  98. ".pushsection .altinstructions,\"a\"\n" \
  99. ALTINSTR_ENTRY(feature1, 1) \
  100. ALTINSTR_ENTRY(feature2, 2) \
  101. ".popsection\n" \
  102. ".pushsection .discard,\"aw\",@progbits\n" \
  103. DISCARD_ENTRY(1) \
  104. DISCARD_ENTRY(2) \
  105. ".popsection\n" \
  106. ".pushsection .altinstr_replacement, \"ax\"\n" \
  107. ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
  108. ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
  109. ".popsection"
  110. /*
  111. * This must be included *after* the definition of ALTERNATIVE due to
  112. * <asm/arch_hweight.h>
  113. */
  114. #include <asm/cpufeature.h>
  115. /*
  116. * Alternative instructions for different CPU types or capabilities.
  117. *
  118. * This allows to use optimized instructions even on generic binary
  119. * kernels.
  120. *
  121. * length of oldinstr must be longer or equal the length of newinstr
  122. * It can be padded with nops as needed.
  123. *
  124. * For non barrier like inlines please define new variants
  125. * without volatile and memory clobber.
  126. */
  127. #define alternative(oldinstr, newinstr, feature) \
  128. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
  129. /*
  130. * Alternative inline assembly with input.
  131. *
  132. * Pecularities:
  133. * No memory clobber here.
  134. * Argument numbers start with 1.
  135. * Best is to use constraints that are fixed size (like (%1) ... "r")
  136. * If you use variable sized constraints like "m" or "g" in the
  137. * replacement make sure to pad to the worst case length.
  138. * Leaving an unused argument 0 to keep API compatibility.
  139. */
  140. #define alternative_input(oldinstr, newinstr, feature, input...) \
  141. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  142. : : "i" (0), ## input)
  143. /* Like alternative_input, but with a single output argument */
  144. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  145. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  146. : output : "i" (0), ## input)
  147. /* Like alternative_io, but for replacing a direct call with another one. */
  148. #define alternative_call(oldfunc, newfunc, feature, output, input...) \
  149. asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
  150. : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
  151. /*
  152. * Like alternative_call, but there are two features and respective functions.
  153. * If CPU has feature2, function2 is used.
  154. * Otherwise, if CPU has feature1, function1 is used.
  155. * Otherwise, old function is used.
  156. */
  157. #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
  158. output, input...) \
  159. asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
  160. "call %P[new2]", feature2) \
  161. : output : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
  162. [new2] "i" (newfunc2), ## input)
  163. /*
  164. * use this macro(s) if you need more than one output parameter
  165. * in alternative_io
  166. */
  167. #define ASM_OUTPUT2(a...) a
  168. /*
  169. * use this macro if you need clobbers but no inputs in
  170. * alternative_{input,io,call}()
  171. */
  172. #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
  173. struct paravirt_patch_site;
  174. #ifdef CONFIG_PARAVIRT
  175. void apply_paravirt(struct paravirt_patch_site *start,
  176. struct paravirt_patch_site *end);
  177. #else
  178. static inline void apply_paravirt(struct paravirt_patch_site *start,
  179. struct paravirt_patch_site *end)
  180. {}
  181. #define __parainstructions NULL
  182. #define __parainstructions_end NULL
  183. #endif
  184. extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  185. /*
  186. * Clear and restore the kernel write-protection flag on the local CPU.
  187. * Allows the kernel to edit read-only pages.
  188. * Side-effect: any interrupt handler running between save and restore will have
  189. * the ability to write to read-only pages.
  190. *
  191. * Warning:
  192. * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
  193. * no thread can be preempted in the instructions being modified (no iret to an
  194. * invalid instruction possible) or if the instructions are changed from a
  195. * consistent state to another consistent state atomically.
  196. * On the local CPU you need to be protected again NMI or MCE handlers seeing an
  197. * inconsistent instruction while you patch.
  198. */
  199. extern void *text_poke(void *addr, const void *opcode, size_t len);
  200. extern int poke_int3_handler(struct pt_regs *regs);
  201. extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
  202. #endif /* _ASM_X86_ALTERNATIVE_H */