1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2f05e798aSDavid Howells #ifndef _ASM_X86_SPECIAL_INSNS_H
3f05e798aSDavid Howells #define _ASM_X86_SPECIAL_INSNS_H
4f05e798aSDavid Howells
5f05e798aSDavid Howells
6f05e798aSDavid Howells #ifdef __KERNEL__
7f05e798aSDavid Howells
8719d359dSRoss Zwisler #include <asm/nops.h>
9873d50d5SKees Cook #include <asm/processor-flags.h>
10410367e3SThomas Gleixner #include <linux/irqflags.h>
11873d50d5SKees Cook #include <linux/jump_label.h>
12719d359dSRoss Zwisler
13f05e798aSDavid Howells /*
14aa5cacdcSArvind Sankar * The compiler should not reorder volatile asm statements with respect to each
15aa5cacdcSArvind Sankar * other: they should execute in program order. However GCC 4.9.x and 5.x have
16aa5cacdcSArvind Sankar * a bug (which was fixed in 8.1, 7.3 and 6.5) where they might reorder
17aa5cacdcSArvind Sankar * volatile asm. The write functions are not affected since they have memory
18aa5cacdcSArvind Sankar * clobbers preventing reordering. To prevent reads from being reordered with
19aa5cacdcSArvind Sankar * respect to writes, use a dummy memory operand.
20f05e798aSDavid Howells */
21aa5cacdcSArvind Sankar
22aa5cacdcSArvind Sankar #define __FORCE_ORDER "m"(*(unsigned int *)0x1000UL)
23f05e798aSDavid Howells
247652ac92SThomas Gleixner void native_write_cr0(unsigned long val);
25873d50d5SKees Cook
native_read_cr0(void)26f05e798aSDavid Howells static inline unsigned long native_read_cr0(void)
27f05e798aSDavid Howells {
28f05e798aSDavid Howells unsigned long val;
29aa5cacdcSArvind Sankar asm volatile("mov %%cr0,%0\n\t" : "=r" (val) : __FORCE_ORDER);
30f05e798aSDavid Howells return val;
31f05e798aSDavid Howells }
32f05e798aSDavid Howells
native_read_cr2(void)332823e83aSPeter Zijlstra static __always_inline unsigned long native_read_cr2(void)
34f05e798aSDavid Howells {
35f05e798aSDavid Howells unsigned long val;
36aa5cacdcSArvind Sankar asm volatile("mov %%cr2,%0\n\t" : "=r" (val) : __FORCE_ORDER);
37f05e798aSDavid Howells return val;
38f05e798aSDavid Howells }
39f05e798aSDavid Howells
native_write_cr2(unsigned long val)402823e83aSPeter Zijlstra static __always_inline void native_write_cr2(unsigned long val)
41f05e798aSDavid Howells {
42aa5cacdcSArvind Sankar asm volatile("mov %0,%%cr2": : "r" (val) : "memory");
43f05e798aSDavid Howells }
44f05e798aSDavid Howells
__native_read_cr3(void)456c690ee1SAndy Lutomirski static inline unsigned long __native_read_cr3(void)
46f05e798aSDavid Howells {
47f05e798aSDavid Howells unsigned long val;
48aa5cacdcSArvind Sankar asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : __FORCE_ORDER);
49f05e798aSDavid Howells return val;
50f05e798aSDavid Howells }
51f05e798aSDavid Howells
native_write_cr3(unsigned long val)52f05e798aSDavid Howells static inline void native_write_cr3(unsigned long val)
53f05e798aSDavid Howells {
54aa5cacdcSArvind Sankar asm volatile("mov %0,%%cr3": : "r" (val) : "memory");
55f05e798aSDavid Howells }
56f05e798aSDavid Howells
native_read_cr4(void)57f05e798aSDavid Howells static inline unsigned long native_read_cr4(void)
58f05e798aSDavid Howells {
59f05e798aSDavid Howells unsigned long val;
60f05e798aSDavid Howells #ifdef CONFIG_X86_32
611ef55be1SAndy Lutomirski /*
621ef55be1SAndy Lutomirski * This could fault if CR4 does not exist. Non-existent CR4
631ef55be1SAndy Lutomirski * is functionally equivalent to CR4 == 0. Keep it simple and pretend
641ef55be1SAndy Lutomirski * that CR4 == 0 on CPUs that don't have CR4.
651ef55be1SAndy Lutomirski */
66f05e798aSDavid Howells asm volatile("1: mov %%cr4, %0\n"
67f05e798aSDavid Howells "2:\n"
68f05e798aSDavid Howells _ASM_EXTABLE(1b, 2b)
69aa5cacdcSArvind Sankar : "=r" (val) : "0" (0), __FORCE_ORDER);
70f05e798aSDavid Howells #else
711ef55be1SAndy Lutomirski /* CR4 always exists on x86_64. */
72aa5cacdcSArvind Sankar asm volatile("mov %%cr4,%0\n\t" : "=r" (val) : __FORCE_ORDER);
73f05e798aSDavid Howells #endif
74f05e798aSDavid Howells return val;
75f05e798aSDavid Howells }
76f05e798aSDavid Howells
777652ac92SThomas Gleixner void native_write_cr4(unsigned long val);
78f05e798aSDavid Howells
79a927cb83SDave Hansen #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
rdpkru(void)80c806e887SSebastian Andrzej Siewior static inline u32 rdpkru(void)
81a927cb83SDave Hansen {
82a927cb83SDave Hansen u32 ecx = 0;
83a927cb83SDave Hansen u32 edx, pkru;
84a927cb83SDave Hansen
85a927cb83SDave Hansen /*
86a927cb83SDave Hansen * "rdpkru" instruction. Places PKRU contents in to EAX,
87a927cb83SDave Hansen * clears EDX and requires that ecx=0.
88a927cb83SDave Hansen */
89a927cb83SDave Hansen asm volatile(".byte 0x0f,0x01,0xee\n\t"
90a927cb83SDave Hansen : "=a" (pkru), "=d" (edx)
91a927cb83SDave Hansen : "c" (ecx));
92a927cb83SDave Hansen return pkru;
93a927cb83SDave Hansen }
949e90199cSXiao Guangrong
wrpkru(u32 pkru)95c806e887SSebastian Andrzej Siewior static inline void wrpkru(u32 pkru)
969e90199cSXiao Guangrong {
979e90199cSXiao Guangrong u32 ecx = 0, edx = 0;
989e90199cSXiao Guangrong
999e90199cSXiao Guangrong /*
1009e90199cSXiao Guangrong * "wrpkru" instruction. Loads contents in EAX to PKRU,
1019e90199cSXiao Guangrong * requires that ecx = edx = 0.
1029e90199cSXiao Guangrong */
1039e90199cSXiao Guangrong asm volatile(".byte 0x0f,0x01,0xef\n\t"
1049e90199cSXiao Guangrong : : "a" (pkru), "c"(ecx), "d"(edx));
1059e90199cSXiao Guangrong }
106c806e887SSebastian Andrzej Siewior
107a927cb83SDave Hansen #else
rdpkru(void)108c806e887SSebastian Andrzej Siewior static inline u32 rdpkru(void)
109a927cb83SDave Hansen {
110a927cb83SDave Hansen return 0;
111a927cb83SDave Hansen }
1129e90199cSXiao Guangrong
wrpkru(u32 pkru)11372a6c08cSThomas Gleixner static inline void wrpkru(u32 pkru)
1149e90199cSXiao Guangrong {
1159e90199cSXiao Guangrong }
116a927cb83SDave Hansen #endif
117a927cb83SDave Hansen
native_wbinvd(void)11810a09940SPeter Zijlstra static __always_inline void native_wbinvd(void)
119f05e798aSDavid Howells {
120f05e798aSDavid Howells asm volatile("wbinvd": : :"memory");
121f05e798aSDavid Howells }
122f05e798aSDavid Howells
__read_cr4(void)12387930019SJuergen Gross static inline unsigned long __read_cr4(void)
12487930019SJuergen Gross {
12587930019SJuergen Gross return native_read_cr4();
12687930019SJuergen Gross }
12787930019SJuergen Gross
128fdc0269eSJuergen Gross #ifdef CONFIG_PARAVIRT_XXL
129f05e798aSDavid Howells #include <asm/paravirt.h>
130fdc0269eSJuergen Gross #else
131f05e798aSDavid Howells
read_cr0(void)132f05e798aSDavid Howells static inline unsigned long read_cr0(void)
133f05e798aSDavid Howells {
134f05e798aSDavid Howells return native_read_cr0();
135f05e798aSDavid Howells }
136f05e798aSDavid Howells
write_cr0(unsigned long x)137f05e798aSDavid Howells static inline void write_cr0(unsigned long x)
138f05e798aSDavid Howells {
139f05e798aSDavid Howells native_write_cr0(x);
140f05e798aSDavid Howells }
141f05e798aSDavid Howells
read_cr2(void)1422823e83aSPeter Zijlstra static __always_inline unsigned long read_cr2(void)
143f05e798aSDavid Howells {
144f05e798aSDavid Howells return native_read_cr2();
145f05e798aSDavid Howells }
146f05e798aSDavid Howells
write_cr2(unsigned long x)1472823e83aSPeter Zijlstra static __always_inline void write_cr2(unsigned long x)
148f05e798aSDavid Howells {
149f05e798aSDavid Howells native_write_cr2(x);
150f05e798aSDavid Howells }
151f05e798aSDavid Howells
1526c690ee1SAndy Lutomirski /*
1536c690ee1SAndy Lutomirski * Careful! CR3 contains more than just an address. You probably want
1546c690ee1SAndy Lutomirski * read_cr3_pa() instead.
1556c690ee1SAndy Lutomirski */
__read_cr3(void)1566c690ee1SAndy Lutomirski static inline unsigned long __read_cr3(void)
157f05e798aSDavid Howells {
1586c690ee1SAndy Lutomirski return __native_read_cr3();
159f05e798aSDavid Howells }
160f05e798aSDavid Howells
write_cr3(unsigned long x)161f05e798aSDavid Howells static inline void write_cr3(unsigned long x)
162f05e798aSDavid Howells {
163f05e798aSDavid Howells native_write_cr3(x);
164f05e798aSDavid Howells }
165f05e798aSDavid Howells
__write_cr4(unsigned long x)1661e02ce4cSAndy Lutomirski static inline void __write_cr4(unsigned long x)
167f05e798aSDavid Howells {
168f05e798aSDavid Howells native_write_cr4(x);
169f05e798aSDavid Howells }
170f05e798aSDavid Howells
wbinvd(void)17110a09940SPeter Zijlstra static __always_inline void wbinvd(void)
172f05e798aSDavid Howells {
173f05e798aSDavid Howells native_wbinvd();
174f05e798aSDavid Howells }
175f05e798aSDavid Howells
1769bad5658SJuergen Gross #endif /* CONFIG_PARAVIRT_XXL */
177f05e798aSDavid Howells
clflush(volatile void * __p)1782ec8efe6SPeter Zijlstra static __always_inline void clflush(volatile void *__p)
179f05e798aSDavid Howells {
180f05e798aSDavid Howells asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
181f05e798aSDavid Howells }
182f05e798aSDavid Howells
clflushopt(volatile void * __p)183171699f7SRoss Zwisler static inline void clflushopt(volatile void *__p)
184171699f7SRoss Zwisler {
185a89dfde3SPeter Zijlstra alternative_io(".byte 0x3e; clflush %P0",
186171699f7SRoss Zwisler ".byte 0x66; clflush %P0",
187171699f7SRoss Zwisler X86_FEATURE_CLFLUSHOPT,
188171699f7SRoss Zwisler "+m" (*(volatile char __force *)__p));
189171699f7SRoss Zwisler }
190171699f7SRoss Zwisler
clwb(volatile void * __p)191d9dc64f3SRoss Zwisler static inline void clwb(volatile void *__p)
192d9dc64f3SRoss Zwisler {
193d9dc64f3SRoss Zwisler volatile struct { char x[64]; } *p = __p;
194d9dc64f3SRoss Zwisler
195d9dc64f3SRoss Zwisler asm volatile(ALTERNATIVE_2(
196a89dfde3SPeter Zijlstra ".byte 0x3e; clflush (%[pax])",
197d9dc64f3SRoss Zwisler ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */
198d9dc64f3SRoss Zwisler X86_FEATURE_CLFLUSHOPT,
199d9dc64f3SRoss Zwisler ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */
200d9dc64f3SRoss Zwisler X86_FEATURE_CLWB)
201d9dc64f3SRoss Zwisler : [p] "+m" (*p)
202d9dc64f3SRoss Zwisler : [pax] "a" (p));
203d9dc64f3SRoss Zwisler }
204d9dc64f3SRoss Zwisler
20592805476SRick Edgecombe #ifdef CONFIG_X86_USER_SHADOW_STACK
write_user_shstk_64(u64 __user * addr,u64 val)20692805476SRick Edgecombe static inline int write_user_shstk_64(u64 __user *addr, u64 val)
20792805476SRick Edgecombe {
208aaff74d8SLinus Torvalds asm goto("1: wrussq %[val], (%[addr])\n"
20992805476SRick Edgecombe _ASM_EXTABLE(1b, %l[fail])
21092805476SRick Edgecombe :: [addr] "r" (addr), [val] "r" (val)
21192805476SRick Edgecombe :: fail);
21292805476SRick Edgecombe return 0;
21392805476SRick Edgecombe fail:
21492805476SRick Edgecombe return -EFAULT;
21592805476SRick Edgecombe }
21692805476SRick Edgecombe #endif /* CONFIG_X86_USER_SHADOW_STACK */
21792805476SRick Edgecombe
218f05e798aSDavid Howells #define nop() asm volatile ("nop")
219f05e798aSDavid Howells
serialize(void)220*ee37f3a5SJuergen Gross static __always_inline void serialize(void)
221bf9c912fSRicardo Neri {
222bf9c912fSRicardo Neri /* Instruction opcode for SERIALIZE; supported in binutils >= 2.35. */
223bf9c912fSRicardo Neri asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
224bf9c912fSRicardo Neri }
225bf9c912fSRicardo Neri
2260888e103SDave Jiang /* The dst parameter must be 64-bytes aligned */
movdir64b(void __iomem * dst,const void * src)2276ae58d87SDave Jiang static inline void movdir64b(void __iomem *dst, const void *src)
2280888e103SDave Jiang {
2290888e103SDave Jiang const struct { char _[64]; } *__src = src;
2306ae58d87SDave Jiang struct { char _[64]; } __iomem *__dst = dst;
2310888e103SDave Jiang
2320888e103SDave Jiang /*
2330888e103SDave Jiang * MOVDIR64B %(rdx), rax.
2340888e103SDave Jiang *
2350888e103SDave Jiang * Both __src and __dst must be memory constraints in order to tell the
2360888e103SDave Jiang * compiler that no other memory accesses should be reordered around
2370888e103SDave Jiang * this one.
2380888e103SDave Jiang *
2390888e103SDave Jiang * Also, both must be supplied as lvalues because this tells
2400888e103SDave Jiang * the compiler what the object is (its size) the instruction accesses.
2410888e103SDave Jiang * I.e., not the pointers but what they point to, thus the deref'ing '*'.
2420888e103SDave Jiang */
2430888e103SDave Jiang asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
2440888e103SDave Jiang : "+m" (*__dst)
2450888e103SDave Jiang : "m" (*__src), "a" (__dst), "d" (__src));
2460888e103SDave Jiang }
2470888e103SDave Jiang
2487f5933f8SDave Jiang /**
2497f5933f8SDave Jiang * enqcmds - Enqueue a command in supervisor (CPL0) mode
2507f5933f8SDave Jiang * @dst: destination, in MMIO space (must be 512-bit aligned)
2517f5933f8SDave Jiang * @src: 512 bits memory operand
2527f5933f8SDave Jiang *
2537f5933f8SDave Jiang * The ENQCMDS instruction allows software to write a 512-bit command to
2547f5933f8SDave Jiang * a 512-bit-aligned special MMIO region that supports the instruction.
2557f5933f8SDave Jiang * A return status is loaded into the ZF flag in the RFLAGS register.
2567f5933f8SDave Jiang * ZF = 0 equates to success, and ZF = 1 indicates retry or error.
2577f5933f8SDave Jiang *
2587f5933f8SDave Jiang * This function issues the ENQCMDS instruction to submit data from
2597f5933f8SDave Jiang * kernel space to MMIO space, in a unit of 512 bits. Order of data access
2607f5933f8SDave Jiang * is not guaranteed, nor is a memory barrier performed afterwards. It
2617f5933f8SDave Jiang * returns 0 on success and -EAGAIN on failure.
2627f5933f8SDave Jiang *
2637f5933f8SDave Jiang * Warning: Do not use this helper unless your driver has checked that the
2647f5933f8SDave Jiang * ENQCMDS instruction is supported on the platform and the device accepts
2657f5933f8SDave Jiang * ENQCMDS.
2667f5933f8SDave Jiang */
enqcmds(void __iomem * dst,const void * src)2677f5933f8SDave Jiang static inline int enqcmds(void __iomem *dst, const void *src)
2687f5933f8SDave Jiang {
2697f5933f8SDave Jiang const struct { char _[64]; } *__src = src;
2705c99720bSDave Jiang struct { char _[64]; } __iomem *__dst = dst;
271d81ff5feSKees Cook bool zf;
2727f5933f8SDave Jiang
2737f5933f8SDave Jiang /*
2747f5933f8SDave Jiang * ENQCMDS %(rdx), rax
2757f5933f8SDave Jiang *
2767f5933f8SDave Jiang * See movdir64b()'s comment on operand specification.
2777f5933f8SDave Jiang */
2787f5933f8SDave Jiang asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
2797f5933f8SDave Jiang CC_SET(z)
2807f5933f8SDave Jiang : CC_OUT(z) (zf), "+m" (*__dst)
2817f5933f8SDave Jiang : "m" (*__src), "a" (__dst), "d" (__src));
2827f5933f8SDave Jiang
2837f5933f8SDave Jiang /* Submission failure is indicated via EFLAGS.ZF=1 */
2847f5933f8SDave Jiang if (zf)
2857f5933f8SDave Jiang return -EAGAIN;
2867f5933f8SDave Jiang
2877f5933f8SDave Jiang return 0;
2887f5933f8SDave Jiang }
2897f5933f8SDave Jiang
tile_release(void)290821ad23dSPeter Zijlstra static __always_inline void tile_release(void)
291f17b1687SChang S. Bae {
292f17b1687SChang S. Bae /*
293f17b1687SChang S. Bae * Instruction opcode for TILERELEASE; supported in binutils
294f17b1687SChang S. Bae * version >= 2.36.
295f17b1687SChang S. Bae */
296f17b1687SChang S. Bae asm volatile(".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0");
297f17b1687SChang S. Bae }
298f17b1687SChang S. Bae
299f05e798aSDavid Howells #endif /* __KERNEL__ */
300f05e798aSDavid Howells
301f05e798aSDavid Howells #endif /* _ASM_X86_SPECIAL_INSNS_H */
302