xref: /linux/arch/x86/include/asm/special_insns.h (revision 1bd2400fba444db006c1949f6ea02cbc5543cee7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_SPECIAL_INSNS_H
3 #define _ASM_X86_SPECIAL_INSNS_H
4 
5 #ifdef __KERNEL__
6 #include <asm/nops.h>
7 #include <asm/processor-flags.h>
8 
9 #include <linux/errno.h>
10 #include <linux/irqflags.h>
11 #include <linux/jump_label.h>
12 
13 void native_write_cr0(unsigned long val);
14 
native_read_cr0(void)15 static inline unsigned long native_read_cr0(void)
16 {
17 	unsigned long val;
18 	asm volatile("mov %%cr0,%0" : "=r" (val));
19 	return val;
20 }
21 
native_read_cr2(void)22 static __always_inline unsigned long native_read_cr2(void)
23 {
24 	unsigned long val;
25 	asm volatile("mov %%cr2,%0" : "=r" (val));
26 	return val;
27 }
28 
native_write_cr2(unsigned long val)29 static __always_inline void native_write_cr2(unsigned long val)
30 {
31 	asm volatile("mov %0,%%cr2": : "r" (val) : "memory");
32 }
33 
__native_read_cr3(void)34 static __always_inline unsigned long __native_read_cr3(void)
35 {
36 	unsigned long val;
37 	asm volatile("mov %%cr3,%0" : "=r" (val));
38 	return val;
39 }
40 
native_write_cr3(unsigned long val)41 static __always_inline void native_write_cr3(unsigned long val)
42 {
43 	asm volatile("mov %0,%%cr3": : "r" (val) : "memory");
44 }
45 
native_read_cr4(void)46 static inline unsigned long native_read_cr4(void)
47 {
48 	unsigned long val;
49 	asm volatile("mov %%cr4,%0" : "=r" (val));
50 	return val;
51 }
52 
53 void native_write_cr4(unsigned long val);
54 
55 #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
rdpkru(void)56 static inline u32 rdpkru(void)
57 {
58 	u32 ecx = 0;
59 	u32 edx, pkru;
60 
61 	/*
62 	 * "rdpkru" instruction.  Places PKRU contents in to EAX,
63 	 * clears EDX and requires that ecx=0.
64 	 */
65 	asm volatile("rdpkru" : "=a" (pkru), "=d" (edx) : "c" (ecx));
66 	return pkru;
67 }
68 
wrpkru(u32 pkru)69 static inline void wrpkru(u32 pkru)
70 {
71 	u32 ecx = 0, edx = 0;
72 
73 	/*
74 	 * "wrpkru" instruction.  Loads contents in EAX to PKRU,
75 	 * requires that ecx = edx = 0.
76 	 */
77 	asm volatile("wrpkru" : : "a" (pkru), "c"(ecx), "d"(edx));
78 }
79 
80 #else
rdpkru(void)81 static inline u32 rdpkru(void)
82 {
83 	return 0;
84 }
85 
wrpkru(u32 pkru)86 static inline void wrpkru(u32 pkru)
87 {
88 }
89 #endif
90 
91 /*
92  * Write back all modified lines in all levels of cache associated with this
93  * logical processor to main memory, and then invalidate all caches.  Depending
94  * on the micro-architecture, WBINVD (and WBNOINVD below) may or may not affect
95  * lower level caches associated with another logical processor that shares any
96  * level of this processor's cache hierarchy.
97  */
wbinvd(void)98 static __always_inline void wbinvd(void)
99 {
100 	asm volatile("wbinvd" : : : "memory");
101 }
102 
103 /* Instruction encoding provided for binutils backwards compatibility. */
104 #define ASM_WBNOINVD _ASM_BYTES(0xf3,0x0f,0x09)
105 
106 /*
107  * Write back all modified lines in all levels of cache associated with this
108  * logical processor to main memory, but do NOT explicitly invalidate caches,
109  * i.e. leave all/most cache lines in the hierarchy in non-modified state.
110  */
wbnoinvd(void)111 static __always_inline void wbnoinvd(void)
112 {
113 	/*
114 	 * Explicitly encode WBINVD if X86_FEATURE_WBNOINVD is unavailable even
115 	 * though WBNOINVD is backwards compatible (it's simply WBINVD with an
116 	 * ignored REP prefix), to guarantee that WBNOINVD isn't used if it
117 	 * needs to be avoided for any reason.  For all supported usage in the
118 	 * kernel, WBINVD is functionally a superset of WBNOINVD.
119 	 */
120 	alternative("wbinvd", ASM_WBNOINVD, X86_FEATURE_WBNOINVD);
121 }
122 
__read_cr4(void)123 static inline unsigned long __read_cr4(void)
124 {
125 	return native_read_cr4();
126 }
127 
128 #ifdef CONFIG_PARAVIRT_XXL
129 #include <asm/paravirt.h>
130 #else
131 
read_cr0(void)132 static inline unsigned long read_cr0(void)
133 {
134 	return native_read_cr0();
135 }
136 
write_cr0(unsigned long x)137 static inline void write_cr0(unsigned long x)
138 {
139 	native_write_cr0(x);
140 }
141 
read_cr2(void)142 static __always_inline unsigned long read_cr2(void)
143 {
144 	return native_read_cr2();
145 }
146 
write_cr2(unsigned long x)147 static __always_inline void write_cr2(unsigned long x)
148 {
149 	native_write_cr2(x);
150 }
151 
152 /*
153  * Careful!  CR3 contains more than just an address.  You probably want
154  * read_cr3_pa() instead.
155  */
__read_cr3(void)156 static inline unsigned long __read_cr3(void)
157 {
158 	return __native_read_cr3();
159 }
160 
write_cr3(unsigned long x)161 static inline void write_cr3(unsigned long x)
162 {
163 	native_write_cr3(x);
164 }
165 
__write_cr4(unsigned long x)166 static inline void __write_cr4(unsigned long x)
167 {
168 	native_write_cr4(x);
169 }
170 #endif /* CONFIG_PARAVIRT_XXL */
171 
clflush(volatile void * __p)172 static __always_inline void clflush(volatile void *__p)
173 {
174 	asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p));
175 }
176 
clflushopt(volatile void * __p)177 static inline void clflushopt(volatile void *__p)
178 {
179 	alternative_io("ds clflush %0",
180 		       "clflushopt %0", X86_FEATURE_CLFLUSHOPT,
181 		       "+m" (*(volatile char __force *)__p));
182 }
183 
clwb(volatile void * __p)184 static inline void clwb(volatile void *__p)
185 {
186 	volatile struct { char x[64]; } *p = __p;
187 
188 	asm_inline volatile(ALTERNATIVE_2(
189 		"ds clflush %0",
190 		"clflushopt %0", X86_FEATURE_CLFLUSHOPT,
191 		"clwb %0", X86_FEATURE_CLWB)
192 		: "+m" (*p));
193 }
194 
195 #ifdef CONFIG_X86_USER_SHADOW_STACK
write_user_shstk_64(u64 __user * addr,u64 val)196 static inline int write_user_shstk_64(u64 __user *addr, u64 val)
197 {
198 	asm goto("1: wrussq %[val], %[addr]\n"
199 			  _ASM_EXTABLE(1b, %l[fail])
200 			  :: [addr] "m" (*addr), [val] "r" (val)
201 			  :: fail);
202 	return 0;
203 fail:
204 	return -EFAULT;
205 }
206 #endif /* CONFIG_X86_USER_SHADOW_STACK */
207 
208 #define nop() asm volatile ("nop")
209 
serialize(void)210 static __always_inline void serialize(void)
211 {
212 	/* Instruction opcode for SERIALIZE; supported in binutils >= 2.35. */
213 	asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory");
214 }
215 
216 /* The dst parameter must be 64-bytes aligned */
movdir64b(void * dst,const void * src)217 static inline void movdir64b(void *dst, const void *src)
218 {
219 	const struct { char _[64]; } *__src = src;
220 	struct { char _[64]; } *__dst = dst;
221 
222 	/*
223 	 * MOVDIR64B %(rdx), rax.
224 	 *
225 	 * Both __src and __dst must be memory constraints in order to tell the
226 	 * compiler that no other memory accesses should be reordered around
227 	 * this one.
228 	 *
229 	 * Also, both must be supplied as lvalues because this tells
230 	 * the compiler what the object is (its size) the instruction accesses.
231 	 * I.e., not the pointers but what they point to, thus the deref'ing '*'.
232 	 */
233 	asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
234 		     : "+m" (*__dst)
235 		     :  "m" (*__src), "a" (__dst), "d" (__src));
236 }
237 
movdir64b_io(void __iomem * dst,const void * src)238 static inline void movdir64b_io(void __iomem *dst, const void *src)
239 {
240 	movdir64b((void __force *)dst, src);
241 }
242 
243 /**
244  * enqcmds - Enqueue a command in supervisor (CPL0) mode
245  * @dst: destination, in MMIO space (must be 512-bit aligned)
246  * @src: 512 bits memory operand
247  *
248  * The ENQCMDS instruction allows software to write a 512-bit command to
249  * a 512-bit-aligned special MMIO region that supports the instruction.
250  * A return status is loaded into the ZF flag in the RFLAGS register.
251  * ZF = 0 equates to success, and ZF = 1 indicates retry or error.
252  *
253  * This function issues the ENQCMDS instruction to submit data from
254  * kernel space to MMIO space, in a unit of 512 bits. Order of data access
255  * is not guaranteed, nor is a memory barrier performed afterwards. It
256  * returns 0 on success and -EAGAIN on failure.
257  *
258  * Warning: Do not use this helper unless your driver has checked that the
259  * ENQCMDS instruction is supported on the platform and the device accepts
260  * ENQCMDS.
261  */
enqcmds(void __iomem * dst,const void * src)262 static inline int enqcmds(void __iomem *dst, const void *src)
263 {
264 	const struct { char _[64]; } *__src = src;
265 	struct { char _[64]; } __iomem *__dst = dst;
266 	bool zf;
267 
268 	/*
269 	 * ENQCMDS %(rdx), rax
270 	 *
271 	 * See movdir64b()'s comment on operand specification.
272 	 */
273 	asm volatile(".byte 0xf3, 0x0f, 0x38, 0xf8, 0x02, 0x66, 0x90"
274 		     : "=@ccz" (zf), "+m" (*__dst)
275 		     : "m" (*__src), "a" (__dst), "d" (__src));
276 
277 	/* Submission failure is indicated via EFLAGS.ZF=1 */
278 	if (zf)
279 		return -EAGAIN;
280 
281 	return 0;
282 }
283 
tile_release(void)284 static __always_inline void tile_release(void)
285 {
286 	/*
287 	 * Instruction opcode for TILERELEASE; supported in binutils
288 	 * version >= 2.36.
289 	 */
290 	asm volatile(".byte 0xc4, 0xe2, 0x78, 0x49, 0xc0");
291 }
292 
293 #endif /* __KERNEL__ */
294 
295 #endif /* _ASM_X86_SPECIAL_INSNS_H */
296