1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_SPECIAL_INSNS_H
3 #define _ASM_X86_SPECIAL_INSNS_H
9 #include <asm/processor-flags.h>
10 #include <linux/irqflags.h>
11 #include <linux/jump_label.h>
14 * The compiler should not reorder volatile asm statements with respect to each
15 * other: they should execute in program order. However GCC 4.9.x and 5.x have
16 * a bug (which was fixed in 8.1, 7.3 and 6.5) where they might reorder
17 * volatile asm. The write functions are not affected since they have memory
18 * clobbers preventing reordering. To prevent reads from being reordered with
19 * respect to writes, use a dummy memory operand.
22 #define __FORCE_ORDER "m"(*(unsigned int *)0x1000UL)
24 void native_write_cr0(unsigned long val
);
26 static inline unsigned long native_read_cr0(void)
29 asm volatile("mov %%cr0,%0\n\t" : "=r" (val
) : __FORCE_ORDER
);
33 static __always_inline
unsigned long native_read_cr2(void)
36 asm volatile("mov %%cr2,%0\n\t" : "=r" (val
) : __FORCE_ORDER
);
40 static __always_inline
void native_write_cr2(unsigned long val
)
42 asm volatile("mov %0,%%cr2": : "r" (val
) : "memory");
45 static inline unsigned long __native_read_cr3(void)
48 asm volatile("mov %%cr3,%0\n\t" : "=r" (val
) : __FORCE_ORDER
);
52 static inline void native_write_cr3(unsigned long val
)
54 asm volatile("mov %0,%%cr3": : "r" (val
) : "memory");
57 static inline unsigned long native_read_cr4(void)
62 * This could fault if CR4 does not exist. Non-existent CR4
63 * is functionally equivalent to CR4 == 0. Keep it simple and pretend
64 * that CR4 == 0 on CPUs that don't have CR4.
66 asm volatile("1: mov %%cr4, %0\n"
69 : "=r" (val
) : "0" (0), __FORCE_ORDER
);
71 /* CR4 always exists on x86_64. */
72 asm volatile("mov %%cr4,%0\n\t" : "=r" (val
) : __FORCE_ORDER
);
77 void native_write_cr4(unsigned long val
);
79 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
80 static inline u32
rdpkru(void)
86 * "rdpkru" instruction. Places PKRU contents in to EAX,
87 * clears EDX and requires that ecx=0.
89 asm volatile(".byte 0x0f,0x01,0xee\n\t"
90 : "=a" (pkru
), "=d" (edx
)
95 static inline void wrpkru(u32 pkru
)
100 * "wrpkru" instruction. Loads contents in EAX to PKRU,
101 * requires that ecx = edx = 0.
103 asm volatile(".byte 0x0f,0x01,0xef\n\t"
104 : : "a" (pkru
), "c"(ecx
), "d"(edx
));
107 static inline void __write_pkru(u32 pkru
)
110 * WRPKRU is relatively expensive compared to RDPKRU.
111 * Avoid WRPKRU when it would not change the value.
113 if (pkru
== rdpkru())
120 static inline u32
rdpkru(void)
125 static inline void __write_pkru(u32 pkru
)
130 static inline void native_wbinvd(void)
132 asm volatile("wbinvd": : :"memory");
135 extern asmlinkage
void asm_load_gs_index(unsigned int selector
);
137 static inline void native_load_gs_index(unsigned int selector
)
141 local_irq_save(flags
);
142 asm_load_gs_index(selector
);
143 local_irq_restore(flags
);
146 static inline unsigned long __read_cr4(void)
148 return native_read_cr4();
151 #ifdef CONFIG_PARAVIRT_XXL
152 #include <asm/paravirt.h>
155 static inline unsigned long read_cr0(void)
157 return native_read_cr0();
160 static inline void write_cr0(unsigned long x
)
165 static __always_inline
unsigned long read_cr2(void)
167 return native_read_cr2();
170 static __always_inline
void write_cr2(unsigned long x
)
176 * Careful! CR3 contains more than just an address. You probably want
177 * read_cr3_pa() instead.
179 static inline unsigned long __read_cr3(void)
181 return __native_read_cr3();
184 static inline void write_cr3(unsigned long x
)
189 static inline void __write_cr4(unsigned long x
)
194 static inline void wbinvd(void)
201 static inline void load_gs_index(unsigned int selector
)
203 native_load_gs_index(selector
);
208 #endif /* CONFIG_PARAVIRT_XXL */
210 static inline void clflush(volatile void *__p
)
212 asm volatile("clflush %0" : "+m" (*(volatile char __force
*)__p
));
215 static inline void clflushopt(volatile void *__p
)
217 alternative_io(".byte " __stringify(NOP_DS_PREFIX
) "; clflush %P0",
218 ".byte 0x66; clflush %P0",
219 X86_FEATURE_CLFLUSHOPT
,
220 "+m" (*(volatile char __force
*)__p
));
223 static inline void clwb(volatile void *__p
)
225 volatile struct { char x
[64]; } *p
= __p
;
227 asm volatile(ALTERNATIVE_2(
228 ".byte " __stringify(NOP_DS_PREFIX
) "; clflush (%[pax])",
229 ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */
230 X86_FEATURE_CLFLUSHOPT
,
231 ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
237 #define nop() asm volatile ("nop")
239 static inline void serialize(void)
241 /* Instruction opcode for SERIALIZE; supported in binutils >= 2.35. */
242 asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
245 /* The dst parameter must be 64-bytes aligned */
246 static inline void movdir64b(void *dst
, const void *src
)
248 const struct { char _
[64]; } *__src
= src
;
249 struct { char _
[64]; } *__dst
= dst
;
252 * MOVDIR64B %(rdx), rax.
254 * Both __src and __dst must be memory constraints in order to tell the
255 * compiler that no other memory accesses should be reordered around
258 * Also, both must be supplied as lvalues because this tells
259 * the compiler what the object is (its size) the instruction accesses.
260 * I.e., not the pointers but what they point to, thus the deref'ing '*'.
262 asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
264 : "m" (*__src
), "a" (__dst
), "d" (__src
));
268 * enqcmds - Enqueue a command in supervisor (CPL0) mode
269 * @dst: destination, in MMIO space (must be 512-bit aligned)
270 * @src: 512 bits memory operand
272 * The ENQCMDS instruction allows software to write a 512-bit command to
273 * a 512-bit-aligned special MMIO region that supports the instruction.
274 * A return status is loaded into the ZF flag in the RFLAGS register.
275 * ZF = 0 equates to success, and ZF = 1 indicates retry or error.
277 * This function issues the ENQCMDS instruction to submit data from
278 * kernel space to MMIO space, in a unit of 512 bits. Order of data access
279 * is not guaranteed, nor is a memory barrier performed afterwards. It
280 * returns 0 on success and -EAGAIN on failure.
282 * Warning: Do not use this helper unless your driver has checked that the
283 * ENQCMDS instruction is supported on the platform and the device accepts
286 static inline int enqcmds(void __iomem
*dst
, const void *src
)
288 const struct { char _
[64]; } *__src
= src
;
289 struct { char _
[64]; } *__dst
= dst
;
293 * ENQCMDS %(rdx), rax
295 * See movdir64b()'s comment on operand specification.
297 asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
299 : CC_OUT(z
) (zf
), "+m" (*__dst
)
300 : "m" (*__src
), "a" (__dst
), "d" (__src
));
302 /* Submission failure is indicated via EFLAGS.ZF=1 */
309 #endif /* __KERNEL__ */
311 #endif /* _ASM_X86_SPECIAL_INSNS_H */