1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __KVM_X86_VMX_HYPERV_H
3 #define __KVM_X86_VMX_HYPERV_H
5 #include <linux/jump_label.h>
7 #include <asm/hyperv-tlfs.h>
8 #include <asm/mshyperv.h>
11 #include "../hyperv.h"
13 #include "capabilities.h"
19 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
21 #define KVM_EVMCS_VERSION 1
28 extern const struct evmcs_field vmcs_field_to_evmcs_1[];
29 extern const unsigned int nr_evmcs_1_fields;
31 static __always_inline int evmcs_field_offset(unsigned long field,
34 unsigned int index = ROL16(field, 6);
35 const struct evmcs_field *evmcs_field;
37 if (unlikely(index >= nr_evmcs_1_fields))
40 evmcs_field = &vmcs_field_to_evmcs_1[index];
43 * Use offset=0 to detect holes in eVMCS. This offset belongs to
44 * 'revision_id' but this field has no encoding and is supposed to
45 * be accessed directly.
47 if (unlikely(!evmcs_field->offset))
51 *clean_field = evmcs_field->clean_field;
53 return evmcs_field->offset;
56 static inline u64 evmcs_read_any(struct hv_enlightened_vmcs *evmcs,
57 unsigned long field, u16 offset)
60 * vmcs12_read_any() doesn't care whether the supplied structure
61 * is 'struct vmcs12' or 'struct hv_enlightened_vmcs' as it takes
62 * the exact offset of the required field, use it for convenience
65 return vmcs12_read_any((void *)evmcs, field, offset);
68 #if IS_ENABLED(CONFIG_HYPERV)
70 DECLARE_STATIC_KEY_FALSE(__kvm_is_using_evmcs);
72 static __always_inline bool kvm_is_using_evmcs(void)
74 return static_branch_unlikely(&__kvm_is_using_evmcs);
77 static __always_inline int get_evmcs_offset(unsigned long field,
80 int offset = evmcs_field_offset(field, clean_field);
82 WARN_ONCE(offset < 0, "accessing unsupported EVMCS field %lx\n", field);
86 static __always_inline void evmcs_write64(unsigned long field, u64 value)
89 int offset = get_evmcs_offset(field, &clean_field);
94 *(u64 *)((char *)current_evmcs + offset) = value;
96 current_evmcs->hv_clean_fields &= ~clean_field;
99 static __always_inline void evmcs_write32(unsigned long field, u32 value)
102 int offset = get_evmcs_offset(field, &clean_field);
107 *(u32 *)((char *)current_evmcs + offset) = value;
108 current_evmcs->hv_clean_fields &= ~clean_field;
111 static __always_inline void evmcs_write16(unsigned long field, u16 value)
114 int offset = get_evmcs_offset(field, &clean_field);
119 *(u16 *)((char *)current_evmcs + offset) = value;
120 current_evmcs->hv_clean_fields &= ~clean_field;
123 static __always_inline u64 evmcs_read64(unsigned long field)
125 int offset = get_evmcs_offset(field, NULL);
130 return *(u64 *)((char *)current_evmcs + offset);
133 static __always_inline u32 evmcs_read32(unsigned long field)
135 int offset = get_evmcs_offset(field, NULL);
140 return *(u32 *)((char *)current_evmcs + offset);
143 static __always_inline u16 evmcs_read16(unsigned long field)
145 int offset = get_evmcs_offset(field, NULL);
150 return *(u16 *)((char *)current_evmcs + offset);
153 static inline void evmcs_load(u64 phys_addr)
155 struct hv_vp_assist_page *vp_ap =
156 hv_get_vp_assist_page(smp_processor_id());
158 if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
159 vp_ap->nested_control.features.directhypercall = 1;
160 vp_ap->current_nested_vmcs = phys_addr;
161 vp_ap->enlighten_vmentry = 1;
164 void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
165 #else /* !IS_ENABLED(CONFIG_HYPERV) */
166 static __always_inline bool kvm_is_using_evmcs(void) { return false; }
167 static __always_inline void evmcs_write64(unsigned long field, u64 value) {}
168 static __always_inline void evmcs_write32(unsigned long field, u32 value) {}
169 static __always_inline void evmcs_write16(unsigned long field, u16 value) {}
170 static __always_inline u64 evmcs_read64(unsigned long field) { return 0; }
171 static __always_inline u32 evmcs_read32(unsigned long field) { return 0; }
172 static __always_inline u16 evmcs_read16(unsigned long field) { return 0; }
173 static inline void evmcs_load(u64 phys_addr) {}
174 #endif /* IS_ENABLED(CONFIG_HYPERV) */
176 #define EVMPTR_INVALID (-1ULL)
177 #define EVMPTR_MAP_PENDING (-2ULL)
179 static inline bool evmptr_is_valid(u64 evmptr)
181 return evmptr != EVMPTR_INVALID && evmptr != EVMPTR_MAP_PENDING;
184 enum nested_evmptrld_status {
191 u64 nested_get_evmptr(struct kvm_vcpu *vcpu);
192 uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
193 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
194 uint16_t *vmcs_version);
195 void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
196 int nested_evmcs_check_controls(struct vmcs12 *vmcs12);
197 bool nested_evmcs_l2_tlb_flush_enabled(struct kvm_vcpu *vcpu);
198 void vmx_hv_inject_synthetic_vmexit_post_tlb_flush(struct kvm_vcpu *vcpu);
200 #endif /* __KVM_X86_VMX_HYPERV_H */