Merge git://git.infradead.org/hdrinstall-2.6
[safe/jmp/linux-2.6] / include / asm-x86_64 / alternative.h
1 #ifndef _X86_64_ALTERNATIVE_H
2 #define _X86_64_ALTERNATIVE_H
3
4 #ifdef __KERNEL__
5
6 #include <linux/types.h>
7
8 struct alt_instr {
9         u8 *instr;              /* original instruction */
10         u8 *replacement;
11         u8  cpuid;              /* cpuid bit set for replacement */
12         u8  instrlen;           /* length of original instruction */
13         u8  replacementlen;     /* length of new instruction, <= instrlen */
14         u8  pad[5];
15 };
16
17 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
18
19 struct module;
20
21 #ifdef CONFIG_SMP
22 extern void alternatives_smp_module_add(struct module *mod, char *name,
23                                         void *locks, void *locks_end,
24                                         void *text, void *text_end);
25 extern void alternatives_smp_module_del(struct module *mod);
26 extern void alternatives_smp_switch(int smp);
27 #else
28 static inline void alternatives_smp_module_add(struct module *mod, char *name,
29                                         void *locks, void *locks_end,
30                                         void *text, void *text_end) {}
31 static inline void alternatives_smp_module_del(struct module *mod) {}
32 static inline void alternatives_smp_switch(int smp) {}
33 #endif
34
35 #endif
36
37 /*
38  * Alternative instructions for different CPU types or capabilities.
39  *
40  * This allows to use optimized instructions even on generic binary
41  * kernels.
42  *
43  * length of oldinstr must be longer or equal the length of newinstr
44  * It can be padded with nops as needed.
45  *
46  * For non barrier like inlines please define new variants
47  * without volatile and memory clobber.
48  */
49 #define alternative(oldinstr, newinstr, feature)        \
50         asm volatile ("661:\n\t" oldinstr "\n662:\n"                 \
51                       ".section .altinstructions,\"a\"\n"            \
52                       "  .align 8\n"                                   \
53                       "  .quad 661b\n"            /* label */          \
54                       "  .quad 663f\n"            /* new instruction */ \
55                       "  .byte %c0\n"             /* feature bit */    \
56                       "  .byte 662b-661b\n"       /* sourcelen */      \
57                       "  .byte 664f-663f\n"       /* replacementlen */ \
58                       ".previous\n"                                     \
59                       ".section .altinstr_replacement,\"ax\"\n"         \
60                       "663:\n\t" newinstr "\n664:\n"   /* replacement */ \
61                       ".previous" :: "i" (feature) : "memory")
62
63 /*
64  * Alternative inline assembly with input.
65  *
66  * Pecularities:
67  * No memory clobber here.
68  * Argument numbers start with 1.
69  * Best is to use constraints that are fixed size (like (%1) ... "r")
70  * If you use variable sized constraints like "m" or "g" in the
71  * replacement make sure to pad to the worst case length.
72  */
73 #define alternative_input(oldinstr, newinstr, feature, input...)        \
74         asm volatile ("661:\n\t" oldinstr "\n662:\n"                    \
75                       ".section .altinstructions,\"a\"\n"               \
76                       "  .align 8\n"                                    \
77                       "  .quad 661b\n"            /* label */           \
78                       "  .quad 663f\n"            /* new instruction */ \
79                       "  .byte %c0\n"             /* feature bit */     \
80                       "  .byte 662b-661b\n"       /* sourcelen */       \
81                       "  .byte 664f-663f\n"       /* replacementlen */  \
82                       ".previous\n"                                     \
83                       ".section .altinstr_replacement,\"ax\"\n"         \
84                       "663:\n\t" newinstr "\n664:\n"   /* replacement */ \
85                       ".previous" :: "i" (feature), ##input)
86
87 /* Like alternative_input, but with a single output argument */
88 #define alternative_io(oldinstr, newinstr, feature, output, input...) \
89         asm volatile ("661:\n\t" oldinstr "\n662:\n"                    \
90                       ".section .altinstructions,\"a\"\n"               \
91                       "  .align 8\n"                                    \
92                       "  .quad 661b\n"            /* label */           \
93                       "  .quad 663f\n"            /* new instruction */ \
94                       "  .byte %c[feat]\n"        /* feature bit */     \
95                       "  .byte 662b-661b\n"       /* sourcelen */       \
96                       "  .byte 664f-663f\n"       /* replacementlen */  \
97                       ".previous\n"                                     \
98                       ".section .altinstr_replacement,\"ax\"\n"         \
99                       "663:\n\t" newinstr "\n664:\n"   /* replacement */ \
100                       ".previous" : output : [feat] "i" (feature), ##input)
101
102 /*
103  * Alternative inline assembly for SMP.
104  *
105  * alternative_smp() takes two versions (SMP first, UP second) and is
106  * for more complex stuff such as spinlocks.
107  *
108  * The LOCK_PREFIX macro defined here replaces the LOCK and
109  * LOCK_PREFIX macros used everywhere in the source tree.
110  *
111  * SMP alternatives use the same data structures as the other
112  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
113  * UP system running a SMP kernel.  The existing apply_alternatives()
114  * works fine for patching a SMP kernel for UP.
115  *
116  * The SMP alternative tables can be kept after boot and contain both
117  * UP and SMP versions of the instructions to allow switching back to
118  * SMP at runtime, when hotplugging in a new CPU, which is especially
119  * useful in virtualized environments.
120  *
121  * The very common lock prefix is handled as special case in a
122  * separate table which is a pure address list without replacement ptr
123  * and size information.  That keeps the table sizes small.
124  */
125
126 #ifdef CONFIG_SMP
127 #define alternative_smp(smpinstr, upinstr, args...)                     \
128         asm volatile ("661:\n\t" smpinstr "\n662:\n"                    \
129                       ".section .smp_altinstructions,\"a\"\n"           \
130                       "  .align 8\n"                                    \
131                       "  .quad 661b\n"            /* label */           \
132                       "  .quad 663f\n"            /* new instruction */ \
133                       "  .byte 0x66\n"            /* X86_FEATURE_UP */  \
134                       "  .byte 662b-661b\n"       /* sourcelen */       \
135                       "  .byte 664f-663f\n"       /* replacementlen */  \
136                       ".previous\n"                                     \
137                       ".section .smp_altinstr_replacement,\"awx\"\n"    \
138                       "663:\n\t" upinstr "\n"     /* replacement */     \
139                       "664:\n\t.fill 662b-661b,1,0x42\n" /* space for original */ \
140                       ".previous" : args)
141
142 #define LOCK_PREFIX \
143                 ".section .smp_locks,\"a\"\n"   \
144                 "  .align 8\n"                  \
145                 "  .quad 661f\n" /* address */  \
146                 ".previous\n"                   \
147                 "661:\n\tlock; "
148
149 #else /* ! CONFIG_SMP */
150 #define alternative_smp(smpinstr, upinstr, args...) \
151         asm volatile (upinstr : args)
152 #define LOCK_PREFIX ""
153 #endif
154
155 #endif /* _X86_64_ALTERNATIVE_H */