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