alternative-asm.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_S390_ALTERNATIVE_ASM_H
  3. #define _ASM_S390_ALTERNATIVE_ASM_H
  4. #ifdef __ASSEMBLY__
  5. /*
  6. * Check the length of an instruction sequence. The length may not be larger
  7. * than 254 bytes and it has to be divisible by 2.
  8. */
  9. .macro alt_len_check start,end
  10. .if ( \end - \start ) > 254
  11. .error "cpu alternatives does not support instructions blocks > 254 bytes\n"
  12. .endif
  13. .if ( \end - \start ) % 2
  14. .error "cpu alternatives instructions length is odd\n"
  15. .endif
  16. .endm
  17. /*
  18. * Issue one struct alt_instr descriptor entry (need to put it into
  19. * the section .altinstructions, see below). This entry contains
  20. * enough information for the alternatives patching code to patch an
  21. * instruction. See apply_alternatives().
  22. */
  23. .macro alt_entry orig_start, orig_end, alt_start, alt_end, feature
  24. .long \orig_start - .
  25. .long \alt_start - .
  26. .word \feature
  27. .byte \orig_end - \orig_start
  28. .byte \alt_end - \alt_start
  29. .endm
  30. /*
  31. * Fill up @bytes with nops. The macro emits 6-byte nop instructions
  32. * for the bulk of the area, possibly followed by a 4-byte and/or
  33. * a 2-byte nop if the size of the area is not divisible by 6.
  34. */
  35. .macro alt_pad_fill bytes
  36. .fill ( \bytes ) / 6, 6, 0xc0040000
  37. .fill ( \bytes ) % 6 / 4, 4, 0x47000000
  38. .fill ( \bytes ) % 6 % 4 / 2, 2, 0x0700
  39. .endm
  40. /*
  41. * Fill up @bytes with nops. If the number of bytes is larger
  42. * than 6, emit a jg instruction to branch over all nops, then
  43. * fill an area of size (@bytes - 6) with nop instructions.
  44. */
  45. .macro alt_pad bytes
  46. .if ( \bytes > 0 )
  47. .if ( \bytes > 6 )
  48. jg . + \bytes
  49. alt_pad_fill \bytes - 6
  50. .else
  51. alt_pad_fill \bytes
  52. .endif
  53. .endif
  54. .endm
  55. /*
  56. * Define an alternative between two instructions. If @feature is
  57. * present, early code in apply_alternatives() replaces @oldinstr with
  58. * @newinstr. ".skip" directive takes care of proper instruction padding
  59. * in case @newinstr is longer than @oldinstr.
  60. */
  61. .macro ALTERNATIVE oldinstr, newinstr, feature
  62. .pushsection .altinstr_replacement,"ax"
  63. 770: \newinstr
  64. 771: .popsection
  65. 772: \oldinstr
  66. 773: alt_len_check 770b, 771b
  67. alt_len_check 772b, 773b
  68. alt_pad ( ( 771b - 770b ) - ( 773b - 772b ) )
  69. 774: .pushsection .altinstructions,"a"
  70. alt_entry 772b, 774b, 770b, 771b, \feature
  71. .popsection
  72. .endm
  73. /*
  74. * Define an alternative between two instructions. If @feature is
  75. * present, early code in apply_alternatives() replaces @oldinstr with
  76. * @newinstr. ".skip" directive takes care of proper instruction padding
  77. * in case @newinstr is longer than @oldinstr.
  78. */
  79. .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
  80. .pushsection .altinstr_replacement,"ax"
  81. 770: \newinstr1
  82. 771: \newinstr2
  83. 772: .popsection
  84. 773: \oldinstr
  85. 774: alt_len_check 770b, 771b
  86. alt_len_check 771b, 772b
  87. alt_len_check 773b, 774b
  88. .if ( 771b - 770b > 772b - 771b )
  89. alt_pad ( ( 771b - 770b ) - ( 774b - 773b ) )
  90. .else
  91. alt_pad ( ( 772b - 771b ) - ( 774b - 773b ) )
  92. .endif
  93. 775: .pushsection .altinstructions,"a"
  94. alt_entry 773b, 775b, 770b, 771b,\feature1
  95. alt_entry 773b, 775b, 771b, 772b,\feature2
  96. .popsection
  97. .endm
  98. #endif /* __ASSEMBLY__ */
  99. #endif /* _ASM_S390_ALTERNATIVE_ASM_H */