Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 2 | #ifndef _ASM_X86_SPECIAL_INSNS_H |
| 3 | #define _ASM_X86_SPECIAL_INSNS_H |
| 4 | |
| 5 | |
| 6 | #ifdef __KERNEL__ |
| 7 | |
Ross Zwisler | 719d359 | 2015-02-19 10:37:28 -0700 | [diff] [blame] | 8 | #include <asm/nops.h> |
Kees Cook | 873d50d | 2019-06-17 21:55:02 -0700 | [diff] [blame] | 9 | #include <asm/processor-flags.h> |
Thomas Gleixner | 410367e | 2020-03-04 23:32:15 +0100 | [diff] [blame] | 10 | #include <linux/irqflags.h> |
Kees Cook | 873d50d | 2019-06-17 21:55:02 -0700 | [diff] [blame] | 11 | #include <linux/jump_label.h> |
Ross Zwisler | 719d359 | 2015-02-19 10:37:28 -0700 | [diff] [blame] | 12 | |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 13 | /* |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 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. |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 20 | */ |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 21 | |
| 22 | #define __FORCE_ORDER "m"(*(unsigned int *)0x1000UL) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 23 | |
Thomas Gleixner | 7652ac9 | 2019-07-10 21:42:46 +0200 | [diff] [blame] | 24 | void native_write_cr0(unsigned long val); |
Kees Cook | 873d50d | 2019-06-17 21:55:02 -0700 | [diff] [blame] | 25 | |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 26 | static inline unsigned long native_read_cr0(void) |
| 27 | { |
| 28 | unsigned long val; |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 29 | asm volatile("mov %%cr0,%0\n\t" : "=r" (val) : __FORCE_ORDER); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 30 | return val; |
| 31 | } |
| 32 | |
Peter Zijlstra | 2823e83 | 2020-06-03 13:40:22 +0200 | [diff] [blame] | 33 | static __always_inline unsigned long native_read_cr2(void) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 34 | { |
| 35 | unsigned long val; |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 36 | asm volatile("mov %%cr2,%0\n\t" : "=r" (val) : __FORCE_ORDER); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 37 | return val; |
| 38 | } |
| 39 | |
Peter Zijlstra | 2823e83 | 2020-06-03 13:40:22 +0200 | [diff] [blame] | 40 | static __always_inline void native_write_cr2(unsigned long val) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 41 | { |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 42 | asm volatile("mov %0,%%cr2": : "r" (val) : "memory"); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 45 | static inline unsigned long __native_read_cr3(void) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 46 | { |
| 47 | unsigned long val; |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 48 | asm volatile("mov %%cr3,%0\n\t" : "=r" (val) : __FORCE_ORDER); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 49 | return val; |
| 50 | } |
| 51 | |
| 52 | static inline void native_write_cr3(unsigned long val) |
| 53 | { |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 54 | asm volatile("mov %0,%%cr3": : "r" (val) : "memory"); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline unsigned long native_read_cr4(void) |
| 58 | { |
| 59 | unsigned long val; |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 60 | #ifdef CONFIG_X86_32 |
Andy Lutomirski | 1ef55be1 | 2016-09-29 12:48:12 -0700 | [diff] [blame] | 61 | /* |
| 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. |
| 65 | */ |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 66 | asm volatile("1: mov %%cr4, %0\n" |
| 67 | "2:\n" |
| 68 | _ASM_EXTABLE(1b, 2b) |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 69 | : "=r" (val) : "0" (0), __FORCE_ORDER); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 70 | #else |
Andy Lutomirski | 1ef55be1 | 2016-09-29 12:48:12 -0700 | [diff] [blame] | 71 | /* CR4 always exists on x86_64. */ |
Arvind Sankar | aa5cacd | 2020-09-02 19:21:52 -0400 | [diff] [blame] | 72 | asm volatile("mov %%cr4,%0\n\t" : "=r" (val) : __FORCE_ORDER); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 73 | #endif |
| 74 | return val; |
| 75 | } |
| 76 | |
Thomas Gleixner | 7652ac9 | 2019-07-10 21:42:46 +0200 | [diff] [blame] | 77 | void native_write_cr4(unsigned long val); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 78 | |
Dave Hansen | a927cb8 | 2016-02-12 13:02:15 -0800 | [diff] [blame] | 79 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS |
Sebastian Andrzej Siewior | c806e887 | 2019-04-03 18:41:41 +0200 | [diff] [blame] | 80 | static inline u32 rdpkru(void) |
Dave Hansen | a927cb8 | 2016-02-12 13:02:15 -0800 | [diff] [blame] | 81 | { |
| 82 | u32 ecx = 0; |
| 83 | u32 edx, pkru; |
| 84 | |
| 85 | /* |
| 86 | * "rdpkru" instruction. Places PKRU contents in to EAX, |
| 87 | * clears EDX and requires that ecx=0. |
| 88 | */ |
| 89 | asm volatile(".byte 0x0f,0x01,0xee\n\t" |
| 90 | : "=a" (pkru), "=d" (edx) |
| 91 | : "c" (ecx)); |
| 92 | return pkru; |
| 93 | } |
Xiao Guangrong | 9e90199 | 2016-03-22 16:51:17 +0800 | [diff] [blame] | 94 | |
Sebastian Andrzej Siewior | c806e887 | 2019-04-03 18:41:41 +0200 | [diff] [blame] | 95 | static inline void wrpkru(u32 pkru) |
Xiao Guangrong | 9e90199 | 2016-03-22 16:51:17 +0800 | [diff] [blame] | 96 | { |
| 97 | u32 ecx = 0, edx = 0; |
| 98 | |
| 99 | /* |
| 100 | * "wrpkru" instruction. Loads contents in EAX to PKRU, |
| 101 | * requires that ecx = edx = 0. |
| 102 | */ |
| 103 | asm volatile(".byte 0x0f,0x01,0xef\n\t" |
| 104 | : : "a" (pkru), "c"(ecx), "d"(edx)); |
| 105 | } |
Sebastian Andrzej Siewior | c806e887 | 2019-04-03 18:41:41 +0200 | [diff] [blame] | 106 | |
Dave Hansen | a927cb8 | 2016-02-12 13:02:15 -0800 | [diff] [blame] | 107 | #else |
Sebastian Andrzej Siewior | c806e887 | 2019-04-03 18:41:41 +0200 | [diff] [blame] | 108 | static inline u32 rdpkru(void) |
Dave Hansen | a927cb8 | 2016-02-12 13:02:15 -0800 | [diff] [blame] | 109 | { |
| 110 | return 0; |
| 111 | } |
Xiao Guangrong | 9e90199 | 2016-03-22 16:51:17 +0800 | [diff] [blame] | 112 | |
Thomas Gleixner | 72a6c08 | 2021-06-23 14:02:23 +0200 | [diff] [blame] | 113 | static inline void wrpkru(u32 pkru) |
Xiao Guangrong | 9e90199 | 2016-03-22 16:51:17 +0800 | [diff] [blame] | 114 | { |
| 115 | } |
Dave Hansen | a927cb8 | 2016-02-12 13:02:15 -0800 | [diff] [blame] | 116 | #endif |
| 117 | |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 118 | static inline void native_wbinvd(void) |
| 119 | { |
| 120 | asm volatile("wbinvd": : :"memory"); |
| 121 | } |
| 122 | |
Thomas Gleixner | 410367e | 2020-03-04 23:32:15 +0100 | [diff] [blame] | 123 | extern asmlinkage void asm_load_gs_index(unsigned int selector); |
| 124 | |
| 125 | static 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 | } |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 133 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 134 | static inline unsigned long __read_cr4(void) |
| 135 | { |
| 136 | return native_read_cr4(); |
| 137 | } |
| 138 | |
Juergen Gross | fdc0269 | 2018-08-28 09:40:25 +0200 | [diff] [blame] | 139 | #ifdef CONFIG_PARAVIRT_XXL |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 140 | #include <asm/paravirt.h> |
Juergen Gross | fdc0269 | 2018-08-28 09:40:25 +0200 | [diff] [blame] | 141 | #else |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 142 | |
| 143 | static inline unsigned long read_cr0(void) |
| 144 | { |
| 145 | return native_read_cr0(); |
| 146 | } |
| 147 | |
| 148 | static inline void write_cr0(unsigned long x) |
| 149 | { |
| 150 | native_write_cr0(x); |
| 151 | } |
| 152 | |
Peter Zijlstra | 2823e83 | 2020-06-03 13:40:22 +0200 | [diff] [blame] | 153 | static __always_inline unsigned long read_cr2(void) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 154 | { |
| 155 | return native_read_cr2(); |
| 156 | } |
| 157 | |
Peter Zijlstra | 2823e83 | 2020-06-03 13:40:22 +0200 | [diff] [blame] | 158 | static __always_inline void write_cr2(unsigned long x) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 159 | { |
| 160 | native_write_cr2(x); |
| 161 | } |
| 162 | |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 163 | /* |
| 164 | * Careful! CR3 contains more than just an address. You probably want |
| 165 | * read_cr3_pa() instead. |
| 166 | */ |
| 167 | static inline unsigned long __read_cr3(void) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 168 | { |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 169 | return __native_read_cr3(); |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static inline void write_cr3(unsigned long x) |
| 173 | { |
| 174 | native_write_cr3(x); |
| 175 | } |
| 176 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 177 | static inline void __write_cr4(unsigned long x) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 178 | { |
| 179 | native_write_cr4(x); |
| 180 | } |
| 181 | |
| 182 | static inline void wbinvd(void) |
| 183 | { |
| 184 | native_wbinvd(); |
| 185 | } |
| 186 | |
| 187 | #ifdef CONFIG_X86_64 |
| 188 | |
Thomas Gleixner | 410367e | 2020-03-04 23:32:15 +0100 | [diff] [blame] | 189 | static inline void load_gs_index(unsigned int selector) |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 190 | { |
| 191 | native_load_gs_index(selector); |
| 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |
Juergen Gross | fdc0269 | 2018-08-28 09:40:25 +0200 | [diff] [blame] | 196 | #endif /* CONFIG_PARAVIRT_XXL */ |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 197 | |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 198 | static inline void clflush(volatile void *__p) |
| 199 | { |
| 200 | asm volatile("clflush %0" : "+m" (*(volatile char __force *)__p)); |
| 201 | } |
| 202 | |
Ross Zwisler | 171699f | 2014-02-26 12:06:49 -0700 | [diff] [blame] | 203 | static inline void clflushopt(volatile void *__p) |
| 204 | { |
Peter Zijlstra | a89dfde | 2021-03-12 12:32:54 +0100 | [diff] [blame] | 205 | alternative_io(".byte 0x3e; clflush %P0", |
Ross Zwisler | 171699f | 2014-02-26 12:06:49 -0700 | [diff] [blame] | 206 | ".byte 0x66; clflush %P0", |
| 207 | X86_FEATURE_CLFLUSHOPT, |
| 208 | "+m" (*(volatile char __force *)__p)); |
| 209 | } |
| 210 | |
Ross Zwisler | d9dc64f | 2015-01-27 09:53:51 -0700 | [diff] [blame] | 211 | static inline void clwb(volatile void *__p) |
| 212 | { |
| 213 | volatile struct { char x[64]; } *p = __p; |
| 214 | |
| 215 | asm volatile(ALTERNATIVE_2( |
Peter Zijlstra | a89dfde | 2021-03-12 12:32:54 +0100 | [diff] [blame] | 216 | ".byte 0x3e; clflush (%[pax])", |
Ross Zwisler | d9dc64f | 2015-01-27 09:53:51 -0700 | [diff] [blame] | 217 | ".byte 0x66; clflush (%[pax])", /* clflushopt (%%rax) */ |
| 218 | X86_FEATURE_CLFLUSHOPT, |
| 219 | ".byte 0x66, 0x0f, 0xae, 0x30", /* clwb (%%rax) */ |
| 220 | X86_FEATURE_CLWB) |
| 221 | : [p] "+m" (*p) |
| 222 | : [pax] "a" (p)); |
| 223 | } |
| 224 | |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 225 | #define nop() asm volatile ("nop") |
| 226 | |
Ricardo Neri | bf9c912 | 2020-08-06 20:28:33 -0700 | [diff] [blame] | 227 | static inline void serialize(void) |
| 228 | { |
| 229 | /* Instruction opcode for SERIALIZE; supported in binutils >= 2.35. */ |
| 230 | asm volatile(".byte 0xf, 0x1, 0xe8" ::: "memory"); |
| 231 | } |
| 232 | |
Dave Jiang | 0888e10 | 2020-10-05 08:11:22 -0700 | [diff] [blame] | 233 | /* The dst parameter must be 64-bytes aligned */ |
Dave Jiang | 6ae58d8 | 2021-01-07 09:44:51 -0700 | [diff] [blame] | 234 | static inline void movdir64b(void __iomem *dst, const void *src) |
Dave Jiang | 0888e10 | 2020-10-05 08:11:22 -0700 | [diff] [blame] | 235 | { |
| 236 | const struct { char _[64]; } *__src = src; |
Dave Jiang | 6ae58d8 | 2021-01-07 09:44:51 -0700 | [diff] [blame] | 237 | struct { char _[64]; } __iomem *__dst = dst; |
Dave Jiang | 0888e10 | 2020-10-05 08:11:22 -0700 | [diff] [blame] | 238 | |
| 239 | /* |
| 240 | * MOVDIR64B %(rdx), rax. |
| 241 | * |
| 242 | * Both __src and __dst must be memory constraints in order to tell the |
| 243 | * compiler that no other memory accesses should be reordered around |
| 244 | * this one. |
| 245 | * |
| 246 | * Also, both must be supplied as lvalues because this tells |
| 247 | * the compiler what the object is (its size) the instruction accesses. |
| 248 | * I.e., not the pointers but what they point to, thus the deref'ing '*'. |
| 249 | */ |
| 250 | asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02" |
| 251 | : "+m" (*__dst) |
| 252 | : "m" (*__src), "a" (__dst), "d" (__src)); |
| 253 | } |
| 254 | |
Dave Jiang | 7f5933f | 2020-10-05 08:11:23 -0700 | [diff] [blame] | 255 | /** |
| 256 | * enqcmds - Enqueue a command in supervisor (CPL0) mode |
| 257 | * @dst: destination, in MMIO space (must be 512-bit aligned) |
| 258 | * @src: 512 bits memory operand |
| 259 | * |
| 260 | * The ENQCMDS instruction allows software to write a 512-bit command to |
| 261 | * a 512-bit-aligned special MMIO region that supports the instruction. |
| 262 | * A return status is loaded into the ZF flag in the RFLAGS register. |
| 263 | * ZF = 0 equates to success, and ZF = 1 indicates retry or error. |
| 264 | * |
| 265 | * This function issues the ENQCMDS instruction to submit data from |
| 266 | * kernel space to MMIO space, in a unit of 512 bits. Order of data access |
| 267 | * is not guaranteed, nor is a memory barrier performed afterwards. It |
| 268 | * returns 0 on success and -EAGAIN on failure. |
| 269 | * |
| 270 | * Warning: Do not use this helper unless your driver has checked that the |
| 271 | * ENQCMDS instruction is supported on the platform and the device accepts |
| 272 | * ENQCMDS. |
| 273 | */ |
| 274 | static inline int enqcmds(void __iomem *dst, const void *src) |
| 275 | { |
| 276 | const struct { char _[64]; } *__src = src; |
Dave Jiang | 5c99720 | 2021-01-07 09:45:21 -0700 | [diff] [blame] | 277 | struct { char _[64]; } __iomem *__dst = dst; |
Kees Cook | d81ff5f | 2021-09-10 15:33:32 -0700 | [diff] [blame] | 278 | bool zf; |
Dave Jiang | 7f5933f | 2020-10-05 08:11:23 -0700 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * ENQCMDS %(rdx), rax |
| 282 | * |
| 283 | * See movdir64b()'s comment on operand specification. |
| 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 | /* Submission failure is indicated via EFLAGS.ZF=1 */ |
| 291 | if (zf) |
| 292 | return -EAGAIN; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
David Howells | f05e798 | 2012-03-28 18:11:12 +0100 | [diff] [blame] | 297 | #endif /* __KERNEL__ */ |
| 298 | |
| 299 | #endif /* _ASM_X86_SPECIAL_INSNS_H */ |