1
2#ifndef _ASM_X86_ALTERNATIVE_H
3#define _ASM_X86_ALTERNATIVE_H
4
5#ifndef __ASSEMBLY__
6
7#include <linux/types.h>
8#include <linux/stddef.h>
9#include <linux/stringify.h>
10#include <asm/asm.h>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#ifdef CONFIG_SMP
34#define LOCK_PREFIX_HERE \
35 ".pushsection .smp_locks,\"a\"\n" \
36 ".balign 4\n" \
37 ".long 671f - .\n" \
38 ".popsection\n" \
39 "671:"
40
41#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
42
43#else
44#define LOCK_PREFIX_HERE ""
45#define LOCK_PREFIX ""
46#endif
47
48struct alt_instr {
49 s32 instr_offset;
50 s32 repl_offset;
51 u16 cpuid;
52 u8 instrlen;
53 u8 replacementlen;
54 u8 padlen;
55} __packed;
56
57
58
59
60
61extern int alternatives_patched;
62
63extern void alternative_instructions(void);
64extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
65
66struct module;
67
68#ifdef CONFIG_SMP
69extern void alternatives_smp_module_add(struct module *mod, char *name,
70 void *locks, void *locks_end,
71 void *text, void *text_end);
72extern void alternatives_smp_module_del(struct module *mod);
73extern void alternatives_enable_smp(void);
74extern int alternatives_text_reserved(void *start, void *end);
75extern bool skip_smp_alternatives;
76#else
77static inline void alternatives_smp_module_add(struct module *mod, char *name,
78 void *locks, void *locks_end,
79 void *text, void *text_end) {}
80static inline void alternatives_smp_module_del(struct module *mod) {}
81static inline void alternatives_enable_smp(void) {}
82static inline int alternatives_text_reserved(void *start, void *end)
83{
84 return 0;
85}
86#endif
87
88#define b_replacement(num) "664"#num
89#define e_replacement(num) "665"#num
90
91#define alt_end_marker "663"
92#define alt_slen "662b-661b"
93#define alt_pad_len alt_end_marker"b-662b"
94#define alt_total_slen alt_end_marker"b-661b"
95#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
96
97#define __OLDINSTR(oldinstr, num) \
98 "661:\n\t" oldinstr "\n662:\n" \
99 ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
100 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
101
102#define OLDINSTR(oldinstr, num) \
103 __OLDINSTR(oldinstr, num) \
104 alt_end_marker ":\n"
105
106
107
108
109
110
111
112#define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
113
114
115
116
117
118#define OLDINSTR_2(oldinstr, num1, num2) \
119 "661:\n\t" oldinstr "\n662:\n" \
120 ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
121 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
122 alt_end_marker ":\n"
123
124#define ALTINSTR_ENTRY(feature, num) \
125 " .long 661b - .\n" \
126 " .long " b_replacement(num)"f - .\n" \
127 " .word " __stringify(feature) "\n" \
128 " .byte " alt_total_slen "\n" \
129 " .byte " alt_rlen(num) "\n" \
130 " .byte " alt_pad_len "\n"
131
132#define ALTINSTR_REPLACEMENT(newinstr, feature, num) \
133 b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
134
135
136#define ALTERNATIVE(oldinstr, newinstr, feature) \
137 OLDINSTR(oldinstr, 1) \
138 ".pushsection .altinstructions,\"a\"\n" \
139 ALTINSTR_ENTRY(feature, 1) \
140 ".popsection\n" \
141 ".pushsection .altinstr_replacement, \"ax\"\n" \
142 ALTINSTR_REPLACEMENT(newinstr, feature, 1) \
143 ".popsection\n"
144
145#define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
146 OLDINSTR_2(oldinstr, 1, 2) \
147 ".pushsection .altinstructions,\"a\"\n" \
148 ALTINSTR_ENTRY(feature1, 1) \
149 ALTINSTR_ENTRY(feature2, 2) \
150 ".popsection\n" \
151 ".pushsection .altinstr_replacement, \"ax\"\n" \
152 ALTINSTR_REPLACEMENT(newinstr1, feature1, 1) \
153 ALTINSTR_REPLACEMENT(newinstr2, feature2, 2) \
154 ".popsection\n"
155
156
157
158
159
160
161
162
163
164
165
166
167
168#define alternative(oldinstr, newinstr, feature) \
169 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
170
171#define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
172 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
173
174
175
176
177
178
179
180
181
182
183
184
185#define alternative_input(oldinstr, newinstr, feature, input...) \
186 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
187 : : "i" (0), ## input)
188
189
190
191
192
193
194
195
196
197#define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2, \
198 feature2, input...) \
199 asm volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, \
200 newinstr2, feature2) \
201 : : "i" (0), ## input)
202
203
204#define alternative_io(oldinstr, newinstr, feature, output, input...) \
205 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
206 : output : "i" (0), ## input)
207
208
209#define alternative_call(oldfunc, newfunc, feature, output, input...) \
210 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
211 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
212
213
214
215
216
217
218
219#define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2, \
220 output, input...) \
221 asm volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
222 "call %P[new2]", feature2) \
223 : output, ASM_CALL_CONSTRAINT \
224 : [old] "i" (oldfunc), [new1] "i" (newfunc1), \
225 [new2] "i" (newfunc2), ## input)
226
227
228
229
230
231#define ASM_OUTPUT2(a...) a
232
233
234
235
236
237#define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
238
239#endif
240
241#endif
242