blob: a09fe85c268e7e06943ec01f7c9d500457c9852a [file] [log] [blame]
Gerd Hoffmannd167a512006-06-26 13:56:16 +02001#ifndef _X86_64_ALTERNATIVE_H
2#define _X86_64_ALTERNATIVE_H
3
4#ifdef __KERNEL__
5
6#include <linux/types.h>
Rusty Russell139ec7c2006-12-07 02:14:08 +01007#include <linux/stddef.h>
Jan Beulich61171b82006-08-30 19:37:10 +02008#include <asm/cpufeature.h>
Gerd Hoffmannd167a512006-06-26 13:56:16 +02009
10struct alt_instr {
11 u8 *instr; /* original instruction */
12 u8 *replacement;
13 u8 cpuid; /* cpuid bit set for replacement */
14 u8 instrlen; /* length of original instruction */
15 u8 replacementlen; /* length of new instruction, <= instrlen */
16 u8 pad[5];
17};
18
Jeremy Fitzhardingec169859d2007-05-02 19:27:12 +020019extern void alternative_instructions(void);
Gerd Hoffmannd167a512006-06-26 13:56:16 +020020extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
21
22struct module;
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -070023
24#ifdef CONFIG_SMP
Gerd Hoffmannd167a512006-06-26 13:56:16 +020025extern void alternatives_smp_module_add(struct module *mod, char *name,
26 void *locks, void *locks_end,
27 void *text, void *text_end);
28extern void alternatives_smp_module_del(struct module *mod);
29extern void alternatives_smp_switch(int smp);
Gerd Hoffmann8ec4d412006-07-01 04:36:18 -070030#else
31static inline void alternatives_smp_module_add(struct module *mod, char *name,
32 void *locks, void *locks_end,
33 void *text, void *text_end) {}
34static inline void alternatives_smp_module_del(struct module *mod) {}
35static inline void alternatives_smp_switch(int smp) {}
36#endif
Gerd Hoffmannd167a512006-06-26 13:56:16 +020037
38#endif
39
40/*
41 * Alternative instructions for different CPU types or capabilities.
42 *
43 * This allows to use optimized instructions even on generic binary
44 * kernels.
45 *
46 * length of oldinstr must be longer or equal the length of newinstr
47 * It can be padded with nops as needed.
48 *
49 * For non barrier like inlines please define new variants
50 * without volatile and memory clobber.
51 */
52#define alternative(oldinstr, newinstr, feature) \
53 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
54 ".section .altinstructions,\"a\"\n" \
55 " .align 8\n" \
56 " .quad 661b\n" /* label */ \
57 " .quad 663f\n" /* new instruction */ \
58 " .byte %c0\n" /* feature bit */ \
59 " .byte 662b-661b\n" /* sourcelen */ \
60 " .byte 664f-663f\n" /* replacementlen */ \
61 ".previous\n" \
62 ".section .altinstr_replacement,\"ax\"\n" \
63 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
64 ".previous" :: "i" (feature) : "memory")
65
66/*
67 * Alternative inline assembly with input.
68 *
69 * Pecularities:
70 * No memory clobber here.
71 * Argument numbers start with 1.
72 * Best is to use constraints that are fixed size (like (%1) ... "r")
73 * If you use variable sized constraints like "m" or "g" in the
74 * replacement make sure to pad to the worst case length.
75 */
76#define alternative_input(oldinstr, newinstr, feature, input...) \
77 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
78 ".section .altinstructions,\"a\"\n" \
79 " .align 8\n" \
80 " .quad 661b\n" /* label */ \
81 " .quad 663f\n" /* new instruction */ \
82 " .byte %c0\n" /* feature bit */ \
83 " .byte 662b-661b\n" /* sourcelen */ \
84 " .byte 664f-663f\n" /* replacementlen */ \
85 ".previous\n" \
86 ".section .altinstr_replacement,\"ax\"\n" \
87 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
88 ".previous" :: "i" (feature), ##input)
89
90/* Like alternative_input, but with a single output argument */
91#define alternative_io(oldinstr, newinstr, feature, output, input...) \
92 asm volatile ("661:\n\t" oldinstr "\n662:\n" \
93 ".section .altinstructions,\"a\"\n" \
94 " .align 8\n" \
95 " .quad 661b\n" /* label */ \
96 " .quad 663f\n" /* new instruction */ \
97 " .byte %c[feat]\n" /* feature bit */ \
98 " .byte 662b-661b\n" /* sourcelen */ \
99 " .byte 664f-663f\n" /* replacementlen */ \
100 ".previous\n" \
101 ".section .altinstr_replacement,\"ax\"\n" \
102 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
103 ".previous" : output : [feat] "i" (feature), ##input)
104
105/*
106 * Alternative inline assembly for SMP.
107 *
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200108 * 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
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200127#define LOCK_PREFIX \
128 ".section .smp_locks,\"a\"\n" \
129 " .align 8\n" \
130 " .quad 661f\n" /* address */ \
131 ".previous\n" \
132 "661:\n\tlock; "
133
134#else /* ! CONFIG_SMP */
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200135#define LOCK_PREFIX ""
136#endif
137
Rusty Russell139ec7c2006-12-07 02:14:08 +0100138struct paravirt_patch;
139#ifdef CONFIG_PARAVIRT
140void apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end);
141#else
142static inline void
143apply_paravirt(struct paravirt_patch *start, struct paravirt_patch *end)
144{}
Jeremy Fitzhardinge441d40d2007-05-02 19:27:16 +0200145#define __parainstructions NULL
146#define __parainstructions_end NULL
Rusty Russell139ec7c2006-12-07 02:14:08 +0100147#endif
148
Gerd Hoffmannd167a512006-06-26 13:56:16 +0200149#endif /* _X86_64_ALTERNATIVE_H */