Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | #include <linux/errno.h> |
| 4 | #include <linux/smp.h> |
| 5 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 6 | #include "../hyperv.h" |
Paolo Bonzini | 2509199 | 2020-04-17 10:34:29 -0400 | [diff] [blame] | 7 | #include "../cpuid.h" |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 8 | #include "evmcs.h" |
| 9 | #include "vmcs.h" |
| 10 | #include "vmx.h" |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 11 | #include "trace.h" |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 12 | |
| 13 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 14 | |
| 15 | #if IS_ENABLED(CONFIG_HYPERV) |
| 16 | |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 17 | #define EVMCS1_OFFSET(x) offsetof(struct hv_enlightened_vmcs, x) |
| 18 | #define EVMCS1_FIELD(number, name, clean_field)[ROL16(number, 6)] = \ |
| 19 | {EVMCS1_OFFSET(name), clean_field} |
| 20 | |
| 21 | const struct evmcs_field vmcs_field_to_evmcs_1[] = { |
| 22 | /* 64 bit rw */ |
| 23 | EVMCS1_FIELD(GUEST_RIP, guest_rip, |
| 24 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 25 | EVMCS1_FIELD(GUEST_RSP, guest_rsp, |
| 26 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC), |
| 27 | EVMCS1_FIELD(GUEST_RFLAGS, guest_rflags, |
| 28 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC), |
| 29 | EVMCS1_FIELD(HOST_IA32_PAT, host_ia32_pat, |
| 30 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 31 | EVMCS1_FIELD(HOST_IA32_EFER, host_ia32_efer, |
| 32 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 33 | EVMCS1_FIELD(HOST_CR0, host_cr0, |
| 34 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 35 | EVMCS1_FIELD(HOST_CR3, host_cr3, |
| 36 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 37 | EVMCS1_FIELD(HOST_CR4, host_cr4, |
| 38 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 39 | EVMCS1_FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp, |
| 40 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 41 | EVMCS1_FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip, |
| 42 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 43 | EVMCS1_FIELD(HOST_RIP, host_rip, |
| 44 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 45 | EVMCS1_FIELD(IO_BITMAP_A, io_bitmap_a, |
| 46 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP), |
| 47 | EVMCS1_FIELD(IO_BITMAP_B, io_bitmap_b, |
| 48 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP), |
| 49 | EVMCS1_FIELD(MSR_BITMAP, msr_bitmap, |
| 50 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP), |
| 51 | EVMCS1_FIELD(GUEST_ES_BASE, guest_es_base, |
| 52 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 53 | EVMCS1_FIELD(GUEST_CS_BASE, guest_cs_base, |
| 54 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 55 | EVMCS1_FIELD(GUEST_SS_BASE, guest_ss_base, |
| 56 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 57 | EVMCS1_FIELD(GUEST_DS_BASE, guest_ds_base, |
| 58 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 59 | EVMCS1_FIELD(GUEST_FS_BASE, guest_fs_base, |
| 60 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 61 | EVMCS1_FIELD(GUEST_GS_BASE, guest_gs_base, |
| 62 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 63 | EVMCS1_FIELD(GUEST_LDTR_BASE, guest_ldtr_base, |
| 64 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 65 | EVMCS1_FIELD(GUEST_TR_BASE, guest_tr_base, |
| 66 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 67 | EVMCS1_FIELD(GUEST_GDTR_BASE, guest_gdtr_base, |
| 68 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 69 | EVMCS1_FIELD(GUEST_IDTR_BASE, guest_idtr_base, |
| 70 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 71 | EVMCS1_FIELD(TSC_OFFSET, tsc_offset, |
| 72 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2), |
| 73 | EVMCS1_FIELD(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr, |
| 74 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2), |
| 75 | EVMCS1_FIELD(VMCS_LINK_POINTER, vmcs_link_pointer, |
| 76 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 77 | EVMCS1_FIELD(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl, |
| 78 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 79 | EVMCS1_FIELD(GUEST_IA32_PAT, guest_ia32_pat, |
| 80 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 81 | EVMCS1_FIELD(GUEST_IA32_EFER, guest_ia32_efer, |
| 82 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 83 | EVMCS1_FIELD(GUEST_PDPTR0, guest_pdptr0, |
| 84 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 85 | EVMCS1_FIELD(GUEST_PDPTR1, guest_pdptr1, |
| 86 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 87 | EVMCS1_FIELD(GUEST_PDPTR2, guest_pdptr2, |
| 88 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 89 | EVMCS1_FIELD(GUEST_PDPTR3, guest_pdptr3, |
| 90 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 91 | EVMCS1_FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions, |
| 92 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 93 | EVMCS1_FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp, |
| 94 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 95 | EVMCS1_FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip, |
| 96 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 97 | EVMCS1_FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask, |
| 98 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 99 | EVMCS1_FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask, |
| 100 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 101 | EVMCS1_FIELD(CR0_READ_SHADOW, cr0_read_shadow, |
| 102 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 103 | EVMCS1_FIELD(CR4_READ_SHADOW, cr4_read_shadow, |
| 104 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 105 | EVMCS1_FIELD(GUEST_CR0, guest_cr0, |
| 106 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 107 | EVMCS1_FIELD(GUEST_CR3, guest_cr3, |
| 108 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 109 | EVMCS1_FIELD(GUEST_CR4, guest_cr4, |
| 110 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 111 | EVMCS1_FIELD(GUEST_DR7, guest_dr7, |
| 112 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR), |
| 113 | EVMCS1_FIELD(HOST_FS_BASE, host_fs_base, |
| 114 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER), |
| 115 | EVMCS1_FIELD(HOST_GS_BASE, host_gs_base, |
| 116 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER), |
| 117 | EVMCS1_FIELD(HOST_TR_BASE, host_tr_base, |
| 118 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER), |
| 119 | EVMCS1_FIELD(HOST_GDTR_BASE, host_gdtr_base, |
| 120 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER), |
| 121 | EVMCS1_FIELD(HOST_IDTR_BASE, host_idtr_base, |
| 122 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER), |
| 123 | EVMCS1_FIELD(HOST_RSP, host_rsp, |
| 124 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER), |
| 125 | EVMCS1_FIELD(EPT_POINTER, ept_pointer, |
| 126 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT), |
| 127 | EVMCS1_FIELD(GUEST_BNDCFGS, guest_bndcfgs, |
| 128 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 129 | EVMCS1_FIELD(XSS_EXIT_BITMAP, xss_exit_bitmap, |
| 130 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2), |
| 131 | |
| 132 | /* 64 bit read only */ |
| 133 | EVMCS1_FIELD(GUEST_PHYSICAL_ADDRESS, guest_physical_address, |
| 134 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 135 | EVMCS1_FIELD(EXIT_QUALIFICATION, exit_qualification, |
| 136 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 137 | /* |
| 138 | * Not defined in KVM: |
| 139 | * |
| 140 | * EVMCS1_FIELD(0x00006402, exit_io_instruction_ecx, |
| 141 | * HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE); |
| 142 | * EVMCS1_FIELD(0x00006404, exit_io_instruction_esi, |
| 143 | * HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE); |
| 144 | * EVMCS1_FIELD(0x00006406, exit_io_instruction_esi, |
| 145 | * HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE); |
| 146 | * EVMCS1_FIELD(0x00006408, exit_io_instruction_eip, |
| 147 | * HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE); |
| 148 | */ |
| 149 | EVMCS1_FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address, |
| 150 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 151 | |
| 152 | /* |
| 153 | * No mask defined in the spec as Hyper-V doesn't currently support |
| 154 | * these. Future proof by resetting the whole clean field mask on |
| 155 | * access. |
| 156 | */ |
| 157 | EVMCS1_FIELD(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr, |
| 158 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 159 | EVMCS1_FIELD(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr, |
| 160 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 161 | EVMCS1_FIELD(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr, |
| 162 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 163 | |
| 164 | /* 32 bit rw */ |
| 165 | EVMCS1_FIELD(TPR_THRESHOLD, tpr_threshold, |
| 166 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 167 | EVMCS1_FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info, |
| 168 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC), |
| 169 | EVMCS1_FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control, |
| 170 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC), |
| 171 | EVMCS1_FIELD(EXCEPTION_BITMAP, exception_bitmap, |
| 172 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN), |
| 173 | EVMCS1_FIELD(VM_ENTRY_CONTROLS, vm_entry_controls, |
| 174 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY), |
| 175 | EVMCS1_FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field, |
| 176 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT), |
| 177 | EVMCS1_FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 178 | vm_entry_exception_error_code, |
| 179 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT), |
| 180 | EVMCS1_FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len, |
| 181 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT), |
| 182 | EVMCS1_FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs, |
| 183 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 184 | EVMCS1_FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control, |
| 185 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1), |
| 186 | EVMCS1_FIELD(VM_EXIT_CONTROLS, vm_exit_controls, |
| 187 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1), |
| 188 | EVMCS1_FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control, |
| 189 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1), |
| 190 | EVMCS1_FIELD(GUEST_ES_LIMIT, guest_es_limit, |
| 191 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 192 | EVMCS1_FIELD(GUEST_CS_LIMIT, guest_cs_limit, |
| 193 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 194 | EVMCS1_FIELD(GUEST_SS_LIMIT, guest_ss_limit, |
| 195 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 196 | EVMCS1_FIELD(GUEST_DS_LIMIT, guest_ds_limit, |
| 197 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 198 | EVMCS1_FIELD(GUEST_FS_LIMIT, guest_fs_limit, |
| 199 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 200 | EVMCS1_FIELD(GUEST_GS_LIMIT, guest_gs_limit, |
| 201 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 202 | EVMCS1_FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit, |
| 203 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 204 | EVMCS1_FIELD(GUEST_TR_LIMIT, guest_tr_limit, |
| 205 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 206 | EVMCS1_FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit, |
| 207 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 208 | EVMCS1_FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit, |
| 209 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 210 | EVMCS1_FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes, |
| 211 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 212 | EVMCS1_FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes, |
| 213 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 214 | EVMCS1_FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes, |
| 215 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 216 | EVMCS1_FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes, |
| 217 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 218 | EVMCS1_FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes, |
| 219 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 220 | EVMCS1_FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes, |
| 221 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 222 | EVMCS1_FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes, |
| 223 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 224 | EVMCS1_FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes, |
| 225 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 226 | EVMCS1_FIELD(GUEST_ACTIVITY_STATE, guest_activity_state, |
| 227 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 228 | EVMCS1_FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs, |
| 229 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1), |
| 230 | |
| 231 | /* 32 bit read only */ |
| 232 | EVMCS1_FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error, |
| 233 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 234 | EVMCS1_FIELD(VM_EXIT_REASON, vm_exit_reason, |
| 235 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 236 | EVMCS1_FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info, |
| 237 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 238 | EVMCS1_FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code, |
| 239 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 240 | EVMCS1_FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field, |
| 241 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 242 | EVMCS1_FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code, |
| 243 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 244 | EVMCS1_FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len, |
| 245 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 246 | EVMCS1_FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info, |
| 247 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE), |
| 248 | |
| 249 | /* No mask defined in the spec (not used) */ |
| 250 | EVMCS1_FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask, |
| 251 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 252 | EVMCS1_FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match, |
| 253 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 254 | EVMCS1_FIELD(CR3_TARGET_COUNT, cr3_target_count, |
| 255 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 256 | EVMCS1_FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count, |
| 257 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 258 | EVMCS1_FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count, |
| 259 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 260 | EVMCS1_FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count, |
| 261 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL), |
| 262 | |
| 263 | /* 16 bit rw */ |
| 264 | EVMCS1_FIELD(HOST_ES_SELECTOR, host_es_selector, |
| 265 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 266 | EVMCS1_FIELD(HOST_CS_SELECTOR, host_cs_selector, |
| 267 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 268 | EVMCS1_FIELD(HOST_SS_SELECTOR, host_ss_selector, |
| 269 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 270 | EVMCS1_FIELD(HOST_DS_SELECTOR, host_ds_selector, |
| 271 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 272 | EVMCS1_FIELD(HOST_FS_SELECTOR, host_fs_selector, |
| 273 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 274 | EVMCS1_FIELD(HOST_GS_SELECTOR, host_gs_selector, |
| 275 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 276 | EVMCS1_FIELD(HOST_TR_SELECTOR, host_tr_selector, |
| 277 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1), |
| 278 | EVMCS1_FIELD(GUEST_ES_SELECTOR, guest_es_selector, |
| 279 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 280 | EVMCS1_FIELD(GUEST_CS_SELECTOR, guest_cs_selector, |
| 281 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 282 | EVMCS1_FIELD(GUEST_SS_SELECTOR, guest_ss_selector, |
| 283 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 284 | EVMCS1_FIELD(GUEST_DS_SELECTOR, guest_ds_selector, |
| 285 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 286 | EVMCS1_FIELD(GUEST_FS_SELECTOR, guest_fs_selector, |
| 287 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 288 | EVMCS1_FIELD(GUEST_GS_SELECTOR, guest_gs_selector, |
| 289 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 290 | EVMCS1_FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector, |
| 291 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 292 | EVMCS1_FIELD(GUEST_TR_SELECTOR, guest_tr_selector, |
| 293 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2), |
| 294 | EVMCS1_FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id, |
| 295 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT), |
| 296 | }; |
| 297 | const unsigned int nr_evmcs_1_fields = ARRAY_SIZE(vmcs_field_to_evmcs_1); |
| 298 | |
Vitaly Kuznetsov | 064eedf | 2020-10-14 16:33:46 +0200 | [diff] [blame] | 299 | __init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 300 | { |
| 301 | vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL; |
| 302 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC; |
| 303 | |
| 304 | vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL; |
| 305 | vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL; |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 306 | } |
| 307 | #endif |
| 308 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 309 | bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa) |
| 310 | { |
| 311 | struct hv_vp_assist_page assist_page; |
| 312 | |
| 313 | *evmcs_gpa = -1ull; |
| 314 | |
| 315 | if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page))) |
| 316 | return false; |
| 317 | |
| 318 | if (unlikely(!assist_page.enlighten_vmentry)) |
| 319 | return false; |
| 320 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 321 | if (unlikely(!evmptr_is_valid(assist_page.current_nested_vmcs))) |
| 322 | return false; |
| 323 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 324 | *evmcs_gpa = assist_page.current_nested_vmcs; |
| 325 | |
| 326 | return true; |
| 327 | } |
| 328 | |
Vitaly Kuznetsov | e2e871a | 2018-12-10 18:21:55 +0100 | [diff] [blame] | 329 | uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu) |
| 330 | { |
Paolo Bonzini | 2509199 | 2020-04-17 10:34:29 -0400 | [diff] [blame] | 331 | /* |
| 332 | * vmcs_version represents the range of supported Enlightened VMCS |
| 333 | * versions: lower 8 bits is the minimal version, higher 8 bits is the |
| 334 | * maximum supported version. KVM supports versions from 1 to |
| 335 | * KVM_EVMCS_VERSION. |
| 336 | */ |
| 337 | if (kvm_cpu_cap_get(X86_FEATURE_VMX) && |
Vitaly Kuznetsov | c21d54f | 2020-09-29 17:09:43 +0200 | [diff] [blame] | 338 | (!vcpu || to_vmx(vcpu)->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 2509199 | 2020-04-17 10:34:29 -0400 | [diff] [blame] | 339 | return (KVM_EVMCS_VERSION << 8) | 1; |
Vitaly Kuznetsov | e2e871a | 2018-12-10 18:21:55 +0100 | [diff] [blame] | 340 | |
Paolo Bonzini | 2509199 | 2020-04-17 10:34:29 -0400 | [diff] [blame] | 341 | return 0; |
Vitaly Kuznetsov | e2e871a | 2018-12-10 18:21:55 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Vitaly Kuznetsov | 31de3d2 | 2020-02-05 13:30:33 +0100 | [diff] [blame] | 344 | void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata) |
| 345 | { |
| 346 | u32 ctl_low = (u32)*pdata; |
| 347 | u32 ctl_high = (u32)(*pdata >> 32); |
| 348 | |
| 349 | /* |
| 350 | * Hyper-V 2016 and 2019 try using these features even when eVMCS |
| 351 | * is enabled but there are no corresponding fields. |
| 352 | */ |
| 353 | switch (msr_index) { |
| 354 | case MSR_IA32_VMX_EXIT_CTLS: |
| 355 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Vitaly Kuznetsov | 8d68bad | 2021-09-07 18:35:30 +0200 | [diff] [blame] | 356 | ctl_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL; |
Vitaly Kuznetsov | 31de3d2 | 2020-02-05 13:30:33 +0100 | [diff] [blame] | 357 | break; |
| 358 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 359 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Vitaly Kuznetsov | 8d68bad | 2021-09-07 18:35:30 +0200 | [diff] [blame] | 360 | ctl_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL; |
Vitaly Kuznetsov | 31de3d2 | 2020-02-05 13:30:33 +0100 | [diff] [blame] | 361 | break; |
| 362 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Vitaly Kuznetsov | 8d68bad | 2021-09-07 18:35:30 +0200 | [diff] [blame] | 363 | ctl_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC; |
| 364 | break; |
| 365 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 366 | ctl_high &= ~EVMCS1_UNSUPPORTED_PINCTRL; |
| 367 | break; |
| 368 | case MSR_IA32_VMX_VMFUNC: |
| 369 | ctl_low &= ~EVMCS1_UNSUPPORTED_VMFUNC; |
Vitaly Kuznetsov | 31de3d2 | 2020-02-05 13:30:33 +0100 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | |
| 373 | *pdata = ctl_low | ((u64)ctl_high << 32); |
| 374 | } |
| 375 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 376 | int nested_evmcs_check_controls(struct vmcs12 *vmcs12) |
| 377 | { |
| 378 | int ret = 0; |
| 379 | u32 unsupp_ctl; |
| 380 | |
| 381 | unsupp_ctl = vmcs12->pin_based_vm_exec_control & |
| 382 | EVMCS1_UNSUPPORTED_PINCTRL; |
| 383 | if (unsupp_ctl) { |
| 384 | trace_kvm_nested_vmenter_failed( |
| 385 | "eVMCS: unsupported pin-based VM-execution controls", |
| 386 | unsupp_ctl); |
| 387 | ret = -EINVAL; |
| 388 | } |
| 389 | |
| 390 | unsupp_ctl = vmcs12->secondary_vm_exec_control & |
| 391 | EVMCS1_UNSUPPORTED_2NDEXEC; |
| 392 | if (unsupp_ctl) { |
| 393 | trace_kvm_nested_vmenter_failed( |
| 394 | "eVMCS: unsupported secondary VM-execution controls", |
| 395 | unsupp_ctl); |
| 396 | ret = -EINVAL; |
| 397 | } |
| 398 | |
| 399 | unsupp_ctl = vmcs12->vm_exit_controls & |
| 400 | EVMCS1_UNSUPPORTED_VMEXIT_CTRL; |
| 401 | if (unsupp_ctl) { |
| 402 | trace_kvm_nested_vmenter_failed( |
| 403 | "eVMCS: unsupported VM-exit controls", |
| 404 | unsupp_ctl); |
| 405 | ret = -EINVAL; |
| 406 | } |
| 407 | |
| 408 | unsupp_ctl = vmcs12->vm_entry_controls & |
| 409 | EVMCS1_UNSUPPORTED_VMENTRY_CTRL; |
| 410 | if (unsupp_ctl) { |
| 411 | trace_kvm_nested_vmenter_failed( |
| 412 | "eVMCS: unsupported VM-entry controls", |
| 413 | unsupp_ctl); |
| 414 | ret = -EINVAL; |
| 415 | } |
| 416 | |
| 417 | unsupp_ctl = vmcs12->vm_function_control & EVMCS1_UNSUPPORTED_VMFUNC; |
| 418 | if (unsupp_ctl) { |
| 419 | trace_kvm_nested_vmenter_failed( |
| 420 | "eVMCS: unsupported VM-function controls", |
| 421 | unsupp_ctl); |
| 422 | ret = -EINVAL; |
| 423 | } |
| 424 | |
| 425 | return ret; |
| 426 | } |
| 427 | |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 428 | int nested_enable_evmcs(struct kvm_vcpu *vcpu, |
| 429 | uint16_t *vmcs_version) |
| 430 | { |
| 431 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 3a2f577 | 2019-01-17 18:12:09 +0100 | [diff] [blame] | 432 | |
| 433 | vmx->nested.enlightened_vmcs_enabled = true; |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 434 | |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 435 | if (vmcs_version) |
Vitaly Kuznetsov | e2e871a | 2018-12-10 18:21:55 +0100 | [diff] [blame] | 436 | *vmcs_version = nested_get_evmcs_version(vcpu); |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 437 | |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 438 | return 0; |
| 439 | } |