alternative_64.h 5.5 KB

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