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>
13 void vmread_error(unsigned long field, bool fault);
14 void vmwrite_error(unsigned long field, unsigned long value);
15 void vmclear_error(struct vmcs *vmcs, u64 phys_addr);
16 void vmptrld_error(struct vmcs *vmcs, u64 phys_addr);
17 void invvpid_error(unsigned long ext, u16 vpid, gva_t gva);
18 void invept_error(unsigned long ext, u64 eptp, gpa_t gpa);
20 #ifndef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
22 * The VMREAD error trampoline _always_ uses the stack to pass parameters, even
23 * for 64-bit targets. Preserving all registers allows the VMREAD inline asm
24 * blob to avoid clobbering GPRs, which in turn allows the compiler to better
25 * optimize sequences of VMREADs.
27 * Declare the trampoline as an opaque label as it's not safe to call from C
28 * code; there is no way to tell the compiler to pass params on the stack for
31 * void vmread_error_trampoline(unsigned long field, bool fault);
33 extern unsigned long vmread_error_trampoline;
36 static __always_inline void vmcs_check16(unsigned long field)
38 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
39 "16-bit accessor invalid for 64-bit field");
40 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
41 "16-bit accessor invalid for 64-bit high field");
42 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
43 "16-bit accessor invalid for 32-bit high field");
44 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
45 "16-bit accessor invalid for natural width field");
48 static __always_inline void vmcs_check32(unsigned long field)
50 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
51 "32-bit accessor invalid for 16-bit field");
52 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
53 "32-bit accessor invalid for 64-bit field");
54 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
55 "32-bit accessor invalid for 64-bit high field");
56 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
57 "32-bit accessor invalid for natural width field");
60 static __always_inline void vmcs_check64(unsigned long field)
62 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
63 "64-bit accessor invalid for 16-bit field");
64 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
65 "64-bit accessor invalid for 64-bit high field");
66 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
67 "64-bit accessor invalid for 32-bit field");
68 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
69 "64-bit accessor invalid for natural width field");
72 static __always_inline void vmcs_checkl(unsigned long field)
74 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
75 "Natural width accessor invalid for 16-bit field");
76 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
77 "Natural width accessor invalid for 64-bit field");
78 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
79 "Natural width accessor invalid for 64-bit high field");
80 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
81 "Natural width accessor invalid for 32-bit field");
84 static __always_inline unsigned long __vmcs_readl(unsigned long field)
88 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
90 asm_volatile_goto("1: vmread %[field], %[output]\n\t"
93 _ASM_EXTABLE(1b, %l[do_exception])
95 : [output] "=r" (value)
98 : do_fail, do_exception);
103 WARN_ONCE(1, KBUILD_MODNAME ": vmread failed: field=%lx\n", field);
104 pr_warn_ratelimited(KBUILD_MODNAME ": vmread failed: field=%lx\n", field);
108 kvm_spurious_fault();
111 #else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
113 asm volatile("1: vmread %2, %1\n\t"
114 ".byte 0x3e\n\t" /* branch taken hint */
118 * VMREAD failed. Push '0' for @fault, push the failing
119 * @field, and bounce through the trampoline to preserve
120 * volatile registers.
126 "call vmread_error_trampoline\n\t"
129 * Unwind the stack. Note, the trampoline zeros out the
130 * memory for @fault so that the result is '0' on error.
136 /* VMREAD faulted. As above, except push '1' for @fault. */
137 _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_ONE_REG, %1)
139 : ASM_CALL_CONSTRAINT, "=&r"(value) : "r"(field) : "cc");
142 #endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */
145 static __always_inline u16 vmcs_read16(unsigned long field)
148 if (static_branch_unlikely(&enable_evmcs))
149 return evmcs_read16(field);
150 return __vmcs_readl(field);
153 static __always_inline u32 vmcs_read32(unsigned long field)
156 if (static_branch_unlikely(&enable_evmcs))
157 return evmcs_read32(field);
158 return __vmcs_readl(field);
161 static __always_inline u64 vmcs_read64(unsigned long field)
164 if (static_branch_unlikely(&enable_evmcs))
165 return evmcs_read64(field);
167 return __vmcs_readl(field);
169 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
173 static __always_inline unsigned long vmcs_readl(unsigned long field)
176 if (static_branch_unlikely(&enable_evmcs))
177 return evmcs_read64(field);
178 return __vmcs_readl(field);
181 #define vmx_asm1(insn, op1, error_args...) \
183 asm_volatile_goto("1: " __stringify(insn) " %0\n\t" \
184 ".byte 0x2e\n\t" /* branch not taken hint */ \
185 "jna %l[error]\n\t" \
186 _ASM_EXTABLE(1b, %l[fault]) \
187 : : op1 : "cc" : error, fault); \
190 instrumentation_begin(); \
191 insn##_error(error_args); \
192 instrumentation_end(); \
195 kvm_spurious_fault(); \
198 #define vmx_asm2(insn, op1, op2, error_args...) \
200 asm_volatile_goto("1: " __stringify(insn) " %1, %0\n\t" \
201 ".byte 0x2e\n\t" /* branch not taken hint */ \
202 "jna %l[error]\n\t" \
203 _ASM_EXTABLE(1b, %l[fault]) \
204 : : op1, op2 : "cc" : error, fault); \
207 instrumentation_begin(); \
208 insn##_error(error_args); \
209 instrumentation_end(); \
212 kvm_spurious_fault(); \
215 static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
217 vmx_asm2(vmwrite, "r"(field), "rm"(value), field, value);
220 static __always_inline void vmcs_write16(unsigned long field, u16 value)
223 if (static_branch_unlikely(&enable_evmcs))
224 return evmcs_write16(field, value);
226 __vmcs_writel(field, value);
229 static __always_inline void vmcs_write32(unsigned long field, u32 value)
232 if (static_branch_unlikely(&enable_evmcs))
233 return evmcs_write32(field, value);
235 __vmcs_writel(field, value);
238 static __always_inline void vmcs_write64(unsigned long field, u64 value)
241 if (static_branch_unlikely(&enable_evmcs))
242 return evmcs_write64(field, value);
244 __vmcs_writel(field, value);
245 #ifndef CONFIG_X86_64
246 __vmcs_writel(field+1, value >> 32);
250 static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
253 if (static_branch_unlikely(&enable_evmcs))
254 return evmcs_write64(field, value);
256 __vmcs_writel(field, value);
259 static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
261 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
262 "vmcs_clear_bits does not support 64-bit fields");
263 if (static_branch_unlikely(&enable_evmcs))
264 return evmcs_write32(field, evmcs_read32(field) & ~mask);
266 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
269 static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
271 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
272 "vmcs_set_bits does not support 64-bit fields");
273 if (static_branch_unlikely(&enable_evmcs))
274 return evmcs_write32(field, evmcs_read32(field) | mask);
276 __vmcs_writel(field, __vmcs_readl(field) | mask);
279 static inline void vmcs_clear(struct vmcs *vmcs)
281 u64 phys_addr = __pa(vmcs);
283 vmx_asm1(vmclear, "m"(phys_addr), vmcs, phys_addr);
286 static inline void vmcs_load(struct vmcs *vmcs)
288 u64 phys_addr = __pa(vmcs);
290 if (static_branch_unlikely(&enable_evmcs))
291 return evmcs_load(phys_addr);
293 vmx_asm1(vmptrld, "m"(phys_addr), vmcs, phys_addr);
296 static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
302 } operand = { vpid, 0, gva };
304 vmx_asm2(invvpid, "r"(ext), "m"(operand), ext, vpid, gva);
307 static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
311 } operand = {eptp, gpa};
313 vmx_asm2(invept, "r"(ext), "m"(operand), ext, eptp, gpa);
316 static inline void vpid_sync_vcpu_single(int vpid)
321 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
324 static inline void vpid_sync_vcpu_global(void)
326 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
329 static inline void vpid_sync_context(int vpid)
331 if (cpu_has_vmx_invvpid_single())
332 vpid_sync_vcpu_single(vpid);
334 vpid_sync_vcpu_global();
337 static inline void vpid_sync_vcpu_addr(int vpid, gva_t addr)
342 if (cpu_has_vmx_invvpid_individual_addr())
343 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
345 vpid_sync_context(vpid);
348 static inline void ept_sync_global(void)
350 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
353 static inline void ept_sync_context(u64 eptp)
355 if (cpu_has_vmx_invept_context())
356 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
361 #endif /* __KVM_X86_VMX_INSN_H */