1 #ifndef _ASM_S390_ALTERNATIVE_H 2 #define _ASM_S390_ALTERNATIVE_H 3 4 #ifndef __ASSEMBLY__ 5 6 #include <linux/types.h> 7 #include <linux/stddef.h> 8 #include <linux/stringify.h> 9 10 struct alt_instr { 11 s32 instr_offset; /* original instruction */ 12 s32 repl_offset; /* offset to replacement instruction */ 13 u16 facility; /* facility bit set for replacement */ 14 u8 instrlen; /* length of original instruction */ 15 u8 replacementlen; /* length of new instruction */ 16 } __packed; 17 18 void apply_alternative_instructions(void); 19 void apply_alternatives(struct alt_instr *start, struct alt_instr *end); 20 21 /* 22 * |661: |662: |6620 |663: 23 * +-----------+---------------------+ 24 * | oldinstr | oldinstr_padding | 25 * | +----------+----------+ 26 * | | | | 27 * | | >6 bytes |6/4/2 nops| 28 * | |6 bytes jg-----------> 29 * +-----------+---------------------+ 30 * ^^ static padding ^^ 31 * 32 * .altinstr_replacement section 33 * +---------------------+-----------+ 34 * |6641: |6651: 35 * | alternative instr 1 | 36 * +-----------+---------+- - - - - -+ 37 * |6642: |6652: | 38 * | alternative instr 2 | padding 39 * +---------------------+- - - - - -+ 40 * ^ runtime ^ 41 * 42 * .altinstructions section 43 * +---------------------------------+ 44 * | alt_instr entries for each | 45 * | alternative instr | 46 * +---------------------------------+ 47 */ 48 49 #define b_altinstr(num) "664"#num 50 #define e_altinstr(num) "665"#num 51 52 #define e_oldinstr_pad_end "663" 53 #define oldinstr_len "662b-661b" 54 #define oldinstr_total_len e_oldinstr_pad_end"b-661b" 55 #define altinstr_len(num) e_altinstr(num)"b-"b_altinstr(num)"b" 56 #define oldinstr_pad_len(num) \ 57 "-(((" altinstr_len(num) ")-(" oldinstr_len ")) > 0) * " \ 58 "((" altinstr_len(num) ")-(" oldinstr_len "))" 59 60 #define INSTR_LEN_SANITY_CHECK(len) \ 61 ".if " len " > 254\n" \ 62 "\t.error \"cpu alternatives does not support instructions " \ 63 "blocks > 254 bytes\"\n" \ 64 ".endif\n" \ 65 ".if (" len ") %% 2\n" \ 66 "\t.error \"cpu alternatives instructions length is odd\"\n" \ 67 ".endif\n" 68 69 #define OLDINSTR_PADDING(oldinstr, num) \ 70 ".if " oldinstr_pad_len(num) " > 6\n" \ 71 "\tjg " e_oldinstr_pad_end "f\n" \ 72 "6620:\n" \ 73 "\t.fill (" oldinstr_pad_len(num) " - (6620b-662b)) / 2, 2, 0x0700\n" \ 74 ".else\n" \ 75 "\t.fill " oldinstr_pad_len(num) " / 6, 6, 0xc0040000\n" \ 76 "\t.fill " oldinstr_pad_len(num) " %% 6 / 4, 4, 0x47000000\n" \ 77 "\t.fill " oldinstr_pad_len(num) " %% 6 %% 4 / 2, 2, 0x0700\n" \ 78 ".endif\n" 79 80 #define OLDINSTR(oldinstr, num) \ 81 "661:\n\t" oldinstr "\n662:\n" \ 82 OLDINSTR_PADDING(oldinstr, num) \ 83 e_oldinstr_pad_end ":\n" \ 84 INSTR_LEN_SANITY_CHECK(oldinstr_len) 85 86 #define OLDINSTR_2(oldinstr, num1, num2) \ 87 "661:\n\t" oldinstr "\n662:\n" \ 88 ".if " altinstr_len(num1) " < " altinstr_len(num2) "\n" \ 89 OLDINSTR_PADDING(oldinstr, num2) \ 90 ".else\n" \ 91 OLDINSTR_PADDING(oldinstr, num1) \ 92 ".endif\n" \ 93 e_oldinstr_pad_end ":\n" \ 94 INSTR_LEN_SANITY_CHECK(oldinstr_len) 95 96 #define ALTINSTR_ENTRY(facility, num) \ 97 "\t.long 661b - .\n" /* old instruction */ \ 98 "\t.long " b_altinstr(num)"b - .\n" /* alt instruction */ \ 99 "\t.word " __stringify(facility) "\n" /* facility bit */ \ 100 "\t.byte " oldinstr_total_len "\n" /* source len */ \ 101 "\t.byte " altinstr_len(num) "\n" /* alt instruction len */ 102 103 #define ALTINSTR_REPLACEMENT(altinstr, num) /* replacement */ \ 104 b_altinstr(num)":\n\t" altinstr "\n" e_altinstr(num) ":\n" \ 105 INSTR_LEN_SANITY_CHECK(altinstr_len(num)) 106 107 /* alternative assembly primitive: */ 108 #define ALTERNATIVE(oldinstr, altinstr, facility) \ 109 ".pushsection .altinstr_replacement, \"ax\"\n" \ 110 ALTINSTR_REPLACEMENT(altinstr, 1) \ 111 ".popsection\n" \ 112 OLDINSTR(oldinstr, 1) \ 113 ".pushsection .altinstructions,\"a\"\n" \ 114 ALTINSTR_ENTRY(facility, 1) \ 115 ".popsection\n" 116 117 #define ALTERNATIVE_2(oldinstr, altinstr1, facility1, altinstr2, facility2)\ 118 ".pushsection .altinstr_replacement, \"ax\"\n" \ 119 ALTINSTR_REPLACEMENT(altinstr1, 1) \ 120 ALTINSTR_REPLACEMENT(altinstr2, 2) \ 121 ".popsection\n" \ 122 OLDINSTR_2(oldinstr, 1, 2) \ 123 ".pushsection .altinstructions,\"a\"\n" \ 124 ALTINSTR_ENTRY(facility1, 1) \ 125 ALTINSTR_ENTRY(facility2, 2) \ 126 ".popsection\n" 127 128 /* 129 * Alternative instructions for different CPU types or capabilities. 130 * 131 * This allows to use optimized instructions even on generic binary 132 * kernels. 133 * 134 * oldinstr is padded with jump and nops at compile time if altinstr is 135 * longer. altinstr is padded with jump and nops at run-time during patching. 136 * 137 * For non barrier like inlines please define new variants 138 * without volatile and memory clobber. 139 */ 140 #define alternative(oldinstr, altinstr, facility) \ 141 asm volatile(ALTERNATIVE(oldinstr, altinstr, facility) : : : "memory") 142 143 #define alternative_2(oldinstr, altinstr1, facility1, altinstr2, facility2) \ 144 asm volatile(ALTERNATIVE_2(oldinstr, altinstr1, facility1, \ 145 altinstr2, facility2) ::: "memory") 146 147 #endif /* __ASSEMBLY__ */ 148 149 #endif /* _ASM_S390_ALTERNATIVE_H */ 150