0001
0002 #ifndef _ASM_X86_ALTERNATIVE_H
0003 #define _ASM_X86_ALTERNATIVE_H
0004
0005 #include <linux/types.h>
0006 #include <linux/stringify.h>
0007 #include <asm/asm.h>
0008
0009 #define ALTINSTR_FLAG_INV (1 << 15)
0010 #define ALT_NOT(feat) ((feat) | ALTINSTR_FLAG_INV)
0011
0012 #ifndef __ASSEMBLY__
0013
0014 #include <linux/stddef.h>
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 #ifdef CONFIG_SMP
0038 #define LOCK_PREFIX_HERE \
0039 ".pushsection .smp_locks,\"a\"\n" \
0040 ".balign 4\n" \
0041 ".long 671f - .\n" \
0042 ".popsection\n" \
0043 "671:"
0044
0045 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
0046
0047 #else
0048 #define LOCK_PREFIX_HERE ""
0049 #define LOCK_PREFIX ""
0050 #endif
0051
0052
0053
0054
0055
0056 #define ANNOTATE_IGNORE_ALTERNATIVE \
0057 "999:\n\t" \
0058 ".pushsection .discard.ignore_alts\n\t" \
0059 ".long 999b - .\n\t" \
0060 ".popsection\n\t"
0061
0062 struct alt_instr {
0063 s32 instr_offset;
0064 s32 repl_offset;
0065 u16 cpuid;
0066 u8 instrlen;
0067 u8 replacementlen;
0068 } __packed;
0069
0070
0071
0072
0073
0074 extern int alternatives_patched;
0075
0076 extern void alternative_instructions(void);
0077 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
0078 extern void apply_retpolines(s32 *start, s32 *end);
0079 extern void apply_returns(s32 *start, s32 *end);
0080 extern void apply_ibt_endbr(s32 *start, s32 *end);
0081
0082 struct module;
0083
0084 #ifdef CONFIG_SMP
0085 extern void alternatives_smp_module_add(struct module *mod, char *name,
0086 void *locks, void *locks_end,
0087 void *text, void *text_end);
0088 extern void alternatives_smp_module_del(struct module *mod);
0089 extern void alternatives_enable_smp(void);
0090 extern int alternatives_text_reserved(void *start, void *end);
0091 extern bool skip_smp_alternatives;
0092 #else
0093 static inline void alternatives_smp_module_add(struct module *mod, char *name,
0094 void *locks, void *locks_end,
0095 void *text, void *text_end) {}
0096 static inline void alternatives_smp_module_del(struct module *mod) {}
0097 static inline void alternatives_enable_smp(void) {}
0098 static inline int alternatives_text_reserved(void *start, void *end)
0099 {
0100 return 0;
0101 }
0102 #endif
0103
0104 #define b_replacement(num) "664"#num
0105 #define e_replacement(num) "665"#num
0106
0107 #define alt_end_marker "663"
0108 #define alt_slen "662b-661b"
0109 #define alt_total_slen alt_end_marker"b-661b"
0110 #define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
0111
0112 #define OLDINSTR(oldinstr, num) \
0113 "# ALT: oldnstr\n" \
0114 "661:\n\t" oldinstr "\n662:\n" \
0115 "# ALT: padding\n" \
0116 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
0117 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
0118 alt_end_marker ":\n"
0119
0120
0121
0122
0123
0124
0125
0126 #define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
0127
0128
0129
0130
0131
0132 #define OLDINSTR_2(oldinstr, num1, num2) \
0133 "# ALT: oldinstr2\n" \
0134 "661:\n\t" oldinstr "\n662:\n" \
0135 "# ALT: padding2\n" \
0136 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
0137 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
0138 alt_end_marker ":\n"
0139
0140 #define OLDINSTR_3(oldinsn, n1, n2, n3) \
0141 "# ALT: oldinstr3\n" \
0142 "661:\n\t" oldinsn "\n662:\n" \
0143 "# ALT: padding3\n" \
0144 ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
0145 " - (" alt_slen ")) > 0) * " \
0146 "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
0147 " - (" alt_slen ")), 0x90\n" \
0148 alt_end_marker ":\n"
0149
0150 #define ALTINSTR_ENTRY(feature, num) \
0151 " .long 661b - .\n" \
0152 " .long " b_replacement(num)"f - .\n" \
0153 " .word " __stringify(feature) "\n" \
0154 " .byte " alt_total_slen "\n" \
0155 " .byte " alt_rlen(num) "\n"
0156
0157 #define ALTINSTR_REPLACEMENT(newinstr, num) \
0158 "# ALT: replacement " #num "\n" \
0159 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
0160
0161
0162 #define ALTERNATIVE(oldinstr, newinstr, feature) \
0163 OLDINSTR(oldinstr, 1) \
0164 ".pushsection .altinstructions,\"a\"\n" \
0165 ALTINSTR_ENTRY(feature, 1) \
0166 ".popsection\n" \
0167 ".pushsection .altinstr_replacement, \"ax\"\n" \
0168 ALTINSTR_REPLACEMENT(newinstr, 1) \
0169 ".popsection\n"
0170
0171 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
0172 OLDINSTR_2(oldinstr, 1, 2) \
0173 ".pushsection .altinstructions,\"a\"\n" \
0174 ALTINSTR_ENTRY(feature1, 1) \
0175 ALTINSTR_ENTRY(feature2, 2) \
0176 ".popsection\n" \
0177 ".pushsection .altinstr_replacement, \"ax\"\n" \
0178 ALTINSTR_REPLACEMENT(newinstr1, 1) \
0179 ALTINSTR_REPLACEMENT(newinstr2, 2) \
0180 ".popsection\n"
0181
0182
0183 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
0184 ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
0185 newinstr_yes, feature)
0186
0187 #define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
0188 OLDINSTR_3(oldinsn, 1, 2, 3) \
0189 ".pushsection .altinstructions,\"a\"\n" \
0190 ALTINSTR_ENTRY(feat1, 1) \
0191 ALTINSTR_ENTRY(feat2, 2) \
0192 ALTINSTR_ENTRY(feat3, 3) \
0193 ".popsection\n" \
0194 ".pushsection .altinstr_replacement, \"ax\"\n" \
0195 ALTINSTR_REPLACEMENT(newinsn1, 1) \
0196 ALTINSTR_REPLACEMENT(newinsn2, 2) \
0197 ALTINSTR_REPLACEMENT(newinsn3, 3) \
0198 ".popsection\n"
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 #define alternative(oldinstr, newinstr, feature) \
0213 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
0214
0215 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
0216 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
0217
0218 #define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
0219 asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 #define alternative_input(oldinstr, newinstr, feature, input...) \
0230 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
0231 : : "i" (0), ## input)
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
0242 feature2, input...) \
0243 asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
0244 newinstr2, feature2) \
0245 : : "i" (0), ## input)
0246
0247
0248 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
0249 asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
0250 : output : "i" (0), ## input)
0251
0252
0253 #define alternative_call(oldfunc, newfunc, feature, output, input...) \
0254 asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
0255 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
0256
0257
0258
0259
0260
0261
0262
0263 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
0264 output, input...) \
0265 asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
0266 "call %P[new2]", feature2) \
0267 : output, ASM_CALL_CONSTRAINT \
0268 : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
0269 [new2] "i" (newfunc2), ## input)
0270
0271
0272
0273
0274
0275 #define ASM_OUTPUT2(a...) a
0276
0277
0278
0279
0280
0281 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
0282
0283 #else
0284
0285 #ifdef CONFIG_SMP
0286 .macro LOCK_PREFIX
0287 672: lock
0288 .pushsection .smp_locks,"a"
0289 .balign 4
0290 .long 672b - .
0291 .popsection
0292 .endm
0293 #else
0294 .macro LOCK_PREFIX
0295 .endm
0296 #endif
0297
0298
0299
0300
0301
0302 .macro ANNOTATE_IGNORE_ALTERNATIVE
0303 .Lannotate_\@:
0304 .pushsection .discard.ignore_alts
0305 .long .Lannotate_\@ - .
0306 .popsection
0307 .endm
0308
0309
0310
0311
0312
0313
0314
0315 .macro altinstruction_entry orig alt feature orig_len alt_len
0316 .long \orig - .
0317 .long \alt - .
0318 .word \feature
0319 .byte \orig_len
0320 .byte \alt_len
0321 .endm
0322
0323
0324
0325
0326
0327
0328
0329 .macro ALTERNATIVE oldinstr, newinstr, feature
0330 140:
0331 \oldinstr
0332 141:
0333 .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
0334 142:
0335
0336 .pushsection .altinstructions,"a"
0337 altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
0338 .popsection
0339
0340 .pushsection .altinstr_replacement,"ax"
0341 143:
0342 \newinstr
0343 144:
0344 .popsection
0345 .endm
0346
0347 #define old_len 141b-140b
0348 #define new_len1 144f-143f
0349 #define new_len2 145f-144f
0350
0351
0352
0353
0354
0355
0356
0357 #define alt_max_short(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
0358
0359
0360
0361
0362
0363
0364
0365 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
0366 140:
0367 \oldinstr
0368 141:
0369 .skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
0370 (alt_max_short(new_len1, new_len2) - (old_len)),0x90
0371 142:
0372
0373 .pushsection .altinstructions,"a"
0374 altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
0375 altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
0376 .popsection
0377
0378 .pushsection .altinstr_replacement,"ax"
0379 143:
0380 \newinstr1
0381 144:
0382 \newinstr2
0383 145:
0384 .popsection
0385 .endm
0386
0387
0388 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
0389 ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
0390 newinstr_yes, feature
0391
0392 #endif
0393
0394 #endif