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