pandora: defconfig: update
[pandora-kernel.git] / arch / x86 / include / asm / alternative.h
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  * Alternative inline assembly for SMP.
13  *
14  * The LOCK_PREFIX macro defined here replaces the LOCK and
15  * LOCK_PREFIX macros used everywhere in the source tree.
16  *
17  * SMP alternatives use the same data structures as the other
18  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
19  * UP system running a SMP kernel.  The existing apply_alternatives()
20  * works fine for patching a SMP kernel for UP.
21  *
22  * The SMP alternative tables can be kept after boot and contain both
23  * UP and SMP versions of the instructions to allow switching back to
24  * SMP at runtime, when hotplugging in a new CPU, which is especially
25  * useful in virtualized environments.
26  *
27  * The very common lock prefix is handled as special case in a
28  * separate table which is a pure address list without replacement ptr
29  * and size information.  That keeps the table sizes small.
30  */
31
32 #ifdef CONFIG_SMP
33 #define LOCK_PREFIX_HERE \
34                 ".section .smp_locks,\"a\"\n"   \
35                 ".balign 4\n"                   \
36                 ".long 671f - .\n" /* offset */ \
37                 ".previous\n"                   \
38                 "671:"
39
40 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
41
42 #else /* ! CONFIG_SMP */
43 #define LOCK_PREFIX_HERE ""
44 #define LOCK_PREFIX ""
45 #endif
46
47 struct alt_instr {
48         s32 instr_offset;       /* original instruction */
49         s32 repl_offset;        /* offset to replacement instruction */
50         u16 cpuid;              /* cpuid bit set for replacement */
51         u8  instrlen;           /* length of original instruction */
52         u8  replacementlen;     /* length of new instruction */
53         u8  padlen;             /* length of build-time padding */
54 } __packed;
55
56 extern void alternative_instructions(void);
57 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
58
59 struct module;
60
61 #ifdef CONFIG_SMP
62 extern void alternatives_smp_module_add(struct module *mod, char *name,
63                                         void *locks, void *locks_end,
64                                         void *text, void *text_end);
65 extern void alternatives_smp_module_del(struct module *mod);
66 extern void alternatives_enable_smp(void);
67 extern int alternatives_text_reserved(void *start, void *end);
68 extern bool skip_smp_alternatives;
69 #else
70 static inline void alternatives_smp_module_add(struct module *mod, char *name,
71                                                void *locks, void *locks_end,
72                                                void *text, void *text_end) {}
73 static inline void alternatives_smp_module_del(struct module *mod) {}
74 static inline void alternatives_enable_smp(void) {}
75 static inline int alternatives_text_reserved(void *start, void *end)
76 {
77         return 0;
78 }
79 #endif  /* CONFIG_SMP */
80
81 #define b_replacement(num)      "664"#num
82 #define e_replacement(num)      "665"#num
83
84 #define alt_end_marker          "663"
85 #define alt_slen                "662b-661b"
86 #define alt_pad_len             alt_end_marker"b-662b"
87 #define alt_total_slen          alt_end_marker"b-661b"
88 #define alt_rlen(num)           e_replacement(num)"f-"b_replacement(num)"f"
89
90 #define __OLDINSTR(oldinstr, num)                                       \
91         "661:\n\t" oldinstr "\n662:\n"                                  \
92         ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * "          \
93                 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"
94
95 #define OLDINSTR(oldinstr, num)                                         \
96         __OLDINSTR(oldinstr, num)                                       \
97         alt_end_marker ":\n"
98
99 /*
100  * max without conditionals. Idea adapted from:
101  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
102  *
103  * The additional "-" is needed because gas works with s32s.
104  */
105 #define alt_max_short(a, b)     "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") - (" b ")))))"
106
107 /*
108  * Pad the second replacement alternative with additional NOPs if it is
109  * additionally longer than the first replacement alternative.
110  */
111 #define OLDINSTR_2(oldinstr, num1, num2) \
112         "661:\n\t" oldinstr "\n662:\n"                                                          \
113         ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * "  \
114                 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n"  \
115         alt_end_marker ":\n"
116
117 #define ALTINSTR_ENTRY(feature, num)                                          \
118         " .long 661b - .\n"                             /* label           */ \
119         " .long " b_replacement(num)"f - .\n"           /* new instruction */ \
120         " .word " __stringify(feature) "\n"             /* feature bit     */ \
121         " .byte " alt_total_slen "\n"                   /* source len      */ \
122         " .byte " alt_rlen(num) "\n"                    /* replacement len */ \
123         " .byte " alt_pad_len "\n"                      /* pad len */
124
125 #define ALTINSTR_REPLACEMENT(newinstr, feature, num)    /* replacement */     \
126         b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n\t"
127
128 /* alternative assembly primitive: */
129 #define ALTERNATIVE(oldinstr, newinstr, feature)                        \
130         OLDINSTR(oldinstr, 1)                                           \
131         ".pushsection .altinstructions,\"a\"\n"                         \
132         ALTINSTR_ENTRY(feature, 1)                                      \
133         ".popsection\n"                                                 \
134         ".pushsection .altinstr_replacement, \"ax\"\n"                  \
135         ALTINSTR_REPLACEMENT(newinstr, feature, 1)                      \
136         ".popsection\n"
137
138 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
139         OLDINSTR_2(oldinstr, 1, 2)                                      \
140         ".pushsection .altinstructions,\"a\"\n"                         \
141         ALTINSTR_ENTRY(feature1, 1)                                     \
142         ALTINSTR_ENTRY(feature2, 2)                                     \
143         ".popsection\n"                                                 \
144         ".pushsection .altinstr_replacement, \"ax\"\n"                  \
145         ALTINSTR_REPLACEMENT(newinstr1, feature1, 1)                    \
146         ALTINSTR_REPLACEMENT(newinstr2, feature2, 2)                    \
147         ".popsection\n"
148
149 /*
150  * This must be included *after* the definition of ALTERNATIVE due to
151  * <asm/arch_hweight.h>
152  */
153 #include <asm/cpufeature.h>
154
155 /*
156  * Alternative instructions for different CPU types or capabilities.
157  *
158  * This allows to use optimized instructions even on generic binary
159  * kernels.
160  *
161  * length of oldinstr must be longer or equal the length of newinstr
162  * It can be padded with nops as needed.
163  *
164  * For non barrier like inlines please define new variants
165  * without volatile and memory clobber.
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  * Alternative inline assembly with input.
175  *
176  * Pecularities:
177  * No memory clobber here.
178  * Argument numbers start with 1.
179  * Best is to use constraints that are fixed size (like (%1) ... "r")
180  * If you use variable sized constraints like "m" or "g" in the
181  * replacement make sure to pad to the worst case length.
182  * Leaving an unused argument 0 to keep API compatibility.
183  */
184 #define alternative_input(oldinstr, newinstr, feature, input...)        \
185         asm volatile (ALTERNATIVE(oldinstr, newinstr, feature)          \
186                 : : "i" (0), ## input)
187
188 /* Like alternative_input, but with a single output argument */
189 #define alternative_io(oldinstr, newinstr, feature, output, input...)   \
190         asm volatile (ALTERNATIVE(oldinstr, newinstr, feature)          \
191                 : output : "i" (0), ## input)
192
193 /* Like alternative_io, but for replacing a direct call with another one. */
194 #define alternative_call(oldfunc, newfunc, feature, output, input...)   \
195         asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
196                 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
197
198 /*
199  * use this macro(s) if you need more than one output parameter
200  * in alternative_io
201  */
202 #define ASM_OUTPUT2(a...) a
203
204 struct paravirt_patch_site;
205 #ifdef CONFIG_PARAVIRT
206 void apply_paravirt(struct paravirt_patch_site *start,
207                     struct paravirt_patch_site *end);
208 #else
209 static inline void apply_paravirt(struct paravirt_patch_site *start,
210                                   struct paravirt_patch_site *end)
211 {}
212 #define __parainstructions      NULL
213 #define __parainstructions_end  NULL
214 #endif
215
216 extern void *text_poke_early(void *addr, const void *opcode, size_t len);
217
218 /*
219  * Clear and restore the kernel write-protection flag on the local CPU.
220  * Allows the kernel to edit read-only pages.
221  * Side-effect: any interrupt handler running between save and restore will have
222  * the ability to write to read-only pages.
223  *
224  * Warning:
225  * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
226  * no thread can be preempted in the instructions being modified (no iret to an
227  * invalid instruction possible) or if the instructions are changed from a
228  * consistent state to another consistent state atomically.
229  * More care must be taken when modifying code in the SMP case because of
230  * Intel's errata. text_poke_smp() takes care that errata, but still
231  * doesn't support NMI/MCE handler code modifying.
232  * On the local CPU you need to be protected again NMI or MCE handlers seeing an
233  * inconsistent instruction while you patch.
234  */
235 struct text_poke_param {
236         void *addr;
237         const void *opcode;
238         size_t len;
239 };
240
241 extern void *text_poke(void *addr, const void *opcode, size_t len);
242 extern void *text_poke_smp(void *addr, const void *opcode, size_t len);
243 extern void text_poke_smp_batch(struct text_poke_param *params, int n);
244
245 #endif /* __ASSEMBLY__ */
246
247 #endif /* _ASM_X86_ALTERNATIVE_H */