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