alternative.h 5.5 KB

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