x86: Fix LOCK_PREFIX_HERE for uniprocessor build
[deliverable/linux.git] / arch / x86 / include / asm / alternative.h
CommitLineData
1965aae3
PA
1#ifndef _ASM_X86_ALTERNATIVE_H
2#define _ASM_X86_ALTERNATIVE_H
6b592570
PA
3
4#include <linux/types.h>
5#include <linux/stddef.h>
edc953fa 6#include <linux/stringify.h>
6b592570
PA
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
b3ac891b 31#define LOCK_PREFIX_HERE \
6b592570
PA
32 ".section .smp_locks,\"a\"\n" \
33 _ASM_ALIGN "\n" \
b3ac891b 34 _ASM_PTR "671f\n" /* address */ \
6b592570 35 ".previous\n" \
b3ac891b
LB
36 "671:"
37
38#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
6b592570
PA
39
40#else /* ! CONFIG_SMP */
b701a47b 41#define LOCK_PREFIX_HERE ""
6b592570
PA
42#define LOCK_PREFIX ""
43#endif
44
45/* This must be included *after* the definition of LOCK_PREFIX */
46#include <asm/cpufeature.h>
47
48struct alt_instr {
49 u8 *instr; /* original instruction */
50 u8 *replacement;
51 u8 cpuid; /* cpuid bit set for replacement */
52 u8 instrlen; /* length of original instruction */
53 u8 replacementlen; /* length of new instruction, <= instrlen */
54 u8 pad1;
55#ifdef CONFIG_X86_64
56 u32 pad2;
57#endif
58};
59
60extern void alternative_instructions(void);
61extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
62
63struct module;
64
65#ifdef CONFIG_SMP
66extern void alternatives_smp_module_add(struct module *mod, char *name,
67 void *locks, void *locks_end,
68 void *text, void *text_end);
69extern void alternatives_smp_module_del(struct module *mod);
70extern void alternatives_smp_switch(int smp);
71#else
72static inline void alternatives_smp_module_add(struct module *mod, char *name,
2ac1ea7c
JP
73 void *locks, void *locks_end,
74 void *text, void *text_end) {}
6b592570
PA
75static inline void alternatives_smp_module_del(struct module *mod) {}
76static inline void alternatives_smp_switch(int smp) {}
77#endif /* CONFIG_SMP */
78
edc953fa
MD
79/* alternative assembly primitive: */
80#define ALTERNATIVE(oldinstr, newinstr, feature) \
81 \
82 "661:\n\t" oldinstr "\n662:\n" \
83 ".section .altinstructions,\"a\"\n" \
84 _ASM_ALIGN "\n" \
85 _ASM_PTR "661b\n" /* label */ \
86 _ASM_PTR "663f\n" /* new instruction */ \
87 " .byte " __stringify(feature) "\n" /* feature bit */ \
88 " .byte 662b-661b\n" /* sourcelen */ \
89 " .byte 664f-663f\n" /* replacementlen */ \
01be50a3 90 " .byte 0xff + (664f-663f) - (662b-661b)\n" /* rlen <= slen */ \
edc953fa
MD
91 ".previous\n" \
92 ".section .altinstr_replacement, \"ax\"\n" \
93 "663:\n\t" newinstr "\n664:\n" /* replacement */ \
94 ".previous"
95
6b592570
PA
96/*
97 * Alternative instructions for different CPU types or capabilities.
98 *
99 * This allows to use optimized instructions even on generic binary
100 * kernels.
101 *
102 * length of oldinstr must be longer or equal the length of newinstr
103 * It can be padded with nops as needed.
104 *
105 * For non barrier like inlines please define new variants
106 * without volatile and memory clobber.
107 */
108#define alternative(oldinstr, newinstr, feature) \
edc953fa 109 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
6b592570
PA
110
111/*
112 * Alternative inline assembly with input.
113 *
114 * Pecularities:
115 * No memory clobber here.
116 * Argument numbers start with 1.
117 * Best is to use constraints that are fixed size (like (%1) ... "r")
118 * If you use variable sized constraints like "m" or "g" in the
119 * replacement make sure to pad to the worst case length.
edc953fa 120 * Leaving an unused argument 0 to keep API compatibility.
6b592570
PA
121 */
122#define alternative_input(oldinstr, newinstr, feature, input...) \
edc953fa
MD
123 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
124 : : "i" (0), ## input)
6b592570
PA
125
126/* Like alternative_input, but with a single output argument */
127#define alternative_io(oldinstr, newinstr, feature, output, input...) \
edc953fa
MD
128 asm volatile (ALTERNATIVE(oldinstr, newinstr, feature) \
129 : output : "i" (0), ## input)
6b592570 130
1b1d9258
JB
131/* Like alternative_io, but for replacing a direct call with another one. */
132#define alternative_call(oldfunc, newfunc, feature, output, input...) \
133 asm volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
134 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
135
6b592570
PA
136/*
137 * use this macro(s) if you need more than one output parameter
138 * in alternative_io
139 */
1b1d9258 140#define ASM_OUTPUT2(a...) a
6b592570
PA
141
142struct paravirt_patch_site;
143#ifdef CONFIG_PARAVIRT
144void apply_paravirt(struct paravirt_patch_site *start,
145 struct paravirt_patch_site *end);
96a388de 146#else
2ac1ea7c
JP
147static inline void apply_paravirt(struct paravirt_patch_site *start,
148 struct paravirt_patch_site *end)
6b592570
PA
149{}
150#define __parainstructions NULL
151#define __parainstructions_end NULL
96a388de 152#endif
6b592570 153
e587cadd
MD
154/*
155 * Clear and restore the kernel write-protection flag on the local CPU.
156 * Allows the kernel to edit read-only pages.
157 * Side-effect: any interrupt handler running between save and restore will have
158 * the ability to write to read-only pages.
159 *
160 * Warning:
161 * Code patching in the UP case is safe if NMIs and MCE handlers are stopped and
162 * no thread can be preempted in the instructions being modified (no iret to an
163 * invalid instruction possible) or if the instructions are changed from a
164 * consistent state to another consistent state atomically.
165 * More care must be taken when modifying code in the SMP case because of
166 * Intel's errata.
167 * On the local CPU you need to be protected again NMI or MCE handlers seeing an
168 * inconsistent instruction while you patch.
e587cadd 169 */
e587cadd 170extern void *text_poke(void *addr, const void *opcode, size_t len);
6b592570 171
1965aae3 172#endif /* _ASM_X86_ALTERNATIVE_H */
This page took 0.217786 seconds and 5 git commands to generate.