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 asmlinkage void vmread_error(unsigned long field, bool fault);
16 __attribute__((regparm(0))) void vmread_error_trampoline(unsigned long field,
18 void vmwrite_error(unsigned long field, unsigned long value);
19 void vmclear_error(struct vmcs *vmcs, u64 phys_addr);
20 void vmptrld_error(struct vmcs *vmcs, u64 phys_addr);
21 void invvpid_error(unsigned long ext, u16 vpid, gva_t gva);
22 void invept_error(unsigned long ext, u64 eptp, gpa_t gpa);
24 static __always_inline void vmcs_check16(unsigned long field)
26 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
27 "16-bit accessor invalid for 64-bit field");
28 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
29 "16-bit accessor invalid for 64-bit high field");
30 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
31 "16-bit accessor invalid for 32-bit high field");
32 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
33 "16-bit accessor invalid for natural width field");
36 static __always_inline void vmcs_check32(unsigned long field)
38 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
39 "32-bit accessor invalid for 16-bit field");
40 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
41 "32-bit accessor invalid for natural width field");
44 static __always_inline void vmcs_check64(unsigned long field)
46 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
47 "64-bit accessor invalid for 16-bit field");
48 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
49 "64-bit accessor invalid for 64-bit high field");
50 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
51 "64-bit accessor invalid for 32-bit field");
52 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
53 "64-bit accessor invalid for natural width field");
56 static __always_inline void vmcs_checkl(unsigned long field)
58 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
59 "Natural width accessor invalid for 16-bit field");
60 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
61 "Natural width accessor invalid for 64-bit field");
62 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
63 "Natural width accessor invalid for 64-bit high field");
64 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
65 "Natural width accessor invalid for 32-bit field");
68 static __always_inline unsigned long __vmcs_readl(unsigned long field)
72 asm volatile("1: vmread %2, %1\n\t"
73 ".byte 0x3e\n\t" /* branch taken hint */
77 * VMREAD failed. Push '0' for @fault, push the failing
78 * @field, and bounce through the trampoline to preserve
83 "2:call vmread_error_trampoline\n\t"
86 * Unwind the stack. Note, the trampoline zeros out the
87 * memory for @fault so that the result is '0' on error.
93 /* VMREAD faulted. As above, except push '1' for @fault. */
94 ".pushsection .fixup, \"ax\"\n\t"
100 : ASM_CALL_CONSTRAINT, "=r"(value) : "r"(field) : "cc");
104 static __always_inline u16 vmcs_read16(unsigned long field)
107 if (static_branch_unlikely(&enable_evmcs))
108 return evmcs_read16(field);
109 return __vmcs_readl(field);
112 static __always_inline u32 vmcs_read32(unsigned long field)
115 if (static_branch_unlikely(&enable_evmcs))
116 return evmcs_read32(field);
117 return __vmcs_readl(field);
120 static __always_inline u64 vmcs_read64(unsigned long field)
123 if (static_branch_unlikely(&enable_evmcs))
124 return evmcs_read64(field);
126 return __vmcs_readl(field);
128 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
132 static __always_inline unsigned long vmcs_readl(unsigned long field)
135 if (static_branch_unlikely(&enable_evmcs))
136 return evmcs_read64(field);
137 return __vmcs_readl(field);
140 #define vmx_asm1(insn, op1, error_args...) \
142 asm_volatile_goto("1: " __stringify(insn) " %0\n\t" \
143 ".byte 0x2e\n\t" /* branch not taken hint */ \
144 "jna %l[error]\n\t" \
145 _ASM_EXTABLE(1b, %l[fault]) \
146 : : op1 : "cc" : error, fault); \
149 insn##_error(error_args); \
152 kvm_spurious_fault(); \
155 #define vmx_asm2(insn, op1, op2, error_args...) \
157 asm_volatile_goto("1: " __stringify(insn) " %1, %0\n\t" \
158 ".byte 0x2e\n\t" /* branch not taken hint */ \
159 "jna %l[error]\n\t" \
160 _ASM_EXTABLE(1b, %l[fault]) \
161 : : op1, op2 : "cc" : error, fault); \
164 insn##_error(error_args); \
167 kvm_spurious_fault(); \
170 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
172 vmx_asm2(vmwrite, "r"(field), "rm"(value), field, value);
175 static __always_inline void vmcs_write16(unsigned long field, u16 value)
178 if (static_branch_unlikely(&enable_evmcs))
179 return evmcs_write16(field, value);
181 __vmcs_writel(field, value);
184 static __always_inline void vmcs_write32(unsigned long field, u32 value)
187 if (static_branch_unlikely(&enable_evmcs))
188 return evmcs_write32(field, value);
190 __vmcs_writel(field, value);
193 static __always_inline void vmcs_write64(unsigned long field, u64 value)
196 if (static_branch_unlikely(&enable_evmcs))
197 return evmcs_write64(field, value);
199 __vmcs_writel(field, value);
200 #ifndef CONFIG_X86_64
201 __vmcs_writel(field+1, value >> 32);
205 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
208 if (static_branch_unlikely(&enable_evmcs))
209 return evmcs_write64(field, value);
211 __vmcs_writel(field, value);
214 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
216 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
217 "vmcs_clear_bits does not support 64-bit fields");
218 if (static_branch_unlikely(&enable_evmcs))
219 return evmcs_write32(field, evmcs_read32(field) & ~mask);
221 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
224 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
226 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
227 "vmcs_set_bits does not support 64-bit fields");
228 if (static_branch_unlikely(&enable_evmcs))
229 return evmcs_write32(field, evmcs_read32(field) | mask);
231 __vmcs_writel(field, __vmcs_readl(field) | mask);
234 static inline void vmcs_clear(struct vmcs *vmcs)
236 u64 phys_addr = __pa(vmcs);
238 vmx_asm1(vmclear, "m"(phys_addr), vmcs, phys_addr);
241 static inline void vmcs_load(struct vmcs *vmcs)
243 u64 phys_addr = __pa(vmcs);
245 if (static_branch_unlikely(&enable_evmcs))
246 return evmcs_load(phys_addr);
248 vmx_asm1(vmptrld, "m"(phys_addr), vmcs, phys_addr);
251 static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
257 } operand = { vpid, 0, gva };
259 vmx_asm2(invvpid, "r"(ext), "m"(operand), ext, vpid, gva);
262 static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
266 } operand = {eptp, gpa};
268 vmx_asm2(invept, "r"(ext), "m"(operand), ext, eptp, gpa);
271 static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
276 if (cpu_has_vmx_invvpid_individual_addr()) {
277 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
284 static inline void vpid_sync_vcpu_single(int vpid)
289 if (cpu_has_vmx_invvpid_single())
290 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
293 static inline void vpid_sync_vcpu_global(void)
295 if (cpu_has_vmx_invvpid_global())
296 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
299 static inline void vpid_sync_context(int vpid)
301 if (cpu_has_vmx_invvpid_single())
302 vpid_sync_vcpu_single(vpid);
304 vpid_sync_vcpu_global();
307 static inline void ept_sync_global(void)
309 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
312 static inline void ept_sync_context(u64 eptp)
314 if (cpu_has_vmx_invept_context())
315 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
320 #endif /* __KVM_X86_VMX_INSN_H */