alternative.h 5.9 KB

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