Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | #include <linux/frame.h> |
| 4 | #include <linux/percpu.h> |
| 5 | |
| 6 | #include <asm/debugreg.h> |
| 7 | #include <asm/mmu_context.h> |
| 8 | |
| 9 | #include "cpuid.h" |
| 10 | #include "hyperv.h" |
| 11 | #include "mmu.h" |
| 12 | #include "nested.h" |
| 13 | #include "trace.h" |
| 14 | #include "x86.h" |
| 15 | |
| 16 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 17 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
| 18 | |
| 19 | static bool __read_mostly nested_early_check = 0; |
| 20 | module_param(nested_early_check, bool, S_IRUGO); |
| 21 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 22 | /* |
| 23 | * Hyper-V requires all of these, so mark them as supported even though |
| 24 | * they are just treated the same as all-context. |
| 25 | */ |
| 26 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 27 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 28 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 29 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 30 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 31 | |
| 32 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 33 | |
| 34 | enum { |
| 35 | VMX_VMREAD_BITMAP, |
| 36 | VMX_VMWRITE_BITMAP, |
| 37 | VMX_BITMAP_NR |
| 38 | }; |
| 39 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 40 | |
| 41 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 42 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
| 43 | |
| 44 | static u16 shadow_read_only_fields[] = { |
| 45 | #define SHADOW_FIELD_RO(x) x, |
| 46 | #include "vmcs_shadow_fields.h" |
| 47 | }; |
| 48 | static int max_shadow_read_only_fields = |
| 49 | ARRAY_SIZE(shadow_read_only_fields); |
| 50 | |
| 51 | static u16 shadow_read_write_fields[] = { |
| 52 | #define SHADOW_FIELD_RW(x) x, |
| 53 | #include "vmcs_shadow_fields.h" |
| 54 | }; |
| 55 | static int max_shadow_read_write_fields = |
| 56 | ARRAY_SIZE(shadow_read_write_fields); |
| 57 | |
Yi Wang | 8997f65 | 2019-01-21 15:27:05 +0800 | [diff] [blame] | 58 | static void init_vmcs_shadow_fields(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 59 | { |
| 60 | int i, j; |
| 61 | |
| 62 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 63 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 64 | |
| 65 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 66 | u16 field = shadow_read_only_fields[i]; |
| 67 | |
| 68 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 69 | (i + 1 == max_shadow_read_only_fields || |
| 70 | shadow_read_only_fields[i + 1] != field + 1)) |
| 71 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 72 | field + 1); |
| 73 | |
| 74 | clear_bit(field, vmx_vmread_bitmap); |
| 75 | #ifdef CONFIG_X86_64 |
| 76 | if (field & 1) |
| 77 | continue; |
| 78 | #endif |
| 79 | if (j < i) |
| 80 | shadow_read_only_fields[j] = field; |
| 81 | j++; |
| 82 | } |
| 83 | max_shadow_read_only_fields = j; |
| 84 | |
| 85 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 86 | u16 field = shadow_read_write_fields[i]; |
| 87 | |
| 88 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 89 | (i + 1 == max_shadow_read_write_fields || |
| 90 | shadow_read_write_fields[i + 1] != field + 1)) |
| 91 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 92 | field + 1); |
| 93 | |
| 94 | /* |
| 95 | * PML and the preemption timer can be emulated, but the |
| 96 | * processor cannot vmwrite to fields that don't exist |
| 97 | * on bare metal. |
| 98 | */ |
| 99 | switch (field) { |
| 100 | case GUEST_PML_INDEX: |
| 101 | if (!cpu_has_vmx_pml()) |
| 102 | continue; |
| 103 | break; |
| 104 | case VMX_PREEMPTION_TIMER_VALUE: |
| 105 | if (!cpu_has_vmx_preemption_timer()) |
| 106 | continue; |
| 107 | break; |
| 108 | case GUEST_INTR_STATUS: |
| 109 | if (!cpu_has_vmx_apicv()) |
| 110 | continue; |
| 111 | break; |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | clear_bit(field, vmx_vmwrite_bitmap); |
| 117 | clear_bit(field, vmx_vmread_bitmap); |
| 118 | #ifdef CONFIG_X86_64 |
| 119 | if (field & 1) |
| 120 | continue; |
| 121 | #endif |
| 122 | if (j < i) |
| 123 | shadow_read_write_fields[j] = field; |
| 124 | j++; |
| 125 | } |
| 126 | max_shadow_read_write_fields = j; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 131 | * set the success or error code of an emulated VMX instruction (as specified |
| 132 | * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated |
| 133 | * instruction. |
| 134 | */ |
| 135 | static int nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 136 | { |
| 137 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 138 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 139 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 140 | return kvm_skip_emulated_instruction(vcpu); |
| 141 | } |
| 142 | |
| 143 | static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 144 | { |
| 145 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 146 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 147 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 148 | | X86_EFLAGS_CF); |
| 149 | return kvm_skip_emulated_instruction(vcpu); |
| 150 | } |
| 151 | |
| 152 | static int nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 153 | u32 vm_instruction_error) |
| 154 | { |
| 155 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 156 | |
| 157 | /* |
| 158 | * failValid writes the error number to the current VMCS, which |
| 159 | * can't be done if there isn't a current VMCS. |
| 160 | */ |
| 161 | if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs) |
| 162 | return nested_vmx_failInvalid(vcpu); |
| 163 | |
| 164 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 165 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 166 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 167 | | X86_EFLAGS_ZF); |
| 168 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 169 | /* |
| 170 | * We don't need to force a shadow sync because |
| 171 | * VM_INSTRUCTION_ERROR is not shadowed |
| 172 | */ |
| 173 | return kvm_skip_emulated_instruction(vcpu); |
| 174 | } |
| 175 | |
| 176 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 177 | { |
| 178 | /* TODO: not to reset guest simply here. */ |
| 179 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 180 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
| 181 | } |
| 182 | |
| 183 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 184 | { |
| 185 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 186 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 187 | } |
| 188 | |
| 189 | static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) |
| 190 | { |
| 191 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 192 | |
| 193 | if (!vmx->nested.hv_evmcs) |
| 194 | return; |
| 195 | |
| 196 | kunmap(vmx->nested.hv_evmcs_page); |
| 197 | kvm_release_page_dirty(vmx->nested.hv_evmcs_page); |
| 198 | vmx->nested.hv_evmcs_vmptr = -1ull; |
| 199 | vmx->nested.hv_evmcs_page = NULL; |
| 200 | vmx->nested.hv_evmcs = NULL; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 205 | * just stops using VMX. |
| 206 | */ |
| 207 | static void free_nested(struct kvm_vcpu *vcpu) |
| 208 | { |
| 209 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 210 | |
| 211 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
| 212 | return; |
| 213 | |
Peter Shier | ecec768 | 2018-10-11 11:46:46 -0700 | [diff] [blame] | 214 | hrtimer_cancel(&vmx->nested.preemption_timer); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 215 | vmx->nested.vmxon = false; |
| 216 | vmx->nested.smm.vmxon = false; |
| 217 | free_vpid(vmx->nested.vpid02); |
| 218 | vmx->nested.posted_intr_nv = -1; |
| 219 | vmx->nested.current_vmptr = -1ull; |
| 220 | if (enable_shadow_vmcs) { |
| 221 | vmx_disable_shadow_vmcs(vmx); |
| 222 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 223 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 224 | vmx->vmcs01.shadow_vmcs = NULL; |
| 225 | } |
| 226 | kfree(vmx->nested.cached_vmcs12); |
| 227 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 228 | /* Unpin physical memory we referred to in the vmcs02 */ |
| 229 | if (vmx->nested.apic_access_page) { |
| 230 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
| 231 | vmx->nested.apic_access_page = NULL; |
| 232 | } |
| 233 | if (vmx->nested.virtual_apic_page) { |
| 234 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
| 235 | vmx->nested.virtual_apic_page = NULL; |
| 236 | } |
| 237 | if (vmx->nested.pi_desc_page) { |
| 238 | kunmap(vmx->nested.pi_desc_page); |
| 239 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
| 240 | vmx->nested.pi_desc_page = NULL; |
| 241 | vmx->nested.pi_desc = NULL; |
| 242 | } |
| 243 | |
| 244 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 245 | |
| 246 | nested_release_evmcs(vcpu); |
| 247 | |
| 248 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 249 | } |
| 250 | |
| 251 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
| 252 | { |
| 253 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 254 | int cpu; |
| 255 | |
| 256 | if (vmx->loaded_vmcs == vmcs) |
| 257 | return; |
| 258 | |
| 259 | cpu = get_cpu(); |
| 260 | vmx_vcpu_put(vcpu); |
| 261 | vmx->loaded_vmcs = vmcs; |
| 262 | vmx_vcpu_load(vcpu, cpu); |
| 263 | put_cpu(); |
| 264 | |
| 265 | vm_entry_controls_reset_shadow(vmx); |
| 266 | vm_exit_controls_reset_shadow(vmx); |
| 267 | vmx_segment_cache_clear(vmx); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Ensure that the current vmcs of the logical processor is the |
| 272 | * vmcs01 of the vcpu before calling free_nested(). |
| 273 | */ |
| 274 | void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 275 | { |
| 276 | vcpu_load(vcpu); |
| 277 | vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01); |
| 278 | free_nested(vcpu); |
| 279 | vcpu_put(vcpu); |
| 280 | } |
| 281 | |
| 282 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 283 | struct x86_exception *fault) |
| 284 | { |
| 285 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 286 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 287 | u32 exit_reason; |
| 288 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 289 | |
| 290 | if (vmx->nested.pml_full) { |
| 291 | exit_reason = EXIT_REASON_PML_FULL; |
| 292 | vmx->nested.pml_full = false; |
| 293 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 294 | } else if (fault->error_code & PFERR_RSVD_MASK) |
| 295 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
| 296 | else |
| 297 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 298 | |
| 299 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
| 300 | vmcs12->guest_physical_address = fault->address; |
| 301 | } |
| 302 | |
| 303 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 304 | { |
| 305 | WARN_ON(mmu_is_nested(vcpu)); |
| 306 | |
| 307 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
| 308 | kvm_init_shadow_ept_mmu(vcpu, |
| 309 | to_vmx(vcpu)->nested.msrs.ept_caps & |
| 310 | VMX_EPT_EXECUTE_ONLY_BIT, |
| 311 | nested_ept_ad_enabled(vcpu), |
| 312 | nested_ept_get_cr3(vcpu)); |
| 313 | vcpu->arch.mmu->set_cr3 = vmx_set_cr3; |
| 314 | vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3; |
| 315 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 316 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 317 | |
| 318 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 319 | } |
| 320 | |
| 321 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 322 | { |
| 323 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 324 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 325 | } |
| 326 | |
| 327 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 328 | u16 error_code) |
| 329 | { |
| 330 | bool inequality, bit; |
| 331 | |
| 332 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 333 | inequality = |
| 334 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 335 | vmcs12->page_fault_error_code_match; |
| 336 | return inequality ^ bit; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /* |
| 341 | * KVM wants to inject page-faults which it got to the guest. This function |
| 342 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 343 | */ |
| 344 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 345 | { |
| 346 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 347 | unsigned int nr = vcpu->arch.exception.nr; |
| 348 | bool has_payload = vcpu->arch.exception.has_payload; |
| 349 | unsigned long payload = vcpu->arch.exception.payload; |
| 350 | |
| 351 | if (nr == PF_VECTOR) { |
| 352 | if (vcpu->arch.exception.nested_apf) { |
| 353 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 354 | return 1; |
| 355 | } |
| 356 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 357 | vcpu->arch.exception.error_code)) { |
| 358 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 359 | return 1; |
| 360 | } |
| 361 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 362 | if (nr == DB_VECTOR) { |
| 363 | if (!has_payload) { |
| 364 | payload = vcpu->arch.dr6; |
| 365 | payload &= ~(DR6_FIXED_1 | DR6_BT); |
| 366 | payload ^= DR6_RTM; |
| 367 | } |
| 368 | *exit_qual = payload; |
| 369 | } else |
| 370 | *exit_qual = 0; |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 379 | struct x86_exception *fault) |
| 380 | { |
| 381 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 382 | |
| 383 | WARN_ON(!is_guest_mode(vcpu)); |
| 384 | |
| 385 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 386 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 387 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 388 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 389 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 390 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 391 | fault->address); |
| 392 | } else { |
| 393 | kvm_inject_page_fault(vcpu, fault); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 398 | { |
| 399 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 400 | } |
| 401 | |
| 402 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 403 | struct vmcs12 *vmcs12) |
| 404 | { |
| 405 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 406 | return 0; |
| 407 | |
| 408 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 409 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 416 | struct vmcs12 *vmcs12) |
| 417 | { |
| 418 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 419 | return 0; |
| 420 | |
| 421 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
| 422 | return -EINVAL; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 428 | struct vmcs12 *vmcs12) |
| 429 | { |
| 430 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 431 | return 0; |
| 432 | |
| 433 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 434 | return -EINVAL; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 441 | */ |
| 442 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 443 | { |
| 444 | unsigned long *msr_bitmap; |
| 445 | int f = sizeof(unsigned long); |
| 446 | |
| 447 | if (!cpu_has_vmx_msr_bitmap()) |
| 448 | return true; |
| 449 | |
| 450 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 451 | |
| 452 | if (msr <= 0x1fff) { |
| 453 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 454 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 455 | msr &= 0x1fff; |
| 456 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 457 | } |
| 458 | |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 464 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 465 | */ |
| 466 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 467 | unsigned long *msr_bitmap_nested, |
| 468 | u32 msr, int type) |
| 469 | { |
| 470 | int f = sizeof(unsigned long); |
| 471 | |
| 472 | /* |
| 473 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 474 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 475 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 476 | */ |
| 477 | if (msr <= 0x1fff) { |
| 478 | if (type & MSR_TYPE_R && |
| 479 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 480 | /* read-low */ |
| 481 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 482 | |
| 483 | if (type & MSR_TYPE_W && |
| 484 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 485 | /* write-low */ |
| 486 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 487 | |
| 488 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 489 | msr &= 0x1fff; |
| 490 | if (type & MSR_TYPE_R && |
| 491 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 492 | /* read-high */ |
| 493 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 494 | |
| 495 | if (type & MSR_TYPE_W && |
| 496 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 497 | /* write-high */ |
| 498 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 499 | |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 505 | * we do not use the hardware. |
| 506 | */ |
| 507 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 508 | struct vmcs12 *vmcs12) |
| 509 | { |
| 510 | int msr; |
| 511 | struct page *page; |
| 512 | unsigned long *msr_bitmap_l1; |
| 513 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
| 514 | /* |
| 515 | * pred_cmd & spec_ctrl are trying to verify two things: |
| 516 | * |
| 517 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 518 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 519 | * from the L12 MSR bitmap that is too permissive. |
| 520 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 521 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 522 | * works properly because we only update the L01 MSR bitmap lazily. |
| 523 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 524 | * updated to reflect this when L1 (or its L2s) actually write to |
| 525 | * the MSR. |
| 526 | */ |
| 527 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 528 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
| 529 | |
| 530 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 531 | if (!cpu_has_vmx_msr_bitmap() || |
| 532 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 533 | return false; |
| 534 | |
| 535 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 536 | !pred_cmd && !spec_ctrl) |
| 537 | return false; |
| 538 | |
| 539 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 540 | if (is_error_page(page)) |
| 541 | return false; |
| 542 | |
| 543 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
| 544 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 545 | /* |
| 546 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 547 | * just lets the processor take the value from the virtual-APIC page; |
| 548 | * take those 256 bits directly from the L1 bitmap. |
| 549 | */ |
| 550 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 551 | unsigned word = msr / BITS_PER_LONG; |
| 552 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 553 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 554 | } |
| 555 | } else { |
| 556 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 557 | unsigned word = msr / BITS_PER_LONG; |
| 558 | msr_bitmap_l0[word] = ~0; |
| 559 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | nested_vmx_disable_intercept_for_msr( |
| 564 | msr_bitmap_l1, msr_bitmap_l0, |
| 565 | X2APIC_MSR(APIC_TASKPRI), |
| 566 | MSR_TYPE_W); |
| 567 | |
| 568 | if (nested_cpu_has_vid(vmcs12)) { |
| 569 | nested_vmx_disable_intercept_for_msr( |
| 570 | msr_bitmap_l1, msr_bitmap_l0, |
| 571 | X2APIC_MSR(APIC_EOI), |
| 572 | MSR_TYPE_W); |
| 573 | nested_vmx_disable_intercept_for_msr( |
| 574 | msr_bitmap_l1, msr_bitmap_l0, |
| 575 | X2APIC_MSR(APIC_SELF_IPI), |
| 576 | MSR_TYPE_W); |
| 577 | } |
| 578 | |
| 579 | if (spec_ctrl) |
| 580 | nested_vmx_disable_intercept_for_msr( |
| 581 | msr_bitmap_l1, msr_bitmap_l0, |
| 582 | MSR_IA32_SPEC_CTRL, |
| 583 | MSR_TYPE_R | MSR_TYPE_W); |
| 584 | |
| 585 | if (pred_cmd) |
| 586 | nested_vmx_disable_intercept_for_msr( |
| 587 | msr_bitmap_l1, msr_bitmap_l0, |
| 588 | MSR_IA32_PRED_CMD, |
| 589 | MSR_TYPE_W); |
| 590 | |
| 591 | kunmap(page); |
| 592 | kvm_release_page_clean(page); |
| 593 | |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 598 | struct vmcs12 *vmcs12) |
| 599 | { |
| 600 | struct vmcs12 *shadow; |
| 601 | struct page *page; |
| 602 | |
| 603 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 604 | vmcs12->vmcs_link_pointer == -1ull) |
| 605 | return; |
| 606 | |
| 607 | shadow = get_shadow_vmcs12(vcpu); |
| 608 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 609 | |
| 610 | memcpy(shadow, kmap(page), VMCS12_SIZE); |
| 611 | |
| 612 | kunmap(page); |
| 613 | kvm_release_page_clean(page); |
| 614 | } |
| 615 | |
| 616 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 617 | struct vmcs12 *vmcs12) |
| 618 | { |
| 619 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 620 | |
| 621 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 622 | vmcs12->vmcs_link_pointer == -1ull) |
| 623 | return; |
| 624 | |
| 625 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 626 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * In nested virtualization, check if L1 has set |
| 631 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 632 | */ |
| 633 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 634 | { |
| 635 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 636 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 637 | } |
| 638 | |
| 639 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 640 | { |
| 641 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
| 642 | } |
| 643 | |
| 644 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 645 | struct vmcs12 *vmcs12) |
| 646 | { |
| 647 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 648 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 649 | return -EINVAL; |
| 650 | else |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 655 | struct vmcs12 *vmcs12) |
| 656 | { |
| 657 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 658 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 659 | !nested_cpu_has_vid(vmcs12) && |
| 660 | !nested_cpu_has_posted_intr(vmcs12)) |
| 661 | return 0; |
| 662 | |
| 663 | /* |
| 664 | * If virtualize x2apic mode is enabled, |
| 665 | * virtualize apic access must be disabled. |
| 666 | */ |
| 667 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 668 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | /* |
| 672 | * If virtual interrupt delivery is enabled, |
| 673 | * we must exit on external interrupts. |
| 674 | */ |
| 675 | if (nested_cpu_has_vid(vmcs12) && |
| 676 | !nested_exit_on_intr(vcpu)) |
| 677 | return -EINVAL; |
| 678 | |
| 679 | /* |
| 680 | * bits 15:8 should be zero in posted_intr_nv, |
| 681 | * the descriptor address has been already checked |
| 682 | * in nested_get_vmcs12_pages. |
| 683 | * |
| 684 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 685 | */ |
| 686 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 687 | (!nested_cpu_has_vid(vmcs12) || |
| 688 | !nested_exit_intr_ack_set(vcpu) || |
| 689 | (vmcs12->posted_intr_nv & 0xff00) || |
| 690 | (vmcs12->posted_intr_desc_addr & 0x3f) || |
| 691 | (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))) |
| 692 | return -EINVAL; |
| 693 | |
| 694 | /* tpr shadow is needed by all apicv features. */ |
| 695 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 696 | return -EINVAL; |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 702 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 703 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 704 | int maxphyaddr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 705 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 706 | if (count == 0) |
| 707 | return 0; |
| 708 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 709 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 710 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 711 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 712 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 716 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 717 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 718 | { |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 719 | if (nested_vmx_check_msr_switch(vcpu, vmcs12->vm_exit_msr_load_count, |
| 720 | vmcs12->vm_exit_msr_load_addr) || |
| 721 | nested_vmx_check_msr_switch(vcpu, vmcs12->vm_exit_msr_store_count, |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 722 | vmcs12->vm_exit_msr_store_addr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 723 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 724 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 725 | return 0; |
| 726 | } |
| 727 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 728 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 729 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 730 | { |
| 731 | if (nested_vmx_check_msr_switch(vcpu, vmcs12->vm_entry_msr_load_count, |
| 732 | vmcs12->vm_entry_msr_load_addr)) |
| 733 | return -EINVAL; |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 738 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 739 | struct vmcs12 *vmcs12) |
| 740 | { |
| 741 | if (!nested_cpu_has_pml(vmcs12)) |
| 742 | return 0; |
| 743 | |
| 744 | if (!nested_cpu_has_ept(vmcs12) || |
| 745 | !page_address_valid(vcpu, vmcs12->pml_address)) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 752 | struct vmcs12 *vmcs12) |
| 753 | { |
| 754 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 755 | !nested_cpu_has_ept(vmcs12)) |
| 756 | return -EINVAL; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 761 | struct vmcs12 *vmcs12) |
| 762 | { |
| 763 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 764 | !nested_cpu_has_ept(vmcs12)) |
| 765 | return -EINVAL; |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 770 | struct vmcs12 *vmcs12) |
| 771 | { |
| 772 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 773 | return 0; |
| 774 | |
| 775 | if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) || |
| 776 | !page_address_valid(vcpu, vmcs12->vmwrite_bitmap)) |
| 777 | return -EINVAL; |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 783 | struct vmx_msr_entry *e) |
| 784 | { |
| 785 | /* x2APIC MSR accesses are not allowed */ |
| 786 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
| 787 | return -EINVAL; |
| 788 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 789 | e->index == MSR_IA32_UCODE_REV) |
| 790 | return -EINVAL; |
| 791 | if (e->reserved != 0) |
| 792 | return -EINVAL; |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 797 | struct vmx_msr_entry *e) |
| 798 | { |
| 799 | if (e->index == MSR_FS_BASE || |
| 800 | e->index == MSR_GS_BASE || |
| 801 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 802 | nested_vmx_msr_check_common(vcpu, e)) |
| 803 | return -EINVAL; |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 808 | struct vmx_msr_entry *e) |
| 809 | { |
| 810 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 811 | nested_vmx_msr_check_common(vcpu, e)) |
| 812 | return -EINVAL; |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Load guest's/host's msr at nested entry/exit. |
| 818 | * return 0 for success, entry index for failure. |
| 819 | */ |
| 820 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 821 | { |
| 822 | u32 i; |
| 823 | struct vmx_msr_entry e; |
| 824 | struct msr_data msr; |
| 825 | |
| 826 | msr.host_initiated = false; |
| 827 | for (i = 0; i < count; i++) { |
| 828 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 829 | &e, sizeof(e))) { |
| 830 | pr_debug_ratelimited( |
| 831 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 832 | __func__, i, gpa + i * sizeof(e)); |
| 833 | goto fail; |
| 834 | } |
| 835 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 836 | pr_debug_ratelimited( |
| 837 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 838 | __func__, i, e.index, e.reserved); |
| 839 | goto fail; |
| 840 | } |
| 841 | msr.index = e.index; |
| 842 | msr.data = e.value; |
| 843 | if (kvm_set_msr(vcpu, &msr)) { |
| 844 | pr_debug_ratelimited( |
| 845 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 846 | __func__, i, e.index, e.value); |
| 847 | goto fail; |
| 848 | } |
| 849 | } |
| 850 | return 0; |
| 851 | fail: |
| 852 | return i + 1; |
| 853 | } |
| 854 | |
| 855 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 856 | { |
| 857 | u32 i; |
| 858 | struct vmx_msr_entry e; |
| 859 | |
| 860 | for (i = 0; i < count; i++) { |
| 861 | struct msr_data msr_info; |
| 862 | if (kvm_vcpu_read_guest(vcpu, |
| 863 | gpa + i * sizeof(e), |
| 864 | &e, 2 * sizeof(u32))) { |
| 865 | pr_debug_ratelimited( |
| 866 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 867 | __func__, i, gpa + i * sizeof(e)); |
| 868 | return -EINVAL; |
| 869 | } |
| 870 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
| 871 | pr_debug_ratelimited( |
| 872 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 873 | __func__, i, e.index, e.reserved); |
| 874 | return -EINVAL; |
| 875 | } |
| 876 | msr_info.host_initiated = false; |
| 877 | msr_info.index = e.index; |
| 878 | if (kvm_get_msr(vcpu, &msr_info)) { |
| 879 | pr_debug_ratelimited( |
| 880 | "%s cannot read MSR (%u, 0x%x)\n", |
| 881 | __func__, i, e.index); |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | if (kvm_vcpu_write_guest(vcpu, |
| 885 | gpa + i * sizeof(e) + |
| 886 | offsetof(struct vmx_msr_entry, value), |
| 887 | &msr_info.data, sizeof(msr_info.data))) { |
| 888 | pr_debug_ratelimited( |
| 889 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 890 | __func__, i, e.index, msr_info.data); |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | } |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 898 | { |
| 899 | unsigned long invalid_mask; |
| 900 | |
| 901 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 902 | return (val & invalid_mask) == 0; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 907 | * emulating VM entry into a guest with EPT enabled. |
| 908 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 909 | * is assigned to entry_failure_code on failure. |
| 910 | */ |
| 911 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, |
| 912 | u32 *entry_failure_code) |
| 913 | { |
| 914 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
| 915 | if (!nested_cr3_valid(vcpu, cr3)) { |
| 916 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 917 | return 1; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 922 | * must not be dereferenced. |
| 923 | */ |
| 924 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 925 | !nested_ept) { |
| 926 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 927 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 928 | return 1; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | if (!nested_ept) |
| 934 | kvm_mmu_new_cr3(vcpu, cr3, false); |
| 935 | |
| 936 | vcpu->arch.cr3 = cr3; |
| 937 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 938 | |
| 939 | kvm_init_mmu(vcpu, false); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * Returns if KVM is able to config CPU to tag TLB entries |
| 946 | * populated by L2 differently than TLB entries populated |
| 947 | * by L1. |
| 948 | * |
| 949 | * If L1 uses EPT, then TLB entries are tagged with different EPTP. |
| 950 | * |
| 951 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 952 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 953 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 954 | */ |
| 955 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 956 | { |
| 957 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 958 | |
| 959 | return nested_cpu_has_ept(vmcs12) || |
| 960 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 961 | } |
| 962 | |
| 963 | static u16 nested_get_vpid02(struct kvm_vcpu *vcpu) |
| 964 | { |
| 965 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 966 | |
| 967 | return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid; |
| 968 | } |
| 969 | |
| 970 | |
| 971 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 972 | { |
| 973 | return fixed_bits_valid(control, low, high); |
| 974 | } |
| 975 | |
| 976 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 977 | { |
| 978 | return low | ((u64)high << 32); |
| 979 | } |
| 980 | |
| 981 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 982 | { |
| 983 | superset &= mask; |
| 984 | subset &= mask; |
| 985 | |
| 986 | return (superset | subset) == superset; |
| 987 | } |
| 988 | |
| 989 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 990 | { |
| 991 | const u64 feature_and_reserved = |
| 992 | /* feature (except bit 48; see below) */ |
| 993 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 994 | /* reserved */ |
| 995 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 996 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 997 | |
| 998 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 999 | return -EINVAL; |
| 1000 | |
| 1001 | /* |
| 1002 | * KVM does not emulate a version of VMX that constrains physical |
| 1003 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1004 | */ |
| 1005 | if (data & BIT_ULL(48)) |
| 1006 | return -EINVAL; |
| 1007 | |
| 1008 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1009 | vmx_basic_vmcs_revision_id(data)) |
| 1010 | return -EINVAL; |
| 1011 | |
| 1012 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1013 | return -EINVAL; |
| 1014 | |
| 1015 | vmx->nested.msrs.basic = data; |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int |
| 1020 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1021 | { |
| 1022 | u64 supported; |
| 1023 | u32 *lowp, *highp; |
| 1024 | |
| 1025 | switch (msr_index) { |
| 1026 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1027 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1028 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1029 | break; |
| 1030 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1031 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1032 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1033 | break; |
| 1034 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1035 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1036 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1037 | break; |
| 1038 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1039 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1040 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1041 | break; |
| 1042 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1043 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1044 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1045 | break; |
| 1046 | default: |
| 1047 | BUG(); |
| 1048 | } |
| 1049 | |
| 1050 | supported = vmx_control_msr(*lowp, *highp); |
| 1051 | |
| 1052 | /* Check must-be-1 bits are still 1. */ |
| 1053 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1054 | return -EINVAL; |
| 1055 | |
| 1056 | /* Check must-be-0 bits are still 0. */ |
| 1057 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1058 | return -EINVAL; |
| 1059 | |
| 1060 | *lowp = data; |
| 1061 | *highp = data >> 32; |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1066 | { |
| 1067 | const u64 feature_and_reserved_bits = |
| 1068 | /* feature */ |
| 1069 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1070 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1071 | /* reserved */ |
| 1072 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1073 | u64 vmx_misc; |
| 1074 | |
| 1075 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1076 | vmx->nested.msrs.misc_high); |
| 1077 | |
| 1078 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1079 | return -EINVAL; |
| 1080 | |
| 1081 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1082 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1083 | vmx_misc_preemption_timer_rate(data) != |
| 1084 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1085 | return -EINVAL; |
| 1086 | |
| 1087 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1088 | return -EINVAL; |
| 1089 | |
| 1090 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1091 | return -EINVAL; |
| 1092 | |
| 1093 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1094 | return -EINVAL; |
| 1095 | |
| 1096 | vmx->nested.msrs.misc_low = data; |
| 1097 | vmx->nested.msrs.misc_high = data >> 32; |
| 1098 | |
| 1099 | /* |
| 1100 | * If L1 has read-only VM-exit information fields, use the |
| 1101 | * less permissive vmx_vmwrite_bitmap to specify write |
| 1102 | * permissions for the shadow VMCS. |
| 1103 | */ |
| 1104 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 1105 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 1106 | |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1111 | { |
| 1112 | u64 vmx_ept_vpid_cap; |
| 1113 | |
| 1114 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1115 | vmx->nested.msrs.vpid_caps); |
| 1116 | |
| 1117 | /* Every bit is either reserved or a feature bit. */ |
| 1118 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | vmx->nested.msrs.ept_caps = data; |
| 1122 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1127 | { |
| 1128 | u64 *msr; |
| 1129 | |
| 1130 | switch (msr_index) { |
| 1131 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1132 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1133 | break; |
| 1134 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1135 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1136 | break; |
| 1137 | default: |
| 1138 | BUG(); |
| 1139 | } |
| 1140 | |
| 1141 | /* |
| 1142 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1143 | * must be 1 in the restored value. |
| 1144 | */ |
| 1145 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1146 | return -EINVAL; |
| 1147 | |
| 1148 | *msr = data; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
| 1153 | * Called when userspace is restoring VMX MSRs. |
| 1154 | * |
| 1155 | * Returns 0 on success, non-0 otherwise. |
| 1156 | */ |
| 1157 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1158 | { |
| 1159 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1160 | |
| 1161 | /* |
| 1162 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1163 | * is in VMX operation. |
| 1164 | */ |
| 1165 | if (vmx->nested.vmxon) |
| 1166 | return -EBUSY; |
| 1167 | |
| 1168 | switch (msr_index) { |
| 1169 | case MSR_IA32_VMX_BASIC: |
| 1170 | return vmx_restore_vmx_basic(vmx, data); |
| 1171 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1172 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1173 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1174 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1175 | /* |
| 1176 | * The "non-true" VMX capability MSRs are generated from the |
| 1177 | * "true" MSRs, so we do not support restoring them directly. |
| 1178 | * |
| 1179 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1180 | * should restore the "true" MSRs with the must-be-1 bits |
| 1181 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1182 | * DEFAULT SETTINGS". |
| 1183 | */ |
| 1184 | return -EINVAL; |
| 1185 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1186 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1187 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1188 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1189 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1190 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1191 | case MSR_IA32_VMX_MISC: |
| 1192 | return vmx_restore_vmx_misc(vmx, data); |
| 1193 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1194 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1195 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1196 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1197 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1198 | /* |
| 1199 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1200 | * do not support restoring them directly. |
| 1201 | */ |
| 1202 | return -EINVAL; |
| 1203 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1204 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1205 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1206 | vmx->nested.msrs.vmcs_enum = data; |
| 1207 | return 0; |
| 1208 | default: |
| 1209 | /* |
| 1210 | * The rest of the VMX capability MSRs do not support restore. |
| 1211 | */ |
| 1212 | return -EINVAL; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /* Returns 0 on success, non-0 otherwise. */ |
| 1217 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1218 | { |
| 1219 | switch (msr_index) { |
| 1220 | case MSR_IA32_VMX_BASIC: |
| 1221 | *pdata = msrs->basic; |
| 1222 | break; |
| 1223 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1224 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1225 | *pdata = vmx_control_msr( |
| 1226 | msrs->pinbased_ctls_low, |
| 1227 | msrs->pinbased_ctls_high); |
| 1228 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1229 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1230 | break; |
| 1231 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1232 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1233 | *pdata = vmx_control_msr( |
| 1234 | msrs->procbased_ctls_low, |
| 1235 | msrs->procbased_ctls_high); |
| 1236 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1237 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1238 | break; |
| 1239 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1240 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1241 | *pdata = vmx_control_msr( |
| 1242 | msrs->exit_ctls_low, |
| 1243 | msrs->exit_ctls_high); |
| 1244 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1245 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1246 | break; |
| 1247 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1248 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1249 | *pdata = vmx_control_msr( |
| 1250 | msrs->entry_ctls_low, |
| 1251 | msrs->entry_ctls_high); |
| 1252 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1253 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1254 | break; |
| 1255 | case MSR_IA32_VMX_MISC: |
| 1256 | *pdata = vmx_control_msr( |
| 1257 | msrs->misc_low, |
| 1258 | msrs->misc_high); |
| 1259 | break; |
| 1260 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1261 | *pdata = msrs->cr0_fixed0; |
| 1262 | break; |
| 1263 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1264 | *pdata = msrs->cr0_fixed1; |
| 1265 | break; |
| 1266 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1267 | *pdata = msrs->cr4_fixed0; |
| 1268 | break; |
| 1269 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1270 | *pdata = msrs->cr4_fixed1; |
| 1271 | break; |
| 1272 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1273 | *pdata = msrs->vmcs_enum; |
| 1274 | break; |
| 1275 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1276 | *pdata = vmx_control_msr( |
| 1277 | msrs->secondary_ctls_low, |
| 1278 | msrs->secondary_ctls_high); |
| 1279 | break; |
| 1280 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1281 | *pdata = msrs->ept_caps | |
| 1282 | ((u64)msrs->vpid_caps << 32); |
| 1283 | break; |
| 1284 | case MSR_IA32_VMX_VMFUNC: |
| 1285 | *pdata = msrs->vmfunc_controls; |
| 1286 | break; |
| 1287 | default: |
| 1288 | return 1; |
| 1289 | } |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 1296 | * they have been modified by the L1 guest. Note that the "read-only" |
| 1297 | * VM-exit information fields are actually writable if the vCPU is |
| 1298 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 1299 | */ |
| 1300 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1301 | { |
| 1302 | const u16 *fields[] = { |
| 1303 | shadow_read_write_fields, |
| 1304 | shadow_read_only_fields |
| 1305 | }; |
| 1306 | const int max_fields[] = { |
| 1307 | max_shadow_read_write_fields, |
| 1308 | max_shadow_read_only_fields |
| 1309 | }; |
| 1310 | int i, q; |
| 1311 | unsigned long field; |
| 1312 | u64 field_value; |
| 1313 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
| 1314 | |
| 1315 | preempt_disable(); |
| 1316 | |
| 1317 | vmcs_load(shadow_vmcs); |
| 1318 | |
| 1319 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1320 | for (i = 0; i < max_fields[q]; i++) { |
| 1321 | field = fields[q][i]; |
| 1322 | field_value = __vmcs_readl(field); |
| 1323 | vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value); |
| 1324 | } |
| 1325 | /* |
| 1326 | * Skip the VM-exit information fields if they are read-only. |
| 1327 | */ |
| 1328 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 1329 | break; |
| 1330 | } |
| 1331 | |
| 1332 | vmcs_clear(shadow_vmcs); |
| 1333 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1334 | |
| 1335 | preempt_enable(); |
| 1336 | } |
| 1337 | |
| 1338 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1339 | { |
| 1340 | const u16 *fields[] = { |
| 1341 | shadow_read_write_fields, |
| 1342 | shadow_read_only_fields |
| 1343 | }; |
| 1344 | const int max_fields[] = { |
| 1345 | max_shadow_read_write_fields, |
| 1346 | max_shadow_read_only_fields |
| 1347 | }; |
| 1348 | int i, q; |
| 1349 | unsigned long field; |
| 1350 | u64 field_value = 0; |
| 1351 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
| 1352 | |
| 1353 | vmcs_load(shadow_vmcs); |
| 1354 | |
| 1355 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1356 | for (i = 0; i < max_fields[q]; i++) { |
| 1357 | field = fields[q][i]; |
| 1358 | vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value); |
| 1359 | __vmcs_writel(field, field_value); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | vmcs_clear(shadow_vmcs); |
| 1364 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1365 | } |
| 1366 | |
| 1367 | static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx) |
| 1368 | { |
| 1369 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1370 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1371 | |
| 1372 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1373 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1374 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1375 | |
| 1376 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1377 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1378 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1379 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1380 | vmcs12->guest_interruptibility_info = |
| 1381 | evmcs->guest_interruptibility_info; |
| 1382 | } |
| 1383 | |
| 1384 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1385 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1386 | vmcs12->cpu_based_vm_exec_control = |
| 1387 | evmcs->cpu_based_vm_exec_control; |
| 1388 | } |
| 1389 | |
| 1390 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1391 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1392 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1393 | } |
| 1394 | |
| 1395 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1396 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1397 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1398 | } |
| 1399 | |
| 1400 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1401 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1402 | vmcs12->vm_entry_intr_info_field = |
| 1403 | evmcs->vm_entry_intr_info_field; |
| 1404 | vmcs12->vm_entry_exception_error_code = |
| 1405 | evmcs->vm_entry_exception_error_code; |
| 1406 | vmcs12->vm_entry_instruction_len = |
| 1407 | evmcs->vm_entry_instruction_len; |
| 1408 | } |
| 1409 | |
| 1410 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1411 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1412 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1413 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1414 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1415 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1416 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1417 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1418 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1419 | vmcs12->host_rip = evmcs->host_rip; |
| 1420 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1421 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1422 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1423 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1424 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1425 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1426 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1427 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1428 | } |
| 1429 | |
| 1430 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1431 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1432 | vmcs12->pin_based_vm_exec_control = |
| 1433 | evmcs->pin_based_vm_exec_control; |
| 1434 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1435 | vmcs12->secondary_vm_exec_control = |
| 1436 | evmcs->secondary_vm_exec_control; |
| 1437 | } |
| 1438 | |
| 1439 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1440 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1441 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1442 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1443 | } |
| 1444 | |
| 1445 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1446 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1447 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1448 | } |
| 1449 | |
| 1450 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1451 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1452 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1453 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1454 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1455 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1456 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1457 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1458 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1459 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1460 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1461 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1462 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1463 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1464 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1465 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1466 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1467 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1468 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1469 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1470 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1471 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1472 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1473 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1474 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1475 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1476 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1477 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1478 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1479 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1480 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1481 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1482 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1483 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1484 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1485 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1486 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1487 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1488 | } |
| 1489 | |
| 1490 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1491 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1492 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1493 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1494 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1495 | } |
| 1496 | |
| 1497 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1498 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1499 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1500 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1501 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1502 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1503 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1504 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1505 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1506 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1507 | } |
| 1508 | |
| 1509 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1510 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1511 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1512 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1513 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1514 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1515 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1516 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1517 | } |
| 1518 | |
| 1519 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1520 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1521 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1522 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1523 | } |
| 1524 | |
| 1525 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1526 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1527 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1528 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1529 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1530 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1531 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1532 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1533 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1534 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1535 | vmcs12->guest_pending_dbg_exceptions = |
| 1536 | evmcs->guest_pending_dbg_exceptions; |
| 1537 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1538 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1539 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1540 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1541 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1542 | } |
| 1543 | |
| 1544 | /* |
| 1545 | * Not used? |
| 1546 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1547 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1548 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
| 1549 | * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0; |
| 1550 | * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1; |
| 1551 | * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2; |
| 1552 | * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3; |
| 1553 | * vmcs12->page_fault_error_code_mask = |
| 1554 | * evmcs->page_fault_error_code_mask; |
| 1555 | * vmcs12->page_fault_error_code_match = |
| 1556 | * evmcs->page_fault_error_code_match; |
| 1557 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1558 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1559 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1560 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1561 | */ |
| 1562 | |
| 1563 | /* |
| 1564 | * Read only fields: |
| 1565 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1566 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1567 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1568 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1569 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1570 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1571 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1572 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1573 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1574 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1575 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1576 | * |
| 1577 | * Not present in struct vmcs12: |
| 1578 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1579 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1580 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1581 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1582 | */ |
| 1583 | |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
| 1587 | static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
| 1588 | { |
| 1589 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1590 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1591 | |
| 1592 | /* |
| 1593 | * Should not be changed by KVM: |
| 1594 | * |
| 1595 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1596 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1597 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1598 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1599 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1600 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1601 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1602 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1603 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1604 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1605 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1606 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1607 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1608 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1609 | * evmcs->host_rip = vmcs12->host_rip; |
| 1610 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1611 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1612 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1613 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1614 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1615 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1616 | * evmcs->host_rsp = vmcs12->host_rsp; |
| 1617 | * sync_vmcs12() doesn't read these: |
| 1618 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1619 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1620 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1621 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1622 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1623 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1624 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1625 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
| 1626 | * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0; |
| 1627 | * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1; |
| 1628 | * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2; |
| 1629 | * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3; |
| 1630 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1631 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1632 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1633 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1634 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1635 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1636 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1637 | * evmcs->page_fault_error_code_mask = |
| 1638 | * vmcs12->page_fault_error_code_mask; |
| 1639 | * evmcs->page_fault_error_code_match = |
| 1640 | * vmcs12->page_fault_error_code_match; |
| 1641 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1642 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1643 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1644 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1645 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1646 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1647 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1648 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1649 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1650 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1651 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1652 | * |
| 1653 | * Not present in struct vmcs12: |
| 1654 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1655 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1656 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1657 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1658 | */ |
| 1659 | |
| 1660 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1661 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1662 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1663 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1664 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1665 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1666 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1667 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1668 | |
| 1669 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1670 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1671 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1672 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1673 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1674 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1675 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1676 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1677 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1678 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1679 | |
| 1680 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1681 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1682 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1683 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1684 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1685 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1686 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1687 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1688 | |
| 1689 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1690 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1691 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1692 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1693 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1694 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1695 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1696 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1697 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1698 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1699 | |
| 1700 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1701 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1702 | |
| 1703 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1704 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1705 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1706 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1707 | |
| 1708 | evmcs->guest_pending_dbg_exceptions = |
| 1709 | vmcs12->guest_pending_dbg_exceptions; |
| 1710 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1711 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1712 | |
| 1713 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1714 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1715 | |
| 1716 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1717 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1718 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1719 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1720 | |
| 1721 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1722 | |
| 1723 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1724 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1725 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1726 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1727 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1728 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1729 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1730 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1731 | |
| 1732 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1733 | |
| 1734 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1735 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1736 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1737 | |
| 1738 | evmcs->guest_interruptibility_info = |
| 1739 | vmcs12->guest_interruptibility_info; |
| 1740 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1741 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1742 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1743 | evmcs->vm_entry_exception_error_code = |
| 1744 | vmcs12->vm_entry_exception_error_code; |
| 1745 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1746 | |
| 1747 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1748 | |
| 1749 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1750 | |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1756 | * instruction. |
| 1757 | */ |
| 1758 | static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu, |
| 1759 | bool from_launch) |
| 1760 | { |
| 1761 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1762 | struct hv_vp_assist_page assist_page; |
| 1763 | |
| 1764 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
| 1765 | return 1; |
| 1766 | |
| 1767 | if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page))) |
| 1768 | return 1; |
| 1769 | |
| 1770 | if (unlikely(!assist_page.enlighten_vmentry)) |
| 1771 | return 1; |
| 1772 | |
| 1773 | if (unlikely(assist_page.current_nested_vmcs != |
| 1774 | vmx->nested.hv_evmcs_vmptr)) { |
| 1775 | |
| 1776 | if (!vmx->nested.hv_evmcs) |
| 1777 | vmx->nested.current_vmptr = -1ull; |
| 1778 | |
| 1779 | nested_release_evmcs(vcpu); |
| 1780 | |
| 1781 | vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page( |
| 1782 | vcpu, assist_page.current_nested_vmcs); |
| 1783 | |
| 1784 | if (unlikely(is_error_page(vmx->nested.hv_evmcs_page))) |
| 1785 | return 0; |
| 1786 | |
| 1787 | vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page); |
| 1788 | |
| 1789 | /* |
| 1790 | * Currently, KVM only supports eVMCS version 1 |
| 1791 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 1792 | * value to first u32 field of eVMCS which should specify eVMCS |
| 1793 | * VersionNumber. |
| 1794 | * |
| 1795 | * Guest should be aware of supported eVMCS versions by host by |
| 1796 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 1797 | * expected to set this CPUID leaf according to the value |
| 1798 | * returned in vmcs_version from nested_enable_evmcs(). |
| 1799 | * |
| 1800 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 1801 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 1802 | * just sets first u32 field of eVMCS to revision_id specified |
| 1803 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 1804 | * which is one of the supported versions specified in |
| 1805 | * CPUID.0x4000000A.EAX[0:15]. |
| 1806 | * |
| 1807 | * To overcome Hyper-V bug, we accept here either a supported |
| 1808 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 1809 | * u32 field of eVMCS. |
| 1810 | */ |
| 1811 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 1812 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 1813 | nested_release_evmcs(vcpu); |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | vmx->nested.dirty_vmcs12 = true; |
| 1818 | /* |
| 1819 | * As we keep L2 state for one guest only 'hv_clean_fields' mask |
| 1820 | * can't be used when we switch between them. Reset it here for |
| 1821 | * simplicity. |
| 1822 | */ |
| 1823 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 1824 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 1825 | vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs; |
| 1826 | |
| 1827 | /* |
| 1828 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 1829 | * reloaded from guest's memory (read only fields, fields not |
| 1830 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 1831 | * are no leftovers. |
| 1832 | */ |
| 1833 | if (from_launch) { |
| 1834 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1835 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 1836 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 1837 | } |
| 1838 | |
| 1839 | } |
| 1840 | return 1; |
| 1841 | } |
| 1842 | |
| 1843 | void nested_sync_from_vmcs12(struct kvm_vcpu *vcpu) |
| 1844 | { |
| 1845 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1846 | |
| 1847 | /* |
| 1848 | * hv_evmcs may end up being not mapped after migration (when |
| 1849 | * L2 was running), map it here to make sure vmcs12 changes are |
| 1850 | * properly reflected. |
| 1851 | */ |
| 1852 | if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) |
| 1853 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 1854 | |
| 1855 | if (vmx->nested.hv_evmcs) { |
| 1856 | copy_vmcs12_to_enlightened(vmx); |
| 1857 | /* All fields are clean */ |
| 1858 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 1859 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 1860 | } else { |
| 1861 | copy_vmcs12_to_shadow(vmx); |
| 1862 | } |
| 1863 | |
| 1864 | vmx->nested.need_vmcs12_sync = false; |
| 1865 | } |
| 1866 | |
| 1867 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 1868 | { |
| 1869 | struct vcpu_vmx *vmx = |
| 1870 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 1871 | |
| 1872 | vmx->nested.preemption_timer_expired = true; |
| 1873 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 1874 | kvm_vcpu_kick(&vmx->vcpu); |
| 1875 | |
| 1876 | return HRTIMER_NORESTART; |
| 1877 | } |
| 1878 | |
| 1879 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 1880 | { |
| 1881 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 1882 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1883 | |
| 1884 | /* |
| 1885 | * A timer value of zero is architecturally guaranteed to cause |
| 1886 | * a VMExit prior to executing any instructions in the guest. |
| 1887 | */ |
| 1888 | if (preemption_timeout == 0) { |
| 1889 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 1894 | return; |
| 1895 | |
| 1896 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 1897 | preemption_timeout *= 1000000; |
| 1898 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 1899 | hrtimer_start(&vmx->nested.preemption_timer, |
| 1900 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 1901 | } |
| 1902 | |
| 1903 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 1904 | { |
| 1905 | if (vmx->nested.nested_run_pending && |
| 1906 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 1907 | return vmcs12->guest_ia32_efer; |
| 1908 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 1909 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 1910 | else |
| 1911 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 1912 | } |
| 1913 | |
| 1914 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 1915 | { |
| 1916 | /* |
| 1917 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 1918 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 1919 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 1920 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 1921 | */ |
| 1922 | if (vmx->nested.vmcs02_initialized) |
| 1923 | return; |
| 1924 | vmx->nested.vmcs02_initialized = true; |
| 1925 | |
| 1926 | /* |
| 1927 | * We don't care what the EPTP value is we just need to guarantee |
| 1928 | * it's valid so we don't get a false positive when doing early |
| 1929 | * consistency checks. |
| 1930 | */ |
| 1931 | if (enable_ept && nested_early_check) |
| 1932 | vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0)); |
| 1933 | |
| 1934 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 1935 | if (cpu_has_vmx_vmfunc()) |
| 1936 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 1937 | |
| 1938 | if (cpu_has_vmx_posted_intr()) |
| 1939 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 1940 | |
| 1941 | if (cpu_has_vmx_msr_bitmap()) |
| 1942 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 1943 | |
| 1944 | if (enable_pml) |
| 1945 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 1946 | |
| 1947 | /* |
| 1948 | * Set the MSR load/store lists to match L0's settings. Only the |
| 1949 | * addresses are constant (for vmcs02), the counts can change based |
| 1950 | * on L2's behavior, e.g. switching to/from long mode. |
| 1951 | */ |
| 1952 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 1953 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 1954 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 1955 | |
| 1956 | vmx_set_constant_host_state(vmx); |
| 1957 | } |
| 1958 | |
| 1959 | static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx, |
| 1960 | struct vmcs12 *vmcs12) |
| 1961 | { |
| 1962 | prepare_vmcs02_constant_state(vmx); |
| 1963 | |
| 1964 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 1965 | |
| 1966 | if (enable_vpid) { |
| 1967 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 1968 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 1969 | else |
| 1970 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 1975 | { |
| 1976 | u32 exec_control, vmcs12_exec_ctrl; |
| 1977 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 1978 | |
| 1979 | if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) |
| 1980 | prepare_vmcs02_early_full(vmx, vmcs12); |
| 1981 | |
| 1982 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1983 | * PIN CONTROLS |
| 1984 | */ |
| 1985 | exec_control = vmcs12->pin_based_vm_exec_control; |
| 1986 | |
| 1987 | /* Preemption timer setting is computed directly in vmx_vcpu_run. */ |
| 1988 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 1989 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1990 | vmx->loaded_vmcs->hv_timer_armed = false; |
| 1991 | |
| 1992 | /* Posted interrupts setting is only taken from vmcs12. */ |
| 1993 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 1994 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 1995 | vmx->nested.pi_pending = false; |
| 1996 | } else { |
| 1997 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 1998 | } |
| 1999 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
| 2000 | |
| 2001 | /* |
| 2002 | * EXEC CONTROLS |
| 2003 | */ |
| 2004 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 2005 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2006 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 2007 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2008 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2009 | |
| 2010 | /* |
| 2011 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 2012 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 2013 | * will result in a VM entry failure. |
| 2014 | */ |
| 2015 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 2016 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
| 2017 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
| 2018 | } else { |
| 2019 | #ifdef CONFIG_X86_64 |
| 2020 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2021 | CPU_BASED_CR8_STORE_EXITING; |
| 2022 | #endif |
| 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2027 | * for I/O port accesses. |
| 2028 | */ |
| 2029 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2030 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 2031 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 2032 | |
| 2033 | /* |
| 2034 | * SECONDARY EXEC CONTROLS |
| 2035 | */ |
| 2036 | if (cpu_has_secondary_exec_ctrls()) { |
| 2037 | exec_control = vmx->secondary_exec_control; |
| 2038 | |
| 2039 | /* Take the following fields only from vmcs12 */ |
| 2040 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 2041 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 2042 | SECONDARY_EXEC_RDTSCP | |
| 2043 | SECONDARY_EXEC_XSAVES | |
| 2044 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2045 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 2046 | SECONDARY_EXEC_ENABLE_VMFUNC); |
| 2047 | if (nested_cpu_has(vmcs12, |
| 2048 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 2049 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 2050 | ~SECONDARY_EXEC_ENABLE_PML; |
| 2051 | exec_control |= vmcs12_exec_ctrl; |
| 2052 | } |
| 2053 | |
| 2054 | /* VMCS shadowing for L2 is emulated for now */ |
| 2055 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2056 | |
| 2057 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2058 | vmcs_write16(GUEST_INTR_STATUS, |
| 2059 | vmcs12->guest_intr_status); |
| 2060 | |
| 2061 | /* |
| 2062 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 2063 | * nested_get_vmcs12_pages will either fix it up or |
| 2064 | * remove the VM execution control. |
| 2065 | */ |
| 2066 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 2067 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 2068 | |
| 2069 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2070 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
| 2071 | |
| 2072 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 2073 | } |
| 2074 | |
| 2075 | /* |
| 2076 | * ENTRY CONTROLS |
| 2077 | * |
| 2078 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2079 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2080 | * on the related bits (if supported by the CPU) in the hope that |
| 2081 | * we can avoid VMWrites during vmx_set_efer(). |
| 2082 | */ |
| 2083 | exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) & |
| 2084 | ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER; |
| 2085 | if (cpu_has_load_ia32_efer()) { |
| 2086 | if (guest_efer & EFER_LMA) |
| 2087 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2088 | if (guest_efer != host_efer) |
| 2089 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2090 | } |
| 2091 | vm_entry_controls_init(vmx, exec_control); |
| 2092 | |
| 2093 | /* |
| 2094 | * EXIT CONTROLS |
| 2095 | * |
| 2096 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2097 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2098 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2099 | */ |
| 2100 | exec_control = vmx_vmexit_ctrl(); |
| 2101 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2102 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
| 2103 | vm_exit_controls_init(vmx, exec_control); |
| 2104 | |
| 2105 | /* |
| 2106 | * Conceptually we want to copy the PML address and index from |
| 2107 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 2108 | * since we always flush the log on each vmexit and never change |
| 2109 | * the PML address (once set), this happens to be equivalent to |
| 2110 | * simply resetting the index in vmcs02. |
| 2111 | */ |
| 2112 | if (enable_pml) |
| 2113 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 2114 | |
| 2115 | /* |
| 2116 | * Interrupt/Exception Fields |
| 2117 | */ |
| 2118 | if (vmx->nested.nested_run_pending) { |
| 2119 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2120 | vmcs12->vm_entry_intr_info_field); |
| 2121 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2122 | vmcs12->vm_entry_exception_error_code); |
| 2123 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2124 | vmcs12->vm_entry_instruction_len); |
| 2125 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2126 | vmcs12->guest_interruptibility_info); |
| 2127 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2128 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2129 | } else { |
| 2130 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2135 | { |
| 2136 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2137 | |
| 2138 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2139 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2140 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2141 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2142 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2143 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2144 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2145 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2146 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2147 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2148 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2149 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2150 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2151 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2152 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2153 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2154 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2155 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2156 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2157 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 2158 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2159 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2160 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2161 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2162 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2163 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2164 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2165 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2166 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2167 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2168 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2169 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2170 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2171 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2172 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2173 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 2174 | } |
| 2175 | |
| 2176 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2177 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2178 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2179 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2180 | vmcs12->guest_pending_dbg_exceptions); |
| 2181 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2182 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2183 | |
| 2184 | /* |
| 2185 | * L1 may access the L2's PDPTR, so save them to construct |
| 2186 | * vmcs12 |
| 2187 | */ |
| 2188 | if (enable_ept) { |
| 2189 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2190 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2191 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2192 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2197 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2198 | |
| 2199 | /* |
| 2200 | * Whether page-faults are trapped is determined by a combination of |
| 2201 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 2202 | * If enable_ept, L0 doesn't care about page faults and we should |
| 2203 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 2204 | * care about (at least some) page faults, and because it is not easy |
| 2205 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 2206 | * to exit on each and every L2 page fault. This is done by setting |
| 2207 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 2208 | * Note that below we don't need special code to set EB.PF beyond the |
| 2209 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2210 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2211 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2212 | */ |
| 2213 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 2214 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 2215 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 2216 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 2217 | |
| 2218 | if (cpu_has_vmx_apicv()) { |
| 2219 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2220 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2221 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2222 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2223 | } |
| 2224 | |
| 2225 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2226 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2227 | |
| 2228 | set_cr4_guest_host_mask(vmx); |
| 2229 | |
| 2230 | if (kvm_mpx_supported()) { |
| 2231 | if (vmx->nested.nested_run_pending && |
| 2232 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2233 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 2234 | else |
| 2235 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2241 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2242 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2243 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2244 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2245 | * function also has additional necessary side-effects, like setting various |
| 2246 | * vcpu->arch fields. |
| 2247 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2248 | * is assigned to entry_failure_code on failure. |
| 2249 | */ |
| 2250 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 2251 | u32 *entry_failure_code) |
| 2252 | { |
| 2253 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2254 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2255 | |
| 2256 | if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) { |
| 2257 | prepare_vmcs02_full(vmx, vmcs12); |
| 2258 | vmx->nested.dirty_vmcs12 = false; |
| 2259 | } |
| 2260 | |
| 2261 | /* |
| 2262 | * First, the fields that are shadowed. This must be kept in sync |
| 2263 | * with vmcs_shadow_fields.h. |
| 2264 | */ |
| 2265 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2266 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2267 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2268 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 2269 | } |
| 2270 | |
| 2271 | if (vmx->nested.nested_run_pending && |
| 2272 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2273 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2274 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2275 | } else { |
| 2276 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2277 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2278 | } |
| 2279 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2280 | |
| 2281 | vmx->nested.preemption_timer_expired = false; |
| 2282 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 2283 | vmx_start_preemption_timer(vcpu); |
| 2284 | |
| 2285 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2286 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2287 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2288 | */ |
| 2289 | update_exception_bitmap(vcpu); |
| 2290 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2291 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2292 | |
| 2293 | if (vmx->nested.nested_run_pending && |
| 2294 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2295 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2296 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2297 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2298 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2299 | } |
| 2300 | |
| 2301 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 2302 | |
| 2303 | if (kvm_has_tsc_control) |
| 2304 | decache_tsc_multiplier(vmx); |
| 2305 | |
| 2306 | if (enable_vpid) { |
| 2307 | /* |
| 2308 | * There is no direct mapping between vpid02 and vpid12, the |
| 2309 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 2310 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 2311 | * The vpid12 is allocated by L1 for L2, so it will not |
| 2312 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 2313 | * even if spawn a lot of nested vCPUs. |
| 2314 | */ |
| 2315 | if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) { |
| 2316 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 2317 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 2318 | __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false); |
| 2319 | } |
| 2320 | } else { |
| 2321 | /* |
| 2322 | * If L1 use EPT, then L0 needs to execute INVEPT on |
| 2323 | * EPTP02 instead of EPTP01. Therefore, delay TLB |
| 2324 | * flush until vmcs02->eptp is fully updated by |
| 2325 | * KVM_REQ_LOAD_CR3. Note that this assumes |
| 2326 | * KVM_REQ_TLB_FLUSH is evaluated after |
| 2327 | * KVM_REQ_LOAD_CR3 in vcpu_enter_guest(). |
| 2328 | */ |
| 2329 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | if (nested_cpu_has_ept(vmcs12)) |
| 2334 | nested_ept_init_mmu_context(vcpu); |
| 2335 | else if (nested_cpu_has2(vmcs12, |
| 2336 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2337 | vmx_flush_tlb(vcpu, true); |
| 2338 | |
| 2339 | /* |
| 2340 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2341 | * bits which we consider mandatory enabled. |
| 2342 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2343 | * the specifications by L1; It's not enough to take |
| 2344 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2345 | * have more bits than L1 expected. |
| 2346 | */ |
| 2347 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2348 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2349 | |
| 2350 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2351 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2352 | |
| 2353 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2354 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2355 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2356 | |
| 2357 | /* |
| 2358 | * Guest state is invalid and unrestricted guest is disabled, |
| 2359 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2360 | * Fail the VMEntry. |
| 2361 | */ |
| 2362 | if (vmx->emulation_required) { |
| 2363 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 2364 | return 1; |
| 2365 | } |
| 2366 | |
| 2367 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2368 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
| 2369 | entry_failure_code)) |
| 2370 | return 1; |
| 2371 | |
| 2372 | if (!enable_ept) |
| 2373 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2374 | |
| 2375 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 2376 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 2377 | return 0; |
| 2378 | } |
| 2379 | |
| 2380 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2381 | { |
| 2382 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2383 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 2384 | return -EINVAL; |
| 2385 | |
| 2386 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 2387 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 2388 | return -EINVAL; |
| 2389 | |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 2394 | { |
| 2395 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2396 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 2397 | |
| 2398 | /* Check for memory type validity */ |
| 2399 | switch (address & VMX_EPTP_MT_MASK) { |
| 2400 | case VMX_EPTP_MT_UC: |
| 2401 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
| 2402 | return false; |
| 2403 | break; |
| 2404 | case VMX_EPTP_MT_WB: |
| 2405 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
| 2406 | return false; |
| 2407 | break; |
| 2408 | default: |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
| 2412 | /* only 4 levels page-walk length are valid */ |
| 2413 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
| 2414 | return false; |
| 2415 | |
| 2416 | /* Reserved bits should not be set */ |
| 2417 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 2418 | return false; |
| 2419 | |
| 2420 | /* AD, if set, should be supported */ |
| 2421 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
| 2422 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
| 2423 | return false; |
| 2424 | } |
| 2425 | |
| 2426 | return true; |
| 2427 | } |
| 2428 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2429 | /* |
| 2430 | * Checks related to VM-Execution Control Fields |
| 2431 | */ |
| 2432 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2433 | struct vmcs12 *vmcs12) |
| 2434 | { |
| 2435 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2436 | |
| 2437 | if (!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2438 | vmx->nested.msrs.pinbased_ctls_low, |
| 2439 | vmx->nested.msrs.pinbased_ctls_high) || |
| 2440 | !vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2441 | vmx->nested.msrs.procbased_ctls_low, |
| 2442 | vmx->nested.msrs.procbased_ctls_high)) |
| 2443 | return -EINVAL; |
| 2444 | |
| 2445 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 2446 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2447 | vmx->nested.msrs.secondary_ctls_low, |
| 2448 | vmx->nested.msrs.secondary_ctls_high)) |
| 2449 | return -EINVAL; |
| 2450 | |
| 2451 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu) || |
| 2452 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2453 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2454 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2455 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2456 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2457 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2458 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2459 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2460 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2461 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
| 2462 | (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
| 2463 | return -EINVAL; |
| 2464 | |
| 2465 | if (nested_cpu_has_ept(vmcs12) && |
| 2466 | !valid_ept_address(vcpu, vmcs12->ept_pointer)) |
| 2467 | return -EINVAL; |
| 2468 | |
| 2469 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 2470 | if (vmcs12->vm_function_control & |
| 2471 | ~vmx->nested.msrs.vmfunc_controls) |
| 2472 | return -EINVAL; |
| 2473 | |
| 2474 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 2475 | if (!nested_cpu_has_ept(vmcs12) || |
| 2476 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 2477 | return -EINVAL; |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2484 | /* |
| 2485 | * Checks related to VM-Exit Control Fields |
| 2486 | */ |
| 2487 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2488 | struct vmcs12 *vmcs12) |
| 2489 | { |
| 2490 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2491 | |
| 2492 | if (!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2493 | vmx->nested.msrs.exit_ctls_low, |
| 2494 | vmx->nested.msrs.exit_ctls_high) || |
| 2495 | nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12)) |
| 2496 | return -EINVAL; |
| 2497 | |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2501 | /* |
| 2502 | * Checks related to VM-Entry Control Fields |
| 2503 | */ |
| 2504 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2505 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2506 | { |
| 2507 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2508 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2509 | if (!vmx_control_verify(vmcs12->vm_entry_controls, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2510 | vmx->nested.msrs.entry_ctls_low, |
| 2511 | vmx->nested.msrs.entry_ctls_high)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2512 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2513 | |
| 2514 | /* |
| 2515 | * From the Intel SDM, volume 3: |
| 2516 | * Fields relevant to VM-entry event injection must be set properly. |
| 2517 | * These fields are the VM-entry interruption-information field, the |
| 2518 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2519 | */ |
| 2520 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2521 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2522 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2523 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2524 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2525 | bool should_have_error_code; |
| 2526 | bool urg = nested_cpu_has2(vmcs12, |
| 2527 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2528 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2529 | |
| 2530 | /* VM-entry interruption-info field: interruption type */ |
| 2531 | if (intr_type == INTR_TYPE_RESERVED || |
| 2532 | (intr_type == INTR_TYPE_OTHER_EVENT && |
| 2533 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2534 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2535 | |
| 2536 | /* VM-entry interruption-info field: vector */ |
| 2537 | if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2538 | (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2539 | (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2540 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2541 | |
| 2542 | /* VM-entry interruption-info field: deliver error code */ |
| 2543 | should_have_error_code = |
| 2544 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2545 | x86_exception_has_error_code(vector); |
| 2546 | if (has_error_code != should_have_error_code) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2547 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2548 | |
| 2549 | /* VM-entry exception error code */ |
| 2550 | if (has_error_code && |
| 2551 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2552 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2553 | |
| 2554 | /* VM-entry interruption-info field: reserved bits */ |
| 2555 | if (intr_info & INTR_INFO_RESVD_BITS_MASK) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2556 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2557 | |
| 2558 | /* VM-entry instruction length */ |
| 2559 | switch (intr_type) { |
| 2560 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2561 | case INTR_TYPE_SOFT_INTR: |
| 2562 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
| 2563 | if ((vmcs12->vm_entry_instruction_len > 15) || |
| 2564 | (vmcs12->vm_entry_instruction_len == 0 && |
| 2565 | !nested_cpu_has_zero_length_injection(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2566 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2570 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2571 | return -EINVAL; |
| 2572 | |
| 2573 | return 0; |
| 2574 | } |
| 2575 | |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2576 | /* |
| 2577 | * Checks related to Host Control Registers and MSRs |
| 2578 | */ |
| 2579 | static int nested_check_host_control_regs(struct kvm_vcpu *vcpu, |
| 2580 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2581 | { |
| 2582 | bool ia32e; |
| 2583 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2584 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 2585 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 2586 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2587 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2588 | /* |
| 2589 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2590 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2591 | * the values of the LMA and LME bits in the field must each be that of |
| 2592 | * the host address-space size VM-exit control. |
| 2593 | */ |
| 2594 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 2595 | ia32e = (vmcs12->vm_exit_controls & |
| 2596 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 2597 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 2598 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 2599 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2600 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2601 | } |
| 2602 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
| 2605 | |
Krish Sadhukhan | 4e445ae | 2018-12-12 13:30:12 -0500 | [diff] [blame] | 2606 | /* |
| 2607 | * Checks related to Guest Non-register State |
| 2608 | */ |
| 2609 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2610 | { |
| 2611 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 2612 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
Krish Sadhukhan | 4e445ae | 2018-12-12 13:30:12 -0500 | [diff] [blame] | 2613 | return -EINVAL; |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2614 | |
Krish Sadhukhan | 4e445ae | 2018-12-12 13:30:12 -0500 | [diff] [blame] | 2615 | return 0; |
| 2616 | } |
| 2617 | |
| 2618 | static int nested_vmx_check_vmentry_prereqs(struct kvm_vcpu *vcpu, |
| 2619 | struct vmcs12 *vmcs12) |
| 2620 | { |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2621 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2622 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2623 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
| 2624 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 2625 | |
| 2626 | if (nested_check_host_control_regs(vcpu, vmcs12)) |
| 2627 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 2628 | |
Krish Sadhukhan | 4e445ae | 2018-12-12 13:30:12 -0500 | [diff] [blame] | 2629 | if (nested_check_guest_non_reg_state(vmcs12)) |
| 2630 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 2631 | |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2632 | return 0; |
| 2633 | } |
| 2634 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2635 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2636 | struct vmcs12 *vmcs12) |
| 2637 | { |
| 2638 | int r; |
| 2639 | struct page *page; |
| 2640 | struct vmcs12 *shadow; |
| 2641 | |
| 2642 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 2643 | return 0; |
| 2644 | |
| 2645 | if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)) |
| 2646 | return -EINVAL; |
| 2647 | |
| 2648 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 2649 | if (is_error_page(page)) |
| 2650 | return -EINVAL; |
| 2651 | |
| 2652 | r = 0; |
| 2653 | shadow = kmap(page); |
| 2654 | if (shadow->hdr.revision_id != VMCS12_REVISION || |
| 2655 | shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)) |
| 2656 | r = -EINVAL; |
| 2657 | kunmap(page); |
| 2658 | kvm_release_page_clean(page); |
| 2659 | return r; |
| 2660 | } |
| 2661 | |
Krish Sadhukhan | 16322a3b | 2018-12-12 13:30:06 -0500 | [diff] [blame] | 2662 | static int nested_vmx_check_vmentry_postreqs(struct kvm_vcpu *vcpu, |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2663 | struct vmcs12 *vmcs12, |
| 2664 | u32 *exit_qual) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2665 | { |
| 2666 | bool ia32e; |
| 2667 | |
| 2668 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 2669 | |
| 2670 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 2671 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 2672 | return 1; |
| 2673 | |
| 2674 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
| 2675 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 2676 | return 1; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
| 2680 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 2681 | * are performed on the field for the IA32_EFER MSR: |
| 2682 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 2683 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 2684 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 2685 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 2686 | * CR0.PG) is 1. |
| 2687 | */ |
| 2688 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 2689 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 2690 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 2691 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 2692 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 2693 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 2694 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 2695 | return 1; |
| 2696 | } |
| 2697 | |
| 2698 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 2699 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 2700 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 2701 | return 1; |
| 2702 | |
| 2703 | return 0; |
| 2704 | } |
| 2705 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2706 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2707 | { |
| 2708 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2709 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 2710 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2711 | |
| 2712 | if (!nested_early_check) |
| 2713 | return 0; |
| 2714 | |
| 2715 | if (vmx->msr_autoload.host.nr) |
| 2716 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 2717 | if (vmx->msr_autoload.guest.nr) |
| 2718 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 2719 | |
| 2720 | preempt_disable(); |
| 2721 | |
| 2722 | vmx_prepare_switch_to_guest(vcpu); |
| 2723 | |
| 2724 | /* |
| 2725 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 2726 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
| 2727 | * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e. |
| 2728 | * there is no need to preserve other bits or save/restore the field. |
| 2729 | */ |
| 2730 | vmcs_writel(GUEST_RFLAGS, 0); |
| 2731 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2732 | cr3 = __get_current_cr3_fast(); |
| 2733 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 2734 | vmcs_writel(HOST_CR3, cr3); |
| 2735 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 2736 | } |
| 2737 | |
| 2738 | cr4 = cr4_read_shadow(); |
| 2739 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 2740 | vmcs_writel(HOST_CR4, cr4); |
| 2741 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 2742 | } |
| 2743 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2744 | asm( |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2745 | "sub $%c[wordsize], %%" _ASM_SP "\n\t" /* temporarily adjust RSP for CALL */ |
Sean Christopherson | 5a87816 | 2019-01-25 07:41:02 -0800 | [diff] [blame] | 2746 | "cmp %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t" |
| 2747 | "je 1f \n\t" |
Sean Christopherson | fbda0fd | 2019-01-25 07:41:01 -0800 | [diff] [blame] | 2748 | __ex("vmwrite %%" _ASM_SP ", %[HOST_RSP]") "\n\t" |
Sean Christopherson | 5a87816 | 2019-01-25 07:41:02 -0800 | [diff] [blame] | 2749 | "mov %%" _ASM_SP ", %c[host_state_rsp](%[loaded_vmcs]) \n\t" |
| 2750 | "1: \n\t" |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2751 | "add $%c[wordsize], %%" _ASM_SP "\n\t" /* un-adjust RSP */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2752 | |
| 2753 | /* Check if vmlaunch or vmresume is needed */ |
Sean Christopherson | 74dfa27 | 2019-01-25 07:41:00 -0800 | [diff] [blame] | 2754 | "cmpb $0, %c[launched](%[loaded_vmcs])\n\t" |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2755 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 2756 | /* |
| 2757 | * VMLAUNCH and VMRESUME clear RFLAGS.{CF,ZF} on VM-Exit, set |
| 2758 | * RFLAGS.CF on VM-Fail Invalid and set RFLAGS.ZF on VM-Fail |
| 2759 | * Valid. vmx_vmenter() directly "returns" RFLAGS, and so the |
Sean Christopherson | bbc0b82 | 2019-01-25 07:40:59 -0800 | [diff] [blame] | 2760 | * results of VM-Enter is captured via CC_{SET,OUT} to vm_fail. |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 2761 | */ |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2762 | "call vmx_vmenter\n\t" |
| 2763 | |
Sean Christopherson | bbc0b82 | 2019-01-25 07:40:59 -0800 | [diff] [blame] | 2764 | CC_SET(be) |
| 2765 | : ASM_CALL_CONSTRAINT, CC_OUT(be) (vm_fail) |
Sean Christopherson | 5a87816 | 2019-01-25 07:41:02 -0800 | [diff] [blame] | 2766 | : [HOST_RSP]"r"((unsigned long)HOST_RSP), |
Sean Christopherson | 74dfa27 | 2019-01-25 07:41:00 -0800 | [diff] [blame] | 2767 | [loaded_vmcs]"r"(vmx->loaded_vmcs), |
| 2768 | [launched]"i"(offsetof(struct loaded_vmcs, launched)), |
Sean Christopherson | 5a87816 | 2019-01-25 07:41:02 -0800 | [diff] [blame] | 2769 | [host_state_rsp]"i"(offsetof(struct loaded_vmcs, host_state.rsp)), |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2770 | [wordsize]"i"(sizeof(ulong)) |
Sean Christopherson | 9ce0a07 | 2019-01-25 07:40:55 -0800 | [diff] [blame] | 2771 | : "cc", "memory" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2772 | ); |
| 2773 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2774 | preempt_enable(); |
| 2775 | |
| 2776 | if (vmx->msr_autoload.host.nr) |
| 2777 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2778 | if (vmx->msr_autoload.guest.nr) |
| 2779 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2780 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 2781 | if (vm_fail) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2782 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 2783 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2784 | return 1; |
| 2785 | } |
| 2786 | |
| 2787 | /* |
| 2788 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 2789 | */ |
| 2790 | local_irq_enable(); |
| 2791 | if (hw_breakpoint_active()) |
| 2792 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
| 2793 | |
| 2794 | /* |
| 2795 | * A non-failing VMEntry means we somehow entered guest mode with |
| 2796 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 2797 | * is no telling what memory has been modified or what state has |
| 2798 | * been exposed to unknown code. Hitting this all but guarantees |
| 2799 | * a (very critical) hardware issue. |
| 2800 | */ |
| 2801 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 2802 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 2803 | |
| 2804 | return 0; |
| 2805 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2806 | |
| 2807 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 2808 | struct vmcs12 *vmcs12); |
| 2809 | |
| 2810 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 2811 | { |
| 2812 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2813 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2814 | struct page *page; |
| 2815 | u64 hpa; |
| 2816 | |
| 2817 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 2818 | /* |
| 2819 | * Translate L1 physical address to host physical |
| 2820 | * address for vmcs02. Keep the page pinned, so this |
| 2821 | * physical address remains valid. We keep a reference |
| 2822 | * to it so we can release it later. |
| 2823 | */ |
| 2824 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
| 2825 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
| 2826 | vmx->nested.apic_access_page = NULL; |
| 2827 | } |
| 2828 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
| 2829 | /* |
| 2830 | * If translation failed, no matter: This feature asks |
| 2831 | * to exit when accessing the given address, and if it |
| 2832 | * can never be accessed, this feature won't do |
| 2833 | * anything anyway. |
| 2834 | */ |
| 2835 | if (!is_error_page(page)) { |
| 2836 | vmx->nested.apic_access_page = page; |
| 2837 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 2838 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 2839 | } else { |
| 2840 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 2841 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 2846 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
| 2847 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
| 2848 | vmx->nested.virtual_apic_page = NULL; |
| 2849 | } |
| 2850 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
| 2851 | |
| 2852 | /* |
| 2853 | * If translation failed, VM entry will fail because |
| 2854 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 2855 | * Failing the vm entry is _not_ what the processor |
| 2856 | * does but it's basically the only possibility we |
| 2857 | * have. We could still enter the guest if CR8 load |
| 2858 | * exits are enabled, CR8 store exits are enabled, and |
| 2859 | * virtualize APIC access is disabled; in this case |
| 2860 | * the processor would never use the TPR shadow and we |
| 2861 | * could simply clear the bit from the execution |
| 2862 | * control. But such a configuration is useless, so |
| 2863 | * let's keep the code simple. |
| 2864 | */ |
| 2865 | if (!is_error_page(page)) { |
| 2866 | vmx->nested.virtual_apic_page = page; |
| 2867 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 2868 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 2873 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 2874 | kunmap(vmx->nested.pi_desc_page); |
| 2875 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
| 2876 | vmx->nested.pi_desc_page = NULL; |
Linus Torvalds | 42b00f1 | 2018-12-26 11:46:28 -0800 | [diff] [blame] | 2877 | vmx->nested.pi_desc = NULL; |
| 2878 | vmcs_write64(POSTED_INTR_DESC_ADDR, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2879 | } |
| 2880 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 2881 | if (is_error_page(page)) |
| 2882 | return; |
| 2883 | vmx->nested.pi_desc_page = page; |
| 2884 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
| 2885 | vmx->nested.pi_desc = |
| 2886 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 2887 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 2888 | (PAGE_SIZE - 1))); |
| 2889 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 2890 | page_to_phys(vmx->nested.pi_desc_page) + |
| 2891 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 2892 | (PAGE_SIZE - 1))); |
| 2893 | } |
| 2894 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
| 2895 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 2896 | CPU_BASED_USE_MSR_BITMAPS); |
| 2897 | else |
| 2898 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 2899 | CPU_BASED_USE_MSR_BITMAPS); |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 2904 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 2905 | * slightly different). It also specifies what exception to inject otherwise. |
| 2906 | * Note that many of these exceptions have priority over VM exits, so they |
| 2907 | * don't have to be checked again here. |
| 2908 | */ |
| 2909 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 2910 | { |
| 2911 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 2912 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 2913 | return 0; |
| 2914 | } |
| 2915 | |
| 2916 | if (vmx_get_cpl(vcpu)) { |
| 2917 | kvm_inject_gp(vcpu, 0); |
| 2918 | return 0; |
| 2919 | } |
| 2920 | |
| 2921 | return 1; |
| 2922 | } |
| 2923 | |
| 2924 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 2925 | { |
| 2926 | u8 rvi = vmx_get_rvi(); |
| 2927 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 2928 | |
| 2929 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 2930 | } |
| 2931 | |
| 2932 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 2933 | struct vmcs12 *vmcs12); |
| 2934 | |
| 2935 | /* |
| 2936 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 2937 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
| 2938 | + * |
| 2939 | + * Returns: |
| 2940 | + * 0 - success, i.e. proceed with actual VMEnter |
| 2941 | + * 1 - consistency check VMExit |
| 2942 | + * -1 - consistency check VMFail |
| 2943 | */ |
| 2944 | int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
| 2945 | { |
| 2946 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2947 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2948 | bool evaluate_pending_interrupts; |
| 2949 | u32 exit_reason = EXIT_REASON_INVALID_STATE; |
| 2950 | u32 exit_qual; |
| 2951 | |
| 2952 | evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
| 2953 | (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING); |
| 2954 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 2955 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 2956 | |
| 2957 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 2958 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 2959 | if (kvm_mpx_supported() && |
| 2960 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2961 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 2962 | |
| 2963 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 2964 | |
| 2965 | prepare_vmcs02_early(vmx, vmcs12); |
| 2966 | |
| 2967 | if (from_vmentry) { |
| 2968 | nested_get_vmcs12_pages(vcpu); |
| 2969 | |
| 2970 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 2971 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 2972 | return -1; |
| 2973 | } |
| 2974 | |
Krish Sadhukhan | 16322a3b | 2018-12-12 13:30:06 -0500 | [diff] [blame] | 2975 | if (nested_vmx_check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2976 | goto vmentry_fail_vmexit; |
| 2977 | } |
| 2978 | |
| 2979 | enter_guest_mode(vcpu); |
| 2980 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 2981 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 2982 | |
| 2983 | if (prepare_vmcs02(vcpu, vmcs12, &exit_qual)) |
| 2984 | goto vmentry_fail_vmexit_guest_mode; |
| 2985 | |
| 2986 | if (from_vmentry) { |
| 2987 | exit_reason = EXIT_REASON_MSR_LOAD_FAIL; |
| 2988 | exit_qual = nested_vmx_load_msr(vcpu, |
| 2989 | vmcs12->vm_entry_msr_load_addr, |
| 2990 | vmcs12->vm_entry_msr_load_count); |
| 2991 | if (exit_qual) |
| 2992 | goto vmentry_fail_vmexit_guest_mode; |
| 2993 | } else { |
| 2994 | /* |
| 2995 | * The MMU is not initialized to point at the right entities yet and |
| 2996 | * "get pages" would need to read data from the guest (i.e. we will |
| 2997 | * need to perform gpa to hpa translation). Request a call |
| 2998 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 2999 | * have already been set at vmentry time and should not be reset. |
| 3000 | */ |
| 3001 | kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu); |
| 3002 | } |
| 3003 | |
| 3004 | /* |
| 3005 | * If L1 had a pending IRQ/NMI until it executed |
| 3006 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3007 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3008 | * evaluate if this pending event should cause an exit from L2 |
| 3009 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3010 | * intercept EXTERNAL_INTERRUPT). |
| 3011 | * |
| 3012 | * Usually this would be handled by the processor noticing an |
| 3013 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3014 | * pending virtual interrupts. However, this setting was done |
| 3015 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3016 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3017 | */ |
| 3018 | if (unlikely(evaluate_pending_interrupts)) |
| 3019 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3020 | |
| 3021 | /* |
| 3022 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3023 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3024 | * returned as far as L1 is concerned. It will only return (and set |
| 3025 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3026 | */ |
| 3027 | return 0; |
| 3028 | |
| 3029 | /* |
| 3030 | * A failed consistency check that leads to a VMExit during L1's |
| 3031 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3032 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3033 | */ |
| 3034 | vmentry_fail_vmexit_guest_mode: |
| 3035 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 3036 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3037 | leave_guest_mode(vcpu); |
| 3038 | |
| 3039 | vmentry_fail_vmexit: |
| 3040 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3041 | |
| 3042 | if (!from_vmentry) |
| 3043 | return 1; |
| 3044 | |
| 3045 | load_vmcs12_host_state(vcpu, vmcs12); |
| 3046 | vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 3047 | vmcs12->exit_qualification = exit_qual; |
| 3048 | if (enable_shadow_vmcs || vmx->nested.hv_evmcs) |
| 3049 | vmx->nested.need_vmcs12_sync = true; |
| 3050 | return 1; |
| 3051 | } |
| 3052 | |
| 3053 | /* |
| 3054 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3055 | * for running an L2 nested guest. |
| 3056 | */ |
| 3057 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3058 | { |
| 3059 | struct vmcs12 *vmcs12; |
| 3060 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3061 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
| 3062 | int ret; |
| 3063 | |
| 3064 | if (!nested_vmx_check_permission(vcpu)) |
| 3065 | return 1; |
| 3066 | |
| 3067 | if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true)) |
| 3068 | return 1; |
| 3069 | |
| 3070 | if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull) |
| 3071 | return nested_vmx_failInvalid(vcpu); |
| 3072 | |
| 3073 | vmcs12 = get_vmcs12(vcpu); |
| 3074 | |
| 3075 | /* |
| 3076 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3077 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3078 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3079 | * VM-instruction error field. |
| 3080 | */ |
| 3081 | if (vmcs12->hdr.shadow_vmcs) |
| 3082 | return nested_vmx_failInvalid(vcpu); |
| 3083 | |
| 3084 | if (vmx->nested.hv_evmcs) { |
| 3085 | copy_enlightened_to_vmcs12(vmx); |
| 3086 | /* Enlightened VMCS doesn't have launch state */ |
| 3087 | vmcs12->launch_state = !launch; |
| 3088 | } else if (enable_shadow_vmcs) { |
| 3089 | copy_shadow_to_vmcs12(vmx); |
| 3090 | } |
| 3091 | |
| 3092 | /* |
| 3093 | * The nested entry process starts with enforcing various prerequisites |
| 3094 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3095 | * they fail: As the SDM explains, some conditions should cause the |
| 3096 | * instruction to fail, while others will cause the instruction to seem |
| 3097 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3098 | * To speed up the normal (success) code path, we should avoid checking |
| 3099 | * for misconfigurations which will anyway be caught by the processor |
| 3100 | * when using the merged vmcs02. |
| 3101 | */ |
| 3102 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) |
| 3103 | return nested_vmx_failValid(vcpu, |
| 3104 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 3105 | |
| 3106 | if (vmcs12->launch_state == launch) |
| 3107 | return nested_vmx_failValid(vcpu, |
| 3108 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3109 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3110 | |
Krish Sadhukhan | 16322a3b | 2018-12-12 13:30:06 -0500 | [diff] [blame] | 3111 | ret = nested_vmx_check_vmentry_prereqs(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3112 | if (ret) |
| 3113 | return nested_vmx_failValid(vcpu, ret); |
| 3114 | |
| 3115 | /* |
| 3116 | * We're finally done with prerequisite checking, and can start with |
| 3117 | * the nested entry. |
| 3118 | */ |
| 3119 | vmx->nested.nested_run_pending = 1; |
| 3120 | ret = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3121 | vmx->nested.nested_run_pending = !ret; |
| 3122 | if (ret > 0) |
| 3123 | return 1; |
| 3124 | else if (ret) |
| 3125 | return nested_vmx_failValid(vcpu, |
| 3126 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 3127 | |
| 3128 | /* Hide L1D cache contents from the nested guest. */ |
| 3129 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3130 | |
| 3131 | /* |
| 3132 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3133 | * also be used as part of restoring nVMX state for |
| 3134 | * snapshot restore (migration). |
| 3135 | * |
| 3136 | * In this flow, it is assumed that vmcs12 cache was |
| 3137 | * trasferred as part of captured nVMX state and should |
| 3138 | * therefore not be read from guest memory (which may not |
| 3139 | * exist on destination host yet). |
| 3140 | */ |
| 3141 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3142 | |
| 3143 | /* |
Jim Mattson | 9ebdfe5 | 2018-11-26 11:22:32 -0800 | [diff] [blame] | 3144 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3145 | * awakened by event injection or by an NMI-window VM-exit or |
| 3146 | * by an interrupt-window VM-exit, halt the vcpu. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3147 | */ |
| 3148 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 9ebdfe5 | 2018-11-26 11:22:32 -0800 | [diff] [blame] | 3149 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3150 | !(vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_NMI_PENDING) && |
| 3151 | !((vmcs12->cpu_based_vm_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING) && |
| 3152 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3153 | vmx->nested.nested_run_pending = 0; |
| 3154 | return kvm_vcpu_halt(vcpu); |
| 3155 | } |
| 3156 | return 1; |
| 3157 | } |
| 3158 | |
| 3159 | /* |
| 3160 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 3161 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 3162 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3163 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3164 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3165 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3166 | * didn't trap the bit, because if L1 did, so would L0). |
| 3167 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3168 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3169 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3170 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3171 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3172 | * changed these bits, and therefore they need to be updated, but L0 |
| 3173 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3174 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3175 | */ |
| 3176 | static inline unsigned long |
| 3177 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3178 | { |
| 3179 | return |
| 3180 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3181 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3182 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3183 | vcpu->arch.cr0_guest_owned_bits)); |
| 3184 | } |
| 3185 | |
| 3186 | static inline unsigned long |
| 3187 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3188 | { |
| 3189 | return |
| 3190 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3191 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3192 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3193 | vcpu->arch.cr4_guest_owned_bits)); |
| 3194 | } |
| 3195 | |
| 3196 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3197 | struct vmcs12 *vmcs12) |
| 3198 | { |
| 3199 | u32 idt_vectoring; |
| 3200 | unsigned int nr; |
| 3201 | |
| 3202 | if (vcpu->arch.exception.injected) { |
| 3203 | nr = vcpu->arch.exception.nr; |
| 3204 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3205 | |
| 3206 | if (kvm_exception_is_soft(nr)) { |
| 3207 | vmcs12->vm_exit_instruction_len = |
| 3208 | vcpu->arch.event_exit_inst_len; |
| 3209 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3210 | } else |
| 3211 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3212 | |
| 3213 | if (vcpu->arch.exception.has_error_code) { |
| 3214 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3215 | vmcs12->idt_vectoring_error_code = |
| 3216 | vcpu->arch.exception.error_code; |
| 3217 | } |
| 3218 | |
| 3219 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3220 | } else if (vcpu->arch.nmi_injected) { |
| 3221 | vmcs12->idt_vectoring_info_field = |
| 3222 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3223 | } else if (vcpu->arch.interrupt.injected) { |
| 3224 | nr = vcpu->arch.interrupt.nr; |
| 3225 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3226 | |
| 3227 | if (vcpu->arch.interrupt.soft) { |
| 3228 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3229 | vmcs12->vm_entry_instruction_len = |
| 3230 | vcpu->arch.event_exit_inst_len; |
| 3231 | } else |
| 3232 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3233 | |
| 3234 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | |
| 3239 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 3240 | { |
| 3241 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3242 | gfn_t gfn; |
| 3243 | |
| 3244 | /* |
| 3245 | * Don't need to mark the APIC access page dirty; it is never |
| 3246 | * written to by the CPU during APIC virtualization. |
| 3247 | */ |
| 3248 | |
| 3249 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3250 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3251 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3252 | } |
| 3253 | |
| 3254 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3255 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3256 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
| 3261 | { |
| 3262 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3263 | int max_irr; |
| 3264 | void *vapic_page; |
| 3265 | u16 status; |
| 3266 | |
| 3267 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 3268 | return; |
| 3269 | |
| 3270 | vmx->nested.pi_pending = false; |
| 3271 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 3272 | return; |
| 3273 | |
| 3274 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3275 | if (max_irr != 256) { |
| 3276 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
| 3277 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3278 | vapic_page, &max_irr); |
| 3279 | kunmap(vmx->nested.virtual_apic_page); |
| 3280 | |
| 3281 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3282 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3283 | status &= ~0xff; |
| 3284 | status |= (u8)max_irr; |
| 3285 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 3290 | } |
| 3291 | |
| 3292 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3293 | unsigned long exit_qual) |
| 3294 | { |
| 3295 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3296 | unsigned int nr = vcpu->arch.exception.nr; |
| 3297 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3298 | |
| 3299 | if (vcpu->arch.exception.has_error_code) { |
| 3300 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3301 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3302 | } |
| 3303 | |
| 3304 | if (kvm_exception_is_soft(nr)) |
| 3305 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3306 | else |
| 3307 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3308 | |
| 3309 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3310 | vmx_get_nmi_mask(vcpu)) |
| 3311 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3312 | |
| 3313 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3314 | } |
| 3315 | |
| 3316 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 3317 | { |
| 3318 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3319 | unsigned long exit_qual; |
| 3320 | bool block_nested_events = |
| 3321 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
| 3322 | |
| 3323 | if (vcpu->arch.exception.pending && |
| 3324 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
| 3325 | if (block_nested_events) |
| 3326 | return -EBUSY; |
| 3327 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
| 3331 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3332 | vmx->nested.preemption_timer_expired) { |
| 3333 | if (block_nested_events) |
| 3334 | return -EBUSY; |
| 3335 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3336 | return 0; |
| 3337 | } |
| 3338 | |
| 3339 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
| 3340 | if (block_nested_events) |
| 3341 | return -EBUSY; |
| 3342 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3343 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3344 | INTR_INFO_VALID_MASK, 0); |
| 3345 | /* |
| 3346 | * The NMI-triggered VM exit counts as injection: |
| 3347 | * clear this one and block further NMIs. |
| 3348 | */ |
| 3349 | vcpu->arch.nmi_pending = 0; |
| 3350 | vmx_set_nmi_mask(vcpu, true); |
| 3351 | return 0; |
| 3352 | } |
| 3353 | |
| 3354 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 3355 | nested_exit_on_intr(vcpu)) { |
| 3356 | if (block_nested_events) |
| 3357 | return -EBUSY; |
| 3358 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3359 | return 0; |
| 3360 | } |
| 3361 | |
| 3362 | vmx_complete_nested_posted_interrupt(vcpu); |
| 3363 | return 0; |
| 3364 | } |
| 3365 | |
| 3366 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3367 | { |
| 3368 | ktime_t remaining = |
| 3369 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3370 | u64 value; |
| 3371 | |
| 3372 | if (ktime_to_ns(remaining) <= 0) |
| 3373 | return 0; |
| 3374 | |
| 3375 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3376 | do_div(value, 1000000); |
| 3377 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3378 | } |
| 3379 | |
| 3380 | /* |
| 3381 | * Update the guest state fields of vmcs12 to reflect changes that |
| 3382 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 3383 | * VM-entry controls is also updated, since this is really a guest |
| 3384 | * state bit.) |
| 3385 | */ |
| 3386 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3387 | { |
| 3388 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 3389 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 3390 | |
| 3391 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 3392 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 3393 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 3394 | |
| 3395 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 3396 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 3397 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 3398 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 3399 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 3400 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 3401 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 3402 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 3403 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 3404 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 3405 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 3406 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 3407 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 3408 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 3409 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 3410 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 3411 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3412 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3413 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 3414 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 3415 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 3416 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 3417 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 3418 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 3419 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 3420 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 3421 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 3422 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 3423 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 3424 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 3425 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 3426 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 3427 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 3428 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 3429 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 3430 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 3431 | |
| 3432 | vmcs12->guest_interruptibility_info = |
| 3433 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3434 | vmcs12->guest_pending_dbg_exceptions = |
| 3435 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 3436 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 3437 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 3438 | else |
| 3439 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 3440 | |
| 3441 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3442 | if (vmcs12->vm_exit_controls & |
| 3443 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 3444 | vmcs12->vmx_preemption_timer_value = |
| 3445 | vmx_get_preemption_timer_value(vcpu); |
| 3446 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 3447 | } |
| 3448 | |
| 3449 | /* |
| 3450 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 3451 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 3452 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 3453 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 3454 | * |
| 3455 | * Additionally, restore L2's PDPTR to vmcs12. |
| 3456 | */ |
| 3457 | if (enable_ept) { |
| 3458 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
| 3459 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 3460 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 3461 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 3462 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 3463 | } |
| 3464 | |
| 3465 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 3466 | |
| 3467 | if (nested_cpu_has_vid(vmcs12)) |
| 3468 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 3469 | |
| 3470 | vmcs12->vm_entry_controls = |
| 3471 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 3472 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 3473 | |
| 3474 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 3475 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 3476 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3477 | } |
| 3478 | |
| 3479 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 3480 | * the relevant bit asks not to trap the change */ |
| 3481 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
| 3482 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
| 3483 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 3484 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
| 3485 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 3486 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 3487 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
| 3488 | if (kvm_mpx_supported()) |
| 3489 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3490 | } |
| 3491 | |
| 3492 | /* |
| 3493 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 3494 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 3495 | * and this function updates it to reflect the changes to the guest state while |
| 3496 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 3497 | * without going back to L1), and to reflect the exit reason. |
| 3498 | * Note that we do not have to copy here all VMCS fields, just those that |
| 3499 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 3500 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 3501 | * which already writes to vmcs12 directly. |
| 3502 | */ |
| 3503 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 3504 | u32 exit_reason, u32 exit_intr_info, |
| 3505 | unsigned long exit_qualification) |
| 3506 | { |
| 3507 | /* update guest state fields: */ |
| 3508 | sync_vmcs12(vcpu, vmcs12); |
| 3509 | |
| 3510 | /* update exit information fields: */ |
| 3511 | |
| 3512 | vmcs12->vm_exit_reason = exit_reason; |
| 3513 | vmcs12->exit_qualification = exit_qualification; |
| 3514 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 3515 | |
| 3516 | vmcs12->idt_vectoring_info_field = 0; |
| 3517 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 3518 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 3519 | |
| 3520 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 3521 | vmcs12->launch_state = 1; |
| 3522 | |
| 3523 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 3524 | * instead of reading the real value. */ |
| 3525 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 3526 | |
| 3527 | /* |
| 3528 | * Transfer the event that L0 or L1 may wanted to inject into |
| 3529 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 3530 | */ |
| 3531 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 3532 | |
| 3533 | /* |
| 3534 | * According to spec, there's no need to store the guest's |
| 3535 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 3536 | * during or after loading the guest state. Since this exit |
| 3537 | * does not fall in that category, we need to save the MSRs. |
| 3538 | */ |
| 3539 | if (nested_vmx_store_msr(vcpu, |
| 3540 | vmcs12->vm_exit_msr_store_addr, |
| 3541 | vmcs12->vm_exit_msr_store_count)) |
| 3542 | nested_vmx_abort(vcpu, |
| 3543 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | /* |
| 3547 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 3548 | * preserved above and would only end up incorrectly in L1. |
| 3549 | */ |
| 3550 | vcpu->arch.nmi_injected = false; |
| 3551 | kvm_clear_exception_queue(vcpu); |
| 3552 | kvm_clear_interrupt_queue(vcpu); |
| 3553 | } |
| 3554 | |
| 3555 | /* |
| 3556 | * A part of what we need to when the nested L2 guest exits and we want to |
| 3557 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 3558 | * in vmcs12. |
| 3559 | * This function is to be called not only on normal nested exit, but also on |
| 3560 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 3561 | * Failures During or After Loading Guest State"). |
| 3562 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 3563 | */ |
| 3564 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3565 | struct vmcs12 *vmcs12) |
| 3566 | { |
| 3567 | struct kvm_segment seg; |
| 3568 | u32 entry_failure_code; |
| 3569 | |
| 3570 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 3571 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 3572 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 3573 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 3574 | else |
| 3575 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 3576 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 3577 | |
| 3578 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 3579 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
| 3580 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 3581 | vmx_set_interrupt_shadow(vcpu, 0); |
| 3582 | |
| 3583 | /* |
| 3584 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 3585 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 3586 | * |
| 3587 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 3588 | * (KVM doesn't change it); |
| 3589 | */ |
| 3590 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 3591 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 3592 | |
| 3593 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 3594 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 3595 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 3596 | |
| 3597 | nested_ept_uninit_mmu_context(vcpu); |
| 3598 | |
| 3599 | /* |
| 3600 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 3601 | * couldn't have changed. |
| 3602 | */ |
| 3603 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 3604 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 3605 | |
| 3606 | if (!enable_ept) |
| 3607 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 3608 | |
| 3609 | /* |
| 3610 | * If vmcs01 doesn't use VPID, CPU flushes TLB on every |
| 3611 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 3612 | * |
| 3613 | * If vmcs12 doesn't use VPID, L1 expects TLB to be |
| 3614 | * flushed on every VMEntry/VMExit. |
| 3615 | * |
| 3616 | * Otherwise, we can preserve TLB entries as long as we are |
| 3617 | * able to tag L1 TLB entries differently than L2 TLB entries. |
| 3618 | * |
| 3619 | * If vmcs12 uses EPT, we need to execute this flush on EPTP01 |
| 3620 | * and therefore we request the TLB flush to happen only after VMCS EPTP |
| 3621 | * has been set by KVM_REQ_LOAD_CR3. |
| 3622 | */ |
| 3623 | if (enable_vpid && |
| 3624 | (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) { |
| 3625 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 3626 | } |
| 3627 | |
| 3628 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 3629 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 3630 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 3631 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 3632 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 3633 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 3634 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 3635 | |
| 3636 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 3637 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 3638 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 3639 | |
| 3640 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 3641 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 3642 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 3643 | } |
| 3644 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3645 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 3646 | vmcs12->host_ia32_perf_global_ctrl); |
| 3647 | |
| 3648 | /* Set L1 segment info according to Intel SDM |
| 3649 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 3650 | seg = (struct kvm_segment) { |
| 3651 | .base = 0, |
| 3652 | .limit = 0xFFFFFFFF, |
| 3653 | .selector = vmcs12->host_cs_selector, |
| 3654 | .type = 11, |
| 3655 | .present = 1, |
| 3656 | .s = 1, |
| 3657 | .g = 1 |
| 3658 | }; |
| 3659 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 3660 | seg.l = 1; |
| 3661 | else |
| 3662 | seg.db = 1; |
| 3663 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 3664 | seg = (struct kvm_segment) { |
| 3665 | .base = 0, |
| 3666 | .limit = 0xFFFFFFFF, |
| 3667 | .type = 3, |
| 3668 | .present = 1, |
| 3669 | .s = 1, |
| 3670 | .db = 1, |
| 3671 | .g = 1 |
| 3672 | }; |
| 3673 | seg.selector = vmcs12->host_ds_selector; |
| 3674 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 3675 | seg.selector = vmcs12->host_es_selector; |
| 3676 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 3677 | seg.selector = vmcs12->host_ss_selector; |
| 3678 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 3679 | seg.selector = vmcs12->host_fs_selector; |
| 3680 | seg.base = vmcs12->host_fs_base; |
| 3681 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 3682 | seg.selector = vmcs12->host_gs_selector; |
| 3683 | seg.base = vmcs12->host_gs_base; |
| 3684 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 3685 | seg = (struct kvm_segment) { |
| 3686 | .base = vmcs12->host_tr_base, |
| 3687 | .limit = 0x67, |
| 3688 | .selector = vmcs12->host_tr_selector, |
| 3689 | .type = 11, |
| 3690 | .present = 1 |
| 3691 | }; |
| 3692 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 3693 | |
| 3694 | kvm_set_dr(vcpu, 7, 0x400); |
| 3695 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 3696 | |
| 3697 | if (cpu_has_vmx_msr_bitmap()) |
| 3698 | vmx_update_msr_bitmap(vcpu); |
| 3699 | |
| 3700 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 3701 | vmcs12->vm_exit_msr_load_count)) |
| 3702 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 3703 | } |
| 3704 | |
| 3705 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 3706 | { |
| 3707 | struct shared_msr_entry *efer_msr; |
| 3708 | unsigned int i; |
| 3709 | |
| 3710 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 3711 | return vmcs_read64(GUEST_IA32_EFER); |
| 3712 | |
| 3713 | if (cpu_has_load_ia32_efer()) |
| 3714 | return host_efer; |
| 3715 | |
| 3716 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 3717 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 3718 | return vmx->msr_autoload.guest.val[i].value; |
| 3719 | } |
| 3720 | |
| 3721 | efer_msr = find_msr_entry(vmx, MSR_EFER); |
| 3722 | if (efer_msr) |
| 3723 | return efer_msr->data; |
| 3724 | |
| 3725 | return host_efer; |
| 3726 | } |
| 3727 | |
| 3728 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 3729 | { |
| 3730 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3731 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3732 | struct vmx_msr_entry g, h; |
| 3733 | struct msr_data msr; |
| 3734 | gpa_t gpa; |
| 3735 | u32 i, j; |
| 3736 | |
| 3737 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 3738 | |
| 3739 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 3740 | /* |
| 3741 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 3742 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 3743 | * and vcpu->arch.dr7 is not squirreled away before the |
| 3744 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 3745 | */ |
| 3746 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 3747 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 3748 | else |
| 3749 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 3750 | } |
| 3751 | |
| 3752 | /* |
| 3753 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 3754 | * handle a variety of side effects to KVM's software model. |
| 3755 | */ |
| 3756 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 3757 | |
| 3758 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 3759 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 3760 | |
| 3761 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 3762 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 3763 | |
| 3764 | nested_ept_uninit_mmu_context(vcpu); |
| 3765 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3766 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 3767 | |
| 3768 | /* |
| 3769 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 3770 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 3771 | * VMFail, like everything else we just need to ensure our |
| 3772 | * software model is up-to-date. |
| 3773 | */ |
| 3774 | ept_save_pdptrs(vcpu); |
| 3775 | |
| 3776 | kvm_mmu_reset_context(vcpu); |
| 3777 | |
| 3778 | if (cpu_has_vmx_msr_bitmap()) |
| 3779 | vmx_update_msr_bitmap(vcpu); |
| 3780 | |
| 3781 | /* |
| 3782 | * This nasty bit of open coding is a compromise between blindly |
| 3783 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 3784 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 3785 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 3786 | * expensive since the lists are unbound by hardware). For each |
| 3787 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 3788 | * list, reload it from the exit load list if it exists and differs |
| 3789 | * from the guest value. The intent is to stuff host state as |
| 3790 | * silently as possible, not to fully process the exit load list. |
| 3791 | */ |
| 3792 | msr.host_initiated = false; |
| 3793 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 3794 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 3795 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 3796 | pr_debug_ratelimited( |
| 3797 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 3798 | __func__, i, gpa); |
| 3799 | goto vmabort; |
| 3800 | } |
| 3801 | |
| 3802 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 3803 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 3804 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 3805 | pr_debug_ratelimited( |
| 3806 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 3807 | __func__, j, gpa); |
| 3808 | goto vmabort; |
| 3809 | } |
| 3810 | if (h.index != g.index) |
| 3811 | continue; |
| 3812 | if (h.value == g.value) |
| 3813 | break; |
| 3814 | |
| 3815 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 3816 | pr_debug_ratelimited( |
| 3817 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 3818 | __func__, j, h.index, h.reserved); |
| 3819 | goto vmabort; |
| 3820 | } |
| 3821 | |
| 3822 | msr.index = h.index; |
| 3823 | msr.data = h.value; |
| 3824 | if (kvm_set_msr(vcpu, &msr)) { |
| 3825 | pr_debug_ratelimited( |
| 3826 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 3827 | __func__, j, h.index, h.value); |
| 3828 | goto vmabort; |
| 3829 | } |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | return; |
| 3834 | |
| 3835 | vmabort: |
| 3836 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 3837 | } |
| 3838 | |
| 3839 | /* |
| 3840 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 3841 | * and modify vmcs12 to make it see what it would expect to see there if |
| 3842 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 3843 | */ |
| 3844 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 3845 | u32 exit_intr_info, unsigned long exit_qualification) |
| 3846 | { |
| 3847 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3848 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3849 | |
| 3850 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 3851 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 3852 | |
| 3853 | leave_guest_mode(vcpu); |
| 3854 | |
| 3855 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 3856 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3857 | |
| 3858 | if (likely(!vmx->fail)) { |
| 3859 | if (exit_reason == -1) |
| 3860 | sync_vmcs12(vcpu, vmcs12); |
| 3861 | else |
| 3862 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 3863 | exit_qualification); |
| 3864 | |
| 3865 | /* |
| 3866 | * Must happen outside of sync_vmcs12() as it will |
| 3867 | * also be used to capture vmcs12 cache as part of |
| 3868 | * capturing nVMX state for snapshot (migration). |
| 3869 | * |
| 3870 | * Otherwise, this flush will dirty guest memory at a |
| 3871 | * point it is already assumed by user-space to be |
| 3872 | * immutable. |
| 3873 | */ |
| 3874 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3875 | } else { |
| 3876 | /* |
| 3877 | * The only expected VM-instruction error is "VM entry with |
| 3878 | * invalid control field(s)." Anything else indicates a |
| 3879 | * problem with L0. And we should never get here with a |
| 3880 | * VMFail of any type if early consistency checks are enabled. |
| 3881 | */ |
| 3882 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 3883 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 3884 | WARN_ON_ONCE(nested_early_check); |
| 3885 | } |
| 3886 | |
| 3887 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3888 | |
| 3889 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 3890 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3891 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3892 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 3893 | |
| 3894 | if (kvm_has_tsc_control) |
| 3895 | decache_tsc_multiplier(vmx); |
| 3896 | |
| 3897 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 3898 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 3899 | vmx_set_virtual_apic_mode(vcpu); |
| 3900 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 3901 | nested_cpu_has2(vmcs12, |
| 3902 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3903 | vmx_flush_tlb(vcpu, true); |
| 3904 | } |
| 3905 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3906 | /* Unpin physical memory we referred to in vmcs02 */ |
| 3907 | if (vmx->nested.apic_access_page) { |
| 3908 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
| 3909 | vmx->nested.apic_access_page = NULL; |
| 3910 | } |
| 3911 | if (vmx->nested.virtual_apic_page) { |
| 3912 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
| 3913 | vmx->nested.virtual_apic_page = NULL; |
| 3914 | } |
| 3915 | if (vmx->nested.pi_desc_page) { |
| 3916 | kunmap(vmx->nested.pi_desc_page); |
| 3917 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
| 3918 | vmx->nested.pi_desc_page = NULL; |
| 3919 | vmx->nested.pi_desc = NULL; |
| 3920 | } |
| 3921 | |
| 3922 | /* |
| 3923 | * We are now running in L2, mmu_notifier will force to reload the |
| 3924 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 3925 | */ |
| 3926 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 3927 | |
| 3928 | if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs)) |
| 3929 | vmx->nested.need_vmcs12_sync = true; |
| 3930 | |
| 3931 | /* in case we halted in L2 */ |
| 3932 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 3933 | |
| 3934 | if (likely(!vmx->fail)) { |
| 3935 | /* |
| 3936 | * TODO: SDM says that with acknowledge interrupt on |
| 3937 | * exit, bit 31 of the VM-exit interrupt information |
| 3938 | * (valid interrupt) is always set to 1 on |
| 3939 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 3940 | * need kvm_cpu_has_interrupt(). See the commit |
| 3941 | * message for details. |
| 3942 | */ |
| 3943 | if (nested_exit_intr_ack_set(vcpu) && |
| 3944 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 3945 | kvm_cpu_has_interrupt(vcpu)) { |
| 3946 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 3947 | WARN_ON(irq < 0); |
| 3948 | vmcs12->vm_exit_intr_info = irq | |
| 3949 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 3950 | } |
| 3951 | |
| 3952 | if (exit_reason != -1) |
| 3953 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 3954 | vmcs12->exit_qualification, |
| 3955 | vmcs12->idt_vectoring_info_field, |
| 3956 | vmcs12->vm_exit_intr_info, |
| 3957 | vmcs12->vm_exit_intr_error_code, |
| 3958 | KVM_ISA_VMX); |
| 3959 | |
| 3960 | load_vmcs12_host_state(vcpu, vmcs12); |
| 3961 | |
| 3962 | return; |
| 3963 | } |
| 3964 | |
| 3965 | /* |
| 3966 | * After an early L2 VM-entry failure, we're now back |
| 3967 | * in L1 which thinks it just finished a VMLAUNCH or |
| 3968 | * VMRESUME instruction, so we need to set the failure |
| 3969 | * flag and the VM-instruction error field of the VMCS |
| 3970 | * accordingly, and skip the emulated instruction. |
| 3971 | */ |
| 3972 | (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 3973 | |
| 3974 | /* |
| 3975 | * Restore L1's host state to KVM's software model. We're here |
| 3976 | * because a consistency check was caught by hardware, which |
| 3977 | * means some amount of guest state has been propagated to KVM's |
| 3978 | * model and needs to be unwound to the host's state. |
| 3979 | */ |
| 3980 | nested_vmx_restore_host_state(vcpu); |
| 3981 | |
| 3982 | vmx->fail = 0; |
| 3983 | } |
| 3984 | |
| 3985 | /* |
| 3986 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 3987 | * exit caused by such an instruction (run by a guest hypervisor). |
| 3988 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 3989 | * #UD or #GP. |
| 3990 | */ |
| 3991 | int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualification, |
| 3992 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
| 3993 | { |
| 3994 | gva_t off; |
| 3995 | bool exn; |
| 3996 | struct kvm_segment s; |
| 3997 | |
| 3998 | /* |
| 3999 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4000 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4001 | * addressing components of the operand. Only the displacement part |
| 4002 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4003 | * For how an actual address is calculated from all these components, |
| 4004 | * refer to Vol. 1, "Operand Addressing". |
| 4005 | */ |
| 4006 | int scaling = vmx_instruction_info & 3; |
| 4007 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4008 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4009 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4010 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4011 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4012 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4013 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4014 | |
| 4015 | if (is_reg) { |
| 4016 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4017 | return 1; |
| 4018 | } |
| 4019 | |
| 4020 | /* Addr = segment_base + offset */ |
| 4021 | /* offset = base + [index * scale] + displacement */ |
| 4022 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame^] | 4023 | if (addr_size == 1) |
| 4024 | off = (gva_t)sign_extend64(off, 31); |
| 4025 | else if (addr_size == 0) |
| 4026 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4027 | if (base_is_valid) |
| 4028 | off += kvm_register_read(vcpu, base_reg); |
| 4029 | if (index_is_valid) |
| 4030 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 4031 | vmx_get_segment(vcpu, &s, seg_reg); |
| 4032 | *ret = s.base + off; |
| 4033 | |
| 4034 | if (addr_size == 1) /* 32 bit */ |
| 4035 | *ret &= 0xffffffff; |
| 4036 | |
| 4037 | /* Checks for #GP/#SS exceptions. */ |
| 4038 | exn = false; |
| 4039 | if (is_long_mode(vcpu)) { |
| 4040 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4041 | * non-canonical form. This is the only check on the memory |
| 4042 | * destination for long mode! |
| 4043 | */ |
| 4044 | exn = is_noncanonical_address(*ret, vcpu); |
| 4045 | } else if (is_protmode(vcpu)) { |
| 4046 | /* Protected mode: apply checks for segment validity in the |
| 4047 | * following order: |
| 4048 | * - segment type check (#GP(0) may be thrown) |
| 4049 | * - usability check (#GP(0)/#SS(0)) |
| 4050 | * - limit check (#GP(0)/#SS(0)) |
| 4051 | */ |
| 4052 | if (wr) |
| 4053 | /* #GP(0) if the destination operand is located in a |
| 4054 | * read-only data segment or any code segment. |
| 4055 | */ |
| 4056 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4057 | else |
| 4058 | /* #GP(0) if the source operand is located in an |
| 4059 | * execute-only code segment |
| 4060 | */ |
| 4061 | exn = ((s.type & 0xa) == 8); |
| 4062 | if (exn) { |
| 4063 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4064 | return 1; |
| 4065 | } |
| 4066 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4067 | */ |
| 4068 | exn = (s.unusable != 0); |
| 4069 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 4070 | * operand is outside the segment limit. |
| 4071 | */ |
| 4072 | exn = exn || (off + sizeof(u64) > s.limit); |
| 4073 | } |
| 4074 | if (exn) { |
| 4075 | kvm_queue_exception_e(vcpu, |
| 4076 | seg_reg == VCPU_SREG_SS ? |
| 4077 | SS_VECTOR : GP_VECTOR, |
| 4078 | 0); |
| 4079 | return 1; |
| 4080 | } |
| 4081 | |
| 4082 | return 0; |
| 4083 | } |
| 4084 | |
| 4085 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
| 4086 | { |
| 4087 | gva_t gva; |
| 4088 | struct x86_exception e; |
| 4089 | |
| 4090 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 4091 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
| 4092 | return 1; |
| 4093 | |
| 4094 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
| 4095 | kvm_inject_page_fault(vcpu, &e); |
| 4096 | return 1; |
| 4097 | } |
| 4098 | |
| 4099 | return 0; |
| 4100 | } |
| 4101 | |
| 4102 | /* |
| 4103 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4104 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4105 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4106 | */ |
| 4107 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4108 | { |
| 4109 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4110 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4111 | |
| 4112 | /* |
| 4113 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4114 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4115 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4116 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4117 | */ |
| 4118 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4119 | |
| 4120 | if (!loaded_vmcs->shadow_vmcs) { |
| 4121 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4122 | if (loaded_vmcs->shadow_vmcs) |
| 4123 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4124 | } |
| 4125 | return loaded_vmcs->shadow_vmcs; |
| 4126 | } |
| 4127 | |
| 4128 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4129 | { |
| 4130 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4131 | int r; |
| 4132 | |
| 4133 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4134 | if (r < 0) |
| 4135 | goto out_vmcs02; |
| 4136 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 4137 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4138 | if (!vmx->nested.cached_vmcs12) |
| 4139 | goto out_cached_vmcs12; |
| 4140 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 4141 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4142 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4143 | goto out_cached_shadow_vmcs12; |
| 4144 | |
| 4145 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4146 | goto out_shadow_vmcs; |
| 4147 | |
| 4148 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 4149 | HRTIMER_MODE_REL_PINNED); |
| 4150 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4151 | |
| 4152 | vmx->nested.vpid02 = allocate_vpid(); |
| 4153 | |
| 4154 | vmx->nested.vmcs02_initialized = false; |
| 4155 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4156 | |
| 4157 | if (pt_mode == PT_MODE_HOST_GUEST) { |
| 4158 | vmx->pt_desc.guest.ctl = 0; |
| 4159 | pt_update_intercept_for_msr(vmx); |
| 4160 | } |
| 4161 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4162 | return 0; |
| 4163 | |
| 4164 | out_shadow_vmcs: |
| 4165 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4166 | |
| 4167 | out_cached_shadow_vmcs12: |
| 4168 | kfree(vmx->nested.cached_vmcs12); |
| 4169 | |
| 4170 | out_cached_vmcs12: |
| 4171 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4172 | |
| 4173 | out_vmcs02: |
| 4174 | return -ENOMEM; |
| 4175 | } |
| 4176 | |
| 4177 | /* |
| 4178 | * Emulate the VMXON instruction. |
| 4179 | * Currently, we just remember that VMX is active, and do not save or even |
| 4180 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 4181 | * do not currently need to store anything in that guest-allocated memory |
| 4182 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 4183 | * argument is different from the VMXON pointer (which the spec says they do). |
| 4184 | */ |
| 4185 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4186 | { |
| 4187 | int ret; |
| 4188 | gpa_t vmptr; |
| 4189 | struct page *page; |
| 4190 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4191 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 4192 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4193 | |
| 4194 | /* |
| 4195 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4196 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 4197 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 4198 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4199 | * have already been checked by hardware, prior to the VM-exit for |
| 4200 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4201 | * that bit set to 1 in non-root mode. |
| 4202 | */ |
| 4203 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4204 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4205 | return 1; |
| 4206 | } |
| 4207 | |
| 4208 | /* CPL=0 must be checked manually. */ |
| 4209 | if (vmx_get_cpl(vcpu)) { |
| 4210 | kvm_inject_gp(vcpu, 0); |
| 4211 | return 1; |
| 4212 | } |
| 4213 | |
| 4214 | if (vmx->nested.vmxon) |
| 4215 | return nested_vmx_failValid(vcpu, |
| 4216 | VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
| 4217 | |
| 4218 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4219 | != VMXON_NEEDED_FEATURES) { |
| 4220 | kvm_inject_gp(vcpu, 0); |
| 4221 | return 1; |
| 4222 | } |
| 4223 | |
| 4224 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
| 4225 | return 1; |
| 4226 | |
| 4227 | /* |
| 4228 | * SDM 3: 24.11.5 |
| 4229 | * The first 4 bytes of VMXON region contain the supported |
| 4230 | * VMCS revision identifier |
| 4231 | * |
| 4232 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4233 | * which replaces physical address width with 32 |
| 4234 | */ |
| 4235 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) |
| 4236 | return nested_vmx_failInvalid(vcpu); |
| 4237 | |
| 4238 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 4239 | if (is_error_page(page)) |
| 4240 | return nested_vmx_failInvalid(vcpu); |
| 4241 | |
| 4242 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 4243 | kunmap(page); |
| 4244 | kvm_release_page_clean(page); |
| 4245 | return nested_vmx_failInvalid(vcpu); |
| 4246 | } |
| 4247 | kunmap(page); |
| 4248 | kvm_release_page_clean(page); |
| 4249 | |
| 4250 | vmx->nested.vmxon_ptr = vmptr; |
| 4251 | ret = enter_vmx_operation(vcpu); |
| 4252 | if (ret) |
| 4253 | return ret; |
| 4254 | |
| 4255 | return nested_vmx_succeed(vcpu); |
| 4256 | } |
| 4257 | |
| 4258 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4259 | { |
| 4260 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4261 | |
| 4262 | if (vmx->nested.current_vmptr == -1ull) |
| 4263 | return; |
| 4264 | |
| 4265 | if (enable_shadow_vmcs) { |
| 4266 | /* copy to memory all shadowed fields in case |
| 4267 | they were modified */ |
| 4268 | copy_shadow_to_vmcs12(vmx); |
| 4269 | vmx->nested.need_vmcs12_sync = false; |
| 4270 | vmx_disable_shadow_vmcs(vmx); |
| 4271 | } |
| 4272 | vmx->nested.posted_intr_nv = -1; |
| 4273 | |
| 4274 | /* Flush VMCS12 to guest memory */ |
| 4275 | kvm_vcpu_write_guest_page(vcpu, |
| 4276 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4277 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4278 | |
| 4279 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4280 | |
| 4281 | vmx->nested.current_vmptr = -1ull; |
| 4282 | } |
| 4283 | |
| 4284 | /* Emulate the VMXOFF instruction */ |
| 4285 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4286 | { |
| 4287 | if (!nested_vmx_check_permission(vcpu)) |
| 4288 | return 1; |
| 4289 | free_nested(vcpu); |
| 4290 | return nested_vmx_succeed(vcpu); |
| 4291 | } |
| 4292 | |
| 4293 | /* Emulate the VMCLEAR instruction */ |
| 4294 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 4295 | { |
| 4296 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4297 | u32 zero = 0; |
| 4298 | gpa_t vmptr; |
| 4299 | |
| 4300 | if (!nested_vmx_check_permission(vcpu)) |
| 4301 | return 1; |
| 4302 | |
| 4303 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
| 4304 | return 1; |
| 4305 | |
| 4306 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) |
| 4307 | return nested_vmx_failValid(vcpu, |
| 4308 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 4309 | |
| 4310 | if (vmptr == vmx->nested.vmxon_ptr) |
| 4311 | return nested_vmx_failValid(vcpu, |
| 4312 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 4313 | |
| 4314 | if (vmx->nested.hv_evmcs_page) { |
| 4315 | if (vmptr == vmx->nested.hv_evmcs_vmptr) |
| 4316 | nested_release_evmcs(vcpu); |
| 4317 | } else { |
| 4318 | if (vmptr == vmx->nested.current_vmptr) |
| 4319 | nested_release_vmcs12(vcpu); |
| 4320 | |
| 4321 | kvm_vcpu_write_guest(vcpu, |
| 4322 | vmptr + offsetof(struct vmcs12, |
| 4323 | launch_state), |
| 4324 | &zero, sizeof(zero)); |
| 4325 | } |
| 4326 | |
| 4327 | return nested_vmx_succeed(vcpu); |
| 4328 | } |
| 4329 | |
| 4330 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 4331 | |
| 4332 | /* Emulate the VMLAUNCH instruction */ |
| 4333 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 4334 | { |
| 4335 | return nested_vmx_run(vcpu, true); |
| 4336 | } |
| 4337 | |
| 4338 | /* Emulate the VMRESUME instruction */ |
| 4339 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 4340 | { |
| 4341 | |
| 4342 | return nested_vmx_run(vcpu, false); |
| 4343 | } |
| 4344 | |
| 4345 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 4346 | { |
| 4347 | unsigned long field; |
| 4348 | u64 field_value; |
| 4349 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4350 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4351 | gva_t gva = 0; |
| 4352 | struct vmcs12 *vmcs12; |
| 4353 | |
| 4354 | if (!nested_vmx_check_permission(vcpu)) |
| 4355 | return 1; |
| 4356 | |
| 4357 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) |
| 4358 | return nested_vmx_failInvalid(vcpu); |
| 4359 | |
| 4360 | if (!is_guest_mode(vcpu)) |
| 4361 | vmcs12 = get_vmcs12(vcpu); |
| 4362 | else { |
| 4363 | /* |
| 4364 | * When vmcs->vmcs_link_pointer is -1ull, any VMREAD |
| 4365 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 4366 | */ |
| 4367 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) |
| 4368 | return nested_vmx_failInvalid(vcpu); |
| 4369 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 4370 | } |
| 4371 | |
| 4372 | /* Decode instruction info and find the field to read */ |
| 4373 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 4374 | /* Read the field, zero-extended to a u64 field_value */ |
| 4375 | if (vmcs12_read_any(vmcs12, field, &field_value) < 0) |
| 4376 | return nested_vmx_failValid(vcpu, |
| 4377 | VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 4378 | |
| 4379 | /* |
| 4380 | * Now copy part of this value to register or memory, as requested. |
| 4381 | * Note that the number of bits actually copied is 32 or 64 depending |
| 4382 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 4383 | */ |
| 4384 | if (vmx_instruction_info & (1u << 10)) { |
| 4385 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
| 4386 | field_value); |
| 4387 | } else { |
| 4388 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 4389 | vmx_instruction_info, true, &gva)) |
| 4390 | return 1; |
| 4391 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
| 4392 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 4393 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 4394 | } |
| 4395 | |
| 4396 | return nested_vmx_succeed(vcpu); |
| 4397 | } |
| 4398 | |
| 4399 | |
| 4400 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 4401 | { |
| 4402 | unsigned long field; |
| 4403 | gva_t gva; |
| 4404 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4405 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4406 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4407 | |
| 4408 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 4409 | * mode, and eventually we need to write that into a field of several |
| 4410 | * possible lengths. The code below first zero-extends the value to 64 |
| 4411 | * bit (field_value), and then copies only the appropriate number of |
| 4412 | * bits into the vmcs12 field. |
| 4413 | */ |
| 4414 | u64 field_value = 0; |
| 4415 | struct x86_exception e; |
| 4416 | struct vmcs12 *vmcs12; |
| 4417 | |
| 4418 | if (!nested_vmx_check_permission(vcpu)) |
| 4419 | return 1; |
| 4420 | |
| 4421 | if (vmx->nested.current_vmptr == -1ull) |
| 4422 | return nested_vmx_failInvalid(vcpu); |
| 4423 | |
| 4424 | if (vmx_instruction_info & (1u << 10)) |
| 4425 | field_value = kvm_register_readl(vcpu, |
| 4426 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 4427 | else { |
| 4428 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 4429 | vmx_instruction_info, false, &gva)) |
| 4430 | return 1; |
| 4431 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 4432 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
| 4433 | kvm_inject_page_fault(vcpu, &e); |
| 4434 | return 1; |
| 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | |
| 4439 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 4440 | /* |
| 4441 | * If the vCPU supports "VMWRITE to any supported field in the |
| 4442 | * VMCS," then the "read-only" fields are actually read/write. |
| 4443 | */ |
| 4444 | if (vmcs_field_readonly(field) && |
| 4445 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
| 4446 | return nested_vmx_failValid(vcpu, |
| 4447 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 4448 | |
| 4449 | if (!is_guest_mode(vcpu)) |
| 4450 | vmcs12 = get_vmcs12(vcpu); |
| 4451 | else { |
| 4452 | /* |
| 4453 | * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE |
| 4454 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 4455 | */ |
| 4456 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) |
| 4457 | return nested_vmx_failInvalid(vcpu); |
| 4458 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 4459 | } |
| 4460 | |
| 4461 | if (vmcs12_write_any(vmcs12, field, field_value) < 0) |
| 4462 | return nested_vmx_failValid(vcpu, |
| 4463 | VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 4464 | |
| 4465 | /* |
| 4466 | * Do not track vmcs12 dirty-state if in guest-mode |
| 4467 | * as we actually dirty shadow vmcs12 instead of vmcs12. |
| 4468 | */ |
| 4469 | if (!is_guest_mode(vcpu)) { |
| 4470 | switch (field) { |
| 4471 | #define SHADOW_FIELD_RW(x) case x: |
| 4472 | #include "vmcs_shadow_fields.h" |
| 4473 | /* |
| 4474 | * The fields that can be updated by L1 without a vmexit are |
| 4475 | * always updated in the vmcs02, the others go down the slow |
| 4476 | * path of prepare_vmcs02. |
| 4477 | */ |
| 4478 | break; |
| 4479 | default: |
| 4480 | vmx->nested.dirty_vmcs12 = true; |
| 4481 | break; |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | return nested_vmx_succeed(vcpu); |
| 4486 | } |
| 4487 | |
| 4488 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 4489 | { |
| 4490 | vmx->nested.current_vmptr = vmptr; |
| 4491 | if (enable_shadow_vmcs) { |
| 4492 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4493 | SECONDARY_EXEC_SHADOW_VMCS); |
| 4494 | vmcs_write64(VMCS_LINK_POINTER, |
| 4495 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 4496 | vmx->nested.need_vmcs12_sync = true; |
| 4497 | } |
| 4498 | vmx->nested.dirty_vmcs12 = true; |
| 4499 | } |
| 4500 | |
| 4501 | /* Emulate the VMPTRLD instruction */ |
| 4502 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 4503 | { |
| 4504 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4505 | gpa_t vmptr; |
| 4506 | |
| 4507 | if (!nested_vmx_check_permission(vcpu)) |
| 4508 | return 1; |
| 4509 | |
| 4510 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
| 4511 | return 1; |
| 4512 | |
| 4513 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) |
| 4514 | return nested_vmx_failValid(vcpu, |
| 4515 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 4516 | |
| 4517 | if (vmptr == vmx->nested.vmxon_ptr) |
| 4518 | return nested_vmx_failValid(vcpu, |
| 4519 | VMXERR_VMPTRLD_VMXON_POINTER); |
| 4520 | |
| 4521 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
| 4522 | if (vmx->nested.hv_evmcs) |
| 4523 | return 1; |
| 4524 | |
| 4525 | if (vmx->nested.current_vmptr != vmptr) { |
| 4526 | struct vmcs12 *new_vmcs12; |
| 4527 | struct page *page; |
| 4528 | |
| 4529 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 4530 | if (is_error_page(page)) { |
| 4531 | /* |
| 4532 | * Reads from an unbacked page return all 1s, |
| 4533 | * which means that the 32 bits located at the |
| 4534 | * given physical address won't match the required |
| 4535 | * VMCS12_REVISION identifier. |
| 4536 | */ |
Vitaly Kuznetsov | 826c136 | 2019-01-09 18:22:56 +0100 | [diff] [blame] | 4537 | return nested_vmx_failValid(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4538 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4539 | } |
| 4540 | new_vmcs12 = kmap(page); |
| 4541 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 4542 | (new_vmcs12->hdr.shadow_vmcs && |
| 4543 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
| 4544 | kunmap(page); |
| 4545 | kvm_release_page_clean(page); |
| 4546 | return nested_vmx_failValid(vcpu, |
| 4547 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 4548 | } |
| 4549 | |
| 4550 | nested_release_vmcs12(vcpu); |
| 4551 | |
| 4552 | /* |
| 4553 | * Load VMCS12 from guest memory since it is not already |
| 4554 | * cached. |
| 4555 | */ |
| 4556 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 4557 | kunmap(page); |
| 4558 | kvm_release_page_clean(page); |
| 4559 | |
| 4560 | set_current_vmptr(vmx, vmptr); |
| 4561 | } |
| 4562 | |
| 4563 | return nested_vmx_succeed(vcpu); |
| 4564 | } |
| 4565 | |
| 4566 | /* Emulate the VMPTRST instruction */ |
| 4567 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 4568 | { |
| 4569 | unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION); |
| 4570 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4571 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 4572 | struct x86_exception e; |
| 4573 | gva_t gva; |
| 4574 | |
| 4575 | if (!nested_vmx_check_permission(vcpu)) |
| 4576 | return 1; |
| 4577 | |
| 4578 | if (unlikely(to_vmx(vcpu)->nested.hv_evmcs)) |
| 4579 | return 1; |
| 4580 | |
| 4581 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva)) |
| 4582 | return 1; |
| 4583 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
| 4584 | if (kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 4585 | sizeof(gpa_t), &e)) { |
| 4586 | kvm_inject_page_fault(vcpu, &e); |
| 4587 | return 1; |
| 4588 | } |
| 4589 | return nested_vmx_succeed(vcpu); |
| 4590 | } |
| 4591 | |
| 4592 | /* Emulate the INVEPT instruction */ |
| 4593 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 4594 | { |
| 4595 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4596 | u32 vmx_instruction_info, types; |
| 4597 | unsigned long type; |
| 4598 | gva_t gva; |
| 4599 | struct x86_exception e; |
| 4600 | struct { |
| 4601 | u64 eptp, gpa; |
| 4602 | } operand; |
| 4603 | |
| 4604 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 4605 | SECONDARY_EXEC_ENABLE_EPT) || |
| 4606 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 4607 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4608 | return 1; |
| 4609 | } |
| 4610 | |
| 4611 | if (!nested_vmx_check_permission(vcpu)) |
| 4612 | return 1; |
| 4613 | |
| 4614 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4615 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 4616 | |
| 4617 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 4618 | |
| 4619 | if (type >= 32 || !(types & (1 << type))) |
| 4620 | return nested_vmx_failValid(vcpu, |
| 4621 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 4622 | |
| 4623 | /* According to the Intel VMX instruction reference, the memory |
| 4624 | * operand is read even if it isn't needed (e.g., for type==global) |
| 4625 | */ |
| 4626 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 4627 | vmx_instruction_info, false, &gva)) |
| 4628 | return 1; |
| 4629 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 4630 | kvm_inject_page_fault(vcpu, &e); |
| 4631 | return 1; |
| 4632 | } |
| 4633 | |
| 4634 | switch (type) { |
| 4635 | case VMX_EPT_EXTENT_GLOBAL: |
| 4636 | /* |
| 4637 | * TODO: track mappings and invalidate |
| 4638 | * single context requests appropriately |
| 4639 | */ |
| 4640 | case VMX_EPT_EXTENT_CONTEXT: |
| 4641 | kvm_mmu_sync_roots(vcpu); |
| 4642 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 4643 | break; |
| 4644 | default: |
| 4645 | BUG_ON(1); |
| 4646 | break; |
| 4647 | } |
| 4648 | |
| 4649 | return nested_vmx_succeed(vcpu); |
| 4650 | } |
| 4651 | |
| 4652 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 4653 | { |
| 4654 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4655 | u32 vmx_instruction_info; |
| 4656 | unsigned long type, types; |
| 4657 | gva_t gva; |
| 4658 | struct x86_exception e; |
| 4659 | struct { |
| 4660 | u64 vpid; |
| 4661 | u64 gla; |
| 4662 | } operand; |
| 4663 | u16 vpid02; |
| 4664 | |
| 4665 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 4666 | SECONDARY_EXEC_ENABLE_VPID) || |
| 4667 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 4668 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4669 | return 1; |
| 4670 | } |
| 4671 | |
| 4672 | if (!nested_vmx_check_permission(vcpu)) |
| 4673 | return 1; |
| 4674 | |
| 4675 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4676 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 4677 | |
| 4678 | types = (vmx->nested.msrs.vpid_caps & |
| 4679 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 4680 | |
| 4681 | if (type >= 32 || !(types & (1 << type))) |
| 4682 | return nested_vmx_failValid(vcpu, |
| 4683 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 4684 | |
| 4685 | /* according to the intel vmx instruction reference, the memory |
| 4686 | * operand is read even if it isn't needed (e.g., for type==global) |
| 4687 | */ |
| 4688 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 4689 | vmx_instruction_info, false, &gva)) |
| 4690 | return 1; |
| 4691 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 4692 | kvm_inject_page_fault(vcpu, &e); |
| 4693 | return 1; |
| 4694 | } |
| 4695 | if (operand.vpid >> 16) |
| 4696 | return nested_vmx_failValid(vcpu, |
| 4697 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 4698 | |
| 4699 | vpid02 = nested_get_vpid02(vcpu); |
| 4700 | switch (type) { |
| 4701 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 4702 | if (!operand.vpid || |
| 4703 | is_noncanonical_address(operand.gla, vcpu)) |
| 4704 | return nested_vmx_failValid(vcpu, |
| 4705 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 4706 | if (cpu_has_vmx_invvpid_individual_addr()) { |
| 4707 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
| 4708 | vpid02, operand.gla); |
| 4709 | } else |
| 4710 | __vmx_flush_tlb(vcpu, vpid02, false); |
| 4711 | break; |
| 4712 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 4713 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 4714 | if (!operand.vpid) |
| 4715 | return nested_vmx_failValid(vcpu, |
| 4716 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 4717 | __vmx_flush_tlb(vcpu, vpid02, false); |
| 4718 | break; |
| 4719 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
| 4720 | __vmx_flush_tlb(vcpu, vpid02, false); |
| 4721 | break; |
| 4722 | default: |
| 4723 | WARN_ON_ONCE(1); |
| 4724 | return kvm_skip_emulated_instruction(vcpu); |
| 4725 | } |
| 4726 | |
| 4727 | return nested_vmx_succeed(vcpu); |
| 4728 | } |
| 4729 | |
| 4730 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 4731 | struct vmcs12 *vmcs12) |
| 4732 | { |
| 4733 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4734 | u64 address; |
| 4735 | bool accessed_dirty; |
| 4736 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4737 | |
| 4738 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 4739 | !nested_cpu_has_ept(vmcs12)) |
| 4740 | return 1; |
| 4741 | |
| 4742 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 4743 | return 1; |
| 4744 | |
| 4745 | |
| 4746 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 4747 | &address, index * 8, 8)) |
| 4748 | return 1; |
| 4749 | |
| 4750 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
| 4751 | |
| 4752 | /* |
| 4753 | * If the (L2) guest does a vmfunc to the currently |
| 4754 | * active ept pointer, we don't have to do anything else |
| 4755 | */ |
| 4756 | if (vmcs12->ept_pointer != address) { |
| 4757 | if (!valid_ept_address(vcpu, address)) |
| 4758 | return 1; |
| 4759 | |
| 4760 | kvm_mmu_unload(vcpu); |
| 4761 | mmu->ept_ad = accessed_dirty; |
| 4762 | mmu->mmu_role.base.ad_disabled = !accessed_dirty; |
| 4763 | vmcs12->ept_pointer = address; |
| 4764 | /* |
| 4765 | * TODO: Check what's the correct approach in case |
| 4766 | * mmu reload fails. Currently, we just let the next |
| 4767 | * reload potentially fail |
| 4768 | */ |
| 4769 | kvm_mmu_reload(vcpu); |
| 4770 | } |
| 4771 | |
| 4772 | return 0; |
| 4773 | } |
| 4774 | |
| 4775 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 4776 | { |
| 4777 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4778 | struct vmcs12 *vmcs12; |
| 4779 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 4780 | |
| 4781 | /* |
| 4782 | * VMFUNC is only supported for nested guests, but we always enable the |
| 4783 | * secondary control for simplicity; for non-nested mode, fake that we |
| 4784 | * didn't by injecting #UD. |
| 4785 | */ |
| 4786 | if (!is_guest_mode(vcpu)) { |
| 4787 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4788 | return 1; |
| 4789 | } |
| 4790 | |
| 4791 | vmcs12 = get_vmcs12(vcpu); |
| 4792 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 4793 | goto fail; |
| 4794 | |
| 4795 | switch (function) { |
| 4796 | case 0: |
| 4797 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 4798 | goto fail; |
| 4799 | break; |
| 4800 | default: |
| 4801 | goto fail; |
| 4802 | } |
| 4803 | return kvm_skip_emulated_instruction(vcpu); |
| 4804 | |
| 4805 | fail: |
| 4806 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 4807 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 4808 | vmcs_readl(EXIT_QUALIFICATION)); |
| 4809 | return 1; |
| 4810 | } |
| 4811 | |
| 4812 | |
| 4813 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 4814 | struct vmcs12 *vmcs12) |
| 4815 | { |
| 4816 | unsigned long exit_qualification; |
| 4817 | gpa_t bitmap, last_bitmap; |
| 4818 | unsigned int port; |
| 4819 | int size; |
| 4820 | u8 b; |
| 4821 | |
| 4822 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 4823 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 4824 | |
| 4825 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4826 | |
| 4827 | port = exit_qualification >> 16; |
| 4828 | size = (exit_qualification & 7) + 1; |
| 4829 | |
| 4830 | last_bitmap = (gpa_t)-1; |
| 4831 | b = -1; |
| 4832 | |
| 4833 | while (size > 0) { |
| 4834 | if (port < 0x8000) |
| 4835 | bitmap = vmcs12->io_bitmap_a; |
| 4836 | else if (port < 0x10000) |
| 4837 | bitmap = vmcs12->io_bitmap_b; |
| 4838 | else |
| 4839 | return true; |
| 4840 | bitmap += (port & 0x7fff) / 8; |
| 4841 | |
| 4842 | if (last_bitmap != bitmap) |
| 4843 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 4844 | return true; |
| 4845 | if (b & (1 << (port & 7))) |
| 4846 | return true; |
| 4847 | |
| 4848 | port++; |
| 4849 | size--; |
| 4850 | last_bitmap = bitmap; |
| 4851 | } |
| 4852 | |
| 4853 | return false; |
| 4854 | } |
| 4855 | |
| 4856 | /* |
| 4857 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 4858 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 4859 | * disinterest in the current event (read or write a specific MSR) by using an |
| 4860 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 4861 | */ |
| 4862 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 4863 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 4864 | { |
| 4865 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4866 | gpa_t bitmap; |
| 4867 | |
| 4868 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 4869 | return true; |
| 4870 | |
| 4871 | /* |
| 4872 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 4873 | * for the four combinations of read/write and low/high MSR numbers. |
| 4874 | * First we need to figure out which of the four to use: |
| 4875 | */ |
| 4876 | bitmap = vmcs12->msr_bitmap; |
| 4877 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 4878 | bitmap += 2048; |
| 4879 | if (msr_index >= 0xc0000000) { |
| 4880 | msr_index -= 0xc0000000; |
| 4881 | bitmap += 1024; |
| 4882 | } |
| 4883 | |
| 4884 | /* Then read the msr_index'th bit from this bitmap: */ |
| 4885 | if (msr_index < 1024*8) { |
| 4886 | unsigned char b; |
| 4887 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 4888 | return true; |
| 4889 | return 1 & (b >> (msr_index & 7)); |
| 4890 | } else |
| 4891 | return true; /* let L1 handle the wrong parameter */ |
| 4892 | } |
| 4893 | |
| 4894 | /* |
| 4895 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 4896 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 4897 | * intercept (via guest_host_mask etc.) the current event. |
| 4898 | */ |
| 4899 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 4900 | struct vmcs12 *vmcs12) |
| 4901 | { |
| 4902 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4903 | int cr = exit_qualification & 15; |
| 4904 | int reg; |
| 4905 | unsigned long val; |
| 4906 | |
| 4907 | switch ((exit_qualification >> 4) & 3) { |
| 4908 | case 0: /* mov to cr */ |
| 4909 | reg = (exit_qualification >> 8) & 15; |
| 4910 | val = kvm_register_readl(vcpu, reg); |
| 4911 | switch (cr) { |
| 4912 | case 0: |
| 4913 | if (vmcs12->cr0_guest_host_mask & |
| 4914 | (val ^ vmcs12->cr0_read_shadow)) |
| 4915 | return true; |
| 4916 | break; |
| 4917 | case 3: |
| 4918 | if ((vmcs12->cr3_target_count >= 1 && |
| 4919 | vmcs12->cr3_target_value0 == val) || |
| 4920 | (vmcs12->cr3_target_count >= 2 && |
| 4921 | vmcs12->cr3_target_value1 == val) || |
| 4922 | (vmcs12->cr3_target_count >= 3 && |
| 4923 | vmcs12->cr3_target_value2 == val) || |
| 4924 | (vmcs12->cr3_target_count >= 4 && |
| 4925 | vmcs12->cr3_target_value3 == val)) |
| 4926 | return false; |
| 4927 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 4928 | return true; |
| 4929 | break; |
| 4930 | case 4: |
| 4931 | if (vmcs12->cr4_guest_host_mask & |
| 4932 | (vmcs12->cr4_read_shadow ^ val)) |
| 4933 | return true; |
| 4934 | break; |
| 4935 | case 8: |
| 4936 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 4937 | return true; |
| 4938 | break; |
| 4939 | } |
| 4940 | break; |
| 4941 | case 2: /* clts */ |
| 4942 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 4943 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 4944 | return true; |
| 4945 | break; |
| 4946 | case 1: /* mov from cr */ |
| 4947 | switch (cr) { |
| 4948 | case 3: |
| 4949 | if (vmcs12->cpu_based_vm_exec_control & |
| 4950 | CPU_BASED_CR3_STORE_EXITING) |
| 4951 | return true; |
| 4952 | break; |
| 4953 | case 8: |
| 4954 | if (vmcs12->cpu_based_vm_exec_control & |
| 4955 | CPU_BASED_CR8_STORE_EXITING) |
| 4956 | return true; |
| 4957 | break; |
| 4958 | } |
| 4959 | break; |
| 4960 | case 3: /* lmsw */ |
| 4961 | /* |
| 4962 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 4963 | * cr0. Other attempted changes are ignored, with no exit. |
| 4964 | */ |
| 4965 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 4966 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 4967 | (val ^ vmcs12->cr0_read_shadow)) |
| 4968 | return true; |
| 4969 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 4970 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 4971 | (val & 0x1)) |
| 4972 | return true; |
| 4973 | break; |
| 4974 | } |
| 4975 | return false; |
| 4976 | } |
| 4977 | |
| 4978 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 4979 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 4980 | { |
| 4981 | u32 vmx_instruction_info; |
| 4982 | unsigned long field; |
| 4983 | u8 b; |
| 4984 | |
| 4985 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 4986 | return true; |
| 4987 | |
| 4988 | /* Decode instruction info and find the field to access */ |
| 4989 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4990 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 4991 | |
| 4992 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 4993 | if (field >> 15) |
| 4994 | return true; |
| 4995 | |
| 4996 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 4997 | return true; |
| 4998 | |
| 4999 | return 1 & (b >> (field & 7)); |
| 5000 | } |
| 5001 | |
| 5002 | /* |
| 5003 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 5004 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 5005 | * when in is_guest_mode (L2). |
| 5006 | */ |
| 5007 | bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 5008 | { |
| 5009 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5010 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5011 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5012 | |
| 5013 | if (vmx->nested.nested_run_pending) |
| 5014 | return false; |
| 5015 | |
| 5016 | if (unlikely(vmx->fail)) { |
| 5017 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 5018 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 5019 | return true; |
| 5020 | } |
| 5021 | |
| 5022 | /* |
| 5023 | * The host physical addresses of some pages of guest memory |
| 5024 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 5025 | * Page). The CPU may write to these pages via their host |
| 5026 | * physical address while L2 is running, bypassing any |
| 5027 | * address-translation-based dirty tracking (e.g. EPT write |
| 5028 | * protection). |
| 5029 | * |
| 5030 | * Mark them dirty on every exit from L2 to prevent them from |
| 5031 | * getting out of sync with dirty tracking. |
| 5032 | */ |
| 5033 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 5034 | |
| 5035 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 5036 | vmcs_readl(EXIT_QUALIFICATION), |
| 5037 | vmx->idt_vectoring_info, |
| 5038 | intr_info, |
| 5039 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 5040 | KVM_ISA_VMX); |
| 5041 | |
| 5042 | switch (exit_reason) { |
| 5043 | case EXIT_REASON_EXCEPTION_NMI: |
| 5044 | if (is_nmi(intr_info)) |
| 5045 | return false; |
| 5046 | else if (is_page_fault(intr_info)) |
| 5047 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
| 5048 | else if (is_debug(intr_info) && |
| 5049 | vcpu->guest_debug & |
| 5050 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5051 | return false; |
| 5052 | else if (is_breakpoint(intr_info) && |
| 5053 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5054 | return false; |
| 5055 | return vmcs12->exception_bitmap & |
| 5056 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5057 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5058 | return false; |
| 5059 | case EXIT_REASON_TRIPLE_FAULT: |
| 5060 | return true; |
| 5061 | case EXIT_REASON_PENDING_INTERRUPT: |
| 5062 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
| 5063 | case EXIT_REASON_NMI_WINDOW: |
| 5064 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
| 5065 | case EXIT_REASON_TASK_SWITCH: |
| 5066 | return true; |
| 5067 | case EXIT_REASON_CPUID: |
| 5068 | return true; |
| 5069 | case EXIT_REASON_HLT: |
| 5070 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5071 | case EXIT_REASON_INVD: |
| 5072 | return true; |
| 5073 | case EXIT_REASON_INVLPG: |
| 5074 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5075 | case EXIT_REASON_RDPMC: |
| 5076 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5077 | case EXIT_REASON_RDRAND: |
| 5078 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5079 | case EXIT_REASON_RDSEED: |
| 5080 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5081 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5082 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5083 | case EXIT_REASON_VMREAD: |
| 5084 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5085 | vmcs12->vmread_bitmap); |
| 5086 | case EXIT_REASON_VMWRITE: |
| 5087 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5088 | vmcs12->vmwrite_bitmap); |
| 5089 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5090 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5091 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5092 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5093 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5094 | /* |
| 5095 | * VMX instructions trap unconditionally. This allows L1 to |
| 5096 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5097 | */ |
| 5098 | return true; |
| 5099 | case EXIT_REASON_CR_ACCESS: |
| 5100 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5101 | case EXIT_REASON_DR_ACCESS: |
| 5102 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5103 | case EXIT_REASON_IO_INSTRUCTION: |
| 5104 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5105 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5106 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5107 | case EXIT_REASON_MSR_READ: |
| 5108 | case EXIT_REASON_MSR_WRITE: |
| 5109 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5110 | case EXIT_REASON_INVALID_STATE: |
| 5111 | return true; |
| 5112 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5113 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5114 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 5115 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
| 5116 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5117 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5118 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5119 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5120 | nested_cpu_has2(vmcs12, |
| 5121 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5122 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5123 | return false; |
| 5124 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5125 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 5126 | case EXIT_REASON_APIC_ACCESS: |
| 5127 | case EXIT_REASON_APIC_WRITE: |
| 5128 | case EXIT_REASON_EOI_INDUCED: |
| 5129 | /* |
| 5130 | * The controls for "virtualize APIC accesses," "APIC- |
| 5131 | * register virtualization," and "virtual-interrupt |
| 5132 | * delivery" only come from vmcs12. |
| 5133 | */ |
| 5134 | return true; |
| 5135 | case EXIT_REASON_EPT_VIOLATION: |
| 5136 | /* |
| 5137 | * L0 always deals with the EPT violation. If nested EPT is |
| 5138 | * used, and the nested mmu code discovers that the address is |
| 5139 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5140 | * will be injected with nested_ept_inject_page_fault() |
| 5141 | */ |
| 5142 | return false; |
| 5143 | case EXIT_REASON_EPT_MISCONFIG: |
| 5144 | /* |
| 5145 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5146 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5147 | * (EPT on EPT). So any problems with the structure of the |
| 5148 | * table is L0's fault. |
| 5149 | */ |
| 5150 | return false; |
| 5151 | case EXIT_REASON_INVPCID: |
| 5152 | return |
| 5153 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 5154 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5155 | case EXIT_REASON_WBINVD: |
| 5156 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5157 | case EXIT_REASON_XSETBV: |
| 5158 | return true; |
| 5159 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 5160 | /* |
| 5161 | * This should never happen, since it is not possible to |
| 5162 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 5163 | * If if it were, XSS would have to be checked against |
| 5164 | * the XSS exit bitmap in vmcs12. |
| 5165 | */ |
| 5166 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
| 5167 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5168 | return false; |
| 5169 | case EXIT_REASON_PML_FULL: |
| 5170 | /* We emulate PML support to L1. */ |
| 5171 | return false; |
| 5172 | case EXIT_REASON_VMFUNC: |
| 5173 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5174 | return false; |
| 5175 | case EXIT_REASON_ENCLS: |
| 5176 | /* SGX is never exposed to L1 */ |
| 5177 | return false; |
| 5178 | default: |
| 5179 | return true; |
| 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | |
| 5184 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 5185 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 5186 | u32 user_data_size) |
| 5187 | { |
| 5188 | struct vcpu_vmx *vmx; |
| 5189 | struct vmcs12 *vmcs12; |
| 5190 | struct kvm_nested_state kvm_state = { |
| 5191 | .flags = 0, |
| 5192 | .format = 0, |
| 5193 | .size = sizeof(kvm_state), |
| 5194 | .vmx.vmxon_pa = -1ull, |
| 5195 | .vmx.vmcs_pa = -1ull, |
| 5196 | }; |
| 5197 | |
| 5198 | if (!vcpu) |
| 5199 | return kvm_state.size + 2 * VMCS12_SIZE; |
| 5200 | |
| 5201 | vmx = to_vmx(vcpu); |
| 5202 | vmcs12 = get_vmcs12(vcpu); |
| 5203 | |
| 5204 | if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled) |
| 5205 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 5206 | |
| 5207 | if (nested_vmx_allowed(vcpu) && |
| 5208 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
| 5209 | kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 5210 | kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr; |
| 5211 | |
| 5212 | if (vmx_has_valid_vmcs12(vcpu)) { |
| 5213 | kvm_state.size += VMCS12_SIZE; |
| 5214 | |
| 5215 | if (is_guest_mode(vcpu) && |
| 5216 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 5217 | vmcs12->vmcs_link_pointer != -1ull) |
| 5218 | kvm_state.size += VMCS12_SIZE; |
| 5219 | } |
| 5220 | |
| 5221 | if (vmx->nested.smm.vmxon) |
| 5222 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
| 5223 | |
| 5224 | if (vmx->nested.smm.guest_mode) |
| 5225 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
| 5226 | |
| 5227 | if (is_guest_mode(vcpu)) { |
| 5228 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 5229 | |
| 5230 | if (vmx->nested.nested_run_pending) |
| 5231 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | if (user_data_size < kvm_state.size) |
| 5236 | goto out; |
| 5237 | |
| 5238 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 5239 | return -EFAULT; |
| 5240 | |
| 5241 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 5242 | goto out; |
| 5243 | |
| 5244 | /* |
| 5245 | * When running L2, the authoritative vmcs12 state is in the |
| 5246 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 5247 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
| 5248 | * need_vmcs12_sync is set, in which case, the authoritative |
| 5249 | * vmcs12 state is in the vmcs12 already. |
| 5250 | */ |
| 5251 | if (is_guest_mode(vcpu)) { |
| 5252 | sync_vmcs12(vcpu, vmcs12); |
| 5253 | } else if (!vmx->nested.need_vmcs12_sync) { |
| 5254 | if (vmx->nested.hv_evmcs) |
| 5255 | copy_enlightened_to_vmcs12(vmx); |
| 5256 | else if (enable_shadow_vmcs) |
| 5257 | copy_shadow_to_vmcs12(vmx); |
| 5258 | } |
| 5259 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 5260 | /* |
| 5261 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 5262 | * of the struct. |
| 5263 | */ |
| 5264 | if (copy_to_user(user_kvm_nested_state->data, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5265 | return -EFAULT; |
| 5266 | |
| 5267 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 5268 | vmcs12->vmcs_link_pointer != -1ull) { |
| 5269 | if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 5270 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5271 | return -EFAULT; |
| 5272 | } |
| 5273 | |
| 5274 | out: |
| 5275 | return kvm_state.size; |
| 5276 | } |
| 5277 | |
| 5278 | /* |
| 5279 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 5280 | */ |
| 5281 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 5282 | { |
| 5283 | if (is_guest_mode(vcpu)) { |
| 5284 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 5285 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 5286 | } |
| 5287 | free_nested(vcpu); |
| 5288 | } |
| 5289 | |
| 5290 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 5291 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 5292 | struct kvm_nested_state *kvm_state) |
| 5293 | { |
| 5294 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5295 | struct vmcs12 *vmcs12; |
| 5296 | u32 exit_qual; |
| 5297 | int ret; |
| 5298 | |
| 5299 | if (kvm_state->format != 0) |
| 5300 | return -EINVAL; |
| 5301 | |
| 5302 | if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) |
| 5303 | nested_enable_evmcs(vcpu, NULL); |
| 5304 | |
| 5305 | if (!nested_vmx_allowed(vcpu)) |
| 5306 | return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL; |
| 5307 | |
| 5308 | if (kvm_state->vmx.vmxon_pa == -1ull) { |
| 5309 | if (kvm_state->vmx.smm.flags) |
| 5310 | return -EINVAL; |
| 5311 | |
| 5312 | if (kvm_state->vmx.vmcs_pa != -1ull) |
| 5313 | return -EINVAL; |
| 5314 | |
| 5315 | vmx_leave_nested(vcpu); |
| 5316 | return 0; |
| 5317 | } |
| 5318 | |
| 5319 | if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa)) |
| 5320 | return -EINVAL; |
| 5321 | |
| 5322 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 5323 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 5324 | return -EINVAL; |
| 5325 | |
| 5326 | if (kvm_state->vmx.smm.flags & |
| 5327 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 5328 | return -EINVAL; |
| 5329 | |
| 5330 | /* |
| 5331 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 5332 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 5333 | * must be zero. |
| 5334 | */ |
| 5335 | if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags) |
| 5336 | return -EINVAL; |
| 5337 | |
| 5338 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 5339 | !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
| 5340 | return -EINVAL; |
| 5341 | |
| 5342 | vmx_leave_nested(vcpu); |
| 5343 | if (kvm_state->vmx.vmxon_pa == -1ull) |
| 5344 | return 0; |
| 5345 | |
| 5346 | vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa; |
| 5347 | ret = enter_vmx_operation(vcpu); |
| 5348 | if (ret) |
| 5349 | return ret; |
| 5350 | |
| 5351 | /* Empty 'VMXON' state is permitted */ |
| 5352 | if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12)) |
| 5353 | return 0; |
| 5354 | |
| 5355 | if (kvm_state->vmx.vmcs_pa != -1ull) { |
| 5356 | if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa || |
| 5357 | !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa)) |
| 5358 | return -EINVAL; |
| 5359 | |
| 5360 | set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa); |
| 5361 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 5362 | /* |
| 5363 | * Sync eVMCS upon entry as we may not have |
| 5364 | * HV_X64_MSR_VP_ASSIST_PAGE set up yet. |
| 5365 | */ |
| 5366 | vmx->nested.need_vmcs12_sync = true; |
| 5367 | } else { |
| 5368 | return -EINVAL; |
| 5369 | } |
| 5370 | |
| 5371 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
| 5372 | vmx->nested.smm.vmxon = true; |
| 5373 | vmx->nested.vmxon = false; |
| 5374 | |
| 5375 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) |
| 5376 | vmx->nested.smm.guest_mode = true; |
| 5377 | } |
| 5378 | |
| 5379 | vmcs12 = get_vmcs12(vcpu); |
| 5380 | if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12))) |
| 5381 | return -EFAULT; |
| 5382 | |
| 5383 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 5384 | return -EINVAL; |
| 5385 | |
| 5386 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 5387 | return 0; |
| 5388 | |
| 5389 | vmx->nested.nested_run_pending = |
| 5390 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 5391 | |
| 5392 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 5393 | vmcs12->vmcs_link_pointer != -1ull) { |
| 5394 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 5395 | |
| 5396 | if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12)) |
| 5397 | return -EINVAL; |
| 5398 | |
| 5399 | if (copy_from_user(shadow_vmcs12, |
| 5400 | user_kvm_nested_state->data + VMCS12_SIZE, |
| 5401 | sizeof(*vmcs12))) |
| 5402 | return -EFAULT; |
| 5403 | |
| 5404 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 5405 | !shadow_vmcs12->hdr.shadow_vmcs) |
| 5406 | return -EINVAL; |
| 5407 | } |
| 5408 | |
Krish Sadhukhan | 16322a3b | 2018-12-12 13:30:06 -0500 | [diff] [blame] | 5409 | if (nested_vmx_check_vmentry_prereqs(vcpu, vmcs12) || |
| 5410 | nested_vmx_check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5411 | return -EINVAL; |
| 5412 | |
| 5413 | vmx->nested.dirty_vmcs12 = true; |
| 5414 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
| 5415 | if (ret) |
| 5416 | return -EINVAL; |
| 5417 | |
| 5418 | return 0; |
| 5419 | } |
| 5420 | |
| 5421 | void nested_vmx_vcpu_setup(void) |
| 5422 | { |
| 5423 | if (enable_shadow_vmcs) { |
| 5424 | /* |
| 5425 | * At vCPU creation, "VMWRITE to any supported field |
| 5426 | * in the VMCS" is supported, so use the more |
| 5427 | * permissive vmx_vmread_bitmap to specify both read |
| 5428 | * and write permissions for the shadow VMCS. |
| 5429 | */ |
| 5430 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5431 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5432 | } |
| 5433 | } |
| 5434 | |
| 5435 | /* |
| 5436 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 5437 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 5438 | * The same values should also be used to verify that vmcs12 control fields are |
| 5439 | * valid during nested entry from L1 to L2. |
| 5440 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 5441 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 5442 | * bit in the high half is on if the corresponding bit in the control field |
| 5443 | * may be on. See also vmx_control_verify(). |
| 5444 | */ |
| 5445 | void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps, |
| 5446 | bool apicv) |
| 5447 | { |
| 5448 | /* |
| 5449 | * Note that as a general rule, the high half of the MSRs (bits in |
| 5450 | * the control fields which may be 1) should be initialized by the |
| 5451 | * intersection of the underlying hardware's MSR (i.e., features which |
| 5452 | * can be supported) and the list of features we want to expose - |
| 5453 | * because they are known to be properly supported in our code. |
| 5454 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 5455 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 5456 | * reason is that if one of these bits is necessary, it will appear |
| 5457 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 5458 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 5459 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
| 5460 | * These rules have exceptions below. |
| 5461 | */ |
| 5462 | |
| 5463 | /* pin-based controls */ |
| 5464 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 5465 | msrs->pinbased_ctls_low, |
| 5466 | msrs->pinbased_ctls_high); |
| 5467 | msrs->pinbased_ctls_low |= |
| 5468 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 5469 | msrs->pinbased_ctls_high &= |
| 5470 | PIN_BASED_EXT_INTR_MASK | |
| 5471 | PIN_BASED_NMI_EXITING | |
| 5472 | PIN_BASED_VIRTUAL_NMIS | |
| 5473 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
| 5474 | msrs->pinbased_ctls_high |= |
| 5475 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 5476 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 5477 | |
| 5478 | /* exit controls */ |
| 5479 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 5480 | msrs->exit_ctls_low, |
| 5481 | msrs->exit_ctls_high); |
| 5482 | msrs->exit_ctls_low = |
| 5483 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 5484 | |
| 5485 | msrs->exit_ctls_high &= |
| 5486 | #ifdef CONFIG_X86_64 |
| 5487 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 5488 | #endif |
| 5489 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
| 5490 | msrs->exit_ctls_high |= |
| 5491 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 5492 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 5493 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 5494 | |
| 5495 | /* We support free control of debug control saving. */ |
| 5496 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 5497 | |
| 5498 | /* entry controls */ |
| 5499 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 5500 | msrs->entry_ctls_low, |
| 5501 | msrs->entry_ctls_high); |
| 5502 | msrs->entry_ctls_low = |
| 5503 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 5504 | msrs->entry_ctls_high &= |
| 5505 | #ifdef CONFIG_X86_64 |
| 5506 | VM_ENTRY_IA32E_MODE | |
| 5507 | #endif |
| 5508 | VM_ENTRY_LOAD_IA32_PAT; |
| 5509 | msrs->entry_ctls_high |= |
| 5510 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 5511 | |
| 5512 | /* We support free control of debug control loading. */ |
| 5513 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 5514 | |
| 5515 | /* cpu-based controls */ |
| 5516 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 5517 | msrs->procbased_ctls_low, |
| 5518 | msrs->procbased_ctls_high); |
| 5519 | msrs->procbased_ctls_low = |
| 5520 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 5521 | msrs->procbased_ctls_high &= |
| 5522 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 5523 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
| 5524 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 5525 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 5526 | CPU_BASED_CR3_STORE_EXITING | |
| 5527 | #ifdef CONFIG_X86_64 |
| 5528 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 5529 | #endif |
| 5530 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 5531 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 5532 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 5533 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 5534 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 5535 | /* |
| 5536 | * We can allow some features even when not supported by the |
| 5537 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 5538 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 5539 | * without MSR bitmaps. |
| 5540 | */ |
| 5541 | msrs->procbased_ctls_high |= |
| 5542 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 5543 | CPU_BASED_USE_MSR_BITMAPS; |
| 5544 | |
| 5545 | /* We support free control of CR3 access interception. */ |
| 5546 | msrs->procbased_ctls_low &= |
| 5547 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 5548 | |
| 5549 | /* |
| 5550 | * secondary cpu-based controls. Do not include those that |
| 5551 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 5552 | */ |
| 5553 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 5554 | msrs->secondary_ctls_low, |
| 5555 | msrs->secondary_ctls_high); |
| 5556 | msrs->secondary_ctls_low = 0; |
| 5557 | msrs->secondary_ctls_high &= |
| 5558 | SECONDARY_EXEC_DESC | |
| 5559 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 5560 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5561 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 5562 | SECONDARY_EXEC_WBINVD_EXITING; |
| 5563 | |
| 5564 | /* |
| 5565 | * We can emulate "VMCS shadowing," even if the hardware |
| 5566 | * doesn't support it. |
| 5567 | */ |
| 5568 | msrs->secondary_ctls_high |= |
| 5569 | SECONDARY_EXEC_SHADOW_VMCS; |
| 5570 | |
| 5571 | if (enable_ept) { |
| 5572 | /* nested EPT: emulate EPT also to L1 */ |
| 5573 | msrs->secondary_ctls_high |= |
| 5574 | SECONDARY_EXEC_ENABLE_EPT; |
| 5575 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
| 5576 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
| 5577 | if (cpu_has_vmx_ept_execute_only()) |
| 5578 | msrs->ept_caps |= |
| 5579 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 5580 | msrs->ept_caps &= ept_caps; |
| 5581 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 5582 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 5583 | VMX_EPT_1GB_PAGE_BIT; |
| 5584 | if (enable_ept_ad_bits) { |
| 5585 | msrs->secondary_ctls_high |= |
| 5586 | SECONDARY_EXEC_ENABLE_PML; |
| 5587 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 5588 | } |
| 5589 | } |
| 5590 | |
| 5591 | if (cpu_has_vmx_vmfunc()) { |
| 5592 | msrs->secondary_ctls_high |= |
| 5593 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 5594 | /* |
| 5595 | * Advertise EPTP switching unconditionally |
| 5596 | * since we emulate it |
| 5597 | */ |
| 5598 | if (enable_ept) |
| 5599 | msrs->vmfunc_controls = |
| 5600 | VMX_VMFUNC_EPTP_SWITCHING; |
| 5601 | } |
| 5602 | |
| 5603 | /* |
| 5604 | * Old versions of KVM use the single-context version without |
| 5605 | * checking for support, so declare that it is supported even |
| 5606 | * though it is treated as global context. The alternative is |
| 5607 | * not failing the single-context invvpid, and it is worse. |
| 5608 | */ |
| 5609 | if (enable_vpid) { |
| 5610 | msrs->secondary_ctls_high |= |
| 5611 | SECONDARY_EXEC_ENABLE_VPID; |
| 5612 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 5613 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 5614 | } |
| 5615 | |
| 5616 | if (enable_unrestricted_guest) |
| 5617 | msrs->secondary_ctls_high |= |
| 5618 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 5619 | |
| 5620 | if (flexpriority_enabled) |
| 5621 | msrs->secondary_ctls_high |= |
| 5622 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5623 | |
| 5624 | /* miscellaneous data */ |
| 5625 | rdmsr(MSR_IA32_VMX_MISC, |
| 5626 | msrs->misc_low, |
| 5627 | msrs->misc_high); |
| 5628 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 5629 | msrs->misc_low |= |
| 5630 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 5631 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
| 5632 | VMX_MISC_ACTIVITY_HLT; |
| 5633 | msrs->misc_high = 0; |
| 5634 | |
| 5635 | /* |
| 5636 | * This MSR reports some information about VMX support. We |
| 5637 | * should return information about the VMX we emulate for the |
| 5638 | * guest, and the VMCS structure we give it - not about the |
| 5639 | * VMX support of the underlying hardware. |
| 5640 | */ |
| 5641 | msrs->basic = |
| 5642 | VMCS12_REVISION | |
| 5643 | VMX_BASIC_TRUE_CTLS | |
| 5644 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 5645 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 5646 | |
| 5647 | if (cpu_has_vmx_basic_inout()) |
| 5648 | msrs->basic |= VMX_BASIC_INOUT; |
| 5649 | |
| 5650 | /* |
| 5651 | * These MSRs specify bits which the guest must keep fixed on |
| 5652 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 5653 | * We picked the standard core2 setting. |
| 5654 | */ |
| 5655 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 5656 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 5657 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 5658 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 5659 | |
| 5660 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 5661 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 5662 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 5663 | |
| 5664 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
| 5665 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
| 5666 | } |
| 5667 | |
| 5668 | void nested_vmx_hardware_unsetup(void) |
| 5669 | { |
| 5670 | int i; |
| 5671 | |
| 5672 | if (enable_shadow_vmcs) { |
| 5673 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 5674 | free_page((unsigned long)vmx_bitmap[i]); |
| 5675 | } |
| 5676 | } |
| 5677 | |
| 5678 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
| 5679 | { |
| 5680 | int i; |
| 5681 | |
| 5682 | if (!cpu_has_vmx_shadow_vmcs()) |
| 5683 | enable_shadow_vmcs = 0; |
| 5684 | if (enable_shadow_vmcs) { |
| 5685 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 5686 | vmx_bitmap[i] = (unsigned long *) |
| 5687 | __get_free_page(GFP_KERNEL); |
| 5688 | if (!vmx_bitmap[i]) { |
| 5689 | nested_vmx_hardware_unsetup(); |
| 5690 | return -ENOMEM; |
| 5691 | } |
| 5692 | } |
| 5693 | |
| 5694 | init_vmcs_shadow_fields(); |
| 5695 | } |
| 5696 | |
| 5697 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear, |
| 5698 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
| 5699 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld, |
| 5700 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst, |
| 5701 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread, |
| 5702 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume, |
| 5703 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite, |
| 5704 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff, |
| 5705 | exit_handlers[EXIT_REASON_VMON] = handle_vmon, |
| 5706 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept, |
| 5707 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid, |
| 5708 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc, |
| 5709 | |
| 5710 | kvm_x86_ops->check_nested_events = vmx_check_nested_events; |
| 5711 | kvm_x86_ops->get_nested_state = vmx_get_nested_state; |
| 5712 | kvm_x86_ops->set_nested_state = vmx_set_nested_state; |
| 5713 | kvm_x86_ops->get_vmcs12_pages = nested_get_vmcs12_pages, |
| 5714 | kvm_x86_ops->nested_enable_evmcs = nested_enable_evmcs; |
Vitaly Kuznetsov | e2e871a | 2018-12-10 18:21:55 +0100 | [diff] [blame] | 5715 | kvm_x86_ops->nested_get_evmcs_version = nested_get_evmcs_version; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5716 | |
| 5717 | return 0; |
| 5718 | } |