alternative.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /*
  8. * Alternative inline assembly for SMP.
  9. *
  10. * The LOCK_PREFIX macro defined here replaces the LOCK and
  11. * LOCK_PREFIX macros used everywhere in the source tree.
  12. *
  13. * SMP alternatives use the same data structures as the other
  14. * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  15. * UP system running a SMP kernel. The existing apply_alternatives()
  16. * works fine for patching a SMP kernel for UP.
  17. *
  18. * The SMP alternative tables can be kept after boot and contain both
  19. * UP and SMP versions of the instructions to allow switching back to
  20. * SMP at runtime, when hotplugging in a new CPU, which is especially
  21. * useful in virtualized environments.
  22. *
  23. * The very common lock prefix is handled as special case in a
  24. * separate table which is a pure address list without replacement ptr
  25. * and size information. That keeps the table sizes small.
  26. */
  27. #ifdef CONFIG_SMP
  28. #define LOCK_PREFIX_HERE \
  29. ".section .smp_locks,\"a\"\n" \
  30. ".balign 4\n" \
  31. ".long 671f - .\n" /* offset */ \
  32. ".previous\n" \
  33. "671:"
  34. #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
  35. #else /* ! CONFIG_SMP */
  36. #define LOCK_PREFIX_HERE ""
  37. #define LOCK_PREFIX ""
  38. #endif
  39. struct alt_instr {
  40. u8 *instr; /* original instruction */
  41. u8 *replacement;
  42. u8 cpuid; /* cpuid bit set for replacement */
  43. u8 instrlen; /* length of original instruction */
  44. u8 replacementlen; /* length of new instruction, <= instrlen */
  45. u8 pad1;
  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. " .byte " __stringify(feature) "\n" /* feature bit */ \
  80. " .byte 662b-661b\n" /* sourcelen */ \
  81. " .byte 664f-663f\n" /* replacementlen */ \
  82. " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \
  83. ".previous\n" \
  84. ".section .altinstr_replacement, \"ax\"\n" \
  85. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  86. ".previous"
  87. /*
  88. * This must be included *after* the definition of ALTERNATIVE due to
  89. * <asm/arch_hweight.h>
  90. */
  91. #include <asm/cpufeature.h>
  92. /*
  93. * Alternative instructions for different CPU types or capabilities.
  94. *
  95. * This allows to use optimized instructions even on generic binary
  96. * kernels.
  97. *
  98. * length of oldinstr must be longer or equal the length of newinstr
  99. * It can be padded with nops as needed.
  100. *
  101. * For non barrier like inlines please define new variants
  102. * without volatile and memory clobber.
  103. */
  104. #define alternative(oldinstr, newinstr, feature) \
  105. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
  106. /*
  107. * Alternative inline assembly with input.
  108. *
  109. * Pecularities:
  110. * No memory clobber here.
  111. * Argument numbers start with 1.
  112. * Best is to use constraints that are fixed size (like (%1) ... "r")
  113. * If you use variable sized constraints like "m" or "g" in the
  114. * replacement make sure to pad to the worst case length.
  115. * Leaving an unused argument 0 to keep API compatibility.
  116. */
  117. #define alternative_input(oldinstr, newinstr, feature, input...) \
  118. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  119. : : "i" (0), ## input)
  120. /* Like alternative_input, but with a single output argument */
  121. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  122. asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
  123. : output : "i" (0), ## input)
  124. /* Like alternative_io, but for replacing a direct call with another one. */
  125. #define alternative_call(oldfunc, newfunc, feature, output, input...) \
  126. asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
  127. : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
  128. /*
  129. * use this macro(s) if you need more than one output parameter
  130. * in alternative_io
  131. */
  132. #define ASM_OUTPUT2(a...) a
  133. struct paravirt_patch_site;
  134. #ifdef CONFIG_PARAVIRT
  135. void apply_paravirt(struct paravirt_patch_site *start,
  136. struct paravirt_patch_site *end);
  137. #else
  138. static inline void apply_paravirt(struct paravirt_patch_site *start,
  139. struct paravirt_patch_site *end)
  140. {}
  141. #define __parainstructions NULL
  142. #define __parainstructions_end NULL
  143. #endif
  144. /*
  145. * Clear and restore the kernel write-protection flag on the local CPU.
  146. * Allows the kernel to edit read-only pages.
  147. * Side-effect: any interrupt handler running between save and restore will have
  148. * the ability to write to read-only pages.
  149. *
  150. * Warning:
  151. * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
  152. * no thread can be preempted in the instructions being modified (no iret to an
  153. * invalid instruction possible) or if the instructions are changed from a
  154. * consistent state to another consistent state atomically.
  155. * More care must be taken when modifying code in the SMP case because of
  156. * Intel's errata. text_poke_smp() takes care that errata, but still
  157. * doesn't support NMI/MCE handler code modifying.
  158. * On the local CPU you need to be protected again NMI or MCE handlers seeing an
  159. * inconsistent instruction while you patch.
  160. */
  161. extern void *text_poke(void *addr, const void *opcode, size_t len);
  162. extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
  163. #endif /* _ASM_X86_ALTERNATIVE_H */