alternative.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <linux/jump_label.h>
  7. #include <asm/asm.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. ".section .smp_locks,\"a\"\n" \
  31. ".balign 4\n" \
  32. ".long 671f - .\n" /* offset */ \
  33. ".previous\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. u8 *instr; /* original instruction */
  42. u8 *replacement;
  43. u16 cpuid; /* cpuid bit set for replacement */
  44. u8 instrlen; /* length of original instruction */
  45. u8 replacementlen; /* length of new instruction, <= instrlen */
  46. #ifdef CONFIG_X86_64
  47. u32 pad2;
  48. #endif
  49. };
  50. extern void alternative_instructions(void);
  51. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  52. struct module;
  53. #ifdef CONFIG_SMP
  54. extern void alternatives_smp_module_add(struct module *mod, char *name,
  55. void *locks, void *locks_end,
  56. void *text, void *text_end);
  57. extern void alternatives_smp_module_del(struct module *mod);
  58. extern void alternatives_smp_switch(int smp);
  59. extern int alternatives_text_reserved(void *start, void *end);
  60. #else
  61. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  62. void *locks, void *locks_end,
  63. void *text, void *text_end) {}
  64. static inline void alternatives_smp_module_del(struct module *mod) {}
  65. static inline void alternatives_smp_switch(int smp) {}
  66. static inline int alternatives_text_reserved(void *start, void *end)
  67. {
  68. return 0;
  69. }
  70. #endif /* CONFIG_SMP */
  71. /* alternative assembly primitive: */
  72. #define ALTERNATIVE(oldinstr, newinstr, feature) \
  73. \
  74. "661:\n\t" oldinstr "\n662:\n" \
  75. ".section .altinstructions,\"a\"\n" \
  76. _ASM_ALIGN "\n" \
  77. _ASM_PTR "661b\n" /* label */ \
  78. _ASM_PTR "663f\n" /* new instruction */ \
  79. " .word " __stringify(feature) "\n" /* feature bit */ \
  80. " .byte 662b-661b\n" /* sourcelen */ \
  81. " .byte 664f-663f\n" /* replacementlen */ \
  82. ".previous\n" \
  83. ".section .discard,\"aw\",@progbits\n" \
  84. " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \
  85. ".previous\n" \
  86. ".section .altinstr_replacement, \"ax\"\n" \
  87. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  88. ".previous"
  89. /*
  90. * This must be included *after* the definition of ALTERNATIVE due to
  91. * <asm/arch_hweight.h>
  92. */
  93. #include <asm/cpufeature.h>
  94. /*
  95. * Alternative instructions for different CPU types or capabilities.
  96. *
  97. * This allows to use optimized instructions even on generic binary
  98. * kernels.
  99. *
  100. * length of oldinstr must be longer or equal the length of newinstr
  101. * It can be padded with nops as needed.
  102. *
  103. * For non barrier like inlines please define new variants
  104. * without volatile and memory clobber.
  105. */
  106. #define alternative(oldinstr, newinstr, feature) \
  107. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
  108. /*
  109. * Alternative inline assembly with input.
  110. *
  111. * Pecularities:
  112. * No memory clobber here.
  113. * Argument numbers start with 1.
  114. * Best is to use constraints that are fixed size (like (%1) ... "r")
  115. * If you use variable sized constraints like "m" or "g" in the
  116. * replacement make sure to pad to the worst case length.
  117. * Leaving an unused argument 0 to keep API compatibility.
  118. */
  119. #define alternative_input(oldinstr, newinstr, feature, input...) \
  120. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  121. : : "i" (0), ## input)
  122. /* Like alternative_input, but with a single output argument */
  123. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  124. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  125. : output : "i" (0), ## input)
  126. /* Like alternative_io, but for replacing a direct call with another one. */
  127. #define alternative_call(oldfunc, newfunc, feature, output, input...) \
  128. asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
  129. : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
  130. /*
  131. * use this macro(s) if you need more than one output parameter
  132. * in alternative_io
  133. */
  134. #define ASM_OUTPUT2(a...) a
  135. struct paravirt_patch_site;
  136. #ifdef CONFIG_PARAVIRT
  137. void apply_paravirt(struct paravirt_patch_site *start,
  138. struct paravirt_patch_site *end);
  139. #else
  140. static inline void apply_paravirt(struct paravirt_patch_site *start,
  141. struct paravirt_patch_site *end)
  142. {}
  143. #define __parainstructions NULL
  144. #define __parainstructions_end NULL
  145. #endif
  146. extern void *text_poke_early(void *addr, const void *opcode, size_t len);
  147. /*
  148. * Clear and restore the kernel write-protection flag on the local CPU.
  149. * Allows the kernel to edit read-only pages.
  150. * Side-effect: any interrupt handler running between save and restore will have
  151. * the ability to write to read-only pages.
  152. *
  153. * Warning:
  154. * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
  155. * no thread can be preempted in the instructions being modified (no iret to an
  156. * invalid instruction possible) or if the instructions are changed from a
  157. * consistent state to another consistent state atomically.
  158. * More care must be taken when modifying code in the SMP case because of
  159. * Intel's errata. text_poke_smp() takes care that errata, but still
  160. * doesn't support NMI/MCE handler code modifying.
  161. * On the local CPU you need to be protected again NMI or MCE handlers seeing an
  162. * inconsistent instruction while you patch.
  163. */
  164. extern void *text_poke(void *addr, const void *opcode, size_t len);
  165. extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
  166. #if defined(CONFIG_DYNAMIC_FTRACE) || defined(HAVE_JUMP_LABEL)
  167. #define IDEAL_NOP_SIZE_5 5
  168. extern unsigned char ideal_nop5[IDEAL_NOP_SIZE_5];
  169. extern void arch_init_ideal_nop5(void);
  170. #else
  171. static inline void arch_init_ideal_nop5(void) {}
  172. #endif
  173. #endif /* _ASM_X86_ALTERNATIVE_H */