alternative_32.h 5.4 KB

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