alternative.h 4.6 KB

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