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 | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 14 | #include "trace.h" |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 15 | #include "vmx.h" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 16 | #include "x86.h" |
| 17 | |
| 18 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 19 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
| 20 | |
| 21 | static bool __read_mostly nested_early_check = 0; |
| 22 | module_param(nested_early_check, bool, S_IRUGO); |
| 23 | |
Sean Christopherson | 648fc8a | 2021-02-03 16:01:16 -0800 | [diff] [blame] | 24 | #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 25 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 26 | /* |
| 27 | * Hyper-V requires all of these, so mark them as supported even though |
| 28 | * they are just treated the same as all-context. |
| 29 | */ |
| 30 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 31 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 32 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 33 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 34 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 35 | |
| 36 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 37 | |
| 38 | enum { |
| 39 | VMX_VMREAD_BITMAP, |
| 40 | VMX_VMWRITE_BITMAP, |
| 41 | VMX_BITMAP_NR |
| 42 | }; |
| 43 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 44 | |
| 45 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 46 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
| 47 | |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 48 | struct shadow_vmcs_field { |
| 49 | u16 encoding; |
| 50 | u16 offset; |
| 51 | }; |
| 52 | static struct shadow_vmcs_field shadow_read_only_fields[] = { |
| 53 | #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) }, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 54 | #include "vmcs_shadow_fields.h" |
| 55 | }; |
| 56 | static int max_shadow_read_only_fields = |
| 57 | ARRAY_SIZE(shadow_read_only_fields); |
| 58 | |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 59 | static struct shadow_vmcs_field shadow_read_write_fields[] = { |
| 60 | #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) }, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 61 | #include "vmcs_shadow_fields.h" |
| 62 | }; |
| 63 | static int max_shadow_read_write_fields = |
| 64 | ARRAY_SIZE(shadow_read_write_fields); |
| 65 | |
Yi Wang | 8997f65 | 2019-01-21 15:27:05 +0800 | [diff] [blame] | 66 | static void init_vmcs_shadow_fields(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 67 | { |
| 68 | int i, j; |
| 69 | |
| 70 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 71 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 72 | |
| 73 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 74 | struct shadow_vmcs_field entry = shadow_read_only_fields[i]; |
| 75 | u16 field = entry.encoding; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 76 | |
| 77 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 78 | (i + 1 == max_shadow_read_only_fields || |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 79 | shadow_read_only_fields[i + 1].encoding != field + 1)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 80 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 81 | field + 1); |
| 82 | |
| 83 | clear_bit(field, vmx_vmread_bitmap); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 84 | if (field & 1) |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 86 | continue; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 87 | #else |
| 88 | entry.offset += sizeof(u32); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 89 | #endif |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 90 | shadow_read_only_fields[j++] = entry; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 91 | } |
| 92 | max_shadow_read_only_fields = j; |
| 93 | |
| 94 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 95 | struct shadow_vmcs_field entry = shadow_read_write_fields[i]; |
| 96 | u16 field = entry.encoding; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 97 | |
| 98 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 99 | (i + 1 == max_shadow_read_write_fields || |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 100 | shadow_read_write_fields[i + 1].encoding != field + 1)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 101 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 102 | field + 1); |
| 103 | |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 104 | WARN_ONCE(field >= GUEST_ES_AR_BYTES && |
| 105 | field <= GUEST_TR_AR_BYTES, |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 106 | "Update vmcs12_write_any() to drop reserved bits from AR_BYTES"); |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 107 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 108 | /* |
| 109 | * PML and the preemption timer can be emulated, but the |
| 110 | * processor cannot vmwrite to fields that don't exist |
| 111 | * on bare metal. |
| 112 | */ |
| 113 | switch (field) { |
| 114 | case GUEST_PML_INDEX: |
| 115 | if (!cpu_has_vmx_pml()) |
| 116 | continue; |
| 117 | break; |
| 118 | case VMX_PREEMPTION_TIMER_VALUE: |
| 119 | if (!cpu_has_vmx_preemption_timer()) |
| 120 | continue; |
| 121 | break; |
| 122 | case GUEST_INTR_STATUS: |
| 123 | if (!cpu_has_vmx_apicv()) |
| 124 | continue; |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | clear_bit(field, vmx_vmwrite_bitmap); |
| 131 | clear_bit(field, vmx_vmread_bitmap); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 132 | if (field & 1) |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 133 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 134 | continue; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 135 | #else |
| 136 | entry.offset += sizeof(u32); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 137 | #endif |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 138 | shadow_read_write_fields[j++] = entry; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 139 | } |
| 140 | max_shadow_read_write_fields = j; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 145 | * set the success or error code of an emulated VMX instruction (as specified |
| 146 | * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated |
| 147 | * instruction. |
| 148 | */ |
| 149 | static int nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 150 | { |
| 151 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 152 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 153 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 154 | return kvm_skip_emulated_instruction(vcpu); |
| 155 | } |
| 156 | |
| 157 | static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 158 | { |
| 159 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 160 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 161 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 162 | | X86_EFLAGS_CF); |
| 163 | return kvm_skip_emulated_instruction(vcpu); |
| 164 | } |
| 165 | |
| 166 | static int nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 167 | u32 vm_instruction_error) |
| 168 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 169 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 170 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 171 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 172 | | X86_EFLAGS_ZF); |
| 173 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 174 | /* |
| 175 | * We don't need to force a shadow sync because |
| 176 | * VM_INSTRUCTION_ERROR is not shadowed |
| 177 | */ |
| 178 | return kvm_skip_emulated_instruction(vcpu); |
| 179 | } |
| 180 | |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 181 | static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error) |
| 182 | { |
| 183 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 184 | |
| 185 | /* |
| 186 | * failValid writes the error number to the current VMCS, which |
| 187 | * can't be done if there isn't a current VMCS. |
| 188 | */ |
| 189 | if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs) |
| 190 | return nested_vmx_failInvalid(vcpu); |
| 191 | |
| 192 | return nested_vmx_failValid(vcpu, vm_instruction_error); |
| 193 | } |
| 194 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 195 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 196 | { |
| 197 | /* TODO: not to reset guest simply here. */ |
| 198 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 199 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
| 200 | } |
| 201 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 202 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 203 | { |
| 204 | return fixed_bits_valid(control, low, high); |
| 205 | } |
| 206 | |
| 207 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 208 | { |
| 209 | return low | ((u64)high << 32); |
| 210 | } |
| 211 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 212 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 213 | { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 214 | secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 215 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 216 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) |
| 220 | { |
| 221 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 222 | |
| 223 | if (!vmx->nested.hv_evmcs) |
| 224 | return; |
| 225 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 226 | kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true); |
Vitaly Kuznetsov | 95fa101 | 2020-03-09 16:52:11 +0100 | [diff] [blame] | 227 | vmx->nested.hv_evmcs_vmptr = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 228 | vmx->nested.hv_evmcs = NULL; |
| 229 | } |
| 230 | |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 231 | static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx, |
| 232 | struct loaded_vmcs *prev) |
| 233 | { |
| 234 | struct vmcs_host_state *dest, *src; |
| 235 | |
| 236 | if (unlikely(!vmx->guest_state_loaded)) |
| 237 | return; |
| 238 | |
| 239 | src = &prev->host_state; |
| 240 | dest = &vmx->loaded_vmcs->host_state; |
| 241 | |
| 242 | vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base); |
| 243 | dest->ldt_sel = src->ldt_sel; |
| 244 | #ifdef CONFIG_X86_64 |
| 245 | dest->ds_sel = src->ds_sel; |
| 246 | dest->es_sel = src->es_sel; |
| 247 | #endif |
| 248 | } |
| 249 | |
| 250 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
| 251 | { |
| 252 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 253 | struct loaded_vmcs *prev; |
| 254 | int cpu; |
| 255 | |
Sean Christopherson | 138534a | 2020-09-23 11:44:52 -0700 | [diff] [blame] | 256 | if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs)) |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 257 | return; |
| 258 | |
| 259 | cpu = get_cpu(); |
| 260 | prev = vmx->loaded_vmcs; |
| 261 | vmx->loaded_vmcs = vmcs; |
| 262 | vmx_vcpu_load_vmcs(vcpu, cpu, prev); |
| 263 | vmx_sync_vmcs_host_state(vmx, prev); |
| 264 | put_cpu(); |
| 265 | |
| 266 | vmx_register_cache_reset(vcpu); |
| 267 | } |
| 268 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 269 | /* |
| 270 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 271 | * just stops using VMX. |
| 272 | */ |
| 273 | static void free_nested(struct kvm_vcpu *vcpu) |
| 274 | { |
| 275 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 276 | |
Sean Christopherson | df82a24 | 2020-09-23 11:44:50 -0700 | [diff] [blame] | 277 | if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01)) |
| 278 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 279 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 280 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
| 281 | return; |
| 282 | |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 283 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Jan Kiszka | cf64527 | 2019-07-21 13:52:18 +0200 | [diff] [blame] | 284 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 285 | vmx->nested.vmxon = false; |
| 286 | vmx->nested.smm.vmxon = false; |
| 287 | free_vpid(vmx->nested.vpid02); |
| 288 | vmx->nested.posted_intr_nv = -1; |
| 289 | vmx->nested.current_vmptr = -1ull; |
| 290 | if (enable_shadow_vmcs) { |
| 291 | vmx_disable_shadow_vmcs(vmx); |
| 292 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 293 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 294 | vmx->vmcs01.shadow_vmcs = NULL; |
| 295 | } |
| 296 | kfree(vmx->nested.cached_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 297 | vmx->nested.cached_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 298 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 299 | vmx->nested.cached_shadow_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 300 | /* Unpin physical memory we referred to in the vmcs02 */ |
| 301 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 302 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 303 | vmx->nested.apic_access_page = NULL; |
| 304 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 305 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 306 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 307 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 308 | |
| 309 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 310 | |
| 311 | nested_release_evmcs(vcpu); |
| 312 | |
| 313 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 314 | } |
| 315 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 316 | /* |
| 317 | * Ensure that the current vmcs of the logical processor is the |
| 318 | * vmcs01 of the vcpu before calling free_nested(). |
| 319 | */ |
| 320 | void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 321 | { |
| 322 | vcpu_load(vcpu); |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 323 | vmx_leave_nested(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 324 | vcpu_put(vcpu); |
| 325 | } |
| 326 | |
| 327 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 328 | struct x86_exception *fault) |
| 329 | { |
| 330 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 331 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 332 | u32 vm_exit_reason; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 333 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 334 | |
| 335 | if (vmx->nested.pml_full) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 336 | vm_exit_reason = EXIT_REASON_PML_FULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 337 | vmx->nested.pml_full = false; |
| 338 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 339 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 340 | vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 341 | else |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 342 | vm_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 343 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 344 | nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 345 | vmcs12->guest_physical_address = fault->address; |
| 346 | } |
| 347 | |
| 348 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 349 | { |
| 350 | WARN_ON(mmu_is_nested(vcpu)); |
| 351 | |
| 352 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
| 353 | kvm_init_shadow_ept_mmu(vcpu, |
| 354 | to_vmx(vcpu)->nested.msrs.ept_caps & |
| 355 | VMX_EPT_EXECUTE_ONLY_BIT, |
| 356 | nested_ept_ad_enabled(vcpu), |
Sean Christopherson | ac69dfa | 2020-03-02 18:02:37 -0800 | [diff] [blame] | 357 | nested_ept_get_eptp(vcpu)); |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 358 | vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 359 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 360 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 361 | |
| 362 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 363 | } |
| 364 | |
| 365 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 366 | { |
| 367 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 368 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 369 | } |
| 370 | |
| 371 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 372 | u16 error_code) |
| 373 | { |
| 374 | bool inequality, bit; |
| 375 | |
| 376 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 377 | inequality = |
| 378 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 379 | vmcs12->page_fault_error_code_match; |
| 380 | return inequality ^ bit; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /* |
| 385 | * KVM wants to inject page-faults which it got to the guest. This function |
| 386 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 387 | */ |
| 388 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 389 | { |
| 390 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 391 | unsigned int nr = vcpu->arch.exception.nr; |
| 392 | bool has_payload = vcpu->arch.exception.has_payload; |
| 393 | unsigned long payload = vcpu->arch.exception.payload; |
| 394 | |
| 395 | if (nr == PF_VECTOR) { |
| 396 | if (vcpu->arch.exception.nested_apf) { |
| 397 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 398 | return 1; |
| 399 | } |
| 400 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 401 | vcpu->arch.exception.error_code)) { |
| 402 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 403 | return 1; |
| 404 | } |
| 405 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 406 | if (nr == DB_VECTOR) { |
| 407 | if (!has_payload) { |
| 408 | payload = vcpu->arch.dr6; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 409 | payload &= ~DR6_BT; |
| 410 | payload ^= DR6_ACTIVE_LOW; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 411 | } |
| 412 | *exit_qual = payload; |
| 413 | } else |
| 414 | *exit_qual = 0; |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 423 | struct x86_exception *fault) |
| 424 | { |
| 425 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 426 | |
| 427 | WARN_ON(!is_guest_mode(vcpu)); |
| 428 | |
| 429 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 430 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 431 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 432 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 433 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 434 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 435 | fault->address); |
| 436 | } else { |
| 437 | kvm_inject_page_fault(vcpu, fault); |
| 438 | } |
| 439 | } |
| 440 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 441 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 442 | struct vmcs12 *vmcs12) |
| 443 | { |
| 444 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 445 | return 0; |
| 446 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 447 | if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || |
| 448 | CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 449 | return -EINVAL; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 455 | struct vmcs12 *vmcs12) |
| 456 | { |
| 457 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 458 | return 0; |
| 459 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 460 | if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 461 | return -EINVAL; |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 467 | struct vmcs12 *vmcs12) |
| 468 | { |
| 469 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 470 | return 0; |
| 471 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 472 | if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 473 | return -EINVAL; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 480 | */ |
| 481 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 482 | { |
| 483 | unsigned long *msr_bitmap; |
| 484 | int f = sizeof(unsigned long); |
| 485 | |
| 486 | if (!cpu_has_vmx_msr_bitmap()) |
| 487 | return true; |
| 488 | |
| 489 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 490 | |
| 491 | if (msr <= 0x1fff) { |
| 492 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 493 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 494 | msr &= 0x1fff; |
| 495 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 496 | } |
| 497 | |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 503 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 504 | */ |
| 505 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 506 | unsigned long *msr_bitmap_nested, |
| 507 | u32 msr, int type) |
| 508 | { |
| 509 | int f = sizeof(unsigned long); |
| 510 | |
| 511 | /* |
| 512 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 513 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 514 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 515 | */ |
| 516 | if (msr <= 0x1fff) { |
| 517 | if (type & MSR_TYPE_R && |
| 518 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 519 | /* read-low */ |
| 520 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 521 | |
| 522 | if (type & MSR_TYPE_W && |
| 523 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 524 | /* write-low */ |
| 525 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 526 | |
| 527 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 528 | msr &= 0x1fff; |
| 529 | if (type & MSR_TYPE_R && |
| 530 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 531 | /* read-high */ |
| 532 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 533 | |
| 534 | if (type & MSR_TYPE_W && |
| 535 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 536 | /* write-high */ |
| 537 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 538 | |
| 539 | } |
| 540 | } |
| 541 | |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 542 | static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) |
| 543 | { |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 544 | int msr; |
| 545 | |
| 546 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 547 | unsigned word = msr / BITS_PER_LONG; |
| 548 | |
| 549 | msr_bitmap[word] = ~0; |
| 550 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 551 | } |
| 552 | } |
| 553 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 554 | /* |
| 555 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 556 | * we do not use the hardware. |
| 557 | */ |
| 558 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 559 | struct vmcs12 *vmcs12) |
| 560 | { |
| 561 | int msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 562 | unsigned long *msr_bitmap_l1; |
| 563 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 564 | struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 565 | |
| 566 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 567 | if (!cpu_has_vmx_msr_bitmap() || |
| 568 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 569 | return false; |
| 570 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 571 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 572 | return false; |
| 573 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 574 | msr_bitmap_l1 = (unsigned long *)map->hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 575 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 576 | /* |
| 577 | * To keep the control flow simple, pay eight 8-byte writes (sixteen |
| 578 | * 4-byte writes on 32-bit systems) up front to enable intercepts for |
| 579 | * the x2APIC MSR range and selectively disable them below. |
| 580 | */ |
| 581 | enable_x2apic_msr_intercepts(msr_bitmap_l0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 582 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 583 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
| 584 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 585 | /* |
| 586 | * L0 need not intercept reads for MSRs between 0x800 |
| 587 | * and 0x8ff, it just lets the processor take the value |
| 588 | * from the virtual-APIC page; take those 256 bits |
| 589 | * directly from the L1 bitmap. |
| 590 | */ |
| 591 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 592 | unsigned word = msr / BITS_PER_LONG; |
| 593 | |
| 594 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 595 | } |
| 596 | } |
| 597 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 598 | nested_vmx_disable_intercept_for_msr( |
| 599 | msr_bitmap_l1, msr_bitmap_l0, |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 600 | X2APIC_MSR(APIC_TASKPRI), |
Marc Orr | c73f4c9 | 2019-04-01 23:56:00 -0700 | [diff] [blame] | 601 | MSR_TYPE_R | MSR_TYPE_W); |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 602 | |
| 603 | if (nested_cpu_has_vid(vmcs12)) { |
| 604 | nested_vmx_disable_intercept_for_msr( |
| 605 | msr_bitmap_l1, msr_bitmap_l0, |
| 606 | X2APIC_MSR(APIC_EOI), |
| 607 | MSR_TYPE_W); |
| 608 | nested_vmx_disable_intercept_for_msr( |
| 609 | msr_bitmap_l1, msr_bitmap_l0, |
| 610 | X2APIC_MSR(APIC_SELF_IPI), |
| 611 | MSR_TYPE_W); |
| 612 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 615 | /* KVM unconditionally exposes the FS/GS base MSRs to L1. */ |
| 616 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 617 | MSR_FS_BASE, MSR_TYPE_RW); |
| 618 | |
| 619 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 620 | MSR_GS_BASE, MSR_TYPE_RW); |
| 621 | |
| 622 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 623 | MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 624 | |
| 625 | /* |
| 626 | * Checking the L0->L1 bitmap is trying to verify two things: |
| 627 | * |
| 628 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 629 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 630 | * from the L12 MSR bitmap that is too permissive. |
| 631 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 632 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 633 | * works properly because we only update the L01 MSR bitmap lazily. |
| 634 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 635 | * updated to reflect this when L1 (or its L2s) actually write to |
| 636 | * the MSR. |
| 637 | */ |
| 638 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 639 | nested_vmx_disable_intercept_for_msr( |
| 640 | msr_bitmap_l1, msr_bitmap_l0, |
| 641 | MSR_IA32_SPEC_CTRL, |
| 642 | MSR_TYPE_R | MSR_TYPE_W); |
| 643 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 644 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 645 | nested_vmx_disable_intercept_for_msr( |
| 646 | msr_bitmap_l1, msr_bitmap_l0, |
| 647 | MSR_IA32_PRED_CMD, |
| 648 | MSR_TYPE_W); |
| 649 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 650 | kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 651 | |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 656 | struct vmcs12 *vmcs12) |
| 657 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 658 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 659 | struct vmcs12 *shadow; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 660 | |
| 661 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 662 | vmcs12->vmcs_link_pointer == -1ull) |
| 663 | return; |
| 664 | |
| 665 | shadow = get_shadow_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 666 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 667 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)) |
| 668 | return; |
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 | memcpy(shadow, map.hva, VMCS12_SIZE); |
| 671 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 675 | struct vmcs12 *vmcs12) |
| 676 | { |
| 677 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 678 | |
| 679 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 680 | vmcs12->vmcs_link_pointer == -1ull) |
| 681 | return; |
| 682 | |
| 683 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 684 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * In nested virtualization, check if L1 has set |
| 689 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 690 | */ |
| 691 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 692 | { |
| 693 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 694 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 695 | } |
| 696 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 697 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 698 | struct vmcs12 *vmcs12) |
| 699 | { |
| 700 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 701 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 702 | return -EINVAL; |
| 703 | else |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 708 | struct vmcs12 *vmcs12) |
| 709 | { |
| 710 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 711 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 712 | !nested_cpu_has_vid(vmcs12) && |
| 713 | !nested_cpu_has_posted_intr(vmcs12)) |
| 714 | return 0; |
| 715 | |
| 716 | /* |
| 717 | * If virtualize x2apic mode is enabled, |
| 718 | * virtualize apic access must be disabled. |
| 719 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 720 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 721 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 722 | return -EINVAL; |
| 723 | |
| 724 | /* |
| 725 | * If virtual interrupt delivery is enabled, |
| 726 | * we must exit on external interrupts. |
| 727 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 728 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 729 | return -EINVAL; |
| 730 | |
| 731 | /* |
| 732 | * bits 15:8 should be zero in posted_intr_nv, |
| 733 | * the descriptor address has been already checked |
| 734 | * in nested_get_vmcs12_pages. |
| 735 | * |
| 736 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 737 | */ |
| 738 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 739 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 740 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 741 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 742 | 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] | 743 | return -EINVAL; |
| 744 | |
| 745 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 746 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 747 | return -EINVAL; |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 753 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 754 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 755 | if (count == 0) |
| 756 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 757 | |
| 758 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 759 | !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] | 760 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 761 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 762 | return 0; |
| 763 | } |
| 764 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 765 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 766 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 767 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 768 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 769 | vmcs12->vm_exit_msr_load_count, |
| 770 | vmcs12->vm_exit_msr_load_addr)) || |
| 771 | CC(nested_vmx_check_msr_switch(vcpu, |
| 772 | vmcs12->vm_exit_msr_store_count, |
| 773 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 774 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 775 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 779 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 780 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 781 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 782 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 783 | vmcs12->vm_entry_msr_load_count, |
| 784 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 785 | return -EINVAL; |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 790 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 791 | struct vmcs12 *vmcs12) |
| 792 | { |
| 793 | if (!nested_cpu_has_pml(vmcs12)) |
| 794 | return 0; |
| 795 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 796 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 797 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 804 | struct vmcs12 *vmcs12) |
| 805 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 806 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 807 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 808 | return -EINVAL; |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 813 | struct vmcs12 *vmcs12) |
| 814 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 815 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 816 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 817 | return -EINVAL; |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 822 | struct vmcs12 *vmcs12) |
| 823 | { |
| 824 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 825 | return 0; |
| 826 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 827 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 828 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 829 | return -EINVAL; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 835 | struct vmx_msr_entry *e) |
| 836 | { |
| 837 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 838 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 839 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 840 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 841 | CC(e->index == MSR_IA32_UCODE_REV)) |
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->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 844 | return -EINVAL; |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 849 | struct vmx_msr_entry *e) |
| 850 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 851 | if (CC(e->index == MSR_FS_BASE) || |
| 852 | CC(e->index == MSR_GS_BASE) || |
| 853 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 854 | nested_vmx_msr_check_common(vcpu, e)) |
| 855 | return -EINVAL; |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 860 | struct vmx_msr_entry *e) |
| 861 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 862 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 863 | nested_vmx_msr_check_common(vcpu, e)) |
| 864 | return -EINVAL; |
| 865 | return 0; |
| 866 | } |
| 867 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 868 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 869 | { |
| 870 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 871 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 872 | vmx->nested.msrs.misc_high); |
| 873 | |
| 874 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 875 | } |
| 876 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 877 | /* |
| 878 | * Load guest's/host's msr at nested entry/exit. |
| 879 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 880 | * |
| 881 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 882 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 883 | * as possible, process all valid entries before failing rather than precheck |
| 884 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 885 | */ |
| 886 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 887 | { |
| 888 | u32 i; |
| 889 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 890 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 891 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 892 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 893 | if (unlikely(i >= max_msr_list_size)) |
| 894 | goto fail; |
| 895 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 896 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 897 | &e, sizeof(e))) { |
| 898 | pr_debug_ratelimited( |
| 899 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 900 | __func__, i, gpa + i * sizeof(e)); |
| 901 | goto fail; |
| 902 | } |
| 903 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 904 | pr_debug_ratelimited( |
| 905 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 906 | __func__, i, e.index, e.reserved); |
| 907 | goto fail; |
| 908 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 909 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 910 | pr_debug_ratelimited( |
| 911 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 912 | __func__, i, e.index, e.value); |
| 913 | goto fail; |
| 914 | } |
| 915 | } |
| 916 | return 0; |
| 917 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 918 | /* 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] | 919 | return i + 1; |
| 920 | } |
| 921 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 922 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 923 | u32 msr_index, |
| 924 | u64 *data) |
| 925 | { |
| 926 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 927 | |
| 928 | /* |
| 929 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 930 | * does not include the time taken for emulation of the L2->L1 |
| 931 | * VM-exit in L0, use the more accurate value. |
| 932 | */ |
| 933 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 934 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 935 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 936 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 937 | if (i >= 0) { |
| 938 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 939 | |
| 940 | *data = kvm_read_l1_tsc(vcpu, val); |
| 941 | return true; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 946 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 947 | msr_index); |
| 948 | return false; |
| 949 | } |
| 950 | return true; |
| 951 | } |
| 952 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 953 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 954 | struct vmx_msr_entry *e) |
| 955 | { |
| 956 | if (kvm_vcpu_read_guest(vcpu, |
| 957 | gpa + i * sizeof(*e), |
| 958 | e, 2 * sizeof(u32))) { |
| 959 | pr_debug_ratelimited( |
| 960 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 961 | __func__, i, gpa + i * sizeof(*e)); |
| 962 | return false; |
| 963 | } |
| 964 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 965 | pr_debug_ratelimited( |
| 966 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 967 | __func__, i, e->index, e->reserved); |
| 968 | return false; |
| 969 | } |
| 970 | return true; |
| 971 | } |
| 972 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 973 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 974 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 975 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 976 | u32 i; |
| 977 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 978 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 979 | |
| 980 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 981 | if (unlikely(i >= max_msr_list_size)) |
| 982 | return -EINVAL; |
| 983 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 984 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 985 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 986 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 987 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 988 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 989 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 990 | if (kvm_vcpu_write_guest(vcpu, |
| 991 | gpa + i * sizeof(e) + |
| 992 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 993 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 994 | pr_debug_ratelimited( |
| 995 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 996 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 997 | return -EINVAL; |
| 998 | } |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1003 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1004 | { |
| 1005 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1006 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1007 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1008 | struct vmx_msr_entry e; |
| 1009 | u32 i; |
| 1010 | |
| 1011 | for (i = 0; i < count; i++) { |
| 1012 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1013 | return false; |
| 1014 | |
| 1015 | if (e.index == msr_index) |
| 1016 | return true; |
| 1017 | } |
| 1018 | return false; |
| 1019 | } |
| 1020 | |
| 1021 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1022 | u32 msr_index) |
| 1023 | { |
| 1024 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1025 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1026 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1027 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1028 | bool in_autostore_list; |
| 1029 | int last; |
| 1030 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1031 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1032 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1033 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1034 | |
| 1035 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1036 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1037 | /* |
| 1038 | * Emulated VMEntry does not fail here. Instead a less |
| 1039 | * accurate value will be returned by |
| 1040 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1041 | * instead of reading the value from the vmcs02 VMExit |
| 1042 | * MSR-store area. |
| 1043 | */ |
| 1044 | pr_warn_ratelimited( |
| 1045 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1046 | msr_index); |
| 1047 | return; |
| 1048 | } |
| 1049 | last = autostore->nr++; |
| 1050 | autostore->val[last].index = msr_index; |
| 1051 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1052 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1053 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1057 | /* |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1058 | * Returns true if the MMU needs to be sync'd on nested VM-Enter/VM-Exit. |
| 1059 | * tl;dr: the MMU needs a sync if L0 is using shadow paging and L1 didn't |
| 1060 | * enable VPID for L2 (implying it expects a TLB flush on VMX transitions). |
| 1061 | * Here's why. |
| 1062 | * |
| 1063 | * If EPT is enabled by L0 a sync is never needed: |
| 1064 | * - if it is disabled by L1, then L0 is not shadowing L1 or L2 PTEs, there |
| 1065 | * cannot be unsync'd SPTEs for either L1 or L2. |
| 1066 | * |
| 1067 | * - if it is also enabled by L1, then L0 doesn't need to sync on VM-Enter |
| 1068 | * VM-Enter as VM-Enter isn't required to invalidate guest-physical mappings |
| 1069 | * (irrespective of VPID), i.e. L1 can't rely on the (virtual) CPU to flush |
| 1070 | * stale guest-physical mappings for L2 from the TLB. And as above, L0 isn't |
| 1071 | * shadowing L1 PTEs so there are no unsync'd SPTEs to sync on VM-Exit. |
| 1072 | * |
| 1073 | * If EPT is disabled by L0: |
| 1074 | * - if VPID is enabled by L1 (for L2), the situation is similar to when L1 |
| 1075 | * enables EPT: L0 doesn't need to sync as VM-Enter and VM-Exit aren't |
| 1076 | * required to invalidate linear mappings (EPT is disabled so there are |
| 1077 | * no combined or guest-physical mappings), i.e. L1 can't rely on the |
| 1078 | * (virtual) CPU to flush stale linear mappings for either L2 or itself (L1). |
| 1079 | * |
| 1080 | * - however if VPID is disabled by L1, then a sync is needed as L1 expects all |
| 1081 | * linear mappings (EPT is disabled so there are no combined or guest-physical |
| 1082 | * mappings) to be invalidated on both VM-Enter and VM-Exit. |
| 1083 | * |
| 1084 | * Note, this logic is subtly different than nested_has_guest_tlb_tag(), which |
| 1085 | * additionally checks that L2 has been assigned a VPID (when EPT is disabled). |
| 1086 | * Whether or not L2 has been assigned a VPID by L0 is irrelevant with respect |
| 1087 | * to L1's expectations, e.g. L0 needs to invalidate hardware TLB entries if L2 |
| 1088 | * doesn't have a unique VPID to prevent reusing L1's entries (assuming L1 has |
| 1089 | * been assigned a VPID), but L0 doesn't need to do a MMU sync because L1 |
| 1090 | * doesn't expect stale (virtual) TLB entries to be flushed, i.e. L1 doesn't |
| 1091 | * know that L0 will flush the TLB and so L1 will do INVVPID as needed to flush |
| 1092 | * stale TLB entries, at which point L0 will sync L2's MMU. |
| 1093 | */ |
| 1094 | static bool nested_vmx_transition_mmu_sync(struct kvm_vcpu *vcpu) |
| 1095 | { |
| 1096 | return !enable_ept && !nested_cpu_has_vpid(get_vmcs12(vcpu)); |
| 1097 | } |
| 1098 | |
| 1099 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1100 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1101 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1102 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1103 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1104 | */ |
| 1105 | 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] | 1106 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1107 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1108 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1109 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1110 | return -EINVAL; |
| 1111 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1112 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1115 | * must not be dereferenced. |
| 1116 | */ |
| 1117 | if (!nested_ept && is_pae_paging(vcpu) && |
| 1118 | (cr3 != kvm_read_cr3(vcpu) || pdptrs_changed(vcpu))) { |
| 1119 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) { |
| 1120 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1121 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1125 | /* |
Sean Christopherson | 9805c5f | 2020-03-20 14:28:30 -0700 | [diff] [blame] | 1126 | * Unconditionally skip the TLB flush on fast CR3 switch, all TLB |
| 1127 | * flushes are handled by nested_vmx_transition_tlb_flush(). See |
| 1128 | * nested_vmx_transition_mmu_sync for details on skipping the MMU sync. |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1129 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1130 | if (!nested_ept) |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 1131 | kvm_mmu_new_pgd(vcpu, cr3, true, |
Sean Christopherson | 41fab65e | 2020-03-20 14:28:29 -0700 | [diff] [blame] | 1132 | !nested_vmx_transition_mmu_sync(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1133 | |
| 1134 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1135 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1136 | |
| 1137 | kvm_init_mmu(vcpu, false); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1144 | * populated by L2 differently than TLB entries populated |
| 1145 | * by L1. |
| 1146 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1147 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1148 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1149 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1150 | * |
| 1151 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1152 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1153 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1154 | */ |
| 1155 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1156 | { |
| 1157 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1158 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1159 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1160 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1161 | } |
| 1162 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1163 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1164 | struct vmcs12 *vmcs12, |
| 1165 | bool is_vmenter) |
| 1166 | { |
| 1167 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1168 | |
| 1169 | /* |
| 1170 | * If VPID is disabled, linear and combined mappings are flushed on |
| 1171 | * VM-Enter/VM-Exit, and guest-physical mappings are valid only for |
| 1172 | * their associated EPTP. |
| 1173 | */ |
| 1174 | if (!enable_vpid) |
| 1175 | return; |
| 1176 | |
| 1177 | /* |
| 1178 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1179 | * for *all* contexts to be flushed on VM-Enter/VM-Exit. |
| 1180 | * |
| 1181 | * If VPID is enabled and used by vmc12, but L2 does not have a unique |
| 1182 | * 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] | 1183 | * a VPID for L2, flush the current context as the effective ASID is |
| 1184 | * common to both L1 and L2. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1185 | * |
| 1186 | * Defer the flush so that it runs after vmcs02.EPTP has been set by |
| 1187 | * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid |
| 1188 | * redundant flushes further down the nested pipeline. |
| 1189 | * |
| 1190 | * If a TLB flush isn't required due to any of the above, and vpid12 is |
| 1191 | * changing then the new "virtual" VPID (vpid12) will reuse the same |
| 1192 | * "real" VPID (vpid02), and so needs to be sync'd. There is no direct |
| 1193 | * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for |
| 1194 | * all nested vCPUs. |
| 1195 | */ |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1196 | if (!nested_cpu_has_vpid(vmcs12)) { |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1197 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1198 | } else if (!nested_has_guest_tlb_tag(vcpu)) { |
| 1199 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1200 | } else if (is_vmenter && |
| 1201 | vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 1202 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 1203 | vpid_sync_context(nested_get_vpid02(vcpu)); |
| 1204 | } |
| 1205 | } |
| 1206 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1207 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1208 | { |
| 1209 | superset &= mask; |
| 1210 | subset &= mask; |
| 1211 | |
| 1212 | return (superset | subset) == superset; |
| 1213 | } |
| 1214 | |
| 1215 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1216 | { |
| 1217 | const u64 feature_and_reserved = |
| 1218 | /* feature (except bit 48; see below) */ |
| 1219 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1220 | /* reserved */ |
| 1221 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1222 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1223 | |
| 1224 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1225 | return -EINVAL; |
| 1226 | |
| 1227 | /* |
| 1228 | * KVM does not emulate a version of VMX that constrains physical |
| 1229 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1230 | */ |
| 1231 | if (data & BIT_ULL(48)) |
| 1232 | return -EINVAL; |
| 1233 | |
| 1234 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1235 | vmx_basic_vmcs_revision_id(data)) |
| 1236 | return -EINVAL; |
| 1237 | |
| 1238 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1239 | return -EINVAL; |
| 1240 | |
| 1241 | vmx->nested.msrs.basic = data; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | static int |
| 1246 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1247 | { |
| 1248 | u64 supported; |
| 1249 | u32 *lowp, *highp; |
| 1250 | |
| 1251 | switch (msr_index) { |
| 1252 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1253 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1254 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1255 | break; |
| 1256 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1257 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1258 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1259 | break; |
| 1260 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1261 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1262 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1263 | break; |
| 1264 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1265 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1266 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1267 | break; |
| 1268 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1269 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1270 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1271 | break; |
| 1272 | default: |
| 1273 | BUG(); |
| 1274 | } |
| 1275 | |
| 1276 | supported = vmx_control_msr(*lowp, *highp); |
| 1277 | |
| 1278 | /* Check must-be-1 bits are still 1. */ |
| 1279 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1280 | return -EINVAL; |
| 1281 | |
| 1282 | /* Check must-be-0 bits are still 0. */ |
| 1283 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1284 | return -EINVAL; |
| 1285 | |
| 1286 | *lowp = data; |
| 1287 | *highp = data >> 32; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1292 | { |
| 1293 | const u64 feature_and_reserved_bits = |
| 1294 | /* feature */ |
| 1295 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1296 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1297 | /* reserved */ |
| 1298 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1299 | u64 vmx_misc; |
| 1300 | |
| 1301 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1302 | vmx->nested.msrs.misc_high); |
| 1303 | |
| 1304 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1305 | return -EINVAL; |
| 1306 | |
| 1307 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1308 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1309 | vmx_misc_preemption_timer_rate(data) != |
| 1310 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1311 | return -EINVAL; |
| 1312 | |
| 1313 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1314 | return -EINVAL; |
| 1315 | |
| 1316 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1317 | return -EINVAL; |
| 1318 | |
| 1319 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1320 | return -EINVAL; |
| 1321 | |
| 1322 | vmx->nested.msrs.misc_low = data; |
| 1323 | vmx->nested.msrs.misc_high = data >> 32; |
| 1324 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1329 | { |
| 1330 | u64 vmx_ept_vpid_cap; |
| 1331 | |
| 1332 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1333 | vmx->nested.msrs.vpid_caps); |
| 1334 | |
| 1335 | /* Every bit is either reserved or a feature bit. */ |
| 1336 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1337 | return -EINVAL; |
| 1338 | |
| 1339 | vmx->nested.msrs.ept_caps = data; |
| 1340 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1345 | { |
| 1346 | u64 *msr; |
| 1347 | |
| 1348 | switch (msr_index) { |
| 1349 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1350 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1351 | break; |
| 1352 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1353 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1354 | break; |
| 1355 | default: |
| 1356 | BUG(); |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1361 | * must be 1 in the restored value. |
| 1362 | */ |
| 1363 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1364 | return -EINVAL; |
| 1365 | |
| 1366 | *msr = data; |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * Called when userspace is restoring VMX MSRs. |
| 1372 | * |
| 1373 | * Returns 0 on success, non-0 otherwise. |
| 1374 | */ |
| 1375 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1376 | { |
| 1377 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1378 | |
| 1379 | /* |
| 1380 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1381 | * is in VMX operation. |
| 1382 | */ |
| 1383 | if (vmx->nested.vmxon) |
| 1384 | return -EBUSY; |
| 1385 | |
| 1386 | switch (msr_index) { |
| 1387 | case MSR_IA32_VMX_BASIC: |
| 1388 | return vmx_restore_vmx_basic(vmx, data); |
| 1389 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1390 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1391 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1392 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1393 | /* |
| 1394 | * The "non-true" VMX capability MSRs are generated from the |
| 1395 | * "true" MSRs, so we do not support restoring them directly. |
| 1396 | * |
| 1397 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1398 | * should restore the "true" MSRs with the must-be-1 bits |
| 1399 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1400 | * DEFAULT SETTINGS". |
| 1401 | */ |
| 1402 | return -EINVAL; |
| 1403 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1404 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1405 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1406 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1407 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1408 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1409 | case MSR_IA32_VMX_MISC: |
| 1410 | return vmx_restore_vmx_misc(vmx, data); |
| 1411 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1412 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1413 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1414 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1415 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1416 | /* |
| 1417 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1418 | * do not support restoring them directly. |
| 1419 | */ |
| 1420 | return -EINVAL; |
| 1421 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1422 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1423 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1424 | vmx->nested.msrs.vmcs_enum = data; |
| 1425 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1426 | case MSR_IA32_VMX_VMFUNC: |
| 1427 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1428 | return -EINVAL; |
| 1429 | vmx->nested.msrs.vmfunc_controls = data; |
| 1430 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1431 | default: |
| 1432 | /* |
| 1433 | * The rest of the VMX capability MSRs do not support restore. |
| 1434 | */ |
| 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | /* Returns 0 on success, non-0 otherwise. */ |
| 1440 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1441 | { |
| 1442 | switch (msr_index) { |
| 1443 | case MSR_IA32_VMX_BASIC: |
| 1444 | *pdata = msrs->basic; |
| 1445 | break; |
| 1446 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1447 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1448 | *pdata = vmx_control_msr( |
| 1449 | msrs->pinbased_ctls_low, |
| 1450 | msrs->pinbased_ctls_high); |
| 1451 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1452 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1453 | break; |
| 1454 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1455 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1456 | *pdata = vmx_control_msr( |
| 1457 | msrs->procbased_ctls_low, |
| 1458 | msrs->procbased_ctls_high); |
| 1459 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1460 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1461 | break; |
| 1462 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1463 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1464 | *pdata = vmx_control_msr( |
| 1465 | msrs->exit_ctls_low, |
| 1466 | msrs->exit_ctls_high); |
| 1467 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1468 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1469 | break; |
| 1470 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1471 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1472 | *pdata = vmx_control_msr( |
| 1473 | msrs->entry_ctls_low, |
| 1474 | msrs->entry_ctls_high); |
| 1475 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1476 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1477 | break; |
| 1478 | case MSR_IA32_VMX_MISC: |
| 1479 | *pdata = vmx_control_msr( |
| 1480 | msrs->misc_low, |
| 1481 | msrs->misc_high); |
| 1482 | break; |
| 1483 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1484 | *pdata = msrs->cr0_fixed0; |
| 1485 | break; |
| 1486 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1487 | *pdata = msrs->cr0_fixed1; |
| 1488 | break; |
| 1489 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1490 | *pdata = msrs->cr4_fixed0; |
| 1491 | break; |
| 1492 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1493 | *pdata = msrs->cr4_fixed1; |
| 1494 | break; |
| 1495 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1496 | *pdata = msrs->vmcs_enum; |
| 1497 | break; |
| 1498 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1499 | *pdata = vmx_control_msr( |
| 1500 | msrs->secondary_ctls_low, |
| 1501 | msrs->secondary_ctls_high); |
| 1502 | break; |
| 1503 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1504 | *pdata = msrs->ept_caps | |
| 1505 | ((u64)msrs->vpid_caps << 32); |
| 1506 | break; |
| 1507 | case MSR_IA32_VMX_VMFUNC: |
| 1508 | *pdata = msrs->vmfunc_controls; |
| 1509 | break; |
| 1510 | default: |
| 1511 | return 1; |
| 1512 | } |
| 1513 | |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1518 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1519 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1520 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1521 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1522 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1523 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1524 | */ |
| 1525 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1526 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1527 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1528 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1529 | struct shadow_vmcs_field field; |
| 1530 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1531 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1532 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1533 | if (WARN_ON(!shadow_vmcs)) |
| 1534 | return; |
| 1535 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1536 | preempt_disable(); |
| 1537 | |
| 1538 | vmcs_load(shadow_vmcs); |
| 1539 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1540 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1541 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1542 | val = __vmcs_readl(field.encoding); |
| 1543 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | vmcs_clear(shadow_vmcs); |
| 1547 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1548 | |
| 1549 | preempt_enable(); |
| 1550 | } |
| 1551 | |
| 1552 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1553 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1554 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1555 | shadow_read_write_fields, |
| 1556 | shadow_read_only_fields |
| 1557 | }; |
| 1558 | const int max_fields[] = { |
| 1559 | max_shadow_read_write_fields, |
| 1560 | max_shadow_read_only_fields |
| 1561 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1562 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1563 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1564 | struct shadow_vmcs_field field; |
| 1565 | unsigned long val; |
| 1566 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1567 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1568 | if (WARN_ON(!shadow_vmcs)) |
| 1569 | return; |
| 1570 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1571 | vmcs_load(shadow_vmcs); |
| 1572 | |
| 1573 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1574 | for (i = 0; i < max_fields[q]; i++) { |
| 1575 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1576 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1577 | field.offset); |
| 1578 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | vmcs_clear(shadow_vmcs); |
| 1583 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1584 | } |
| 1585 | |
| 1586 | static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx) |
| 1587 | { |
| 1588 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1589 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1590 | |
| 1591 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1592 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1593 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1594 | |
| 1595 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1596 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1597 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1598 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1599 | vmcs12->guest_interruptibility_info = |
| 1600 | evmcs->guest_interruptibility_info; |
| 1601 | } |
| 1602 | |
| 1603 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1604 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1605 | vmcs12->cpu_based_vm_exec_control = |
| 1606 | evmcs->cpu_based_vm_exec_control; |
| 1607 | } |
| 1608 | |
| 1609 | if (unlikely(!(evmcs->hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1610 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1611 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1612 | } |
| 1613 | |
| 1614 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1615 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1616 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1617 | } |
| 1618 | |
| 1619 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1620 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1621 | vmcs12->vm_entry_intr_info_field = |
| 1622 | evmcs->vm_entry_intr_info_field; |
| 1623 | vmcs12->vm_entry_exception_error_code = |
| 1624 | evmcs->vm_entry_exception_error_code; |
| 1625 | vmcs12->vm_entry_instruction_len = |
| 1626 | evmcs->vm_entry_instruction_len; |
| 1627 | } |
| 1628 | |
| 1629 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1630 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1631 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1632 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1633 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1634 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1635 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1636 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1637 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1638 | vmcs12->host_rip = evmcs->host_rip; |
| 1639 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1640 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1641 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1642 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1643 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1644 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1645 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1646 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1647 | } |
| 1648 | |
| 1649 | if (unlikely(!(evmcs->hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1650 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1651 | vmcs12->pin_based_vm_exec_control = |
| 1652 | evmcs->pin_based_vm_exec_control; |
| 1653 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1654 | vmcs12->secondary_vm_exec_control = |
| 1655 | evmcs->secondary_vm_exec_control; |
| 1656 | } |
| 1657 | |
| 1658 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1659 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1660 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1661 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1662 | } |
| 1663 | |
| 1664 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1665 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1666 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1667 | } |
| 1668 | |
| 1669 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1670 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1671 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1672 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1673 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1674 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1675 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1676 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1677 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1678 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1679 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1680 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1681 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1682 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1683 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1684 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1685 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1686 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1687 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1688 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1689 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1690 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1691 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1692 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1693 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1694 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1695 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1696 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1697 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1698 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1699 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1700 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1701 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1702 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1703 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1704 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1705 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1706 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1707 | } |
| 1708 | |
| 1709 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1710 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1711 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1712 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1713 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1714 | } |
| 1715 | |
| 1716 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1717 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1718 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1719 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1720 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1721 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1722 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1723 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1724 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1725 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1726 | } |
| 1727 | |
| 1728 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1729 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1730 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1731 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1732 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1733 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1734 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1735 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1736 | } |
| 1737 | |
| 1738 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1739 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1740 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1741 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1742 | } |
| 1743 | |
| 1744 | if (unlikely(!(evmcs->hv_clean_fields & |
| 1745 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1746 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1747 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1748 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1749 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1750 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1751 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1752 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1753 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1754 | vmcs12->guest_pending_dbg_exceptions = |
| 1755 | evmcs->guest_pending_dbg_exceptions; |
| 1756 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1757 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1758 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1759 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1760 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1761 | } |
| 1762 | |
| 1763 | /* |
| 1764 | * Not used? |
| 1765 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1766 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1767 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1768 | * vmcs12->page_fault_error_code_mask = |
| 1769 | * evmcs->page_fault_error_code_mask; |
| 1770 | * vmcs12->page_fault_error_code_match = |
| 1771 | * evmcs->page_fault_error_code_match; |
| 1772 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1773 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1774 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1775 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1776 | */ |
| 1777 | |
| 1778 | /* |
| 1779 | * Read only fields: |
| 1780 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1781 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1782 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1783 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1784 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1785 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1786 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1787 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1788 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1789 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1790 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1791 | * |
| 1792 | * Not present in struct vmcs12: |
| 1793 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1794 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1795 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1796 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1797 | */ |
| 1798 | |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
| 1803 | { |
| 1804 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1805 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1806 | |
| 1807 | /* |
| 1808 | * Should not be changed by KVM: |
| 1809 | * |
| 1810 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1811 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1812 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1813 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1814 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1815 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1816 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1817 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1818 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1819 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1820 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1821 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1822 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1823 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1824 | * evmcs->host_rip = vmcs12->host_rip; |
| 1825 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1826 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1827 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1828 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1829 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1830 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1831 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1832 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1833 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1834 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1835 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1836 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1837 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1838 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1839 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1840 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1841 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1842 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1843 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1844 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1845 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1846 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1847 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1848 | * evmcs->page_fault_error_code_mask = |
| 1849 | * vmcs12->page_fault_error_code_mask; |
| 1850 | * evmcs->page_fault_error_code_match = |
| 1851 | * vmcs12->page_fault_error_code_match; |
| 1852 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1853 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1854 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1855 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1856 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1857 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1858 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1859 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1860 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1861 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1862 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1863 | * |
| 1864 | * Not present in struct vmcs12: |
| 1865 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1866 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1867 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1868 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1869 | */ |
| 1870 | |
| 1871 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1872 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1873 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1874 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1875 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1876 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1877 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1878 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1879 | |
| 1880 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1881 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1882 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1883 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1884 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1885 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1886 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1887 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1888 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1889 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1890 | |
| 1891 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1892 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1893 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1894 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1895 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1896 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1897 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1898 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1899 | |
| 1900 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1901 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1902 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1903 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1904 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1905 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1906 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1907 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1908 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1909 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1910 | |
| 1911 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1912 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1913 | |
| 1914 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1915 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1916 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1917 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1918 | |
| 1919 | evmcs->guest_pending_dbg_exceptions = |
| 1920 | vmcs12->guest_pending_dbg_exceptions; |
| 1921 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1922 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1923 | |
| 1924 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1925 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1926 | |
| 1927 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1928 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1929 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1930 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1931 | |
| 1932 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1933 | |
| 1934 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1935 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1936 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1937 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1938 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1939 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1940 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1941 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1942 | |
| 1943 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1944 | |
| 1945 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1946 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1947 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1948 | |
| 1949 | evmcs->guest_interruptibility_info = |
| 1950 | vmcs12->guest_interruptibility_info; |
| 1951 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1952 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1953 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1954 | evmcs->vm_entry_exception_error_code = |
| 1955 | vmcs12->vm_entry_exception_error_code; |
| 1956 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1957 | |
| 1958 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1959 | |
| 1960 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1961 | |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
| 1965 | /* |
| 1966 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1967 | * instruction. |
| 1968 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1969 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1970 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1971 | { |
| 1972 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1973 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1974 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1975 | |
| 1976 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1977 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1978 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1979 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) |
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 | 95fa101 | 2020-03-09 16:52:11 +0100 | [diff] [blame] | 1982 | if (unlikely(!vmx->nested.hv_evmcs || |
| 1983 | evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1984 | if (!vmx->nested.hv_evmcs) |
| 1985 | vmx->nested.current_vmptr = -1ull; |
| 1986 | |
| 1987 | nested_release_evmcs(vcpu); |
| 1988 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1989 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1990 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1991 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1992 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1993 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1994 | |
| 1995 | /* |
| 1996 | * Currently, KVM only supports eVMCS version 1 |
| 1997 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 1998 | * value to first u32 field of eVMCS which should specify eVMCS |
| 1999 | * VersionNumber. |
| 2000 | * |
| 2001 | * Guest should be aware of supported eVMCS versions by host by |
| 2002 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 2003 | * expected to set this CPUID leaf according to the value |
| 2004 | * returned in vmcs_version from nested_enable_evmcs(). |
| 2005 | * |
| 2006 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 2007 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 2008 | * just sets first u32 field of eVMCS to revision_id specified |
| 2009 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 2010 | * which is one of the supported versions specified in |
| 2011 | * CPUID.0x4000000A.EAX[0:15]. |
| 2012 | * |
| 2013 | * To overcome Hyper-V bug, we accept here either a supported |
| 2014 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 2015 | * u32 field of eVMCS. |
| 2016 | */ |
| 2017 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 2018 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 2019 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2020 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | vmx->nested.dirty_vmcs12 = true; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 2024 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2025 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2026 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2027 | /* |
| 2028 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2029 | * reloaded from guest's memory (read only fields, fields not |
| 2030 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2031 | * are no leftovers. |
| 2032 | */ |
| 2033 | if (from_launch) { |
| 2034 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2035 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2036 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2037 | } |
| 2038 | |
| 2039 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2040 | |
| 2041 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2042 | * 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] | 2043 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2044 | */ |
| 2045 | if (from_launch || evmcs_gpa_changed) |
| 2046 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2047 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2048 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2049 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2052 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2053 | { |
| 2054 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2055 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2056 | if (vmx->nested.hv_evmcs) { |
| 2057 | copy_vmcs12_to_enlightened(vmx); |
| 2058 | /* All fields are clean */ |
| 2059 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2060 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2061 | } else { |
| 2062 | copy_vmcs12_to_shadow(vmx); |
| 2063 | } |
| 2064 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2065 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2069 | { |
| 2070 | struct vcpu_vmx *vmx = |
| 2071 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2072 | |
| 2073 | vmx->nested.preemption_timer_expired = true; |
| 2074 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2075 | kvm_vcpu_kick(&vmx->vcpu); |
| 2076 | |
| 2077 | return HRTIMER_NORESTART; |
| 2078 | } |
| 2079 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2080 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2081 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2082 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2083 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2084 | |
| 2085 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2086 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2087 | |
| 2088 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2089 | vmx->nested.preemption_timer_deadline = |
| 2090 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2091 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2092 | } |
| 2093 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2097 | u64 preemption_timeout) |
| 2098 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2099 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2100 | |
| 2101 | /* |
| 2102 | * A timer value of zero is architecturally guaranteed to cause |
| 2103 | * a VMExit prior to executing any instructions in the guest. |
| 2104 | */ |
| 2105 | if (preemption_timeout == 0) { |
| 2106 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2107 | return; |
| 2108 | } |
| 2109 | |
| 2110 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2111 | return; |
| 2112 | |
| 2113 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2114 | preemption_timeout *= 1000000; |
| 2115 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2116 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2117 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2118 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2122 | { |
| 2123 | if (vmx->nested.nested_run_pending && |
| 2124 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2125 | return vmcs12->guest_ia32_efer; |
| 2126 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2127 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2128 | else |
| 2129 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2130 | } |
| 2131 | |
| 2132 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2133 | { |
| 2134 | /* |
| 2135 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2136 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2137 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2138 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2139 | */ |
| 2140 | if (vmx->nested.vmcs02_initialized) |
| 2141 | return; |
| 2142 | vmx->nested.vmcs02_initialized = true; |
| 2143 | |
| 2144 | /* |
| 2145 | * We don't care what the EPTP value is we just need to guarantee |
| 2146 | * it's valid so we don't get a false positive when doing early |
| 2147 | * consistency checks. |
| 2148 | */ |
| 2149 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2150 | vmcs_write64(EPT_POINTER, |
| 2151 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2152 | |
| 2153 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2154 | if (cpu_has_vmx_vmfunc()) |
| 2155 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2156 | |
| 2157 | if (cpu_has_vmx_posted_intr()) |
| 2158 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2159 | |
| 2160 | if (cpu_has_vmx_msr_bitmap()) |
| 2161 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2162 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2163 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2164 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2165 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2166 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2167 | */ |
| 2168 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2169 | vmcs_write64(PML_ADDRESS, 0); |
| 2170 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2171 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2172 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2173 | if (cpu_has_vmx_encls_vmexit()) |
| 2174 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2175 | |
| 2176 | /* |
| 2177 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2178 | * addresses are constant (for vmcs02), the counts can change based |
| 2179 | * on L2's behavior, e.g. switching to/from long mode. |
| 2180 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2181 | 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] | 2182 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2183 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2184 | |
| 2185 | vmx_set_constant_host_state(vmx); |
| 2186 | } |
| 2187 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2188 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2189 | struct vmcs12 *vmcs12) |
| 2190 | { |
| 2191 | prepare_vmcs02_constant_state(vmx); |
| 2192 | |
| 2193 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 2194 | |
| 2195 | if (enable_vpid) { |
| 2196 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2197 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2198 | else |
| 2199 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2204 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2205 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2206 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2207 | |
| 2208 | if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2209 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2210 | |
| 2211 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2212 | * PIN CONTROLS |
| 2213 | */ |
Sean Christopherson | c075c3e | 2019-05-07 12:17:53 -0700 | [diff] [blame] | 2214 | exec_control = vmx_pin_based_exec_ctrl(vmx); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2215 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2216 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2217 | |
| 2218 | /* Posted interrupts setting is only taken from vmcs12. */ |
| 2219 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 2220 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 2221 | vmx->nested.pi_pending = false; |
| 2222 | } else { |
| 2223 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 2224 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2225 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2226 | |
| 2227 | /* |
| 2228 | * EXEC CONTROLS |
| 2229 | */ |
| 2230 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2231 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2232 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2233 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2234 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2235 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2236 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2237 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2238 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2239 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2240 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2241 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2242 | CPU_BASED_CR8_STORE_EXITING; |
| 2243 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2244 | |
| 2245 | /* |
| 2246 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2247 | * for I/O port accesses. |
| 2248 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2249 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2250 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2251 | |
| 2252 | /* |
| 2253 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2254 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2255 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2256 | * only set/clear this bit. |
| 2257 | */ |
| 2258 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2259 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2260 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2261 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2262 | |
| 2263 | /* |
| 2264 | * SECONDARY EXEC CONTROLS |
| 2265 | */ |
| 2266 | if (cpu_has_secondary_exec_ctrls()) { |
| 2267 | exec_control = vmx->secondary_exec_control; |
| 2268 | |
| 2269 | /* Take the following fields only from vmcs12 */ |
| 2270 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 2271 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2272 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2273 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2274 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2275 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2276 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 2277 | SECONDARY_EXEC_ENABLE_VMFUNC); |
| 2278 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2279 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2280 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2281 | |
| 2282 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2283 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2284 | |
| 2285 | /* VMCS shadowing for L2 is emulated for now */ |
| 2286 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2287 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2288 | /* |
| 2289 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2290 | * will not have to rewrite the controls just for this bit. |
| 2291 | */ |
| 2292 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2293 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2294 | exec_control |= SECONDARY_EXEC_DESC; |
| 2295 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2296 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2297 | vmcs_write16(GUEST_INTR_STATUS, |
| 2298 | vmcs12->guest_intr_status); |
| 2299 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2300 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2301 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2302 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2303 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | * ENTRY CONTROLS |
| 2308 | * |
| 2309 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2310 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2311 | * on the related bits (if supported by the CPU) in the hope that |
| 2312 | * we can avoid VMWrites during vmx_set_efer(). |
| 2313 | */ |
| 2314 | exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) & |
| 2315 | ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER; |
| 2316 | if (cpu_has_load_ia32_efer()) { |
| 2317 | if (guest_efer & EFER_LMA) |
| 2318 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2319 | if (guest_efer != host_efer) |
| 2320 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2321 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2322 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2323 | |
| 2324 | /* |
| 2325 | * EXIT CONTROLS |
| 2326 | * |
| 2327 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2328 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2329 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2330 | */ |
| 2331 | exec_control = vmx_vmexit_ctrl(); |
| 2332 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2333 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2334 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2335 | |
| 2336 | /* |
| 2337 | * Interrupt/Exception Fields |
| 2338 | */ |
| 2339 | if (vmx->nested.nested_run_pending) { |
| 2340 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2341 | vmcs12->vm_entry_intr_info_field); |
| 2342 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2343 | vmcs12->vm_entry_exception_error_code); |
| 2344 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2345 | vmcs12->vm_entry_instruction_len); |
| 2346 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2347 | vmcs12->guest_interruptibility_info); |
| 2348 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2349 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2350 | } else { |
| 2351 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2352 | } |
| 2353 | } |
| 2354 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2355 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2356 | { |
| 2357 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2358 | |
| 2359 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2360 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2361 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2362 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2363 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2364 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2365 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2366 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2367 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2368 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2369 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2370 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2371 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2372 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2373 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2374 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2375 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2376 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2377 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2378 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2379 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2380 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2381 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2382 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2383 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2384 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2385 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2386 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2387 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2388 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2389 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2390 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2391 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2392 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2393 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2394 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2395 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2396 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2397 | |
| 2398 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2402 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2403 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2404 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2405 | vmcs12->guest_pending_dbg_exceptions); |
| 2406 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2407 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2408 | |
| 2409 | /* |
| 2410 | * L1 may access the L2's PDPTR, so save them to construct |
| 2411 | * vmcs12 |
| 2412 | */ |
| 2413 | if (enable_ept) { |
| 2414 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2415 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2416 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2417 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2418 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2419 | |
| 2420 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2421 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2422 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2426 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2427 | |
| 2428 | /* |
| 2429 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2430 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2431 | * doesn't care about page faults then we should set all of these to |
| 2432 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2433 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2434 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2435 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2436 | * Note that below we don't need special code to set EB.PF beyond the |
| 2437 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2438 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2439 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2440 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2441 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2442 | /* |
| 2443 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2444 | * go ahead and use it? |
| 2445 | */ |
| 2446 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2447 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2448 | } else { |
| 2449 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2450 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2451 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2452 | |
| 2453 | if (cpu_has_vmx_apicv()) { |
| 2454 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2455 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2456 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2457 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2458 | } |
| 2459 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2460 | /* |
| 2461 | * Make sure the msr_autostore list is up to date before we set the |
| 2462 | * count in the vmcs02. |
| 2463 | */ |
| 2464 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2465 | |
| 2466 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2467 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2468 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2469 | |
| 2470 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | /* |
| 2474 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2475 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2476 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2477 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2478 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2479 | * function also has additional necessary side-effects, like setting various |
| 2480 | * vcpu->arch fields. |
| 2481 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2482 | * is assigned to entry_failure_code on failure. |
| 2483 | */ |
| 2484 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2485 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2486 | { |
| 2487 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2488 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2489 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2490 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2491 | if (vmx->nested.dirty_vmcs12 || hv_evmcs) { |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2492 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2493 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2494 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2495 | load_guest_pdptrs_vmcs12 = !hv_evmcs || |
| 2496 | !(hv_evmcs->hv_clean_fields & |
| 2497 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | if (vmx->nested.nested_run_pending && |
| 2501 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2502 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2503 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2504 | } else { |
| 2505 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2506 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2507 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2508 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2509 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2510 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2511 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2512 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2513 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2514 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2515 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2516 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2517 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2518 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2519 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2520 | |
| 2521 | if (vmx->nested.nested_run_pending && |
| 2522 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2523 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2524 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2525 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2526 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2527 | } |
| 2528 | |
| 2529 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 2530 | |
| 2531 | if (kvm_has_tsc_control) |
| 2532 | decache_tsc_multiplier(vmx); |
| 2533 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2534 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2535 | |
| 2536 | if (nested_cpu_has_ept(vmcs12)) |
| 2537 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2538 | |
| 2539 | /* |
| 2540 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2541 | * bits which we consider mandatory enabled. |
| 2542 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2543 | * the specifications by L1; It's not enough to take |
| 2544 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2545 | * have more bits than L1 expected. |
| 2546 | */ |
| 2547 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2548 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2549 | |
| 2550 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2551 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2552 | |
| 2553 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2554 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2555 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2556 | |
| 2557 | /* |
| 2558 | * Guest state is invalid and unrestricted guest is disabled, |
| 2559 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2560 | * Fail the VMEntry. |
| 2561 | */ |
Sean Christopherson | 2ba4493 | 2020-09-23 11:44:48 -0700 | [diff] [blame] | 2562 | if (CC(!vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2563 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2564 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2568 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
| 2569 | entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2570 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2571 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2572 | /* |
| 2573 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2574 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2575 | * 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] | 2576 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2577 | * transition to HLT instead of running L2. |
| 2578 | */ |
| 2579 | if (enable_ept) |
| 2580 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2581 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2582 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2583 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2584 | is_pae_paging(vcpu)) { |
| 2585 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2586 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2587 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2588 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2589 | } |
| 2590 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2591 | if (!enable_ept) |
| 2592 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2593 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2594 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2595 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 2596 | vmcs12->guest_ia32_perf_global_ctrl))) |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2597 | return -EINVAL; |
| 2598 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2599 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2600 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2601 | return 0; |
| 2602 | } |
| 2603 | |
| 2604 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2605 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2606 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2607 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2608 | return -EINVAL; |
| 2609 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2610 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2611 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2612 | return -EINVAL; |
| 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2617 | 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] | 2618 | { |
| 2619 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2620 | |
| 2621 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2622 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2623 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2624 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2625 | return false; |
| 2626 | break; |
| 2627 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2628 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2629 | return false; |
| 2630 | break; |
| 2631 | default: |
| 2632 | return false; |
| 2633 | } |
| 2634 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2635 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2636 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2637 | case VMX_EPTP_PWL_5: |
| 2638 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2639 | return false; |
| 2640 | break; |
| 2641 | case VMX_EPTP_PWL_4: |
| 2642 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2643 | return false; |
| 2644 | break; |
| 2645 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2646 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2647 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2648 | |
| 2649 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2650 | 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] | 2651 | return false; |
| 2652 | |
| 2653 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2654 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2655 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2656 | return false; |
| 2657 | } |
| 2658 | |
| 2659 | return true; |
| 2660 | } |
| 2661 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2662 | /* |
| 2663 | * Checks related to VM-Execution Control Fields |
| 2664 | */ |
| 2665 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2666 | struct vmcs12 *vmcs12) |
| 2667 | { |
| 2668 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2669 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2670 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2671 | vmx->nested.msrs.pinbased_ctls_low, |
| 2672 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2673 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2674 | vmx->nested.msrs.procbased_ctls_low, |
| 2675 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2676 | return -EINVAL; |
| 2677 | |
| 2678 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2679 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2680 | vmx->nested.msrs.secondary_ctls_low, |
| 2681 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2682 | return -EINVAL; |
| 2683 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2684 | 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] | 2685 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2686 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2687 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2688 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2689 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2690 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2691 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2692 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2693 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2694 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2695 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2696 | return -EINVAL; |
| 2697 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2698 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2699 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2700 | return -EINVAL; |
| 2701 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2702 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2703 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2704 | return -EINVAL; |
| 2705 | |
| 2706 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2707 | if (CC(vmcs12->vm_function_control & |
| 2708 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2709 | return -EINVAL; |
| 2710 | |
| 2711 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2712 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2713 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2714 | return -EINVAL; |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | return 0; |
| 2719 | } |
| 2720 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2721 | /* |
| 2722 | * Checks related to VM-Exit Control Fields |
| 2723 | */ |
| 2724 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2725 | struct vmcs12 *vmcs12) |
| 2726 | { |
| 2727 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2728 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2729 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2730 | vmx->nested.msrs.exit_ctls_low, |
| 2731 | vmx->nested.msrs.exit_ctls_high)) || |
| 2732 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2733 | return -EINVAL; |
| 2734 | |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2738 | /* |
| 2739 | * Checks related to VM-Entry Control Fields |
| 2740 | */ |
| 2741 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2742 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2743 | { |
| 2744 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2745 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2746 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2747 | vmx->nested.msrs.entry_ctls_low, |
| 2748 | vmx->nested.msrs.entry_ctls_high))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2749 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2750 | |
| 2751 | /* |
| 2752 | * From the Intel SDM, volume 3: |
| 2753 | * Fields relevant to VM-entry event injection must be set properly. |
| 2754 | * These fields are the VM-entry interruption-information field, the |
| 2755 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2756 | */ |
| 2757 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2758 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2759 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2760 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2761 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2762 | bool should_have_error_code; |
| 2763 | bool urg = nested_cpu_has2(vmcs12, |
| 2764 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2765 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2766 | |
| 2767 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2768 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2769 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2770 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2771 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2772 | |
| 2773 | /* VM-entry interruption-info field: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2774 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2775 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2776 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2777 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2778 | |
| 2779 | /* VM-entry interruption-info field: deliver error code */ |
| 2780 | should_have_error_code = |
| 2781 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2782 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2783 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2784 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2785 | |
| 2786 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2787 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2788 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2789 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2790 | |
| 2791 | /* VM-entry interruption-info field: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2792 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2793 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2794 | |
| 2795 | /* VM-entry instruction length */ |
| 2796 | switch (intr_type) { |
| 2797 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2798 | case INTR_TYPE_SOFT_INTR: |
| 2799 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2800 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2801 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2802 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2803 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2804 | } |
| 2805 | } |
| 2806 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2807 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2808 | return -EINVAL; |
| 2809 | |
| 2810 | return 0; |
| 2811 | } |
| 2812 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2813 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2814 | struct vmcs12 *vmcs12) |
| 2815 | { |
| 2816 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2817 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2818 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2819 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2820 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2821 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2822 | return nested_evmcs_check_controls(vmcs12); |
| 2823 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2824 | return 0; |
| 2825 | } |
| 2826 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2827 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2828 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2829 | { |
| 2830 | bool ia32e; |
| 2831 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2832 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2833 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2834 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2835 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2836 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2837 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2838 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2839 | return -EINVAL; |
| 2840 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2841 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2842 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2843 | return -EINVAL; |
| 2844 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2845 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2846 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2847 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2848 | return -EINVAL; |
| 2849 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2850 | #ifdef CONFIG_X86_64 |
| 2851 | ia32e = !!(vcpu->arch.efer & EFER_LMA); |
| 2852 | #else |
| 2853 | ia32e = false; |
| 2854 | #endif |
| 2855 | |
| 2856 | if (ia32e) { |
| 2857 | if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) || |
| 2858 | CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
| 2859 | return -EINVAL; |
| 2860 | } else { |
| 2861 | if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) || |
| 2862 | CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
| 2863 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2864 | CC((vmcs12->host_rip) >> 32)) |
| 2865 | return -EINVAL; |
| 2866 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2867 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2868 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2869 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2870 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2871 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2872 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2873 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2874 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2875 | CC(vmcs12->host_cs_selector == 0) || |
| 2876 | CC(vmcs12->host_tr_selector == 0) || |
| 2877 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2878 | return -EINVAL; |
| 2879 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2880 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2881 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2882 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2883 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2884 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2885 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2886 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2887 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2888 | /* |
| 2889 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2890 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2891 | * the values of the LMA and LME bits in the field must each be that of |
| 2892 | * the host address-space size VM-exit control. |
| 2893 | */ |
| 2894 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2895 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2896 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2897 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2898 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2899 | } |
| 2900 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2901 | return 0; |
| 2902 | } |
| 2903 | |
| 2904 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2905 | struct vmcs12 *vmcs12) |
| 2906 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2907 | int r = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2908 | struct vmcs12 *shadow; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2909 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2910 | |
| 2911 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 2912 | return 0; |
| 2913 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2914 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2915 | return -EINVAL; |
| 2916 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2917 | 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] | 2918 | return -EINVAL; |
| 2919 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2920 | shadow = map.hva; |
| 2921 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2922 | if (CC(shadow->hdr.revision_id != VMCS12_REVISION) || |
| 2923 | CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2924 | r = -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2925 | |
| 2926 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2927 | return r; |
| 2928 | } |
| 2929 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2930 | /* |
| 2931 | * Checks related to Guest Non-register State |
| 2932 | */ |
| 2933 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2934 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2935 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2936 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2937 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2938 | return -EINVAL; |
| 2939 | |
| 2940 | return 0; |
| 2941 | } |
| 2942 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2943 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2944 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2945 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2946 | { |
| 2947 | bool ia32e; |
| 2948 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2949 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2950 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2951 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2952 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2953 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2954 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2955 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2956 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2957 | return -EINVAL; |
| 2958 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2959 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2960 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2961 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2962 | |
| 2963 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2964 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2965 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2966 | } |
| 2967 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 2968 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2969 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2970 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 2971 | return -EINVAL; |
| 2972 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2973 | /* |
| 2974 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 2975 | * are performed on the field for the IA32_EFER MSR: |
| 2976 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 2977 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 2978 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 2979 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 2980 | * CR0.PG) is 1. |
| 2981 | */ |
| 2982 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 2983 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 2984 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2985 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 2986 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 2987 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 2988 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2989 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2993 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 2994 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2995 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2996 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 2997 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2998 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2999 | |
| 3000 | return 0; |
| 3001 | } |
| 3002 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3003 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3004 | { |
| 3005 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3006 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3007 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3008 | |
| 3009 | if (!nested_early_check) |
| 3010 | return 0; |
| 3011 | |
| 3012 | if (vmx->msr_autoload.host.nr) |
| 3013 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3014 | if (vmx->msr_autoload.guest.nr) |
| 3015 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3016 | |
| 3017 | preempt_disable(); |
| 3018 | |
| 3019 | vmx_prepare_switch_to_guest(vcpu); |
| 3020 | |
| 3021 | /* |
| 3022 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3023 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3024 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3025 | * there is no need to preserve other bits or save/restore the field. |
| 3026 | */ |
| 3027 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3028 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3029 | cr3 = __get_current_cr3_fast(); |
| 3030 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 3031 | vmcs_writel(HOST_CR3, cr3); |
| 3032 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 3033 | } |
| 3034 | |
| 3035 | cr4 = cr4_read_shadow(); |
| 3036 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3037 | vmcs_writel(HOST_CR4, cr4); |
| 3038 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3039 | } |
| 3040 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3041 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3042 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3043 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3044 | if (vmx->msr_autoload.host.nr) |
| 3045 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3046 | if (vmx->msr_autoload.guest.nr) |
| 3047 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3048 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3049 | if (vm_fail) { |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3050 | u32 error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3051 | |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3052 | preempt_enable(); |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3053 | |
| 3054 | trace_kvm_nested_vmenter_failed( |
| 3055 | "early hardware check VM-instruction error: ", error); |
| 3056 | WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3057 | return 1; |
| 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 3062 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3063 | if (hw_breakpoint_active()) |
| 3064 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3065 | local_irq_enable(); |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3066 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3067 | |
| 3068 | /* |
| 3069 | * A non-failing VMEntry means we somehow entered guest mode with |
| 3070 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 3071 | * is no telling what memory has been modified or what state has |
| 3072 | * been exposed to unknown code. Hitting this all but guarantees |
| 3073 | * a (very critical) hardware issue. |
| 3074 | */ |
| 3075 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 3076 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 3077 | |
| 3078 | return 0; |
| 3079 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3080 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3081 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3082 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3083 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3084 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3085 | /* |
| 3086 | * hv_evmcs may end up being not mapped after migration (when |
| 3087 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3088 | * properly reflected. |
| 3089 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3090 | if (vmx->nested.enlightened_vmcs_enabled && !vmx->nested.hv_evmcs) { |
| 3091 | enum nested_evmptrld_status evmptrld_status = |
| 3092 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3093 | |
| 3094 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
| 3095 | evmptrld_status == EVMPTRLD_ERROR) { |
| 3096 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3097 | __func__); |
| 3098 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3099 | vcpu->run->internal.suberror = |
| 3100 | KVM_INTERNAL_ERROR_EMULATION; |
| 3101 | vcpu->run->internal.ndata = 0; |
| 3102 | return false; |
| 3103 | } |
| 3104 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3105 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3106 | return true; |
| 3107 | } |
| 3108 | |
| 3109 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3110 | { |
| 3111 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3112 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3113 | struct kvm_host_map *map; |
| 3114 | struct page *page; |
| 3115 | u64 hpa; |
| 3116 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3117 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3118 | /* |
| 3119 | * Translate L1 physical address to host physical |
| 3120 | * address for vmcs02. Keep the page pinned, so this |
| 3121 | * physical address remains valid. We keep a reference |
| 3122 | * to it so we can release it later. |
| 3123 | */ |
| 3124 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 3125 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3126 | vmx->nested.apic_access_page = NULL; |
| 3127 | } |
| 3128 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3129 | if (!is_error_page(page)) { |
| 3130 | vmx->nested.apic_access_page = page; |
| 3131 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 3132 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 3133 | } else { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3134 | pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n", |
| 3135 | __func__); |
| 3136 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3137 | vcpu->run->internal.suberror = |
| 3138 | KVM_INTERNAL_ERROR_EMULATION; |
| 3139 | vcpu->run->internal.ndata = 0; |
| 3140 | return false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3141 | } |
| 3142 | } |
| 3143 | |
| 3144 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3145 | map = &vmx->nested.virtual_apic_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3146 | |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3147 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { |
| 3148 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3149 | } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && |
| 3150 | nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && |
| 3151 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3152 | /* |
| 3153 | * The processor will never use the TPR shadow, simply |
| 3154 | * clear the bit from the execution control. Such a |
| 3155 | * configuration is useless, but it happens in tests. |
| 3156 | * For any other configuration, failing the vm entry is |
| 3157 | * _not_ what the processor does but it's basically the |
| 3158 | * only possibility we have. |
| 3159 | */ |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3160 | exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3161 | } else { |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 3162 | /* |
| 3163 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to |
| 3164 | * force VM-Entry to fail. |
| 3165 | */ |
| 3166 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | if (nested_cpu_has_posted_intr(vmcs12)) { |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 3171 | map = &vmx->nested.pi_desc_map; |
| 3172 | |
| 3173 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { |
| 3174 | vmx->nested.pi_desc = |
| 3175 | (struct pi_desc *)(((void *)map->hva) + |
| 3176 | offset_in_page(vmcs12->posted_intr_desc_addr)); |
| 3177 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 3178 | 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] | 3179 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3180 | } |
| 3181 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3182 | exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3183 | else |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3184 | exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3185 | |
| 3186 | return true; |
| 3187 | } |
| 3188 | |
| 3189 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3190 | { |
| 3191 | if (!nested_get_evmcs_page(vcpu)) |
| 3192 | return false; |
| 3193 | |
| 3194 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3195 | return false; |
| 3196 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3197 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3198 | } |
| 3199 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3200 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3201 | { |
| 3202 | struct vmcs12 *vmcs12; |
| 3203 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3204 | gpa_t dst; |
| 3205 | |
| 3206 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3207 | return 0; |
| 3208 | |
| 3209 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3210 | return 1; |
| 3211 | |
| 3212 | /* |
| 3213 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3214 | * set is already checked as part of A/D emulation. |
| 3215 | */ |
| 3216 | vmcs12 = get_vmcs12(vcpu); |
| 3217 | if (!nested_cpu_has_pml(vmcs12)) |
| 3218 | return 0; |
| 3219 | |
| 3220 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3221 | vmx->nested.pml_full = true; |
| 3222 | return 1; |
| 3223 | } |
| 3224 | |
| 3225 | gpa &= ~0xFFFull; |
| 3226 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3227 | |
| 3228 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3229 | offset_in_page(dst), sizeof(gpa))) |
| 3230 | return 0; |
| 3231 | |
| 3232 | vmcs12->guest_pml_index--; |
| 3233 | |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3237 | /* |
| 3238 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3239 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3240 | * slightly different). It also specifies what exception to inject otherwise. |
| 3241 | * Note that many of these exceptions have priority over VM exits, so they |
| 3242 | * don't have to be checked again here. |
| 3243 | */ |
| 3244 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3245 | { |
| 3246 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3247 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3248 | return 0; |
| 3249 | } |
| 3250 | |
| 3251 | if (vmx_get_cpl(vcpu)) { |
| 3252 | kvm_inject_gp(vcpu, 0); |
| 3253 | return 0; |
| 3254 | } |
| 3255 | |
| 3256 | return 1; |
| 3257 | } |
| 3258 | |
| 3259 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3260 | { |
| 3261 | u8 rvi = vmx_get_rvi(); |
| 3262 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3263 | |
| 3264 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3265 | } |
| 3266 | |
| 3267 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3268 | struct vmcs12 *vmcs12); |
| 3269 | |
| 3270 | /* |
| 3271 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3272 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3273 | * |
| 3274 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3275 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3276 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3277 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3278 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3279 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3280 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3281 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3282 | { |
| 3283 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3284 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3285 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3286 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3287 | union vmx_exit_reason exit_reason = { |
| 3288 | .basic = EXIT_REASON_INVALID_STATE, |
| 3289 | .failed_vmentry = 1, |
| 3290 | }; |
| 3291 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3292 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3293 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 3294 | kvm_vcpu_flush_tlb_current(vcpu); |
| 3295 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3296 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3297 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3298 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3299 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3300 | |
| 3301 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3302 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3303 | if (kvm_mpx_supported() && |
| 3304 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3305 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3306 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3307 | /* |
| 3308 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3309 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3310 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3311 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3312 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3313 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3314 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3315 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3316 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3317 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3318 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3319 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3320 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3321 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3322 | */ |
| 3323 | if (!enable_ept && !nested_early_check) |
| 3324 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3325 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3326 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3327 | |
| 3328 | prepare_vmcs02_early(vmx, vmcs12); |
| 3329 | |
| 3330 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3331 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3332 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3333 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3334 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3335 | |
| 3336 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3337 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3338 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3339 | } |
| 3340 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3341 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3342 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3343 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3344 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3345 | goto vmentry_fail_vmexit; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3346 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | enter_guest_mode(vcpu); |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3350 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3351 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 3352 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3353 | if (prepare_vmcs02(vcpu, vmcs12, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3354 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3355 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3356 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3357 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3358 | |
| 3359 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3360 | failed_index = nested_vmx_load_msr(vcpu, |
| 3361 | vmcs12->vm_entry_msr_load_addr, |
| 3362 | vmcs12->vm_entry_msr_load_count); |
| 3363 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3364 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3365 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3366 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3367 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3368 | } else { |
| 3369 | /* |
| 3370 | * The MMU is not initialized to point at the right entities yet and |
| 3371 | * "get pages" would need to read data from the guest (i.e. we will |
| 3372 | * need to perform gpa to hpa translation). Request a call |
| 3373 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3374 | * have already been set at vmentry time and should not be reset. |
| 3375 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3376 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * If L1 had a pending IRQ/NMI until it executed |
| 3381 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3382 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3383 | * evaluate if this pending event should cause an exit from L2 |
| 3384 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3385 | * intercept EXTERNAL_INTERRUPT). |
| 3386 | * |
| 3387 | * Usually this would be handled by the processor noticing an |
| 3388 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3389 | * pending virtual interrupts. However, this setting was done |
| 3390 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3391 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3392 | */ |
| 3393 | if (unlikely(evaluate_pending_interrupts)) |
| 3394 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3395 | |
| 3396 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3397 | * Do not start the preemption timer hrtimer until after we know |
| 3398 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3399 | * the timer. |
| 3400 | */ |
| 3401 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3402 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3403 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3404 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3405 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3406 | |
| 3407 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3408 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3409 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3410 | * returned as far as L1 is concerned. It will only return (and set |
| 3411 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3412 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3413 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3414 | |
| 3415 | /* |
| 3416 | * A failed consistency check that leads to a VMExit during L1's |
| 3417 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3418 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3419 | */ |
| 3420 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3421 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3422 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3423 | leave_guest_mode(vcpu); |
| 3424 | |
| 3425 | vmentry_fail_vmexit: |
| 3426 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3427 | |
| 3428 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3429 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3430 | |
| 3431 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3432 | vmcs12->vm_exit_reason = exit_reason.full; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3433 | if (enable_shadow_vmcs || vmx->nested.hv_evmcs) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3434 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3435 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* |
| 3439 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3440 | * for running an L2 nested guest. |
| 3441 | */ |
| 3442 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3443 | { |
| 3444 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3445 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3446 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3447 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3448 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3449 | |
Dongli Zhang | 43c11d9 | 2021-03-05 14:57:47 -0800 | [diff] [blame] | 3450 | ++vcpu->stat.nested_run; |
| 3451 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3452 | if (!nested_vmx_check_permission(vcpu)) |
| 3453 | return 1; |
| 3454 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3455 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3456 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3457 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3458 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3459 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3460 | return nested_vmx_failInvalid(vcpu); |
| 3461 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3462 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3463 | if (CC(!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3464 | return nested_vmx_failInvalid(vcpu); |
| 3465 | |
| 3466 | vmcs12 = get_vmcs12(vcpu); |
| 3467 | |
| 3468 | /* |
| 3469 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3470 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3471 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3472 | * VM-instruction error field. |
| 3473 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3474 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3475 | return nested_vmx_failInvalid(vcpu); |
| 3476 | |
| 3477 | if (vmx->nested.hv_evmcs) { |
| 3478 | copy_enlightened_to_vmcs12(vmx); |
| 3479 | /* Enlightened VMCS doesn't have launch state */ |
| 3480 | vmcs12->launch_state = !launch; |
| 3481 | } else if (enable_shadow_vmcs) { |
| 3482 | copy_shadow_to_vmcs12(vmx); |
| 3483 | } |
| 3484 | |
| 3485 | /* |
| 3486 | * The nested entry process starts with enforcing various prerequisites |
| 3487 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3488 | * they fail: As the SDM explains, some conditions should cause the |
| 3489 | * instruction to fail, while others will cause the instruction to seem |
| 3490 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3491 | * To speed up the normal (success) code path, we should avoid checking |
| 3492 | * for misconfigurations which will anyway be caught by the processor |
| 3493 | * when using the merged vmcs02. |
| 3494 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3495 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3496 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3497 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3498 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3499 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3500 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3501 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3502 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3503 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3504 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3505 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3506 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3507 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3508 | |
| 3509 | /* |
| 3510 | * We're finally done with prerequisite checking, and can start with |
| 3511 | * the nested entry. |
| 3512 | */ |
| 3513 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3514 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3515 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3516 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3517 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3518 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3519 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3520 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3521 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3522 | vmx->nested.pi_pending = true; |
| 3523 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3524 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3525 | } |
| 3526 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3527 | /* Hide L1D cache contents from the nested guest. */ |
| 3528 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3529 | |
| 3530 | /* |
| 3531 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3532 | * also be used as part of restoring nVMX state for |
| 3533 | * snapshot restore (migration). |
| 3534 | * |
| 3535 | * In this flow, it is assumed that vmcs12 cache was |
| 3536 | * trasferred as part of captured nVMX state and should |
| 3537 | * therefore not be read from guest memory (which may not |
| 3538 | * exist on destination host yet). |
| 3539 | */ |
| 3540 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3541 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3542 | switch (vmcs12->guest_activity_state) { |
| 3543 | case GUEST_ACTIVITY_HLT: |
| 3544 | /* |
| 3545 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3546 | * awakened by event injection or by an NMI-window VM-exit or |
| 3547 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3548 | */ |
| 3549 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3550 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3551 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3552 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3553 | vmx->nested.nested_run_pending = 0; |
| 3554 | return kvm_vcpu_halt(vcpu); |
| 3555 | } |
| 3556 | break; |
| 3557 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3558 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3559 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3560 | break; |
| 3561 | default: |
| 3562 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3563 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3564 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3565 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3566 | |
| 3567 | vmentry_failed: |
| 3568 | vmx->nested.nested_run_pending = 0; |
| 3569 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3570 | return 0; |
| 3571 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3572 | return 1; |
| 3573 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3574 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * 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] | 3579 | * 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] | 3580 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3581 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3582 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3583 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3584 | * didn't trap the bit, because if L1 did, so would L0). |
| 3585 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3586 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3587 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3588 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3589 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3590 | * changed these bits, and therefore they need to be updated, but L0 |
| 3591 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3592 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3593 | */ |
| 3594 | static inline unsigned long |
| 3595 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3596 | { |
| 3597 | return |
| 3598 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3599 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3600 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3601 | vcpu->arch.cr0_guest_owned_bits)); |
| 3602 | } |
| 3603 | |
| 3604 | static inline unsigned long |
| 3605 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3606 | { |
| 3607 | return |
| 3608 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3609 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3610 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3611 | vcpu->arch.cr4_guest_owned_bits)); |
| 3612 | } |
| 3613 | |
| 3614 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3615 | struct vmcs12 *vmcs12) |
| 3616 | { |
| 3617 | u32 idt_vectoring; |
| 3618 | unsigned int nr; |
| 3619 | |
| 3620 | if (vcpu->arch.exception.injected) { |
| 3621 | nr = vcpu->arch.exception.nr; |
| 3622 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3623 | |
| 3624 | if (kvm_exception_is_soft(nr)) { |
| 3625 | vmcs12->vm_exit_instruction_len = |
| 3626 | vcpu->arch.event_exit_inst_len; |
| 3627 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3628 | } else |
| 3629 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3630 | |
| 3631 | if (vcpu->arch.exception.has_error_code) { |
| 3632 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3633 | vmcs12->idt_vectoring_error_code = |
| 3634 | vcpu->arch.exception.error_code; |
| 3635 | } |
| 3636 | |
| 3637 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3638 | } else if (vcpu->arch.nmi_injected) { |
| 3639 | vmcs12->idt_vectoring_info_field = |
| 3640 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3641 | } else if (vcpu->arch.interrupt.injected) { |
| 3642 | nr = vcpu->arch.interrupt.nr; |
| 3643 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3644 | |
| 3645 | if (vcpu->arch.interrupt.soft) { |
| 3646 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3647 | vmcs12->vm_entry_instruction_len = |
| 3648 | vcpu->arch.event_exit_inst_len; |
| 3649 | } else |
| 3650 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3651 | |
| 3652 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3657 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3658 | { |
| 3659 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3660 | gfn_t gfn; |
| 3661 | |
| 3662 | /* |
| 3663 | * Don't need to mark the APIC access page dirty; it is never |
| 3664 | * written to by the CPU during APIC virtualization. |
| 3665 | */ |
| 3666 | |
| 3667 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3668 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3669 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3670 | } |
| 3671 | |
| 3672 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3673 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3674 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
| 3679 | { |
| 3680 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3681 | int max_irr; |
| 3682 | void *vapic_page; |
| 3683 | u16 status; |
| 3684 | |
| 3685 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 3686 | return; |
| 3687 | |
| 3688 | vmx->nested.pi_pending = false; |
| 3689 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 3690 | return; |
| 3691 | |
| 3692 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3693 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3694 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3695 | if (!vapic_page) |
| 3696 | return; |
| 3697 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3698 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3699 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3700 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3701 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3702 | status &= ~0xff; |
| 3703 | status |= (u8)max_irr; |
| 3704 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 3709 | } |
| 3710 | |
| 3711 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3712 | unsigned long exit_qual) |
| 3713 | { |
| 3714 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3715 | unsigned int nr = vcpu->arch.exception.nr; |
| 3716 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3717 | |
| 3718 | if (vcpu->arch.exception.has_error_code) { |
| 3719 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3720 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3721 | } |
| 3722 | |
| 3723 | if (kvm_exception_is_soft(nr)) |
| 3724 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3725 | else |
| 3726 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3727 | |
| 3728 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3729 | vmx_get_nmi_mask(vcpu)) |
| 3730 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3731 | |
| 3732 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3733 | } |
| 3734 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3735 | /* |
| 3736 | * Returns true if a debug trap is pending delivery. |
| 3737 | * |
| 3738 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3739 | * exception may be inferred from the presence of an exception payload. |
| 3740 | */ |
| 3741 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3742 | { |
| 3743 | return vcpu->arch.exception.pending && |
| 3744 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3745 | vcpu->arch.exception.payload; |
| 3746 | } |
| 3747 | |
| 3748 | /* |
| 3749 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3750 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3751 | * represents these debug traps with a payload that is said to be compatible |
| 3752 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3753 | * field if a VM-exit is delivered before the debug trap. |
| 3754 | */ |
| 3755 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3756 | { |
| 3757 | if (vmx_pending_dbg_trap(vcpu)) |
| 3758 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3759 | vcpu->arch.exception.payload); |
| 3760 | } |
| 3761 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3762 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3763 | { |
| 3764 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3765 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3766 | } |
| 3767 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3768 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3769 | { |
| 3770 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3771 | unsigned long exit_qual; |
| 3772 | bool block_nested_events = |
| 3773 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3774 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3775 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3776 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3777 | /* |
| 3778 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3779 | * this state is discarded. |
| 3780 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3781 | if (!block_nested_events) |
| 3782 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3783 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3784 | if (lapic_in_kernel(vcpu) && |
| 3785 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3786 | if (block_nested_events) |
| 3787 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3788 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3789 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3790 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3791 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3792 | return 0; |
| 3793 | } |
| 3794 | |
| 3795 | if (lapic_in_kernel(vcpu) && |
| 3796 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3797 | if (block_nested_events) |
| 3798 | return -EBUSY; |
| 3799 | |
| 3800 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3801 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3802 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3803 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3804 | return 0; |
| 3805 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3806 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3807 | /* |
| 3808 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3809 | * |
| 3810 | * Note that only a pending nested run can block a pending exception. |
| 3811 | * Otherwise an injected NMI/interrupt should either be |
| 3812 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3813 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3814 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3815 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3816 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3817 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3818 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3819 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3820 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3821 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3822 | return 0; |
| 3823 | } |
| 3824 | |
| 3825 | if (mtf_pending) { |
| 3826 | if (block_nested_events) |
| 3827 | return -EBUSY; |
| 3828 | nested_vmx_update_pending_dbg(vcpu); |
| 3829 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3830 | return 0; |
| 3831 | } |
| 3832 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3833 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3834 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3835 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3836 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3837 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3838 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3839 | return 0; |
| 3840 | } |
| 3841 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3842 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3843 | if (block_nested_events) |
| 3844 | return -EBUSY; |
| 3845 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3846 | return 0; |
| 3847 | } |
| 3848 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3849 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3850 | if (block_nested_events) |
| 3851 | return -EBUSY; |
| 3852 | goto no_vmexit; |
| 3853 | } |
| 3854 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3855 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3856 | if (block_nested_events) |
| 3857 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3858 | if (!nested_exit_on_nmi(vcpu)) |
| 3859 | goto no_vmexit; |
| 3860 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3861 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3862 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3863 | INTR_INFO_VALID_MASK, 0); |
| 3864 | /* |
| 3865 | * The NMI-triggered VM exit counts as injection: |
| 3866 | * clear this one and block further NMIs. |
| 3867 | */ |
| 3868 | vcpu->arch.nmi_pending = 0; |
| 3869 | vmx_set_nmi_mask(vcpu, true); |
| 3870 | return 0; |
| 3871 | } |
| 3872 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3873 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3874 | if (block_nested_events) |
| 3875 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3876 | if (!nested_exit_on_intr(vcpu)) |
| 3877 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3878 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3879 | return 0; |
| 3880 | } |
| 3881 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3882 | no_vmexit: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3883 | vmx_complete_nested_posted_interrupt(vcpu); |
| 3884 | return 0; |
| 3885 | } |
| 3886 | |
| 3887 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3888 | { |
| 3889 | ktime_t remaining = |
| 3890 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3891 | u64 value; |
| 3892 | |
| 3893 | if (ktime_to_ns(remaining) <= 0) |
| 3894 | return 0; |
| 3895 | |
| 3896 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3897 | do_div(value, 1000000); |
| 3898 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3899 | } |
| 3900 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3901 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3902 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3903 | switch (field) { |
| 3904 | case GUEST_ES_SELECTOR: |
| 3905 | case GUEST_CS_SELECTOR: |
| 3906 | case GUEST_SS_SELECTOR: |
| 3907 | case GUEST_DS_SELECTOR: |
| 3908 | case GUEST_FS_SELECTOR: |
| 3909 | case GUEST_GS_SELECTOR: |
| 3910 | case GUEST_LDTR_SELECTOR: |
| 3911 | case GUEST_TR_SELECTOR: |
| 3912 | case GUEST_ES_LIMIT: |
| 3913 | case GUEST_CS_LIMIT: |
| 3914 | case GUEST_SS_LIMIT: |
| 3915 | case GUEST_DS_LIMIT: |
| 3916 | case GUEST_FS_LIMIT: |
| 3917 | case GUEST_GS_LIMIT: |
| 3918 | case GUEST_LDTR_LIMIT: |
| 3919 | case GUEST_TR_LIMIT: |
| 3920 | case GUEST_GDTR_LIMIT: |
| 3921 | case GUEST_IDTR_LIMIT: |
| 3922 | case GUEST_ES_AR_BYTES: |
| 3923 | case GUEST_DS_AR_BYTES: |
| 3924 | case GUEST_FS_AR_BYTES: |
| 3925 | case GUEST_GS_AR_BYTES: |
| 3926 | case GUEST_LDTR_AR_BYTES: |
| 3927 | case GUEST_TR_AR_BYTES: |
| 3928 | case GUEST_ES_BASE: |
| 3929 | case GUEST_CS_BASE: |
| 3930 | case GUEST_SS_BASE: |
| 3931 | case GUEST_DS_BASE: |
| 3932 | case GUEST_FS_BASE: |
| 3933 | case GUEST_GS_BASE: |
| 3934 | case GUEST_LDTR_BASE: |
| 3935 | case GUEST_TR_BASE: |
| 3936 | case GUEST_GDTR_BASE: |
| 3937 | case GUEST_IDTR_BASE: |
| 3938 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 3939 | case GUEST_BNDCFGS: |
| 3940 | return true; |
| 3941 | default: |
| 3942 | break; |
| 3943 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3944 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3945 | return false; |
| 3946 | } |
| 3947 | |
| 3948 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 3949 | struct vmcs12 *vmcs12) |
| 3950 | { |
| 3951 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3952 | |
| 3953 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 3954 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 3955 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 3956 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 3957 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 3958 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 3959 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 3960 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 3961 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 3962 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 3963 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 3964 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 3965 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 3966 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 3967 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 3968 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 3969 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3970 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3971 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3972 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 3973 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 3974 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 3975 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 3976 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 3977 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 3978 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 3979 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 3980 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 3981 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 3982 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 3983 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 3984 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 3985 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 3986 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3987 | vmcs12->guest_pending_dbg_exceptions = |
| 3988 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 3989 | if (kvm_mpx_supported()) |
| 3990 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3991 | |
| 3992 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 3993 | } |
| 3994 | |
| 3995 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 3996 | struct vmcs12 *vmcs12) |
| 3997 | { |
| 3998 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3999 | int cpu; |
| 4000 | |
| 4001 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4002 | return; |
| 4003 | |
| 4004 | |
| 4005 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4006 | |
| 4007 | cpu = get_cpu(); |
| 4008 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4009 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4010 | |
| 4011 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4012 | |
| 4013 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4014 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4015 | put_cpu(); |
| 4016 | } |
| 4017 | |
| 4018 | /* |
| 4019 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4020 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4021 | * VM-entry controls is also updated, since this is really a guest |
| 4022 | * state bit.) |
| 4023 | */ |
| 4024 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4025 | { |
| 4026 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4027 | |
| 4028 | if (vmx->nested.hv_evmcs) |
| 4029 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4030 | |
| 4031 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = !vmx->nested.hv_evmcs; |
| 4032 | |
| 4033 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4034 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4035 | |
| 4036 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4037 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4038 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4039 | |
| 4040 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4041 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4042 | |
| 4043 | vmcs12->guest_interruptibility_info = |
| 4044 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4045 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4046 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4047 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4048 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4049 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4050 | else |
| 4051 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4052 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4053 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4054 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4055 | !vmx->nested.nested_run_pending) |
| 4056 | vmcs12->vmx_preemption_timer_value = |
| 4057 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4058 | |
| 4059 | /* |
| 4060 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4061 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4062 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4063 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4064 | * |
| 4065 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4066 | */ |
| 4067 | if (enable_ept) { |
| 4068 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4069 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4070 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4071 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4072 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4073 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4074 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4078 | |
| 4079 | if (nested_cpu_has_vid(vmcs12)) |
| 4080 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4081 | |
| 4082 | vmcs12->vm_entry_controls = |
| 4083 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4084 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4085 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4086 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4087 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4088 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4089 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4090 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | /* |
| 4094 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4095 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4096 | * and this function updates it to reflect the changes to the guest state while |
| 4097 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4098 | * without going back to L1), and to reflect the exit reason. |
| 4099 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4100 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4101 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4102 | * which already writes to vmcs12 directly. |
| 4103 | */ |
| 4104 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4105 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4106 | unsigned long exit_qualification) |
| 4107 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4108 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4109 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame^] | 4110 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4111 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4112 | vmcs12->exit_qualification = exit_qualification; |
| 4113 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4114 | |
| 4115 | vmcs12->idt_vectoring_info_field = 0; |
| 4116 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4117 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4118 | |
| 4119 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4120 | vmcs12->launch_state = 1; |
| 4121 | |
| 4122 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4123 | * instead of reading the real value. */ |
| 4124 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4125 | |
| 4126 | /* |
| 4127 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4128 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4129 | */ |
| 4130 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4131 | |
| 4132 | /* |
| 4133 | * According to spec, there's no need to store the guest's |
| 4134 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4135 | * during or after loading the guest state. Since this exit |
| 4136 | * does not fall in that category, we need to save the MSRs. |
| 4137 | */ |
| 4138 | if (nested_vmx_store_msr(vcpu, |
| 4139 | vmcs12->vm_exit_msr_store_addr, |
| 4140 | vmcs12->vm_exit_msr_store_count)) |
| 4141 | nested_vmx_abort(vcpu, |
| 4142 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | /* |
| 4146 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4147 | * preserved above and would only end up incorrectly in L1. |
| 4148 | */ |
| 4149 | vcpu->arch.nmi_injected = false; |
| 4150 | kvm_clear_exception_queue(vcpu); |
| 4151 | kvm_clear_interrupt_queue(vcpu); |
| 4152 | } |
| 4153 | |
| 4154 | /* |
| 4155 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4156 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4157 | * in vmcs12. |
| 4158 | * This function is to be called not only on normal nested exit, but also on |
| 4159 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4160 | * Failures During or After Loading Guest State"). |
| 4161 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4162 | */ |
| 4163 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4164 | struct vmcs12 *vmcs12) |
| 4165 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4166 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4167 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4168 | |
| 4169 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4170 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4171 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4172 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4173 | else |
| 4174 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4175 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4176 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4177 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4178 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4179 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4180 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4181 | |
| 4182 | /* |
| 4183 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4184 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4185 | * |
| 4186 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4187 | * (KVM doesn't change it); |
| 4188 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4189 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4190 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4191 | |
| 4192 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4193 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4194 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4195 | |
| 4196 | nested_ept_uninit_mmu_context(vcpu); |
| 4197 | |
| 4198 | /* |
| 4199 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4200 | * couldn't have changed. |
| 4201 | */ |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4202 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4203 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4204 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4205 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4206 | |
| 4207 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4208 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4209 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4210 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4211 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4212 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4213 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4214 | |
| 4215 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4216 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4217 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4218 | |
| 4219 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4220 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4221 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4222 | } |
| 4223 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4224 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4225 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4226 | |
| 4227 | /* Set L1 segment info according to Intel SDM |
| 4228 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4229 | seg = (struct kvm_segment) { |
| 4230 | .base = 0, |
| 4231 | .limit = 0xFFFFFFFF, |
| 4232 | .selector = vmcs12->host_cs_selector, |
| 4233 | .type = 11, |
| 4234 | .present = 1, |
| 4235 | .s = 1, |
| 4236 | .g = 1 |
| 4237 | }; |
| 4238 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4239 | seg.l = 1; |
| 4240 | else |
| 4241 | seg.db = 1; |
| 4242 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 4243 | seg = (struct kvm_segment) { |
| 4244 | .base = 0, |
| 4245 | .limit = 0xFFFFFFFF, |
| 4246 | .type = 3, |
| 4247 | .present = 1, |
| 4248 | .s = 1, |
| 4249 | .db = 1, |
| 4250 | .g = 1 |
| 4251 | }; |
| 4252 | seg.selector = vmcs12->host_ds_selector; |
| 4253 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 4254 | seg.selector = vmcs12->host_es_selector; |
| 4255 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 4256 | seg.selector = vmcs12->host_ss_selector; |
| 4257 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 4258 | seg.selector = vmcs12->host_fs_selector; |
| 4259 | seg.base = vmcs12->host_fs_base; |
| 4260 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 4261 | seg.selector = vmcs12->host_gs_selector; |
| 4262 | seg.base = vmcs12->host_gs_base; |
| 4263 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 4264 | seg = (struct kvm_segment) { |
| 4265 | .base = vmcs12->host_tr_base, |
| 4266 | .limit = 0x67, |
| 4267 | .selector = vmcs12->host_tr_selector, |
| 4268 | .type = 11, |
| 4269 | .present = 1 |
| 4270 | }; |
| 4271 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 4272 | |
| 4273 | kvm_set_dr(vcpu, 7, 0x400); |
| 4274 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4275 | |
| 4276 | if (cpu_has_vmx_msr_bitmap()) |
| 4277 | vmx_update_msr_bitmap(vcpu); |
| 4278 | |
| 4279 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4280 | vmcs12->vm_exit_msr_load_count)) |
| 4281 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4282 | } |
| 4283 | |
| 4284 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4285 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4286 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4287 | unsigned int i; |
| 4288 | |
| 4289 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4290 | return vmcs_read64(GUEST_IA32_EFER); |
| 4291 | |
| 4292 | if (cpu_has_load_ia32_efer()) |
| 4293 | return host_efer; |
| 4294 | |
| 4295 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4296 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4297 | return vmx->msr_autoload.guest.val[i].value; |
| 4298 | } |
| 4299 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4300 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4301 | if (efer_msr) |
| 4302 | return efer_msr->data; |
| 4303 | |
| 4304 | return host_efer; |
| 4305 | } |
| 4306 | |
| 4307 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4308 | { |
| 4309 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4310 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4311 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4312 | gpa_t gpa; |
| 4313 | u32 i, j; |
| 4314 | |
| 4315 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4316 | |
| 4317 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4318 | /* |
| 4319 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4320 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4321 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4322 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4323 | */ |
| 4324 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4325 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4326 | else |
| 4327 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4328 | } |
| 4329 | |
| 4330 | /* |
| 4331 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4332 | * handle a variety of side effects to KVM's software model. |
| 4333 | */ |
| 4334 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4335 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4336 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4337 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4338 | |
| 4339 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4340 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4341 | |
| 4342 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4343 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4344 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4345 | |
| 4346 | /* |
| 4347 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4348 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4349 | * VMFail, like everything else we just need to ensure our |
| 4350 | * software model is up-to-date. |
| 4351 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4352 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4353 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4354 | |
| 4355 | kvm_mmu_reset_context(vcpu); |
| 4356 | |
| 4357 | if (cpu_has_vmx_msr_bitmap()) |
| 4358 | vmx_update_msr_bitmap(vcpu); |
| 4359 | |
| 4360 | /* |
| 4361 | * This nasty bit of open coding is a compromise between blindly |
| 4362 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4363 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4364 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4365 | * expensive since the lists are unbound by hardware). For each |
| 4366 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4367 | * list, reload it from the exit load list if it exists and differs |
| 4368 | * from the guest value. The intent is to stuff host state as |
| 4369 | * silently as possible, not to fully process the exit load list. |
| 4370 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4371 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4372 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4373 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4374 | pr_debug_ratelimited( |
| 4375 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4376 | __func__, i, gpa); |
| 4377 | goto vmabort; |
| 4378 | } |
| 4379 | |
| 4380 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4381 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4382 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4383 | pr_debug_ratelimited( |
| 4384 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4385 | __func__, j, gpa); |
| 4386 | goto vmabort; |
| 4387 | } |
| 4388 | if (h.index != g.index) |
| 4389 | continue; |
| 4390 | if (h.value == g.value) |
| 4391 | break; |
| 4392 | |
| 4393 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4394 | pr_debug_ratelimited( |
| 4395 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4396 | __func__, j, h.index, h.reserved); |
| 4397 | goto vmabort; |
| 4398 | } |
| 4399 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4400 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4401 | pr_debug_ratelimited( |
| 4402 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4403 | __func__, j, h.index, h.value); |
| 4404 | goto vmabort; |
| 4405 | } |
| 4406 | } |
| 4407 | } |
| 4408 | |
| 4409 | return; |
| 4410 | |
| 4411 | vmabort: |
| 4412 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4413 | } |
| 4414 | |
| 4415 | /* |
| 4416 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4417 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4418 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4419 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4420 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4421 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4422 | { |
| 4423 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4424 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4425 | |
| 4426 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4427 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4428 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4429 | /* Similarly, triple faults in L2 should never escape. */ |
| 4430 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4431 | |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4432 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
| 4433 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4434 | /* Service the TLB flush request for L2 before switching to L1. */ |
| 4435 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 4436 | kvm_vcpu_flush_tlb_current(vcpu); |
| 4437 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4438 | /* |
| 4439 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4440 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4441 | * up-to-date before switching to L1. |
| 4442 | */ |
| 4443 | if (enable_ept && is_pae_paging(vcpu)) |
| 4444 | vmx_ept_load_pdptrs(vcpu); |
| 4445 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4446 | leave_guest_mode(vcpu); |
| 4447 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4448 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4449 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4450 | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 4451 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4452 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 4453 | |
| 4454 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4455 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4456 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4457 | if (vm_exit_reason != -1) |
| 4458 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4459 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4460 | |
| 4461 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4462 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4463 | * also be used to capture vmcs12 cache as part of |
| 4464 | * capturing nVMX state for snapshot (migration). |
| 4465 | * |
| 4466 | * Otherwise, this flush will dirty guest memory at a |
| 4467 | * point it is already assumed by user-space to be |
| 4468 | * immutable. |
| 4469 | */ |
| 4470 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4471 | } else { |
| 4472 | /* |
| 4473 | * The only expected VM-instruction error is "VM entry with |
| 4474 | * invalid control field(s)." Anything else indicates a |
| 4475 | * problem with L0. And we should never get here with a |
| 4476 | * VMFail of any type if early consistency checks are enabled. |
| 4477 | */ |
| 4478 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4479 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4480 | WARN_ON_ONCE(nested_early_check); |
| 4481 | } |
| 4482 | |
| 4483 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4484 | |
| 4485 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4486 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4487 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4488 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4489 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4490 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4491 | |
| 4492 | if (kvm_has_tsc_control) |
| 4493 | decache_tsc_multiplier(vmx); |
| 4494 | |
| 4495 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4496 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4497 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4498 | } |
| 4499 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4500 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4501 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4502 | vmx_update_cpu_dirty_logging(vcpu); |
| 4503 | } |
| 4504 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4505 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4506 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4507 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4508 | vmx->nested.apic_access_page = NULL; |
| 4509 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4510 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4511 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4512 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4513 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4514 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4515 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4516 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4517 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4518 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4519 | if ((vm_exit_reason != -1) && |
| 4520 | (enable_shadow_vmcs || vmx->nested.hv_evmcs)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4521 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4522 | |
| 4523 | /* in case we halted in L2 */ |
| 4524 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4525 | |
| 4526 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4527 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4528 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4529 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4530 | WARN_ON(irq < 0); |
| 4531 | vmcs12->vm_exit_intr_info = irq | |
| 4532 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4533 | } |
| 4534 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4535 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4536 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4537 | vmcs12->exit_qualification, |
| 4538 | vmcs12->idt_vectoring_info_field, |
| 4539 | vmcs12->vm_exit_intr_info, |
| 4540 | vmcs12->vm_exit_intr_error_code, |
| 4541 | KVM_ISA_VMX); |
| 4542 | |
| 4543 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4544 | |
| 4545 | return; |
| 4546 | } |
| 4547 | |
| 4548 | /* |
| 4549 | * After an early L2 VM-entry failure, we're now back |
| 4550 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4551 | * VMRESUME instruction, so we need to set the failure |
| 4552 | * flag and the VM-instruction error field of the VMCS |
| 4553 | * accordingly, and skip the emulated instruction. |
| 4554 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4555 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4556 | |
| 4557 | /* |
| 4558 | * Restore L1's host state to KVM's software model. We're here |
| 4559 | * because a consistency check was caught by hardware, which |
| 4560 | * means some amount of guest state has been propagated to KVM's |
| 4561 | * model and needs to be unwound to the host's state. |
| 4562 | */ |
| 4563 | nested_vmx_restore_host_state(vcpu); |
| 4564 | |
| 4565 | vmx->fail = 0; |
| 4566 | } |
| 4567 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4568 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4569 | { |
| 4570 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4571 | } |
| 4572 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4573 | /* |
| 4574 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4575 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4576 | * 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] | 4577 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4578 | */ |
| 4579 | 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] | 4580 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4581 | { |
| 4582 | gva_t off; |
| 4583 | bool exn; |
| 4584 | struct kvm_segment s; |
| 4585 | |
| 4586 | /* |
| 4587 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4588 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4589 | * addressing components of the operand. Only the displacement part |
| 4590 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4591 | * For how an actual address is calculated from all these components, |
| 4592 | * refer to Vol. 1, "Operand Addressing". |
| 4593 | */ |
| 4594 | int scaling = vmx_instruction_info & 3; |
| 4595 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4596 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4597 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4598 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4599 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4600 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4601 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4602 | |
| 4603 | if (is_reg) { |
| 4604 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4605 | return 1; |
| 4606 | } |
| 4607 | |
| 4608 | /* Addr = segment_base + offset */ |
| 4609 | /* offset = base + [index * scale] + displacement */ |
| 4610 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4611 | if (addr_size == 1) |
| 4612 | off = (gva_t)sign_extend64(off, 31); |
| 4613 | else if (addr_size == 0) |
| 4614 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4615 | if (base_is_valid) |
| 4616 | off += kvm_register_read(vcpu, base_reg); |
| 4617 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4618 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4619 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4620 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4621 | /* |
| 4622 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4623 | * based on the address size of the instruction. Note that this is |
| 4624 | * the *effective address*, i.e. the address prior to accounting for |
| 4625 | * the segment's base. |
| 4626 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4627 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4628 | off &= 0xffffffff; |
| 4629 | else if (addr_size == 0) /* 16 bit */ |
| 4630 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4631 | |
| 4632 | /* Checks for #GP/#SS exceptions. */ |
| 4633 | exn = false; |
| 4634 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4635 | /* |
| 4636 | * The virtual/linear address is never truncated in 64-bit |
| 4637 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4638 | * address when using FS/GS with a non-zero base. |
| 4639 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4640 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4641 | *ret = s.base + off; |
| 4642 | else |
| 4643 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4644 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4645 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4646 | * non-canonical form. This is the only check on the memory |
| 4647 | * destination for long mode! |
| 4648 | */ |
| 4649 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4650 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4651 | /* |
| 4652 | * When not in long mode, the virtual/linear address is |
| 4653 | * unconditionally truncated to 32 bits regardless of the |
| 4654 | * address size. |
| 4655 | */ |
| 4656 | *ret = (s.base + off) & 0xffffffff; |
| 4657 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4658 | /* Protected mode: apply checks for segment validity in the |
| 4659 | * following order: |
| 4660 | * - segment type check (#GP(0) may be thrown) |
| 4661 | * - usability check (#GP(0)/#SS(0)) |
| 4662 | * - limit check (#GP(0)/#SS(0)) |
| 4663 | */ |
| 4664 | if (wr) |
| 4665 | /* #GP(0) if the destination operand is located in a |
| 4666 | * read-only data segment or any code segment. |
| 4667 | */ |
| 4668 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4669 | else |
| 4670 | /* #GP(0) if the source operand is located in an |
| 4671 | * execute-only code segment |
| 4672 | */ |
| 4673 | exn = ((s.type & 0xa) == 8); |
| 4674 | if (exn) { |
| 4675 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4676 | return 1; |
| 4677 | } |
| 4678 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4679 | */ |
| 4680 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4681 | |
| 4682 | /* |
| 4683 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4684 | * outside the segment limit. All CPUs that support VMX ignore |
| 4685 | * limit checks for flat segments, i.e. segments with base==0, |
| 4686 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4687 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4688 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4689 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4690 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4691 | } |
| 4692 | if (exn) { |
| 4693 | kvm_queue_exception_e(vcpu, |
| 4694 | seg_reg == VCPU_SREG_SS ? |
| 4695 | SS_VECTOR : GP_VECTOR, |
| 4696 | 0); |
| 4697 | return 1; |
| 4698 | } |
| 4699 | |
| 4700 | return 0; |
| 4701 | } |
| 4702 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4703 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4704 | { |
| 4705 | struct vcpu_vmx *vmx; |
| 4706 | |
| 4707 | if (!nested_vmx_allowed(vcpu)) |
| 4708 | return; |
| 4709 | |
| 4710 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4711 | 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] | 4712 | vmx->nested.msrs.entry_ctls_high |= |
| 4713 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4714 | vmx->nested.msrs.exit_ctls_high |= |
| 4715 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4716 | } else { |
| 4717 | vmx->nested.msrs.entry_ctls_high &= |
| 4718 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4719 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4720 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4721 | } |
| 4722 | } |
| 4723 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4724 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4725 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4726 | { |
| 4727 | gva_t gva; |
| 4728 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4729 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4730 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4731 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4732 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4733 | sizeof(*vmpointer), &gva)) { |
| 4734 | *ret = 1; |
| 4735 | return -EINVAL; |
| 4736 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4737 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4738 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4739 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4740 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4741 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | return 0; |
| 4745 | } |
| 4746 | |
| 4747 | /* |
| 4748 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4749 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4750 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4751 | */ |
| 4752 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4753 | { |
| 4754 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4755 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4756 | |
| 4757 | /* |
| 4758 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4759 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4760 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4761 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4762 | */ |
| 4763 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4764 | |
| 4765 | if (!loaded_vmcs->shadow_vmcs) { |
| 4766 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4767 | if (loaded_vmcs->shadow_vmcs) |
| 4768 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4769 | } |
| 4770 | return loaded_vmcs->shadow_vmcs; |
| 4771 | } |
| 4772 | |
| 4773 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4774 | { |
| 4775 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4776 | int r; |
| 4777 | |
| 4778 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4779 | if (r < 0) |
| 4780 | goto out_vmcs02; |
| 4781 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4782 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4783 | if (!vmx->nested.cached_vmcs12) |
| 4784 | goto out_cached_vmcs12; |
| 4785 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4786 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4787 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4788 | goto out_cached_shadow_vmcs12; |
| 4789 | |
| 4790 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4791 | goto out_shadow_vmcs; |
| 4792 | |
| 4793 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4794 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4795 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4796 | |
| 4797 | vmx->nested.vpid02 = allocate_vpid(); |
| 4798 | |
| 4799 | vmx->nested.vmcs02_initialized = false; |
| 4800 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4801 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4802 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4803 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4804 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4805 | } |
| 4806 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4807 | return 0; |
| 4808 | |
| 4809 | out_shadow_vmcs: |
| 4810 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4811 | |
| 4812 | out_cached_shadow_vmcs12: |
| 4813 | kfree(vmx->nested.cached_vmcs12); |
| 4814 | |
| 4815 | out_cached_vmcs12: |
| 4816 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4817 | |
| 4818 | out_vmcs02: |
| 4819 | return -ENOMEM; |
| 4820 | } |
| 4821 | |
| 4822 | /* |
| 4823 | * Emulate the VMXON instruction. |
| 4824 | * Currently, we just remember that VMX is active, and do not save or even |
| 4825 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 4826 | * do not currently need to store anything in that guest-allocated memory |
| 4827 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 4828 | * argument is different from the VMXON pointer (which the spec says they do). |
| 4829 | */ |
| 4830 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4831 | { |
| 4832 | int ret; |
| 4833 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4834 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4835 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4836 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4837 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4838 | |
| 4839 | /* |
| 4840 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4841 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4842 | * 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] | 4843 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4844 | * have already been checked by hardware, prior to the VM-exit for |
| 4845 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4846 | * that bit set to 1 in non-root mode. |
| 4847 | */ |
| 4848 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4849 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4850 | return 1; |
| 4851 | } |
| 4852 | |
| 4853 | /* CPL=0 must be checked manually. */ |
| 4854 | if (vmx_get_cpl(vcpu)) { |
| 4855 | kvm_inject_gp(vcpu, 0); |
| 4856 | return 1; |
| 4857 | } |
| 4858 | |
| 4859 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4860 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4861 | |
| 4862 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4863 | != VMXON_NEEDED_FEATURES) { |
| 4864 | kvm_inject_gp(vcpu, 0); |
| 4865 | return 1; |
| 4866 | } |
| 4867 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4868 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4869 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4870 | |
| 4871 | /* |
| 4872 | * SDM 3: 24.11.5 |
| 4873 | * The first 4 bytes of VMXON region contain the supported |
| 4874 | * VMCS revision identifier |
| 4875 | * |
| 4876 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4877 | * which replaces physical address width with 32 |
| 4878 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4879 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4880 | return nested_vmx_failInvalid(vcpu); |
| 4881 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4882 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4883 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4884 | return nested_vmx_failInvalid(vcpu); |
| 4885 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4886 | vmx->nested.vmxon_ptr = vmptr; |
| 4887 | ret = enter_vmx_operation(vcpu); |
| 4888 | if (ret) |
| 4889 | return ret; |
| 4890 | |
| 4891 | return nested_vmx_succeed(vcpu); |
| 4892 | } |
| 4893 | |
| 4894 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4895 | { |
| 4896 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4897 | |
| 4898 | if (vmx->nested.current_vmptr == -1ull) |
| 4899 | return; |
| 4900 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4901 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4902 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4903 | if (enable_shadow_vmcs) { |
| 4904 | /* copy to memory all shadowed fields in case |
| 4905 | they were modified */ |
| 4906 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4907 | vmx_disable_shadow_vmcs(vmx); |
| 4908 | } |
| 4909 | vmx->nested.posted_intr_nv = -1; |
| 4910 | |
| 4911 | /* Flush VMCS12 to guest memory */ |
| 4912 | kvm_vcpu_write_guest_page(vcpu, |
| 4913 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4914 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4915 | |
| 4916 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4917 | |
| 4918 | vmx->nested.current_vmptr = -1ull; |
| 4919 | } |
| 4920 | |
| 4921 | /* Emulate the VMXOFF instruction */ |
| 4922 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4923 | { |
| 4924 | if (!nested_vmx_check_permission(vcpu)) |
| 4925 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4926 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4927 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4928 | |
| 4929 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 4930 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4931 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4932 | return nested_vmx_succeed(vcpu); |
| 4933 | } |
| 4934 | |
| 4935 | /* Emulate the VMCLEAR instruction */ |
| 4936 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 4937 | { |
| 4938 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4939 | u32 zero = 0; |
| 4940 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4941 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4942 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4943 | |
| 4944 | if (!nested_vmx_check_permission(vcpu)) |
| 4945 | return 1; |
| 4946 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4947 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 4948 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4949 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4950 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4951 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4952 | |
| 4953 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4954 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4955 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4956 | /* |
| 4957 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 4958 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 4959 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 4960 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 4961 | * have to be the currently active EVMCS on the calling CPU and there's |
| 4962 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 4963 | * state. It is possible that the area will stay mapped as |
| 4964 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 4965 | */ |
| 4966 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 4967 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4968 | if (vmptr == vmx->nested.current_vmptr) |
| 4969 | nested_release_vmcs12(vcpu); |
| 4970 | |
| 4971 | kvm_vcpu_write_guest(vcpu, |
| 4972 | vmptr + offsetof(struct vmcs12, |
| 4973 | launch_state), |
| 4974 | &zero, sizeof(zero)); |
| 4975 | } |
| 4976 | |
| 4977 | return nested_vmx_succeed(vcpu); |
| 4978 | } |
| 4979 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4980 | /* Emulate the VMLAUNCH instruction */ |
| 4981 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 4982 | { |
| 4983 | return nested_vmx_run(vcpu, true); |
| 4984 | } |
| 4985 | |
| 4986 | /* Emulate the VMRESUME instruction */ |
| 4987 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 4988 | { |
| 4989 | |
| 4990 | return nested_vmx_run(vcpu, false); |
| 4991 | } |
| 4992 | |
| 4993 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 4994 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 4995 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 4996 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4997 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 4998 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4999 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5000 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5001 | unsigned long field; |
| 5002 | u64 value; |
| 5003 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5004 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5005 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5006 | |
| 5007 | if (!nested_vmx_check_permission(vcpu)) |
| 5008 | return 1; |
| 5009 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5010 | /* |
| 5011 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5012 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5013 | */ |
| 5014 | if (vmx->nested.current_vmptr == -1ull || |
| 5015 | (is_guest_mode(vcpu) && |
| 5016 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5017 | return nested_vmx_failInvalid(vcpu); |
| 5018 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5019 | /* Decode instruction info and find the field to read */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5020 | field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5021 | |
| 5022 | offset = vmcs_field_to_offset(field); |
| 5023 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5024 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5025 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5026 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5027 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5028 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5029 | /* Read the field, zero-extended to a u64 value */ |
| 5030 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5031 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5032 | /* |
| 5033 | * Now copy part of this value to register or memory, as requested. |
| 5034 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5035 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5036 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5037 | if (instr_info & BIT(10)) { |
| 5038 | kvm_register_writel(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5039 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5040 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5041 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5042 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5043 | return 1; |
| 5044 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5045 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5046 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5047 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | return nested_vmx_succeed(vcpu); |
| 5051 | } |
| 5052 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5053 | static bool is_shadow_field_rw(unsigned long field) |
| 5054 | { |
| 5055 | switch (field) { |
| 5056 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5057 | #include "vmcs_shadow_fields.h" |
| 5058 | return true; |
| 5059 | default: |
| 5060 | break; |
| 5061 | } |
| 5062 | return false; |
| 5063 | } |
| 5064 | |
| 5065 | static bool is_shadow_field_ro(unsigned long field) |
| 5066 | { |
| 5067 | switch (field) { |
| 5068 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5069 | #include "vmcs_shadow_fields.h" |
| 5070 | return true; |
| 5071 | default: |
| 5072 | break; |
| 5073 | } |
| 5074 | return false; |
| 5075 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5076 | |
| 5077 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5078 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5079 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5080 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5081 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5082 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5083 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5084 | struct x86_exception e; |
| 5085 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5086 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5087 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5088 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5089 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5090 | /* |
| 5091 | * 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] | 5092 | * mode, and eventually we need to write that into a field of several |
| 5093 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5094 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5095 | * bits into the vmcs12 field. |
| 5096 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5097 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5098 | |
| 5099 | if (!nested_vmx_check_permission(vcpu)) |
| 5100 | return 1; |
| 5101 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5102 | /* |
| 5103 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5104 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5105 | */ |
| 5106 | if (vmx->nested.current_vmptr == -1ull || |
| 5107 | (is_guest_mode(vcpu) && |
| 5108 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5109 | return nested_vmx_failInvalid(vcpu); |
| 5110 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5111 | if (instr_info & BIT(10)) |
| 5112 | value = kvm_register_readl(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5113 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5114 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5115 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5116 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5117 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5118 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5119 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5120 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5121 | } |
| 5122 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5123 | field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5124 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5125 | offset = vmcs_field_to_offset(field); |
| 5126 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5127 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5128 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5129 | /* |
| 5130 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5131 | * VMCS," then the "read-only" fields are actually read/write. |
| 5132 | */ |
| 5133 | if (vmcs_field_readonly(field) && |
| 5134 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5135 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5136 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5137 | /* |
| 5138 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5139 | * vmcs12, else we may crush a field or consume a stale value. |
| 5140 | */ |
| 5141 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5142 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5143 | |
| 5144 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5145 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5146 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5147 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5148 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5149 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5150 | * 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] | 5151 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5152 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5153 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5154 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5155 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5156 | |
| 5157 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5158 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5159 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5160 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5161 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5162 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5163 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5164 | /* |
| 5165 | * L1 can read these fields without exiting, ensure the |
| 5166 | * shadow VMCS is up-to-date. |
| 5167 | */ |
| 5168 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5169 | preempt_disable(); |
| 5170 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5171 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5172 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5173 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5174 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5175 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5176 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5177 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5178 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | return nested_vmx_succeed(vcpu); |
| 5182 | } |
| 5183 | |
| 5184 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5185 | { |
| 5186 | vmx->nested.current_vmptr = vmptr; |
| 5187 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5188 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5189 | vmcs_write64(VMCS_LINK_POINTER, |
| 5190 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5191 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5192 | } |
| 5193 | vmx->nested.dirty_vmcs12 = true; |
| 5194 | } |
| 5195 | |
| 5196 | /* Emulate the VMPTRLD instruction */ |
| 5197 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5198 | { |
| 5199 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5200 | gpa_t vmptr; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5201 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5202 | |
| 5203 | if (!nested_vmx_check_permission(vcpu)) |
| 5204 | return 1; |
| 5205 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5206 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5207 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5208 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5209 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5210 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5211 | |
| 5212 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5213 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5214 | |
| 5215 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
| 5216 | if (vmx->nested.hv_evmcs) |
| 5217 | return 1; |
| 5218 | |
| 5219 | if (vmx->nested.current_vmptr != vmptr) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5220 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5221 | struct vmcs12 *new_vmcs12; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5222 | |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5223 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5224 | /* |
| 5225 | * Reads from an unbacked page return all 1s, |
| 5226 | * which means that the 32 bits located at the |
| 5227 | * given physical address won't match the required |
| 5228 | * VMCS12_REVISION identifier. |
| 5229 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5230 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5231 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5232 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5233 | |
| 5234 | new_vmcs12 = map.hva; |
| 5235 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5236 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 5237 | (new_vmcs12->hdr.shadow_vmcs && |
| 5238 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5239 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5240 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5241 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5242 | } |
| 5243 | |
| 5244 | nested_release_vmcs12(vcpu); |
| 5245 | |
| 5246 | /* |
| 5247 | * Load VMCS12 from guest memory since it is not already |
| 5248 | * cached. |
| 5249 | */ |
| 5250 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5251 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5252 | |
| 5253 | set_current_vmptr(vmx, vmptr); |
| 5254 | } |
| 5255 | |
| 5256 | return nested_vmx_succeed(vcpu); |
| 5257 | } |
| 5258 | |
| 5259 | /* Emulate the VMPTRST instruction */ |
| 5260 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5261 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5262 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5263 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5264 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5265 | struct x86_exception e; |
| 5266 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5267 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5268 | |
| 5269 | if (!nested_vmx_check_permission(vcpu)) |
| 5270 | return 1; |
| 5271 | |
| 5272 | if (unlikely(to_vmx(vcpu)->nested.hv_evmcs)) |
| 5273 | return 1; |
| 5274 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5275 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5276 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5277 | return 1; |
| 5278 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5279 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5280 | sizeof(gpa_t), &e); |
| 5281 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5282 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5283 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5284 | return nested_vmx_succeed(vcpu); |
| 5285 | } |
| 5286 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5287 | #define EPTP_PA_MASK GENMASK_ULL(51, 12) |
| 5288 | |
| 5289 | static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) |
| 5290 | { |
| 5291 | return VALID_PAGE(root_hpa) && |
| 5292 | ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); |
| 5293 | } |
| 5294 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5295 | /* Emulate the INVEPT instruction */ |
| 5296 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5297 | { |
| 5298 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5299 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5300 | unsigned long type, roots_to_free; |
| 5301 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5302 | gva_t gva; |
| 5303 | struct x86_exception e; |
| 5304 | struct { |
| 5305 | u64 eptp, gpa; |
| 5306 | } operand; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5307 | int i, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5308 | |
| 5309 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5310 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5311 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5312 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5313 | return 1; |
| 5314 | } |
| 5315 | |
| 5316 | if (!nested_vmx_check_permission(vcpu)) |
| 5317 | return 1; |
| 5318 | |
| 5319 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5320 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 5321 | |
| 5322 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5323 | |
| 5324 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5325 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5326 | |
| 5327 | /* According to the Intel VMX instruction reference, the memory |
| 5328 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5329 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5330 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5331 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5332 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5333 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5334 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5335 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5336 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5337 | /* |
| 5338 | * Nested EPT roots are always held through guest_mmu, |
| 5339 | * not root_mmu. |
| 5340 | */ |
| 5341 | mmu = &vcpu->arch.guest_mmu; |
| 5342 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5343 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5344 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5345 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5346 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5347 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5348 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5349 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5350 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5351 | operand.eptp)) |
| 5352 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5353 | |
| 5354 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5355 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5356 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5357 | operand.eptp)) |
| 5358 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5359 | } |
| 5360 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5361 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5362 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5363 | break; |
| 5364 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5365 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5366 | break; |
| 5367 | } |
| 5368 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5369 | if (roots_to_free) |
| 5370 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5371 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5372 | return nested_vmx_succeed(vcpu); |
| 5373 | } |
| 5374 | |
| 5375 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5376 | { |
| 5377 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5378 | u32 vmx_instruction_info; |
| 5379 | unsigned long type, types; |
| 5380 | gva_t gva; |
| 5381 | struct x86_exception e; |
| 5382 | struct { |
| 5383 | u64 vpid; |
| 5384 | u64 gla; |
| 5385 | } operand; |
| 5386 | u16 vpid02; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5387 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5388 | |
| 5389 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5390 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5391 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5392 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5393 | return 1; |
| 5394 | } |
| 5395 | |
| 5396 | if (!nested_vmx_check_permission(vcpu)) |
| 5397 | return 1; |
| 5398 | |
| 5399 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5400 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 5401 | |
| 5402 | types = (vmx->nested.msrs.vpid_caps & |
| 5403 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5404 | |
| 5405 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5406 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5407 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5408 | |
| 5409 | /* according to the intel vmx instruction reference, the memory |
| 5410 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5411 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5412 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5413 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5414 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5415 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5416 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5417 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5418 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5419 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5420 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5421 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5422 | |
| 5423 | vpid02 = nested_get_vpid02(vcpu); |
| 5424 | switch (type) { |
| 5425 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5426 | if (!operand.vpid || |
| 5427 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5428 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5429 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5430 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5431 | break; |
| 5432 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5433 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5434 | if (!operand.vpid) |
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); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5437 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5438 | break; |
| 5439 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5440 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5441 | break; |
| 5442 | default: |
| 5443 | WARN_ON_ONCE(1); |
| 5444 | return kvm_skip_emulated_instruction(vcpu); |
| 5445 | } |
| 5446 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5447 | /* |
| 5448 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
| 5449 | * linear mappings for L2 (tagged with L2's VPID). Free all roots as |
| 5450 | * VPIDs are not tracked in the MMU role. |
| 5451 | * |
| 5452 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5453 | * an MMU when EPT is disabled. |
| 5454 | * |
| 5455 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5456 | */ |
| 5457 | if (!enable_ept) |
| 5458 | kvm_mmu_free_roots(vcpu, &vcpu->arch.root_mmu, |
| 5459 | KVM_MMU_ROOTS_ALL); |
| 5460 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5461 | return nested_vmx_succeed(vcpu); |
| 5462 | } |
| 5463 | |
| 5464 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5465 | struct vmcs12 *vmcs12) |
| 5466 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5467 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5468 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5469 | bool accessed_dirty; |
| 5470 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 5471 | |
| 5472 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 5473 | !nested_cpu_has_ept(vmcs12)) |
| 5474 | return 1; |
| 5475 | |
| 5476 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5477 | return 1; |
| 5478 | |
| 5479 | |
| 5480 | 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] | 5481 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5482 | return 1; |
| 5483 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5484 | accessed_dirty = !!(new_eptp & VMX_EPTP_AD_ENABLE_BIT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5485 | |
| 5486 | /* |
| 5487 | * If the (L2) guest does a vmfunc to the currently |
| 5488 | * active ept pointer, we don't have to do anything else |
| 5489 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5490 | if (vmcs12->ept_pointer != new_eptp) { |
| 5491 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5492 | return 1; |
| 5493 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5494 | mmu->ept_ad = accessed_dirty; |
| 5495 | mmu->mmu_role.base.ad_disabled = !accessed_dirty; |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5496 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5497 | |
| 5498 | kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5499 | } |
| 5500 | |
| 5501 | return 0; |
| 5502 | } |
| 5503 | |
| 5504 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5505 | { |
| 5506 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5507 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5508 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5509 | |
| 5510 | /* |
| 5511 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5512 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5513 | * didn't by injecting #UD. |
| 5514 | */ |
| 5515 | if (!is_guest_mode(vcpu)) { |
| 5516 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5517 | return 1; |
| 5518 | } |
| 5519 | |
| 5520 | vmcs12 = get_vmcs12(vcpu); |
| 5521 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 5522 | goto fail; |
| 5523 | |
| 5524 | switch (function) { |
| 5525 | case 0: |
| 5526 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5527 | goto fail; |
| 5528 | break; |
| 5529 | default: |
| 5530 | goto fail; |
| 5531 | } |
| 5532 | return kvm_skip_emulated_instruction(vcpu); |
| 5533 | |
| 5534 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5535 | /* |
| 5536 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5537 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5538 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5539 | */ |
| 5540 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5541 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5542 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5543 | return 1; |
| 5544 | } |
| 5545 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5546 | /* |
| 5547 | * Return true if an IO instruction with the specified port and size should cause |
| 5548 | * a VM-exit into L1. |
| 5549 | */ |
| 5550 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5551 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5552 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5553 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5554 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5555 | u8 b; |
| 5556 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5557 | last_bitmap = (gpa_t)-1; |
| 5558 | b = -1; |
| 5559 | |
| 5560 | while (size > 0) { |
| 5561 | if (port < 0x8000) |
| 5562 | bitmap = vmcs12->io_bitmap_a; |
| 5563 | else if (port < 0x10000) |
| 5564 | bitmap = vmcs12->io_bitmap_b; |
| 5565 | else |
| 5566 | return true; |
| 5567 | bitmap += (port & 0x7fff) / 8; |
| 5568 | |
| 5569 | if (last_bitmap != bitmap) |
| 5570 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5571 | return true; |
| 5572 | if (b & (1 << (port & 7))) |
| 5573 | return true; |
| 5574 | |
| 5575 | port++; |
| 5576 | size--; |
| 5577 | last_bitmap = bitmap; |
| 5578 | } |
| 5579 | |
| 5580 | return false; |
| 5581 | } |
| 5582 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5583 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5584 | struct vmcs12 *vmcs12) |
| 5585 | { |
| 5586 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5587 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5588 | int size; |
| 5589 | |
| 5590 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5591 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5592 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5593 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5594 | |
| 5595 | port = exit_qualification >> 16; |
| 5596 | size = (exit_qualification & 7) + 1; |
| 5597 | |
| 5598 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5599 | } |
| 5600 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5601 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5602 | * 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] | 5603 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5604 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5605 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5606 | */ |
| 5607 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5608 | struct vmcs12 *vmcs12, |
| 5609 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5610 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5611 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5612 | gpa_t bitmap; |
| 5613 | |
| 5614 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5615 | return true; |
| 5616 | |
| 5617 | /* |
| 5618 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5619 | * for the four combinations of read/write and low/high MSR numbers. |
| 5620 | * First we need to figure out which of the four to use: |
| 5621 | */ |
| 5622 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5623 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5624 | bitmap += 2048; |
| 5625 | if (msr_index >= 0xc0000000) { |
| 5626 | msr_index -= 0xc0000000; |
| 5627 | bitmap += 1024; |
| 5628 | } |
| 5629 | |
| 5630 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5631 | if (msr_index < 1024*8) { |
| 5632 | unsigned char b; |
| 5633 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5634 | return true; |
| 5635 | return 1 & (b >> (msr_index & 7)); |
| 5636 | } else |
| 5637 | return true; /* let L1 handle the wrong parameter */ |
| 5638 | } |
| 5639 | |
| 5640 | /* |
| 5641 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5642 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5643 | * intercept (via guest_host_mask etc.) the current event. |
| 5644 | */ |
| 5645 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5646 | struct vmcs12 *vmcs12) |
| 5647 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5648 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5649 | int cr = exit_qualification & 15; |
| 5650 | int reg; |
| 5651 | unsigned long val; |
| 5652 | |
| 5653 | switch ((exit_qualification >> 4) & 3) { |
| 5654 | case 0: /* mov to cr */ |
| 5655 | reg = (exit_qualification >> 8) & 15; |
| 5656 | val = kvm_register_readl(vcpu, reg); |
| 5657 | switch (cr) { |
| 5658 | case 0: |
| 5659 | if (vmcs12->cr0_guest_host_mask & |
| 5660 | (val ^ vmcs12->cr0_read_shadow)) |
| 5661 | return true; |
| 5662 | break; |
| 5663 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5664 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5665 | return true; |
| 5666 | break; |
| 5667 | case 4: |
| 5668 | if (vmcs12->cr4_guest_host_mask & |
| 5669 | (vmcs12->cr4_read_shadow ^ val)) |
| 5670 | return true; |
| 5671 | break; |
| 5672 | case 8: |
| 5673 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5674 | return true; |
| 5675 | break; |
| 5676 | } |
| 5677 | break; |
| 5678 | case 2: /* clts */ |
| 5679 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5680 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5681 | return true; |
| 5682 | break; |
| 5683 | case 1: /* mov from cr */ |
| 5684 | switch (cr) { |
| 5685 | case 3: |
| 5686 | if (vmcs12->cpu_based_vm_exec_control & |
| 5687 | CPU_BASED_CR3_STORE_EXITING) |
| 5688 | return true; |
| 5689 | break; |
| 5690 | case 8: |
| 5691 | if (vmcs12->cpu_based_vm_exec_control & |
| 5692 | CPU_BASED_CR8_STORE_EXITING) |
| 5693 | return true; |
| 5694 | break; |
| 5695 | } |
| 5696 | break; |
| 5697 | case 3: /* lmsw */ |
| 5698 | /* |
| 5699 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5700 | * cr0. Other attempted changes are ignored, with no exit. |
| 5701 | */ |
| 5702 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5703 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5704 | (val ^ vmcs12->cr0_read_shadow)) |
| 5705 | return true; |
| 5706 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5707 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5708 | (val & 0x1)) |
| 5709 | return true; |
| 5710 | break; |
| 5711 | } |
| 5712 | return false; |
| 5713 | } |
| 5714 | |
| 5715 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5716 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5717 | { |
| 5718 | u32 vmx_instruction_info; |
| 5719 | unsigned long field; |
| 5720 | u8 b; |
| 5721 | |
| 5722 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5723 | return true; |
| 5724 | |
| 5725 | /* Decode instruction info and find the field to access */ |
| 5726 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5727 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5728 | |
| 5729 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5730 | if (field >> 15) |
| 5731 | return true; |
| 5732 | |
| 5733 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5734 | return true; |
| 5735 | |
| 5736 | return 1 & (b >> (field & 7)); |
| 5737 | } |
| 5738 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5739 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5740 | { |
| 5741 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5742 | |
| 5743 | if (nested_cpu_has_mtf(vmcs12)) |
| 5744 | return true; |
| 5745 | |
| 5746 | /* |
| 5747 | * An MTF VM-exit may be injected into the guest by setting the |
| 5748 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5749 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5750 | * control. |
| 5751 | */ |
| 5752 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5753 | | INTR_TYPE_OTHER_EVENT); |
| 5754 | } |
| 5755 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5756 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5757 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5758 | * 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] | 5759 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5760 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5761 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5762 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5763 | u32 intr_info; |
| 5764 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5765 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5766 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5767 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5768 | if (is_nmi(intr_info)) |
| 5769 | return true; |
| 5770 | else if (is_page_fault(intr_info)) |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 5771 | return vcpu->arch.apf.host_apf_flags || !enable_ept; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5772 | else if (is_debug(intr_info) && |
| 5773 | vcpu->guest_debug & |
| 5774 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5775 | return true; |
| 5776 | else if (is_breakpoint(intr_info) && |
| 5777 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5778 | return true; |
| 5779 | return false; |
| 5780 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5781 | return true; |
| 5782 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5783 | return true; |
| 5784 | case EXIT_REASON_EPT_VIOLATION: |
| 5785 | /* |
| 5786 | * L0 always deals with the EPT violation. If nested EPT is |
| 5787 | * used, and the nested mmu code discovers that the address is |
| 5788 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5789 | * will be injected with nested_ept_inject_page_fault() |
| 5790 | */ |
| 5791 | return true; |
| 5792 | case EXIT_REASON_EPT_MISCONFIG: |
| 5793 | /* |
| 5794 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5795 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5796 | * (EPT on EPT). So any problems with the structure of the |
| 5797 | * table is L0's fault. |
| 5798 | */ |
| 5799 | return true; |
| 5800 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5801 | return true; |
| 5802 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5803 | /* |
| 5804 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5805 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5806 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5807 | return true; |
| 5808 | case EXIT_REASON_VMFUNC: |
| 5809 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5810 | return true; |
| 5811 | case EXIT_REASON_ENCLS: |
| 5812 | /* SGX is never exposed to L1 */ |
| 5813 | return true; |
| 5814 | default: |
| 5815 | break; |
| 5816 | } |
| 5817 | return false; |
| 5818 | } |
| 5819 | |
| 5820 | /* |
| 5821 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5822 | * is_guest_mode (L2). |
| 5823 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5824 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5825 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5826 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5827 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5828 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5829 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5830 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5831 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5832 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5833 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5834 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5835 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5836 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5837 | return vmcs12->exception_bitmap & |
| 5838 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5839 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5840 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5841 | case EXIT_REASON_TRIPLE_FAULT: |
| 5842 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5843 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5844 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5845 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5846 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5847 | case EXIT_REASON_TASK_SWITCH: |
| 5848 | return true; |
| 5849 | case EXIT_REASON_CPUID: |
| 5850 | return true; |
| 5851 | case EXIT_REASON_HLT: |
| 5852 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5853 | case EXIT_REASON_INVD: |
| 5854 | return true; |
| 5855 | case EXIT_REASON_INVLPG: |
| 5856 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5857 | case EXIT_REASON_RDPMC: |
| 5858 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5859 | case EXIT_REASON_RDRAND: |
| 5860 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5861 | case EXIT_REASON_RDSEED: |
| 5862 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5863 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5864 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5865 | case EXIT_REASON_VMREAD: |
| 5866 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5867 | vmcs12->vmread_bitmap); |
| 5868 | case EXIT_REASON_VMWRITE: |
| 5869 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5870 | vmcs12->vmwrite_bitmap); |
| 5871 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5872 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5873 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5874 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5875 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5876 | /* |
| 5877 | * VMX instructions trap unconditionally. This allows L1 to |
| 5878 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5879 | */ |
| 5880 | return true; |
| 5881 | case EXIT_REASON_CR_ACCESS: |
| 5882 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5883 | case EXIT_REASON_DR_ACCESS: |
| 5884 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5885 | case EXIT_REASON_IO_INSTRUCTION: |
| 5886 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5887 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5888 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5889 | case EXIT_REASON_MSR_READ: |
| 5890 | case EXIT_REASON_MSR_WRITE: |
| 5891 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5892 | case EXIT_REASON_INVALID_STATE: |
| 5893 | return true; |
| 5894 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5895 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5896 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5897 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5898 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5899 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5900 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5901 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5902 | nested_cpu_has2(vmcs12, |
| 5903 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5904 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5905 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5906 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5907 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 5908 | case EXIT_REASON_APIC_ACCESS: |
| 5909 | case EXIT_REASON_APIC_WRITE: |
| 5910 | case EXIT_REASON_EOI_INDUCED: |
| 5911 | /* |
| 5912 | * The controls for "virtualize APIC accesses," "APIC- |
| 5913 | * register virtualization," and "virtual-interrupt |
| 5914 | * delivery" only come from vmcs12. |
| 5915 | */ |
| 5916 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5917 | case EXIT_REASON_INVPCID: |
| 5918 | return |
| 5919 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 5920 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5921 | case EXIT_REASON_WBINVD: |
| 5922 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5923 | case EXIT_REASON_XSETBV: |
| 5924 | return true; |
| 5925 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 5926 | /* |
| 5927 | * This should never happen, since it is not possible to |
| 5928 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 5929 | * If if it were, XSS would have to be checked against |
| 5930 | * the XSS exit bitmap in vmcs12. |
| 5931 | */ |
| 5932 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 5933 | case EXIT_REASON_UMWAIT: |
| 5934 | case EXIT_REASON_TPAUSE: |
| 5935 | return nested_cpu_has2(vmcs12, |
| 5936 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5937 | default: |
| 5938 | return true; |
| 5939 | } |
| 5940 | } |
| 5941 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5942 | /* |
| 5943 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 5944 | * reflected into L1. |
| 5945 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 5946 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5947 | { |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 5948 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5949 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 5950 | unsigned long exit_qual; |
| 5951 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 5952 | |
| 5953 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 5954 | |
| 5955 | /* |
| 5956 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 5957 | * has already loaded L2's state. |
| 5958 | */ |
| 5959 | if (unlikely(vmx->fail)) { |
| 5960 | trace_kvm_nested_vmenter_failed( |
| 5961 | "hardware VM-instruction error: ", |
| 5962 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 5963 | exit_intr_info = 0; |
| 5964 | exit_qual = 0; |
| 5965 | goto reflect_vmexit; |
| 5966 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5967 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5968 | trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 5969 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5970 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 5971 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 5972 | return false; |
| 5973 | |
| 5974 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 5975 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5976 | return false; |
| 5977 | |
| 5978 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 5979 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 5980 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 5981 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 5982 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5983 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 5984 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 5985 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5986 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5987 | |
| 5988 | vmcs12->vm_exit_intr_error_code = |
| 5989 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 5990 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 5991 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5992 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 5993 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5994 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5995 | return true; |
| 5996 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5997 | |
| 5998 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 5999 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6000 | u32 user_data_size) |
| 6001 | { |
| 6002 | struct vcpu_vmx *vmx; |
| 6003 | struct vmcs12 *vmcs12; |
| 6004 | struct kvm_nested_state kvm_state = { |
| 6005 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6006 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6007 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6008 | .hdr.vmx.flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6009 | .hdr.vmx.vmxon_pa = -1ull, |
| 6010 | .hdr.vmx.vmcs12_pa = -1ull, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6011 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6012 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6013 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6014 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6015 | |
| 6016 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6017 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6018 | |
| 6019 | vmx = to_vmx(vcpu); |
| 6020 | vmcs12 = get_vmcs12(vcpu); |
| 6021 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6022 | if (nested_vmx_allowed(vcpu) && |
| 6023 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6024 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6025 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6026 | |
| 6027 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6028 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6029 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6030 | if (vmx->nested.hv_evmcs) |
| 6031 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6032 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6033 | if (is_guest_mode(vcpu) && |
| 6034 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6035 | vmcs12->vmcs_link_pointer != -1ull) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6036 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6040 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6041 | |
| 6042 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6043 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6044 | |
| 6045 | if (is_guest_mode(vcpu)) { |
| 6046 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6047 | |
| 6048 | if (vmx->nested.nested_run_pending) |
| 6049 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6050 | |
| 6051 | if (vmx->nested.mtf_pending) |
| 6052 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6053 | |
| 6054 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6055 | vmx->nested.has_preemption_timer_deadline) { |
| 6056 | kvm_state.hdr.vmx.flags |= |
| 6057 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6058 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6059 | vmx->nested.preemption_timer_deadline; |
| 6060 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | if (user_data_size < kvm_state.size) |
| 6065 | goto out; |
| 6066 | |
| 6067 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6068 | return -EFAULT; |
| 6069 | |
| 6070 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6071 | goto out; |
| 6072 | |
| 6073 | /* |
| 6074 | * When running L2, the authoritative vmcs12 state is in the |
| 6075 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6076 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6077 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6078 | * vmcs12 state is in the vmcs12 already. |
| 6079 | */ |
| 6080 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6081 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6082 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6083 | } else { |
| 6084 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6085 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
| 6086 | if (vmx->nested.hv_evmcs) |
| 6087 | copy_enlightened_to_vmcs12(vmx); |
| 6088 | else if (enable_shadow_vmcs) |
| 6089 | copy_shadow_to_vmcs12(vmx); |
| 6090 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6091 | } |
| 6092 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6093 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6094 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6095 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6096 | /* |
| 6097 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6098 | * of the struct. |
| 6099 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6100 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6101 | return -EFAULT; |
| 6102 | |
| 6103 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6104 | vmcs12->vmcs_link_pointer != -1ull) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6105 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6106 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6107 | return -EFAULT; |
| 6108 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6109 | out: |
| 6110 | return kvm_state.size; |
| 6111 | } |
| 6112 | |
| 6113 | /* |
| 6114 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6115 | */ |
| 6116 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6117 | { |
| 6118 | if (is_guest_mode(vcpu)) { |
| 6119 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6120 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6121 | } |
| 6122 | free_nested(vcpu); |
| 6123 | } |
| 6124 | |
| 6125 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6126 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6127 | struct kvm_nested_state *kvm_state) |
| 6128 | { |
| 6129 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6130 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6131 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6132 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6133 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6134 | int ret; |
| 6135 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6136 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6137 | return -EINVAL; |
| 6138 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6139 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) { |
| 6140 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6141 | return -EINVAL; |
| 6142 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6143 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6144 | return -EINVAL; |
| 6145 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6146 | /* |
| 6147 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6148 | * enable eVMCS capability on vCPU. However, since then |
| 6149 | * code was changed such that flag signals vmcs12 should |
| 6150 | * be copied into eVMCS in guest memory. |
| 6151 | * |
| 6152 | * To preserve backwards compatability, allow user |
| 6153 | * to set this flag even when there is no VMXON region. |
| 6154 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6155 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6156 | return -EINVAL; |
| 6157 | } else { |
| 6158 | if (!nested_vmx_allowed(vcpu)) |
| 6159 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6160 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6161 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6162 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6163 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6164 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6165 | 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] | 6166 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6167 | return -EINVAL; |
| 6168 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6169 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6170 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6171 | return -EINVAL; |
| 6172 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6173 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6174 | return -EINVAL; |
| 6175 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6176 | /* |
| 6177 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6178 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6179 | * must be zero. |
| 6180 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6181 | if (is_smm(vcpu) ? |
| 6182 | (kvm_state->flags & |
| 6183 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6184 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6185 | return -EINVAL; |
| 6186 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6187 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6188 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6189 | return -EINVAL; |
| 6190 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6191 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6192 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6193 | return -EINVAL; |
| 6194 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6195 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6196 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6197 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6198 | return 0; |
| 6199 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6200 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6201 | ret = enter_vmx_operation(vcpu); |
| 6202 | if (ret) |
| 6203 | return ret; |
| 6204 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6205 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6206 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6207 | /* See vmx_has_valid_vmcs12. */ |
| 6208 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6209 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
| 6210 | (kvm_state->hdr.vmx.vmcs12_pa != -1ull)) |
| 6211 | return -EINVAL; |
| 6212 | else |
| 6213 | return 0; |
| 6214 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6215 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6216 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) { |
| 6217 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6218 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6219 | return -EINVAL; |
| 6220 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6221 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6222 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6223 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6224 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6225 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6226 | * restored yet. EVMCS will be mapped from |
| 6227 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6228 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6229 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6230 | } else { |
| 6231 | return -EINVAL; |
| 6232 | } |
| 6233 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6234 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6235 | vmx->nested.smm.vmxon = true; |
| 6236 | vmx->nested.vmxon = false; |
| 6237 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6238 | 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] | 6239 | vmx->nested.smm.guest_mode = true; |
| 6240 | } |
| 6241 | |
| 6242 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6243 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6244 | return -EFAULT; |
| 6245 | |
| 6246 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6247 | return -EINVAL; |
| 6248 | |
| 6249 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6250 | return 0; |
| 6251 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6252 | vmx->nested.nested_run_pending = |
| 6253 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6254 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6255 | vmx->nested.mtf_pending = |
| 6256 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6257 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6258 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6259 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6260 | vmcs12->vmcs_link_pointer != -1ull) { |
| 6261 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6262 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6263 | if (kvm_state->size < |
| 6264 | sizeof(*kvm_state) + |
| 6265 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6266 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6267 | |
| 6268 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6269 | user_vmx_nested_state->shadow_vmcs12, |
| 6270 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6271 | ret = -EFAULT; |
| 6272 | goto error_guest_mode; |
| 6273 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6274 | |
| 6275 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6276 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6277 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6278 | } |
| 6279 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6280 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6281 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6282 | vmx->nested.has_preemption_timer_deadline = true; |
| 6283 | vmx->nested.preemption_timer_deadline = |
| 6284 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6285 | } |
| 6286 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6287 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6288 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6289 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6290 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6291 | |
| 6292 | vmx->nested.dirty_vmcs12 = true; |
| 6293 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6294 | if (ret) |
| 6295 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6296 | |
| 6297 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6298 | |
| 6299 | error_guest_mode: |
| 6300 | vmx->nested.nested_run_pending = 0; |
| 6301 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6302 | } |
| 6303 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6304 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6305 | { |
| 6306 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6307 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6308 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6309 | } |
| 6310 | } |
| 6311 | |
| 6312 | /* |
| 6313 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6314 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6315 | * The same values should also be used to verify that vmcs12 control fields are |
| 6316 | * valid during nested entry from L1 to L2. |
| 6317 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6318 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6319 | * bit in the high half is on if the corresponding bit in the control field |
| 6320 | * may be on. See also vmx_control_verify(). |
| 6321 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6322 | 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] | 6323 | { |
| 6324 | /* |
| 6325 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6326 | * the control fields which may be 1) should be initialized by the |
| 6327 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6328 | * can be supported) and the list of features we want to expose - |
| 6329 | * because they are known to be properly supported in our code. |
| 6330 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6331 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6332 | * reason is that if one of these bits is necessary, it will appear |
| 6333 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6334 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6335 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6336 | * These rules have exceptions below. |
| 6337 | */ |
| 6338 | |
| 6339 | /* pin-based controls */ |
| 6340 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6341 | msrs->pinbased_ctls_low, |
| 6342 | msrs->pinbased_ctls_high); |
| 6343 | msrs->pinbased_ctls_low |= |
| 6344 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6345 | msrs->pinbased_ctls_high &= |
| 6346 | PIN_BASED_EXT_INTR_MASK | |
| 6347 | PIN_BASED_NMI_EXITING | |
| 6348 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6349 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6350 | msrs->pinbased_ctls_high |= |
| 6351 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6352 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6353 | |
| 6354 | /* exit controls */ |
| 6355 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6356 | msrs->exit_ctls_low, |
| 6357 | msrs->exit_ctls_high); |
| 6358 | msrs->exit_ctls_low = |
| 6359 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6360 | |
| 6361 | msrs->exit_ctls_high &= |
| 6362 | #ifdef CONFIG_X86_64 |
| 6363 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6364 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6365 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6366 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6367 | msrs->exit_ctls_high |= |
| 6368 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6369 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6370 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6371 | |
| 6372 | /* We support free control of debug control saving. */ |
| 6373 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6374 | |
| 6375 | /* entry controls */ |
| 6376 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6377 | msrs->entry_ctls_low, |
| 6378 | msrs->entry_ctls_high); |
| 6379 | msrs->entry_ctls_low = |
| 6380 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6381 | msrs->entry_ctls_high &= |
| 6382 | #ifdef CONFIG_X86_64 |
| 6383 | VM_ENTRY_IA32E_MODE | |
| 6384 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6385 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6386 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6387 | msrs->entry_ctls_high |= |
| 6388 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6389 | |
| 6390 | /* We support free control of debug control loading. */ |
| 6391 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6392 | |
| 6393 | /* cpu-based controls */ |
| 6394 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6395 | msrs->procbased_ctls_low, |
| 6396 | msrs->procbased_ctls_high); |
| 6397 | msrs->procbased_ctls_low = |
| 6398 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6399 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6400 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6401 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6402 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6403 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6404 | CPU_BASED_CR3_STORE_EXITING | |
| 6405 | #ifdef CONFIG_X86_64 |
| 6406 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6407 | #endif |
| 6408 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6409 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6410 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6411 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6412 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6413 | /* |
| 6414 | * We can allow some features even when not supported by the |
| 6415 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6416 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6417 | * without MSR bitmaps. |
| 6418 | */ |
| 6419 | msrs->procbased_ctls_high |= |
| 6420 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6421 | CPU_BASED_USE_MSR_BITMAPS; |
| 6422 | |
| 6423 | /* We support free control of CR3 access interception. */ |
| 6424 | msrs->procbased_ctls_low &= |
| 6425 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6426 | |
| 6427 | /* |
| 6428 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6429 | * depend on CPUID bits, they are added later by |
| 6430 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6431 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6432 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6433 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6434 | msrs->secondary_ctls_low, |
| 6435 | msrs->secondary_ctls_high); |
| 6436 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6437 | msrs->secondary_ctls_low = 0; |
| 6438 | msrs->secondary_ctls_high &= |
| 6439 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6440 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6441 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6442 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6443 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6444 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6445 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6446 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6447 | SECONDARY_EXEC_RDSEED_EXITING | |
| 6448 | SECONDARY_EXEC_XSAVES; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6449 | |
| 6450 | /* |
| 6451 | * We can emulate "VMCS shadowing," even if the hardware |
| 6452 | * doesn't support it. |
| 6453 | */ |
| 6454 | msrs->secondary_ctls_high |= |
| 6455 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6456 | |
| 6457 | if (enable_ept) { |
| 6458 | /* nested EPT: emulate EPT also to L1 */ |
| 6459 | msrs->secondary_ctls_high |= |
| 6460 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6461 | msrs->ept_caps = |
| 6462 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6463 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6464 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6465 | VMX_EPT_INVEPT_BIT | |
| 6466 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6467 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6468 | msrs->ept_caps &= ept_caps; |
| 6469 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6470 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6471 | VMX_EPT_1GB_PAGE_BIT; |
| 6472 | if (enable_ept_ad_bits) { |
| 6473 | msrs->secondary_ctls_high |= |
| 6474 | SECONDARY_EXEC_ENABLE_PML; |
| 6475 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6476 | } |
| 6477 | } |
| 6478 | |
| 6479 | if (cpu_has_vmx_vmfunc()) { |
| 6480 | msrs->secondary_ctls_high |= |
| 6481 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6482 | /* |
| 6483 | * Advertise EPTP switching unconditionally |
| 6484 | * since we emulate it |
| 6485 | */ |
| 6486 | if (enable_ept) |
| 6487 | msrs->vmfunc_controls = |
| 6488 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6489 | } |
| 6490 | |
| 6491 | /* |
| 6492 | * Old versions of KVM use the single-context version without |
| 6493 | * checking for support, so declare that it is supported even |
| 6494 | * though it is treated as global context. The alternative is |
| 6495 | * not failing the single-context invvpid, and it is worse. |
| 6496 | */ |
| 6497 | if (enable_vpid) { |
| 6498 | msrs->secondary_ctls_high |= |
| 6499 | SECONDARY_EXEC_ENABLE_VPID; |
| 6500 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6501 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6502 | } |
| 6503 | |
| 6504 | if (enable_unrestricted_guest) |
| 6505 | msrs->secondary_ctls_high |= |
| 6506 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6507 | |
| 6508 | if (flexpriority_enabled) |
| 6509 | msrs->secondary_ctls_high |= |
| 6510 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6511 | |
| 6512 | /* miscellaneous data */ |
| 6513 | rdmsr(MSR_IA32_VMX_MISC, |
| 6514 | msrs->misc_low, |
| 6515 | msrs->misc_high); |
| 6516 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6517 | msrs->misc_low |= |
| 6518 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6519 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6520 | VMX_MISC_ACTIVITY_HLT | |
| 6521 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6522 | msrs->misc_high = 0; |
| 6523 | |
| 6524 | /* |
| 6525 | * This MSR reports some information about VMX support. We |
| 6526 | * should return information about the VMX we emulate for the |
| 6527 | * guest, and the VMCS structure we give it - not about the |
| 6528 | * VMX support of the underlying hardware. |
| 6529 | */ |
| 6530 | msrs->basic = |
| 6531 | VMCS12_REVISION | |
| 6532 | VMX_BASIC_TRUE_CTLS | |
| 6533 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6534 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6535 | |
| 6536 | if (cpu_has_vmx_basic_inout()) |
| 6537 | msrs->basic |= VMX_BASIC_INOUT; |
| 6538 | |
| 6539 | /* |
| 6540 | * These MSRs specify bits which the guest must keep fixed on |
| 6541 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6542 | * We picked the standard core2 setting. |
| 6543 | */ |
| 6544 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6545 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6546 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6547 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6548 | |
| 6549 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6550 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6551 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6552 | |
| 6553 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
| 6554 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
| 6555 | } |
| 6556 | |
| 6557 | void nested_vmx_hardware_unsetup(void) |
| 6558 | { |
| 6559 | int i; |
| 6560 | |
| 6561 | if (enable_shadow_vmcs) { |
| 6562 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6563 | free_page((unsigned long)vmx_bitmap[i]); |
| 6564 | } |
| 6565 | } |
| 6566 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6567 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6568 | { |
| 6569 | int i; |
| 6570 | |
| 6571 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6572 | enable_shadow_vmcs = 0; |
| 6573 | if (enable_shadow_vmcs) { |
| 6574 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6575 | /* |
| 6576 | * The vmx_bitmap is not tied to a VM and so should |
| 6577 | * not be charged to a memcg. |
| 6578 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6579 | vmx_bitmap[i] = (unsigned long *) |
| 6580 | __get_free_page(GFP_KERNEL); |
| 6581 | if (!vmx_bitmap[i]) { |
| 6582 | nested_vmx_hardware_unsetup(); |
| 6583 | return -ENOMEM; |
| 6584 | } |
| 6585 | } |
| 6586 | |
| 6587 | init_vmcs_shadow_fields(); |
| 6588 | } |
| 6589 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6590 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6591 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6592 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6593 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6594 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6595 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6596 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6597 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6598 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6599 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6600 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6601 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6602 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6603 | return 0; |
| 6604 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6605 | |
| 6606 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6607 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6608 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6609 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6610 | .get_state = vmx_get_nested_state, |
| 6611 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6612 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6613 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6614 | .enable_evmcs = nested_enable_evmcs, |
| 6615 | .get_evmcs_version = nested_get_evmcs_version, |
| 6616 | }; |