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