alternative.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef _X86_64_ALTERNATIVE_H
  2. #define _X86_64_ALTERNATIVE_H
  3. #ifdef __KERNEL__
  4. #include <linux/types.h>
  5. #include <linux/stddef.h>
  6. #include <asm/cpufeature.h>
  7. struct alt_instr {
  8. u8 *instr; /* original instruction */
  9. u8 *replacement;
  10. u8 cpuid; /* cpuid bit set for replacement */
  11. u8 instrlen; /* length of original instruction */
  12. u8 replacementlen; /* length of new instruction, <= instrlen */
  13. u8 pad[5];
  14. };
  15. extern void alternative_instructions(void);
  16. extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
  17. struct module;
  18. #ifdef CONFIG_SMP
  19. extern void alternatives_smp_module_add(struct module *mod, char *name,
  20. void *locks, void *locks_end,
  21. void *text, void *text_end);
  22. extern void alternatives_smp_module_del(struct module *mod);
  23. extern void alternatives_smp_switch(int smp);
  24. #else
  25. static inline void alternatives_smp_module_add(struct module *mod, char *name,
  26. void *locks, void *locks_end,
  27. void *text, void *text_end) {}
  28. static inline void alternatives_smp_module_del(struct module *mod) {}
  29. static inline void alternatives_smp_switch(int smp) {}
  30. #endif
  31. #endif
  32. /*
  33. * Alternative instructions for different CPU types or capabilities.
  34. *
  35. * This allows to use optimized instructions even on generic binary
  36. * kernels.
  37. *
  38. * length of oldinstr must be longer or equal the length of newinstr
  39. * It can be padded with nops as needed.
  40. *
  41. * For non barrier like inlines please define new variants
  42. * without volatile and memory clobber.
  43. */
  44. #define alternative(oldinstr, newinstr, feature) \
  45. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  46. ".section .altinstructions,\"a\"\n" \
  47. " .align 8\n" \
  48. " .quad 661b\n" /* label */ \
  49. " .quad 663f\n" /* new instruction */ \
  50. " .byte %c0\n" /* feature bit */ \
  51. " .byte 662b-661b\n" /* sourcelen */ \
  52. " .byte 664f-663f\n" /* replacementlen */ \
  53. ".previous\n" \
  54. ".section .altinstr_replacement,\"ax\"\n" \
  55. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  56. ".previous" :: "i" (feature) : "memory")
  57. /*
  58. * Alternative inline assembly with input.
  59. *
  60. * Pecularities:
  61. * No memory clobber here.
  62. * Argument numbers start with 1.
  63. * Best is to use constraints that are fixed size (like (%1) ... "r")
  64. * If you use variable sized constraints like "m" or "g" in the
  65. * replacement make sure to pad to the worst case length.
  66. */
  67. #define alternative_input(oldinstr, newinstr, feature, input...) \
  68. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  69. ".section .altinstructions,\"a\"\n" \
  70. " .align 8\n" \
  71. " .quad 661b\n" /* label */ \
  72. " .quad 663f\n" /* new instruction */ \
  73. " .byte %c0\n" /* feature bit */ \
  74. " .byte 662b-661b\n" /* sourcelen */ \
  75. " .byte 664f-663f\n" /* replacementlen */ \
  76. ".previous\n" \
  77. ".section .altinstr_replacement,\"ax\"\n" \
  78. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  79. ".previous" :: "i" (feature), ##input)
  80. /* Like alternative_input, but with a single output argument */
  81. #define alternative_io(oldinstr, newinstr, feature, output, input...) \
  82. asm volatile ("661:\n\t" oldinstr "\n662:\n" \
  83. ".section .altinstructions,\"a\"\n" \
  84. " .align 8\n" \
  85. " .quad 661b\n" /* label */ \
  86. " .quad 663f\n" /* new instruction */ \
  87. " .byte %c[feat]\n" /* feature bit */ \
  88. " .byte 662b-661b\n" /* sourcelen */ \
  89. " .byte 664f-663f\n" /* replacementlen */ \
  90. ".previous\n" \
  91. ".section .altinstr_replacement,\"ax\"\n" \
  92. "663:\n\t" newinstr "\n664:\n" /* replacement */ \
  93. ".previous" : output : [feat] "i" (feature), ##input)
  94. /*
  95. * use this macro(s) if you need more than one output parameter
  96. * in alternative_io
  97. */
  98. #define ASM_OUTPUT2(a, b) a, b
  99. /*
  100. * Alternative inline assembly for SMP.
  101. *
  102. * The LOCK_PREFIX macro defined here replaces the LOCK and
  103. * LOCK_PREFIX macros used everywhere in the source tree.
  104. *
  105. * SMP alternatives use the same data structures as the other
  106. * alternatives and the X86_FEATURE_UP flag to indicate the case of a
  107. * UP system running a SMP kernel. The existing apply_alternatives()
  108. * works fine for patching a SMP kernel for UP.
  109. *
  110. * The SMP alternative tables can be kept after boot and contain both
  111. * UP and SMP versions of the instructions to allow switching back to
  112. * SMP at runtime, when hotplugging in a new CPU, which is especially
  113. * useful in virtualized environments.
  114. *
  115. * The very common lock prefix is handled as special case in a
  116. * separate table which is a pure address list without replacement ptr
  117. * and size information. That keeps the table sizes small.
  118. */
  119. #ifdef CONFIG_SMP
  120. #define LOCK_PREFIX \
  121. ".section .smp_locks,\"a\"\n" \
  122. " .align 8\n" \
  123. " .quad 661f\n" /* address */ \
  124. ".previous\n" \
  125. "661:\n\tlock; "
  126. #else /* ! CONFIG_SMP */
  127. #define LOCK_PREFIX ""
  128. #endif
  129. struct paravirt_patch;
  130. #ifdef CONFIG_PARAVIRT
  131. void apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end);
  132. #else
  133. static inline void
  134. apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end)
  135. {}
  136. #define __parainstructions NULL
  137. #define __parainstructions_end NULL
  138. #endif
  139. #endif /* _X86_64_ALTERNATIVE_H */