blob: 6c98821fef5ed9f0b953141a905b99893663652e [file] [log] [blame]
H. Peter Anvin76f30752012-09-21 12:43:09 -07001#ifndef _ASM_X86_ALTERNATIVE_ASM_H
2#define _ASM_X86_ALTERNATIVE_ASM_H
3
Adrian Bunk7e02cb92007-10-17 18:04:38 +02004#ifdef __ASSEMBLY__
5
Jan Beulich99063c02009-11-27 15:06:16 +00006#include <asm/asm.h>
Adrian Bunk7e02cb92007-10-17 18:04:38 +02007
8#ifdef CONFIG_SMP
9 .macro LOCK_PREFIX
Eric Dumazetceb7b402012-01-03 17:35:40 +010010672: lock
H. Peter Anvin9cebed42012-09-21 12:43:08 -070011 .pushsection .smp_locks,"a"
Jan Beulich5967ed82010-04-21 16:08:14 +010012 .balign 4
Eric Dumazetceb7b402012-01-03 17:35:40 +010013 .long 672b - .
H. Peter Anvin9cebed42012-09-21 12:43:08 -070014 .popsection
Adrian Bunk7e02cb92007-10-17 18:04:38 +020015 .endm
16#else
17 .macro LOCK_PREFIX
18 .endm
19#endif
20
Borislav Petkov5b673a42015-04-04 16:40:45 +020021/*
22 * Issue one struct alt_instr descriptor entry (need to put it into
23 * the section .altinstructions, see below). This entry contains
24 * enough information for the alternatives patching code to patch an
25 * instruction. See apply_alternatives().
26 */
Borislav Petkov43321952014-12-27 10:41:52 +010027.macro altinstruction_entry orig alt feature orig_len alt_len pad_len
Andy Lutomirski59e97e42011-07-13 09:24:10 -040028 .long \orig - .
29 .long \alt - .
Fenghua Yu9072d112011-05-17 15:29:13 -070030 .word \feature
31 .byte \orig_len
32 .byte \alt_len
Borislav Petkov43321952014-12-27 10:41:52 +010033 .byte \pad_len
34.endm
35
Borislav Petkov5b673a42015-04-04 16:40:45 +020036/*
37 * Define an alternative between two instructions. If @feature is
38 * present, early code in apply_alternatives() replaces @oldinstr with
39 * @newinstr. ".skip" directive takes care of proper instruction padding
40 * in case @newinstr is longer than @oldinstr.
41 */
Borislav Petkov43321952014-12-27 10:41:52 +010042.macro ALTERNATIVE oldinstr, newinstr, feature
43140:
44 \oldinstr
45141:
46 .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
47142:
48
49 .pushsection .altinstructions,"a"
50 altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f,142b-141b
51 .popsection
52
53 .pushsection .altinstr_replacement,"ax"
54143:
55 \newinstr
56144:
57 .popsection
58.endm
59
Borislav Petkovdbe40582015-04-04 15:34:43 +020060#define old_len 141b-140b
61#define new_len1 144f-143f
62#define new_len2 145f-144f
63
64/*
Mathias Krausefb6da442017-10-05 20:30:12 +020065 * gas compatible max based on the idea from:
Borislav Petkovdbe40582015-04-04 15:34:43 +020066 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
Mathias Krausefb6da442017-10-05 20:30:12 +020067 *
68 * The additional "-" is needed because gas uses a "true" value of -1.
Borislav Petkovdbe40582015-04-04 15:34:43 +020069 */
70#define alt_max_short(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
71
Borislav Petkov5b673a42015-04-04 16:40:45 +020072
73/*
74 * Same as ALTERNATIVE macro above but for two alternatives. If CPU
75 * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
76 * @feature2, it replaces @oldinstr with @feature2.
77 */
Borislav Petkov43321952014-12-27 10:41:52 +010078.macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
79140:
80 \oldinstr
81141:
Borislav Petkovdbe40582015-04-04 15:34:43 +020082 .skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
83 (alt_max_short(new_len1, new_len2) - (old_len)),0x90
Borislav Petkov43321952014-12-27 10:41:52 +010084142:
85
86 .pushsection .altinstructions,"a"
87 altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f,142b-141b
88 altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f,142b-141b
89 .popsection
90
91 .pushsection .altinstr_replacement,"ax"
92143:
93 \newinstr1
94144:
95 \newinstr2
96145:
97 .popsection
Fenghua Yu9072d112011-05-17 15:29:13 -070098.endm
99
Adrian Bunk7e02cb92007-10-17 18:04:38 +0200100#endif /* __ASSEMBLY__ */
H. Peter Anvin76f30752012-09-21 12:43:09 -0700101
102#endif /* _ASM_X86_ALTERNATIVE_ASM_H */