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