1
2#ifndef _ASM_X86_SPECIAL_INSNS_H
3#define _ASM_X86_SPECIAL_INSNS_H
4
5
6#ifdef __KERNEL__
7
8#include <asm/nops.h>
9#include <asm/processor-flags.h>
10#include <linux/irqflags.h>
11#include <linux/jump_label.h>
12
13
14
15
16
17
18
19
20
21
22#define __FORCE_ORDER "m"(*(unsigned int *)0x1000UL)
23
24void native_write_cr0(unsigned long val);
25
26static inline unsigned long native_read_cr0(void)
27{
28 unsigned long val;
29 asm volatile("mov %%cr0,%0\n\t" : "=r" (val) : __FORCE_ORDER);
30 return val;
31}
32
33static __always_inline unsigned long native_read_cr2(void)
34{
35 unsigned long val;
36 asm volatile("mov %%cr2,%0\n\t" : "=r" (val) : __FORCE_ORDER);
37 return val;
38}
39
40static __always_inline void native_write_cr2(unsigned long val)
41{
42 asm volatile("mov %0,%%cr2": : "r" (val) : "memory");
43}
44
45static inline unsigned long __native_read_cr3(void)
46{
47 unsigned long val;
48 asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : __FORCE_ORDER);
49 return val;
50}
51
52static inline void native_write_cr3(unsigned long val)
53{
54 asm volatile("mov %0,%%cr3": : "r" (val) : "memory");
55}
56
57static inline unsigned long native_read_cr4(void)
58{
59 unsigned long val;
60#ifdef CONFIG_X86_32
61
62
63
64
65
66 asm volatile("1: mov %%cr4, %0\n"
67 "2:\n"
68 _ASM_EXTABLE(1b, 2b)
69 : "=r" (val) : "0" (0), __FORCE_ORDER);
70#else
71
72 asm volatile("mov %%cr4,%0\n\t" : "=r" (val) : __FORCE_ORDER);
73#endif
74 return val;
75}
76
77void native_write_cr4(unsigned long val);
78
79#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
80static inline u32 rdpkru(void)
81{
82 u32 ecx = 0;
83 u32 edx, pkru;
84
85
86
87
88
89 asm volatile(".byte 0x0f,0x01,0xee\n\t"
90 : "=a" (pkru), "=d" (edx)
91 : "c" (ecx));
92 return pkru;
93}
94
95static inline void wrpkru(u32 pkru)
96{
97 u32 ecx = 0, edx = 0;
98
99
100
101
102
103 asm volatile(".byte 0x0f,0x01,0xef\n\t"
104 : : "a" (pkru), "c"(ecx), "d"(edx));
105}
106
107#else
108static inline u32 rdpkru(void)
109{
110 return 0;
111}
112
113static inline void wrpkru(u32 pkru)
114{
115}
116#endif
117
118static inline void native_wbinvd(void)
119{
120 asm volatile("wbinvd": : :"memory");
121}
122
123extern asmlinkage void asm_load_gs_index(unsigned int selector);
124
125static inline void native_load_gs_index(unsigned int selector)
126{
127 unsigned long flags;
128
129 local_irq_save(flags);
130 asm_load_gs_index(selector);
131 local_irq_restore(flags);
132}
133
134static inline unsigned long __read_cr4(void)
135{
136 return native_read_cr4();
137}
138
139#ifdef CONFIG_PARAVIRT_XXL
140#include <asm/paravirt.h>
141#else
142
143static inline unsigned long read_cr0(void)
144{
145 return native_read_cr0();
146}
147
148static inline void write_cr0(unsigned long x)
149{
150 native_write_cr0(x);
151}
152
153static __always_inline unsigned long read_cr2(void)
154{
155 return native_read_cr2();
156}
157
158static __always_inline void write_cr2(unsigned long x)
159{
160 native_write_cr2(x);
161}
162
163
164
165
166
167static inline unsigned long __read_cr3(void)
168{
169 return __native_read_cr3();
170}
171
172static inline void write_cr3(unsigned long x)
173{
174 native_write_cr3(x);
175}
176
177static inline void __write_cr4(unsigned long x)
178{
179 native_write_cr4(x);
180}
181
182static inline void wbinvd(void)
183{
184 native_wbinvd();
185}
186
187#ifdef CONFIG_X86_64
188
189static inline void load_gs_index(unsigned int selector)
190{
191 native_load_gs_index(selector);
192}
193
194#endif
195
196#endif
197
198static inline void clflush(volatile void *__p)
199{
200 asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
201}
202
203static inline void clflushopt(volatile void *__p)
204{
205 alternative_io(".byte 0x3e; clflush %P0",
206 ".byte 0x66; clflush %P0",
207 X86_FEATURE_CLFLUSHOPT,
208 "+m" (*(volatile char __force *)__p));
209}
210
211static inline void clwb(volatile void *__p)
212{
213 volatile struct { char x[64]; } *p = __p;
214
215 asm volatile(ALTERNATIVE_2(
216 ".byte 0x3e; clflush (%[pax])",
217 ".byte 0x66; clflush (%[pax])",
218 X86_FEATURE_CLFLUSHOPT,
219 ".byte 0x66, 0x0f, 0xae, 0x30",
220 X86_FEATURE_CLWB)
221 : [p] "+m" (*p)
222 : [pax] "a" (p));
223}
224
225#define nop() asm volatile ("nop")
226
227static inline void serialize(void)
228{
229
230 asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
231}
232
233
234static inline void movdir64b(void __iomem *dst, const void *src)
235{
236 const struct { char _[64]; } *__src = src;
237 struct { char _[64]; } __iomem *__dst = dst;
238
239
240
241
242
243
244
245
246
247
248
249
250 asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
251 : "+m" (*__dst)
252 : "m" (*__src), "a" (__dst), "d" (__src));
253}
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274static inline int enqcmds(void __iomem *dst, const void *src)
275{
276 const struct { char _[64]; } *__src = src;
277 struct { char _[64]; } __iomem *__dst = dst;
278 bool zf;
279
280
281
282
283
284
285 asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
286 CC_SET(z)
287 : CC_OUT(z) (zf), "+m" (*__dst)
288 : "m" (*__src), "a" (__dst), "d" (__src));
289
290
291 if (zf)
292 return -EAGAIN;
293
294 return 0;
295}
296
297#endif
298
299#endif
300