1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_INSN_H
3 #define __KVM_X86_VMX_INSN_H
5 #include <linux/nospec.h>
7 #include <asm/kvm_host.h>
13 #define __ex(x) __kvm_handle_fault_on_reboot(x)
15 __attribute__((regparm(0))) void vmread_error_trampoline(unsigned long field,
17 void vmwrite_error(unsigned long field, unsigned long value);
18 void vmclear_error(struct vmcs *vmcs, u64 phys_addr);
19 void vmptrld_error(struct vmcs *vmcs, u64 phys_addr);
20 void invvpid_error(unsigned long ext, u16 vpid, gva_t gva);
21 void invept_error(unsigned long ext, u64 eptp, gpa_t gpa);
23 static __always_inline void vmcs_check16(unsigned long field)
25 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
26 "16-bit accessor invalid for 64-bit field");
27 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
28 "16-bit accessor invalid for 64-bit high field");
29 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
30 "16-bit accessor invalid for 32-bit high field");
31 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
32 "16-bit accessor invalid for natural width field");
35 static __always_inline void vmcs_check32(unsigned long field)
37 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
38 "32-bit accessor invalid for 16-bit field");
39 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
40 "32-bit accessor invalid for natural width field");
43 static __always_inline void vmcs_check64(unsigned long field)
45 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
46 "64-bit accessor invalid for 16-bit field");
47 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
48 "64-bit accessor invalid for 64-bit high field");
49 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
50 "64-bit accessor invalid for 32-bit field");
51 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
52 "64-bit accessor invalid for natural width field");
55 static __always_inline void vmcs_checkl(unsigned long field)
57 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
58 "Natural width accessor invalid for 16-bit field");
59 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
60 "Natural width accessor invalid for 64-bit field");
61 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
62 "Natural width accessor invalid for 64-bit high field");
63 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
64 "Natural width accessor invalid for 32-bit field");
67 static __always_inline unsigned long __vmcs_readl(unsigned long field)
71 asm volatile("1: vmread %2, %1\n\t"
72 ".byte 0x3e\n\t" /* branch taken hint */
76 * VMREAD failed. Push '0' for @fault, push the failing
77 * @field, and bounce through the trampoline to preserve
82 "2:call vmread_error_trampoline\n\t"
85 * Unwind the stack. Note, the trampoline zeros out the
86 * memory for @fault so that the result is '0' on error.
92 /* VMREAD faulted. As above, except push '1' for @fault. */
93 ".pushsection .fixup, \"ax\"\n\t"
99 : ASM_CALL_CONSTRAINT, "=r"(value) : "r"(field) : "cc");
103 static __always_inline u16 vmcs_read16(unsigned long field)
106 if (static_branch_unlikely(&enable_evmcs))
107 return evmcs_read16(field);
108 return __vmcs_readl(field);
111 static __always_inline u32 vmcs_read32(unsigned long field)
114 if (static_branch_unlikely(&enable_evmcs))
115 return evmcs_read32(field);
116 return __vmcs_readl(field);
119 static __always_inline u64 vmcs_read64(unsigned long field)
122 if (static_branch_unlikely(&enable_evmcs))
123 return evmcs_read64(field);
125 return __vmcs_readl(field);
127 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
131 static __always_inline unsigned long vmcs_readl(unsigned long field)
134 if (static_branch_unlikely(&enable_evmcs))
135 return evmcs_read64(field);
136 return __vmcs_readl(field);
139 #define vmx_asm1(insn, op1, error_args...) \
141 asm_volatile_goto("1: " __stringify(insn) " %0\n\t" \
142 ".byte 0x2e\n\t" /* branch not taken hint */ \
143 "jna %l[error]\n\t" \
144 _ASM_EXTABLE(1b, %l[fault]) \
145 : : op1 : "cc" : error, fault); \
148 insn##_error(error_args); \
151 kvm_spurious_fault(); \
154 #define vmx_asm2(insn, op1, op2, error_args...) \
156 asm_volatile_goto("1: " __stringify(insn) " %1, %0\n\t" \
157 ".byte 0x2e\n\t" /* branch not taken hint */ \
158 "jna %l[error]\n\t" \
159 _ASM_EXTABLE(1b, %l[fault]) \
160 : : op1, op2 : "cc" : error, fault); \
163 insn##_error(error_args); \
166 kvm_spurious_fault(); \
169 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
171 vmx_asm2(vmwrite, "r"(field), "rm"(value), field, value);
174 static __always_inline void vmcs_write16(unsigned long field, u16 value)
177 if (static_branch_unlikely(&enable_evmcs))
178 return evmcs_write16(field, value);
180 __vmcs_writel(field, value);
183 static __always_inline void vmcs_write32(unsigned long field, u32 value)
186 if (static_branch_unlikely(&enable_evmcs))
187 return evmcs_write32(field, value);
189 __vmcs_writel(field, value);
192 static __always_inline void vmcs_write64(unsigned long field, u64 value)
195 if (static_branch_unlikely(&enable_evmcs))
196 return evmcs_write64(field, value);
198 __vmcs_writel(field, value);
199 #ifndef CONFIG_X86_64
200 __vmcs_writel(field+1, value >> 32);
204 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
207 if (static_branch_unlikely(&enable_evmcs))
208 return evmcs_write64(field, value);
210 __vmcs_writel(field, value);
213 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
215 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
216 "vmcs_clear_bits does not support 64-bit fields");
217 if (static_branch_unlikely(&enable_evmcs))
218 return evmcs_write32(field, evmcs_read32(field) & ~mask);
220 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
223 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
225 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
226 "vmcs_set_bits does not support 64-bit fields");
227 if (static_branch_unlikely(&enable_evmcs))
228 return evmcs_write32(field, evmcs_read32(field) | mask);
230 __vmcs_writel(field, __vmcs_readl(field) | mask);
233 static inline void vmcs_clear(struct vmcs *vmcs)
235 u64 phys_addr = __pa(vmcs);
237 vmx_asm1(vmclear, "m"(phys_addr), vmcs, phys_addr);
240 static inline void vmcs_load(struct vmcs *vmcs)
242 u64 phys_addr = __pa(vmcs);
244 if (static_branch_unlikely(&enable_evmcs))
245 return evmcs_load(phys_addr);
247 vmx_asm1(vmptrld, "m"(phys_addr), vmcs, phys_addr);
250 static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
256 } operand = { vpid, 0, gva };
258 vmx_asm2(invvpid, "r"(ext), "m"(operand), ext, vpid, gva);
261 static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
265 } operand = {eptp, gpa};
267 vmx_asm2(invept, "r"(ext), "m"(operand), ext, eptp, gpa);
270 static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
275 if (cpu_has_vmx_invvpid_individual_addr()) {
276 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
283 static inline void vpid_sync_vcpu_single(int vpid)
288 if (cpu_has_vmx_invvpid_single())
289 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
292 static inline void vpid_sync_vcpu_global(void)
294 if (cpu_has_vmx_invvpid_global())
295 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
298 static inline void vpid_sync_context(int vpid)
300 if (cpu_has_vmx_invvpid_single())
301 vpid_sync_vcpu_single(vpid);
303 vpid_sync_vcpu_global();
306 static inline void ept_sync_global(void)
308 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
311 static inline void ept_sync_context(u64 eptp)
313 if (cpu_has_vmx_invept_context())
314 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
319 #endif /* __KVM_X86_VMX_INSN_H */