Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
Julien Thierry | 00089c0 | 2020-09-04 16:30:25 +0100 | [diff] [blame] | 3 | #include <linux/objtool.h> |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4 | #include <linux/percpu.h> |
| 5 | |
| 6 | #include <asm/debugreg.h> |
| 7 | #include <asm/mmu_context.h> |
| 8 | |
| 9 | #include "cpuid.h" |
| 10 | #include "hyperv.h" |
| 11 | #include "mmu.h" |
| 12 | #include "nested.h" |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 13 | #include "pmu.h" |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 14 | #include "sgx.h" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 15 | #include "trace.h" |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 16 | #include "vmx.h" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 17 | #include "x86.h" |
| 18 | |
| 19 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 20 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
| 21 | |
| 22 | static bool __read_mostly nested_early_check = 0; |
| 23 | module_param(nested_early_check, bool, S_IRUGO); |
| 24 | |
Sean Christopherson | 648fc8a | 2021-02-03 16:01:16 -0800 | [diff] [blame] | 25 | #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 26 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 27 | /* |
| 28 | * Hyper-V requires all of these, so mark them as supported even though |
| 29 | * they are just treated the same as all-context. |
| 30 | */ |
| 31 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 32 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 33 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 34 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 35 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 36 | |
| 37 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 38 | |
| 39 | enum { |
| 40 | VMX_VMREAD_BITMAP, |
| 41 | VMX_VMWRITE_BITMAP, |
| 42 | VMX_BITMAP_NR |
| 43 | }; |
| 44 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 45 | |
| 46 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 47 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
| 48 | |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 49 | struct shadow_vmcs_field { |
| 50 | u16 encoding; |
| 51 | u16 offset; |
| 52 | }; |
| 53 | static struct shadow_vmcs_field shadow_read_only_fields[] = { |
| 54 | #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) }, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 55 | #include "vmcs_shadow_fields.h" |
| 56 | }; |
| 57 | static int max_shadow_read_only_fields = |
| 58 | ARRAY_SIZE(shadow_read_only_fields); |
| 59 | |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 60 | static struct shadow_vmcs_field shadow_read_write_fields[] = { |
| 61 | #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) }, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 62 | #include "vmcs_shadow_fields.h" |
| 63 | }; |
| 64 | static int max_shadow_read_write_fields = |
| 65 | ARRAY_SIZE(shadow_read_write_fields); |
| 66 | |
Yi Wang | 8997f65 | 2019-01-21 15:27:05 +0800 | [diff] [blame] | 67 | static void init_vmcs_shadow_fields(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 68 | { |
| 69 | int i, j; |
| 70 | |
| 71 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 72 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 73 | |
| 74 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 75 | struct shadow_vmcs_field entry = shadow_read_only_fields[i]; |
| 76 | u16 field = entry.encoding; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 77 | |
| 78 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 79 | (i + 1 == max_shadow_read_only_fields || |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 80 | shadow_read_only_fields[i + 1].encoding != field + 1)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 81 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 82 | field + 1); |
| 83 | |
| 84 | clear_bit(field, vmx_vmread_bitmap); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 85 | if (field & 1) |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 87 | continue; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 88 | #else |
| 89 | entry.offset += sizeof(u32); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 90 | #endif |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 91 | shadow_read_only_fields[j++] = entry; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 92 | } |
| 93 | max_shadow_read_only_fields = j; |
| 94 | |
| 95 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 96 | struct shadow_vmcs_field entry = shadow_read_write_fields[i]; |
| 97 | u16 field = entry.encoding; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 98 | |
| 99 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 100 | (i + 1 == max_shadow_read_write_fields || |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 101 | shadow_read_write_fields[i + 1].encoding != field + 1)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 102 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 103 | field + 1); |
| 104 | |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 105 | WARN_ONCE(field >= GUEST_ES_AR_BYTES && |
| 106 | field <= GUEST_TR_AR_BYTES, |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 107 | "Update vmcs12_write_any() to drop reserved bits from AR_BYTES"); |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 108 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 109 | /* |
| 110 | * PML and the preemption timer can be emulated, but the |
| 111 | * processor cannot vmwrite to fields that don't exist |
| 112 | * on bare metal. |
| 113 | */ |
| 114 | switch (field) { |
| 115 | case GUEST_PML_INDEX: |
| 116 | if (!cpu_has_vmx_pml()) |
| 117 | continue; |
| 118 | break; |
| 119 | case VMX_PREEMPTION_TIMER_VALUE: |
| 120 | if (!cpu_has_vmx_preemption_timer()) |
| 121 | continue; |
| 122 | break; |
| 123 | case GUEST_INTR_STATUS: |
| 124 | if (!cpu_has_vmx_apicv()) |
| 125 | continue; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | clear_bit(field, vmx_vmwrite_bitmap); |
| 132 | clear_bit(field, vmx_vmread_bitmap); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 133 | if (field & 1) |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 134 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 135 | continue; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 136 | #else |
| 137 | entry.offset += sizeof(u32); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 138 | #endif |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 139 | shadow_read_write_fields[j++] = entry; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 140 | } |
| 141 | max_shadow_read_write_fields = j; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 146 | * set the success or error code of an emulated VMX instruction (as specified |
| 147 | * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated |
| 148 | * instruction. |
| 149 | */ |
| 150 | static int nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 151 | { |
| 152 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 153 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 154 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 155 | return kvm_skip_emulated_instruction(vcpu); |
| 156 | } |
| 157 | |
| 158 | static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 159 | { |
| 160 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 161 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 162 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 163 | | X86_EFLAGS_CF); |
| 164 | return kvm_skip_emulated_instruction(vcpu); |
| 165 | } |
| 166 | |
| 167 | static int nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 168 | u32 vm_instruction_error) |
| 169 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 170 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 171 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 172 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 173 | | X86_EFLAGS_ZF); |
| 174 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 175 | /* |
| 176 | * We don't need to force a shadow sync because |
| 177 | * VM_INSTRUCTION_ERROR is not shadowed |
| 178 | */ |
| 179 | return kvm_skip_emulated_instruction(vcpu); |
| 180 | } |
| 181 | |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 182 | static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error) |
| 183 | { |
| 184 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 185 | |
| 186 | /* |
| 187 | * failValid writes the error number to the current VMCS, which |
| 188 | * can't be done if there isn't a current VMCS. |
| 189 | */ |
| 190 | if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs) |
| 191 | return nested_vmx_failInvalid(vcpu); |
| 192 | |
| 193 | return nested_vmx_failValid(vcpu, vm_instruction_error); |
| 194 | } |
| 195 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 196 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 197 | { |
| 198 | /* TODO: not to reset guest simply here. */ |
| 199 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 200 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
| 201 | } |
| 202 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 203 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 204 | { |
| 205 | return fixed_bits_valid(control, low, high); |
| 206 | } |
| 207 | |
| 208 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 209 | { |
| 210 | return low | ((u64)high << 32); |
| 211 | } |
| 212 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 213 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 214 | { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 215 | secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 216 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 217 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) |
| 221 | { |
| 222 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 223 | |
| 224 | if (!vmx->nested.hv_evmcs) |
| 225 | return; |
| 226 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 227 | kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true); |
Vitaly Kuznetsov | 95fa101 | 2020-03-09 16:52:11 +0100 | [diff] [blame] | 228 | vmx->nested.hv_evmcs_vmptr = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 229 | vmx->nested.hv_evmcs = NULL; |
| 230 | } |
| 231 | |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 232 | static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx, |
| 233 | struct loaded_vmcs *prev) |
| 234 | { |
| 235 | struct vmcs_host_state *dest, *src; |
| 236 | |
| 237 | if (unlikely(!vmx->guest_state_loaded)) |
| 238 | return; |
| 239 | |
| 240 | src = &prev->host_state; |
| 241 | dest = &vmx->loaded_vmcs->host_state; |
| 242 | |
| 243 | vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base); |
| 244 | dest->ldt_sel = src->ldt_sel; |
| 245 | #ifdef CONFIG_X86_64 |
| 246 | dest->ds_sel = src->ds_sel; |
| 247 | dest->es_sel = src->es_sel; |
| 248 | #endif |
| 249 | } |
| 250 | |
| 251 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
| 252 | { |
| 253 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 254 | struct loaded_vmcs *prev; |
| 255 | int cpu; |
| 256 | |
Sean Christopherson | 138534a | 2020-09-23 11:44:52 -0700 | [diff] [blame] | 257 | if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs)) |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 258 | return; |
| 259 | |
| 260 | cpu = get_cpu(); |
| 261 | prev = vmx->loaded_vmcs; |
| 262 | vmx->loaded_vmcs = vmcs; |
| 263 | vmx_vcpu_load_vmcs(vcpu, cpu, prev); |
| 264 | vmx_sync_vmcs_host_state(vmx, prev); |
| 265 | put_cpu(); |
| 266 | |
| 267 | vmx_register_cache_reset(vcpu); |
| 268 | } |
| 269 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 270 | /* |
| 271 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 272 | * just stops using VMX. |
| 273 | */ |
| 274 | static void free_nested(struct kvm_vcpu *vcpu) |
| 275 | { |
| 276 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 277 | |
Sean Christopherson | df82a24 | 2020-09-23 11:44:50 -0700 | [diff] [blame] | 278 | if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01)) |
| 279 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 280 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 281 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
| 282 | return; |
| 283 | |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 284 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Jan Kiszka | cf64527 | 2019-07-21 13:52:18 +0200 | [diff] [blame] | 285 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 286 | vmx->nested.vmxon = false; |
| 287 | vmx->nested.smm.vmxon = false; |
| 288 | free_vpid(vmx->nested.vpid02); |
| 289 | vmx->nested.posted_intr_nv = -1; |
| 290 | vmx->nested.current_vmptr = -1ull; |
| 291 | if (enable_shadow_vmcs) { |
| 292 | vmx_disable_shadow_vmcs(vmx); |
| 293 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 294 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 295 | vmx->vmcs01.shadow_vmcs = NULL; |
| 296 | } |
| 297 | kfree(vmx->nested.cached_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 298 | vmx->nested.cached_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 299 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 300 | vmx->nested.cached_shadow_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 301 | /* Unpin physical memory we referred to in the vmcs02 */ |
| 302 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 303 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 304 | vmx->nested.apic_access_page = NULL; |
| 305 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 306 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 307 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 308 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 309 | |
| 310 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 311 | |
| 312 | nested_release_evmcs(vcpu); |
| 313 | |
| 314 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 315 | } |
| 316 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 317 | /* |
| 318 | * Ensure that the current vmcs of the logical processor is the |
| 319 | * vmcs01 of the vcpu before calling free_nested(). |
| 320 | */ |
| 321 | void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 322 | { |
| 323 | vcpu_load(vcpu); |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 324 | vmx_leave_nested(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 325 | vcpu_put(vcpu); |
| 326 | } |
| 327 | |
| 328 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 329 | struct x86_exception *fault) |
| 330 | { |
| 331 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 332 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 333 | u32 vm_exit_reason; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 334 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 335 | |
| 336 | if (vmx->nested.pml_full) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 337 | vm_exit_reason = EXIT_REASON_PML_FULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 338 | vmx->nested.pml_full = false; |
| 339 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 340 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 341 | vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 342 | else |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 343 | vm_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 344 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 345 | nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 346 | vmcs12->guest_physical_address = fault->address; |
| 347 | } |
| 348 | |
| 349 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 350 | { |
| 351 | WARN_ON(mmu_is_nested(vcpu)); |
| 352 | |
| 353 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
| 354 | kvm_init_shadow_ept_mmu(vcpu, |
| 355 | to_vmx(vcpu)->nested.msrs.ept_caps & |
| 356 | VMX_EPT_EXECUTE_ONLY_BIT, |
| 357 | nested_ept_ad_enabled(vcpu), |
Sean Christopherson | ac69dfa | 2020-03-02 18:02:37 -0800 | [diff] [blame] | 358 | nested_ept_get_eptp(vcpu)); |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 359 | vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 360 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 361 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 362 | |
| 363 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 364 | } |
| 365 | |
| 366 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 367 | { |
| 368 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 369 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 370 | } |
| 371 | |
| 372 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 373 | u16 error_code) |
| 374 | { |
| 375 | bool inequality, bit; |
| 376 | |
| 377 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 378 | inequality = |
| 379 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 380 | vmcs12->page_fault_error_code_match; |
| 381 | return inequality ^ bit; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /* |
| 386 | * KVM wants to inject page-faults which it got to the guest. This function |
| 387 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 388 | */ |
| 389 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 390 | { |
| 391 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 392 | unsigned int nr = vcpu->arch.exception.nr; |
| 393 | bool has_payload = vcpu->arch.exception.has_payload; |
| 394 | unsigned long payload = vcpu->arch.exception.payload; |
| 395 | |
| 396 | if (nr == PF_VECTOR) { |
| 397 | if (vcpu->arch.exception.nested_apf) { |
| 398 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 399 | return 1; |
| 400 | } |
| 401 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 402 | vcpu->arch.exception.error_code)) { |
| 403 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 404 | return 1; |
| 405 | } |
| 406 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 407 | if (nr == DB_VECTOR) { |
| 408 | if (!has_payload) { |
| 409 | payload = vcpu->arch.dr6; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 410 | payload &= ~DR6_BT; |
| 411 | payload ^= DR6_ACTIVE_LOW; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 412 | } |
| 413 | *exit_qual = payload; |
| 414 | } else |
| 415 | *exit_qual = 0; |
| 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 424 | struct x86_exception *fault) |
| 425 | { |
| 426 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 427 | |
| 428 | WARN_ON(!is_guest_mode(vcpu)); |
| 429 | |
| 430 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 431 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 432 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 433 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 434 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 435 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 436 | fault->address); |
| 437 | } else { |
| 438 | kvm_inject_page_fault(vcpu, fault); |
| 439 | } |
| 440 | } |
| 441 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 442 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 443 | struct vmcs12 *vmcs12) |
| 444 | { |
| 445 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 446 | return 0; |
| 447 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 448 | if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || |
| 449 | CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 450 | return -EINVAL; |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 456 | struct vmcs12 *vmcs12) |
| 457 | { |
| 458 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 459 | return 0; |
| 460 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 461 | if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 462 | return -EINVAL; |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 468 | struct vmcs12 *vmcs12) |
| 469 | { |
| 470 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 471 | return 0; |
| 472 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 473 | if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 474 | return -EINVAL; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 481 | */ |
| 482 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 483 | { |
| 484 | unsigned long *msr_bitmap; |
| 485 | int f = sizeof(unsigned long); |
| 486 | |
| 487 | if (!cpu_has_vmx_msr_bitmap()) |
| 488 | return true; |
| 489 | |
| 490 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 491 | |
| 492 | if (msr <= 0x1fff) { |
| 493 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 494 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 495 | msr &= 0x1fff; |
| 496 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 497 | } |
| 498 | |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 504 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 505 | */ |
| 506 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 507 | unsigned long *msr_bitmap_nested, |
| 508 | u32 msr, int type) |
| 509 | { |
| 510 | int f = sizeof(unsigned long); |
| 511 | |
| 512 | /* |
| 513 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 514 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 515 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 516 | */ |
| 517 | if (msr <= 0x1fff) { |
| 518 | if (type & MSR_TYPE_R && |
| 519 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 520 | /* read-low */ |
| 521 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 522 | |
| 523 | if (type & MSR_TYPE_W && |
| 524 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 525 | /* write-low */ |
| 526 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 527 | |
| 528 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 529 | msr &= 0x1fff; |
| 530 | if (type & MSR_TYPE_R && |
| 531 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 532 | /* read-high */ |
| 533 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 534 | |
| 535 | if (type & MSR_TYPE_W && |
| 536 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 537 | /* write-high */ |
| 538 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 539 | |
| 540 | } |
| 541 | } |
| 542 | |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 543 | static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) |
| 544 | { |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 545 | int msr; |
| 546 | |
| 547 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 548 | unsigned word = msr / BITS_PER_LONG; |
| 549 | |
| 550 | msr_bitmap[word] = ~0; |
| 551 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 552 | } |
| 553 | } |
| 554 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 555 | /* |
| 556 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 557 | * we do not use the hardware. |
| 558 | */ |
| 559 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 560 | struct vmcs12 *vmcs12) |
| 561 | { |
| 562 | int msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 563 | unsigned long *msr_bitmap_l1; |
| 564 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 565 | struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 566 | |
| 567 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 568 | if (!cpu_has_vmx_msr_bitmap() || |
| 569 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 570 | return false; |
| 571 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 572 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 573 | return false; |
| 574 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 575 | msr_bitmap_l1 = (unsigned long *)map->hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 576 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 577 | /* |
| 578 | * To keep the control flow simple, pay eight 8-byte writes (sixteen |
| 579 | * 4-byte writes on 32-bit systems) up front to enable intercepts for |
| 580 | * the x2APIC MSR range and selectively disable them below. |
| 581 | */ |
| 582 | enable_x2apic_msr_intercepts(msr_bitmap_l0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 583 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 584 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
| 585 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 586 | /* |
| 587 | * L0 need not intercept reads for MSRs between 0x800 |
| 588 | * and 0x8ff, it just lets the processor take the value |
| 589 | * from the virtual-APIC page; take those 256 bits |
| 590 | * directly from the L1 bitmap. |
| 591 | */ |
| 592 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 593 | unsigned word = msr / BITS_PER_LONG; |
| 594 | |
| 595 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 596 | } |
| 597 | } |
| 598 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 599 | nested_vmx_disable_intercept_for_msr( |
| 600 | msr_bitmap_l1, msr_bitmap_l0, |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 601 | X2APIC_MSR(APIC_TASKPRI), |
Marc Orr | c73f4c9 | 2019-04-01 23:56:00 -0700 | [diff] [blame] | 602 | MSR_TYPE_R | MSR_TYPE_W); |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 603 | |
| 604 | if (nested_cpu_has_vid(vmcs12)) { |
| 605 | nested_vmx_disable_intercept_for_msr( |
| 606 | msr_bitmap_l1, msr_bitmap_l0, |
| 607 | X2APIC_MSR(APIC_EOI), |
| 608 | MSR_TYPE_W); |
| 609 | nested_vmx_disable_intercept_for_msr( |
| 610 | msr_bitmap_l1, msr_bitmap_l0, |
| 611 | X2APIC_MSR(APIC_SELF_IPI), |
| 612 | MSR_TYPE_W); |
| 613 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 616 | /* KVM unconditionally exposes the FS/GS base MSRs to L1. */ |
Sean Christopherson | dbdd096 | 2021-04-21 19:38:31 -0700 | [diff] [blame] | 617 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 618 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 619 | MSR_FS_BASE, MSR_TYPE_RW); |
| 620 | |
| 621 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 622 | MSR_GS_BASE, MSR_TYPE_RW); |
| 623 | |
| 624 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 625 | MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
Sean Christopherson | dbdd096 | 2021-04-21 19:38:31 -0700 | [diff] [blame] | 626 | #endif |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * Checking the L0->L1 bitmap is trying to verify two things: |
| 630 | * |
| 631 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 632 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 633 | * from the L12 MSR bitmap that is too permissive. |
| 634 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 635 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 636 | * works properly because we only update the L01 MSR bitmap lazily. |
| 637 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 638 | * updated to reflect this when L1 (or its L2s) actually write to |
| 639 | * the MSR. |
| 640 | */ |
| 641 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 642 | nested_vmx_disable_intercept_for_msr( |
| 643 | msr_bitmap_l1, msr_bitmap_l0, |
| 644 | MSR_IA32_SPEC_CTRL, |
| 645 | MSR_TYPE_R | MSR_TYPE_W); |
| 646 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 647 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 648 | nested_vmx_disable_intercept_for_msr( |
| 649 | msr_bitmap_l1, msr_bitmap_l0, |
| 650 | MSR_IA32_PRED_CMD, |
| 651 | MSR_TYPE_W); |
| 652 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 653 | kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 654 | |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 659 | struct vmcs12 *vmcs12) |
| 660 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 661 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 662 | struct vmcs12 *shadow; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 663 | |
| 664 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 665 | vmcs12->vmcs_link_pointer == -1ull) |
| 666 | return; |
| 667 | |
| 668 | shadow = get_shadow_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 669 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 670 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)) |
| 671 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 672 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 673 | memcpy(shadow, map.hva, VMCS12_SIZE); |
| 674 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 678 | struct vmcs12 *vmcs12) |
| 679 | { |
| 680 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 681 | |
| 682 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 683 | vmcs12->vmcs_link_pointer == -1ull) |
| 684 | return; |
| 685 | |
| 686 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 687 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * In nested virtualization, check if L1 has set |
| 692 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 693 | */ |
| 694 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 695 | { |
| 696 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 697 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 698 | } |
| 699 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 700 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 701 | struct vmcs12 *vmcs12) |
| 702 | { |
| 703 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 704 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 705 | return -EINVAL; |
| 706 | else |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 711 | struct vmcs12 *vmcs12) |
| 712 | { |
| 713 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 714 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 715 | !nested_cpu_has_vid(vmcs12) && |
| 716 | !nested_cpu_has_posted_intr(vmcs12)) |
| 717 | return 0; |
| 718 | |
| 719 | /* |
| 720 | * If virtualize x2apic mode is enabled, |
| 721 | * virtualize apic access must be disabled. |
| 722 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 723 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 724 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 725 | return -EINVAL; |
| 726 | |
| 727 | /* |
| 728 | * If virtual interrupt delivery is enabled, |
| 729 | * we must exit on external interrupts. |
| 730 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 731 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 732 | return -EINVAL; |
| 733 | |
| 734 | /* |
| 735 | * bits 15:8 should be zero in posted_intr_nv, |
| 736 | * the descriptor address has been already checked |
| 737 | * in nested_get_vmcs12_pages. |
| 738 | * |
| 739 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 740 | */ |
| 741 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 742 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 743 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 744 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 745 | CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64)))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 746 | return -EINVAL; |
| 747 | |
| 748 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 749 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 750 | return -EINVAL; |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 756 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 757 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 758 | if (count == 0) |
| 759 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 760 | |
| 761 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 762 | !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 763 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 764 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 765 | return 0; |
| 766 | } |
| 767 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 768 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 769 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 770 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 771 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 772 | vmcs12->vm_exit_msr_load_count, |
| 773 | vmcs12->vm_exit_msr_load_addr)) || |
| 774 | CC(nested_vmx_check_msr_switch(vcpu, |
| 775 | vmcs12->vm_exit_msr_store_count, |
| 776 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 777 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 778 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 779 | return 0; |
| 780 | } |
| 781 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 782 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 783 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 784 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 785 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 786 | vmcs12->vm_entry_msr_load_count, |
| 787 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 788 | return -EINVAL; |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 793 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 794 | struct vmcs12 *vmcs12) |
| 795 | { |
| 796 | if (!nested_cpu_has_pml(vmcs12)) |
| 797 | return 0; |
| 798 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 799 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 800 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 801 | return -EINVAL; |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 807 | struct vmcs12 *vmcs12) |
| 808 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 809 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 810 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 811 | return -EINVAL; |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 816 | struct vmcs12 *vmcs12) |
| 817 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 818 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 819 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 820 | return -EINVAL; |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 825 | struct vmcs12 *vmcs12) |
| 826 | { |
| 827 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 828 | return 0; |
| 829 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 830 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 831 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 832 | return -EINVAL; |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 838 | struct vmx_msr_entry *e) |
| 839 | { |
| 840 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 841 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 842 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 843 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 844 | CC(e->index == MSR_IA32_UCODE_REV)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 845 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 846 | if (CC(e->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 847 | return -EINVAL; |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 852 | struct vmx_msr_entry *e) |
| 853 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 854 | if (CC(e->index == MSR_FS_BASE) || |
| 855 | CC(e->index == MSR_GS_BASE) || |
| 856 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 857 | nested_vmx_msr_check_common(vcpu, e)) |
| 858 | return -EINVAL; |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 863 | struct vmx_msr_entry *e) |
| 864 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 865 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 866 | nested_vmx_msr_check_common(vcpu, e)) |
| 867 | return -EINVAL; |
| 868 | return 0; |
| 869 | } |
| 870 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 871 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 872 | { |
| 873 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 874 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 875 | vmx->nested.msrs.misc_high); |
| 876 | |
| 877 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 878 | } |
| 879 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 880 | /* |
| 881 | * Load guest's/host's msr at nested entry/exit. |
| 882 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 883 | * |
| 884 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 885 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 886 | * as possible, process all valid entries before failing rather than precheck |
| 887 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 888 | */ |
| 889 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 890 | { |
| 891 | u32 i; |
| 892 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 893 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 894 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 895 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 896 | if (unlikely(i >= max_msr_list_size)) |
| 897 | goto fail; |
| 898 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 899 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 900 | &e, sizeof(e))) { |
| 901 | pr_debug_ratelimited( |
| 902 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 903 | __func__, i, gpa + i * sizeof(e)); |
| 904 | goto fail; |
| 905 | } |
| 906 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 907 | pr_debug_ratelimited( |
| 908 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 909 | __func__, i, e.index, e.reserved); |
| 910 | goto fail; |
| 911 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 912 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 913 | pr_debug_ratelimited( |
| 914 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 915 | __func__, i, e.index, e.value); |
| 916 | goto fail; |
| 917 | } |
| 918 | } |
| 919 | return 0; |
| 920 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 921 | /* 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] | 922 | return i + 1; |
| 923 | } |
| 924 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 925 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 926 | u32 msr_index, |
| 927 | u64 *data) |
| 928 | { |
| 929 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 930 | |
| 931 | /* |
| 932 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 933 | * does not include the time taken for emulation of the L2->L1 |
| 934 | * VM-exit in L0, use the more accurate value. |
| 935 | */ |
| 936 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 937 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 938 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 939 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 940 | if (i >= 0) { |
| 941 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 942 | |
| 943 | *data = kvm_read_l1_tsc(vcpu, val); |
| 944 | return true; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 949 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 950 | msr_index); |
| 951 | return false; |
| 952 | } |
| 953 | return true; |
| 954 | } |
| 955 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 956 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 957 | struct vmx_msr_entry *e) |
| 958 | { |
| 959 | if (kvm_vcpu_read_guest(vcpu, |
| 960 | gpa + i * sizeof(*e), |
| 961 | e, 2 * sizeof(u32))) { |
| 962 | pr_debug_ratelimited( |
| 963 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 964 | __func__, i, gpa + i * sizeof(*e)); |
| 965 | return false; |
| 966 | } |
| 967 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 968 | pr_debug_ratelimited( |
| 969 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 970 | __func__, i, e->index, e->reserved); |
| 971 | return false; |
| 972 | } |
| 973 | return true; |
| 974 | } |
| 975 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 976 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 977 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 978 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 979 | u32 i; |
| 980 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 981 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 982 | |
| 983 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 984 | if (unlikely(i >= max_msr_list_size)) |
| 985 | return -EINVAL; |
| 986 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 987 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 988 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 989 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 990 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 991 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 992 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 993 | if (kvm_vcpu_write_guest(vcpu, |
| 994 | gpa + i * sizeof(e) + |
| 995 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 996 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 997 | pr_debug_ratelimited( |
| 998 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 999 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1000 | return -EINVAL; |
| 1001 | } |
| 1002 | } |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1006 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1007 | { |
| 1008 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1009 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1010 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1011 | struct vmx_msr_entry e; |
| 1012 | u32 i; |
| 1013 | |
| 1014 | for (i = 0; i < count; i++) { |
| 1015 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1016 | return false; |
| 1017 | |
| 1018 | if (e.index == msr_index) |
| 1019 | return true; |
| 1020 | } |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
| 1024 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1025 | u32 msr_index) |
| 1026 | { |
| 1027 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1028 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1029 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1030 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1031 | bool in_autostore_list; |
| 1032 | int last; |
| 1033 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1034 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1035 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1036 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1037 | |
| 1038 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1039 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1040 | /* |
| 1041 | * Emulated VMEntry does not fail here. Instead a less |
| 1042 | * accurate value will be returned by |
| 1043 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1044 | * instead of reading the value from the vmcs02 VMExit |
| 1045 | * MSR-store area. |
| 1046 | */ |
| 1047 | pr_warn_ratelimited( |
| 1048 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1049 | msr_index); |
| 1050 | return; |
| 1051 | } |
| 1052 | last = autostore->nr++; |
| 1053 | autostore->val[last].index = msr_index; |
| 1054 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1055 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1056 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1060 | /* |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1061 | * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit. |
| 1062 | * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't |
| 1063 | * enable VPID for L2 (implying it expects a TLB flush on VMX transitions). |
| 1064 | * Here's why. |
| 1065 | * |
| 1066 | * If EPT is enabled by L0 a sync is never needed: |
| 1067 | * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there |
| 1068 | * cannot be unsync'd SPTEs for either L1 or L2. |
| 1069 | * |
| 1070 | * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter |
| 1071 | * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings |
| 1072 | * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush |
| 1073 | * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't |
| 1074 | * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit. |
| 1075 | * |
| 1076 | * If EPT is disabled by L0: |
| 1077 | * - if VPID is enabled by L1 (for L2), the situation is similar to when L1 |
| 1078 | * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't |
| 1079 | * required to invalidate linear mappings (EPT is disabled so there are |
| 1080 | * no combined or guest-physical mappings), i.e. L1 can't rely on the |
| 1081 | * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1). |
| 1082 | * |
| 1083 | * - however if VPID is disabled by L1, then a sync is needed as L1 expects all |
| 1084 | * linear mappings (EPT is disabled so there are no combined or guest-physical |
| 1085 | * mappings) to be invalidated on both VM-Enter and VM-Exit. |
| 1086 | * |
| 1087 | * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which |
| 1088 | * additionally checks that L2 has been assigned a VPID (when EPT is disabled). |
| 1089 | * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect |
| 1090 | * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2 |
| 1091 | * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has |
| 1092 | * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1 |
| 1093 | * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't |
| 1094 | * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush |
| 1095 | * stale TLB entries, at which point L0 will sync L2's MMU. |
| 1096 | */ |
| 1097 | static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu) |
| 1098 | { |
| 1099 | return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu)); |
| 1100 | } |
| 1101 | |
| 1102 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1103 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1104 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1105 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1106 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1107 | */ |
| 1108 | 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] | 1109 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1110 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1111 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1112 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1113 | return -EINVAL; |
| 1114 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1115 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1116 | /* |
| 1117 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1118 | * must not be dereferenced. |
| 1119 | */ |
| 1120 | if (!nested_ept && is_pae_paging(vcpu) && |
| 1121 | (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) { |
| 1122 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) { |
| 1123 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1124 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1128 | /* |
Sean Christopherson | 9805c5f | 2020-03-20 14:28:30 -0700 | [diff] [blame] | 1129 | * Unconditionally skip the TLB flush on fast CR3 switch, all TLB |
| 1130 | * flushes are handled by nested_vmx_transition_tlb_flush(). See |
| 1131 | * nested_vmx_transition_mmu_sync for details on skipping the MMU sync. |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1132 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1133 | if (!nested_ept) |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 1134 | kvm_mmu_new_pgd(vcpu, cr3, true, |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1135 | !nested_vmx_transition_mmu_sync(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1136 | |
| 1137 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1138 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1139 | |
| 1140 | kvm_init_mmu(vcpu, false); |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1147 | * populated by L2 differently than TLB entries populated |
| 1148 | * by L1. |
| 1149 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1150 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1151 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1152 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1153 | * |
| 1154 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1155 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1156 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1157 | */ |
| 1158 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1159 | { |
| 1160 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1161 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1162 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1163 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1164 | } |
| 1165 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1166 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1167 | struct vmcs12 *vmcs12, |
| 1168 | bool is_vmenter) |
| 1169 | { |
| 1170 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1171 | |
| 1172 | /* |
| 1173 | * If VPID is disabled, linear and combined mappings are flushed on |
| 1174 | * VM-Enter/VM-Exit, and guest-physical mappings are valid only for |
| 1175 | * their associated EPTP. |
| 1176 | */ |
| 1177 | if (!enable_vpid) |
| 1178 | return; |
| 1179 | |
| 1180 | /* |
| 1181 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1182 | * for *all* contexts to be flushed on VM-Enter/VM-Exit. |
| 1183 | * |
| 1184 | * If VPID is enabled and used by vmc12, but L2 does not have a unique |
| 1185 | * 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] | 1186 | * a VPID for L2, flush the current context as the effective ASID is |
| 1187 | * common to both L1 and L2. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1188 | * |
| 1189 | * Defer the flush so that it runs after vmcs02.EPTP has been set by |
| 1190 | * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid |
| 1191 | * redundant flushes further down the nested pipeline. |
| 1192 | * |
| 1193 | * If a TLB flush isn't required due to any of the above, and vpid12 is |
| 1194 | * changing then the new "virtual" VPID (vpid12) will reuse the same |
| 1195 | * "real" VPID (vpid02), and so needs to be sync'd. There is no direct |
| 1196 | * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for |
| 1197 | * all nested vCPUs. |
| 1198 | */ |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1199 | if (!nested_cpu_has_vpid(vmcs12)) { |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1200 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1201 | } else if (!nested_has_guest_tlb_tag(vcpu)) { |
| 1202 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1203 | } else if (is_vmenter && |
| 1204 | vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 1205 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 1206 | vpid_sync_context(nested_get_vpid02(vcpu)); |
| 1207 | } |
| 1208 | } |
| 1209 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1210 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1211 | { |
| 1212 | superset &= mask; |
| 1213 | subset &= mask; |
| 1214 | |
| 1215 | return (superset | subset) == superset; |
| 1216 | } |
| 1217 | |
| 1218 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1219 | { |
| 1220 | const u64 feature_and_reserved = |
| 1221 | /* feature (except bit 48; see below) */ |
| 1222 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1223 | /* reserved */ |
| 1224 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1225 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1226 | |
| 1227 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1228 | return -EINVAL; |
| 1229 | |
| 1230 | /* |
| 1231 | * KVM does not emulate a version of VMX that constrains physical |
| 1232 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1233 | */ |
| 1234 | if (data & BIT_ULL(48)) |
| 1235 | return -EINVAL; |
| 1236 | |
| 1237 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1238 | vmx_basic_vmcs_revision_id(data)) |
| 1239 | return -EINVAL; |
| 1240 | |
| 1241 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1242 | return -EINVAL; |
| 1243 | |
| 1244 | vmx->nested.msrs.basic = data; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | static int |
| 1249 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1250 | { |
| 1251 | u64 supported; |
| 1252 | u32 *lowp, *highp; |
| 1253 | |
| 1254 | switch (msr_index) { |
| 1255 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1256 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1257 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1258 | break; |
| 1259 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1260 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1261 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1262 | break; |
| 1263 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1264 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1265 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1266 | break; |
| 1267 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1268 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1269 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1270 | break; |
| 1271 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1272 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1273 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1274 | break; |
| 1275 | default: |
| 1276 | BUG(); |
| 1277 | } |
| 1278 | |
| 1279 | supported = vmx_control_msr(*lowp, *highp); |
| 1280 | |
| 1281 | /* Check must-be-1 bits are still 1. */ |
| 1282 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1283 | return -EINVAL; |
| 1284 | |
| 1285 | /* Check must-be-0 bits are still 0. */ |
| 1286 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1287 | return -EINVAL; |
| 1288 | |
| 1289 | *lowp = data; |
| 1290 | *highp = data >> 32; |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1295 | { |
| 1296 | const u64 feature_and_reserved_bits = |
| 1297 | /* feature */ |
| 1298 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1299 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1300 | /* reserved */ |
| 1301 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1302 | u64 vmx_misc; |
| 1303 | |
| 1304 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1305 | vmx->nested.msrs.misc_high); |
| 1306 | |
| 1307 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1308 | return -EINVAL; |
| 1309 | |
| 1310 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1311 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1312 | vmx_misc_preemption_timer_rate(data) != |
| 1313 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1320 | return -EINVAL; |
| 1321 | |
| 1322 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1323 | return -EINVAL; |
| 1324 | |
| 1325 | vmx->nested.msrs.misc_low = data; |
| 1326 | vmx->nested.msrs.misc_high = data >> 32; |
| 1327 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1332 | { |
| 1333 | u64 vmx_ept_vpid_cap; |
| 1334 | |
| 1335 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1336 | vmx->nested.msrs.vpid_caps); |
| 1337 | |
| 1338 | /* Every bit is either reserved or a feature bit. */ |
| 1339 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1340 | return -EINVAL; |
| 1341 | |
| 1342 | vmx->nested.msrs.ept_caps = data; |
| 1343 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1348 | { |
| 1349 | u64 *msr; |
| 1350 | |
| 1351 | switch (msr_index) { |
| 1352 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1353 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1354 | break; |
| 1355 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1356 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1357 | break; |
| 1358 | default: |
| 1359 | BUG(); |
| 1360 | } |
| 1361 | |
| 1362 | /* |
| 1363 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1364 | * must be 1 in the restored value. |
| 1365 | */ |
| 1366 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1367 | return -EINVAL; |
| 1368 | |
| 1369 | *msr = data; |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * Called when userspace is restoring VMX MSRs. |
| 1375 | * |
| 1376 | * Returns 0 on success, non-0 otherwise. |
| 1377 | */ |
| 1378 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1379 | { |
| 1380 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1381 | |
| 1382 | /* |
| 1383 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1384 | * is in VMX operation. |
| 1385 | */ |
| 1386 | if (vmx->nested.vmxon) |
| 1387 | return -EBUSY; |
| 1388 | |
| 1389 | switch (msr_index) { |
| 1390 | case MSR_IA32_VMX_BASIC: |
| 1391 | return vmx_restore_vmx_basic(vmx, data); |
| 1392 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1393 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1394 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1395 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1396 | /* |
| 1397 | * The "non-true" VMX capability MSRs are generated from the |
| 1398 | * "true" MSRs, so we do not support restoring them directly. |
| 1399 | * |
| 1400 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1401 | * should restore the "true" MSRs with the must-be-1 bits |
| 1402 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1403 | * DEFAULT SETTINGS". |
| 1404 | */ |
| 1405 | return -EINVAL; |
| 1406 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1407 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1408 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1409 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1410 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1411 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1412 | case MSR_IA32_VMX_MISC: |
| 1413 | return vmx_restore_vmx_misc(vmx, data); |
| 1414 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1415 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1416 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1417 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1418 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1419 | /* |
| 1420 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1421 | * do not support restoring them directly. |
| 1422 | */ |
| 1423 | return -EINVAL; |
| 1424 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1425 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1426 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1427 | vmx->nested.msrs.vmcs_enum = data; |
| 1428 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1429 | case MSR_IA32_VMX_VMFUNC: |
| 1430 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1431 | return -EINVAL; |
| 1432 | vmx->nested.msrs.vmfunc_controls = data; |
| 1433 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1434 | default: |
| 1435 | /* |
| 1436 | * The rest of the VMX capability MSRs do not support restore. |
| 1437 | */ |
| 1438 | return -EINVAL; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /* Returns 0 on success, non-0 otherwise. */ |
| 1443 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1444 | { |
| 1445 | switch (msr_index) { |
| 1446 | case MSR_IA32_VMX_BASIC: |
| 1447 | *pdata = msrs->basic; |
| 1448 | break; |
| 1449 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1450 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1451 | *pdata = vmx_control_msr( |
| 1452 | msrs->pinbased_ctls_low, |
| 1453 | msrs->pinbased_ctls_high); |
| 1454 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1455 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1456 | break; |
| 1457 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1458 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1459 | *pdata = vmx_control_msr( |
| 1460 | msrs->procbased_ctls_low, |
| 1461 | msrs->procbased_ctls_high); |
| 1462 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1463 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1464 | break; |
| 1465 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1466 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1467 | *pdata = vmx_control_msr( |
| 1468 | msrs->exit_ctls_low, |
| 1469 | msrs->exit_ctls_high); |
| 1470 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1471 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1472 | break; |
| 1473 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1474 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1475 | *pdata = vmx_control_msr( |
| 1476 | msrs->entry_ctls_low, |
| 1477 | msrs->entry_ctls_high); |
| 1478 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1479 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1480 | break; |
| 1481 | case MSR_IA32_VMX_MISC: |
| 1482 | *pdata = vmx_control_msr( |
| 1483 | msrs->misc_low, |
| 1484 | msrs->misc_high); |
| 1485 | break; |
| 1486 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1487 | *pdata = msrs->cr0_fixed0; |
| 1488 | break; |
| 1489 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1490 | *pdata = msrs->cr0_fixed1; |
| 1491 | break; |
| 1492 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1493 | *pdata = msrs->cr4_fixed0; |
| 1494 | break; |
| 1495 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1496 | *pdata = msrs->cr4_fixed1; |
| 1497 | break; |
| 1498 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1499 | *pdata = msrs->vmcs_enum; |
| 1500 | break; |
| 1501 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1502 | *pdata = vmx_control_msr( |
| 1503 | msrs->secondary_ctls_low, |
| 1504 | msrs->secondary_ctls_high); |
| 1505 | break; |
| 1506 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1507 | *pdata = msrs->ept_caps | |
| 1508 | ((u64)msrs->vpid_caps << 32); |
| 1509 | break; |
| 1510 | case MSR_IA32_VMX_VMFUNC: |
| 1511 | *pdata = msrs->vmfunc_controls; |
| 1512 | break; |
| 1513 | default: |
| 1514 | return 1; |
| 1515 | } |
| 1516 | |
| 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1521 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1522 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1523 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1524 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1525 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1526 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1527 | */ |
| 1528 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1529 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1530 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1531 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1532 | struct shadow_vmcs_field field; |
| 1533 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1534 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1535 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1536 | if (WARN_ON(!shadow_vmcs)) |
| 1537 | return; |
| 1538 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1539 | preempt_disable(); |
| 1540 | |
| 1541 | vmcs_load(shadow_vmcs); |
| 1542 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1543 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1544 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1545 | val = __vmcs_readl(field.encoding); |
| 1546 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | vmcs_clear(shadow_vmcs); |
| 1550 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1551 | |
| 1552 | preempt_enable(); |
| 1553 | } |
| 1554 | |
| 1555 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1556 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1557 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1558 | shadow_read_write_fields, |
| 1559 | shadow_read_only_fields |
| 1560 | }; |
| 1561 | const int max_fields[] = { |
| 1562 | max_shadow_read_write_fields, |
| 1563 | max_shadow_read_only_fields |
| 1564 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1565 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1566 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1567 | struct shadow_vmcs_field field; |
| 1568 | unsigned long val; |
| 1569 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1570 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1571 | if (WARN_ON(!shadow_vmcs)) |
| 1572 | return; |
| 1573 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1574 | vmcs_load(shadow_vmcs); |
| 1575 | |
| 1576 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1577 | for (i = 0; i < max_fields[q]; i++) { |
| 1578 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1579 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1580 | field.offset); |
| 1581 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | vmcs_clear(shadow_vmcs); |
| 1586 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1587 | } |
| 1588 | |
| 1589 | static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx) |
| 1590 | { |
| 1591 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1592 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1593 | |
| 1594 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1595 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1596 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1597 | |
| 1598 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1599 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1600 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1601 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1602 | vmcs12->guest_interruptibility_info = |
| 1603 | evmcs->guest_interruptibility_info; |
| 1604 | } |
| 1605 | |
| 1606 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1607 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1608 | vmcs12->cpu_based_vm_exec_control = |
| 1609 | evmcs->cpu_based_vm_exec_control; |
| 1610 | } |
| 1611 | |
| 1612 | if (unlikely(!(evmcs->hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1613 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1614 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1615 | } |
| 1616 | |
| 1617 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1618 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1619 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1620 | } |
| 1621 | |
| 1622 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1623 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1624 | vmcs12->vm_entry_intr_info_field = |
| 1625 | evmcs->vm_entry_intr_info_field; |
| 1626 | vmcs12->vm_entry_exception_error_code = |
| 1627 | evmcs->vm_entry_exception_error_code; |
| 1628 | vmcs12->vm_entry_instruction_len = |
| 1629 | evmcs->vm_entry_instruction_len; |
| 1630 | } |
| 1631 | |
| 1632 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1633 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1634 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1635 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1636 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1637 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1638 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1639 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1640 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1641 | vmcs12->host_rip = evmcs->host_rip; |
| 1642 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1643 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1644 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1645 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1646 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1647 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1648 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1649 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1650 | } |
| 1651 | |
| 1652 | if (unlikely(!(evmcs->hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1653 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1654 | vmcs12->pin_based_vm_exec_control = |
| 1655 | evmcs->pin_based_vm_exec_control; |
| 1656 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1657 | vmcs12->secondary_vm_exec_control = |
| 1658 | evmcs->secondary_vm_exec_control; |
| 1659 | } |
| 1660 | |
| 1661 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1662 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1663 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1664 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1665 | } |
| 1666 | |
| 1667 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1668 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1669 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1670 | } |
| 1671 | |
| 1672 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1673 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1674 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1675 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1676 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1677 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1678 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1679 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1680 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1681 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1682 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1683 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1684 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1685 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1686 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1687 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1688 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1689 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1690 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1691 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1692 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1693 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1694 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1695 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1696 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1697 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1698 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1699 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1700 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1701 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1702 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1703 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1704 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1705 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1706 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1707 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1708 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1709 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1710 | } |
| 1711 | |
| 1712 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1713 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1714 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1715 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1716 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1717 | } |
| 1718 | |
| 1719 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1720 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1721 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1722 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1723 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1724 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1725 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1726 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1727 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1728 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1729 | } |
| 1730 | |
| 1731 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1732 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1733 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1734 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1735 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1736 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1737 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1738 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1739 | } |
| 1740 | |
| 1741 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1742 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1743 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1744 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1745 | } |
| 1746 | |
| 1747 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1748 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1749 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1750 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1751 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1752 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1753 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1754 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1755 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1756 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1757 | vmcs12->guest_pending_dbg_exceptions = |
| 1758 | evmcs->guest_pending_dbg_exceptions; |
| 1759 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1760 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1761 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1762 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1763 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * Not used? |
| 1768 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1769 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1770 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1771 | * vmcs12->page_fault_error_code_mask = |
| 1772 | * evmcs->page_fault_error_code_mask; |
| 1773 | * vmcs12->page_fault_error_code_match = |
| 1774 | * evmcs->page_fault_error_code_match; |
| 1775 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1776 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1777 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1778 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1779 | */ |
| 1780 | |
| 1781 | /* |
| 1782 | * Read only fields: |
| 1783 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1784 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1785 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1786 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1787 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1788 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1789 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1790 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1791 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1792 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1793 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1794 | * |
| 1795 | * Not present in struct vmcs12: |
| 1796 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1797 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1798 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1799 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1800 | */ |
| 1801 | |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
| 1806 | { |
| 1807 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1808 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1809 | |
| 1810 | /* |
| 1811 | * Should not be changed by KVM: |
| 1812 | * |
| 1813 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1814 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1815 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1816 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1817 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1818 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1819 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1820 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1821 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1822 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1823 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1824 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1825 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1826 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1827 | * evmcs->host_rip = vmcs12->host_rip; |
| 1828 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1829 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1830 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1831 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1832 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1833 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1834 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1835 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1836 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1837 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1838 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1839 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1840 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1841 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1842 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1843 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1844 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1845 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1846 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1847 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1848 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1849 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1850 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1851 | * evmcs->page_fault_error_code_mask = |
| 1852 | * vmcs12->page_fault_error_code_mask; |
| 1853 | * evmcs->page_fault_error_code_match = |
| 1854 | * vmcs12->page_fault_error_code_match; |
| 1855 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1856 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1857 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1858 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1859 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1860 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1861 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1862 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1863 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1864 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1865 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1866 | * |
| 1867 | * Not present in struct vmcs12: |
| 1868 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1869 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1870 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1871 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1872 | */ |
| 1873 | |
| 1874 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1875 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1876 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1877 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1878 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1879 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1880 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1881 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1882 | |
| 1883 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1884 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1885 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1886 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1887 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1888 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1889 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1890 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1891 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1892 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1893 | |
| 1894 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1895 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1896 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1897 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1898 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1899 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1900 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1901 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1902 | |
| 1903 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1904 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1905 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1906 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1907 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1908 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1909 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1910 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1911 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1912 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1913 | |
| 1914 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1915 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1916 | |
| 1917 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1918 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1919 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1920 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1921 | |
| 1922 | evmcs->guest_pending_dbg_exceptions = |
| 1923 | vmcs12->guest_pending_dbg_exceptions; |
| 1924 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1925 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1926 | |
| 1927 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1928 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1929 | |
| 1930 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1931 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1932 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1933 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1934 | |
| 1935 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1936 | |
| 1937 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1938 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1939 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1940 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1941 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1942 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1943 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1944 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1945 | |
| 1946 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1947 | |
| 1948 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1949 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1950 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1951 | |
| 1952 | evmcs->guest_interruptibility_info = |
| 1953 | vmcs12->guest_interruptibility_info; |
| 1954 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1955 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1956 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1957 | evmcs->vm_entry_exception_error_code = |
| 1958 | vmcs12->vm_entry_exception_error_code; |
| 1959 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1960 | |
| 1961 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1962 | |
| 1963 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1964 | |
| 1965 | return 0; |
| 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1970 | * instruction. |
| 1971 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1972 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1973 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1974 | { |
| 1975 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1976 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1977 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1978 | |
| 1979 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1980 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1981 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1982 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1983 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1984 | |
Vitaly Kuznetsov | 95fa101 | 2020-03-09 16:52:11 +0100 | [diff] [blame] | 1985 | if (unlikely(!vmx->nested.hv_evmcs || |
| 1986 | evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1987 | if (!vmx->nested.hv_evmcs) |
| 1988 | vmx->nested.current_vmptr = -1ull; |
| 1989 | |
| 1990 | nested_release_evmcs(vcpu); |
| 1991 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1992 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1993 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1994 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1995 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1996 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1997 | |
| 1998 | /* |
| 1999 | * Currently, KVM only supports eVMCS version 1 |
| 2000 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 2001 | * value to first u32 field of eVMCS which should specify eVMCS |
| 2002 | * VersionNumber. |
| 2003 | * |
| 2004 | * Guest should be aware of supported eVMCS versions by host by |
| 2005 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 2006 | * expected to set this CPUID leaf according to the value |
| 2007 | * returned in vmcs_version from nested_enable_evmcs(). |
| 2008 | * |
| 2009 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 2010 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 2011 | * just sets first u32 field of eVMCS to revision_id specified |
| 2012 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 2013 | * which is one of the supported versions specified in |
| 2014 | * CPUID.0x4000000A.EAX[0:15]. |
| 2015 | * |
| 2016 | * To overcome Hyper-V bug, we accept here either a supported |
| 2017 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 2018 | * u32 field of eVMCS. |
| 2019 | */ |
| 2020 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 2021 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 2022 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2023 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | vmx->nested.dirty_vmcs12 = true; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2027 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2028 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2029 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2030 | /* |
| 2031 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2032 | * reloaded from guest's memory (read only fields, fields not |
| 2033 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2034 | * are no leftovers. |
| 2035 | */ |
| 2036 | if (from_launch) { |
| 2037 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2038 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2039 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2040 | } |
| 2041 | |
| 2042 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2043 | |
| 2044 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2045 | * 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] | 2046 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2047 | */ |
| 2048 | if (from_launch || evmcs_gpa_changed) |
| 2049 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2050 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2051 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2052 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2055 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2056 | { |
| 2057 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2058 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2059 | if (vmx->nested.hv_evmcs) { |
| 2060 | copy_vmcs12_to_enlightened(vmx); |
| 2061 | /* All fields are clean */ |
| 2062 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2063 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2064 | } else { |
| 2065 | copy_vmcs12_to_shadow(vmx); |
| 2066 | } |
| 2067 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2068 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2072 | { |
| 2073 | struct vcpu_vmx *vmx = |
| 2074 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2075 | |
| 2076 | vmx->nested.preemption_timer_expired = true; |
| 2077 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2078 | kvm_vcpu_kick(&vmx->vcpu); |
| 2079 | |
| 2080 | return HRTIMER_NORESTART; |
| 2081 | } |
| 2082 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2083 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2084 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2085 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2086 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2087 | |
| 2088 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2089 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2090 | |
| 2091 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2092 | vmx->nested.preemption_timer_deadline = |
| 2093 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2094 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2095 | } |
| 2096 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2100 | u64 preemption_timeout) |
| 2101 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2102 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2103 | |
| 2104 | /* |
| 2105 | * A timer value of zero is architecturally guaranteed to cause |
| 2106 | * a VMExit prior to executing any instructions in the guest. |
| 2107 | */ |
| 2108 | if (preemption_timeout == 0) { |
| 2109 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2110 | return; |
| 2111 | } |
| 2112 | |
| 2113 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2114 | return; |
| 2115 | |
| 2116 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2117 | preemption_timeout *= 1000000; |
| 2118 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2119 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2120 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2121 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2125 | { |
| 2126 | if (vmx->nested.nested_run_pending && |
| 2127 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2128 | return vmcs12->guest_ia32_efer; |
| 2129 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2130 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2131 | else |
| 2132 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2133 | } |
| 2134 | |
| 2135 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2136 | { |
| 2137 | /* |
| 2138 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2139 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2140 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2141 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2142 | */ |
| 2143 | if (vmx->nested.vmcs02_initialized) |
| 2144 | return; |
| 2145 | vmx->nested.vmcs02_initialized = true; |
| 2146 | |
| 2147 | /* |
| 2148 | * We don't care what the EPTP value is we just need to guarantee |
| 2149 | * it's valid so we don't get a false positive when doing early |
| 2150 | * consistency checks. |
| 2151 | */ |
| 2152 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2153 | vmcs_write64(EPT_POINTER, |
| 2154 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2155 | |
| 2156 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2157 | if (cpu_has_vmx_vmfunc()) |
| 2158 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2159 | |
| 2160 | if (cpu_has_vmx_posted_intr()) |
| 2161 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2162 | |
| 2163 | if (cpu_has_vmx_msr_bitmap()) |
| 2164 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2165 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2166 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2167 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2168 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2169 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2170 | */ |
| 2171 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2172 | vmcs_write64(PML_ADDRESS, 0); |
| 2173 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2174 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2175 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2176 | if (cpu_has_vmx_encls_vmexit()) |
| 2177 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2178 | |
| 2179 | /* |
| 2180 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2181 | * addresses are constant (for vmcs02), the counts can change based |
| 2182 | * on L2's behavior, e.g. switching to/from long mode. |
| 2183 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2184 | 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] | 2185 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2186 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2187 | |
| 2188 | vmx_set_constant_host_state(vmx); |
| 2189 | } |
| 2190 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2191 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2192 | struct vmcs12 *vmcs12) |
| 2193 | { |
| 2194 | prepare_vmcs02_constant_state(vmx); |
| 2195 | |
| 2196 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 2197 | |
| 2198 | if (enable_vpid) { |
| 2199 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2200 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2201 | else |
| 2202 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2207 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2208 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2209 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2210 | |
| 2211 | if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2212 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2213 | |
| 2214 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2215 | * PIN CONTROLS |
| 2216 | */ |
Sean Christopherson | c075c3e | 2019-05-07 12:17:53 -0700 | [diff] [blame] | 2217 | exec_control = vmx_pin_based_exec_ctrl(vmx); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2218 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2219 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2220 | |
| 2221 | /* Posted interrupts setting is only taken from vmcs12. */ |
| 2222 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 2223 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 2224 | vmx->nested.pi_pending = false; |
| 2225 | } else { |
| 2226 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 2227 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2228 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2229 | |
| 2230 | /* |
| 2231 | * EXEC CONTROLS |
| 2232 | */ |
| 2233 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2234 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2235 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2236 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2237 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2238 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2239 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2240 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2241 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2242 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2243 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2244 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2245 | CPU_BASED_CR8_STORE_EXITING; |
| 2246 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2247 | |
| 2248 | /* |
| 2249 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2250 | * for I/O port accesses. |
| 2251 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2252 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2253 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2254 | |
| 2255 | /* |
| 2256 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2257 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2258 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2259 | * only set/clear this bit. |
| 2260 | */ |
| 2261 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2262 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2263 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2264 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2265 | |
| 2266 | /* |
| 2267 | * SECONDARY EXEC CONTROLS |
| 2268 | */ |
| 2269 | if (cpu_has_secondary_exec_ctrls()) { |
| 2270 | exec_control = vmx->secondary_exec_control; |
| 2271 | |
| 2272 | /* Take the following fields only from vmcs12 */ |
| 2273 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 2274 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2275 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2276 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2277 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2278 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2279 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2280 | SECONDARY_EXEC_ENABLE_VMFUNC | |
| 2281 | SECONDARY_EXEC_TSC_SCALING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2282 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2283 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2284 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2285 | |
| 2286 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2287 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2288 | |
| 2289 | /* VMCS shadowing for L2 is emulated for now */ |
| 2290 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2291 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2292 | /* |
| 2293 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2294 | * will not have to rewrite the controls just for this bit. |
| 2295 | */ |
| 2296 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2297 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2298 | exec_control |= SECONDARY_EXEC_DESC; |
| 2299 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2300 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2301 | vmcs_write16(GUEST_INTR_STATUS, |
| 2302 | vmcs12->guest_intr_status); |
| 2303 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2304 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2305 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2306 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 2307 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2308 | vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); |
| 2309 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2310 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | /* |
| 2314 | * ENTRY CONTROLS |
| 2315 | * |
| 2316 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2317 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2318 | * on the related bits (if supported by the CPU) in the hope that |
| 2319 | * we can avoid VMWrites during vmx_set_efer(). |
| 2320 | */ |
| 2321 | exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) & |
| 2322 | ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER; |
| 2323 | if (cpu_has_load_ia32_efer()) { |
| 2324 | if (guest_efer & EFER_LMA) |
| 2325 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2326 | if (guest_efer != host_efer) |
| 2327 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2328 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2329 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2330 | |
| 2331 | /* |
| 2332 | * EXIT CONTROLS |
| 2333 | * |
| 2334 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2335 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2336 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2337 | */ |
| 2338 | exec_control = vmx_vmexit_ctrl(); |
| 2339 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2340 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2341 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2342 | |
| 2343 | /* |
| 2344 | * Interrupt/Exception Fields |
| 2345 | */ |
| 2346 | if (vmx->nested.nested_run_pending) { |
| 2347 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2348 | vmcs12->vm_entry_intr_info_field); |
| 2349 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2350 | vmcs12->vm_entry_exception_error_code); |
| 2351 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2352 | vmcs12->vm_entry_instruction_len); |
| 2353 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2354 | vmcs12->guest_interruptibility_info); |
| 2355 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2356 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2357 | } else { |
| 2358 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2359 | } |
| 2360 | } |
| 2361 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2362 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2363 | { |
| 2364 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2365 | |
| 2366 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2367 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2368 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2369 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2370 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2371 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2372 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2373 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2374 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2375 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2376 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2377 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2378 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2379 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2380 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2381 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2382 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2383 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2384 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2385 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2386 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2387 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2388 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2389 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2390 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2391 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2392 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2393 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2394 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2395 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2396 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2397 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2398 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2399 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2400 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2401 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2402 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2403 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2404 | |
| 2405 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2409 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2410 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2411 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2412 | vmcs12->guest_pending_dbg_exceptions); |
| 2413 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2414 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2415 | |
| 2416 | /* |
| 2417 | * L1 may access the L2's PDPTR, so save them to construct |
| 2418 | * vmcs12 |
| 2419 | */ |
| 2420 | if (enable_ept) { |
| 2421 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2422 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2423 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2424 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2425 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2426 | |
| 2427 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2428 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2429 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2433 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2434 | |
| 2435 | /* |
| 2436 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2437 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2438 | * doesn't care about page faults then we should set all of these to |
| 2439 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2440 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2441 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2442 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2443 | * Note that below we don't need special code to set EB.PF beyond the |
| 2444 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2445 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2446 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2447 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2448 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2449 | /* |
| 2450 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2451 | * go ahead and use it? |
| 2452 | */ |
| 2453 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2454 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2455 | } else { |
| 2456 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2457 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2458 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2459 | |
| 2460 | if (cpu_has_vmx_apicv()) { |
| 2461 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2462 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2463 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2464 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2465 | } |
| 2466 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2467 | /* |
| 2468 | * Make sure the msr_autostore list is up to date before we set the |
| 2469 | * count in the vmcs02. |
| 2470 | */ |
| 2471 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2472 | |
| 2473 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2474 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2475 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2476 | |
| 2477 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2482 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2483 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2484 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2485 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2486 | * function also has additional necessary side-effects, like setting various |
| 2487 | * vcpu->arch fields. |
| 2488 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2489 | * is assigned to entry_failure_code on failure. |
| 2490 | */ |
| 2491 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2492 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2493 | { |
| 2494 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2495 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2496 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2497 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2498 | if (vmx->nested.dirty_vmcs12 || hv_evmcs) { |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2499 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2500 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2501 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2502 | load_guest_pdptrs_vmcs12 = !hv_evmcs || |
| 2503 | !(hv_evmcs->hv_clean_fields & |
| 2504 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | if (vmx->nested.nested_run_pending && |
| 2508 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2509 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2510 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2511 | } else { |
| 2512 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2513 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2514 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2515 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2516 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2517 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2518 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2519 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2520 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2521 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2522 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2523 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2524 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2525 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2526 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2527 | |
| 2528 | if (vmx->nested.nested_run_pending && |
| 2529 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2530 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2531 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2532 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2533 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2534 | } |
| 2535 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2536 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( |
| 2537 | vcpu->arch.l1_tsc_offset, |
| 2538 | vmx_get_l2_tsc_offset(vcpu), |
| 2539 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2540 | |
| 2541 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( |
| 2542 | vcpu->arch.l1_tsc_scaling_ratio, |
| 2543 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2544 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2545 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2546 | if (kvm_has_tsc_control) |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 2547 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2548 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2549 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2550 | |
| 2551 | if (nested_cpu_has_ept(vmcs12)) |
| 2552 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2553 | |
| 2554 | /* |
| 2555 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2556 | * bits which we consider mandatory enabled. |
| 2557 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2558 | * the specifications by L1; It's not enough to take |
| 2559 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2560 | * have more bits than L1 expected. |
| 2561 | */ |
| 2562 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2563 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2564 | |
| 2565 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2566 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2567 | |
| 2568 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2569 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2570 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2571 | |
| 2572 | /* |
| 2573 | * Guest state is invalid and unrestricted guest is disabled, |
| 2574 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2575 | * Fail the VMEntry. |
| 2576 | */ |
Sean Christopherson | 2ba4493 | 2020-09-23 11:44:48 -0700 | [diff] [blame] | 2577 | if (CC(!vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2578 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2579 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2583 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
| 2584 | entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2585 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2586 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2587 | /* |
| 2588 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2589 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2590 | * 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] | 2591 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2592 | * transition to HLT instead of running L2. |
| 2593 | */ |
| 2594 | if (enable_ept) |
| 2595 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2596 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2597 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2598 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2599 | is_pae_paging(vcpu)) { |
| 2600 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2601 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2602 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2603 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2604 | } |
| 2605 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2606 | if (!enable_ept) |
| 2607 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2608 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2609 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2610 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 2611 | vmcs12->guest_ia32_perf_global_ctrl))) |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2612 | return -EINVAL; |
| 2613 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2614 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2615 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2620 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2621 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2622 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2623 | return -EINVAL; |
| 2624 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2625 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2626 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2627 | return -EINVAL; |
| 2628 | |
| 2629 | return 0; |
| 2630 | } |
| 2631 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2632 | 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] | 2633 | { |
| 2634 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2635 | |
| 2636 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2637 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2638 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2639 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2640 | return false; |
| 2641 | break; |
| 2642 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2643 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2644 | return false; |
| 2645 | break; |
| 2646 | default: |
| 2647 | return false; |
| 2648 | } |
| 2649 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2650 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2651 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2652 | case VMX_EPTP_PWL_5: |
| 2653 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2654 | return false; |
| 2655 | break; |
| 2656 | case VMX_EPTP_PWL_4: |
| 2657 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2658 | return false; |
| 2659 | break; |
| 2660 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2661 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2662 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2663 | |
| 2664 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2665 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2666 | return false; |
| 2667 | |
| 2668 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2669 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2670 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2671 | return false; |
| 2672 | } |
| 2673 | |
| 2674 | return true; |
| 2675 | } |
| 2676 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2677 | /* |
| 2678 | * Checks related to VM-Execution Control Fields |
| 2679 | */ |
| 2680 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2681 | struct vmcs12 *vmcs12) |
| 2682 | { |
| 2683 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2684 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2685 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2686 | vmx->nested.msrs.pinbased_ctls_low, |
| 2687 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2688 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2689 | vmx->nested.msrs.procbased_ctls_low, |
| 2690 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2691 | return -EINVAL; |
| 2692 | |
| 2693 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2694 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2695 | vmx->nested.msrs.secondary_ctls_low, |
| 2696 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2697 | return -EINVAL; |
| 2698 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2699 | 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] | 2700 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2701 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2702 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2703 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2704 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2705 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2706 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2707 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2708 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2709 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2710 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2711 | return -EINVAL; |
| 2712 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2713 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2714 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2715 | return -EINVAL; |
| 2716 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2717 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2718 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2719 | return -EINVAL; |
| 2720 | |
| 2721 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2722 | if (CC(vmcs12->vm_function_control & |
| 2723 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2724 | return -EINVAL; |
| 2725 | |
| 2726 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2727 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2728 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2729 | return -EINVAL; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | return 0; |
| 2734 | } |
| 2735 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2736 | /* |
| 2737 | * Checks related to VM-Exit Control Fields |
| 2738 | */ |
| 2739 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2740 | struct vmcs12 *vmcs12) |
| 2741 | { |
| 2742 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2743 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2744 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2745 | vmx->nested.msrs.exit_ctls_low, |
| 2746 | vmx->nested.msrs.exit_ctls_high)) || |
| 2747 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2748 | return -EINVAL; |
| 2749 | |
| 2750 | return 0; |
| 2751 | } |
| 2752 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2753 | /* |
| 2754 | * Checks related to VM-Entry Control Fields |
| 2755 | */ |
| 2756 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2757 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2758 | { |
| 2759 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2760 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2761 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2762 | vmx->nested.msrs.entry_ctls_low, |
| 2763 | vmx->nested.msrs.entry_ctls_high))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2764 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2765 | |
| 2766 | /* |
| 2767 | * From the Intel SDM, volume 3: |
| 2768 | * Fields relevant to VM-entry event injection must be set properly. |
| 2769 | * These fields are the VM-entry interruption-information field, the |
| 2770 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2771 | */ |
| 2772 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2773 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2774 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2775 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2776 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2777 | bool should_have_error_code; |
| 2778 | bool urg = nested_cpu_has2(vmcs12, |
| 2779 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2780 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2781 | |
| 2782 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2783 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2784 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2785 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2786 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2787 | |
| 2788 | /* VM-entry interruption-info field: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2789 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2790 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2791 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
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 | /* VM-entry interruption-info field: deliver error code */ |
| 2795 | should_have_error_code = |
| 2796 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2797 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2798 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2799 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2800 | |
| 2801 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2802 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2803 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2804 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2805 | |
| 2806 | /* VM-entry interruption-info field: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2807 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2808 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2809 | |
| 2810 | /* VM-entry instruction length */ |
| 2811 | switch (intr_type) { |
| 2812 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2813 | case INTR_TYPE_SOFT_INTR: |
| 2814 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2815 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2816 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2817 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2818 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2822 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2823 | return -EINVAL; |
| 2824 | |
| 2825 | return 0; |
| 2826 | } |
| 2827 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2828 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2829 | struct vmcs12 *vmcs12) |
| 2830 | { |
| 2831 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2832 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2833 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2834 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2835 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2836 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2837 | return nested_evmcs_check_controls(vmcs12); |
| 2838 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2839 | return 0; |
| 2840 | } |
| 2841 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2842 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2843 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2844 | { |
| 2845 | bool ia32e; |
| 2846 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2847 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2848 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2849 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2850 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2851 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2852 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2853 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2854 | return -EINVAL; |
| 2855 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2856 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2857 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2858 | return -EINVAL; |
| 2859 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2860 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2861 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2862 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2863 | return -EINVAL; |
| 2864 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2865 | #ifdef CONFIG_X86_64 |
| 2866 | ia32e = !!(vcpu->arch.efer & EFER_LMA); |
| 2867 | #else |
| 2868 | ia32e = false; |
| 2869 | #endif |
| 2870 | |
| 2871 | if (ia32e) { |
| 2872 | if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) || |
| 2873 | CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
| 2874 | return -EINVAL; |
| 2875 | } else { |
| 2876 | if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) || |
| 2877 | CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
| 2878 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2879 | CC((vmcs12->host_rip) >> 32)) |
| 2880 | return -EINVAL; |
| 2881 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2882 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2883 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2884 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2885 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2886 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2887 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2888 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2889 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2890 | CC(vmcs12->host_cs_selector == 0) || |
| 2891 | CC(vmcs12->host_tr_selector == 0) || |
| 2892 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2893 | return -EINVAL; |
| 2894 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2895 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2896 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2897 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2898 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2899 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2900 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2901 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2902 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2903 | /* |
| 2904 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2905 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2906 | * the values of the LMA and LME bits in the field must each be that of |
| 2907 | * the host address-space size VM-exit control. |
| 2908 | */ |
| 2909 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2910 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2911 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2912 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2913 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2914 | } |
| 2915 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2916 | return 0; |
| 2917 | } |
| 2918 | |
| 2919 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2920 | struct vmcs12 *vmcs12) |
| 2921 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2922 | int r = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2923 | struct vmcs12 *shadow; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2924 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2925 | |
| 2926 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 2927 | return 0; |
| 2928 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2929 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2930 | return -EINVAL; |
| 2931 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2932 | 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] | 2933 | return -EINVAL; |
| 2934 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2935 | shadow = map.hva; |
| 2936 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2937 | if (CC(shadow->hdr.revision_id != VMCS12_REVISION) || |
| 2938 | CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2939 | r = -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2940 | |
| 2941 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2942 | return r; |
| 2943 | } |
| 2944 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2945 | /* |
| 2946 | * Checks related to Guest Non-register State |
| 2947 | */ |
| 2948 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2949 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2950 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2951 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2952 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2953 | return -EINVAL; |
| 2954 | |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2958 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2959 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2960 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2961 | { |
| 2962 | bool ia32e; |
| 2963 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2964 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2965 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2966 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2967 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2968 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2969 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2970 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2971 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2972 | return -EINVAL; |
| 2973 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2974 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2975 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2976 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2977 | |
| 2978 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2979 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2980 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2981 | } |
| 2982 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 2983 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2984 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2985 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 2986 | return -EINVAL; |
| 2987 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2988 | /* |
| 2989 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 2990 | * are performed on the field for the IA32_EFER MSR: |
| 2991 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 2992 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 2993 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 2994 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 2995 | * CR0.PG) is 1. |
| 2996 | */ |
| 2997 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 2998 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 2999 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3000 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 3001 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 3002 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 3003 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3004 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 3008 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 3009 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3010 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3011 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 3012 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 3013 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3014 | |
| 3015 | return 0; |
| 3016 | } |
| 3017 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3018 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3019 | { |
| 3020 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3021 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3022 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3023 | |
| 3024 | if (!nested_early_check) |
| 3025 | return 0; |
| 3026 | |
| 3027 | if (vmx->msr_autoload.host.nr) |
| 3028 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3029 | if (vmx->msr_autoload.guest.nr) |
| 3030 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3031 | |
| 3032 | preempt_disable(); |
| 3033 | |
| 3034 | vmx_prepare_switch_to_guest(vcpu); |
| 3035 | |
| 3036 | /* |
| 3037 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3038 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3039 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3040 | * there is no need to preserve other bits or save/restore the field. |
| 3041 | */ |
| 3042 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3043 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3044 | cr3 = __get_current_cr3_fast(); |
| 3045 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 3046 | vmcs_writel(HOST_CR3, cr3); |
| 3047 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 3048 | } |
| 3049 | |
| 3050 | cr4 = cr4_read_shadow(); |
| 3051 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3052 | vmcs_writel(HOST_CR4, cr4); |
| 3053 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3054 | } |
| 3055 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3056 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3057 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 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 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3078 | if (hw_breakpoint_active()) |
| 3079 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3080 | local_irq_enable(); |
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 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3096 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3097 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3098 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3099 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3100 | /* |
| 3101 | * hv_evmcs may end up being not mapped after migration (when |
| 3102 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3103 | * properly reflected. |
| 3104 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3105 | if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) { |
| 3106 | enum nested_evmptrld_status evmptrld_status = |
| 3107 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3108 | |
| 3109 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3110 | evmptrld_status == EVMPTRLD_ERROR) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3111 | return false; |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3112 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3113 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3114 | return true; |
| 3115 | } |
| 3116 | |
| 3117 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3118 | { |
| 3119 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3120 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3121 | struct kvm_host_map *map; |
| 3122 | struct page *page; |
| 3123 | u64 hpa; |
| 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); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3193 | |
| 3194 | return true; |
| 3195 | } |
| 3196 | |
| 3197 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3198 | { |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3199 | if (!nested_get_evmcs_page(vcpu)) { |
| 3200 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3201 | __func__); |
| 3202 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3203 | vcpu->run->internal.suberror = |
| 3204 | KVM_INTERNAL_ERROR_EMULATION; |
| 3205 | vcpu->run->internal.ndata = 0; |
| 3206 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3207 | return false; |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3208 | } |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3209 | |
| 3210 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3211 | return false; |
| 3212 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3213 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3214 | } |
| 3215 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3216 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3217 | { |
| 3218 | struct vmcs12 *vmcs12; |
| 3219 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3220 | gpa_t dst; |
| 3221 | |
| 3222 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3223 | return 0; |
| 3224 | |
| 3225 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3226 | return 1; |
| 3227 | |
| 3228 | /* |
| 3229 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3230 | * set is already checked as part of A/D emulation. |
| 3231 | */ |
| 3232 | vmcs12 = get_vmcs12(vcpu); |
| 3233 | if (!nested_cpu_has_pml(vmcs12)) |
| 3234 | return 0; |
| 3235 | |
| 3236 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3237 | vmx->nested.pml_full = true; |
| 3238 | return 1; |
| 3239 | } |
| 3240 | |
| 3241 | gpa &= ~0xFFFull; |
| 3242 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3243 | |
| 3244 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3245 | offset_in_page(dst), sizeof(gpa))) |
| 3246 | return 0; |
| 3247 | |
| 3248 | vmcs12->guest_pml_index--; |
| 3249 | |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3253 | /* |
| 3254 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3255 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3256 | * slightly different). It also specifies what exception to inject otherwise. |
| 3257 | * Note that many of these exceptions have priority over VM exits, so they |
| 3258 | * don't have to be checked again here. |
| 3259 | */ |
| 3260 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3261 | { |
| 3262 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3263 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
| 3267 | if (vmx_get_cpl(vcpu)) { |
| 3268 | kvm_inject_gp(vcpu, 0); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
| 3275 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3276 | { |
| 3277 | u8 rvi = vmx_get_rvi(); |
| 3278 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3279 | |
| 3280 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3281 | } |
| 3282 | |
| 3283 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3284 | struct vmcs12 *vmcs12); |
| 3285 | |
| 3286 | /* |
| 3287 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3288 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3289 | * |
| 3290 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3291 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3292 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3293 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3294 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3295 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3296 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3297 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3298 | { |
| 3299 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3300 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3301 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3302 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3303 | union vmx_exit_reason exit_reason = { |
| 3304 | .basic = EXIT_REASON_INVALID_STATE, |
| 3305 | .failed_vmentry = 1, |
| 3306 | }; |
| 3307 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3308 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3309 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 3310 | kvm_vcpu_flush_tlb_current(vcpu); |
| 3311 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3312 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3313 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3314 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3315 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3316 | |
| 3317 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3318 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3319 | if (kvm_mpx_supported() && |
| 3320 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3321 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3322 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3323 | /* |
| 3324 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3325 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3326 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3327 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3328 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3329 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3330 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3331 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3332 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3333 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3334 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3335 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3336 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3337 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3338 | */ |
| 3339 | if (!enable_ept && !nested_early_check) |
| 3340 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3341 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3342 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3343 | |
| 3344 | prepare_vmcs02_early(vmx, vmcs12); |
| 3345 | |
| 3346 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3347 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3348 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3349 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3350 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3351 | |
| 3352 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3353 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3354 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3355 | } |
| 3356 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3357 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3358 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3359 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3360 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3361 | goto vmentry_fail_vmexit; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3362 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | enter_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3366 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3367 | if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3368 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3369 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3370 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3371 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3372 | |
| 3373 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3374 | failed_index = nested_vmx_load_msr(vcpu, |
| 3375 | vmcs12->vm_entry_msr_load_addr, |
| 3376 | vmcs12->vm_entry_msr_load_count); |
| 3377 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3378 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3379 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3380 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3381 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3382 | } else { |
| 3383 | /* |
| 3384 | * The MMU is not initialized to point at the right entities yet and |
| 3385 | * "get pages" would need to read data from the guest (i.e. we will |
| 3386 | * need to perform gpa to hpa translation). Request a call |
| 3387 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3388 | * have already been set at vmentry time and should not be reset. |
| 3389 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3390 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | /* |
| 3394 | * If L1 had a pending IRQ/NMI until it executed |
| 3395 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3396 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3397 | * evaluate if this pending event should cause an exit from L2 |
| 3398 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3399 | * intercept EXTERNAL_INTERRUPT). |
| 3400 | * |
| 3401 | * Usually this would be handled by the processor noticing an |
| 3402 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3403 | * pending virtual interrupts. However, this setting was done |
| 3404 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3405 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3406 | */ |
| 3407 | if (unlikely(evaluate_pending_interrupts)) |
| 3408 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3409 | |
| 3410 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3411 | * Do not start the preemption timer hrtimer until after we know |
| 3412 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3413 | * the timer. |
| 3414 | */ |
| 3415 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3416 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3417 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3418 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3419 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3420 | |
| 3421 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3422 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3423 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3424 | * returned as far as L1 is concerned. It will only return (and set |
| 3425 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3426 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3427 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3428 | |
| 3429 | /* |
| 3430 | * A failed consistency check that leads to a VMExit during L1's |
| 3431 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3432 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3433 | */ |
| 3434 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3435 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3436 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3437 | leave_guest_mode(vcpu); |
| 3438 | |
| 3439 | vmentry_fail_vmexit: |
| 3440 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3441 | |
| 3442 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3443 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3444 | |
| 3445 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3446 | vmcs12->vm_exit_reason = exit_reason.full; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3447 | if (enable_shadow_vmcs || vmx->nested.hv_evmcs) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3448 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3449 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | /* |
| 3453 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3454 | * for running an L2 nested guest. |
| 3455 | */ |
| 3456 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3457 | { |
| 3458 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3459 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3460 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3461 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3462 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3463 | |
Dongli Zhang | 43c11d9 | 2021-03-05 14:57:47 -0800 | [diff] [blame] | 3464 | ++vcpu->stat.nested_run; |
| 3465 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3466 | if (!nested_vmx_check_permission(vcpu)) |
| 3467 | return 1; |
| 3468 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3469 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3470 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3471 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3472 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3473 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3474 | return nested_vmx_failInvalid(vcpu); |
| 3475 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3476 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3477 | if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3478 | return nested_vmx_failInvalid(vcpu); |
| 3479 | |
| 3480 | vmcs12 = get_vmcs12(vcpu); |
| 3481 | |
| 3482 | /* |
| 3483 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3484 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3485 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3486 | * VM-instruction error field. |
| 3487 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3488 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3489 | return nested_vmx_failInvalid(vcpu); |
| 3490 | |
| 3491 | if (vmx->nested.hv_evmcs) { |
| 3492 | copy_enlightened_to_vmcs12(vmx); |
| 3493 | /* Enlightened VMCS doesn't have launch state */ |
| 3494 | vmcs12->launch_state = !launch; |
| 3495 | } else if (enable_shadow_vmcs) { |
| 3496 | copy_shadow_to_vmcs12(vmx); |
| 3497 | } |
| 3498 | |
| 3499 | /* |
| 3500 | * The nested entry process starts with enforcing various prerequisites |
| 3501 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3502 | * they fail: As the SDM explains, some conditions should cause the |
| 3503 | * instruction to fail, while others will cause the instruction to seem |
| 3504 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3505 | * To speed up the normal (success) code path, we should avoid checking |
| 3506 | * for misconfigurations which will anyway be caught by the processor |
| 3507 | * when using the merged vmcs02. |
| 3508 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3509 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3510 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3511 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3512 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3513 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3514 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3515 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3516 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3517 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3518 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3519 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3520 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3521 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3522 | |
| 3523 | /* |
| 3524 | * We're finally done with prerequisite checking, and can start with |
| 3525 | * the nested entry. |
| 3526 | */ |
| 3527 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3528 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3529 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3530 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3531 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3532 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3533 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3534 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3535 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3536 | vmx->nested.pi_pending = true; |
| 3537 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3538 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3539 | } |
| 3540 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3541 | /* Hide L1D cache contents from the nested guest. */ |
| 3542 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3543 | |
| 3544 | /* |
| 3545 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3546 | * also be used as part of restoring nVMX state for |
| 3547 | * snapshot restore (migration). |
| 3548 | * |
| 3549 | * In this flow, it is assumed that vmcs12 cache was |
Ingo Molnar | 163b099 | 2021-03-21 22:28:53 +0100 | [diff] [blame] | 3550 | * transferred as part of captured nVMX state and should |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3551 | * therefore not be read from guest memory (which may not |
| 3552 | * exist on destination host yet). |
| 3553 | */ |
| 3554 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3555 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3556 | switch (vmcs12->guest_activity_state) { |
| 3557 | case GUEST_ACTIVITY_HLT: |
| 3558 | /* |
| 3559 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3560 | * awakened by event injection or by an NMI-window VM-exit or |
| 3561 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3562 | */ |
| 3563 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3564 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3565 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3566 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3567 | vmx->nested.nested_run_pending = 0; |
| 3568 | return kvm_vcpu_halt(vcpu); |
| 3569 | } |
| 3570 | break; |
| 3571 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3572 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3573 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3574 | break; |
| 3575 | default: |
| 3576 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3577 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3578 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3579 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3580 | |
| 3581 | vmentry_failed: |
| 3582 | vmx->nested.nested_run_pending = 0; |
| 3583 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3584 | return 0; |
| 3585 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3586 | return 1; |
| 3587 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3588 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | /* |
| 3592 | * 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] | 3593 | * 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] | 3594 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3595 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3596 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3597 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3598 | * didn't trap the bit, because if L1 did, so would L0). |
| 3599 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3600 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3601 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3602 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3603 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3604 | * changed these bits, and therefore they need to be updated, but L0 |
| 3605 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3606 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3607 | */ |
| 3608 | static inline unsigned long |
| 3609 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3610 | { |
| 3611 | return |
| 3612 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3613 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3614 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3615 | vcpu->arch.cr0_guest_owned_bits)); |
| 3616 | } |
| 3617 | |
| 3618 | static inline unsigned long |
| 3619 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3620 | { |
| 3621 | return |
| 3622 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3623 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3624 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3625 | vcpu->arch.cr4_guest_owned_bits)); |
| 3626 | } |
| 3627 | |
| 3628 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3629 | struct vmcs12 *vmcs12) |
| 3630 | { |
| 3631 | u32 idt_vectoring; |
| 3632 | unsigned int nr; |
| 3633 | |
| 3634 | if (vcpu->arch.exception.injected) { |
| 3635 | nr = vcpu->arch.exception.nr; |
| 3636 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3637 | |
| 3638 | if (kvm_exception_is_soft(nr)) { |
| 3639 | vmcs12->vm_exit_instruction_len = |
| 3640 | vcpu->arch.event_exit_inst_len; |
| 3641 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3642 | } else |
| 3643 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3644 | |
| 3645 | if (vcpu->arch.exception.has_error_code) { |
| 3646 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3647 | vmcs12->idt_vectoring_error_code = |
| 3648 | vcpu->arch.exception.error_code; |
| 3649 | } |
| 3650 | |
| 3651 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3652 | } else if (vcpu->arch.nmi_injected) { |
| 3653 | vmcs12->idt_vectoring_info_field = |
| 3654 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3655 | } else if (vcpu->arch.interrupt.injected) { |
| 3656 | nr = vcpu->arch.interrupt.nr; |
| 3657 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3658 | |
| 3659 | if (vcpu->arch.interrupt.soft) { |
| 3660 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3661 | vmcs12->vm_entry_instruction_len = |
| 3662 | vcpu->arch.event_exit_inst_len; |
| 3663 | } else |
| 3664 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3665 | |
| 3666 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3671 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3672 | { |
| 3673 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3674 | gfn_t gfn; |
| 3675 | |
| 3676 | /* |
| 3677 | * Don't need to mark the APIC access page dirty; it is never |
| 3678 | * written to by the CPU during APIC virtualization. |
| 3679 | */ |
| 3680 | |
| 3681 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3682 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3683 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3684 | } |
| 3685 | |
| 3686 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3687 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3688 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3689 | } |
| 3690 | } |
| 3691 | |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3692 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3693 | { |
| 3694 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3695 | int max_irr; |
| 3696 | void *vapic_page; |
| 3697 | u16 status; |
| 3698 | |
| 3699 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3700 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3701 | |
| 3702 | vmx->nested.pi_pending = false; |
| 3703 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3704 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3705 | |
| 3706 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3707 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3708 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3709 | if (!vapic_page) |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame^] | 3710 | goto mmio_needed; |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3711 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3712 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3713 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3714 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3715 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3716 | status &= ~0xff; |
| 3717 | status |= (u8)max_irr; |
| 3718 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | nested_mark_vmcs12_pages_dirty(vcpu); |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3723 | return 0; |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame^] | 3724 | |
| 3725 | mmio_needed: |
| 3726 | kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); |
| 3727 | return -ENXIO; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3731 | unsigned long exit_qual) |
| 3732 | { |
| 3733 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3734 | unsigned int nr = vcpu->arch.exception.nr; |
| 3735 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3736 | |
| 3737 | if (vcpu->arch.exception.has_error_code) { |
| 3738 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3739 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3740 | } |
| 3741 | |
| 3742 | if (kvm_exception_is_soft(nr)) |
| 3743 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3744 | else |
| 3745 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3746 | |
| 3747 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3748 | vmx_get_nmi_mask(vcpu)) |
| 3749 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3750 | |
| 3751 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3752 | } |
| 3753 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3754 | /* |
| 3755 | * Returns true if a debug trap is pending delivery. |
| 3756 | * |
| 3757 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3758 | * exception may be inferred from the presence of an exception payload. |
| 3759 | */ |
| 3760 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3761 | { |
| 3762 | return vcpu->arch.exception.pending && |
| 3763 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3764 | vcpu->arch.exception.payload; |
| 3765 | } |
| 3766 | |
| 3767 | /* |
| 3768 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3769 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3770 | * represents these debug traps with a payload that is said to be compatible |
| 3771 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3772 | * field if a VM-exit is delivered before the debug trap. |
| 3773 | */ |
| 3774 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3775 | { |
| 3776 | if (vmx_pending_dbg_trap(vcpu)) |
| 3777 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3778 | vcpu->arch.exception.payload); |
| 3779 | } |
| 3780 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3781 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3782 | { |
| 3783 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3784 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3785 | } |
| 3786 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3787 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3788 | { |
| 3789 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3790 | unsigned long exit_qual; |
| 3791 | bool block_nested_events = |
| 3792 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3793 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3794 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3795 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3796 | /* |
| 3797 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3798 | * this state is discarded. |
| 3799 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3800 | if (!block_nested_events) |
| 3801 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3802 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3803 | if (lapic_in_kernel(vcpu) && |
| 3804 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3805 | if (block_nested_events) |
| 3806 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3807 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3808 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3809 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3810 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | |
| 3814 | if (lapic_in_kernel(vcpu) && |
| 3815 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3816 | if (block_nested_events) |
| 3817 | return -EBUSY; |
| 3818 | |
| 3819 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3820 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3821 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3822 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3823 | return 0; |
| 3824 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3825 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3826 | /* |
| 3827 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3828 | * |
| 3829 | * Note that only a pending nested run can block a pending exception. |
| 3830 | * Otherwise an injected NMI/interrupt should either be |
| 3831 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3832 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3833 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3834 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3835 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3836 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3837 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3838 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3839 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3840 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3841 | return 0; |
| 3842 | } |
| 3843 | |
| 3844 | if (mtf_pending) { |
| 3845 | if (block_nested_events) |
| 3846 | return -EBUSY; |
| 3847 | nested_vmx_update_pending_dbg(vcpu); |
| 3848 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3849 | return 0; |
| 3850 | } |
| 3851 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3852 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3853 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3854 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3855 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3856 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3857 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3858 | return 0; |
| 3859 | } |
| 3860 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3861 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3862 | if (block_nested_events) |
| 3863 | return -EBUSY; |
| 3864 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3865 | return 0; |
| 3866 | } |
| 3867 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3868 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3869 | if (block_nested_events) |
| 3870 | return -EBUSY; |
| 3871 | goto no_vmexit; |
| 3872 | } |
| 3873 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3874 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3875 | if (block_nested_events) |
| 3876 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3877 | if (!nested_exit_on_nmi(vcpu)) |
| 3878 | goto no_vmexit; |
| 3879 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3880 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3881 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3882 | INTR_INFO_VALID_MASK, 0); |
| 3883 | /* |
| 3884 | * The NMI-triggered VM exit counts as injection: |
| 3885 | * clear this one and block further NMIs. |
| 3886 | */ |
| 3887 | vcpu->arch.nmi_pending = 0; |
| 3888 | vmx_set_nmi_mask(vcpu, true); |
| 3889 | return 0; |
| 3890 | } |
| 3891 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3892 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3893 | if (block_nested_events) |
| 3894 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3895 | if (!nested_exit_on_intr(vcpu)) |
| 3896 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3897 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3898 | return 0; |
| 3899 | } |
| 3900 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3901 | no_vmexit: |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3902 | return vmx_complete_nested_posted_interrupt(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3906 | { |
| 3907 | ktime_t remaining = |
| 3908 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3909 | u64 value; |
| 3910 | |
| 3911 | if (ktime_to_ns(remaining) <= 0) |
| 3912 | return 0; |
| 3913 | |
| 3914 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3915 | do_div(value, 1000000); |
| 3916 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3917 | } |
| 3918 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3919 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3920 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3921 | switch (field) { |
| 3922 | case GUEST_ES_SELECTOR: |
| 3923 | case GUEST_CS_SELECTOR: |
| 3924 | case GUEST_SS_SELECTOR: |
| 3925 | case GUEST_DS_SELECTOR: |
| 3926 | case GUEST_FS_SELECTOR: |
| 3927 | case GUEST_GS_SELECTOR: |
| 3928 | case GUEST_LDTR_SELECTOR: |
| 3929 | case GUEST_TR_SELECTOR: |
| 3930 | case GUEST_ES_LIMIT: |
| 3931 | case GUEST_CS_LIMIT: |
| 3932 | case GUEST_SS_LIMIT: |
| 3933 | case GUEST_DS_LIMIT: |
| 3934 | case GUEST_FS_LIMIT: |
| 3935 | case GUEST_GS_LIMIT: |
| 3936 | case GUEST_LDTR_LIMIT: |
| 3937 | case GUEST_TR_LIMIT: |
| 3938 | case GUEST_GDTR_LIMIT: |
| 3939 | case GUEST_IDTR_LIMIT: |
| 3940 | case GUEST_ES_AR_BYTES: |
| 3941 | case GUEST_DS_AR_BYTES: |
| 3942 | case GUEST_FS_AR_BYTES: |
| 3943 | case GUEST_GS_AR_BYTES: |
| 3944 | case GUEST_LDTR_AR_BYTES: |
| 3945 | case GUEST_TR_AR_BYTES: |
| 3946 | case GUEST_ES_BASE: |
| 3947 | case GUEST_CS_BASE: |
| 3948 | case GUEST_SS_BASE: |
| 3949 | case GUEST_DS_BASE: |
| 3950 | case GUEST_FS_BASE: |
| 3951 | case GUEST_GS_BASE: |
| 3952 | case GUEST_LDTR_BASE: |
| 3953 | case GUEST_TR_BASE: |
| 3954 | case GUEST_GDTR_BASE: |
| 3955 | case GUEST_IDTR_BASE: |
| 3956 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 3957 | case GUEST_BNDCFGS: |
| 3958 | return true; |
| 3959 | default: |
| 3960 | break; |
| 3961 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3962 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3963 | return false; |
| 3964 | } |
| 3965 | |
| 3966 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 3967 | struct vmcs12 *vmcs12) |
| 3968 | { |
| 3969 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3970 | |
| 3971 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 3972 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 3973 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 3974 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 3975 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 3976 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 3977 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 3978 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 3979 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 3980 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 3981 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 3982 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 3983 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 3984 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 3985 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 3986 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 3987 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3988 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3989 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3990 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 3991 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 3992 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 3993 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 3994 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 3995 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 3996 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 3997 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 3998 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 3999 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 4000 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 4001 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 4002 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 4003 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 4004 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4005 | vmcs12->guest_pending_dbg_exceptions = |
| 4006 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 4007 | if (kvm_mpx_supported()) |
| 4008 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 4009 | |
| 4010 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 4011 | } |
| 4012 | |
| 4013 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4014 | struct vmcs12 *vmcs12) |
| 4015 | { |
| 4016 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4017 | int cpu; |
| 4018 | |
| 4019 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4020 | return; |
| 4021 | |
| 4022 | |
| 4023 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4024 | |
| 4025 | cpu = get_cpu(); |
| 4026 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4027 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4028 | |
| 4029 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4030 | |
| 4031 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4032 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4033 | put_cpu(); |
| 4034 | } |
| 4035 | |
| 4036 | /* |
| 4037 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4038 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4039 | * VM-entry controls is also updated, since this is really a guest |
| 4040 | * state bit.) |
| 4041 | */ |
| 4042 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4043 | { |
| 4044 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4045 | |
| 4046 | if (vmx->nested.hv_evmcs) |
| 4047 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4048 | |
| 4049 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs; |
| 4050 | |
| 4051 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4052 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4053 | |
| 4054 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4055 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4056 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4057 | |
| 4058 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4059 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4060 | |
| 4061 | vmcs12->guest_interruptibility_info = |
| 4062 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4063 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4064 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4065 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4066 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4067 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4068 | else |
| 4069 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4070 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4071 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4072 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4073 | !vmx->nested.nested_run_pending) |
| 4074 | vmcs12->vmx_preemption_timer_value = |
| 4075 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4076 | |
| 4077 | /* |
| 4078 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4079 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4080 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4081 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4082 | * |
| 4083 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4084 | */ |
| 4085 | if (enable_ept) { |
| 4086 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4087 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4088 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4089 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4090 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4091 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4092 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4096 | |
| 4097 | if (nested_cpu_has_vid(vmcs12)) |
| 4098 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4099 | |
| 4100 | vmcs12->vm_entry_controls = |
| 4101 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4102 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4103 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4104 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4105 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4106 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4107 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4108 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | /* |
| 4112 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4113 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4114 | * and this function updates it to reflect the changes to the guest state while |
| 4115 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4116 | * without going back to L1), and to reflect the exit reason. |
| 4117 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4118 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4119 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4120 | * which already writes to vmcs12 directly. |
| 4121 | */ |
| 4122 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4123 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4124 | unsigned long exit_qualification) |
| 4125 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4126 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4127 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame] | 4128 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4129 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4130 | vmcs12->exit_qualification = exit_qualification; |
| 4131 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4132 | |
| 4133 | vmcs12->idt_vectoring_info_field = 0; |
| 4134 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4135 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4136 | |
| 4137 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4138 | vmcs12->launch_state = 1; |
| 4139 | |
| 4140 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4141 | * instead of reading the real value. */ |
| 4142 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4143 | |
| 4144 | /* |
| 4145 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4146 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4147 | */ |
| 4148 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4149 | |
| 4150 | /* |
| 4151 | * According to spec, there's no need to store the guest's |
| 4152 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4153 | * during or after loading the guest state. Since this exit |
| 4154 | * does not fall in that category, we need to save the MSRs. |
| 4155 | */ |
| 4156 | if (nested_vmx_store_msr(vcpu, |
| 4157 | vmcs12->vm_exit_msr_store_addr, |
| 4158 | vmcs12->vm_exit_msr_store_count)) |
| 4159 | nested_vmx_abort(vcpu, |
| 4160 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | /* |
| 4164 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4165 | * preserved above and would only end up incorrectly in L1. |
| 4166 | */ |
| 4167 | vcpu->arch.nmi_injected = false; |
| 4168 | kvm_clear_exception_queue(vcpu); |
| 4169 | kvm_clear_interrupt_queue(vcpu); |
| 4170 | } |
| 4171 | |
| 4172 | /* |
| 4173 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4174 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4175 | * in vmcs12. |
| 4176 | * This function is to be called not only on normal nested exit, but also on |
| 4177 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4178 | * Failures During or After Loading Guest State"). |
| 4179 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4180 | */ |
| 4181 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4182 | struct vmcs12 *vmcs12) |
| 4183 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4184 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4185 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4186 | |
| 4187 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4188 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4189 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4190 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4191 | else |
| 4192 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4193 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4194 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4195 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4196 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4197 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4198 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4199 | |
| 4200 | /* |
| 4201 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4202 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4203 | * |
| 4204 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4205 | * (KVM doesn't change it); |
| 4206 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4207 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4208 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4209 | |
| 4210 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4211 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4212 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4213 | |
| 4214 | nested_ept_uninit_mmu_context(vcpu); |
| 4215 | |
| 4216 | /* |
| 4217 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4218 | * couldn't have changed. |
| 4219 | */ |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4220 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4221 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4222 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4223 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4224 | |
| 4225 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4226 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4227 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4228 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4229 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4230 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4231 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4232 | |
| 4233 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4234 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4235 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4236 | |
| 4237 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4238 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4239 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4240 | } |
| 4241 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4242 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4243 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4244 | |
| 4245 | /* Set L1 segment info according to Intel SDM |
| 4246 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4247 | seg = (struct kvm_segment) { |
| 4248 | .base = 0, |
| 4249 | .limit = 0xFFFFFFFF, |
| 4250 | .selector = vmcs12->host_cs_selector, |
| 4251 | .type = 11, |
| 4252 | .present = 1, |
| 4253 | .s = 1, |
| 4254 | .g = 1 |
| 4255 | }; |
| 4256 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4257 | seg.l = 1; |
| 4258 | else |
| 4259 | seg.db = 1; |
| 4260 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 4261 | seg = (struct kvm_segment) { |
| 4262 | .base = 0, |
| 4263 | .limit = 0xFFFFFFFF, |
| 4264 | .type = 3, |
| 4265 | .present = 1, |
| 4266 | .s = 1, |
| 4267 | .db = 1, |
| 4268 | .g = 1 |
| 4269 | }; |
| 4270 | seg.selector = vmcs12->host_ds_selector; |
| 4271 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 4272 | seg.selector = vmcs12->host_es_selector; |
| 4273 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 4274 | seg.selector = vmcs12->host_ss_selector; |
| 4275 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 4276 | seg.selector = vmcs12->host_fs_selector; |
| 4277 | seg.base = vmcs12->host_fs_base; |
| 4278 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 4279 | seg.selector = vmcs12->host_gs_selector; |
| 4280 | seg.base = vmcs12->host_gs_base; |
| 4281 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 4282 | seg = (struct kvm_segment) { |
| 4283 | .base = vmcs12->host_tr_base, |
| 4284 | .limit = 0x67, |
| 4285 | .selector = vmcs12->host_tr_selector, |
| 4286 | .type = 11, |
| 4287 | .present = 1 |
| 4288 | }; |
| 4289 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 4290 | |
| 4291 | kvm_set_dr(vcpu, 7, 0x400); |
| 4292 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4293 | |
| 4294 | if (cpu_has_vmx_msr_bitmap()) |
| 4295 | vmx_update_msr_bitmap(vcpu); |
| 4296 | |
| 4297 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4298 | vmcs12->vm_exit_msr_load_count)) |
| 4299 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4300 | } |
| 4301 | |
| 4302 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4303 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4304 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4305 | unsigned int i; |
| 4306 | |
| 4307 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4308 | return vmcs_read64(GUEST_IA32_EFER); |
| 4309 | |
| 4310 | if (cpu_has_load_ia32_efer()) |
| 4311 | return host_efer; |
| 4312 | |
| 4313 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4314 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4315 | return vmx->msr_autoload.guest.val[i].value; |
| 4316 | } |
| 4317 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4318 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4319 | if (efer_msr) |
| 4320 | return efer_msr->data; |
| 4321 | |
| 4322 | return host_efer; |
| 4323 | } |
| 4324 | |
| 4325 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4326 | { |
| 4327 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4328 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4329 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4330 | gpa_t gpa; |
| 4331 | u32 i, j; |
| 4332 | |
| 4333 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4334 | |
| 4335 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4336 | /* |
| 4337 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4338 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4339 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4340 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4341 | */ |
| 4342 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4343 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4344 | else |
| 4345 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4346 | } |
| 4347 | |
| 4348 | /* |
| 4349 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4350 | * handle a variety of side effects to KVM's software model. |
| 4351 | */ |
| 4352 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4353 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4354 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4355 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4356 | |
| 4357 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4358 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4359 | |
| 4360 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4361 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4362 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4363 | |
| 4364 | /* |
| 4365 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4366 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4367 | * VMFail, like everything else we just need to ensure our |
| 4368 | * software model is up-to-date. |
| 4369 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4370 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4371 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4372 | |
| 4373 | kvm_mmu_reset_context(vcpu); |
| 4374 | |
| 4375 | if (cpu_has_vmx_msr_bitmap()) |
| 4376 | vmx_update_msr_bitmap(vcpu); |
| 4377 | |
| 4378 | /* |
| 4379 | * This nasty bit of open coding is a compromise between blindly |
| 4380 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4381 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4382 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4383 | * expensive since the lists are unbound by hardware). For each |
| 4384 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4385 | * list, reload it from the exit load list if it exists and differs |
| 4386 | * from the guest value. The intent is to stuff host state as |
| 4387 | * silently as possible, not to fully process the exit load list. |
| 4388 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4389 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4390 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4391 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4392 | pr_debug_ratelimited( |
| 4393 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4394 | __func__, i, gpa); |
| 4395 | goto vmabort; |
| 4396 | } |
| 4397 | |
| 4398 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4399 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4400 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4401 | pr_debug_ratelimited( |
| 4402 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4403 | __func__, j, gpa); |
| 4404 | goto vmabort; |
| 4405 | } |
| 4406 | if (h.index != g.index) |
| 4407 | continue; |
| 4408 | if (h.value == g.value) |
| 4409 | break; |
| 4410 | |
| 4411 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4412 | pr_debug_ratelimited( |
| 4413 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4414 | __func__, j, h.index, h.reserved); |
| 4415 | goto vmabort; |
| 4416 | } |
| 4417 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4418 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4419 | pr_debug_ratelimited( |
| 4420 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4421 | __func__, j, h.index, h.value); |
| 4422 | goto vmabort; |
| 4423 | } |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | return; |
| 4428 | |
| 4429 | vmabort: |
| 4430 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4431 | } |
| 4432 | |
| 4433 | /* |
| 4434 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4435 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4436 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4437 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4438 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4439 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4440 | { |
| 4441 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4442 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4443 | |
| 4444 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4445 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4446 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4447 | /* Similarly, triple faults in L2 should never escape. */ |
| 4448 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4449 | |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 4450 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 4451 | /* |
| 4452 | * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map |
| 4453 | * Enlightened VMCS after migration and we still need to |
| 4454 | * do that when something is forcing L2->L1 exit prior to |
| 4455 | * the first L2 run. |
| 4456 | */ |
| 4457 | (void)nested_get_evmcs_page(vcpu); |
| 4458 | } |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4459 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4460 | /* Service the TLB flush request for L2 before switching to L1. */ |
| 4461 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 4462 | kvm_vcpu_flush_tlb_current(vcpu); |
| 4463 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4464 | /* |
| 4465 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4466 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4467 | * up-to-date before switching to L1. |
| 4468 | */ |
| 4469 | if (enable_ept && is_pae_paging(vcpu)) |
| 4470 | vmx_ept_load_pdptrs(vcpu); |
| 4471 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4472 | leave_guest_mode(vcpu); |
| 4473 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4474 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4475 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4476 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 4477 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { |
| 4478 | vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; |
| 4479 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) |
| 4480 | vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; |
| 4481 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4482 | |
| 4483 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4484 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4485 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4486 | if (vm_exit_reason != -1) |
| 4487 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4488 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4489 | |
| 4490 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4491 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4492 | * also be used to capture vmcs12 cache as part of |
| 4493 | * capturing nVMX state for snapshot (migration). |
| 4494 | * |
| 4495 | * Otherwise, this flush will dirty guest memory at a |
| 4496 | * point it is already assumed by user-space to be |
| 4497 | * immutable. |
| 4498 | */ |
| 4499 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4500 | } else { |
| 4501 | /* |
| 4502 | * The only expected VM-instruction error is "VM entry with |
| 4503 | * invalid control field(s)." Anything else indicates a |
| 4504 | * problem with L0. And we should never get here with a |
| 4505 | * VMFail of any type if early consistency checks are enabled. |
| 4506 | */ |
| 4507 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4508 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4509 | WARN_ON_ONCE(nested_early_check); |
| 4510 | } |
| 4511 | |
| 4512 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4513 | |
| 4514 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4515 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4516 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4517 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 4518 | if (kvm_has_tsc_control) |
| 4519 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
| 4520 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4521 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4522 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4523 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4524 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4525 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4526 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4529 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4530 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4531 | vmx_update_cpu_dirty_logging(vcpu); |
| 4532 | } |
| 4533 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4534 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4535 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4536 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4537 | vmx->nested.apic_access_page = NULL; |
| 4538 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4539 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4540 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4541 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4542 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4543 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4544 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4545 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4546 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4547 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4548 | if ((vm_exit_reason != -1) && |
| 4549 | (enable_shadow_vmcs || vmx->nested.hv_evmcs)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4550 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4551 | |
| 4552 | /* in case we halted in L2 */ |
| 4553 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4554 | |
| 4555 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4556 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4557 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4558 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4559 | WARN_ON(irq < 0); |
| 4560 | vmcs12->vm_exit_intr_info = irq | |
| 4561 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4562 | } |
| 4563 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4564 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4565 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4566 | vmcs12->exit_qualification, |
| 4567 | vmcs12->idt_vectoring_info_field, |
| 4568 | vmcs12->vm_exit_intr_info, |
| 4569 | vmcs12->vm_exit_intr_error_code, |
| 4570 | KVM_ISA_VMX); |
| 4571 | |
| 4572 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4573 | |
| 4574 | return; |
| 4575 | } |
| 4576 | |
| 4577 | /* |
| 4578 | * After an early L2 VM-entry failure, we're now back |
| 4579 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4580 | * VMRESUME instruction, so we need to set the failure |
| 4581 | * flag and the VM-instruction error field of the VMCS |
| 4582 | * accordingly, and skip the emulated instruction. |
| 4583 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4584 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4585 | |
| 4586 | /* |
| 4587 | * Restore L1's host state to KVM's software model. We're here |
| 4588 | * because a consistency check was caught by hardware, which |
| 4589 | * means some amount of guest state has been propagated to KVM's |
| 4590 | * model and needs to be unwound to the host's state. |
| 4591 | */ |
| 4592 | nested_vmx_restore_host_state(vcpu); |
| 4593 | |
| 4594 | vmx->fail = 0; |
| 4595 | } |
| 4596 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4597 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4598 | { |
| 4599 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4600 | } |
| 4601 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4602 | /* |
| 4603 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4604 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4605 | * 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] | 4606 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4607 | */ |
| 4608 | 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] | 4609 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4610 | { |
| 4611 | gva_t off; |
| 4612 | bool exn; |
| 4613 | struct kvm_segment s; |
| 4614 | |
| 4615 | /* |
| 4616 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4617 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4618 | * addressing components of the operand. Only the displacement part |
| 4619 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4620 | * For how an actual address is calculated from all these components, |
| 4621 | * refer to Vol. 1, "Operand Addressing". |
| 4622 | */ |
| 4623 | int scaling = vmx_instruction_info & 3; |
| 4624 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4625 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4626 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4627 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4628 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4629 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4630 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4631 | |
| 4632 | if (is_reg) { |
| 4633 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4634 | return 1; |
| 4635 | } |
| 4636 | |
| 4637 | /* Addr = segment_base + offset */ |
| 4638 | /* offset = base + [index * scale] + displacement */ |
| 4639 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4640 | if (addr_size == 1) |
| 4641 | off = (gva_t)sign_extend64(off, 31); |
| 4642 | else if (addr_size == 0) |
| 4643 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4644 | if (base_is_valid) |
| 4645 | off += kvm_register_read(vcpu, base_reg); |
| 4646 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4647 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4648 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4649 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4650 | /* |
| 4651 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4652 | * based on the address size of the instruction. Note that this is |
| 4653 | * the *effective address*, i.e. the address prior to accounting for |
| 4654 | * the segment's base. |
| 4655 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4656 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4657 | off &= 0xffffffff; |
| 4658 | else if (addr_size == 0) /* 16 bit */ |
| 4659 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4660 | |
| 4661 | /* Checks for #GP/#SS exceptions. */ |
| 4662 | exn = false; |
| 4663 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4664 | /* |
| 4665 | * The virtual/linear address is never truncated in 64-bit |
| 4666 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4667 | * address when using FS/GS with a non-zero base. |
| 4668 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4669 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4670 | *ret = s.base + off; |
| 4671 | else |
| 4672 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4673 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4674 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4675 | * non-canonical form. This is the only check on the memory |
| 4676 | * destination for long mode! |
| 4677 | */ |
| 4678 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4679 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4680 | /* |
| 4681 | * When not in long mode, the virtual/linear address is |
| 4682 | * unconditionally truncated to 32 bits regardless of the |
| 4683 | * address size. |
| 4684 | */ |
| 4685 | *ret = (s.base + off) & 0xffffffff; |
| 4686 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4687 | /* Protected mode: apply checks for segment validity in the |
| 4688 | * following order: |
| 4689 | * - segment type check (#GP(0) may be thrown) |
| 4690 | * - usability check (#GP(0)/#SS(0)) |
| 4691 | * - limit check (#GP(0)/#SS(0)) |
| 4692 | */ |
| 4693 | if (wr) |
| 4694 | /* #GP(0) if the destination operand is located in a |
| 4695 | * read-only data segment or any code segment. |
| 4696 | */ |
| 4697 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4698 | else |
| 4699 | /* #GP(0) if the source operand is located in an |
| 4700 | * execute-only code segment |
| 4701 | */ |
| 4702 | exn = ((s.type & 0xa) == 8); |
| 4703 | if (exn) { |
| 4704 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4705 | return 1; |
| 4706 | } |
| 4707 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4708 | */ |
| 4709 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4710 | |
| 4711 | /* |
| 4712 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4713 | * outside the segment limit. All CPUs that support VMX ignore |
| 4714 | * limit checks for flat segments, i.e. segments with base==0, |
| 4715 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4716 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4717 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4718 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4719 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4720 | } |
| 4721 | if (exn) { |
| 4722 | kvm_queue_exception_e(vcpu, |
| 4723 | seg_reg == VCPU_SREG_SS ? |
| 4724 | SS_VECTOR : GP_VECTOR, |
| 4725 | 0); |
| 4726 | return 1; |
| 4727 | } |
| 4728 | |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4732 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4733 | { |
| 4734 | struct vcpu_vmx *vmx; |
| 4735 | |
| 4736 | if (!nested_vmx_allowed(vcpu)) |
| 4737 | return; |
| 4738 | |
| 4739 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4740 | 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] | 4741 | vmx->nested.msrs.entry_ctls_high |= |
| 4742 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4743 | vmx->nested.msrs.exit_ctls_high |= |
| 4744 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4745 | } else { |
| 4746 | vmx->nested.msrs.entry_ctls_high &= |
| 4747 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4748 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4749 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4753 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4754 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4755 | { |
| 4756 | gva_t gva; |
| 4757 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4758 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4759 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4760 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4761 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4762 | sizeof(*vmpointer), &gva)) { |
| 4763 | *ret = 1; |
| 4764 | return -EINVAL; |
| 4765 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4766 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4767 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4768 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4769 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4770 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | return 0; |
| 4774 | } |
| 4775 | |
| 4776 | /* |
| 4777 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4778 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4779 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4780 | */ |
| 4781 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4782 | { |
| 4783 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4784 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4785 | |
| 4786 | /* |
| 4787 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4788 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4789 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4790 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4791 | */ |
| 4792 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4793 | |
| 4794 | if (!loaded_vmcs->shadow_vmcs) { |
| 4795 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4796 | if (loaded_vmcs->shadow_vmcs) |
| 4797 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4798 | } |
| 4799 | return loaded_vmcs->shadow_vmcs; |
| 4800 | } |
| 4801 | |
| 4802 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4803 | { |
| 4804 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4805 | int r; |
| 4806 | |
| 4807 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4808 | if (r < 0) |
| 4809 | goto out_vmcs02; |
| 4810 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4811 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4812 | if (!vmx->nested.cached_vmcs12) |
| 4813 | goto out_cached_vmcs12; |
| 4814 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4815 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4816 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4817 | goto out_cached_shadow_vmcs12; |
| 4818 | |
| 4819 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4820 | goto out_shadow_vmcs; |
| 4821 | |
| 4822 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4823 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4824 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4825 | |
| 4826 | vmx->nested.vpid02 = allocate_vpid(); |
| 4827 | |
| 4828 | vmx->nested.vmcs02_initialized = false; |
| 4829 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4830 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4831 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4832 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4833 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4834 | } |
| 4835 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4836 | return 0; |
| 4837 | |
| 4838 | out_shadow_vmcs: |
| 4839 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4840 | |
| 4841 | out_cached_shadow_vmcs12: |
| 4842 | kfree(vmx->nested.cached_vmcs12); |
| 4843 | |
| 4844 | out_cached_vmcs12: |
| 4845 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4846 | |
| 4847 | out_vmcs02: |
| 4848 | return -ENOMEM; |
| 4849 | } |
| 4850 | |
| 4851 | /* |
| 4852 | * Emulate the VMXON instruction. |
| 4853 | * Currently, we just remember that VMX is active, and do not save or even |
| 4854 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 4855 | * do not currently need to store anything in that guest-allocated memory |
| 4856 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 4857 | * argument is different from the VMXON pointer (which the spec says they do). |
| 4858 | */ |
| 4859 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4860 | { |
| 4861 | int ret; |
| 4862 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4863 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4864 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4865 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4866 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4867 | |
| 4868 | /* |
| 4869 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4870 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4871 | * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4872 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4873 | * have already been checked by hardware, prior to the VM-exit for |
| 4874 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4875 | * that bit set to 1 in non-root mode. |
| 4876 | */ |
| 4877 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4878 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4879 | return 1; |
| 4880 | } |
| 4881 | |
| 4882 | /* CPL=0 must be checked manually. */ |
| 4883 | if (vmx_get_cpl(vcpu)) { |
| 4884 | kvm_inject_gp(vcpu, 0); |
| 4885 | return 1; |
| 4886 | } |
| 4887 | |
| 4888 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4889 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4890 | |
| 4891 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4892 | != VMXON_NEEDED_FEATURES) { |
| 4893 | kvm_inject_gp(vcpu, 0); |
| 4894 | return 1; |
| 4895 | } |
| 4896 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4897 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4898 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4899 | |
| 4900 | /* |
| 4901 | * SDM 3: 24.11.5 |
| 4902 | * The first 4 bytes of VMXON region contain the supported |
| 4903 | * VMCS revision identifier |
| 4904 | * |
| 4905 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4906 | * which replaces physical address width with 32 |
| 4907 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4908 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4909 | return nested_vmx_failInvalid(vcpu); |
| 4910 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4911 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4912 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4913 | return nested_vmx_failInvalid(vcpu); |
| 4914 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4915 | vmx->nested.vmxon_ptr = vmptr; |
| 4916 | ret = enter_vmx_operation(vcpu); |
| 4917 | if (ret) |
| 4918 | return ret; |
| 4919 | |
| 4920 | return nested_vmx_succeed(vcpu); |
| 4921 | } |
| 4922 | |
| 4923 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4924 | { |
| 4925 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4926 | |
| 4927 | if (vmx->nested.current_vmptr == -1ull) |
| 4928 | return; |
| 4929 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4930 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4931 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4932 | if (enable_shadow_vmcs) { |
| 4933 | /* copy to memory all shadowed fields in case |
| 4934 | they were modified */ |
| 4935 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4936 | vmx_disable_shadow_vmcs(vmx); |
| 4937 | } |
| 4938 | vmx->nested.posted_intr_nv = -1; |
| 4939 | |
| 4940 | /* Flush VMCS12 to guest memory */ |
| 4941 | kvm_vcpu_write_guest_page(vcpu, |
| 4942 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4943 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4944 | |
| 4945 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4946 | |
| 4947 | vmx->nested.current_vmptr = -1ull; |
| 4948 | } |
| 4949 | |
| 4950 | /* Emulate the VMXOFF instruction */ |
| 4951 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4952 | { |
| 4953 | if (!nested_vmx_check_permission(vcpu)) |
| 4954 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4955 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4956 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4957 | |
| 4958 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 4959 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4960 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4961 | return nested_vmx_succeed(vcpu); |
| 4962 | } |
| 4963 | |
| 4964 | /* Emulate the VMCLEAR instruction */ |
| 4965 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 4966 | { |
| 4967 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4968 | u32 zero = 0; |
| 4969 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4970 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4971 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4972 | |
| 4973 | if (!nested_vmx_check_permission(vcpu)) |
| 4974 | return 1; |
| 4975 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4976 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 4977 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4978 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4979 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4980 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4981 | |
| 4982 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4983 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4984 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4985 | /* |
| 4986 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 4987 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 4988 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 4989 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 4990 | * have to be the currently active EVMCS on the calling CPU and there's |
| 4991 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 4992 | * state. It is possible that the area will stay mapped as |
| 4993 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 4994 | */ |
| 4995 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 4996 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4997 | if (vmptr == vmx->nested.current_vmptr) |
| 4998 | nested_release_vmcs12(vcpu); |
| 4999 | |
| 5000 | kvm_vcpu_write_guest(vcpu, |
| 5001 | vmptr + offsetof(struct vmcs12, |
| 5002 | launch_state), |
| 5003 | &zero, sizeof(zero)); |
| 5004 | } |
| 5005 | |
| 5006 | return nested_vmx_succeed(vcpu); |
| 5007 | } |
| 5008 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5009 | /* Emulate the VMLAUNCH instruction */ |
| 5010 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5011 | { |
| 5012 | return nested_vmx_run(vcpu, true); |
| 5013 | } |
| 5014 | |
| 5015 | /* Emulate the VMRESUME instruction */ |
| 5016 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5017 | { |
| 5018 | |
| 5019 | return nested_vmx_run(vcpu, false); |
| 5020 | } |
| 5021 | |
| 5022 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5023 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5024 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5025 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5026 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5027 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5028 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5029 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5030 | unsigned long field; |
| 5031 | u64 value; |
| 5032 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5033 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5034 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5035 | |
| 5036 | if (!nested_vmx_check_permission(vcpu)) |
| 5037 | return 1; |
| 5038 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5039 | /* |
| 5040 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5041 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5042 | */ |
| 5043 | if (vmx->nested.current_vmptr == -1ull || |
| 5044 | (is_guest_mode(vcpu) && |
| 5045 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5046 | return nested_vmx_failInvalid(vcpu); |
| 5047 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5048 | /* Decode instruction info and find the field to read */ |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5049 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5050 | |
| 5051 | offset = vmcs_field_to_offset(field); |
| 5052 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5053 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5054 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5055 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5056 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5057 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5058 | /* Read the field, zero-extended to a u64 value */ |
| 5059 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5060 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5061 | /* |
| 5062 | * Now copy part of this value to register or memory, as requested. |
| 5063 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5064 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5065 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5066 | if (instr_info & BIT(10)) { |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5067 | kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5068 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5069 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5070 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5071 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5072 | return 1; |
| 5073 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5074 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5075 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5076 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | return nested_vmx_succeed(vcpu); |
| 5080 | } |
| 5081 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5082 | static bool is_shadow_field_rw(unsigned long field) |
| 5083 | { |
| 5084 | switch (field) { |
| 5085 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5086 | #include "vmcs_shadow_fields.h" |
| 5087 | return true; |
| 5088 | default: |
| 5089 | break; |
| 5090 | } |
| 5091 | return false; |
| 5092 | } |
| 5093 | |
| 5094 | static bool is_shadow_field_ro(unsigned long field) |
| 5095 | { |
| 5096 | switch (field) { |
| 5097 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5098 | #include "vmcs_shadow_fields.h" |
| 5099 | return true; |
| 5100 | default: |
| 5101 | break; |
| 5102 | } |
| 5103 | return false; |
| 5104 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5105 | |
| 5106 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5107 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5108 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5109 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5110 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5111 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5112 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5113 | struct x86_exception e; |
| 5114 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5115 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5116 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5117 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5118 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5119 | /* |
| 5120 | * 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] | 5121 | * mode, and eventually we need to write that into a field of several |
| 5122 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5123 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5124 | * bits into the vmcs12 field. |
| 5125 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5126 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5127 | |
| 5128 | if (!nested_vmx_check_permission(vcpu)) |
| 5129 | return 1; |
| 5130 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5131 | /* |
| 5132 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5133 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5134 | */ |
| 5135 | if (vmx->nested.current_vmptr == -1ull || |
| 5136 | (is_guest_mode(vcpu) && |
| 5137 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5138 | return nested_vmx_failInvalid(vcpu); |
| 5139 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5140 | if (instr_info & BIT(10)) |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5141 | value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5142 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5143 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5144 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5145 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5146 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5147 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5148 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5149 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5150 | } |
| 5151 | |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5152 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5153 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5154 | offset = vmcs_field_to_offset(field); |
| 5155 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5156 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5157 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5158 | /* |
| 5159 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5160 | * VMCS," then the "read-only" fields are actually read/write. |
| 5161 | */ |
| 5162 | if (vmcs_field_readonly(field) && |
| 5163 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5164 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5165 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5166 | /* |
| 5167 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5168 | * vmcs12, else we may crush a field or consume a stale value. |
| 5169 | */ |
| 5170 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5171 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5172 | |
| 5173 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5174 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5175 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5176 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5177 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5178 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5179 | * 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] | 5180 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5181 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5182 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5183 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5184 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5185 | |
| 5186 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5187 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5188 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5189 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5190 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5191 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5192 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5193 | /* |
| 5194 | * L1 can read these fields without exiting, ensure the |
| 5195 | * shadow VMCS is up-to-date. |
| 5196 | */ |
| 5197 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5198 | preempt_disable(); |
| 5199 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5200 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5201 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5202 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5203 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5204 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5205 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5206 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5207 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | return nested_vmx_succeed(vcpu); |
| 5211 | } |
| 5212 | |
| 5213 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5214 | { |
| 5215 | vmx->nested.current_vmptr = vmptr; |
| 5216 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5217 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5218 | vmcs_write64(VMCS_LINK_POINTER, |
| 5219 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5220 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5221 | } |
| 5222 | vmx->nested.dirty_vmcs12 = true; |
| 5223 | } |
| 5224 | |
| 5225 | /* Emulate the VMPTRLD instruction */ |
| 5226 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5227 | { |
| 5228 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5229 | gpa_t vmptr; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5230 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5231 | |
| 5232 | if (!nested_vmx_check_permission(vcpu)) |
| 5233 | return 1; |
| 5234 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5235 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5236 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5237 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5238 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5239 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5240 | |
| 5241 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5242 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5243 | |
| 5244 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
| 5245 | if (vmx->nested.hv_evmcs) |
| 5246 | return 1; |
| 5247 | |
| 5248 | if (vmx->nested.current_vmptr != vmptr) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5249 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5250 | struct vmcs12 *new_vmcs12; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5251 | |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5252 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5253 | /* |
| 5254 | * Reads from an unbacked page return all 1s, |
| 5255 | * which means that the 32 bits located at the |
| 5256 | * given physical address won't match the required |
| 5257 | * VMCS12_REVISION identifier. |
| 5258 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5259 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5260 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5261 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5262 | |
| 5263 | new_vmcs12 = map.hva; |
| 5264 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5265 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 5266 | (new_vmcs12->hdr.shadow_vmcs && |
| 5267 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5268 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5269 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5270 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5271 | } |
| 5272 | |
| 5273 | nested_release_vmcs12(vcpu); |
| 5274 | |
| 5275 | /* |
| 5276 | * Load VMCS12 from guest memory since it is not already |
| 5277 | * cached. |
| 5278 | */ |
| 5279 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5280 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5281 | |
| 5282 | set_current_vmptr(vmx, vmptr); |
| 5283 | } |
| 5284 | |
| 5285 | return nested_vmx_succeed(vcpu); |
| 5286 | } |
| 5287 | |
| 5288 | /* Emulate the VMPTRST instruction */ |
| 5289 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5290 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5291 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5292 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5293 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5294 | struct x86_exception e; |
| 5295 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5296 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5297 | |
| 5298 | if (!nested_vmx_check_permission(vcpu)) |
| 5299 | return 1; |
| 5300 | |
| 5301 | if (unlikely(to_vmx(vcpu)->nested.hv_evmcs)) |
| 5302 | return 1; |
| 5303 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5304 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5305 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5306 | return 1; |
| 5307 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5308 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5309 | sizeof(gpa_t), &e); |
| 5310 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5311 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5312 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5313 | return nested_vmx_succeed(vcpu); |
| 5314 | } |
| 5315 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5316 | #define EPTP_PA_MASK GENMASK_ULL(51, 12) |
| 5317 | |
| 5318 | static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) |
| 5319 | { |
| 5320 | return VALID_PAGE(root_hpa) && |
| 5321 | ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); |
| 5322 | } |
| 5323 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5324 | /* Emulate the INVEPT instruction */ |
| 5325 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5326 | { |
| 5327 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5328 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5329 | unsigned long type, roots_to_free; |
| 5330 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5331 | gva_t gva; |
| 5332 | struct x86_exception e; |
| 5333 | struct { |
| 5334 | u64 eptp, gpa; |
| 5335 | } operand; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5336 | int i, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5337 | |
| 5338 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5339 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5340 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5341 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5342 | return 1; |
| 5343 | } |
| 5344 | |
| 5345 | if (!nested_vmx_check_permission(vcpu)) |
| 5346 | return 1; |
| 5347 | |
| 5348 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5349 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5350 | |
| 5351 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5352 | |
| 5353 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5354 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5355 | |
| 5356 | /* According to the Intel VMX instruction reference, the memory |
| 5357 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5358 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5359 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5360 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5361 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5362 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5363 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5364 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5365 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5366 | /* |
| 5367 | * Nested EPT roots are always held through guest_mmu, |
| 5368 | * not root_mmu. |
| 5369 | */ |
| 5370 | mmu = &vcpu->arch.guest_mmu; |
| 5371 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5372 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5373 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5374 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5375 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5376 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5377 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5378 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5379 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5380 | operand.eptp)) |
| 5381 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5382 | |
| 5383 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5384 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5385 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5386 | operand.eptp)) |
| 5387 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5388 | } |
| 5389 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5390 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5391 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5392 | break; |
| 5393 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5394 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5395 | break; |
| 5396 | } |
| 5397 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5398 | if (roots_to_free) |
| 5399 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5400 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5401 | return nested_vmx_succeed(vcpu); |
| 5402 | } |
| 5403 | |
| 5404 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5405 | { |
| 5406 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5407 | u32 vmx_instruction_info; |
| 5408 | unsigned long type, types; |
| 5409 | gva_t gva; |
| 5410 | struct x86_exception e; |
| 5411 | struct { |
| 5412 | u64 vpid; |
| 5413 | u64 gla; |
| 5414 | } operand; |
| 5415 | u16 vpid02; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5416 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5417 | |
| 5418 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5419 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5420 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5421 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5422 | return 1; |
| 5423 | } |
| 5424 | |
| 5425 | if (!nested_vmx_check_permission(vcpu)) |
| 5426 | return 1; |
| 5427 | |
| 5428 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5429 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5430 | |
| 5431 | types = (vmx->nested.msrs.vpid_caps & |
| 5432 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5433 | |
| 5434 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5435 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5436 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5437 | |
| 5438 | /* according to the intel vmx instruction reference, the memory |
| 5439 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5440 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5441 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5442 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5443 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5444 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5445 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5446 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5447 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5448 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5449 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5450 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5451 | |
| 5452 | vpid02 = nested_get_vpid02(vcpu); |
| 5453 | switch (type) { |
| 5454 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5455 | if (!operand.vpid || |
| 5456 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5457 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5458 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5459 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5460 | break; |
| 5461 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5462 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5463 | if (!operand.vpid) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5464 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5465 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5466 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5467 | break; |
| 5468 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5469 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5470 | break; |
| 5471 | default: |
| 5472 | WARN_ON_ONCE(1); |
| 5473 | return kvm_skip_emulated_instruction(vcpu); |
| 5474 | } |
| 5475 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5476 | /* |
| 5477 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
| 5478 | * linear mappings for L2 (tagged with L2's VPID). Free all roots as |
| 5479 | * VPIDs are not tracked in the MMU role. |
| 5480 | * |
| 5481 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5482 | * an MMU when EPT is disabled. |
| 5483 | * |
| 5484 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5485 | */ |
| 5486 | if (!enable_ept) |
| 5487 | kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, |
| 5488 | KVM_MMU_ROOTS_ALL); |
| 5489 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5490 | return nested_vmx_succeed(vcpu); |
| 5491 | } |
| 5492 | |
| 5493 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5494 | struct vmcs12 *vmcs12) |
| 5495 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5496 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5497 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5498 | bool accessed_dirty; |
| 5499 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 5500 | |
| 5501 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 5502 | !nested_cpu_has_ept(vmcs12)) |
| 5503 | return 1; |
| 5504 | |
| 5505 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5506 | return 1; |
| 5507 | |
| 5508 | |
| 5509 | 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] | 5510 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5511 | return 1; |
| 5512 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5513 | accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5514 | |
| 5515 | /* |
| 5516 | * If the (L2) guest does a vmfunc to the currently |
| 5517 | * active ept pointer, we don't have to do anything else |
| 5518 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5519 | if (vmcs12->ept_pointer != new_eptp) { |
| 5520 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5521 | return 1; |
| 5522 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5523 | mmu->ept_ad = accessed_dirty; |
| 5524 | mmu->mmu_role.base.ad_disabled = !accessed_dirty; |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5525 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5526 | |
| 5527 | kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | return 0; |
| 5531 | } |
| 5532 | |
| 5533 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5534 | { |
| 5535 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5536 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5537 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5538 | |
| 5539 | /* |
| 5540 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5541 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5542 | * didn't by injecting #UD. |
| 5543 | */ |
| 5544 | if (!is_guest_mode(vcpu)) { |
| 5545 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5546 | return 1; |
| 5547 | } |
| 5548 | |
| 5549 | vmcs12 = get_vmcs12(vcpu); |
| 5550 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 5551 | goto fail; |
| 5552 | |
| 5553 | switch (function) { |
| 5554 | case 0: |
| 5555 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5556 | goto fail; |
| 5557 | break; |
| 5558 | default: |
| 5559 | goto fail; |
| 5560 | } |
| 5561 | return kvm_skip_emulated_instruction(vcpu); |
| 5562 | |
| 5563 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5564 | /* |
| 5565 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5566 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5567 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5568 | */ |
| 5569 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5570 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5571 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5572 | return 1; |
| 5573 | } |
| 5574 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5575 | /* |
| 5576 | * Return true if an IO instruction with the specified port and size should cause |
| 5577 | * a VM-exit into L1. |
| 5578 | */ |
| 5579 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5580 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5581 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5582 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5583 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5584 | u8 b; |
| 5585 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5586 | last_bitmap = (gpa_t)-1; |
| 5587 | b = -1; |
| 5588 | |
| 5589 | while (size > 0) { |
| 5590 | if (port < 0x8000) |
| 5591 | bitmap = vmcs12->io_bitmap_a; |
| 5592 | else if (port < 0x10000) |
| 5593 | bitmap = vmcs12->io_bitmap_b; |
| 5594 | else |
| 5595 | return true; |
| 5596 | bitmap += (port & 0x7fff) / 8; |
| 5597 | |
| 5598 | if (last_bitmap != bitmap) |
| 5599 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5600 | return true; |
| 5601 | if (b & (1 << (port & 7))) |
| 5602 | return true; |
| 5603 | |
| 5604 | port++; |
| 5605 | size--; |
| 5606 | last_bitmap = bitmap; |
| 5607 | } |
| 5608 | |
| 5609 | return false; |
| 5610 | } |
| 5611 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5612 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5613 | struct vmcs12 *vmcs12) |
| 5614 | { |
| 5615 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5616 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5617 | int size; |
| 5618 | |
| 5619 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5620 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5621 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5622 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5623 | |
| 5624 | port = exit_qualification >> 16; |
| 5625 | size = (exit_qualification & 7) + 1; |
| 5626 | |
| 5627 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5628 | } |
| 5629 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5630 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5631 | * 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] | 5632 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5633 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5634 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5635 | */ |
| 5636 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5637 | struct vmcs12 *vmcs12, |
| 5638 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5639 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5640 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5641 | gpa_t bitmap; |
| 5642 | |
| 5643 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5644 | return true; |
| 5645 | |
| 5646 | /* |
| 5647 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5648 | * for the four combinations of read/write and low/high MSR numbers. |
| 5649 | * First we need to figure out which of the four to use: |
| 5650 | */ |
| 5651 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5652 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5653 | bitmap += 2048; |
| 5654 | if (msr_index >= 0xc0000000) { |
| 5655 | msr_index -= 0xc0000000; |
| 5656 | bitmap += 1024; |
| 5657 | } |
| 5658 | |
| 5659 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5660 | if (msr_index < 1024*8) { |
| 5661 | unsigned char b; |
| 5662 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5663 | return true; |
| 5664 | return 1 & (b >> (msr_index & 7)); |
| 5665 | } else |
| 5666 | return true; /* let L1 handle the wrong parameter */ |
| 5667 | } |
| 5668 | |
| 5669 | /* |
| 5670 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5671 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5672 | * intercept (via guest_host_mask etc.) the current event. |
| 5673 | */ |
| 5674 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5675 | struct vmcs12 *vmcs12) |
| 5676 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5677 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5678 | int cr = exit_qualification & 15; |
| 5679 | int reg; |
| 5680 | unsigned long val; |
| 5681 | |
| 5682 | switch ((exit_qualification >> 4) & 3) { |
| 5683 | case 0: /* mov to cr */ |
| 5684 | reg = (exit_qualification >> 8) & 15; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5685 | val = kvm_register_read(vcpu, reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5686 | switch (cr) { |
| 5687 | case 0: |
| 5688 | if (vmcs12->cr0_guest_host_mask & |
| 5689 | (val ^ vmcs12->cr0_read_shadow)) |
| 5690 | return true; |
| 5691 | break; |
| 5692 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5693 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5694 | return true; |
| 5695 | break; |
| 5696 | case 4: |
| 5697 | if (vmcs12->cr4_guest_host_mask & |
| 5698 | (vmcs12->cr4_read_shadow ^ val)) |
| 5699 | return true; |
| 5700 | break; |
| 5701 | case 8: |
| 5702 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5703 | return true; |
| 5704 | break; |
| 5705 | } |
| 5706 | break; |
| 5707 | case 2: /* clts */ |
| 5708 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5709 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5710 | return true; |
| 5711 | break; |
| 5712 | case 1: /* mov from cr */ |
| 5713 | switch (cr) { |
| 5714 | case 3: |
| 5715 | if (vmcs12->cpu_based_vm_exec_control & |
| 5716 | CPU_BASED_CR3_STORE_EXITING) |
| 5717 | return true; |
| 5718 | break; |
| 5719 | case 8: |
| 5720 | if (vmcs12->cpu_based_vm_exec_control & |
| 5721 | CPU_BASED_CR8_STORE_EXITING) |
| 5722 | return true; |
| 5723 | break; |
| 5724 | } |
| 5725 | break; |
| 5726 | case 3: /* lmsw */ |
| 5727 | /* |
| 5728 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5729 | * cr0. Other attempted changes are ignored, with no exit. |
| 5730 | */ |
| 5731 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5732 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5733 | (val ^ vmcs12->cr0_read_shadow)) |
| 5734 | return true; |
| 5735 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5736 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5737 | (val & 0x1)) |
| 5738 | return true; |
| 5739 | break; |
| 5740 | } |
| 5741 | return false; |
| 5742 | } |
| 5743 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5744 | static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, |
| 5745 | struct vmcs12 *vmcs12) |
| 5746 | { |
| 5747 | u32 encls_leaf; |
| 5748 | |
| 5749 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) || |
| 5750 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) |
| 5751 | return false; |
| 5752 | |
| 5753 | encls_leaf = kvm_rax_read(vcpu); |
| 5754 | if (encls_leaf > 62) |
| 5755 | encls_leaf = 63; |
| 5756 | return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); |
| 5757 | } |
| 5758 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5759 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5760 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5761 | { |
| 5762 | u32 vmx_instruction_info; |
| 5763 | unsigned long field; |
| 5764 | u8 b; |
| 5765 | |
| 5766 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5767 | return true; |
| 5768 | |
| 5769 | /* Decode instruction info and find the field to access */ |
| 5770 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5771 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5772 | |
| 5773 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5774 | if (field >> 15) |
| 5775 | return true; |
| 5776 | |
| 5777 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5778 | return true; |
| 5779 | |
| 5780 | return 1 & (b >> (field & 7)); |
| 5781 | } |
| 5782 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5783 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5784 | { |
| 5785 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5786 | |
| 5787 | if (nested_cpu_has_mtf(vmcs12)) |
| 5788 | return true; |
| 5789 | |
| 5790 | /* |
| 5791 | * An MTF VM-exit may be injected into the guest by setting the |
| 5792 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5793 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5794 | * control. |
| 5795 | */ |
| 5796 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5797 | | INTR_TYPE_OTHER_EVENT); |
| 5798 | } |
| 5799 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5800 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5801 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5802 | * 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] | 5803 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5804 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5805 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5806 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5807 | u32 intr_info; |
| 5808 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5809 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5810 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5811 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5812 | if (is_nmi(intr_info)) |
| 5813 | return true; |
| 5814 | else if (is_page_fault(intr_info)) |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 5815 | return vcpu->arch.apf.host_apf_flags || !enable_ept; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5816 | else if (is_debug(intr_info) && |
| 5817 | vcpu->guest_debug & |
| 5818 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5819 | return true; |
| 5820 | else if (is_breakpoint(intr_info) && |
| 5821 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5822 | return true; |
| 5823 | return false; |
| 5824 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5825 | return true; |
| 5826 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5827 | return true; |
| 5828 | case EXIT_REASON_EPT_VIOLATION: |
| 5829 | /* |
| 5830 | * L0 always deals with the EPT violation. If nested EPT is |
| 5831 | * used, and the nested mmu code discovers that the address is |
| 5832 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5833 | * will be injected with nested_ept_inject_page_fault() |
| 5834 | */ |
| 5835 | return true; |
| 5836 | case EXIT_REASON_EPT_MISCONFIG: |
| 5837 | /* |
| 5838 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5839 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5840 | * (EPT on EPT). So any problems with the structure of the |
| 5841 | * table is L0's fault. |
| 5842 | */ |
| 5843 | return true; |
| 5844 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5845 | return true; |
| 5846 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5847 | /* |
| 5848 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5849 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5850 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5851 | return true; |
| 5852 | case EXIT_REASON_VMFUNC: |
| 5853 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5854 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5855 | default: |
| 5856 | break; |
| 5857 | } |
| 5858 | return false; |
| 5859 | } |
| 5860 | |
| 5861 | /* |
| 5862 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5863 | * is_guest_mode (L2). |
| 5864 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5865 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5866 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5867 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5868 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5869 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5870 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5871 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5872 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5873 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5874 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5875 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5876 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5877 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5878 | return vmcs12->exception_bitmap & |
| 5879 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5880 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5881 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5882 | case EXIT_REASON_TRIPLE_FAULT: |
| 5883 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5884 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5885 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5886 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5887 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5888 | case EXIT_REASON_TASK_SWITCH: |
| 5889 | return true; |
| 5890 | case EXIT_REASON_CPUID: |
| 5891 | return true; |
| 5892 | case EXIT_REASON_HLT: |
| 5893 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5894 | case EXIT_REASON_INVD: |
| 5895 | return true; |
| 5896 | case EXIT_REASON_INVLPG: |
| 5897 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5898 | case EXIT_REASON_RDPMC: |
| 5899 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5900 | case EXIT_REASON_RDRAND: |
| 5901 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5902 | case EXIT_REASON_RDSEED: |
| 5903 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5904 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5905 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5906 | case EXIT_REASON_VMREAD: |
| 5907 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5908 | vmcs12->vmread_bitmap); |
| 5909 | case EXIT_REASON_VMWRITE: |
| 5910 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5911 | vmcs12->vmwrite_bitmap); |
| 5912 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5913 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5914 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5915 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5916 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5917 | /* |
| 5918 | * VMX instructions trap unconditionally. This allows L1 to |
| 5919 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5920 | */ |
| 5921 | return true; |
| 5922 | case EXIT_REASON_CR_ACCESS: |
| 5923 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5924 | case EXIT_REASON_DR_ACCESS: |
| 5925 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5926 | case EXIT_REASON_IO_INSTRUCTION: |
| 5927 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5928 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5929 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5930 | case EXIT_REASON_MSR_READ: |
| 5931 | case EXIT_REASON_MSR_WRITE: |
| 5932 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5933 | case EXIT_REASON_INVALID_STATE: |
| 5934 | return true; |
| 5935 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5936 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5937 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5938 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5939 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5940 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5941 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5942 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5943 | nested_cpu_has2(vmcs12, |
| 5944 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5945 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5946 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5947 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5948 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 5949 | case EXIT_REASON_APIC_ACCESS: |
| 5950 | case EXIT_REASON_APIC_WRITE: |
| 5951 | case EXIT_REASON_EOI_INDUCED: |
| 5952 | /* |
| 5953 | * The controls for "virtualize APIC accesses," "APIC- |
| 5954 | * register virtualization," and "virtual-interrupt |
| 5955 | * delivery" only come from vmcs12. |
| 5956 | */ |
| 5957 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5958 | case EXIT_REASON_INVPCID: |
| 5959 | return |
| 5960 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 5961 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5962 | case EXIT_REASON_WBINVD: |
| 5963 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5964 | case EXIT_REASON_XSETBV: |
| 5965 | return true; |
| 5966 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 5967 | /* |
| 5968 | * This should never happen, since it is not possible to |
| 5969 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 5970 | * If if it were, XSS would have to be checked against |
| 5971 | * the XSS exit bitmap in vmcs12. |
| 5972 | */ |
| 5973 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 5974 | case EXIT_REASON_UMWAIT: |
| 5975 | case EXIT_REASON_TPAUSE: |
| 5976 | return nested_cpu_has2(vmcs12, |
| 5977 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5978 | case EXIT_REASON_ENCLS: |
| 5979 | return nested_vmx_exit_handled_encls(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5980 | default: |
| 5981 | return true; |
| 5982 | } |
| 5983 | } |
| 5984 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5985 | /* |
| 5986 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 5987 | * reflected into L1. |
| 5988 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 5989 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5990 | { |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 5991 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5992 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 5993 | unsigned long exit_qual; |
| 5994 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 5995 | |
| 5996 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 5997 | |
| 5998 | /* |
| 5999 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 6000 | * has already loaded L2's state. |
| 6001 | */ |
| 6002 | if (unlikely(vmx->fail)) { |
| 6003 | trace_kvm_nested_vmenter_failed( |
| 6004 | "hardware VM-instruction error: ", |
| 6005 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 6006 | exit_intr_info = 0; |
| 6007 | exit_qual = 0; |
| 6008 | goto reflect_vmexit; |
| 6009 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6010 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6011 | trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 6012 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6013 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 6014 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 6015 | return false; |
| 6016 | |
| 6017 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 6018 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6019 | return false; |
| 6020 | |
| 6021 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 6022 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 6023 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 6024 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 6025 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6026 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6027 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 6028 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6029 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6030 | |
| 6031 | vmcs12->vm_exit_intr_error_code = |
| 6032 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6033 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6034 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6035 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6036 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6037 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6038 | return true; |
| 6039 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6040 | |
| 6041 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 6042 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6043 | u32 user_data_size) |
| 6044 | { |
| 6045 | struct vcpu_vmx *vmx; |
| 6046 | struct vmcs12 *vmcs12; |
| 6047 | struct kvm_nested_state kvm_state = { |
| 6048 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6049 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6050 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6051 | .hdr.vmx.flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6052 | .hdr.vmx.vmxon_pa = -1ull, |
| 6053 | .hdr.vmx.vmcs12_pa = -1ull, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6054 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6055 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6056 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6057 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6058 | |
| 6059 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6060 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6061 | |
| 6062 | vmx = to_vmx(vcpu); |
| 6063 | vmcs12 = get_vmcs12(vcpu); |
| 6064 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6065 | if (nested_vmx_allowed(vcpu) && |
| 6066 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6067 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6068 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6069 | |
| 6070 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6071 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6072 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6073 | if (vmx->nested.hv_evmcs) |
| 6074 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6075 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6076 | if (is_guest_mode(vcpu) && |
| 6077 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6078 | vmcs12->vmcs_link_pointer != -1ull) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6079 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6080 | } |
| 6081 | |
| 6082 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6083 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6084 | |
| 6085 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6086 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6087 | |
| 6088 | if (is_guest_mode(vcpu)) { |
| 6089 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6090 | |
| 6091 | if (vmx->nested.nested_run_pending) |
| 6092 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6093 | |
| 6094 | if (vmx->nested.mtf_pending) |
| 6095 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6096 | |
| 6097 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6098 | vmx->nested.has_preemption_timer_deadline) { |
| 6099 | kvm_state.hdr.vmx.flags |= |
| 6100 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6101 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6102 | vmx->nested.preemption_timer_deadline; |
| 6103 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6104 | } |
| 6105 | } |
| 6106 | |
| 6107 | if (user_data_size < kvm_state.size) |
| 6108 | goto out; |
| 6109 | |
| 6110 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6111 | return -EFAULT; |
| 6112 | |
| 6113 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6114 | goto out; |
| 6115 | |
| 6116 | /* |
| 6117 | * When running L2, the authoritative vmcs12 state is in the |
| 6118 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6119 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6120 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6121 | * vmcs12 state is in the vmcs12 already. |
| 6122 | */ |
| 6123 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6124 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6125 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6126 | } else { |
| 6127 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6128 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
| 6129 | if (vmx->nested.hv_evmcs) |
| 6130 | copy_enlightened_to_vmcs12(vmx); |
| 6131 | else if (enable_shadow_vmcs) |
| 6132 | copy_shadow_to_vmcs12(vmx); |
| 6133 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6134 | } |
| 6135 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6136 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6137 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6138 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6139 | /* |
| 6140 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6141 | * of the struct. |
| 6142 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6143 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6144 | return -EFAULT; |
| 6145 | |
| 6146 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6147 | vmcs12->vmcs_link_pointer != -1ull) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6148 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6149 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6150 | return -EFAULT; |
| 6151 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6152 | out: |
| 6153 | return kvm_state.size; |
| 6154 | } |
| 6155 | |
| 6156 | /* |
| 6157 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6158 | */ |
| 6159 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6160 | { |
| 6161 | if (is_guest_mode(vcpu)) { |
| 6162 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6163 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6164 | } |
| 6165 | free_nested(vcpu); |
| 6166 | } |
| 6167 | |
| 6168 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6169 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6170 | struct kvm_nested_state *kvm_state) |
| 6171 | { |
| 6172 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6173 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6174 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6175 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6176 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6177 | int ret; |
| 6178 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6179 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6180 | return -EINVAL; |
| 6181 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6182 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) { |
| 6183 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6184 | return -EINVAL; |
| 6185 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6186 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6187 | return -EINVAL; |
| 6188 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6189 | /* |
| 6190 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6191 | * enable eVMCS capability on vCPU. However, since then |
| 6192 | * code was changed such that flag signals vmcs12 should |
| 6193 | * be copied into eVMCS in guest memory. |
| 6194 | * |
| 6195 | * To preserve backwards compatability, allow user |
| 6196 | * to set this flag even when there is no VMXON region. |
| 6197 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6198 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6199 | return -EINVAL; |
| 6200 | } else { |
| 6201 | if (!nested_vmx_allowed(vcpu)) |
| 6202 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6203 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6204 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6205 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6206 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6207 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6208 | 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] | 6209 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6210 | return -EINVAL; |
| 6211 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6212 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6213 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6214 | return -EINVAL; |
| 6215 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6216 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6217 | return -EINVAL; |
| 6218 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6219 | /* |
| 6220 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6221 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6222 | * must be zero. |
| 6223 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6224 | if (is_smm(vcpu) ? |
| 6225 | (kvm_state->flags & |
| 6226 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6227 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6228 | return -EINVAL; |
| 6229 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6230 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6231 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6232 | return -EINVAL; |
| 6233 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6234 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6235 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6236 | return -EINVAL; |
| 6237 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6238 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6239 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6240 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6241 | return 0; |
| 6242 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6243 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6244 | ret = enter_vmx_operation(vcpu); |
| 6245 | if (ret) |
| 6246 | return ret; |
| 6247 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6248 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6249 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6250 | /* See vmx_has_valid_vmcs12. */ |
| 6251 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6252 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
| 6253 | (kvm_state->hdr.vmx.vmcs12_pa != -1ull)) |
| 6254 | return -EINVAL; |
| 6255 | else |
| 6256 | return 0; |
| 6257 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6258 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6259 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) { |
| 6260 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6261 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6262 | return -EINVAL; |
| 6263 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6264 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6265 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6266 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6267 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6268 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6269 | * restored yet. EVMCS will be mapped from |
| 6270 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6271 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6272 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6273 | } else { |
| 6274 | return -EINVAL; |
| 6275 | } |
| 6276 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6277 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6278 | vmx->nested.smm.vmxon = true; |
| 6279 | vmx->nested.vmxon = false; |
| 6280 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6281 | 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] | 6282 | vmx->nested.smm.guest_mode = true; |
| 6283 | } |
| 6284 | |
| 6285 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6286 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6287 | return -EFAULT; |
| 6288 | |
| 6289 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6290 | return -EINVAL; |
| 6291 | |
| 6292 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6293 | return 0; |
| 6294 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6295 | vmx->nested.nested_run_pending = |
| 6296 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6297 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6298 | vmx->nested.mtf_pending = |
| 6299 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6300 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6301 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6302 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6303 | vmcs12->vmcs_link_pointer != -1ull) { |
| 6304 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6305 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6306 | if (kvm_state->size < |
| 6307 | sizeof(*kvm_state) + |
| 6308 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6309 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6310 | |
| 6311 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6312 | user_vmx_nested_state->shadow_vmcs12, |
| 6313 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6314 | ret = -EFAULT; |
| 6315 | goto error_guest_mode; |
| 6316 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6317 | |
| 6318 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6319 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6320 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6321 | } |
| 6322 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6323 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6324 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6325 | vmx->nested.has_preemption_timer_deadline = true; |
| 6326 | vmx->nested.preemption_timer_deadline = |
| 6327 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6328 | } |
| 6329 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6330 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6331 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6332 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6333 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6334 | |
| 6335 | vmx->nested.dirty_vmcs12 = true; |
| 6336 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6337 | if (ret) |
| 6338 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6339 | |
| 6340 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6341 | |
| 6342 | error_guest_mode: |
| 6343 | vmx->nested.nested_run_pending = 0; |
| 6344 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6345 | } |
| 6346 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6347 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6348 | { |
| 6349 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6350 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6351 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | /* |
| 6356 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6357 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6358 | * The same values should also be used to verify that vmcs12 control fields are |
| 6359 | * valid during nested entry from L1 to L2. |
| 6360 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6361 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6362 | * bit in the high half is on if the corresponding bit in the control field |
| 6363 | * may be on. See also vmx_control_verify(). |
| 6364 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6365 | 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] | 6366 | { |
| 6367 | /* |
| 6368 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6369 | * the control fields which may be 1) should be initialized by the |
| 6370 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6371 | * can be supported) and the list of features we want to expose - |
| 6372 | * because they are known to be properly supported in our code. |
| 6373 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6374 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6375 | * reason is that if one of these bits is necessary, it will appear |
| 6376 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6377 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6378 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6379 | * These rules have exceptions below. |
| 6380 | */ |
| 6381 | |
| 6382 | /* pin-based controls */ |
| 6383 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6384 | msrs->pinbased_ctls_low, |
| 6385 | msrs->pinbased_ctls_high); |
| 6386 | msrs->pinbased_ctls_low |= |
| 6387 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6388 | msrs->pinbased_ctls_high &= |
| 6389 | PIN_BASED_EXT_INTR_MASK | |
| 6390 | PIN_BASED_NMI_EXITING | |
| 6391 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6392 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6393 | msrs->pinbased_ctls_high |= |
| 6394 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6395 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6396 | |
| 6397 | /* exit controls */ |
| 6398 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6399 | msrs->exit_ctls_low, |
| 6400 | msrs->exit_ctls_high); |
| 6401 | msrs->exit_ctls_low = |
| 6402 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6403 | |
| 6404 | msrs->exit_ctls_high &= |
| 6405 | #ifdef CONFIG_X86_64 |
| 6406 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6407 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6408 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6409 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6410 | msrs->exit_ctls_high |= |
| 6411 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6412 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6413 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6414 | |
| 6415 | /* We support free control of debug control saving. */ |
| 6416 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6417 | |
| 6418 | /* entry controls */ |
| 6419 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6420 | msrs->entry_ctls_low, |
| 6421 | msrs->entry_ctls_high); |
| 6422 | msrs->entry_ctls_low = |
| 6423 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6424 | msrs->entry_ctls_high &= |
| 6425 | #ifdef CONFIG_X86_64 |
| 6426 | VM_ENTRY_IA32E_MODE | |
| 6427 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6428 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6429 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6430 | msrs->entry_ctls_high |= |
| 6431 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6432 | |
| 6433 | /* We support free control of debug control loading. */ |
| 6434 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6435 | |
| 6436 | /* cpu-based controls */ |
| 6437 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6438 | msrs->procbased_ctls_low, |
| 6439 | msrs->procbased_ctls_high); |
| 6440 | msrs->procbased_ctls_low = |
| 6441 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6442 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6443 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6444 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6445 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6446 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6447 | CPU_BASED_CR3_STORE_EXITING | |
| 6448 | #ifdef CONFIG_X86_64 |
| 6449 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6450 | #endif |
| 6451 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6452 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6453 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6454 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6455 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6456 | /* |
| 6457 | * We can allow some features even when not supported by the |
| 6458 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6459 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6460 | * without MSR bitmaps. |
| 6461 | */ |
| 6462 | msrs->procbased_ctls_high |= |
| 6463 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6464 | CPU_BASED_USE_MSR_BITMAPS; |
| 6465 | |
| 6466 | /* We support free control of CR3 access interception. */ |
| 6467 | msrs->procbased_ctls_low &= |
| 6468 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6469 | |
| 6470 | /* |
| 6471 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6472 | * depend on CPUID bits, they are added later by |
| 6473 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6474 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6475 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6476 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6477 | msrs->secondary_ctls_low, |
| 6478 | msrs->secondary_ctls_high); |
| 6479 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6480 | msrs->secondary_ctls_low = 0; |
| 6481 | msrs->secondary_ctls_high &= |
| 6482 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6483 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6484 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6485 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6486 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6487 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6488 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6489 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6490 | SECONDARY_EXEC_RDSEED_EXITING | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 6491 | SECONDARY_EXEC_XSAVES | |
| 6492 | SECONDARY_EXEC_TSC_SCALING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6493 | |
| 6494 | /* |
| 6495 | * We can emulate "VMCS shadowing," even if the hardware |
| 6496 | * doesn't support it. |
| 6497 | */ |
| 6498 | msrs->secondary_ctls_high |= |
| 6499 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6500 | |
| 6501 | if (enable_ept) { |
| 6502 | /* nested EPT: emulate EPT also to L1 */ |
| 6503 | msrs->secondary_ctls_high |= |
| 6504 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6505 | msrs->ept_caps = |
| 6506 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6507 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6508 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6509 | VMX_EPT_INVEPT_BIT | |
| 6510 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6511 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6512 | msrs->ept_caps &= ept_caps; |
| 6513 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6514 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6515 | VMX_EPT_1GB_PAGE_BIT; |
| 6516 | if (enable_ept_ad_bits) { |
| 6517 | msrs->secondary_ctls_high |= |
| 6518 | SECONDARY_EXEC_ENABLE_PML; |
| 6519 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6520 | } |
| 6521 | } |
| 6522 | |
| 6523 | if (cpu_has_vmx_vmfunc()) { |
| 6524 | msrs->secondary_ctls_high |= |
| 6525 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6526 | /* |
| 6527 | * Advertise EPTP switching unconditionally |
| 6528 | * since we emulate it |
| 6529 | */ |
| 6530 | if (enable_ept) |
| 6531 | msrs->vmfunc_controls = |
| 6532 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6533 | } |
| 6534 | |
| 6535 | /* |
| 6536 | * Old versions of KVM use the single-context version without |
| 6537 | * checking for support, so declare that it is supported even |
| 6538 | * though it is treated as global context. The alternative is |
| 6539 | * not failing the single-context invvpid, and it is worse. |
| 6540 | */ |
| 6541 | if (enable_vpid) { |
| 6542 | msrs->secondary_ctls_high |= |
| 6543 | SECONDARY_EXEC_ENABLE_VPID; |
| 6544 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6545 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6546 | } |
| 6547 | |
| 6548 | if (enable_unrestricted_guest) |
| 6549 | msrs->secondary_ctls_high |= |
| 6550 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6551 | |
| 6552 | if (flexpriority_enabled) |
| 6553 | msrs->secondary_ctls_high |= |
| 6554 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6555 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6556 | if (enable_sgx) |
| 6557 | msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; |
| 6558 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6559 | /* miscellaneous data */ |
| 6560 | rdmsr(MSR_IA32_VMX_MISC, |
| 6561 | msrs->misc_low, |
| 6562 | msrs->misc_high); |
| 6563 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6564 | msrs->misc_low |= |
| 6565 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6566 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6567 | VMX_MISC_ACTIVITY_HLT | |
| 6568 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6569 | msrs->misc_high = 0; |
| 6570 | |
| 6571 | /* |
| 6572 | * This MSR reports some information about VMX support. We |
| 6573 | * should return information about the VMX we emulate for the |
| 6574 | * guest, and the VMCS structure we give it - not about the |
| 6575 | * VMX support of the underlying hardware. |
| 6576 | */ |
| 6577 | msrs->basic = |
| 6578 | VMCS12_REVISION | |
| 6579 | VMX_BASIC_TRUE_CTLS | |
| 6580 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6581 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6582 | |
| 6583 | if (cpu_has_vmx_basic_inout()) |
| 6584 | msrs->basic |= VMX_BASIC_INOUT; |
| 6585 | |
| 6586 | /* |
| 6587 | * These MSRs specify bits which the guest must keep fixed on |
| 6588 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6589 | * We picked the standard core2 setting. |
| 6590 | */ |
| 6591 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6592 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6593 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6594 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6595 | |
| 6596 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6597 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6598 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6599 | |
| 6600 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
| 6601 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
| 6602 | } |
| 6603 | |
| 6604 | void nested_vmx_hardware_unsetup(void) |
| 6605 | { |
| 6606 | int i; |
| 6607 | |
| 6608 | if (enable_shadow_vmcs) { |
| 6609 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6610 | free_page((unsigned long)vmx_bitmap[i]); |
| 6611 | } |
| 6612 | } |
| 6613 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6614 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6615 | { |
| 6616 | int i; |
| 6617 | |
| 6618 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6619 | enable_shadow_vmcs = 0; |
| 6620 | if (enable_shadow_vmcs) { |
| 6621 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6622 | /* |
| 6623 | * The vmx_bitmap is not tied to a VM and so should |
| 6624 | * not be charged to a memcg. |
| 6625 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6626 | vmx_bitmap[i] = (unsigned long *) |
| 6627 | __get_free_page(GFP_KERNEL); |
| 6628 | if (!vmx_bitmap[i]) { |
| 6629 | nested_vmx_hardware_unsetup(); |
| 6630 | return -ENOMEM; |
| 6631 | } |
| 6632 | } |
| 6633 | |
| 6634 | init_vmcs_shadow_fields(); |
| 6635 | } |
| 6636 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6637 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6638 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6639 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6640 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6641 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6642 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6643 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6644 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6645 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6646 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6647 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6648 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6649 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6650 | return 0; |
| 6651 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6652 | |
| 6653 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6654 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6655 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6656 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6657 | .get_state = vmx_get_nested_state, |
| 6658 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6659 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6660 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6661 | .enable_evmcs = nested_enable_evmcs, |
| 6662 | .get_evmcs_version = nested_get_evmcs_version, |
| 6663 | }; |