Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __KVM_X86_VMX_EVMCS_H
0003 #define __KVM_X86_VMX_EVMCS_H
0004 
0005 #include <linux/jump_label.h>
0006 
0007 #include <asm/hyperv-tlfs.h>
0008 #include <asm/mshyperv.h>
0009 #include <asm/vmx.h>
0010 
0011 #include "capabilities.h"
0012 #include "vmcs.h"
0013 #include "vmcs12.h"
0014 
0015 struct vmcs_config;
0016 
0017 DECLARE_STATIC_KEY_FALSE(enable_evmcs);
0018 
0019 #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
0020 
0021 #define KVM_EVMCS_VERSION 1
0022 
0023 /*
0024  * Enlightened VMCSv1 doesn't support these:
0025  *
0026  *  POSTED_INTR_NV                  = 0x00000002,
0027  *  GUEST_INTR_STATUS               = 0x00000810,
0028  *  APIC_ACCESS_ADDR        = 0x00002014,
0029  *  POSTED_INTR_DESC_ADDR           = 0x00002016,
0030  *  EOI_EXIT_BITMAP0                = 0x0000201c,
0031  *  EOI_EXIT_BITMAP1                = 0x0000201e,
0032  *  EOI_EXIT_BITMAP2                = 0x00002020,
0033  *  EOI_EXIT_BITMAP3                = 0x00002022,
0034  *  GUEST_PML_INDEX         = 0x00000812,
0035  *  PML_ADDRESS         = 0x0000200e,
0036  *  VM_FUNCTION_CONTROL             = 0x00002018,
0037  *  EPTP_LIST_ADDRESS               = 0x00002024,
0038  *  VMREAD_BITMAP                   = 0x00002026,
0039  *  VMWRITE_BITMAP                  = 0x00002028,
0040  *
0041  *  TSC_MULTIPLIER                  = 0x00002032,
0042  *  PLE_GAP                         = 0x00004020,
0043  *  PLE_WINDOW                      = 0x00004022,
0044  *  VMX_PREEMPTION_TIMER_VALUE      = 0x0000482E,
0045  *      GUEST_IA32_PERF_GLOBAL_CTRL     = 0x00002808,
0046  *      HOST_IA32_PERF_GLOBAL_CTRL      = 0x00002c04,
0047  *
0048  * Currently unsupported in KVM:
0049  *  GUEST_IA32_RTIT_CTL     = 0x00002814,
0050  */
0051 #define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
0052                     PIN_BASED_VMX_PREEMPTION_TIMER)
0053 #define EVMCS1_UNSUPPORTED_EXEC_CTRL (CPU_BASED_ACTIVATE_TERTIARY_CONTROLS)
0054 #define EVMCS1_UNSUPPORTED_2NDEXEC                  \
0055     (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |             \
0056      SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |          \
0057      SECONDARY_EXEC_APIC_REGISTER_VIRT |                \
0058      SECONDARY_EXEC_ENABLE_PML |                    \
0059      SECONDARY_EXEC_ENABLE_VMFUNC |                 \
0060      SECONDARY_EXEC_SHADOW_VMCS |                   \
0061      SECONDARY_EXEC_TSC_SCALING |                   \
0062      SECONDARY_EXEC_PAUSE_LOOP_EXITING)
0063 #define EVMCS1_UNSUPPORTED_VMEXIT_CTRL                  \
0064     (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |               \
0065      VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
0066 #define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
0067 #define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
0068 
0069 struct evmcs_field {
0070     u16 offset;
0071     u16 clean_field;
0072 };
0073 
0074 extern const struct evmcs_field vmcs_field_to_evmcs_1[];
0075 extern const unsigned int nr_evmcs_1_fields;
0076 
0077 static __always_inline int evmcs_field_offset(unsigned long field,
0078                           u16 *clean_field)
0079 {
0080     unsigned int index = ROL16(field, 6);
0081     const struct evmcs_field *evmcs_field;
0082 
0083     if (unlikely(index >= nr_evmcs_1_fields))
0084         return -ENOENT;
0085 
0086     evmcs_field = &vmcs_field_to_evmcs_1[index];
0087 
0088     /*
0089      * Use offset=0 to detect holes in eVMCS. This offset belongs to
0090      * 'revision_id' but this field has no encoding and is supposed to
0091      * be accessed directly.
0092      */
0093     if (unlikely(!evmcs_field->offset))
0094         return -ENOENT;
0095 
0096     if (clean_field)
0097         *clean_field = evmcs_field->clean_field;
0098 
0099     return evmcs_field->offset;
0100 }
0101 
0102 static inline u64 evmcs_read_any(struct hv_enlightened_vmcs *evmcs,
0103                  unsigned long field, u16 offset)
0104 {
0105     /*
0106      * vmcs12_read_any() doesn't care whether the supplied structure
0107      * is 'struct vmcs12' or 'struct hv_enlightened_vmcs' as it takes
0108      * the exact offset of the required field, use it for convenience
0109      * here.
0110      */
0111     return vmcs12_read_any((void *)evmcs, field, offset);
0112 }
0113 
0114 #if IS_ENABLED(CONFIG_HYPERV)
0115 
0116 static __always_inline int get_evmcs_offset(unsigned long field,
0117                         u16 *clean_field)
0118 {
0119     int offset = evmcs_field_offset(field, clean_field);
0120 
0121     WARN_ONCE(offset < 0, "KVM: accessing unsupported EVMCS field %lx\n",
0122           field);
0123 
0124     return offset;
0125 }
0126 
0127 static __always_inline void evmcs_write64(unsigned long field, u64 value)
0128 {
0129     u16 clean_field;
0130     int offset = get_evmcs_offset(field, &clean_field);
0131 
0132     if (offset < 0)
0133         return;
0134 
0135     *(u64 *)((char *)current_evmcs + offset) = value;
0136 
0137     current_evmcs->hv_clean_fields &= ~clean_field;
0138 }
0139 
0140 static inline void evmcs_write32(unsigned long field, u32 value)
0141 {
0142     u16 clean_field;
0143     int offset = get_evmcs_offset(field, &clean_field);
0144 
0145     if (offset < 0)
0146         return;
0147 
0148     *(u32 *)((char *)current_evmcs + offset) = value;
0149     current_evmcs->hv_clean_fields &= ~clean_field;
0150 }
0151 
0152 static inline void evmcs_write16(unsigned long field, u16 value)
0153 {
0154     u16 clean_field;
0155     int offset = get_evmcs_offset(field, &clean_field);
0156 
0157     if (offset < 0)
0158         return;
0159 
0160     *(u16 *)((char *)current_evmcs + offset) = value;
0161     current_evmcs->hv_clean_fields &= ~clean_field;
0162 }
0163 
0164 static inline u64 evmcs_read64(unsigned long field)
0165 {
0166     int offset = get_evmcs_offset(field, NULL);
0167 
0168     if (offset < 0)
0169         return 0;
0170 
0171     return *(u64 *)((char *)current_evmcs + offset);
0172 }
0173 
0174 static inline u32 evmcs_read32(unsigned long field)
0175 {
0176     int offset = get_evmcs_offset(field, NULL);
0177 
0178     if (offset < 0)
0179         return 0;
0180 
0181     return *(u32 *)((char *)current_evmcs + offset);
0182 }
0183 
0184 static inline u16 evmcs_read16(unsigned long field)
0185 {
0186     int offset = get_evmcs_offset(field, NULL);
0187 
0188     if (offset < 0)
0189         return 0;
0190 
0191     return *(u16 *)((char *)current_evmcs + offset);
0192 }
0193 
0194 static inline void evmcs_touch_msr_bitmap(void)
0195 {
0196     if (unlikely(!current_evmcs))
0197         return;
0198 
0199     if (current_evmcs->hv_enlightenments_control.msr_bitmap)
0200         current_evmcs->hv_clean_fields &=
0201             ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
0202 }
0203 
0204 static inline void evmcs_load(u64 phys_addr)
0205 {
0206     struct hv_vp_assist_page *vp_ap =
0207         hv_get_vp_assist_page(smp_processor_id());
0208 
0209     if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
0210         vp_ap->nested_control.features.directhypercall = 1;
0211     vp_ap->current_nested_vmcs = phys_addr;
0212     vp_ap->enlighten_vmentry = 1;
0213 }
0214 
0215 __init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
0216 #else /* !IS_ENABLED(CONFIG_HYPERV) */
0217 static __always_inline void evmcs_write64(unsigned long field, u64 value) {}
0218 static inline void evmcs_write32(unsigned long field, u32 value) {}
0219 static inline void evmcs_write16(unsigned long field, u16 value) {}
0220 static inline u64 evmcs_read64(unsigned long field) { return 0; }
0221 static inline u32 evmcs_read32(unsigned long field) { return 0; }
0222 static inline u16 evmcs_read16(unsigned long field) { return 0; }
0223 static inline void evmcs_load(u64 phys_addr) {}
0224 static inline void evmcs_touch_msr_bitmap(void) {}
0225 #endif /* IS_ENABLED(CONFIG_HYPERV) */
0226 
0227 #define EVMPTR_INVALID (-1ULL)
0228 #define EVMPTR_MAP_PENDING (-2ULL)
0229 
0230 static inline bool evmptr_is_valid(u64 evmptr)
0231 {
0232     return evmptr != EVMPTR_INVALID && evmptr != EVMPTR_MAP_PENDING;
0233 }
0234 
0235 enum nested_evmptrld_status {
0236     EVMPTRLD_DISABLED,
0237     EVMPTRLD_SUCCEEDED,
0238     EVMPTRLD_VMFAIL,
0239     EVMPTRLD_ERROR,
0240 };
0241 
0242 bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
0243 uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
0244 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
0245             uint16_t *vmcs_version);
0246 void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata);
0247 int nested_evmcs_check_controls(struct vmcs12 *vmcs12);
0248 
0249 #endif /* __KVM_X86_VMX_EVMCS_H */