Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
Julien Thierry | 00089c0 | 2020-09-04 16:30:25 +0100 | [diff] [blame] | 3 | #include <linux/objtool.h> |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4 | #include <linux/percpu.h> |
| 5 | |
| 6 | #include <asm/debugreg.h> |
| 7 | #include <asm/mmu_context.h> |
| 8 | |
| 9 | #include "cpuid.h" |
| 10 | #include "hyperv.h" |
| 11 | #include "mmu.h" |
| 12 | #include "nested.h" |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 13 | #include "pmu.h" |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 14 | #include "sgx.h" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 15 | #include "trace.h" |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 16 | #include "vmx.h" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 17 | #include "x86.h" |
| 18 | |
| 19 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 20 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
| 21 | |
| 22 | static bool __read_mostly nested_early_check = 0; |
| 23 | module_param(nested_early_check, bool, S_IRUGO); |
| 24 | |
Sean Christopherson | 648fc8a | 2021-02-03 16:01:16 -0800 | [diff] [blame] | 25 | #define CC KVM_NESTED_VMENTER_CONSISTENCY_CHECK |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 26 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 27 | /* |
| 28 | * Hyper-V requires all of these, so mark them as supported even though |
| 29 | * they are just treated the same as all-context. |
| 30 | */ |
| 31 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 32 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 33 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 34 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 35 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 36 | |
| 37 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 38 | |
| 39 | enum { |
| 40 | VMX_VMREAD_BITMAP, |
| 41 | VMX_VMWRITE_BITMAP, |
| 42 | VMX_BITMAP_NR |
| 43 | }; |
| 44 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 45 | |
| 46 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 47 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
| 48 | |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 49 | struct shadow_vmcs_field { |
| 50 | u16 encoding; |
| 51 | u16 offset; |
| 52 | }; |
| 53 | static struct shadow_vmcs_field shadow_read_only_fields[] = { |
| 54 | #define SHADOW_FIELD_RO(x, y) { x, offsetof(struct vmcs12, y) }, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 55 | #include "vmcs_shadow_fields.h" |
| 56 | }; |
| 57 | static int max_shadow_read_only_fields = |
| 58 | ARRAY_SIZE(shadow_read_only_fields); |
| 59 | |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 60 | static struct shadow_vmcs_field shadow_read_write_fields[] = { |
| 61 | #define SHADOW_FIELD_RW(x, y) { x, offsetof(struct vmcs12, y) }, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 62 | #include "vmcs_shadow_fields.h" |
| 63 | }; |
| 64 | static int max_shadow_read_write_fields = |
| 65 | ARRAY_SIZE(shadow_read_write_fields); |
| 66 | |
Yi Wang | 8997f65 | 2019-01-21 15:27:05 +0800 | [diff] [blame] | 67 | static void init_vmcs_shadow_fields(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 68 | { |
| 69 | int i, j; |
| 70 | |
| 71 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 72 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 73 | |
| 74 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 75 | struct shadow_vmcs_field entry = shadow_read_only_fields[i]; |
| 76 | u16 field = entry.encoding; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 77 | |
| 78 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 79 | (i + 1 == max_shadow_read_only_fields || |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 80 | shadow_read_only_fields[i + 1].encoding != field + 1)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 81 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 82 | field + 1); |
| 83 | |
| 84 | clear_bit(field, vmx_vmread_bitmap); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 85 | if (field & 1) |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 87 | continue; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 88 | #else |
| 89 | entry.offset += sizeof(u32); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 90 | #endif |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 91 | shadow_read_only_fields[j++] = entry; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 92 | } |
| 93 | max_shadow_read_only_fields = j; |
| 94 | |
| 95 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 96 | struct shadow_vmcs_field entry = shadow_read_write_fields[i]; |
| 97 | u16 field = entry.encoding; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 98 | |
| 99 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
| 100 | (i + 1 == max_shadow_read_write_fields || |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 101 | shadow_read_write_fields[i + 1].encoding != field + 1)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 102 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 103 | field + 1); |
| 104 | |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 105 | WARN_ONCE(field >= GUEST_ES_AR_BYTES && |
| 106 | field <= GUEST_TR_AR_BYTES, |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 107 | "Update vmcs12_write_any() to drop reserved bits from AR_BYTES"); |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 108 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 109 | /* |
| 110 | * PML and the preemption timer can be emulated, but the |
| 111 | * processor cannot vmwrite to fields that don't exist |
| 112 | * on bare metal. |
| 113 | */ |
| 114 | switch (field) { |
| 115 | case GUEST_PML_INDEX: |
| 116 | if (!cpu_has_vmx_pml()) |
| 117 | continue; |
| 118 | break; |
| 119 | case VMX_PREEMPTION_TIMER_VALUE: |
| 120 | if (!cpu_has_vmx_preemption_timer()) |
| 121 | continue; |
| 122 | break; |
| 123 | case GUEST_INTR_STATUS: |
| 124 | if (!cpu_has_vmx_apicv()) |
| 125 | continue; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | clear_bit(field, vmx_vmwrite_bitmap); |
| 132 | clear_bit(field, vmx_vmread_bitmap); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 133 | if (field & 1) |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 134 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 135 | continue; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 136 | #else |
| 137 | entry.offset += sizeof(u32); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 138 | #endif |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 139 | shadow_read_write_fields[j++] = entry; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 140 | } |
| 141 | max_shadow_read_write_fields = j; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 146 | * set the success or error code of an emulated VMX instruction (as specified |
| 147 | * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated |
| 148 | * instruction. |
| 149 | */ |
| 150 | static int nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 151 | { |
| 152 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 153 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 154 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 155 | return kvm_skip_emulated_instruction(vcpu); |
| 156 | } |
| 157 | |
| 158 | static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 159 | { |
| 160 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 161 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 162 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 163 | | X86_EFLAGS_CF); |
| 164 | return kvm_skip_emulated_instruction(vcpu); |
| 165 | } |
| 166 | |
| 167 | static int nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 168 | u32 vm_instruction_error) |
| 169 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 170 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 171 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 172 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 173 | | X86_EFLAGS_ZF); |
| 174 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 175 | /* |
Vitaly Kuznetsov | b7685cf | 2021-05-26 15:20:23 +0200 | [diff] [blame] | 176 | * We don't need to force sync to shadow VMCS because |
| 177 | * VM_INSTRUCTION_ERROR is not shadowed. Enlightened VMCS 'shadows' all |
| 178 | * fields and thus must be synced. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 179 | */ |
Vitaly Kuznetsov | b7685cf | 2021-05-26 15:20:23 +0200 | [diff] [blame] | 180 | if (to_vmx(vcpu)->nested.hv_evmcs_vmptr != EVMPTR_INVALID) |
| 181 | to_vmx(vcpu)->nested.need_vmcs12_to_shadow_sync = true; |
| 182 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 183 | return kvm_skip_emulated_instruction(vcpu); |
| 184 | } |
| 185 | |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 186 | static int nested_vmx_fail(struct kvm_vcpu *vcpu, u32 vm_instruction_error) |
| 187 | { |
| 188 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 189 | |
| 190 | /* |
| 191 | * failValid writes the error number to the current VMCS, which |
| 192 | * can't be done if there isn't a current VMCS. |
| 193 | */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 194 | if (vmx->nested.current_vmptr == -1ull && |
| 195 | !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 196 | return nested_vmx_failInvalid(vcpu); |
| 197 | |
| 198 | return nested_vmx_failValid(vcpu, vm_instruction_error); |
| 199 | } |
| 200 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 201 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 202 | { |
| 203 | /* TODO: not to reset guest simply here. */ |
| 204 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 205 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
| 206 | } |
| 207 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 208 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 209 | { |
| 210 | return fixed_bits_valid(control, low, high); |
| 211 | } |
| 212 | |
| 213 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 214 | { |
| 215 | return low | ((u64)high << 32); |
| 216 | } |
| 217 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 218 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 219 | { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 220 | secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 221 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 222 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) |
| 226 | { |
| 227 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 228 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 229 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
| 230 | kvm_vcpu_unmap(vcpu, &vmx->nested.hv_evmcs_map, true); |
| 231 | vmx->nested.hv_evmcs = NULL; |
| 232 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 233 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 234 | vmx->nested.hv_evmcs_vmptr = EVMPTR_INVALID; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 237 | static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx, |
| 238 | struct loaded_vmcs *prev) |
| 239 | { |
| 240 | struct vmcs_host_state *dest, *src; |
| 241 | |
| 242 | if (unlikely(!vmx->guest_state_loaded)) |
| 243 | return; |
| 244 | |
| 245 | src = &prev->host_state; |
| 246 | dest = &vmx->loaded_vmcs->host_state; |
| 247 | |
| 248 | vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base); |
| 249 | dest->ldt_sel = src->ldt_sel; |
| 250 | #ifdef CONFIG_X86_64 |
| 251 | dest->ds_sel = src->ds_sel; |
| 252 | dest->es_sel = src->es_sel; |
| 253 | #endif |
| 254 | } |
| 255 | |
| 256 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
| 257 | { |
| 258 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 259 | struct loaded_vmcs *prev; |
| 260 | int cpu; |
| 261 | |
Sean Christopherson | 138534a | 2020-09-23 11:44:52 -0700 | [diff] [blame] | 262 | if (WARN_ON_ONCE(vmx->loaded_vmcs == vmcs)) |
Sean Christopherson | c61ca2f | 2020-09-23 11:44:49 -0700 | [diff] [blame] | 263 | return; |
| 264 | |
| 265 | cpu = get_cpu(); |
| 266 | prev = vmx->loaded_vmcs; |
| 267 | vmx->loaded_vmcs = vmcs; |
| 268 | vmx_vcpu_load_vmcs(vcpu, cpu, prev); |
| 269 | vmx_sync_vmcs_host_state(vmx, prev); |
| 270 | put_cpu(); |
| 271 | |
| 272 | vmx_register_cache_reset(vcpu); |
| 273 | } |
| 274 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 275 | /* |
| 276 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 277 | * just stops using VMX. |
| 278 | */ |
| 279 | static void free_nested(struct kvm_vcpu *vcpu) |
| 280 | { |
| 281 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 282 | |
Sean Christopherson | df82a24 | 2020-09-23 11:44:50 -0700 | [diff] [blame] | 283 | if (WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01)) |
| 284 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 285 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 286 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
| 287 | return; |
| 288 | |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 289 | kvm_clear_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Jan Kiszka | cf64527 | 2019-07-21 13:52:18 +0200 | [diff] [blame] | 290 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 291 | vmx->nested.vmxon = false; |
| 292 | vmx->nested.smm.vmxon = false; |
| 293 | free_vpid(vmx->nested.vpid02); |
| 294 | vmx->nested.posted_intr_nv = -1; |
| 295 | vmx->nested.current_vmptr = -1ull; |
| 296 | if (enable_shadow_vmcs) { |
| 297 | vmx_disable_shadow_vmcs(vmx); |
| 298 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 299 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 300 | vmx->vmcs01.shadow_vmcs = NULL; |
| 301 | } |
| 302 | kfree(vmx->nested.cached_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 303 | vmx->nested.cached_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 304 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jan Kiszka | c6bf2ae | 2019-07-21 16:01:36 +0200 | [diff] [blame] | 305 | vmx->nested.cached_shadow_vmcs12 = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 306 | /* Unpin physical memory we referred to in the vmcs02 */ |
| 307 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 308 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 309 | vmx->nested.apic_access_page = NULL; |
| 310 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 311 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 312 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 313 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 314 | |
| 315 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 316 | |
| 317 | nested_release_evmcs(vcpu); |
| 318 | |
| 319 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 320 | } |
| 321 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 322 | /* |
| 323 | * Ensure that the current vmcs of the logical processor is the |
| 324 | * vmcs01 of the vcpu before calling free_nested(). |
| 325 | */ |
| 326 | void nested_vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 327 | { |
| 328 | vcpu_load(vcpu); |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 329 | vmx_leave_nested(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 330 | vcpu_put(vcpu); |
| 331 | } |
| 332 | |
| 333 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 334 | struct x86_exception *fault) |
| 335 | { |
| 336 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 337 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 338 | u32 vm_exit_reason; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 339 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
| 340 | |
| 341 | if (vmx->nested.pml_full) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 342 | vm_exit_reason = EXIT_REASON_PML_FULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 343 | vmx->nested.pml_full = false; |
| 344 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 345 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 346 | vm_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 347 | else |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 348 | vm_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 349 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 350 | nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 351 | vmcs12->guest_physical_address = fault->address; |
| 352 | } |
| 353 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 354 | static void nested_ept_new_eptp(struct kvm_vcpu *vcpu) |
| 355 | { |
| 356 | kvm_init_shadow_ept_mmu(vcpu, |
| 357 | to_vmx(vcpu)->nested.msrs.ept_caps & |
| 358 | VMX_EPT_EXECUTE_ONLY_BIT, |
| 359 | nested_ept_ad_enabled(vcpu), |
| 360 | nested_ept_get_eptp(vcpu)); |
| 361 | } |
| 362 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 363 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
| 364 | { |
| 365 | WARN_ON(mmu_is_nested(vcpu)); |
| 366 | |
| 367 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 368 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | d8dd54e | 2020-03-02 18:02:39 -0800 | [diff] [blame] | 369 | vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 370 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
| 371 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
| 372 | |
| 373 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
| 374 | } |
| 375 | |
| 376 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 377 | { |
| 378 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
| 379 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
| 380 | } |
| 381 | |
| 382 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 383 | u16 error_code) |
| 384 | { |
| 385 | bool inequality, bit; |
| 386 | |
| 387 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 388 | inequality = |
| 389 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 390 | vmcs12->page_fault_error_code_match; |
| 391 | return inequality ^ bit; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /* |
| 396 | * KVM wants to inject page-faults which it got to the guest. This function |
| 397 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 398 | */ |
| 399 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
| 400 | { |
| 401 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 402 | unsigned int nr = vcpu->arch.exception.nr; |
| 403 | bool has_payload = vcpu->arch.exception.has_payload; |
| 404 | unsigned long payload = vcpu->arch.exception.payload; |
| 405 | |
| 406 | if (nr == PF_VECTOR) { |
| 407 | if (vcpu->arch.exception.nested_apf) { |
| 408 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
| 409 | return 1; |
| 410 | } |
| 411 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 412 | vcpu->arch.exception.error_code)) { |
| 413 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
| 414 | return 1; |
| 415 | } |
| 416 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 417 | if (nr == DB_VECTOR) { |
| 418 | if (!has_payload) { |
| 419 | payload = vcpu->arch.dr6; |
Chenyi Qiang | 9a3ecd5 | 2021-02-02 17:04:31 +0800 | [diff] [blame] | 420 | payload &= ~DR6_BT; |
| 421 | payload ^= DR6_ACTIVE_LOW; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 422 | } |
| 423 | *exit_qual = payload; |
| 424 | } else |
| 425 | *exit_qual = 0; |
| 426 | return 1; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 434 | struct x86_exception *fault) |
| 435 | { |
| 436 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 437 | |
| 438 | WARN_ON(!is_guest_mode(vcpu)); |
| 439 | |
| 440 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 441 | !to_vmx(vcpu)->nested.nested_run_pending) { |
| 442 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 443 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 444 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 445 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 446 | fault->address); |
| 447 | } else { |
| 448 | kvm_inject_page_fault(vcpu, fault); |
| 449 | } |
| 450 | } |
| 451 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 452 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 453 | struct vmcs12 *vmcs12) |
| 454 | { |
| 455 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 456 | return 0; |
| 457 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 458 | if (CC(!page_address_valid(vcpu, vmcs12->io_bitmap_a)) || |
| 459 | CC(!page_address_valid(vcpu, vmcs12->io_bitmap_b))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 460 | return -EINVAL; |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 466 | struct vmcs12 *vmcs12) |
| 467 | { |
| 468 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 469 | return 0; |
| 470 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 471 | if (CC(!page_address_valid(vcpu, vmcs12->msr_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 472 | return -EINVAL; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 478 | struct vmcs12 *vmcs12) |
| 479 | { |
| 480 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 481 | return 0; |
| 482 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 483 | if (CC(!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 484 | return -EINVAL; |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 491 | */ |
| 492 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 493 | { |
| 494 | unsigned long *msr_bitmap; |
| 495 | int f = sizeof(unsigned long); |
| 496 | |
| 497 | if (!cpu_has_vmx_msr_bitmap()) |
| 498 | return true; |
| 499 | |
| 500 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 501 | |
| 502 | if (msr <= 0x1fff) { |
| 503 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 504 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 505 | msr &= 0x1fff; |
| 506 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 507 | } |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 514 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 515 | */ |
| 516 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 517 | unsigned long *msr_bitmap_nested, |
| 518 | u32 msr, int type) |
| 519 | { |
| 520 | int f = sizeof(unsigned long); |
| 521 | |
| 522 | /* |
| 523 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 524 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 525 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 526 | */ |
| 527 | if (msr <= 0x1fff) { |
| 528 | if (type & MSR_TYPE_R && |
| 529 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 530 | /* read-low */ |
| 531 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 532 | |
| 533 | if (type & MSR_TYPE_W && |
| 534 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 535 | /* write-low */ |
| 536 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 537 | |
| 538 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 539 | msr &= 0x1fff; |
| 540 | if (type & MSR_TYPE_R && |
| 541 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 542 | /* read-high */ |
| 543 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 544 | |
| 545 | if (type & MSR_TYPE_W && |
| 546 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 547 | /* write-high */ |
| 548 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 549 | |
| 550 | } |
| 551 | } |
| 552 | |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 553 | static inline void enable_x2apic_msr_intercepts(unsigned long *msr_bitmap) |
| 554 | { |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 555 | int msr; |
| 556 | |
| 557 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 558 | unsigned word = msr / BITS_PER_LONG; |
| 559 | |
| 560 | msr_bitmap[word] = ~0; |
| 561 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 562 | } |
| 563 | } |
| 564 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 565 | /* |
| 566 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 567 | * we do not use the hardware. |
| 568 | */ |
| 569 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 570 | struct vmcs12 *vmcs12) |
| 571 | { |
| 572 | int msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 573 | unsigned long *msr_bitmap_l1; |
| 574 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 575 | struct kvm_host_map *map = &to_vmx(vcpu)->nested.msr_bitmap_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 576 | |
| 577 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 578 | if (!cpu_has_vmx_msr_bitmap() || |
| 579 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 580 | return false; |
| 581 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 582 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->msr_bitmap), map)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 583 | return false; |
| 584 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 585 | msr_bitmap_l1 = (unsigned long *)map->hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 586 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 587 | /* |
| 588 | * To keep the control flow simple, pay eight 8-byte writes (sixteen |
| 589 | * 4-byte writes on 32-bit systems) up front to enable intercepts for |
| 590 | * the x2APIC MSR range and selectively disable them below. |
| 591 | */ |
| 592 | enable_x2apic_msr_intercepts(msr_bitmap_l0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 593 | |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 594 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
| 595 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 596 | /* |
| 597 | * L0 need not intercept reads for MSRs between 0x800 |
| 598 | * and 0x8ff, it just lets the processor take the value |
| 599 | * from the virtual-APIC page; take those 256 bits |
| 600 | * directly from the L1 bitmap. |
| 601 | */ |
| 602 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 603 | unsigned word = msr / BITS_PER_LONG; |
| 604 | |
| 605 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 606 | } |
| 607 | } |
| 608 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 609 | nested_vmx_disable_intercept_for_msr( |
| 610 | msr_bitmap_l1, msr_bitmap_l0, |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 611 | X2APIC_MSR(APIC_TASKPRI), |
Marc Orr | c73f4c9 | 2019-04-01 23:56:00 -0700 | [diff] [blame] | 612 | MSR_TYPE_R | MSR_TYPE_W); |
Marc Orr | acff784 | 2019-04-01 23:55:59 -0700 | [diff] [blame] | 613 | |
| 614 | if (nested_cpu_has_vid(vmcs12)) { |
| 615 | nested_vmx_disable_intercept_for_msr( |
| 616 | msr_bitmap_l1, msr_bitmap_l0, |
| 617 | X2APIC_MSR(APIC_EOI), |
| 618 | MSR_TYPE_W); |
| 619 | nested_vmx_disable_intercept_for_msr( |
| 620 | msr_bitmap_l1, msr_bitmap_l0, |
| 621 | X2APIC_MSR(APIC_SELF_IPI), |
| 622 | MSR_TYPE_W); |
| 623 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 626 | /* KVM unconditionally exposes the FS/GS base MSRs to L1. */ |
Sean Christopherson | dbdd096 | 2021-04-21 19:38:31 -0700 | [diff] [blame] | 627 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 628 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 629 | MSR_FS_BASE, MSR_TYPE_RW); |
| 630 | |
| 631 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 632 | MSR_GS_BASE, MSR_TYPE_RW); |
| 633 | |
| 634 | nested_vmx_disable_intercept_for_msr(msr_bitmap_l1, msr_bitmap_l0, |
| 635 | MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
Sean Christopherson | dbdd096 | 2021-04-21 19:38:31 -0700 | [diff] [blame] | 636 | #endif |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * Checking the L0->L1 bitmap is trying to verify two things: |
| 640 | * |
| 641 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 642 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 643 | * from the L12 MSR bitmap that is too permissive. |
| 644 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 645 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 646 | * works properly because we only update the L01 MSR bitmap lazily. |
| 647 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 648 | * updated to reflect this when L1 (or its L2s) actually write to |
| 649 | * the MSR. |
| 650 | */ |
| 651 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 652 | nested_vmx_disable_intercept_for_msr( |
| 653 | msr_bitmap_l1, msr_bitmap_l0, |
| 654 | MSR_IA32_SPEC_CTRL, |
| 655 | MSR_TYPE_R | MSR_TYPE_W); |
| 656 | |
Sean Christopherson | d69129b | 2019-05-08 07:32:15 -0700 | [diff] [blame] | 657 | if (!msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 658 | nested_vmx_disable_intercept_for_msr( |
| 659 | msr_bitmap_l1, msr_bitmap_l0, |
| 660 | MSR_IA32_PRED_CMD, |
| 661 | MSR_TYPE_W); |
| 662 | |
KarimAllah Ahmed | 31f0b6c | 2019-01-31 21:24:36 +0100 | [diff] [blame] | 663 | kvm_vcpu_unmap(vcpu, &to_vmx(vcpu)->nested.msr_bitmap_map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 664 | |
| 665 | return true; |
| 666 | } |
| 667 | |
| 668 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 669 | struct vmcs12 *vmcs12) |
| 670 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 671 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 672 | struct vmcs12 *shadow; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 673 | |
| 674 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 675 | vmcs12->vmcs_link_pointer == -1ull) |
| 676 | return; |
| 677 | |
| 678 | shadow = get_shadow_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 679 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 680 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->vmcs_link_pointer), &map)) |
| 681 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 682 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 683 | memcpy(shadow, map.hva, VMCS12_SIZE); |
| 684 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 688 | struct vmcs12 *vmcs12) |
| 689 | { |
| 690 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 691 | |
| 692 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 693 | vmcs12->vmcs_link_pointer == -1ull) |
| 694 | return; |
| 695 | |
| 696 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 697 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * In nested virtualization, check if L1 has set |
| 702 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 703 | */ |
| 704 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 705 | { |
| 706 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 707 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 708 | } |
| 709 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 710 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 711 | struct vmcs12 *vmcs12) |
| 712 | { |
| 713 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 714 | CC(!page_address_valid(vcpu, vmcs12->apic_access_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 715 | return -EINVAL; |
| 716 | else |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 721 | struct vmcs12 *vmcs12) |
| 722 | { |
| 723 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 724 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
| 725 | !nested_cpu_has_vid(vmcs12) && |
| 726 | !nested_cpu_has_posted_intr(vmcs12)) |
| 727 | return 0; |
| 728 | |
| 729 | /* |
| 730 | * If virtualize x2apic mode is enabled, |
| 731 | * virtualize apic access must be disabled. |
| 732 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 733 | if (CC(nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 734 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | |
| 737 | /* |
| 738 | * If virtual interrupt delivery is enabled, |
| 739 | * we must exit on external interrupts. |
| 740 | */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 741 | if (CC(nested_cpu_has_vid(vmcs12) && !nested_exit_on_intr(vcpu))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 742 | return -EINVAL; |
| 743 | |
| 744 | /* |
| 745 | * bits 15:8 should be zero in posted_intr_nv, |
| 746 | * the descriptor address has been already checked |
| 747 | * in nested_get_vmcs12_pages. |
| 748 | * |
| 749 | * bits 5:0 of posted_intr_desc_addr should be zero. |
| 750 | */ |
| 751 | if (nested_cpu_has_posted_intr(vmcs12) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 752 | (CC(!nested_cpu_has_vid(vmcs12)) || |
| 753 | CC(!nested_exit_intr_ack_set(vcpu)) || |
| 754 | CC((vmcs12->posted_intr_nv & 0xff00)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 755 | CC(!kvm_vcpu_is_legal_aligned_gpa(vcpu, vmcs12->posted_intr_desc_addr, 64)))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 756 | return -EINVAL; |
| 757 | |
| 758 | /* tpr shadow is needed by all apicv features. */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 759 | if (CC(!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 760 | return -EINVAL; |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 766 | u32 count, u64 addr) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 767 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 768 | if (count == 0) |
| 769 | return 0; |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 770 | |
| 771 | if (!kvm_vcpu_is_legal_aligned_gpa(vcpu, addr, 16) || |
| 772 | !kvm_vcpu_is_legal_gpa(vcpu, (addr + count * sizeof(struct vmx_msr_entry) - 1))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 773 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 774 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 778 | static int nested_vmx_check_exit_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 779 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 780 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 781 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 782 | vmcs12->vm_exit_msr_load_count, |
| 783 | vmcs12->vm_exit_msr_load_addr)) || |
| 784 | CC(nested_vmx_check_msr_switch(vcpu, |
| 785 | vmcs12->vm_exit_msr_store_count, |
| 786 | vmcs12->vm_exit_msr_store_addr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 787 | return -EINVAL; |
Sean Christopherson | f9b245e | 2018-12-12 13:30:08 -0500 | [diff] [blame] | 788 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 789 | return 0; |
| 790 | } |
| 791 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 792 | static int nested_vmx_check_entry_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 793 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 794 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 795 | if (CC(nested_vmx_check_msr_switch(vcpu, |
| 796 | vmcs12->vm_entry_msr_load_count, |
| 797 | vmcs12->vm_entry_msr_load_addr))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 803 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 804 | struct vmcs12 *vmcs12) |
| 805 | { |
| 806 | if (!nested_cpu_has_pml(vmcs12)) |
| 807 | return 0; |
| 808 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 809 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 810 | CC(!page_address_valid(vcpu, vmcs12->pml_address))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 811 | return -EINVAL; |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 817 | struct vmcs12 *vmcs12) |
| 818 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 819 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 820 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 821 | return -EINVAL; |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 826 | struct vmcs12 *vmcs12) |
| 827 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 828 | if (CC(nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 829 | !nested_cpu_has_ept(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 830 | return -EINVAL; |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 835 | struct vmcs12 *vmcs12) |
| 836 | { |
| 837 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 838 | return 0; |
| 839 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 840 | if (CC(!page_address_valid(vcpu, vmcs12->vmread_bitmap)) || |
| 841 | CC(!page_address_valid(vcpu, vmcs12->vmwrite_bitmap))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 842 | return -EINVAL; |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 848 | struct vmx_msr_entry *e) |
| 849 | { |
| 850 | /* x2APIC MSR accesses are not allowed */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 851 | if (CC(vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 852 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 853 | if (CC(e->index == MSR_IA32_UCODE_WRITE) || /* SDM Table 35-2 */ |
| 854 | CC(e->index == MSR_IA32_UCODE_REV)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 855 | return -EINVAL; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 856 | if (CC(e->reserved != 0)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 857 | return -EINVAL; |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 862 | struct vmx_msr_entry *e) |
| 863 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 864 | if (CC(e->index == MSR_FS_BASE) || |
| 865 | CC(e->index == MSR_GS_BASE) || |
| 866 | CC(e->index == MSR_IA32_SMM_MONITOR_CTL) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 867 | nested_vmx_msr_check_common(vcpu, e)) |
| 868 | return -EINVAL; |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 873 | struct vmx_msr_entry *e) |
| 874 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 875 | if (CC(e->index == MSR_IA32_SMBASE) || /* SMM is not supported */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 876 | nested_vmx_msr_check_common(vcpu, e)) |
| 877 | return -EINVAL; |
| 878 | return 0; |
| 879 | } |
| 880 | |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 881 | static u32 nested_vmx_max_atomic_switch_msrs(struct kvm_vcpu *vcpu) |
| 882 | { |
| 883 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 884 | u64 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 885 | vmx->nested.msrs.misc_high); |
| 886 | |
| 887 | return (vmx_misc_max_msr(vmx_misc) + 1) * VMX_MISC_MSR_LIST_MULTIPLIER; |
| 888 | } |
| 889 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 890 | /* |
| 891 | * Load guest's/host's msr at nested entry/exit. |
| 892 | * return 0 for success, entry index for failure. |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 893 | * |
| 894 | * One of the failure modes for MSR load/store is when a list exceeds the |
| 895 | * virtual hardware's capacity. To maintain compatibility with hardware inasmuch |
| 896 | * as possible, process all valid entries before failing rather than precheck |
| 897 | * for a capacity violation. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 898 | */ |
| 899 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 900 | { |
| 901 | u32 i; |
| 902 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 903 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 904 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 905 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 906 | if (unlikely(i >= max_msr_list_size)) |
| 907 | goto fail; |
| 908 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 909 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 910 | &e, sizeof(e))) { |
| 911 | pr_debug_ratelimited( |
| 912 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 913 | __func__, i, gpa + i * sizeof(e)); |
| 914 | goto fail; |
| 915 | } |
| 916 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 917 | pr_debug_ratelimited( |
| 918 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 919 | __func__, i, e.index, e.reserved); |
| 920 | goto fail; |
| 921 | } |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 922 | if (kvm_set_msr(vcpu, e.index, e.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 923 | pr_debug_ratelimited( |
| 924 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 925 | __func__, i, e.index, e.value); |
| 926 | goto fail; |
| 927 | } |
| 928 | } |
| 929 | return 0; |
| 930 | fail: |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 931 | /* Note, max_msr_list_size is at most 4096, i.e. this can't wrap. */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 932 | return i + 1; |
| 933 | } |
| 934 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 935 | static bool nested_vmx_get_vmexit_msr_value(struct kvm_vcpu *vcpu, |
| 936 | u32 msr_index, |
| 937 | u64 *data) |
| 938 | { |
| 939 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 940 | |
| 941 | /* |
| 942 | * If the L0 hypervisor stored a more accurate value for the TSC that |
| 943 | * does not include the time taken for emulation of the L2->L1 |
| 944 | * VM-exit in L0, use the more accurate value. |
| 945 | */ |
| 946 | if (msr_index == MSR_IA32_TSC) { |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 947 | int i = vmx_find_loadstore_msr_slot(&vmx->msr_autostore.guest, |
| 948 | MSR_IA32_TSC); |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 949 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 950 | if (i >= 0) { |
| 951 | u64 val = vmx->msr_autostore.guest.val[i].value; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 952 | |
| 953 | *data = kvm_read_l1_tsc(vcpu, val); |
| 954 | return true; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | if (kvm_get_msr(vcpu, msr_index, data)) { |
| 959 | pr_debug_ratelimited("%s cannot read MSR (0x%x)\n", __func__, |
| 960 | msr_index); |
| 961 | return false; |
| 962 | } |
| 963 | return true; |
| 964 | } |
| 965 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 966 | static bool read_and_check_msr_entry(struct kvm_vcpu *vcpu, u64 gpa, int i, |
| 967 | struct vmx_msr_entry *e) |
| 968 | { |
| 969 | if (kvm_vcpu_read_guest(vcpu, |
| 970 | gpa + i * sizeof(*e), |
| 971 | e, 2 * sizeof(u32))) { |
| 972 | pr_debug_ratelimited( |
| 973 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 974 | __func__, i, gpa + i * sizeof(*e)); |
| 975 | return false; |
| 976 | } |
| 977 | if (nested_vmx_store_msr_check(vcpu, e)) { |
| 978 | pr_debug_ratelimited( |
| 979 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 980 | __func__, i, e->index, e->reserved); |
| 981 | return false; |
| 982 | } |
| 983 | return true; |
| 984 | } |
| 985 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 986 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 987 | { |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 988 | u64 data; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 989 | u32 i; |
| 990 | struct vmx_msr_entry e; |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 991 | u32 max_msr_list_size = nested_vmx_max_atomic_switch_msrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 992 | |
| 993 | for (i = 0; i < count; i++) { |
Marc Orr | f0b5105 | 2019-09-17 11:50:57 -0700 | [diff] [blame] | 994 | if (unlikely(i >= max_msr_list_size)) |
| 995 | return -EINVAL; |
| 996 | |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 997 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 998 | return -EINVAL; |
Aaron Lewis | 365d3d5 | 2019-11-07 21:14:36 -0800 | [diff] [blame] | 999 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1000 | if (!nested_vmx_get_vmexit_msr_value(vcpu, e.index, &data)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1001 | return -EINVAL; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1002 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1003 | if (kvm_vcpu_write_guest(vcpu, |
| 1004 | gpa + i * sizeof(e) + |
| 1005 | offsetof(struct vmx_msr_entry, value), |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1006 | &data, sizeof(data))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1007 | pr_debug_ratelimited( |
| 1008 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 1009 | __func__, i, e.index, data); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | } |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1016 | static bool nested_msr_store_list_has_msr(struct kvm_vcpu *vcpu, u32 msr_index) |
| 1017 | { |
| 1018 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1019 | u32 count = vmcs12->vm_exit_msr_store_count; |
| 1020 | u64 gpa = vmcs12->vm_exit_msr_store_addr; |
| 1021 | struct vmx_msr_entry e; |
| 1022 | u32 i; |
| 1023 | |
| 1024 | for (i = 0; i < count; i++) { |
| 1025 | if (!read_and_check_msr_entry(vcpu, gpa, i, &e)) |
| 1026 | return false; |
| 1027 | |
| 1028 | if (e.index == msr_index) |
| 1029 | return true; |
| 1030 | } |
| 1031 | return false; |
| 1032 | } |
| 1033 | |
| 1034 | static void prepare_vmx_msr_autostore_list(struct kvm_vcpu *vcpu, |
| 1035 | u32 msr_index) |
| 1036 | { |
| 1037 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1038 | struct vmx_msrs *autostore = &vmx->msr_autostore.guest; |
| 1039 | bool in_vmcs12_store_list; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1040 | int msr_autostore_slot; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1041 | bool in_autostore_list; |
| 1042 | int last; |
| 1043 | |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1044 | msr_autostore_slot = vmx_find_loadstore_msr_slot(autostore, msr_index); |
| 1045 | in_autostore_list = msr_autostore_slot >= 0; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1046 | in_vmcs12_store_list = nested_msr_store_list_has_msr(vcpu, msr_index); |
| 1047 | |
| 1048 | if (in_vmcs12_store_list && !in_autostore_list) { |
Sean Christopherson | ce833b2 | 2020-09-23 11:03:56 -0700 | [diff] [blame] | 1049 | if (autostore->nr == MAX_NR_LOADSTORE_MSRS) { |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1050 | /* |
| 1051 | * Emulated VMEntry does not fail here. Instead a less |
| 1052 | * accurate value will be returned by |
| 1053 | * nested_vmx_get_vmexit_msr_value() using kvm_get_msr() |
| 1054 | * instead of reading the value from the vmcs02 VMExit |
| 1055 | * MSR-store area. |
| 1056 | */ |
| 1057 | pr_warn_ratelimited( |
| 1058 | "Not enough msr entries in msr_autostore. Can't add msr %x\n", |
| 1059 | msr_index); |
| 1060 | return; |
| 1061 | } |
| 1062 | last = autostore->nr++; |
| 1063 | autostore->val[last].index = msr_index; |
| 1064 | } else if (!in_vmcs12_store_list && in_autostore_list) { |
| 1065 | last = --autostore->nr; |
Sean Christopherson | a128a93 | 2020-09-23 11:03:57 -0700 | [diff] [blame] | 1066 | autostore->val[msr_autostore_slot] = autostore->val[last]; |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1070 | /* |
Sean Christopherson | ea79a75 | 2020-02-04 07:32:59 -0800 | [diff] [blame] | 1071 | * Load guest's/host's cr3 at nested entry/exit. @nested_ept is true if we are |
| 1072 | * emulating VM-Entry into a guest with EPT enabled. On failure, the expected |
| 1073 | * Exit Qualification (for a VM-Entry consistency check VM-Exit) is assigned to |
| 1074 | * @entry_failure_code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1075 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1076 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, |
| 1077 | bool nested_ept, bool reload_pdptrs, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 1078 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1079 | { |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 1080 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, cr3))) { |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1081 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 1082 | return -EINVAL; |
| 1083 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1084 | |
Sean Christopherson | 0cc6920 | 2020-05-01 21:32:26 -0700 | [diff] [blame] | 1085 | /* |
| 1086 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 1087 | * must not be dereferenced. |
| 1088 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 1089 | if (reload_pdptrs && !nested_ept && is_pae_paging(vcpu) && |
Sean Christopherson | bcb72d0 | 2021-06-07 12:01:56 +0300 | [diff] [blame] | 1090 | CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))) { |
| 1091 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 1092 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1095 | if (!nested_ept) |
Sean Christopherson | b512910 | 2021-06-09 16:42:27 -0700 | [diff] [blame] | 1096 | kvm_mmu_new_pgd(vcpu, cr3); |
Sean Christopherson | 07ffaf3 | 2021-06-09 16:42:21 -0700 | [diff] [blame] | 1097 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1098 | vcpu->arch.cr3 = cr3; |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 1099 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1100 | |
Sean Christopherson | c906066 | 2021-06-09 16:42:33 -0700 | [diff] [blame^] | 1101 | kvm_init_mmu(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Returns if KVM is able to config CPU to tag TLB entries |
| 1108 | * populated by L2 differently than TLB entries populated |
| 1109 | * by L1. |
| 1110 | * |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1111 | * If L0 uses EPT, L1 and L2 run with different EPTP because |
| 1112 | * guest_mode is part of kvm_mmu_page_role. Thus, TLB entries |
| 1113 | * are tagged with different EPTP. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1114 | * |
| 1115 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 1116 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 1117 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 1118 | */ |
| 1119 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
| 1120 | { |
| 1121 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1122 | |
Liran Alon | 992edea | 2019-11-20 14:24:52 +0200 | [diff] [blame] | 1123 | return enable_ept || |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1124 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 1125 | } |
| 1126 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1127 | static void nested_vmx_transition_tlb_flush(struct kvm_vcpu *vcpu, |
| 1128 | struct vmcs12 *vmcs12, |
| 1129 | bool is_vmenter) |
| 1130 | { |
| 1131 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1132 | |
| 1133 | /* |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1134 | * If vmcs12 doesn't use VPID, L1 expects linear and combined mappings |
| 1135 | * for *all* contexts to be flushed on VM-Enter/VM-Exit, i.e. it's a |
| 1136 | * full TLB flush from the guest's perspective. This is required even |
| 1137 | * if VPID is disabled in the host as KVM may need to synchronize the |
| 1138 | * MMU in response to the guest TLB flush. |
| 1139 | * |
| 1140 | * Note, using TLB_FLUSH_GUEST is correct even if nested EPT is in use. |
| 1141 | * EPT is a special snowflake, as guest-physical mappings aren't |
| 1142 | * flushed on VPID invalidations, including VM-Enter or VM-Exit with |
| 1143 | * VPID disabled. As a result, KVM _never_ needs to sync nEPT |
| 1144 | * entries on VM-Enter because L1 can't rely on VM-Enter to flush |
| 1145 | * those mappings. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1146 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1147 | if (!nested_cpu_has_vpid(vmcs12)) { |
| 1148 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1149 | return; |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* L2 should never have a VPID if VPID is disabled. */ |
| 1153 | WARN_ON(!enable_vpid); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1156 | * If VPID is enabled and used by vmc12, but L2 does not have a unique |
| 1157 | * TLB tag (ASID), i.e. EPT is disabled and KVM was unable to allocate |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1158 | * a VPID for L2, flush the current context as the effective ASID is |
| 1159 | * common to both L1 and L2. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1160 | * |
| 1161 | * Defer the flush so that it runs after vmcs02.EPTP has been set by |
| 1162 | * KVM_REQ_LOAD_MMU_PGD (if nested EPT is enabled) and to avoid |
| 1163 | * redundant flushes further down the nested pipeline. |
| 1164 | * |
| 1165 | * If a TLB flush isn't required due to any of the above, and vpid12 is |
| 1166 | * changing then the new "virtual" VPID (vpid12) will reuse the same |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1167 | * "real" VPID (vpid02), and so needs to be flushed. There's no direct |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1168 | * mapping between vpid02 and vpid12, vpid02 is per-vCPU and reused for |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1169 | * all nested vCPUs. Remember, a flush on VM-Enter does not invalidate |
| 1170 | * guest-physical mappings, so there is no need to sync the nEPT MMU. |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1171 | */ |
Sean Christopherson | 50a4179 | 2021-06-09 16:42:28 -0700 | [diff] [blame] | 1172 | if (!nested_has_guest_tlb_tag(vcpu)) { |
Sean Christopherson | c51e1ff | 2020-03-20 14:28:22 -0700 | [diff] [blame] | 1173 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 1174 | } else if (is_vmenter && |
| 1175 | vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 1176 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 1177 | vpid_sync_context(nested_get_vpid02(vcpu)); |
| 1178 | } |
| 1179 | } |
| 1180 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1181 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 1182 | { |
| 1183 | superset &= mask; |
| 1184 | subset &= mask; |
| 1185 | |
| 1186 | return (superset | subset) == superset; |
| 1187 | } |
| 1188 | |
| 1189 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 1190 | { |
| 1191 | const u64 feature_and_reserved = |
| 1192 | /* feature (except bit 48; see below) */ |
| 1193 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 1194 | /* reserved */ |
| 1195 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 1196 | u64 vmx_basic = vmx->nested.msrs.basic; |
| 1197 | |
| 1198 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 1199 | return -EINVAL; |
| 1200 | |
| 1201 | /* |
| 1202 | * KVM does not emulate a version of VMX that constrains physical |
| 1203 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 1204 | */ |
| 1205 | if (data & BIT_ULL(48)) |
| 1206 | return -EINVAL; |
| 1207 | |
| 1208 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 1209 | vmx_basic_vmcs_revision_id(data)) |
| 1210 | return -EINVAL; |
| 1211 | |
| 1212 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | vmx->nested.msrs.basic = data; |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | static int |
| 1220 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1221 | { |
| 1222 | u64 supported; |
| 1223 | u32 *lowp, *highp; |
| 1224 | |
| 1225 | switch (msr_index) { |
| 1226 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1227 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 1228 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
| 1229 | break; |
| 1230 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1231 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 1232 | highp = &vmx->nested.msrs.procbased_ctls_high; |
| 1233 | break; |
| 1234 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1235 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 1236 | highp = &vmx->nested.msrs.exit_ctls_high; |
| 1237 | break; |
| 1238 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1239 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 1240 | highp = &vmx->nested.msrs.entry_ctls_high; |
| 1241 | break; |
| 1242 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1243 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 1244 | highp = &vmx->nested.msrs.secondary_ctls_high; |
| 1245 | break; |
| 1246 | default: |
| 1247 | BUG(); |
| 1248 | } |
| 1249 | |
| 1250 | supported = vmx_control_msr(*lowp, *highp); |
| 1251 | |
| 1252 | /* Check must-be-1 bits are still 1. */ |
| 1253 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 1254 | return -EINVAL; |
| 1255 | |
| 1256 | /* Check must-be-0 bits are still 0. */ |
| 1257 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 1258 | return -EINVAL; |
| 1259 | |
| 1260 | *lowp = data; |
| 1261 | *highp = data >> 32; |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 1266 | { |
| 1267 | const u64 feature_and_reserved_bits = |
| 1268 | /* feature */ |
| 1269 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 1270 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 1271 | /* reserved */ |
| 1272 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 1273 | u64 vmx_misc; |
| 1274 | |
| 1275 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 1276 | vmx->nested.msrs.misc_high); |
| 1277 | |
| 1278 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 1279 | return -EINVAL; |
| 1280 | |
| 1281 | if ((vmx->nested.msrs.pinbased_ctls_high & |
| 1282 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 1283 | vmx_misc_preemption_timer_rate(data) != |
| 1284 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 1285 | return -EINVAL; |
| 1286 | |
| 1287 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 1288 | return -EINVAL; |
| 1289 | |
| 1290 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 1294 | return -EINVAL; |
| 1295 | |
| 1296 | vmx->nested.msrs.misc_low = data; |
| 1297 | vmx->nested.msrs.misc_high = data >> 32; |
| 1298 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 1303 | { |
| 1304 | u64 vmx_ept_vpid_cap; |
| 1305 | |
| 1306 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 1307 | vmx->nested.msrs.vpid_caps); |
| 1308 | |
| 1309 | /* Every bit is either reserved or a feature bit. */ |
| 1310 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 1311 | return -EINVAL; |
| 1312 | |
| 1313 | vmx->nested.msrs.ept_caps = data; |
| 1314 | vmx->nested.msrs.vpid_caps = data >> 32; |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 1319 | { |
| 1320 | u64 *msr; |
| 1321 | |
| 1322 | switch (msr_index) { |
| 1323 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1324 | msr = &vmx->nested.msrs.cr0_fixed0; |
| 1325 | break; |
| 1326 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1327 | msr = &vmx->nested.msrs.cr4_fixed0; |
| 1328 | break; |
| 1329 | default: |
| 1330 | BUG(); |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 1335 | * must be 1 in the restored value. |
| 1336 | */ |
| 1337 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 1338 | return -EINVAL; |
| 1339 | |
| 1340 | *msr = data; |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * Called when userspace is restoring VMX MSRs. |
| 1346 | * |
| 1347 | * Returns 0 on success, non-0 otherwise. |
| 1348 | */ |
| 1349 | int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1350 | { |
| 1351 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1352 | |
| 1353 | /* |
| 1354 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 1355 | * is in VMX operation. |
| 1356 | */ |
| 1357 | if (vmx->nested.vmxon) |
| 1358 | return -EBUSY; |
| 1359 | |
| 1360 | switch (msr_index) { |
| 1361 | case MSR_IA32_VMX_BASIC: |
| 1362 | return vmx_restore_vmx_basic(vmx, data); |
| 1363 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1364 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1365 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1366 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1367 | /* |
| 1368 | * The "non-true" VMX capability MSRs are generated from the |
| 1369 | * "true" MSRs, so we do not support restoring them directly. |
| 1370 | * |
| 1371 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 1372 | * should restore the "true" MSRs with the must-be-1 bits |
| 1373 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 1374 | * DEFAULT SETTINGS". |
| 1375 | */ |
| 1376 | return -EINVAL; |
| 1377 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1378 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1379 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1380 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1381 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1382 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 1383 | case MSR_IA32_VMX_MISC: |
| 1384 | return vmx_restore_vmx_misc(vmx, data); |
| 1385 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1386 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1387 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 1388 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1389 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1390 | /* |
| 1391 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 1392 | * do not support restoring them directly. |
| 1393 | */ |
| 1394 | return -EINVAL; |
| 1395 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1396 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 1397 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1398 | vmx->nested.msrs.vmcs_enum = data; |
| 1399 | return 0; |
Paolo Bonzini | e8a70bd | 2019-07-02 14:40:40 +0200 | [diff] [blame] | 1400 | case MSR_IA32_VMX_VMFUNC: |
| 1401 | if (data & ~vmx->nested.msrs.vmfunc_controls) |
| 1402 | return -EINVAL; |
| 1403 | vmx->nested.msrs.vmfunc_controls = data; |
| 1404 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1405 | default: |
| 1406 | /* |
| 1407 | * The rest of the VMX capability MSRs do not support restore. |
| 1408 | */ |
| 1409 | return -EINVAL; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /* Returns 0 on success, non-0 otherwise. */ |
| 1414 | int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
| 1415 | { |
| 1416 | switch (msr_index) { |
| 1417 | case MSR_IA32_VMX_BASIC: |
| 1418 | *pdata = msrs->basic; |
| 1419 | break; |
| 1420 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1421 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1422 | *pdata = vmx_control_msr( |
| 1423 | msrs->pinbased_ctls_low, |
| 1424 | msrs->pinbased_ctls_high); |
| 1425 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 1426 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1427 | break; |
| 1428 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1429 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1430 | *pdata = vmx_control_msr( |
| 1431 | msrs->procbased_ctls_low, |
| 1432 | msrs->procbased_ctls_high); |
| 1433 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 1434 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1435 | break; |
| 1436 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1437 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1438 | *pdata = vmx_control_msr( |
| 1439 | msrs->exit_ctls_low, |
| 1440 | msrs->exit_ctls_high); |
| 1441 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 1442 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1443 | break; |
| 1444 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1445 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1446 | *pdata = vmx_control_msr( |
| 1447 | msrs->entry_ctls_low, |
| 1448 | msrs->entry_ctls_high); |
| 1449 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 1450 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 1451 | break; |
| 1452 | case MSR_IA32_VMX_MISC: |
| 1453 | *pdata = vmx_control_msr( |
| 1454 | msrs->misc_low, |
| 1455 | msrs->misc_high); |
| 1456 | break; |
| 1457 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1458 | *pdata = msrs->cr0_fixed0; |
| 1459 | break; |
| 1460 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1461 | *pdata = msrs->cr0_fixed1; |
| 1462 | break; |
| 1463 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1464 | *pdata = msrs->cr4_fixed0; |
| 1465 | break; |
| 1466 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1467 | *pdata = msrs->cr4_fixed1; |
| 1468 | break; |
| 1469 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1470 | *pdata = msrs->vmcs_enum; |
| 1471 | break; |
| 1472 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1473 | *pdata = vmx_control_msr( |
| 1474 | msrs->secondary_ctls_low, |
| 1475 | msrs->secondary_ctls_high); |
| 1476 | break; |
| 1477 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1478 | *pdata = msrs->ept_caps | |
| 1479 | ((u64)msrs->vpid_caps << 32); |
| 1480 | break; |
| 1481 | case MSR_IA32_VMX_VMFUNC: |
| 1482 | *pdata = msrs->vmfunc_controls; |
| 1483 | break; |
| 1484 | default: |
| 1485 | return 1; |
| 1486 | } |
| 1487 | |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1492 | * Copy the writable VMCS shadow fields back to the VMCS12, in case they have |
| 1493 | * been modified by the L1 guest. Note, "writable" in this context means |
| 1494 | * "writable by the guest", i.e. tagged SHADOW_FIELD_RW; the set of |
| 1495 | * fields tagged SHADOW_FIELD_RO may or may not align with the "read-only" |
| 1496 | * VM-exit information fields (which are actually writable if the vCPU is |
| 1497 | * configured to support "VMWRITE to any supported field in the VMCS"). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1498 | */ |
| 1499 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 1500 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1501 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1502 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1503 | struct shadow_vmcs_field field; |
| 1504 | unsigned long val; |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1505 | int i; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1506 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1507 | if (WARN_ON(!shadow_vmcs)) |
| 1508 | return; |
| 1509 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1510 | preempt_disable(); |
| 1511 | |
| 1512 | vmcs_load(shadow_vmcs); |
| 1513 | |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 1514 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 1515 | field = shadow_read_write_fields[i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1516 | val = __vmcs_readl(field.encoding); |
| 1517 | vmcs12_write_any(vmcs12, field.encoding, field.offset, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | vmcs_clear(shadow_vmcs); |
| 1521 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1522 | |
| 1523 | preempt_enable(); |
| 1524 | } |
| 1525 | |
| 1526 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 1527 | { |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1528 | const struct shadow_vmcs_field *fields[] = { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1529 | shadow_read_write_fields, |
| 1530 | shadow_read_only_fields |
| 1531 | }; |
| 1532 | const int max_fields[] = { |
| 1533 | max_shadow_read_write_fields, |
| 1534 | max_shadow_read_only_fields |
| 1535 | }; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1536 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1537 | struct vmcs12 *vmcs12 = get_vmcs12(&vmx->vcpu); |
| 1538 | struct shadow_vmcs_field field; |
| 1539 | unsigned long val; |
| 1540 | int i, q; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1541 | |
Paolo Bonzini | 88dddc1 | 2019-07-19 18:41:10 +0200 | [diff] [blame] | 1542 | if (WARN_ON(!shadow_vmcs)) |
| 1543 | return; |
| 1544 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1545 | vmcs_load(shadow_vmcs); |
| 1546 | |
| 1547 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 1548 | for (i = 0; i < max_fields[q]; i++) { |
| 1549 | field = fields[q][i]; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 1550 | val = vmcs12_read_any(vmcs12, field.encoding, |
| 1551 | field.offset); |
| 1552 | __vmcs_writel(field.encoding, val); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | vmcs_clear(shadow_vmcs); |
| 1557 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 1558 | } |
| 1559 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1560 | static void copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx, u32 hv_clean_fields) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1561 | { |
| 1562 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1563 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1564 | |
| 1565 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 1566 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 1567 | vmcs12->guest_rip = evmcs->guest_rip; |
| 1568 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1569 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1570 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 1571 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 1572 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 1573 | vmcs12->guest_interruptibility_info = |
| 1574 | evmcs->guest_interruptibility_info; |
| 1575 | } |
| 1576 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1577 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1578 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 1579 | vmcs12->cpu_based_vm_exec_control = |
| 1580 | evmcs->cpu_based_vm_exec_control; |
| 1581 | } |
| 1582 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1583 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1584 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EXCPN))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1585 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 1586 | } |
| 1587 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1588 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1589 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 1590 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 1591 | } |
| 1592 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1593 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1594 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 1595 | vmcs12->vm_entry_intr_info_field = |
| 1596 | evmcs->vm_entry_intr_info_field; |
| 1597 | vmcs12->vm_entry_exception_error_code = |
| 1598 | evmcs->vm_entry_exception_error_code; |
| 1599 | vmcs12->vm_entry_instruction_len = |
| 1600 | evmcs->vm_entry_instruction_len; |
| 1601 | } |
| 1602 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1603 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1604 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 1605 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 1606 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 1607 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 1608 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 1609 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 1610 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 1611 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 1612 | vmcs12->host_rip = evmcs->host_rip; |
| 1613 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 1614 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 1615 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 1616 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 1617 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 1618 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 1619 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 1620 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 1621 | } |
| 1622 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1623 | if (unlikely(!(hv_clean_fields & |
Vitaly Kuznetsov | f9bc522 | 2019-06-13 13:35:02 +0200 | [diff] [blame] | 1624 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP1))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1625 | vmcs12->pin_based_vm_exec_control = |
| 1626 | evmcs->pin_based_vm_exec_control; |
| 1627 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 1628 | vmcs12->secondary_vm_exec_control = |
| 1629 | evmcs->secondary_vm_exec_control; |
| 1630 | } |
| 1631 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1632 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1633 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 1634 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 1635 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 1636 | } |
| 1637 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1638 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1639 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 1640 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 1641 | } |
| 1642 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1643 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1644 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 1645 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 1646 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 1647 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 1648 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 1649 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 1650 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 1651 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 1652 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 1653 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 1654 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 1655 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 1656 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 1657 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 1658 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 1659 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 1660 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 1661 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 1662 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 1663 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 1664 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 1665 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 1666 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 1667 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 1668 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 1669 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 1670 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 1671 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 1672 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 1673 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 1674 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 1675 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 1676 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 1677 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 1678 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 1679 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 1680 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 1681 | } |
| 1682 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1683 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1684 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 1685 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 1686 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 1687 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 1688 | } |
| 1689 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1690 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1691 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 1692 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 1693 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 1694 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 1695 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 1696 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 1697 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 1698 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 1699 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 1700 | } |
| 1701 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1702 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1703 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 1704 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 1705 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 1706 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 1707 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 1708 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 1709 | vmcs12->host_rsp = evmcs->host_rsp; |
| 1710 | } |
| 1711 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1712 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1713 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 1714 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 1715 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 1716 | } |
| 1717 | |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 1718 | if (unlikely(!(hv_clean_fields & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1719 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 1720 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 1721 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 1722 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 1723 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 1724 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 1725 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 1726 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 1727 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 1728 | vmcs12->guest_pending_dbg_exceptions = |
| 1729 | evmcs->guest_pending_dbg_exceptions; |
| 1730 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 1731 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 1732 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 1733 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 1734 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * Not used? |
| 1739 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 1740 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 1741 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1742 | * vmcs12->page_fault_error_code_mask = |
| 1743 | * evmcs->page_fault_error_code_mask; |
| 1744 | * vmcs12->page_fault_error_code_match = |
| 1745 | * evmcs->page_fault_error_code_match; |
| 1746 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 1747 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 1748 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 1749 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 1750 | */ |
| 1751 | |
| 1752 | /* |
| 1753 | * Read only fields: |
| 1754 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 1755 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 1756 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 1757 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 1758 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 1759 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 1760 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 1761 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 1762 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 1763 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 1764 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 1765 | * |
| 1766 | * Not present in struct vmcs12: |
| 1767 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 1768 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 1769 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 1770 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 1771 | */ |
| 1772 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1773 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1776 | static void copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1777 | { |
| 1778 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 1779 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 1780 | |
| 1781 | /* |
| 1782 | * Should not be changed by KVM: |
| 1783 | * |
| 1784 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 1785 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 1786 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 1787 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 1788 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 1789 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 1790 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 1791 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 1792 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 1793 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 1794 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 1795 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 1796 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 1797 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 1798 | * evmcs->host_rip = vmcs12->host_rip; |
| 1799 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 1800 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 1801 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 1802 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 1803 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 1804 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 1805 | * evmcs->host_rsp = vmcs12->host_rsp; |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 1806 | * sync_vmcs02_to_vmcs12() doesn't read these: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1807 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 1808 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 1809 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 1810 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 1811 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 1812 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 1813 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 1814 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1815 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 1816 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 1817 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 1818 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 1819 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 1820 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 1821 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 1822 | * evmcs->page_fault_error_code_mask = |
| 1823 | * vmcs12->page_fault_error_code_mask; |
| 1824 | * evmcs->page_fault_error_code_match = |
| 1825 | * vmcs12->page_fault_error_code_match; |
| 1826 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 1827 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 1828 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 1829 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 1830 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 1831 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 1832 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 1833 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 1834 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 1835 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 1836 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 1837 | * |
| 1838 | * Not present in struct vmcs12: |
| 1839 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 1840 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 1841 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 1842 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 1843 | */ |
| 1844 | |
| 1845 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 1846 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 1847 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 1848 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 1849 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 1850 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 1851 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 1852 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 1853 | |
| 1854 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 1855 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 1856 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 1857 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 1858 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 1859 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 1860 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 1861 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 1862 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 1863 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 1864 | |
| 1865 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 1866 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 1867 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 1868 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 1869 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 1870 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 1871 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 1872 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 1873 | |
| 1874 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 1875 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 1876 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 1877 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 1878 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 1879 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 1880 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 1881 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 1882 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 1883 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 1884 | |
| 1885 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 1886 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 1887 | |
| 1888 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 1889 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 1890 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 1891 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 1892 | |
| 1893 | evmcs->guest_pending_dbg_exceptions = |
| 1894 | vmcs12->guest_pending_dbg_exceptions; |
| 1895 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 1896 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 1897 | |
| 1898 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 1899 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 1900 | |
| 1901 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 1902 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 1903 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 1904 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 1905 | |
| 1906 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 1907 | |
| 1908 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 1909 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 1910 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 1911 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 1912 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 1913 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 1914 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 1915 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 1916 | |
| 1917 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 1918 | |
| 1919 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 1920 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 1921 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 1922 | |
| 1923 | evmcs->guest_interruptibility_info = |
| 1924 | vmcs12->guest_interruptibility_info; |
| 1925 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 1926 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 1927 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 1928 | evmcs->vm_entry_exception_error_code = |
| 1929 | vmcs12->vm_entry_exception_error_code; |
| 1930 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 1931 | |
| 1932 | evmcs->guest_rip = vmcs12->guest_rip; |
| 1933 | |
| 1934 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 1935 | |
Vitaly Kuznetsov | 25641ca | 2021-05-26 15:20:19 +0200 | [diff] [blame] | 1936 | return; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 1941 | * instruction. |
| 1942 | */ |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1943 | static enum nested_evmptrld_status nested_vmx_handle_enlightened_vmptrld( |
| 1944 | struct kvm_vcpu *vcpu, bool from_launch) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1945 | { |
| 1946 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1947 | bool evmcs_gpa_changed = false; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1948 | u64 evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1949 | |
| 1950 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1951 | return EVMPTRLD_DISABLED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1952 | |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1953 | if (!nested_enlightened_vmentry(vcpu, &evmcs_gpa)) { |
| 1954 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1955 | return EVMPTRLD_DISABLED; |
Vitaly Kuznetsov | 0276171 | 2021-05-26 15:20:18 +0200 | [diff] [blame] | 1956 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1957 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 1958 | if (unlikely(evmcs_gpa != vmx->nested.hv_evmcs_vmptr)) { |
| 1959 | vmx->nested.current_vmptr = -1ull; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1960 | |
| 1961 | nested_release_evmcs(vcpu); |
| 1962 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1963 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(evmcs_gpa), |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1964 | &vmx->nested.hv_evmcs_map)) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1965 | return EVMPTRLD_ERROR; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1966 | |
KarimAllah Ahmed | dee9c04 | 2019-01-31 21:24:42 +0100 | [diff] [blame] | 1967 | vmx->nested.hv_evmcs = vmx->nested.hv_evmcs_map.hva; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1968 | |
| 1969 | /* |
| 1970 | * Currently, KVM only supports eVMCS version 1 |
| 1971 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 1972 | * value to first u32 field of eVMCS which should specify eVMCS |
| 1973 | * VersionNumber. |
| 1974 | * |
| 1975 | * Guest should be aware of supported eVMCS versions by host by |
| 1976 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 1977 | * expected to set this CPUID leaf according to the value |
| 1978 | * returned in vmcs_version from nested_enable_evmcs(). |
| 1979 | * |
| 1980 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 1981 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 1982 | * just sets first u32 field of eVMCS to revision_id specified |
| 1983 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 1984 | * which is one of the supported versions specified in |
| 1985 | * CPUID.0x4000000A.EAX[0:15]. |
| 1986 | * |
| 1987 | * To overcome Hyper-V bug, we accept here either a supported |
| 1988 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 1989 | * u32 field of eVMCS. |
| 1990 | */ |
| 1991 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 1992 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
| 1993 | nested_release_evmcs(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 1994 | return EVMPTRLD_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1995 | } |
| 1996 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 1997 | vmx->nested.hv_evmcs_vmptr = evmcs_gpa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 1998 | |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 1999 | evmcs_gpa_changed = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2000 | /* |
| 2001 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 2002 | * reloaded from guest's memory (read only fields, fields not |
| 2003 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 2004 | * are no leftovers. |
| 2005 | */ |
| 2006 | if (from_launch) { |
| 2007 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2008 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 2009 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 2010 | } |
| 2011 | |
| 2012 | } |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2013 | |
| 2014 | /* |
Miaohe Lin | ffdbd50 | 2020-02-07 23:22:45 +0800 | [diff] [blame] | 2015 | * Clean fields data can't be used on VMLAUNCH and when we switch |
Vitaly Kuznetsov | a21a39c | 2019-06-28 13:23:32 +0200 | [diff] [blame] | 2016 | * between different L2 guests as KVM keeps a single VMCS12 per L1. |
| 2017 | */ |
| 2018 | if (from_launch || evmcs_gpa_changed) |
| 2019 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 2020 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2021 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 2022 | return EVMPTRLD_SUCCEEDED; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2023 | } |
| 2024 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2025 | void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2026 | { |
| 2027 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2028 | |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2029 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2030 | copy_vmcs12_to_enlightened(vmx); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2031 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2032 | copy_vmcs12_to_shadow(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2033 | |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 2034 | vmx->nested.need_vmcs12_to_shadow_sync = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 2038 | { |
| 2039 | struct vcpu_vmx *vmx = |
| 2040 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 2041 | |
| 2042 | vmx->nested.preemption_timer_expired = true; |
| 2043 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 2044 | kvm_vcpu_kick(&vmx->vcpu); |
| 2045 | |
| 2046 | return HRTIMER_NORESTART; |
| 2047 | } |
| 2048 | |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2049 | static u64 vmx_calc_preemption_timer_value(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2050 | { |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2051 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2052 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2053 | |
| 2054 | u64 l1_scaled_tsc = kvm_read_l1_tsc(vcpu, rdtsc()) >> |
| 2055 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2056 | |
| 2057 | if (!vmx->nested.has_preemption_timer_deadline) { |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2058 | vmx->nested.preemption_timer_deadline = |
| 2059 | vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2060 | vmx->nested.has_preemption_timer_deadline = true; |
Makarand Sonare | 8d7fbf0 | 2020-05-26 14:51:07 -0700 | [diff] [blame] | 2061 | } |
| 2062 | return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu, |
| 2066 | u64 preemption_timeout) |
| 2067 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2068 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2069 | |
| 2070 | /* |
| 2071 | * A timer value of zero is architecturally guaranteed to cause |
| 2072 | * a VMExit prior to executing any instructions in the guest. |
| 2073 | */ |
| 2074 | if (preemption_timeout == 0) { |
| 2075 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 2076 | return; |
| 2077 | } |
| 2078 | |
| 2079 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 2080 | return; |
| 2081 | |
| 2082 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 2083 | preemption_timeout *= 1000000; |
| 2084 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 2085 | hrtimer_start(&vmx->nested.preemption_timer, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 2086 | ktime_add_ns(ktime_get(), preemption_timeout), |
| 2087 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2091 | { |
| 2092 | if (vmx->nested.nested_run_pending && |
| 2093 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 2094 | return vmcs12->guest_ia32_efer; |
| 2095 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 2096 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 2097 | else |
| 2098 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 2099 | } |
| 2100 | |
| 2101 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
| 2102 | { |
| 2103 | /* |
| 2104 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
| 2105 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 2106 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 2107 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
| 2108 | */ |
| 2109 | if (vmx->nested.vmcs02_initialized) |
| 2110 | return; |
| 2111 | vmx->nested.vmcs02_initialized = true; |
| 2112 | |
| 2113 | /* |
| 2114 | * We don't care what the EPTP value is we just need to guarantee |
| 2115 | * it's valid so we don't get a false positive when doing early |
| 2116 | * consistency checks. |
| 2117 | */ |
| 2118 | if (enable_ept && nested_early_check) |
Sean Christopherson | 2a40b90 | 2020-07-15 20:41:18 -0700 | [diff] [blame] | 2119 | vmcs_write64(EPT_POINTER, |
| 2120 | construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2121 | |
| 2122 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 2123 | if (cpu_has_vmx_vmfunc()) |
| 2124 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 2125 | |
| 2126 | if (cpu_has_vmx_posted_intr()) |
| 2127 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 2128 | |
| 2129 | if (cpu_has_vmx_msr_bitmap()) |
| 2130 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 2131 | |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2132 | /* |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2133 | * PML is emulated for L2, but never enabled in hardware as the MMU |
| 2134 | * handles A/D emulation. Disabling PML for L2 also avoids having to |
| 2135 | * deal with filtering out L2 GPAs from the buffer. |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2136 | */ |
| 2137 | if (enable_pml) { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2138 | vmcs_write64(PML_ADDRESS, 0); |
| 2139 | vmcs_write16(GUEST_PML_INDEX, -1); |
Sean Christopherson | 4d6c989 | 2019-05-07 09:06:30 -0700 | [diff] [blame] | 2140 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2141 | |
Sean Christopherson | c538d57 | 2019-05-07 09:06:29 -0700 | [diff] [blame] | 2142 | if (cpu_has_vmx_encls_vmexit()) |
| 2143 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2144 | |
| 2145 | /* |
| 2146 | * Set the MSR load/store lists to match L0's settings. Only the |
| 2147 | * addresses are constant (for vmcs02), the counts can change based |
| 2148 | * on L2's behavior, e.g. switching to/from long mode. |
| 2149 | */ |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2150 | vmcs_write64(VM_EXIT_MSR_STORE_ADDR, __pa(vmx->msr_autostore.guest.val)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2151 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 2152 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
| 2153 | |
| 2154 | vmx_set_constant_host_state(vmx); |
| 2155 | } |
| 2156 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2157 | static void prepare_vmcs02_early_rare(struct vcpu_vmx *vmx, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2158 | struct vmcs12 *vmcs12) |
| 2159 | { |
| 2160 | prepare_vmcs02_constant_state(vmx); |
| 2161 | |
| 2162 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 2163 | |
| 2164 | if (enable_vpid) { |
| 2165 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 2166 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 2167 | else |
| 2168 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 2173 | { |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2174 | u32 exec_control; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2175 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2176 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2177 | if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2178 | prepare_vmcs02_early_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2179 | |
| 2180 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2181 | * PIN CONTROLS |
| 2182 | */ |
Sean Christopherson | c075c3e | 2019-05-07 12:17:53 -0700 | [diff] [blame] | 2183 | exec_control = vmx_pin_based_exec_ctrl(vmx); |
Sean Christopherson | 804939e | 2019-05-07 12:18:05 -0700 | [diff] [blame] | 2184 | exec_control |= (vmcs12->pin_based_vm_exec_control & |
| 2185 | ~PIN_BASED_VMX_PREEMPTION_TIMER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2186 | |
| 2187 | /* Posted interrupts setting is only taken from vmcs12. */ |
| 2188 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 2189 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 2190 | vmx->nested.pi_pending = false; |
| 2191 | } else { |
| 2192 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 2193 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2194 | pin_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2195 | |
| 2196 | /* |
| 2197 | * EXEC CONTROLS |
| 2198 | */ |
| 2199 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 2200 | exec_control &= ~CPU_BASED_INTR_WINDOW_EXITING; |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2201 | exec_control &= ~CPU_BASED_NMI_WINDOW_EXITING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2202 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2203 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 2204 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 2205 | vmx->nested.l1_tpr_threshold = -1; |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2206 | if (exec_control & CPU_BASED_TPR_SHADOW) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2207 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2208 | #ifdef CONFIG_X86_64 |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 2209 | else |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2210 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 2211 | CPU_BASED_CR8_STORE_EXITING; |
| 2212 | #endif |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2213 | |
| 2214 | /* |
| 2215 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 2216 | * for I/O port accesses. |
| 2217 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2218 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
Sean Christopherson | de0286b | 2019-05-07 12:18:01 -0700 | [diff] [blame] | 2219 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 2220 | |
| 2221 | /* |
| 2222 | * This bit will be computed in nested_get_vmcs12_pages, because |
| 2223 | * we do not have access to L1's MSR bitmap yet. For now, keep |
| 2224 | * the same bit as before, hoping to avoid multiple VMWRITEs that |
| 2225 | * only set/clear this bit. |
| 2226 | */ |
| 2227 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 2228 | exec_control |= exec_controls_get(vmx) & CPU_BASED_USE_MSR_BITMAPS; |
| 2229 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2230 | exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2231 | |
| 2232 | /* |
| 2233 | * SECONDARY EXEC CONTROLS |
| 2234 | */ |
| 2235 | if (cpu_has_secondary_exec_ctrls()) { |
| 2236 | exec_control = vmx->secondary_exec_control; |
| 2237 | |
| 2238 | /* Take the following fields only from vmcs12 */ |
| 2239 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 2240 | SECONDARY_EXEC_ENABLE_INVPCID | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 2241 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2242 | SECONDARY_EXEC_XSAVES | |
Tao Xu | e69e72fa | 2019-07-16 14:55:49 +0800 | [diff] [blame] | 2243 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2244 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 2245 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2246 | SECONDARY_EXEC_ENABLE_VMFUNC | |
| 2247 | SECONDARY_EXEC_TSC_SCALING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2248 | if (nested_cpu_has(vmcs12, |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 2249 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 2250 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 2251 | |
| 2252 | /* PML is emulated and never enabled in hardware for L2. */ |
| 2253 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2254 | |
| 2255 | /* VMCS shadowing for L2 is emulated for now */ |
| 2256 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 2257 | |
Sean Christopherson | 469debd | 2019-05-07 12:18:02 -0700 | [diff] [blame] | 2258 | /* |
| 2259 | * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4() |
| 2260 | * will not have to rewrite the controls just for this bit. |
| 2261 | */ |
| 2262 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() && |
| 2263 | (vmcs12->guest_cr4 & X86_CR4_UMIP)) |
| 2264 | exec_control |= SECONDARY_EXEC_DESC; |
| 2265 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2266 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 2267 | vmcs_write16(GUEST_INTR_STATUS, |
| 2268 | vmcs12->guest_intr_status); |
| 2269 | |
Krish Sadhukhan | bddd82d | 2020-09-21 08:10:25 +0000 | [diff] [blame] | 2270 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 2271 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2272 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 2273 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 2274 | vmx_write_encls_bitmap(&vmx->vcpu, vmcs12); |
| 2275 | |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2276 | secondary_exec_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * ENTRY CONTROLS |
| 2281 | * |
| 2282 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
| 2283 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 2284 | * on the related bits (if supported by the CPU) in the hope that |
| 2285 | * we can avoid VMWrites during vmx_set_efer(). |
| 2286 | */ |
| 2287 | exec_control = (vmcs12->vm_entry_controls | vmx_vmentry_ctrl()) & |
| 2288 | ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER; |
| 2289 | if (cpu_has_load_ia32_efer()) { |
| 2290 | if (guest_efer & EFER_LMA) |
| 2291 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 2292 | if (guest_efer != host_efer) |
| 2293 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 2294 | } |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2295 | vm_entry_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2296 | |
| 2297 | /* |
| 2298 | * EXIT CONTROLS |
| 2299 | * |
| 2300 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 2301 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 2302 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
| 2303 | */ |
| 2304 | exec_control = vmx_vmexit_ctrl(); |
| 2305 | if (cpu_has_load_ia32_efer() && guest_efer != host_efer) |
| 2306 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
Sean Christopherson | 3af80fe | 2019-05-07 12:18:00 -0700 | [diff] [blame] | 2307 | vm_exit_controls_set(vmx, exec_control); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2308 | |
| 2309 | /* |
| 2310 | * Interrupt/Exception Fields |
| 2311 | */ |
| 2312 | if (vmx->nested.nested_run_pending) { |
| 2313 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2314 | vmcs12->vm_entry_intr_info_field); |
| 2315 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2316 | vmcs12->vm_entry_exception_error_code); |
| 2317 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2318 | vmcs12->vm_entry_instruction_len); |
| 2319 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 2320 | vmcs12->guest_interruptibility_info); |
| 2321 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 2322 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
| 2323 | } else { |
| 2324 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 2325 | } |
| 2326 | } |
| 2327 | |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2328 | static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2329 | { |
| 2330 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
| 2331 | |
| 2332 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2333 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 2334 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 2335 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 2336 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 2337 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 2338 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 2339 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 2340 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 2341 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 2342 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 2343 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 2344 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 2345 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 2346 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 2347 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 2348 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 2349 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 2350 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 2351 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 2352 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 2353 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2354 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 2355 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 2356 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 2357 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 2358 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 2359 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 2360 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 2361 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 2362 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 2363 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 2364 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 2365 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 2366 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 2367 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 2368 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 2369 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Sean Christopherson | fc387d8 | 2020-09-23 11:44:46 -0700 | [diff] [blame] | 2370 | |
| 2371 | vmx->segment_cache.bitmask = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 2375 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 2376 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 2377 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 2378 | vmcs12->guest_pending_dbg_exceptions); |
| 2379 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 2380 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 2381 | |
| 2382 | /* |
| 2383 | * L1 may access the L2's PDPTR, so save them to construct |
| 2384 | * vmcs12 |
| 2385 | */ |
| 2386 | if (enable_ept) { |
| 2387 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2388 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2389 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2390 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2391 | } |
Sean Christopherson | c27e5b0 | 2019-05-07 09:06:39 -0700 | [diff] [blame] | 2392 | |
| 2393 | if (kvm_mpx_supported() && vmx->nested.nested_run_pending && |
| 2394 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 2395 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | if (nested_cpu_has_xsaves(vmcs12)) |
| 2399 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 2400 | |
| 2401 | /* |
| 2402 | * Whether page-faults are trapped is determined by a combination of |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2403 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. If L0 |
| 2404 | * doesn't care about page faults then we should set all of these to |
| 2405 | * L1's desires. However, if L0 does care about (some) page faults, it |
| 2406 | * is not easy (if at all possible?) to merge L0 and L1's desires, we |
| 2407 | * simply ask to exit on each and every L2 page fault. This is done by |
| 2408 | * setting MASK=MATCH=0 and (see below) EB.PF=1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2409 | * Note that below we don't need special code to set EB.PF beyond the |
| 2410 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 2411 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 2412 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 2413 | */ |
Paolo Bonzini | a0c1343 | 2020-07-10 17:48:08 +0200 | [diff] [blame] | 2414 | if (vmx_need_pf_intercept(&vmx->vcpu)) { |
| 2415 | /* |
| 2416 | * TODO: if both L0 and L1 need the same MASK and MATCH, |
| 2417 | * go ahead and use it? |
| 2418 | */ |
| 2419 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 2420 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 2421 | } else { |
| 2422 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, vmcs12->page_fault_error_code_mask); |
| 2423 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, vmcs12->page_fault_error_code_match); |
| 2424 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2425 | |
| 2426 | if (cpu_has_vmx_apicv()) { |
| 2427 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 2428 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 2429 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 2430 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 2431 | } |
| 2432 | |
Aaron Lewis | 662f1d1 | 2019-11-07 21:14:39 -0800 | [diff] [blame] | 2433 | /* |
| 2434 | * Make sure the msr_autostore list is up to date before we set the |
| 2435 | * count in the vmcs02. |
| 2436 | */ |
| 2437 | prepare_vmx_msr_autostore_list(&vmx->vcpu, MSR_IA32_TSC); |
| 2438 | |
| 2439 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, vmx->msr_autostore.guest.nr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2440 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 2441 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 2442 | |
| 2443 | set_cr4_guest_host_mask(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 2448 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 2449 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 2450 | * guest in a way that will both be appropriate to L1's requests, and our |
| 2451 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 2452 | * function also has additional necessary side-effects, like setting various |
| 2453 | * vcpu->arch fields. |
| 2454 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 2455 | * is assigned to entry_failure_code on failure. |
| 2456 | */ |
| 2457 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 2458 | bool from_vmentry, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2459 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2460 | { |
| 2461 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2462 | bool load_guest_pdptrs_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2463 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2464 | if (vmx->nested.dirty_vmcs12 || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
Paolo Bonzini | b1346ab | 2019-06-06 17:24:00 +0200 | [diff] [blame] | 2465 | prepare_vmcs02_rare(vmx, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2466 | vmx->nested.dirty_vmcs12 = false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2467 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 2468 | load_guest_pdptrs_vmcs12 = !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) || |
| 2469 | !(vmx->nested.hv_evmcs->hv_clean_fields & |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2470 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | if (vmx->nested.nested_run_pending && |
| 2474 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 2475 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 2476 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 2477 | } else { |
| 2478 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 2479 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 2480 | } |
Sean Christopherson | 3b013a2 | 2019-05-07 09:06:28 -0700 | [diff] [blame] | 2481 | if (kvm_mpx_supported() && (!vmx->nested.nested_run_pending || |
| 2482 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))) |
| 2483 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2484 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 2485 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2486 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 2487 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 2488 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 2489 | */ |
Jason Baron | b6a7cc3 | 2021-01-14 22:27:54 -0500 | [diff] [blame] | 2490 | vmx_update_exception_bitmap(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2491 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 2492 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 2493 | |
| 2494 | if (vmx->nested.nested_run_pending && |
| 2495 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
| 2496 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 2497 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 2498 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2499 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 2500 | } |
| 2501 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 2502 | vcpu->arch.tsc_offset = kvm_calc_nested_tsc_offset( |
| 2503 | vcpu->arch.l1_tsc_offset, |
| 2504 | vmx_get_l2_tsc_offset(vcpu), |
| 2505 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2506 | |
| 2507 | vcpu->arch.tsc_scaling_ratio = kvm_calc_nested_tsc_multiplier( |
| 2508 | vcpu->arch.l1_tsc_scaling_ratio, |
| 2509 | vmx_get_l2_tsc_multiplier(vcpu)); |
| 2510 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2511 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2512 | if (kvm_has_tsc_control) |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 2513 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2514 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 2515 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, true); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2516 | |
| 2517 | if (nested_cpu_has_ept(vmcs12)) |
| 2518 | nested_ept_init_mmu_context(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2519 | |
| 2520 | /* |
| 2521 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 2522 | * bits which we consider mandatory enabled. |
| 2523 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 2524 | * the specifications by L1; It's not enough to take |
| 2525 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 2526 | * have more bits than L1 expected. |
| 2527 | */ |
| 2528 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 2529 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2530 | |
| 2531 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 2532 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 2533 | |
| 2534 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
| 2535 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 2536 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 2537 | |
| 2538 | /* |
| 2539 | * Guest state is invalid and unrestricted guest is disabled, |
| 2540 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 2541 | * Fail the VMEntry. |
| 2542 | */ |
Sean Christopherson | 2ba4493 | 2020-09-23 11:44:48 -0700 | [diff] [blame] | 2543 | if (CC(!vmx_guest_state_valid(vcpu))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2544 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2545 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | /* Shadow page tables on either EPT or shadow page tables. */ |
| 2549 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 2550 | from_vmentry, entry_failure_code)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2551 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2552 | |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2553 | /* |
| 2554 | * Immediately write vmcs02.GUEST_CR3. It will be propagated to vmcs12 |
| 2555 | * on nested VM-Exit, which can occur without actually running L2 and |
Paolo Bonzini | 727a7e2 | 2020-03-05 03:52:50 -0500 | [diff] [blame] | 2556 | * thus without hitting vmx_load_mmu_pgd(), e.g. if L1 is entering L2 with |
Sean Christopherson | 04f11ef | 2019-09-27 14:45:16 -0700 | [diff] [blame] | 2557 | * vmcs12.GUEST_ACTIVITYSTATE=HLT, in which case KVM will intercept the |
| 2558 | * transition to HLT instead of running L2. |
| 2559 | */ |
| 2560 | if (enable_ept) |
| 2561 | vmcs_writel(GUEST_CR3, vmcs12->guest_cr3); |
| 2562 | |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 2563 | /* Late preparation of GUEST_PDPTRs now that EFER and CRs are set. */ |
| 2564 | if (load_guest_pdptrs_vmcs12 && nested_cpu_has_ept(vmcs12) && |
| 2565 | is_pae_paging(vcpu)) { |
| 2566 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 2567 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 2568 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 2569 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 2570 | } |
| 2571 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2572 | if (!enable_ept) |
| 2573 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 2574 | |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2575 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 2576 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 2577 | vmcs12->guest_ia32_perf_global_ctrl))) |
Oliver Upton | 71f7347 | 2019-11-13 16:17:19 -0800 | [diff] [blame] | 2578 | return -EINVAL; |
| 2579 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 2580 | kvm_rsp_write(vcpu, vmcs12->guest_rsp); |
| 2581 | kvm_rip_write(vcpu, vmcs12->guest_rip); |
Vitaly Kuznetsov | dc31338 | 2021-05-26 15:20:24 +0200 | [diff] [blame] | 2582 | |
| 2583 | /* |
| 2584 | * It was observed that genuine Hyper-V running in L1 doesn't reset |
| 2585 | * 'hv_clean_fields' by itself, it only sets the corresponding dirty |
| 2586 | * bits when it changes a field in eVMCS. Mark all fields as clean |
| 2587 | * here. |
| 2588 | */ |
| 2589 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
| 2590 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 2591 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 2592 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2593 | return 0; |
| 2594 | } |
| 2595 | |
| 2596 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 2597 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2598 | if (CC(!nested_cpu_has_nmi_exiting(vmcs12) && |
| 2599 | nested_cpu_has_virtual_nmis(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2600 | return -EINVAL; |
| 2601 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2602 | if (CC(!nested_cpu_has_virtual_nmis(vmcs12) && |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 2603 | nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2604 | return -EINVAL; |
| 2605 | |
| 2606 | return 0; |
| 2607 | } |
| 2608 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2609 | static bool nested_vmx_check_eptp(struct kvm_vcpu *vcpu, u64 new_eptp) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2610 | { |
| 2611 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2612 | |
| 2613 | /* Check for memory type validity */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2614 | switch (new_eptp & VMX_EPTP_MT_MASK) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2615 | case VMX_EPTP_MT_UC: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2616 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2617 | return false; |
| 2618 | break; |
| 2619 | case VMX_EPTP_MT_WB: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2620 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2621 | return false; |
| 2622 | break; |
| 2623 | default: |
| 2624 | return false; |
| 2625 | } |
| 2626 | |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2627 | /* Page-walk levels validity. */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2628 | switch (new_eptp & VMX_EPTP_PWL_MASK) { |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2629 | case VMX_EPTP_PWL_5: |
| 2630 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_5_BIT))) |
| 2631 | return false; |
| 2632 | break; |
| 2633 | case VMX_EPTP_PWL_4: |
| 2634 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_PAGE_WALK_4_BIT))) |
| 2635 | return false; |
| 2636 | break; |
| 2637 | default: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2638 | return false; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 2639 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2640 | |
| 2641 | /* Reserved bits should not be set */ |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2642 | if (CC(kvm_vcpu_is_illegal_gpa(vcpu, new_eptp) || ((new_eptp >> 7) & 0x1f))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2643 | return false; |
| 2644 | |
| 2645 | /* AD, if set, should be supported */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2646 | if (new_eptp & VMX_EPTP_AD_ENABLE_BIT) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2647 | if (CC(!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2648 | return false; |
| 2649 | } |
| 2650 | |
| 2651 | return true; |
| 2652 | } |
| 2653 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2654 | /* |
| 2655 | * Checks related to VM-Execution Control Fields |
| 2656 | */ |
| 2657 | static int nested_check_vm_execution_controls(struct kvm_vcpu *vcpu, |
| 2658 | struct vmcs12 *vmcs12) |
| 2659 | { |
| 2660 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2661 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2662 | if (CC(!vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 2663 | vmx->nested.msrs.pinbased_ctls_low, |
| 2664 | vmx->nested.msrs.pinbased_ctls_high)) || |
| 2665 | CC(!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 2666 | vmx->nested.msrs.procbased_ctls_low, |
| 2667 | vmx->nested.msrs.procbased_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2668 | return -EINVAL; |
| 2669 | |
| 2670 | if (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2671 | CC(!vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 2672 | vmx->nested.msrs.secondary_ctls_low, |
| 2673 | vmx->nested.msrs.secondary_ctls_high))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2674 | return -EINVAL; |
| 2675 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2676 | 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] | 2677 | nested_vmx_check_io_bitmap_controls(vcpu, vmcs12) || |
| 2678 | nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12) || |
| 2679 | nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12) || |
| 2680 | nested_vmx_check_apic_access_controls(vcpu, vmcs12) || |
| 2681 | nested_vmx_check_apicv_controls(vcpu, vmcs12) || |
| 2682 | nested_vmx_check_nmi_controls(vmcs12) || |
| 2683 | nested_vmx_check_pml_controls(vcpu, vmcs12) || |
| 2684 | nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12) || |
| 2685 | nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12) || |
| 2686 | nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12) || |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2687 | CC(nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2688 | return -EINVAL; |
| 2689 | |
Sean Christopherson | bc44121 | 2019-02-12 16:42:23 -0800 | [diff] [blame] | 2690 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 2691 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 2692 | return -EINVAL; |
| 2693 | |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2694 | if (nested_cpu_has_ept(vmcs12) && |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 2695 | CC(!nested_vmx_check_eptp(vcpu, vmcs12->ept_pointer))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2696 | return -EINVAL; |
| 2697 | |
| 2698 | if (nested_cpu_has_vmfunc(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2699 | if (CC(vmcs12->vm_function_control & |
| 2700 | ~vmx->nested.msrs.vmfunc_controls)) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2701 | return -EINVAL; |
| 2702 | |
| 2703 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2704 | if (CC(!nested_cpu_has_ept(vmcs12)) || |
| 2705 | CC(!page_address_valid(vcpu, vmcs12->eptp_list_address))) |
Krish Sadhukhan | 461b4ba | 2018-12-12 13:30:07 -0500 | [diff] [blame] | 2706 | return -EINVAL; |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2713 | /* |
| 2714 | * Checks related to VM-Exit Control Fields |
| 2715 | */ |
| 2716 | static int nested_check_vm_exit_controls(struct kvm_vcpu *vcpu, |
| 2717 | struct vmcs12 *vmcs12) |
| 2718 | { |
| 2719 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2720 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2721 | if (CC(!vmx_control_verify(vmcs12->vm_exit_controls, |
| 2722 | vmx->nested.msrs.exit_ctls_low, |
| 2723 | vmx->nested.msrs.exit_ctls_high)) || |
| 2724 | CC(nested_vmx_check_exit_msr_switch_controls(vcpu, vmcs12))) |
Krish Sadhukhan | 61446ba | 2018-12-12 13:30:09 -0500 | [diff] [blame] | 2725 | return -EINVAL; |
| 2726 | |
| 2727 | return 0; |
| 2728 | } |
| 2729 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2730 | /* |
| 2731 | * Checks related to VM-Entry Control Fields |
| 2732 | */ |
| 2733 | static int nested_check_vm_entry_controls(struct kvm_vcpu *vcpu, |
| 2734 | struct vmcs12 *vmcs12) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2735 | { |
| 2736 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2737 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2738 | if (CC(!vmx_control_verify(vmcs12->vm_entry_controls, |
| 2739 | vmx->nested.msrs.entry_ctls_low, |
| 2740 | vmx->nested.msrs.entry_ctls_high))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2741 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2742 | |
| 2743 | /* |
| 2744 | * From the Intel SDM, volume 3: |
| 2745 | * Fields relevant to VM-entry event injection must be set properly. |
| 2746 | * These fields are the VM-entry interruption-information field, the |
| 2747 | * VM-entry exception error code, and the VM-entry instruction length. |
| 2748 | */ |
| 2749 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 2750 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 2751 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 2752 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 2753 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 2754 | bool should_have_error_code; |
| 2755 | bool urg = nested_cpu_has2(vmcs12, |
| 2756 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 2757 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 2758 | |
| 2759 | /* VM-entry interruption-info field: interruption type */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2760 | if (CC(intr_type == INTR_TYPE_RESERVED) || |
| 2761 | CC(intr_type == INTR_TYPE_OTHER_EVENT && |
| 2762 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2763 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2764 | |
| 2765 | /* VM-entry interruption-info field: vector */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2766 | if (CC(intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 2767 | CC(intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 2768 | CC(intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2769 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2770 | |
| 2771 | /* VM-entry interruption-info field: deliver error code */ |
| 2772 | should_have_error_code = |
| 2773 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 2774 | x86_exception_has_error_code(vector); |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2775 | if (CC(has_error_code != should_have_error_code)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2776 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2777 | |
| 2778 | /* VM-entry exception error code */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2779 | if (CC(has_error_code && |
Sean Christopherson | 567926c | 2019-10-01 09:21:23 -0700 | [diff] [blame] | 2780 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 16))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2781 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2782 | |
| 2783 | /* VM-entry interruption-info field: reserved bits */ |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2784 | if (CC(intr_info & INTR_INFO_RESVD_BITS_MASK)) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2785 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2786 | |
| 2787 | /* VM-entry instruction length */ |
| 2788 | switch (intr_type) { |
| 2789 | case INTR_TYPE_SOFT_EXCEPTION: |
| 2790 | case INTR_TYPE_SOFT_INTR: |
| 2791 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2792 | if (CC(vmcs12->vm_entry_instruction_len > 15) || |
| 2793 | CC(vmcs12->vm_entry_instruction_len == 0 && |
| 2794 | CC(!nested_cpu_has_zero_length_injection(vcpu)))) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2795 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2799 | if (nested_vmx_check_entry_msr_switch_controls(vcpu, vmcs12)) |
| 2800 | return -EINVAL; |
| 2801 | |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2805 | static int nested_vmx_check_controls(struct kvm_vcpu *vcpu, |
| 2806 | struct vmcs12 *vmcs12) |
| 2807 | { |
| 2808 | if (nested_check_vm_execution_controls(vcpu, vmcs12) || |
| 2809 | nested_check_vm_exit_controls(vcpu, vmcs12) || |
| 2810 | nested_check_vm_entry_controls(vcpu, vmcs12)) |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2811 | return -EINVAL; |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2812 | |
Vitaly Kuznetsov | a835023 | 2020-02-05 13:30:34 +0100 | [diff] [blame] | 2813 | if (to_vmx(vcpu)->nested.enlightened_vmcs_enabled) |
| 2814 | return nested_evmcs_check_controls(vmcs12); |
| 2815 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
| 2818 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 2819 | static int nested_vmx_check_host_state(struct kvm_vcpu *vcpu, |
| 2820 | struct vmcs12 *vmcs12) |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2821 | { |
| 2822 | bool ia32e; |
| 2823 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2824 | if (CC(!nested_host_cr0_valid(vcpu, vmcs12->host_cr0)) || |
| 2825 | CC(!nested_host_cr4_valid(vcpu, vmcs12->host_cr4)) || |
Sean Christopherson | 636e8b7 | 2021-02-03 16:01:10 -0800 | [diff] [blame] | 2826 | CC(kvm_vcpu_is_illegal_gpa(vcpu, vmcs12->host_cr3))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2827 | return -EINVAL; |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2828 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2829 | if (CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_esp, vcpu)) || |
| 2830 | CC(is_noncanonical_address(vmcs12->host_ia32_sysenter_eip, vcpu))) |
Krish Sadhukhan | 711eff3 | 2019-02-07 14:05:30 -0500 | [diff] [blame] | 2831 | return -EINVAL; |
| 2832 | |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2833 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2834 | CC(!kvm_pat_valid(vmcs12->host_ia32_pat))) |
Krish Sadhukhan | f6b0db1f | 2019-04-08 17:35:11 -0400 | [diff] [blame] | 2835 | return -EINVAL; |
| 2836 | |
Oliver Upton | c547cb6 | 2019-11-13 16:17:17 -0800 | [diff] [blame] | 2837 | if ((vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2838 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2839 | vmcs12->host_ia32_perf_global_ctrl))) |
| 2840 | return -EINVAL; |
| 2841 | |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2842 | #ifdef CONFIG_X86_64 |
| 2843 | ia32e = !!(vcpu->arch.efer & EFER_LMA); |
| 2844 | #else |
| 2845 | ia32e = false; |
| 2846 | #endif |
| 2847 | |
| 2848 | if (ia32e) { |
| 2849 | if (CC(!(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)) || |
| 2850 | CC(!(vmcs12->host_cr4 & X86_CR4_PAE))) |
| 2851 | return -EINVAL; |
| 2852 | } else { |
| 2853 | if (CC(vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) || |
| 2854 | CC(vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) || |
| 2855 | CC(vmcs12->host_cr4 & X86_CR4_PCIDE) || |
| 2856 | CC((vmcs12->host_rip) >> 32)) |
| 2857 | return -EINVAL; |
| 2858 | } |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2859 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2860 | if (CC(vmcs12->host_cs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2861 | CC(vmcs12->host_ss_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2862 | CC(vmcs12->host_ds_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2863 | CC(vmcs12->host_es_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2864 | CC(vmcs12->host_fs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2865 | CC(vmcs12->host_gs_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2866 | CC(vmcs12->host_tr_selector & (SEGMENT_RPL_MASK | SEGMENT_TI_MASK)) || |
| 2867 | CC(vmcs12->host_cs_selector == 0) || |
| 2868 | CC(vmcs12->host_tr_selector == 0) || |
| 2869 | CC(vmcs12->host_ss_selector == 0 && !ia32e)) |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2870 | return -EINVAL; |
| 2871 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2872 | if (CC(is_noncanonical_address(vmcs12->host_fs_base, vcpu)) || |
| 2873 | CC(is_noncanonical_address(vmcs12->host_gs_base, vcpu)) || |
| 2874 | CC(is_noncanonical_address(vmcs12->host_gdtr_base, vcpu)) || |
| 2875 | CC(is_noncanonical_address(vmcs12->host_idtr_base, vcpu)) || |
Paolo Bonzini | fd3edd4 | 2019-09-25 18:33:53 +0200 | [diff] [blame] | 2876 | CC(is_noncanonical_address(vmcs12->host_tr_base, vcpu)) || |
| 2877 | CC(is_noncanonical_address(vmcs12->host_rip, vcpu))) |
Krish Sadhukhan | 5845038 | 2019-08-09 12:26:19 -0700 | [diff] [blame] | 2878 | return -EINVAL; |
Krish Sadhukhan | 1ef23e1 | 2019-07-03 19:54:35 -0400 | [diff] [blame] | 2879 | |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2880 | /* |
| 2881 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 2882 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 2883 | * the values of the LMA and LME bits in the field must each be that of |
| 2884 | * the host address-space size VM-exit control. |
| 2885 | */ |
| 2886 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2887 | if (CC(!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer)) || |
| 2888 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA)) || |
| 2889 | CC(ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))) |
Krish Sadhukhan | 254b2f3 | 2018-12-12 13:30:11 -0500 | [diff] [blame] | 2890 | return -EINVAL; |
Krish Sadhukhan | 5fbf963 | 2018-12-12 13:30:10 -0500 | [diff] [blame] | 2891 | } |
| 2892 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2893 | return 0; |
| 2894 | } |
| 2895 | |
| 2896 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 2897 | struct vmcs12 *vmcs12) |
| 2898 | { |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2899 | int r = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2900 | struct vmcs12 *shadow; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2901 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2902 | |
| 2903 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 2904 | return 0; |
| 2905 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2906 | if (CC(!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2907 | return -EINVAL; |
| 2908 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2909 | 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] | 2910 | return -EINVAL; |
| 2911 | |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2912 | shadow = map.hva; |
| 2913 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2914 | if (CC(shadow->hdr.revision_id != VMCS12_REVISION) || |
| 2915 | CC(shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2916 | r = -EINVAL; |
KarimAllah Ahmed | 8892530 | 2019-01-31 21:24:41 +0100 | [diff] [blame] | 2917 | |
| 2918 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2919 | return r; |
| 2920 | } |
| 2921 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2922 | /* |
| 2923 | * Checks related to Guest Non-register State |
| 2924 | */ |
| 2925 | static int nested_check_guest_non_reg_state(struct vmcs12 *vmcs12) |
| 2926 | { |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2927 | if (CC(vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 2928 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT && |
| 2929 | vmcs12->guest_activity_state != GUEST_ACTIVITY_WAIT_SIPI)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2930 | return -EINVAL; |
| 2931 | |
| 2932 | return 0; |
| 2933 | } |
| 2934 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 2935 | static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu, |
| 2936 | struct vmcs12 *vmcs12, |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2937 | enum vm_entry_failure_code *entry_failure_code) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2938 | { |
| 2939 | bool ia32e; |
| 2940 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2941 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2942 | |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2943 | if (CC(!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0)) || |
| 2944 | CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2945 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2946 | |
Krish Sadhukhan | b91991b | 2020-01-15 19:54:32 -0500 | [diff] [blame] | 2947 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) && |
| 2948 | CC(!kvm_dr7_valid(vmcs12->guest_dr7))) |
| 2949 | return -EINVAL; |
| 2950 | |
Krish Sadhukhan | de2bc2b | 2019-04-08 17:35:12 -0400 | [diff] [blame] | 2951 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2952 | CC(!kvm_pat_valid(vmcs12->guest_ia32_pat))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2953 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2954 | |
| 2955 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 2956 | *entry_failure_code = ENTRY_FAIL_VMCS_LINK_PTR; |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2957 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2958 | } |
| 2959 | |
Oliver Upton | bfc6ad6 | 2019-11-13 16:17:16 -0800 | [diff] [blame] | 2960 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) && |
| 2961 | CC(!kvm_valid_perf_global_ctrl(vcpu_to_pmu(vcpu), |
| 2962 | vmcs12->guest_ia32_perf_global_ctrl))) |
| 2963 | return -EINVAL; |
| 2964 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2965 | /* |
| 2966 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 2967 | * are performed on the field for the IA32_EFER MSR: |
| 2968 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 2969 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 2970 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 2971 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 2972 | * CR0.PG) is 1. |
| 2973 | */ |
| 2974 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 2975 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 2976 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2977 | if (CC(!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer)) || |
| 2978 | CC(ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA)) || |
| 2979 | CC(((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 2980 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2981 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
Sean Christopherson | 5497b95 | 2019-07-11 08:58:29 -0700 | [diff] [blame] | 2985 | (CC(is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu)) || |
| 2986 | CC((vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2987 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2988 | |
Sean Christopherson | 9c3e922 | 2019-04-11 12:18:05 -0700 | [diff] [blame] | 2989 | if (nested_check_guest_non_reg_state(vmcs12)) |
Sean Christopherson | c80add0 | 2019-04-11 12:18:09 -0700 | [diff] [blame] | 2990 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2991 | |
| 2992 | return 0; |
| 2993 | } |
| 2994 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 2995 | static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 2996 | { |
| 2997 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2998 | unsigned long cr3, cr4; |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 2999 | bool vm_fail; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3000 | |
| 3001 | if (!nested_early_check) |
| 3002 | return 0; |
| 3003 | |
| 3004 | if (vmx->msr_autoload.host.nr) |
| 3005 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 3006 | if (vmx->msr_autoload.guest.nr) |
| 3007 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 3008 | |
| 3009 | preempt_disable(); |
| 3010 | |
| 3011 | vmx_prepare_switch_to_guest(vcpu); |
| 3012 | |
| 3013 | /* |
| 3014 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 3015 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 3016 | * be written (by prepare_vmcs02()) before the "real" VMEnter, i.e. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3017 | * there is no need to preserve other bits or save/restore the field. |
| 3018 | */ |
| 3019 | vmcs_writel(GUEST_RFLAGS, 0); |
| 3020 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3021 | cr3 = __get_current_cr3_fast(); |
| 3022 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 3023 | vmcs_writel(HOST_CR3, cr3); |
| 3024 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 3025 | } |
| 3026 | |
| 3027 | cr4 = cr4_read_shadow(); |
| 3028 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 3029 | vmcs_writel(HOST_CR4, cr4); |
| 3030 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 3031 | } |
| 3032 | |
Uros Bizjak | 150f17b | 2020-12-30 16:26:57 -0800 | [diff] [blame] | 3033 | vm_fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs, |
| 3034 | vmx->loaded_vmcs->launched); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3035 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3036 | if (vmx->msr_autoload.host.nr) |
| 3037 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 3038 | if (vmx->msr_autoload.guest.nr) |
| 3039 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 3040 | |
Sean Christopherson | f1727b4 | 2019-01-25 07:40:58 -0800 | [diff] [blame] | 3041 | if (vm_fail) { |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3042 | u32 error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 3043 | |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3044 | preempt_enable(); |
Sean Christopherson | 380e005 | 2019-07-11 08:58:30 -0700 | [diff] [blame] | 3045 | |
| 3046 | trace_kvm_nested_vmenter_failed( |
| 3047 | "early hardware check VM-instruction error: ", error); |
| 3048 | WARN_ON_ONCE(error != VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3049 | return 1; |
| 3050 | } |
| 3051 | |
| 3052 | /* |
| 3053 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 3054 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3055 | if (hw_breakpoint_active()) |
| 3056 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
Peter Zijlstra | 84b6a34 | 2020-05-29 23:27:36 +0200 | [diff] [blame] | 3057 | local_irq_enable(); |
Wanpeng Li | 541e886 | 2019-05-17 16:49:50 +0800 | [diff] [blame] | 3058 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3059 | |
| 3060 | /* |
| 3061 | * A non-failing VMEntry means we somehow entered guest mode with |
| 3062 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 3063 | * is no telling what memory has been modified or what state has |
| 3064 | * been exposed to unknown code. Hitting this all but guarantees |
| 3065 | * a (very critical) hardware issue. |
| 3066 | */ |
| 3067 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 3068 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 3069 | |
| 3070 | return 0; |
| 3071 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3072 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3073 | static bool nested_get_evmcs_page(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3074 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3075 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3076 | |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3077 | /* |
| 3078 | * hv_evmcs may end up being not mapped after migration (when |
| 3079 | * L2 was running), map it here to make sure vmcs12 changes are |
| 3080 | * properly reflected. |
| 3081 | */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3082 | if (vmx->nested.enlightened_vmcs_enabled && |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 3083 | vmx->nested.hv_evmcs_vmptr == EVMPTR_MAP_PENDING) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3084 | enum nested_evmptrld_status evmptrld_status = |
| 3085 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 3086 | |
| 3087 | if (evmptrld_status == EVMPTRLD_VMFAIL || |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3088 | evmptrld_status == EVMPTRLD_ERROR) |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3089 | return false; |
Vitaly Kuznetsov | 8629b62 | 2021-05-26 15:20:25 +0200 | [diff] [blame] | 3090 | |
| 3091 | /* |
| 3092 | * Post migration VMCS12 always provides the most actual |
| 3093 | * information, copy it to eVMCS upon entry. |
| 3094 | */ |
| 3095 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3096 | } |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 3097 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3098 | return true; |
| 3099 | } |
| 3100 | |
| 3101 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
| 3102 | { |
| 3103 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3104 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3105 | struct kvm_host_map *map; |
| 3106 | struct page *page; |
| 3107 | u64 hpa; |
| 3108 | |
Maxim Levitsky | 158a48e | 2021-06-07 12:02:03 +0300 | [diff] [blame] | 3109 | if (!vcpu->arch.pdptrs_from_userspace && |
| 3110 | !nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3111 | /* |
| 3112 | * Reload the guest's PDPTRs since after a migration |
| 3113 | * the guest CR3 might be restored prior to setting the nested |
| 3114 | * state which can lead to a load of wrong PDPTRs. |
| 3115 | */ |
| 3116 | if (CC(!load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) |
| 3117 | return false; |
| 3118 | } |
| 3119 | |
| 3120 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3121 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3122 | /* |
| 3123 | * Translate L1 physical address to host physical |
| 3124 | * address for vmcs02. Keep the page pinned, so this |
| 3125 | * physical address remains valid. We keep a reference |
| 3126 | * to it so we can release it later. |
| 3127 | */ |
| 3128 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 3129 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3130 | vmx->nested.apic_access_page = NULL; |
| 3131 | } |
| 3132 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3133 | if (!is_error_page(page)) { |
| 3134 | vmx->nested.apic_access_page = page; |
| 3135 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 3136 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 3137 | } else { |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3138 | pr_debug_ratelimited("%s: no backing 'struct page' for APIC-access address in vmcs12\n", |
| 3139 | __func__); |
| 3140 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3141 | vcpu->run->internal.suberror = |
| 3142 | KVM_INTERNAL_ERROR_EMULATION; |
| 3143 | vcpu->run->internal.ndata = 0; |
| 3144 | return false; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | |
| 3148 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3149 | map = &vmx->nested.virtual_apic_map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3150 | |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3151 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->virtual_apic_page_addr), map)) { |
| 3152 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, pfn_to_hpa(map->pfn)); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3153 | } else if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING) && |
| 3154 | nested_cpu_has(vmcs12, CPU_BASED_CR8_STORE_EXITING) && |
| 3155 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 3156 | /* |
| 3157 | * The processor will never use the TPR shadow, simply |
| 3158 | * clear the bit from the execution control. Such a |
| 3159 | * configuration is useless, but it happens in tests. |
| 3160 | * For any other configuration, failing the vm entry is |
| 3161 | * _not_ what the processor does but it's basically the |
| 3162 | * only possibility we have. |
| 3163 | */ |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3164 | exec_controls_clearbit(vmx, CPU_BASED_TPR_SHADOW); |
Paolo Bonzini | 6909081 | 2019-04-15 15:16:17 +0200 | [diff] [blame] | 3165 | } else { |
Sean Christopherson | ca2f546 | 2019-05-07 09:06:33 -0700 | [diff] [blame] | 3166 | /* |
| 3167 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR to |
| 3168 | * force VM-Entry to fail. |
| 3169 | */ |
| 3170 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | if (nested_cpu_has_posted_intr(vmcs12)) { |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 3175 | map = &vmx->nested.pi_desc_map; |
| 3176 | |
| 3177 | if (!kvm_vcpu_map(vcpu, gpa_to_gfn(vmcs12->posted_intr_desc_addr), map)) { |
| 3178 | vmx->nested.pi_desc = |
| 3179 | (struct pi_desc *)(((void *)map->hva) + |
| 3180 | offset_in_page(vmcs12->posted_intr_desc_addr)); |
| 3181 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 3182 | pfn_to_hpa(map->pfn) + offset_in_page(vmcs12->posted_intr_desc_addr)); |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3183 | } else { |
| 3184 | /* |
| 3185 | * Defer the KVM_INTERNAL_EXIT until KVM tries to |
| 3186 | * access the contents of the VMCS12 posted interrupt |
| 3187 | * descriptor. (Note that KVM may do this when it |
| 3188 | * should not, per the architectural specification.) |
| 3189 | */ |
| 3190 | vmx->nested.pi_desc = NULL; |
| 3191 | pin_controls_clearbit(vmx, PIN_BASED_POSTED_INTR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3192 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3193 | } |
| 3194 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3195 | exec_controls_setbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3196 | else |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3197 | exec_controls_clearbit(vmx, CPU_BASED_USE_MSR_BITMAPS); |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3198 | |
| 3199 | return true; |
| 3200 | } |
| 3201 | |
| 3202 | static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu) |
| 3203 | { |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3204 | if (!nested_get_evmcs_page(vcpu)) { |
| 3205 | pr_debug_ratelimited("%s: enlightened vmptrld failed\n", |
| 3206 | __func__); |
| 3207 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3208 | vcpu->run->internal.suberror = |
| 3209 | KVM_INTERNAL_ERROR_EMULATION; |
| 3210 | vcpu->run->internal.ndata = 0; |
| 3211 | |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3212 | return false; |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 3213 | } |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 3214 | |
| 3215 | if (is_guest_mode(vcpu) && !nested_get_vmcs12_pages(vcpu)) |
| 3216 | return false; |
| 3217 | |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3218 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3219 | } |
| 3220 | |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 3221 | static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 3222 | { |
| 3223 | struct vmcs12 *vmcs12; |
| 3224 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3225 | gpa_t dst; |
| 3226 | |
| 3227 | if (WARN_ON_ONCE(!is_guest_mode(vcpu))) |
| 3228 | return 0; |
| 3229 | |
| 3230 | if (WARN_ON_ONCE(vmx->nested.pml_full)) |
| 3231 | return 1; |
| 3232 | |
| 3233 | /* |
| 3234 | * Check if PML is enabled for the nested guest. Whether eptp bit 6 is |
| 3235 | * set is already checked as part of A/D emulation. |
| 3236 | */ |
| 3237 | vmcs12 = get_vmcs12(vcpu); |
| 3238 | if (!nested_cpu_has_pml(vmcs12)) |
| 3239 | return 0; |
| 3240 | |
| 3241 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
| 3242 | vmx->nested.pml_full = true; |
| 3243 | return 1; |
| 3244 | } |
| 3245 | |
| 3246 | gpa &= ~0xFFFull; |
| 3247 | dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index; |
| 3248 | |
| 3249 | if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa, |
| 3250 | offset_in_page(dst), sizeof(gpa))) |
| 3251 | return 0; |
| 3252 | |
| 3253 | vmcs12->guest_pml_index--; |
| 3254 | |
| 3255 | return 0; |
| 3256 | } |
| 3257 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3258 | /* |
| 3259 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 3260 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 3261 | * slightly different). It also specifies what exception to inject otherwise. |
| 3262 | * Note that many of these exceptions have priority over VM exits, so they |
| 3263 | * don't have to be checked again here. |
| 3264 | */ |
| 3265 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 3266 | { |
| 3267 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 3268 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | if (vmx_get_cpl(vcpu)) { |
| 3273 | kvm_inject_gp(vcpu, 0); |
| 3274 | return 0; |
| 3275 | } |
| 3276 | |
| 3277 | return 1; |
| 3278 | } |
| 3279 | |
| 3280 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 3281 | { |
| 3282 | u8 rvi = vmx_get_rvi(); |
| 3283 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 3284 | |
| 3285 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3286 | } |
| 3287 | |
| 3288 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 3289 | struct vmcs12 *vmcs12); |
| 3290 | |
| 3291 | /* |
| 3292 | * If from_vmentry is false, this is being called from state restore (either RSM |
| 3293 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3294 | * |
| 3295 | * Returns: |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 3296 | * NVMX_VMENTRY_SUCCESS: Entered VMX non-root mode |
| 3297 | * NVMX_VMENTRY_VMFAIL: Consistency check VMFail |
| 3298 | * NVMX_VMENTRY_VMEXIT: Consistency check VMExit |
| 3299 | * NVMX_VMENTRY_KVM_INTERNAL_ERROR: KVM internal error |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3300 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3301 | enum nvmx_vmentry_status nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 3302 | bool from_vmentry) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3303 | { |
| 3304 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3305 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3306 | enum vm_entry_failure_code entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3307 | bool evaluate_pending_interrupts; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3308 | union vmx_exit_reason exit_reason = { |
| 3309 | .basic = EXIT_REASON_INVALID_STATE, |
| 3310 | .failed_vmentry = 1, |
| 3311 | }; |
| 3312 | u32 failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3313 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 3314 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 3315 | kvm_vcpu_flush_tlb_current(vcpu); |
| 3316 | |
Sean Christopherson | 2183f56 | 2019-05-07 12:17:56 -0700 | [diff] [blame] | 3317 | evaluate_pending_interrupts = exec_controls_get(vmx) & |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 3318 | (CPU_BASED_INTR_WINDOW_EXITING | CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3319 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 3320 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
| 3321 | |
| 3322 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 3323 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 3324 | if (kvm_mpx_supported() && |
| 3325 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 3326 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 3327 | |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 3328 | /* |
| 3329 | * Overwrite vmcs01.GUEST_CR3 with L1's CR3 if EPT is disabled *and* |
| 3330 | * nested early checks are disabled. In the event of a "late" VM-Fail, |
| 3331 | * i.e. a VM-Fail detected by hardware but not KVM, KVM must unwind its |
| 3332 | * software model to the pre-VMEntry host state. When EPT is disabled, |
| 3333 | * GUEST_CR3 holds KVM's shadow CR3, not L1's "real" CR3, which causes |
| 3334 | * nested_vmx_restore_host_state() to corrupt vcpu->arch.cr3. Stuffing |
| 3335 | * vmcs01.GUEST_CR3 results in the unwind naturally setting arch.cr3 to |
| 3336 | * the correct value. Smashing vmcs01.GUEST_CR3 is safe because nested |
| 3337 | * VM-Exits, and the unwind, reset KVM's MMU, i.e. vmcs01.GUEST_CR3 is |
| 3338 | * guaranteed to be overwritten with a shadow CR3 prior to re-entering |
| 3339 | * L1. Don't stuff vmcs01.GUEST_CR3 when using nested early checks as |
| 3340 | * KVM modifies vcpu->arch.cr3 if and only if the early hardware checks |
| 3341 | * pass, and early VM-Fails do not reset KVM's MMU, i.e. the VM-Fail |
| 3342 | * path would need to manually save/restore vmcs01.GUEST_CR3. |
| 3343 | */ |
| 3344 | if (!enable_ept && !nested_early_check) |
| 3345 | vmcs_writel(GUEST_CR3, vcpu->arch.cr3); |
| 3346 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3347 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
| 3348 | |
| 3349 | prepare_vmcs02_early(vmx, vmcs12); |
| 3350 | |
| 3351 | if (from_vmentry) { |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3352 | if (unlikely(!nested_get_vmcs12_pages(vcpu))) { |
| 3353 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3354 | return NVMX_VMENTRY_KVM_INTERNAL_ERROR; |
Sean Christopherson | b89d5ad | 2020-09-23 11:44:47 -0700 | [diff] [blame] | 3355 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3356 | |
| 3357 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 3358 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3359 | return NVMX_VMENTRY_VMFAIL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3360 | } |
| 3361 | |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3362 | if (nested_vmx_check_guest_state(vcpu, vmcs12, |
| 3363 | &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3364 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3365 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3366 | goto vmentry_fail_vmexit; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3367 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | enter_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3371 | |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 3372 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &entry_failure_code)) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3373 | exit_reason.basic = EXIT_REASON_INVALID_STATE; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3374 | vmcs12->exit_qualification = entry_failure_code; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3375 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3376 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3377 | |
| 3378 | if (from_vmentry) { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3379 | failed_index = nested_vmx_load_msr(vcpu, |
| 3380 | vmcs12->vm_entry_msr_load_addr, |
| 3381 | vmcs12->vm_entry_msr_load_count); |
| 3382 | if (failed_index) { |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3383 | exit_reason.basic = EXIT_REASON_MSR_LOAD_FAIL; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3384 | vmcs12->exit_qualification = failed_index; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3385 | goto vmentry_fail_vmexit_guest_mode; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 3386 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3387 | } else { |
| 3388 | /* |
| 3389 | * The MMU is not initialized to point at the right entities yet and |
| 3390 | * "get pages" would need to read data from the guest (i.e. we will |
| 3391 | * need to perform gpa to hpa translation). Request a call |
| 3392 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 3393 | * have already been set at vmentry time and should not be reset. |
| 3394 | */ |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 3395 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | /* |
| 3399 | * If L1 had a pending IRQ/NMI until it executed |
| 3400 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 3401 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 3402 | * evaluate if this pending event should cause an exit from L2 |
| 3403 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 3404 | * intercept EXTERNAL_INTERRUPT). |
| 3405 | * |
| 3406 | * Usually this would be handled by the processor noticing an |
| 3407 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 3408 | * pending virtual interrupts. However, this setting was done |
| 3409 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 3410 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
| 3411 | */ |
| 3412 | if (unlikely(evaluate_pending_interrupts)) |
| 3413 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3414 | |
| 3415 | /* |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3416 | * Do not start the preemption timer hrtimer until after we know |
| 3417 | * we are successful, so that only nested_vmx_vmexit needs to cancel |
| 3418 | * the timer. |
| 3419 | */ |
| 3420 | vmx->nested.preemption_timer_expired = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3421 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 3422 | u64 timer_value = vmx_calc_preemption_timer_value(vcpu); |
| 3423 | vmx_start_preemption_timer(vcpu, timer_value); |
| 3424 | } |
Paolo Bonzini | 359a6c3 | 2019-01-29 19:14:46 +0100 | [diff] [blame] | 3425 | |
| 3426 | /* |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3427 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 3428 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 3429 | * returned as far as L1 is concerned. It will only return (and set |
| 3430 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 3431 | */ |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3432 | return NVMX_VMENTRY_SUCCESS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3433 | |
| 3434 | /* |
| 3435 | * A failed consistency check that leads to a VMExit during L1's |
| 3436 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 3437 | * 26.7 "VM-entry failures during or after loading guest state". |
| 3438 | */ |
| 3439 | vmentry_fail_vmexit_guest_mode: |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 3440 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3441 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 3442 | leave_guest_mode(vcpu); |
| 3443 | |
| 3444 | vmentry_fail_vmexit: |
| 3445 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 3446 | |
| 3447 | if (!from_vmentry) |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3448 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3449 | |
| 3450 | load_vmcs12_host_state(vcpu, vmcs12); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 3451 | vmcs12->vm_exit_reason = exit_reason.full; |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3452 | if (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 3453 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3454 | return NVMX_VMENTRY_VMEXIT; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | /* |
| 3458 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 3459 | * for running an L2 nested guest. |
| 3460 | */ |
| 3461 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 3462 | { |
| 3463 | struct vmcs12 *vmcs12; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3464 | enum nvmx_vmentry_status status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3465 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3466 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3467 | enum nested_evmptrld_status evmptrld_status; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3468 | |
| 3469 | if (!nested_vmx_check_permission(vcpu)) |
| 3470 | return 1; |
| 3471 | |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3472 | evmptrld_status = nested_vmx_handle_enlightened_vmptrld(vcpu, launch); |
| 3473 | if (evmptrld_status == EVMPTRLD_ERROR) { |
| 3474 | kvm_queue_exception(vcpu, UD_VECTOR); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3475 | return 1; |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3476 | } else if (CC(evmptrld_status == EVMPTRLD_VMFAIL)) { |
Vitaly Kuznetsov | b6a0653 | 2020-03-09 16:52:13 +0100 | [diff] [blame] | 3477 | return nested_vmx_failInvalid(vcpu); |
| 3478 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3479 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3480 | if (CC(!evmptr_is_valid(vmx->nested.hv_evmcs_vmptr) && |
| 3481 | vmx->nested.current_vmptr == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3482 | return nested_vmx_failInvalid(vcpu); |
| 3483 | |
| 3484 | vmcs12 = get_vmcs12(vcpu); |
| 3485 | |
| 3486 | /* |
| 3487 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 3488 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 3489 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 3490 | * VM-instruction error field. |
| 3491 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3492 | if (CC(vmcs12->hdr.shadow_vmcs)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3493 | return nested_vmx_failInvalid(vcpu); |
| 3494 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 3495 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) { |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 3496 | copy_enlightened_to_vmcs12(vmx, vmx->nested.hv_evmcs->hv_clean_fields); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3497 | /* Enlightened VMCS doesn't have launch state */ |
| 3498 | vmcs12->launch_state = !launch; |
| 3499 | } else if (enable_shadow_vmcs) { |
| 3500 | copy_shadow_to_vmcs12(vmx); |
| 3501 | } |
| 3502 | |
| 3503 | /* |
| 3504 | * The nested entry process starts with enforcing various prerequisites |
| 3505 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 3506 | * they fail: As the SDM explains, some conditions should cause the |
| 3507 | * instruction to fail, while others will cause the instruction to seem |
| 3508 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 3509 | * To speed up the normal (success) code path, we should avoid checking |
| 3510 | * for misconfigurations which will anyway be caught by the processor |
| 3511 | * when using the merged vmcs02. |
| 3512 | */ |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3513 | if (CC(interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3514 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3515 | |
Sean Christopherson | fc595f3 | 2020-08-12 11:06:15 -0700 | [diff] [blame] | 3516 | if (CC(vmcs12->launch_state == launch)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3517 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3518 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 3519 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 3520 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3521 | if (nested_vmx_check_controls(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3522 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 3523 | |
Paolo Bonzini | 98d9e85 | 2019-04-12 10:19:57 +0200 | [diff] [blame] | 3524 | if (nested_vmx_check_host_state(vcpu, vmcs12)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3525 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3526 | |
| 3527 | /* |
| 3528 | * We're finally done with prerequisite checking, and can start with |
| 3529 | * the nested entry. |
| 3530 | */ |
| 3531 | vmx->nested.nested_run_pending = 1; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 3532 | vmx->nested.has_preemption_timer_deadline = false; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3533 | status = nested_vmx_enter_non_root_mode(vcpu, true); |
| 3534 | if (unlikely(status != NVMX_VMENTRY_SUCCESS)) |
| 3535 | goto vmentry_failed; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3536 | |
Sean Christopherson | 25bb2cf | 2020-08-12 10:51:29 -0700 | [diff] [blame] | 3537 | /* Emulate processing of posted interrupts on VM-Enter. */ |
| 3538 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 3539 | kvm_apic_has_interrupt(vcpu) == vmx->nested.posted_intr_nv) { |
| 3540 | vmx->nested.pi_pending = true; |
| 3541 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 3542 | kvm_apic_clear_irr(vcpu, vmx->nested.posted_intr_nv); |
| 3543 | } |
| 3544 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3545 | /* Hide L1D cache contents from the nested guest. */ |
| 3546 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 3547 | |
| 3548 | /* |
| 3549 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
| 3550 | * also be used as part of restoring nVMX state for |
| 3551 | * snapshot restore (migration). |
| 3552 | * |
| 3553 | * In this flow, it is assumed that vmcs12 cache was |
Ingo Molnar | 163b099 | 2021-03-21 22:28:53 +0100 | [diff] [blame] | 3554 | * transferred as part of captured nVMX state and should |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3555 | * therefore not be read from guest memory (which may not |
| 3556 | * exist on destination host yet). |
| 3557 | */ |
| 3558 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 3559 | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3560 | switch (vmcs12->guest_activity_state) { |
| 3561 | case GUEST_ACTIVITY_HLT: |
| 3562 | /* |
| 3563 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be |
| 3564 | * awakened by event injection or by an NMI-window VM-exit or |
| 3565 | * by an interrupt-window VM-exit, halt the vcpu. |
| 3566 | */ |
| 3567 | if (!(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) && |
| 3568 | !nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING) && |
| 3569 | !(nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING) && |
| 3570 | (vmcs12->guest_rflags & X86_EFLAGS_IF))) { |
| 3571 | vmx->nested.nested_run_pending = 0; |
| 3572 | return kvm_vcpu_halt(vcpu); |
| 3573 | } |
| 3574 | break; |
| 3575 | case GUEST_ACTIVITY_WAIT_SIPI: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3576 | vmx->nested.nested_run_pending = 0; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3577 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; |
| 3578 | break; |
| 3579 | default: |
| 3580 | break; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3581 | } |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3582 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3583 | return 1; |
Jim Mattson | 671ddc7 | 2019-10-15 10:44:05 -0700 | [diff] [blame] | 3584 | |
| 3585 | vmentry_failed: |
| 3586 | vmx->nested.nested_run_pending = 0; |
| 3587 | if (status == NVMX_VMENTRY_KVM_INTERNAL_ERROR) |
| 3588 | return 0; |
| 3589 | if (status == NVMX_VMENTRY_VMEXIT) |
| 3590 | return 1; |
| 3591 | WARN_ON_ONCE(status != NVMX_VMENTRY_VMFAIL); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 3592 | return nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | /* |
| 3596 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
Miaohe Lin | 67b0ae4 | 2019-12-11 14:26:22 +0800 | [diff] [blame] | 3597 | * because L2 may have changed some cr0 bits directly (CR0_GUEST_HOST_MASK). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3598 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 3599 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 3600 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 3601 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 3602 | * didn't trap the bit, because if L1 did, so would L0). |
| 3603 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 3604 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 3605 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 3606 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 3607 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 3608 | * changed these bits, and therefore they need to be updated, but L0 |
| 3609 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 3610 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 3611 | */ |
| 3612 | static inline unsigned long |
| 3613 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3614 | { |
| 3615 | return |
| 3616 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 3617 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 3618 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 3619 | vcpu->arch.cr0_guest_owned_bits)); |
| 3620 | } |
| 3621 | |
| 3622 | static inline unsigned long |
| 3623 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 3624 | { |
| 3625 | return |
| 3626 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 3627 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 3628 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 3629 | vcpu->arch.cr4_guest_owned_bits)); |
| 3630 | } |
| 3631 | |
| 3632 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 3633 | struct vmcs12 *vmcs12) |
| 3634 | { |
| 3635 | u32 idt_vectoring; |
| 3636 | unsigned int nr; |
| 3637 | |
| 3638 | if (vcpu->arch.exception.injected) { |
| 3639 | nr = vcpu->arch.exception.nr; |
| 3640 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3641 | |
| 3642 | if (kvm_exception_is_soft(nr)) { |
| 3643 | vmcs12->vm_exit_instruction_len = |
| 3644 | vcpu->arch.event_exit_inst_len; |
| 3645 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 3646 | } else |
| 3647 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 3648 | |
| 3649 | if (vcpu->arch.exception.has_error_code) { |
| 3650 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 3651 | vmcs12->idt_vectoring_error_code = |
| 3652 | vcpu->arch.exception.error_code; |
| 3653 | } |
| 3654 | |
| 3655 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3656 | } else if (vcpu->arch.nmi_injected) { |
| 3657 | vmcs12->idt_vectoring_info_field = |
| 3658 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 3659 | } else if (vcpu->arch.interrupt.injected) { |
| 3660 | nr = vcpu->arch.interrupt.nr; |
| 3661 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 3662 | |
| 3663 | if (vcpu->arch.interrupt.soft) { |
| 3664 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 3665 | vmcs12->vm_entry_instruction_len = |
| 3666 | vcpu->arch.event_exit_inst_len; |
| 3667 | } else |
| 3668 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 3669 | |
| 3670 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | |
Paolo Bonzini | 96b100c | 2020-03-17 18:32:50 +0100 | [diff] [blame] | 3675 | void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3676 | { |
| 3677 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3678 | gfn_t gfn; |
| 3679 | |
| 3680 | /* |
| 3681 | * Don't need to mark the APIC access page dirty; it is never |
| 3682 | * written to by the CPU during APIC virtualization. |
| 3683 | */ |
| 3684 | |
| 3685 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 3686 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 3687 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3688 | } |
| 3689 | |
| 3690 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 3691 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 3692 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 3693 | } |
| 3694 | } |
| 3695 | |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3696 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3697 | { |
| 3698 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3699 | int max_irr; |
| 3700 | void *vapic_page; |
| 3701 | u16 status; |
| 3702 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3703 | if (!vmx->nested.pi_pending) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3704 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3705 | |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3706 | if (!vmx->nested.pi_desc) |
| 3707 | goto mmio_needed; |
| 3708 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3709 | vmx->nested.pi_pending = false; |
Jim Mattson | 966eefb | 2021-06-04 10:26:06 -0700 | [diff] [blame] | 3710 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3711 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3712 | return 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3713 | |
| 3714 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 3715 | if (max_irr != 256) { |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3716 | vapic_page = vmx->nested.virtual_apic_map.hva; |
| 3717 | if (!vapic_page) |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3718 | goto mmio_needed; |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 3719 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3720 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 3721 | vapic_page, &max_irr); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3722 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 3723 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 3724 | status &= ~0xff; |
| 3725 | status |= (u8)max_irr; |
| 3726 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | nested_mark_vmcs12_pages_dirty(vcpu); |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3731 | return 0; |
Jim Mattson | 0fe998b | 2021-06-04 10:26:05 -0700 | [diff] [blame] | 3732 | |
| 3733 | mmio_needed: |
| 3734 | kvm_handle_memory_failure(vcpu, X86EMUL_IO_NEEDED, NULL); |
| 3735 | return -ENXIO; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3739 | unsigned long exit_qual) |
| 3740 | { |
| 3741 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3742 | unsigned int nr = vcpu->arch.exception.nr; |
| 3743 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3744 | |
| 3745 | if (vcpu->arch.exception.has_error_code) { |
| 3746 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3747 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3748 | } |
| 3749 | |
| 3750 | if (kvm_exception_is_soft(nr)) |
| 3751 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3752 | else |
| 3753 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3754 | |
| 3755 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3756 | vmx_get_nmi_mask(vcpu)) |
| 3757 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3758 | |
| 3759 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3760 | } |
| 3761 | |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3762 | /* |
| 3763 | * Returns true if a debug trap is pending delivery. |
| 3764 | * |
| 3765 | * In KVM, debug traps bear an exception payload. As such, the class of a #DB |
| 3766 | * exception may be inferred from the presence of an exception payload. |
| 3767 | */ |
| 3768 | static inline bool vmx_pending_dbg_trap(struct kvm_vcpu *vcpu) |
| 3769 | { |
| 3770 | return vcpu->arch.exception.pending && |
| 3771 | vcpu->arch.exception.nr == DB_VECTOR && |
| 3772 | vcpu->arch.exception.payload; |
| 3773 | } |
| 3774 | |
| 3775 | /* |
| 3776 | * Certain VM-exits set the 'pending debug exceptions' field to indicate a |
| 3777 | * recognized #DB (data or single-step) that has yet to be delivered. Since KVM |
| 3778 | * represents these debug traps with a payload that is said to be compatible |
| 3779 | * with the 'pending debug exceptions' field, write the payload to the VMCS |
| 3780 | * field if a VM-exit is delivered before the debug trap. |
| 3781 | */ |
| 3782 | static void nested_vmx_update_pending_dbg(struct kvm_vcpu *vcpu) |
| 3783 | { |
| 3784 | if (vmx_pending_dbg_trap(vcpu)) |
| 3785 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 3786 | vcpu->arch.exception.payload); |
| 3787 | } |
| 3788 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3789 | static bool nested_vmx_preemption_timer_pending(struct kvm_vcpu *vcpu) |
| 3790 | { |
| 3791 | return nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 3792 | to_vmx(vcpu)->nested.preemption_timer_expired; |
| 3793 | } |
| 3794 | |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 3795 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3796 | { |
| 3797 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3798 | unsigned long exit_qual; |
| 3799 | bool block_nested_events = |
| 3800 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3801 | bool mtf_pending = vmx->nested.mtf_pending; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3802 | struct kvm_lapic *apic = vcpu->arch.apic; |
| 3803 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3804 | /* |
| 3805 | * Clear the MTF state. If a higher priority VM-exit is delivered first, |
| 3806 | * this state is discarded. |
| 3807 | */ |
Oliver Upton | 5c8beb4 | 2020-04-06 20:12:37 +0000 | [diff] [blame] | 3808 | if (!block_nested_events) |
| 3809 | vmx->nested.mtf_pending = false; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3810 | |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3811 | if (lapic_in_kernel(vcpu) && |
| 3812 | test_bit(KVM_APIC_INIT, &apic->pending_events)) { |
| 3813 | if (block_nested_events) |
| 3814 | return -EBUSY; |
Oliver Upton | 684c042 | 2020-02-07 02:36:05 -0800 | [diff] [blame] | 3815 | nested_vmx_update_pending_dbg(vcpu); |
Liran Alon | e64a850 | 2019-11-11 14:16:05 +0200 | [diff] [blame] | 3816 | clear_bit(KVM_APIC_INIT, &apic->pending_events); |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 3817 | if (vcpu->arch.mp_state != KVM_MP_STATE_INIT_RECEIVED) |
| 3818 | nested_vmx_vmexit(vcpu, EXIT_REASON_INIT_SIGNAL, 0, 0); |
| 3819 | return 0; |
| 3820 | } |
| 3821 | |
| 3822 | if (lapic_in_kernel(vcpu) && |
| 3823 | test_bit(KVM_APIC_SIPI, &apic->pending_events)) { |
| 3824 | if (block_nested_events) |
| 3825 | return -EBUSY; |
| 3826 | |
| 3827 | clear_bit(KVM_APIC_SIPI, &apic->pending_events); |
| 3828 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 3829 | nested_vmx_vmexit(vcpu, EXIT_REASON_SIPI_SIGNAL, 0, |
| 3830 | apic->sipi_vector & 0xFFUL); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 3831 | return 0; |
| 3832 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3833 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3834 | /* |
| 3835 | * Process any exceptions that are not debug traps before MTF. |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3836 | * |
| 3837 | * Note that only a pending nested run can block a pending exception. |
| 3838 | * Otherwise an injected NMI/interrupt should either be |
| 3839 | * lost or delivered to the nested hypervisor in the IDT_VECTORING_INFO, |
| 3840 | * while delivering the pending exception. |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3841 | */ |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3842 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3843 | if (vcpu->arch.exception.pending && !vmx_pending_dbg_trap(vcpu)) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3844 | if (vmx->nested.nested_run_pending) |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3845 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3846 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3847 | goto no_vmexit; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 3848 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3849 | return 0; |
| 3850 | } |
| 3851 | |
| 3852 | if (mtf_pending) { |
| 3853 | if (block_nested_events) |
| 3854 | return -EBUSY; |
| 3855 | nested_vmx_update_pending_dbg(vcpu); |
| 3856 | nested_vmx_vmexit(vcpu, EXIT_REASON_MONITOR_TRAP_FLAG, 0, 0); |
| 3857 | return 0; |
| 3858 | } |
| 3859 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3860 | if (vcpu->arch.exception.pending) { |
Maxim Levitsky | 4020da3 | 2021-04-01 17:38:14 +0300 | [diff] [blame] | 3861 | if (vmx->nested.nested_run_pending) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3862 | return -EBUSY; |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3863 | if (!nested_vmx_check_exception(vcpu, &exit_qual)) |
| 3864 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3865 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 3866 | return 0; |
| 3867 | } |
| 3868 | |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 3869 | if (nested_vmx_preemption_timer_pending(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3870 | if (block_nested_events) |
| 3871 | return -EBUSY; |
| 3872 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 3873 | return 0; |
| 3874 | } |
| 3875 | |
Sean Christopherson | 1cd2f0b | 2020-04-22 19:25:46 -0700 | [diff] [blame] | 3876 | if (vcpu->arch.smi_pending && !is_smm(vcpu)) { |
| 3877 | if (block_nested_events) |
| 3878 | return -EBUSY; |
| 3879 | goto no_vmexit; |
| 3880 | } |
| 3881 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3882 | if (vcpu->arch.nmi_pending && !vmx_nmi_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3883 | if (block_nested_events) |
| 3884 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3885 | if (!nested_exit_on_nmi(vcpu)) |
| 3886 | goto no_vmexit; |
| 3887 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3888 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 3889 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 3890 | INTR_INFO_VALID_MASK, 0); |
| 3891 | /* |
| 3892 | * The NMI-triggered VM exit counts as injection: |
| 3893 | * clear this one and block further NMIs. |
| 3894 | */ |
| 3895 | vcpu->arch.nmi_pending = 0; |
| 3896 | vmx_set_nmi_mask(vcpu, true); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3900 | if (kvm_cpu_has_interrupt(vcpu) && !vmx_interrupt_blocked(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3901 | if (block_nested_events) |
| 3902 | return -EBUSY; |
Sean Christopherson | 15ff0b4 | 2020-04-22 19:25:45 -0700 | [diff] [blame] | 3903 | if (!nested_exit_on_intr(vcpu)) |
| 3904 | goto no_vmexit; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3905 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
Sean Christopherson | 6ce347a | 2020-04-22 19:25:38 -0700 | [diff] [blame] | 3909 | no_vmexit: |
Jim Mattson | 650293c | 2021-06-04 10:26:02 -0700 | [diff] [blame] | 3910 | return vmx_complete_nested_posted_interrupt(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 3914 | { |
| 3915 | ktime_t remaining = |
| 3916 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 3917 | u64 value; |
| 3918 | |
| 3919 | if (ktime_to_ns(remaining) <= 0) |
| 3920 | return 0; |
| 3921 | |
| 3922 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 3923 | do_div(value, 1000000); |
| 3924 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 3925 | } |
| 3926 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3927 | static bool is_vmcs12_ext_field(unsigned long field) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3928 | { |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3929 | switch (field) { |
| 3930 | case GUEST_ES_SELECTOR: |
| 3931 | case GUEST_CS_SELECTOR: |
| 3932 | case GUEST_SS_SELECTOR: |
| 3933 | case GUEST_DS_SELECTOR: |
| 3934 | case GUEST_FS_SELECTOR: |
| 3935 | case GUEST_GS_SELECTOR: |
| 3936 | case GUEST_LDTR_SELECTOR: |
| 3937 | case GUEST_TR_SELECTOR: |
| 3938 | case GUEST_ES_LIMIT: |
| 3939 | case GUEST_CS_LIMIT: |
| 3940 | case GUEST_SS_LIMIT: |
| 3941 | case GUEST_DS_LIMIT: |
| 3942 | case GUEST_FS_LIMIT: |
| 3943 | case GUEST_GS_LIMIT: |
| 3944 | case GUEST_LDTR_LIMIT: |
| 3945 | case GUEST_TR_LIMIT: |
| 3946 | case GUEST_GDTR_LIMIT: |
| 3947 | case GUEST_IDTR_LIMIT: |
| 3948 | case GUEST_ES_AR_BYTES: |
| 3949 | case GUEST_DS_AR_BYTES: |
| 3950 | case GUEST_FS_AR_BYTES: |
| 3951 | case GUEST_GS_AR_BYTES: |
| 3952 | case GUEST_LDTR_AR_BYTES: |
| 3953 | case GUEST_TR_AR_BYTES: |
| 3954 | case GUEST_ES_BASE: |
| 3955 | case GUEST_CS_BASE: |
| 3956 | case GUEST_SS_BASE: |
| 3957 | case GUEST_DS_BASE: |
| 3958 | case GUEST_FS_BASE: |
| 3959 | case GUEST_GS_BASE: |
| 3960 | case GUEST_LDTR_BASE: |
| 3961 | case GUEST_TR_BASE: |
| 3962 | case GUEST_GDTR_BASE: |
| 3963 | case GUEST_IDTR_BASE: |
| 3964 | case GUEST_PENDING_DBG_EXCEPTIONS: |
| 3965 | case GUEST_BNDCFGS: |
| 3966 | return true; |
| 3967 | default: |
| 3968 | break; |
| 3969 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3970 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 3971 | return false; |
| 3972 | } |
| 3973 | |
| 3974 | static void sync_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 3975 | struct vmcs12 *vmcs12) |
| 3976 | { |
| 3977 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3978 | |
| 3979 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 3980 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 3981 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 3982 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 3983 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 3984 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 3985 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 3986 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 3987 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 3988 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 3989 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 3990 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 3991 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 3992 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 3993 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 3994 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 3995 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3996 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3997 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 3998 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 3999 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 4000 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 4001 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 4002 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 4003 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 4004 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 4005 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 4006 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 4007 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 4008 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 4009 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 4010 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 4011 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 4012 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4013 | vmcs12->guest_pending_dbg_exceptions = |
| 4014 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 4015 | if (kvm_mpx_supported()) |
| 4016 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
| 4017 | |
| 4018 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = false; |
| 4019 | } |
| 4020 | |
| 4021 | static void copy_vmcs02_to_vmcs12_rare(struct kvm_vcpu *vcpu, |
| 4022 | struct vmcs12 *vmcs12) |
| 4023 | { |
| 4024 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4025 | int cpu; |
| 4026 | |
| 4027 | if (!vmx->nested.need_sync_vmcs02_to_vmcs12_rare) |
| 4028 | return; |
| 4029 | |
| 4030 | |
| 4031 | WARN_ON_ONCE(vmx->loaded_vmcs != &vmx->vmcs01); |
| 4032 | |
| 4033 | cpu = get_cpu(); |
| 4034 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4035 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->vmcs01); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4036 | |
| 4037 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4038 | |
| 4039 | vmx->loaded_vmcs = &vmx->vmcs01; |
Sean Christopherson | 1af1bb0 | 2020-05-06 16:58:50 -0700 | [diff] [blame] | 4040 | vmx_vcpu_load_vmcs(vcpu, cpu, &vmx->nested.vmcs02); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4041 | put_cpu(); |
| 4042 | } |
| 4043 | |
| 4044 | /* |
| 4045 | * Update the guest state fields of vmcs12 to reflect changes that |
| 4046 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 4047 | * VM-entry controls is also updated, since this is really a guest |
| 4048 | * state bit.) |
| 4049 | */ |
| 4050 | static void sync_vmcs02_to_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 4051 | { |
| 4052 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4053 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4054 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4055 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 4056 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4057 | vmx->nested.need_sync_vmcs02_to_vmcs12_rare = |
| 4058 | !evmptr_is_valid(vmx->nested.hv_evmcs_vmptr); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4059 | |
| 4060 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 4061 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 4062 | |
| 4063 | vmcs12->guest_rsp = kvm_rsp_read(vcpu); |
| 4064 | vmcs12->guest_rip = kvm_rip_read(vcpu); |
| 4065 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 4066 | |
| 4067 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 4068 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4069 | |
| 4070 | vmcs12->guest_interruptibility_info = |
| 4071 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4072 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4073 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 4074 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 4075 | else if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) |
| 4076 | vmcs12->guest_activity_state = GUEST_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4077 | else |
| 4078 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
| 4079 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4080 | if (nested_cpu_has_preemption_timer(vmcs12) && |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 4081 | vmcs12->vm_exit_controls & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER && |
| 4082 | !vmx->nested.nested_run_pending) |
| 4083 | vmcs12->vmx_preemption_timer_value = |
| 4084 | vmx_get_preemption_timer_value(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4085 | |
| 4086 | /* |
| 4087 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 4088 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 4089 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 4090 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 4091 | * |
| 4092 | * Additionally, restore L2's PDPTR to vmcs12. |
| 4093 | */ |
| 4094 | if (enable_ept) { |
| 4095 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | c7554efc | 2019-05-07 09:06:40 -0700 | [diff] [blame] | 4096 | if (nested_cpu_has_ept(vmcs12) && is_pae_paging(vcpu)) { |
| 4097 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 4098 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 4099 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 4100 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 4101 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 4105 | |
| 4106 | if (nested_cpu_has_vid(vmcs12)) |
| 4107 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 4108 | |
| 4109 | vmcs12->vm_entry_controls = |
| 4110 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
| 4111 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
| 4112 | |
Sean Christopherson | 699a1ac | 2019-05-07 09:06:37 -0700 | [diff] [blame] | 4113 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4114 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4115 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4116 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 4117 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | /* |
| 4121 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 4122 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 4123 | * and this function updates it to reflect the changes to the guest state while |
| 4124 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 4125 | * without going back to L1), and to reflect the exit reason. |
| 4126 | * Note that we do not have to copy here all VMCS fields, just those that |
| 4127 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 4128 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 4129 | * which already writes to vmcs12 directly. |
| 4130 | */ |
| 4131 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4132 | u32 vm_exit_reason, u32 exit_intr_info, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4133 | unsigned long exit_qualification) |
| 4134 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4135 | /* update exit information fields: */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4136 | vmcs12->vm_exit_reason = vm_exit_reason; |
Sean Christopherson | 3c0c2ad | 2021-04-12 16:21:37 +1200 | [diff] [blame] | 4137 | if (to_vmx(vcpu)->exit_reason.enclave_mode) |
| 4138 | vmcs12->vm_exit_reason |= VMX_EXIT_REASONS_SGX_ENCLAVE_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4139 | vmcs12->exit_qualification = exit_qualification; |
| 4140 | vmcs12->vm_exit_intr_info = exit_intr_info; |
| 4141 | |
| 4142 | vmcs12->idt_vectoring_info_field = 0; |
| 4143 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 4144 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 4145 | |
| 4146 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 4147 | vmcs12->launch_state = 1; |
| 4148 | |
| 4149 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 4150 | * instead of reading the real value. */ |
| 4151 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 4152 | |
| 4153 | /* |
| 4154 | * Transfer the event that L0 or L1 may wanted to inject into |
| 4155 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 4156 | */ |
| 4157 | vmcs12_save_pending_event(vcpu, vmcs12); |
Krish Sadhukhan | a0d4f80 | 2018-12-04 19:00:13 -0500 | [diff] [blame] | 4158 | |
| 4159 | /* |
| 4160 | * According to spec, there's no need to store the guest's |
| 4161 | * MSRs if the exit is due to a VM-entry failure that occurs |
| 4162 | * during or after loading the guest state. Since this exit |
| 4163 | * does not fall in that category, we need to save the MSRs. |
| 4164 | */ |
| 4165 | if (nested_vmx_store_msr(vcpu, |
| 4166 | vmcs12->vm_exit_msr_store_addr, |
| 4167 | vmcs12->vm_exit_msr_store_count)) |
| 4168 | nested_vmx_abort(vcpu, |
| 4169 | VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | /* |
| 4173 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 4174 | * preserved above and would only end up incorrectly in L1. |
| 4175 | */ |
| 4176 | vcpu->arch.nmi_injected = false; |
| 4177 | kvm_clear_exception_queue(vcpu); |
| 4178 | kvm_clear_interrupt_queue(vcpu); |
| 4179 | } |
| 4180 | |
| 4181 | /* |
| 4182 | * A part of what we need to when the nested L2 guest exits and we want to |
| 4183 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 4184 | * in vmcs12. |
| 4185 | * This function is to be called not only on normal nested exit, but also on |
| 4186 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 4187 | * Failures During or After Loading Guest State"). |
| 4188 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 4189 | */ |
| 4190 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 4191 | struct vmcs12 *vmcs12) |
| 4192 | { |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 4193 | enum vm_entry_failure_code ignored; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4194 | struct kvm_segment seg; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4195 | |
| 4196 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 4197 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 4198 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4199 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 4200 | else |
| 4201 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 4202 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 4203 | |
Paolo Bonzini | e9c16c7 | 2019-04-30 22:07:26 +0200 | [diff] [blame] | 4204 | kvm_rsp_write(vcpu, vmcs12->host_rsp); |
| 4205 | kvm_rip_write(vcpu, vmcs12->host_rip); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4206 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
| 4207 | vmx_set_interrupt_shadow(vcpu, 0); |
| 4208 | |
| 4209 | /* |
| 4210 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 4211 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 4212 | * |
| 4213 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 4214 | * (KVM doesn't change it); |
| 4215 | */ |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4216 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4217 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
| 4218 | |
| 4219 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
| 4220 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4221 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
| 4222 | |
| 4223 | nested_ept_uninit_mmu_context(vcpu); |
| 4224 | |
| 4225 | /* |
| 4226 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 4227 | * couldn't have changed. |
| 4228 | */ |
Maxim Levitsky | 0f85722 | 2021-06-07 12:02:00 +0300 | [diff] [blame] | 4229 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, true, &ignored)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4230 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 4231 | |
Sean Christopherson | 50b265a | 2020-03-20 14:28:19 -0700 | [diff] [blame] | 4232 | nested_vmx_transition_tlb_flush(vcpu, vmcs12, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4233 | |
| 4234 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 4235 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 4236 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 4237 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 4238 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 4239 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 4240 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
| 4241 | |
| 4242 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 4243 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 4244 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 4245 | |
| 4246 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 4247 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 4248 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 4249 | } |
| 4250 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Oliver Upton | d196842 | 2019-12-13 16:33:58 -0800 | [diff] [blame] | 4251 | WARN_ON_ONCE(kvm_set_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL, |
| 4252 | vmcs12->host_ia32_perf_global_ctrl)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4253 | |
| 4254 | /* Set L1 segment info according to Intel SDM |
| 4255 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 4256 | seg = (struct kvm_segment) { |
| 4257 | .base = 0, |
| 4258 | .limit = 0xFFFFFFFF, |
| 4259 | .selector = vmcs12->host_cs_selector, |
| 4260 | .type = 11, |
| 4261 | .present = 1, |
| 4262 | .s = 1, |
| 4263 | .g = 1 |
| 4264 | }; |
| 4265 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 4266 | seg.l = 1; |
| 4267 | else |
| 4268 | seg.db = 1; |
| 4269 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 4270 | seg = (struct kvm_segment) { |
| 4271 | .base = 0, |
| 4272 | .limit = 0xFFFFFFFF, |
| 4273 | .type = 3, |
| 4274 | .present = 1, |
| 4275 | .s = 1, |
| 4276 | .db = 1, |
| 4277 | .g = 1 |
| 4278 | }; |
| 4279 | seg.selector = vmcs12->host_ds_selector; |
| 4280 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 4281 | seg.selector = vmcs12->host_es_selector; |
| 4282 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 4283 | seg.selector = vmcs12->host_ss_selector; |
| 4284 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 4285 | seg.selector = vmcs12->host_fs_selector; |
| 4286 | seg.base = vmcs12->host_fs_base; |
| 4287 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 4288 | seg.selector = vmcs12->host_gs_selector; |
| 4289 | seg.base = vmcs12->host_gs_base; |
| 4290 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 4291 | seg = (struct kvm_segment) { |
| 4292 | .base = vmcs12->host_tr_base, |
| 4293 | .limit = 0x67, |
| 4294 | .selector = vmcs12->host_tr_selector, |
| 4295 | .type = 11, |
| 4296 | .present = 1 |
| 4297 | }; |
| 4298 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 4299 | |
| 4300 | kvm_set_dr(vcpu, 7, 0x400); |
| 4301 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4302 | |
| 4303 | if (cpu_has_vmx_msr_bitmap()) |
| 4304 | vmx_update_msr_bitmap(vcpu); |
| 4305 | |
| 4306 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 4307 | vmcs12->vm_exit_msr_load_count)) |
| 4308 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4309 | } |
| 4310 | |
| 4311 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 4312 | { |
Sean Christopherson | eb3db1b | 2020-09-23 11:03:58 -0700 | [diff] [blame] | 4313 | struct vmx_uret_msr *efer_msr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4314 | unsigned int i; |
| 4315 | |
| 4316 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 4317 | return vmcs_read64(GUEST_IA32_EFER); |
| 4318 | |
| 4319 | if (cpu_has_load_ia32_efer()) |
| 4320 | return host_efer; |
| 4321 | |
| 4322 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 4323 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 4324 | return vmx->msr_autoload.guest.val[i].value; |
| 4325 | } |
| 4326 | |
Sean Christopherson | d85a803 | 2020-09-23 11:04:06 -0700 | [diff] [blame] | 4327 | efer_msr = vmx_find_uret_msr(vmx, MSR_EFER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4328 | if (efer_msr) |
| 4329 | return efer_msr->data; |
| 4330 | |
| 4331 | return host_efer; |
| 4332 | } |
| 4333 | |
| 4334 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 4335 | { |
| 4336 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4337 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4338 | struct vmx_msr_entry g, h; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4339 | gpa_t gpa; |
| 4340 | u32 i, j; |
| 4341 | |
| 4342 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 4343 | |
| 4344 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 4345 | /* |
| 4346 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 4347 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 4348 | * and vcpu->arch.dr7 is not squirreled away before the |
| 4349 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 4350 | */ |
| 4351 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 4352 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 4353 | else |
| 4354 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 4359 | * handle a variety of side effects to KVM's software model. |
| 4360 | */ |
| 4361 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 4362 | |
Sean Christopherson | fa71e95 | 2020-07-02 21:04:22 -0700 | [diff] [blame] | 4363 | vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4364 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 4365 | |
| 4366 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 4367 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 4368 | |
| 4369 | nested_ept_uninit_mmu_context(vcpu); |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4370 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
Sean Christopherson | cb3c1e2 | 2019-09-27 14:45:22 -0700 | [diff] [blame] | 4371 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4372 | |
| 4373 | /* |
| 4374 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 4375 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 4376 | * VMFail, like everything else we just need to ensure our |
| 4377 | * software model is up-to-date. |
| 4378 | */ |
Sean Christopherson | 9932b49 | 2020-04-15 13:34:50 -0700 | [diff] [blame] | 4379 | if (enable_ept && is_pae_paging(vcpu)) |
Sean Christopherson | f087a02 | 2019-06-07 11:55:34 -0700 | [diff] [blame] | 4380 | ept_save_pdptrs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4381 | |
| 4382 | kvm_mmu_reset_context(vcpu); |
| 4383 | |
| 4384 | if (cpu_has_vmx_msr_bitmap()) |
| 4385 | vmx_update_msr_bitmap(vcpu); |
| 4386 | |
| 4387 | /* |
| 4388 | * This nasty bit of open coding is a compromise between blindly |
| 4389 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 4390 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 4391 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 4392 | * expensive since the lists are unbound by hardware). For each |
| 4393 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 4394 | * list, reload it from the exit load list if it exists and differs |
| 4395 | * from the guest value. The intent is to stuff host state as |
| 4396 | * silently as possible, not to fully process the exit load list. |
| 4397 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4398 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 4399 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 4400 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 4401 | pr_debug_ratelimited( |
| 4402 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 4403 | __func__, i, gpa); |
| 4404 | goto vmabort; |
| 4405 | } |
| 4406 | |
| 4407 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 4408 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 4409 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 4410 | pr_debug_ratelimited( |
| 4411 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 4412 | __func__, j, gpa); |
| 4413 | goto vmabort; |
| 4414 | } |
| 4415 | if (h.index != g.index) |
| 4416 | continue; |
| 4417 | if (h.value == g.value) |
| 4418 | break; |
| 4419 | |
| 4420 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 4421 | pr_debug_ratelimited( |
| 4422 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 4423 | __func__, j, h.index, h.reserved); |
| 4424 | goto vmabort; |
| 4425 | } |
| 4426 | |
Sean Christopherson | f20935d | 2019-09-05 14:22:54 -0700 | [diff] [blame] | 4427 | if (kvm_set_msr(vcpu, h.index, h.value)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4428 | pr_debug_ratelimited( |
| 4429 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 4430 | __func__, j, h.index, h.value); |
| 4431 | goto vmabort; |
| 4432 | } |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | return; |
| 4437 | |
| 4438 | vmabort: |
| 4439 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 4440 | } |
| 4441 | |
| 4442 | /* |
| 4443 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 4444 | * and modify vmcs12 to make it see what it would expect to see there if |
| 4445 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 4446 | */ |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4447 | void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4448 | u32 exit_intr_info, unsigned long exit_qualification) |
| 4449 | { |
| 4450 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4451 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4452 | |
| 4453 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 4454 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 4455 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4456 | /* Similarly, triple faults in L2 should never escape. */ |
| 4457 | WARN_ON_ONCE(kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)); |
| 4458 | |
Vitaly Kuznetsov | f5c7e84 | 2021-05-03 17:08:51 +0200 | [diff] [blame] | 4459 | if (kvm_check_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu)) { |
| 4460 | /* |
| 4461 | * KVM_REQ_GET_NESTED_STATE_PAGES is also used to map |
| 4462 | * Enlightened VMCS after migration and we still need to |
| 4463 | * do that when something is forcing L2->L1 exit prior to |
| 4464 | * the first L2 run. |
| 4465 | */ |
| 4466 | (void)nested_get_evmcs_page(vcpu); |
| 4467 | } |
Maxim Levitsky | f2c7ef3 | 2021-01-07 11:38:51 +0200 | [diff] [blame] | 4468 | |
Sean Christopherson | eeeb4f6 | 2020-03-20 14:28:20 -0700 | [diff] [blame] | 4469 | /* Service the TLB flush request for L2 before switching to L1. */ |
| 4470 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) |
| 4471 | kvm_vcpu_flush_tlb_current(vcpu); |
| 4472 | |
Peter Shier | 43fea4e | 2020-08-20 16:05:45 -0700 | [diff] [blame] | 4473 | /* |
| 4474 | * VCPU_EXREG_PDPTR will be clobbered in arch/x86/kvm/vmx/vmx.h between |
| 4475 | * now and the new vmentry. Ensure that the VMCS02 PDPTR fields are |
| 4476 | * up-to-date before switching to L1. |
| 4477 | */ |
| 4478 | if (enable_ept && is_pae_paging(vcpu)) |
| 4479 | vmx_ept_load_pdptrs(vcpu); |
| 4480 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4481 | leave_guest_mode(vcpu); |
| 4482 | |
Paolo Bonzini | b4b65b5 | 2019-01-29 19:12:35 +0100 | [diff] [blame] | 4483 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 4484 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 4485 | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 4486 | if (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETTING)) { |
| 4487 | vcpu->arch.tsc_offset = vcpu->arch.l1_tsc_offset; |
| 4488 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_TSC_SCALING)) |
| 4489 | vcpu->arch.tsc_scaling_ratio = vcpu->arch.l1_tsc_scaling_ratio; |
| 4490 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4491 | |
| 4492 | if (likely(!vmx->fail)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4493 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | f4f8316 | 2019-05-07 08:36:26 -0700 | [diff] [blame] | 4494 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4495 | if (vm_exit_reason != -1) |
| 4496 | prepare_vmcs12(vcpu, vmcs12, vm_exit_reason, |
| 4497 | exit_intr_info, exit_qualification); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4498 | |
| 4499 | /* |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4500 | * Must happen outside of sync_vmcs02_to_vmcs12() as it will |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4501 | * also be used to capture vmcs12 cache as part of |
| 4502 | * capturing nVMX state for snapshot (migration). |
| 4503 | * |
| 4504 | * Otherwise, this flush will dirty guest memory at a |
| 4505 | * point it is already assumed by user-space to be |
| 4506 | * immutable. |
| 4507 | */ |
| 4508 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4509 | } else { |
| 4510 | /* |
| 4511 | * The only expected VM-instruction error is "VM entry with |
| 4512 | * invalid control field(s)." Anything else indicates a |
| 4513 | * problem with L0. And we should never get here with a |
| 4514 | * VMFail of any type if early consistency checks are enabled. |
| 4515 | */ |
| 4516 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 4517 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 4518 | WARN_ON_ONCE(nested_early_check); |
| 4519 | } |
| 4520 | |
| 4521 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 4522 | |
| 4523 | /* Update any VMCS fields that might have changed while L2 ran */ |
| 4524 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 4525 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 4526 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Ilias Stamatis | 1ab9287 | 2021-06-07 11:54:38 +0100 | [diff] [blame] | 4527 | if (kvm_has_tsc_control) |
| 4528 | vmcs_write64(TSC_MULTIPLIER, vcpu->arch.tsc_scaling_ratio); |
| 4529 | |
Liran Alon | 02d496cf | 2019-11-11 14:30:55 +0200 | [diff] [blame] | 4530 | if (vmx->nested.l1_tpr_threshold != -1) |
| 4531 | vmcs_write32(TPR_THRESHOLD, vmx->nested.l1_tpr_threshold); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4532 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4533 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 4534 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 4535 | vmx_set_virtual_apic_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4536 | } |
| 4537 | |
Makarand Sonare | a85863c | 2021-02-12 16:50:12 -0800 | [diff] [blame] | 4538 | if (vmx->nested.update_vmcs01_cpu_dirty_logging) { |
| 4539 | vmx->nested.update_vmcs01_cpu_dirty_logging = false; |
| 4540 | vmx_update_cpu_dirty_logging(vcpu); |
| 4541 | } |
| 4542 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4543 | /* Unpin physical memory we referred to in vmcs02 */ |
| 4544 | if (vmx->nested.apic_access_page) { |
Liran Alon | b11494b | 2019-11-21 00:31:47 +0200 | [diff] [blame] | 4545 | kvm_release_page_clean(vmx->nested.apic_access_page); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4546 | vmx->nested.apic_access_page = NULL; |
| 4547 | } |
KarimAllah Ahmed | 96c66e8 | 2019-01-31 21:24:37 +0100 | [diff] [blame] | 4548 | kvm_vcpu_unmap(vcpu, &vmx->nested.virtual_apic_map, true); |
KarimAllah Ahmed | 3278e04 | 2019-01-31 21:24:38 +0100 | [diff] [blame] | 4549 | kvm_vcpu_unmap(vcpu, &vmx->nested.pi_desc_map, true); |
| 4550 | vmx->nested.pi_desc = NULL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4551 | |
Sean Christopherson | 1196cb9 | 2020-03-20 14:28:23 -0700 | [diff] [blame] | 4552 | if (vmx->nested.reload_vmcs01_apic_access_page) { |
| 4553 | vmx->nested.reload_vmcs01_apic_access_page = false; |
| 4554 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
| 4555 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4556 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4557 | if ((vm_exit_reason != -1) && |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 4558 | (enable_shadow_vmcs || evmptr_is_valid(vmx->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 4559 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4560 | |
| 4561 | /* in case we halted in L2 */ |
| 4562 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
| 4563 | |
| 4564 | if (likely(!vmx->fail)) { |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4565 | if ((u16)vm_exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
Sean Christopherson | a1c77ab | 2020-03-02 22:27:35 -0800 | [diff] [blame] | 4566 | nested_exit_intr_ack_set(vcpu)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4567 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 4568 | WARN_ON(irq < 0); |
| 4569 | vmcs12->vm_exit_intr_info = irq | |
| 4570 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 4571 | } |
| 4572 | |
Sean Christopherson | 4dcefa3 | 2020-04-15 10:55:18 -0700 | [diff] [blame] | 4573 | if (vm_exit_reason != -1) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4574 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 4575 | vmcs12->exit_qualification, |
| 4576 | vmcs12->idt_vectoring_info_field, |
| 4577 | vmcs12->vm_exit_intr_info, |
| 4578 | vmcs12->vm_exit_intr_error_code, |
| 4579 | KVM_ISA_VMX); |
| 4580 | |
| 4581 | load_vmcs12_host_state(vcpu, vmcs12); |
| 4582 | |
| 4583 | return; |
| 4584 | } |
| 4585 | |
| 4586 | /* |
| 4587 | * After an early L2 VM-entry failure, we're now back |
| 4588 | * in L1 which thinks it just finished a VMLAUNCH or |
| 4589 | * VMRESUME instruction, so we need to set the failure |
| 4590 | * flag and the VM-instruction error field of the VMCS |
| 4591 | * accordingly, and skip the emulated instruction. |
| 4592 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4593 | (void)nested_vmx_fail(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4594 | |
| 4595 | /* |
| 4596 | * Restore L1's host state to KVM's software model. We're here |
| 4597 | * because a consistency check was caught by hardware, which |
| 4598 | * means some amount of guest state has been propagated to KVM's |
| 4599 | * model and needs to be unwound to the host's state. |
| 4600 | */ |
| 4601 | nested_vmx_restore_host_state(vcpu); |
| 4602 | |
| 4603 | vmx->fail = 0; |
| 4604 | } |
| 4605 | |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 4606 | static void nested_vmx_triple_fault(struct kvm_vcpu *vcpu) |
| 4607 | { |
| 4608 | nested_vmx_vmexit(vcpu, EXIT_REASON_TRIPLE_FAULT, 0, 0); |
| 4609 | } |
| 4610 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4611 | /* |
| 4612 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4613 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4614 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
Miaohe Lin | 49f933d | 2020-02-27 11:20:54 +0800 | [diff] [blame] | 4615 | * #UD, #GP, or #SS. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4616 | */ |
| 4617 | 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] | 4618 | u32 vmx_instruction_info, bool wr, int len, gva_t *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4619 | { |
| 4620 | gva_t off; |
| 4621 | bool exn; |
| 4622 | struct kvm_segment s; |
| 4623 | |
| 4624 | /* |
| 4625 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4626 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4627 | * addressing components of the operand. Only the displacement part |
| 4628 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4629 | * For how an actual address is calculated from all these components, |
| 4630 | * refer to Vol. 1, "Operand Addressing". |
| 4631 | */ |
| 4632 | int scaling = vmx_instruction_info & 3; |
| 4633 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4634 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4635 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4636 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4637 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4638 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4639 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4640 | |
| 4641 | if (is_reg) { |
| 4642 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4643 | return 1; |
| 4644 | } |
| 4645 | |
| 4646 | /* Addr = segment_base + offset */ |
| 4647 | /* offset = base + [index * scale] + displacement */ |
| 4648 | off = exit_qualification; /* holds the displacement */ |
Sean Christopherson | 946c522 | 2019-01-23 14:39:23 -0800 | [diff] [blame] | 4649 | if (addr_size == 1) |
| 4650 | off = (gva_t)sign_extend64(off, 31); |
| 4651 | else if (addr_size == 0) |
| 4652 | off = (gva_t)sign_extend64(off, 15); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4653 | if (base_is_valid) |
| 4654 | off += kvm_register_read(vcpu, base_reg); |
| 4655 | if (index_is_valid) |
Miaohe Lin | e630269 | 2020-02-15 10:44:22 +0800 | [diff] [blame] | 4656 | off += kvm_register_read(vcpu, index_reg) << scaling; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4657 | vmx_get_segment(vcpu, &s, seg_reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4658 | |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4659 | /* |
| 4660 | * The effective address, i.e. @off, of a memory operand is truncated |
| 4661 | * based on the address size of the instruction. Note that this is |
| 4662 | * the *effective address*, i.e. the address prior to accounting for |
| 4663 | * the segment's base. |
| 4664 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4665 | if (addr_size == 1) /* 32 bit */ |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4666 | off &= 0xffffffff; |
| 4667 | else if (addr_size == 0) /* 16 bit */ |
| 4668 | off &= 0xffff; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4669 | |
| 4670 | /* Checks for #GP/#SS exceptions. */ |
| 4671 | exn = false; |
| 4672 | if (is_long_mode(vcpu)) { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4673 | /* |
| 4674 | * The virtual/linear address is never truncated in 64-bit |
| 4675 | * mode, e.g. a 32-bit address size can yield a 64-bit virtual |
| 4676 | * address when using FS/GS with a non-zero base. |
| 4677 | */ |
Liran Alon | 6694e48 | 2019-07-15 18:47:44 +0300 | [diff] [blame] | 4678 | if (seg_reg == VCPU_SREG_FS || seg_reg == VCPU_SREG_GS) |
| 4679 | *ret = s.base + off; |
| 4680 | else |
| 4681 | *ret = off; |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4682 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4683 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 4684 | * non-canonical form. This is the only check on the memory |
| 4685 | * destination for long mode! |
| 4686 | */ |
| 4687 | exn = is_noncanonical_address(*ret, vcpu); |
Paolo Bonzini | e0dfacb | 2019-01-30 17:25:38 +0100 | [diff] [blame] | 4688 | } else { |
Sean Christopherson | 8570f9e | 2019-01-23 14:39:24 -0800 | [diff] [blame] | 4689 | /* |
| 4690 | * When not in long mode, the virtual/linear address is |
| 4691 | * unconditionally truncated to 32 bits regardless of the |
| 4692 | * address size. |
| 4693 | */ |
| 4694 | *ret = (s.base + off) & 0xffffffff; |
| 4695 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4696 | /* Protected mode: apply checks for segment validity in the |
| 4697 | * following order: |
| 4698 | * - segment type check (#GP(0) may be thrown) |
| 4699 | * - usability check (#GP(0)/#SS(0)) |
| 4700 | * - limit check (#GP(0)/#SS(0)) |
| 4701 | */ |
| 4702 | if (wr) |
| 4703 | /* #GP(0) if the destination operand is located in a |
| 4704 | * read-only data segment or any code segment. |
| 4705 | */ |
| 4706 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 4707 | else |
| 4708 | /* #GP(0) if the source operand is located in an |
| 4709 | * execute-only code segment |
| 4710 | */ |
| 4711 | exn = ((s.type & 0xa) == 8); |
| 4712 | if (exn) { |
| 4713 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 4714 | return 1; |
| 4715 | } |
| 4716 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 4717 | */ |
| 4718 | exn = (s.unusable != 0); |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4719 | |
| 4720 | /* |
| 4721 | * Protected mode: #GP(0)/#SS(0) if the memory operand is |
| 4722 | * outside the segment limit. All CPUs that support VMX ignore |
| 4723 | * limit checks for flat segments, i.e. segments with base==0, |
| 4724 | * limit==0xffffffff and of type expand-up data or code. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4725 | */ |
Sean Christopherson | 34333cc | 2019-01-23 14:39:25 -0800 | [diff] [blame] | 4726 | if (!(s.base == 0 && s.limit == 0xffffffff && |
| 4727 | ((s.type & 8) || !(s.type & 4)))) |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4728 | exn = exn || ((u64)off + len - 1 > s.limit); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4729 | } |
| 4730 | if (exn) { |
| 4731 | kvm_queue_exception_e(vcpu, |
| 4732 | seg_reg == VCPU_SREG_SS ? |
| 4733 | SS_VECTOR : GP_VECTOR, |
| 4734 | 0); |
| 4735 | return 1; |
| 4736 | } |
| 4737 | |
| 4738 | return 0; |
| 4739 | } |
| 4740 | |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4741 | void nested_vmx_pmu_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 4742 | { |
| 4743 | struct vcpu_vmx *vmx; |
| 4744 | |
| 4745 | if (!nested_vmx_allowed(vcpu)) |
| 4746 | return; |
| 4747 | |
| 4748 | vmx = to_vmx(vcpu); |
Sean Christopherson | afaf0b2 | 2020-03-21 13:26:00 -0700 | [diff] [blame] | 4749 | if (kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, MSR_CORE_PERF_GLOBAL_CTRL)) { |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4750 | vmx->nested.msrs.entry_ctls_high |= |
| 4751 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4752 | vmx->nested.msrs.exit_ctls_high |= |
| 4753 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4754 | } else { |
| 4755 | vmx->nested.msrs.entry_ctls_high &= |
| 4756 | ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 4757 | vmx->nested.msrs.exit_ctls_high &= |
Chenyi Qiang | c6b177a | 2020-08-28 16:56:21 +0800 | [diff] [blame] | 4758 | ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 4759 | } |
| 4760 | } |
| 4761 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4762 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer, |
| 4763 | int *ret) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4764 | { |
| 4765 | gva_t gva; |
| 4766 | struct x86_exception e; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4767 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4768 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 4769 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 4770 | vmcs_read32(VMX_INSTRUCTION_INFO), false, |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4771 | sizeof(*vmpointer), &gva)) { |
| 4772 | *ret = 1; |
| 4773 | return -EINVAL; |
| 4774 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4775 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4776 | r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e); |
| 4777 | if (r != X86EMUL_CONTINUE) { |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 4778 | *ret = kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4779 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | return 0; |
| 4783 | } |
| 4784 | |
| 4785 | /* |
| 4786 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 4787 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 4788 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 4789 | */ |
| 4790 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 4791 | { |
| 4792 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4793 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 4794 | |
| 4795 | /* |
| 4796 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 4797 | * executes VMXON and free it when L1 executes VMXOFF. |
| 4798 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 4799 | * here when vmcs01 already have an allocated shadow vmcs. |
| 4800 | */ |
| 4801 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 4802 | |
| 4803 | if (!loaded_vmcs->shadow_vmcs) { |
| 4804 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 4805 | if (loaded_vmcs->shadow_vmcs) |
| 4806 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 4807 | } |
| 4808 | return loaded_vmcs->shadow_vmcs; |
| 4809 | } |
| 4810 | |
| 4811 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 4812 | { |
| 4813 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4814 | int r; |
| 4815 | |
| 4816 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 4817 | if (r < 0) |
| 4818 | goto out_vmcs02; |
| 4819 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4820 | vmx->nested.cached_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4821 | if (!vmx->nested.cached_vmcs12) |
| 4822 | goto out_cached_vmcs12; |
| 4823 | |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 4824 | vmx->nested.cached_shadow_vmcs12 = kzalloc(VMCS12_SIZE, GFP_KERNEL_ACCOUNT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4825 | if (!vmx->nested.cached_shadow_vmcs12) |
| 4826 | goto out_cached_shadow_vmcs12; |
| 4827 | |
| 4828 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 4829 | goto out_shadow_vmcs; |
| 4830 | |
| 4831 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Jim Mattson | ada0098 | 2020-05-08 13:36:42 -0700 | [diff] [blame] | 4832 | HRTIMER_MODE_ABS_PINNED); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4833 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 4834 | |
| 4835 | vmx->nested.vpid02 = allocate_vpid(); |
| 4836 | |
| 4837 | vmx->nested.vmcs02_initialized = false; |
| 4838 | vmx->nested.vmxon = true; |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4839 | |
Sean Christopherson | 2ef7619 | 2020-03-02 15:56:22 -0800 | [diff] [blame] | 4840 | if (vmx_pt_mode_is_host_guest()) { |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4841 | vmx->pt_desc.guest.ctl = 0; |
Aaron Lewis | 476c9bd | 2020-09-25 16:34:18 +0200 | [diff] [blame] | 4842 | pt_update_intercept_for_msr(vcpu); |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 4843 | } |
| 4844 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4845 | return 0; |
| 4846 | |
| 4847 | out_shadow_vmcs: |
| 4848 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 4849 | |
| 4850 | out_cached_shadow_vmcs12: |
| 4851 | kfree(vmx->nested.cached_vmcs12); |
| 4852 | |
| 4853 | out_cached_vmcs12: |
| 4854 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 4855 | |
| 4856 | out_vmcs02: |
| 4857 | return -ENOMEM; |
| 4858 | } |
| 4859 | |
| 4860 | /* |
| 4861 | * Emulate the VMXON instruction. |
| 4862 | * Currently, we just remember that VMX is active, and do not save or even |
| 4863 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 4864 | * do not currently need to store anything in that guest-allocated memory |
| 4865 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 4866 | * argument is different from the VMXON pointer (which the spec says they do). |
| 4867 | */ |
| 4868 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4869 | { |
| 4870 | int ret; |
| 4871 | gpa_t vmptr; |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4872 | uint32_t revision; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4873 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 4874 | const u64 VMXON_NEEDED_FEATURES = FEAT_CTL_LOCKED |
| 4875 | | FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4876 | |
| 4877 | /* |
| 4878 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 4879 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
Sean Christopherson | c2fe3cd | 2020-10-06 18:44:15 -0700 | [diff] [blame] | 4880 | * 1 (see vmx_is_valid_cr4() for when we allow the guest to set this). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4881 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 4882 | * have already been checked by hardware, prior to the VM-exit for |
| 4883 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 4884 | * that bit set to 1 in non-root mode. |
| 4885 | */ |
| 4886 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
| 4887 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4888 | return 1; |
| 4889 | } |
| 4890 | |
| 4891 | /* CPL=0 must be checked manually. */ |
| 4892 | if (vmx_get_cpl(vcpu)) { |
| 4893 | kvm_inject_gp(vcpu, 0); |
| 4894 | return 1; |
| 4895 | } |
| 4896 | |
| 4897 | if (vmx->nested.vmxon) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4898 | return nested_vmx_fail(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4899 | |
| 4900 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 4901 | != VMXON_NEEDED_FEATURES) { |
| 4902 | kvm_inject_gp(vcpu, 0); |
| 4903 | return 1; |
| 4904 | } |
| 4905 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4906 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &ret)) |
| 4907 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4908 | |
| 4909 | /* |
| 4910 | * SDM 3: 24.11.5 |
| 4911 | * The first 4 bytes of VMXON region contain the supported |
| 4912 | * VMCS revision identifier |
| 4913 | * |
| 4914 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 4915 | * which replaces physical address width with 32 |
| 4916 | */ |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4917 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4918 | return nested_vmx_failInvalid(vcpu); |
| 4919 | |
KarimAllah Ahmed | 2e40893 | 2019-01-31 21:24:31 +0100 | [diff] [blame] | 4920 | if (kvm_read_guest(vcpu->kvm, vmptr, &revision, sizeof(revision)) || |
| 4921 | revision != VMCS12_REVISION) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4922 | return nested_vmx_failInvalid(vcpu); |
| 4923 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4924 | vmx->nested.vmxon_ptr = vmptr; |
| 4925 | ret = enter_vmx_operation(vcpu); |
| 4926 | if (ret) |
| 4927 | return ret; |
| 4928 | |
| 4929 | return nested_vmx_succeed(vcpu); |
| 4930 | } |
| 4931 | |
| 4932 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
| 4933 | { |
| 4934 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4935 | |
| 4936 | if (vmx->nested.current_vmptr == -1ull) |
| 4937 | return; |
| 4938 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 4939 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 4940 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4941 | if (enable_shadow_vmcs) { |
| 4942 | /* copy to memory all shadowed fields in case |
| 4943 | they were modified */ |
| 4944 | copy_shadow_to_vmcs12(vmx); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4945 | vmx_disable_shadow_vmcs(vmx); |
| 4946 | } |
| 4947 | vmx->nested.posted_intr_nv = -1; |
| 4948 | |
| 4949 | /* Flush VMCS12 to guest memory */ |
| 4950 | kvm_vcpu_write_guest_page(vcpu, |
| 4951 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 4952 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
| 4953 | |
| 4954 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 4955 | |
| 4956 | vmx->nested.current_vmptr = -1ull; |
| 4957 | } |
| 4958 | |
| 4959 | /* Emulate the VMXOFF instruction */ |
| 4960 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4961 | { |
| 4962 | if (!nested_vmx_check_permission(vcpu)) |
| 4963 | return 1; |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4964 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4965 | free_nested(vcpu); |
Liran Alon | 4b9852f | 2019-08-26 13:24:49 +0300 | [diff] [blame] | 4966 | |
| 4967 | /* Process a latched INIT during time CPU was in VMX operation */ |
| 4968 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4969 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4970 | return nested_vmx_succeed(vcpu); |
| 4971 | } |
| 4972 | |
| 4973 | /* Emulate the VMCLEAR instruction */ |
| 4974 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 4975 | { |
| 4976 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4977 | u32 zero = 0; |
| 4978 | gpa_t vmptr; |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4979 | u64 evmcs_gpa; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4980 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4981 | |
| 4982 | if (!nested_vmx_check_permission(vcpu)) |
| 4983 | return 1; |
| 4984 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 4985 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 4986 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4987 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 4988 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4989 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4990 | |
| 4991 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 4992 | return nested_vmx_fail(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 4993 | |
Vitaly Kuznetsov | 11e3491 | 2019-06-28 13:23:33 +0200 | [diff] [blame] | 4994 | /* |
| 4995 | * When Enlightened VMEntry is enabled on the calling CPU we treat |
| 4996 | * memory area pointer by vmptr as Enlightened VMCS (as there's no good |
| 4997 | * way to distinguish it from VMCS12) and we must not corrupt it by |
| 4998 | * writing to the non-existent 'launch_state' field. The area doesn't |
| 4999 | * have to be the currently active EVMCS on the calling CPU and there's |
| 5000 | * nothing KVM has to do to transition it from 'active' to 'non-active' |
| 5001 | * state. It is possible that the area will stay mapped as |
| 5002 | * vmx->nested.hv_evmcs but this shouldn't be a problem. |
| 5003 | */ |
| 5004 | if (likely(!vmx->nested.enlightened_vmcs_enabled || |
| 5005 | !nested_enlightened_vmentry(vcpu, &evmcs_gpa))) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5006 | if (vmptr == vmx->nested.current_vmptr) |
| 5007 | nested_release_vmcs12(vcpu); |
| 5008 | |
| 5009 | kvm_vcpu_write_guest(vcpu, |
| 5010 | vmptr + offsetof(struct vmcs12, |
| 5011 | launch_state), |
| 5012 | &zero, sizeof(zero)); |
Vitaly Kuznetsov | 3b19b81 | 2021-05-26 15:20:21 +0200 | [diff] [blame] | 5013 | } else if (vmx->nested.hv_evmcs && vmptr == vmx->nested.hv_evmcs_vmptr) { |
| 5014 | nested_release_evmcs(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | return nested_vmx_succeed(vcpu); |
| 5018 | } |
| 5019 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5020 | /* Emulate the VMLAUNCH instruction */ |
| 5021 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5022 | { |
| 5023 | return nested_vmx_run(vcpu, true); |
| 5024 | } |
| 5025 | |
| 5026 | /* Emulate the VMRESUME instruction */ |
| 5027 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5028 | { |
| 5029 | |
| 5030 | return nested_vmx_run(vcpu, false); |
| 5031 | } |
| 5032 | |
| 5033 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5034 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5035 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5036 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5037 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5038 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5039 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f7eea63 | 2019-09-14 00:26:27 +0200 | [diff] [blame] | 5040 | struct x86_exception e; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5041 | unsigned long field; |
| 5042 | u64 value; |
| 5043 | gva_t gva = 0; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5044 | short offset; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5045 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5046 | |
| 5047 | if (!nested_vmx_check_permission(vcpu)) |
| 5048 | return 1; |
| 5049 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5050 | /* |
| 5051 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5052 | * any VMREAD sets the ALU flags for VMfailInvalid. |
| 5053 | */ |
| 5054 | if (vmx->nested.current_vmptr == -1ull || |
| 5055 | (is_guest_mode(vcpu) && |
| 5056 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5057 | return nested_vmx_failInvalid(vcpu); |
| 5058 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5059 | /* Decode instruction info and find the field to read */ |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5060 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5061 | |
| 5062 | offset = vmcs_field_to_offset(field); |
| 5063 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5064 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5065 | |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 5066 | if (!is_guest_mode(vcpu) && is_vmcs12_ext_field(field)) |
| 5067 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
| 5068 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5069 | /* Read the field, zero-extended to a u64 value */ |
| 5070 | value = vmcs12_read_any(vmcs12, field, offset); |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5071 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5072 | /* |
| 5073 | * Now copy part of this value to register or memory, as requested. |
| 5074 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5075 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5076 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5077 | if (instr_info & BIT(10)) { |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5078 | kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5079 | } else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5080 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5081 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5082 | instr_info, true, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5083 | return 1; |
| 5084 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5085 | r = kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); |
| 5086 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5087 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | return nested_vmx_succeed(vcpu); |
| 5091 | } |
| 5092 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5093 | static bool is_shadow_field_rw(unsigned long field) |
| 5094 | { |
| 5095 | switch (field) { |
| 5096 | #define SHADOW_FIELD_RW(x, y) case x: |
| 5097 | #include "vmcs_shadow_fields.h" |
| 5098 | return true; |
| 5099 | default: |
| 5100 | break; |
| 5101 | } |
| 5102 | return false; |
| 5103 | } |
| 5104 | |
| 5105 | static bool is_shadow_field_ro(unsigned long field) |
| 5106 | { |
| 5107 | switch (field) { |
| 5108 | #define SHADOW_FIELD_RO(x, y) case x: |
| 5109 | #include "vmcs_shadow_fields.h" |
| 5110 | return true; |
| 5111 | default: |
| 5112 | break; |
| 5113 | } |
| 5114 | return false; |
| 5115 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5116 | |
| 5117 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5118 | { |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5119 | struct vmcs12 *vmcs12 = is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) |
| 5120 | : get_vmcs12(vcpu); |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5121 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5122 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5123 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5124 | struct x86_exception e; |
| 5125 | unsigned long field; |
Sean Christopherson | 1c6f0b4 | 2019-05-07 08:36:25 -0700 | [diff] [blame] | 5126 | short offset; |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5127 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5128 | int len, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5129 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5130 | /* |
| 5131 | * The value to write might be 32 or 64 bits, depending on L1's long |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5132 | * mode, and eventually we need to write that into a field of several |
| 5133 | * possible lengths. The code below first zero-extends the value to 64 |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5134 | * bit (value), and then copies only the appropriate number of |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5135 | * bits into the vmcs12 field. |
| 5136 | */ |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5137 | u64 value = 0; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5138 | |
| 5139 | if (!nested_vmx_check_permission(vcpu)) |
| 5140 | return 1; |
| 5141 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5142 | /* |
| 5143 | * In VMX non-root operation, when the VMCS-link pointer is -1ull, |
| 5144 | * any VMWRITE sets the ALU flags for VMfailInvalid. |
| 5145 | */ |
| 5146 | if (vmx->nested.current_vmptr == -1ull || |
| 5147 | (is_guest_mode(vcpu) && |
| 5148 | get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5149 | return nested_vmx_failInvalid(vcpu); |
| 5150 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5151 | if (instr_info & BIT(10)) |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5152 | value = kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5153 | else { |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5154 | len = is_64_bit_mode(vcpu) ? 8 : 4; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5155 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5156 | instr_info, false, len, &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5157 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5158 | r = kvm_read_guest_virt(vcpu, gva, &value, len, &e); |
| 5159 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5160 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5161 | } |
| 5162 | |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5163 | field = kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5164 | |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5165 | offset = vmcs_field_to_offset(field); |
| 5166 | if (offset < 0) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5167 | return nested_vmx_fail(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Jim Mattson | 693e02c | 2019-12-06 15:46:36 -0800 | [diff] [blame] | 5168 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5169 | /* |
| 5170 | * If the vCPU supports "VMWRITE to any supported field in the |
| 5171 | * VMCS," then the "read-only" fields are actually read/write. |
| 5172 | */ |
| 5173 | if (vmcs_field_readonly(field) && |
| 5174 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5175 | return nested_vmx_fail(vcpu, VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5176 | |
Jim Mattson | dd2d604 | 2019-12-06 15:46:35 -0800 | [diff] [blame] | 5177 | /* |
| 5178 | * Ensure vmcs12 is up-to-date before any VMWRITE that dirties |
| 5179 | * vmcs12, else we may crush a field or consume a stale value. |
| 5180 | */ |
| 5181 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) |
| 5182 | copy_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5183 | |
| 5184 | /* |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5185 | * Some Intel CPUs intentionally drop the reserved bits of the AR byte |
| 5186 | * fields on VMWRITE. Emulate this behavior to ensure consistent KVM |
| 5187 | * behavior regardless of the underlying hardware, e.g. if an AR_BYTE |
| 5188 | * field is intercepted for VMWRITE but not VMREAD (in L1), then VMREAD |
| 5189 | * from L1 will return a different value than VMREAD from L2 (L1 sees |
| 5190 | * 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] | 5191 | */ |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5192 | if (field >= GUEST_ES_AR_BYTES && field <= GUEST_TR_AR_BYTES) |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5193 | value &= 0x1f0ff; |
Sean Christopherson | b643780 | 2019-05-07 08:36:24 -0700 | [diff] [blame] | 5194 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5195 | vmcs12_write_any(vmcs12, field, offset, value); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5196 | |
| 5197 | /* |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5198 | * Do not track vmcs12 dirty-state if in guest-mode as we actually |
| 5199 | * dirty shadow vmcs12 instead of vmcs12. Fields that can be updated |
| 5200 | * by L1 without a vmexit are always updated in the vmcs02, i.e. don't |
| 5201 | * "dirty" vmcs12, all others go down the prepare_vmcs02() slow path. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5202 | */ |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5203 | if (!is_guest_mode(vcpu) && !is_shadow_field_rw(field)) { |
| 5204 | /* |
| 5205 | * L1 can read these fields without exiting, ensure the |
| 5206 | * shadow VMCS is up-to-date. |
| 5207 | */ |
| 5208 | if (enable_shadow_vmcs && is_shadow_field_ro(field)) { |
| 5209 | preempt_disable(); |
| 5210 | vmcs_load(vmx->vmcs01.shadow_vmcs); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5211 | |
Jim Mattson | c90f4d0 | 2019-12-06 15:46:37 -0800 | [diff] [blame] | 5212 | __vmcs_writel(field, value); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 5213 | |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5214 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 5215 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 5216 | preempt_enable(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5217 | } |
Sean Christopherson | e217429 | 2019-05-07 08:36:28 -0700 | [diff] [blame] | 5218 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | return nested_vmx_succeed(vcpu); |
| 5222 | } |
| 5223 | |
| 5224 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5225 | { |
| 5226 | vmx->nested.current_vmptr = vmptr; |
| 5227 | if (enable_shadow_vmcs) { |
Sean Christopherson | fe7f895d | 2019-05-07 12:17:57 -0700 | [diff] [blame] | 5228 | secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_SHADOW_VMCS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5229 | vmcs_write64(VMCS_LINK_POINTER, |
| 5230 | __pa(vmx->vmcs01.shadow_vmcs)); |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 5231 | vmx->nested.need_vmcs12_to_shadow_sync = true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5232 | } |
| 5233 | vmx->nested.dirty_vmcs12 = true; |
| 5234 | } |
| 5235 | |
| 5236 | /* Emulate the VMPTRLD instruction */ |
| 5237 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5238 | { |
| 5239 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5240 | gpa_t vmptr; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5241 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5242 | |
| 5243 | if (!nested_vmx_check_permission(vcpu)) |
| 5244 | return 1; |
| 5245 | |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5246 | if (nested_vmx_get_vmptr(vcpu, &vmptr, &r)) |
| 5247 | return r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5248 | |
KarimAllah Ahmed | e0bf266 | 2019-01-31 21:24:43 +0100 | [diff] [blame] | 5249 | if (!page_address_valid(vcpu, vmptr)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5250 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5251 | |
| 5252 | if (vmptr == vmx->nested.vmxon_ptr) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5253 | return nested_vmx_fail(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5254 | |
| 5255 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5256 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5257 | return 1; |
| 5258 | |
| 5259 | if (vmx->nested.current_vmptr != vmptr) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5260 | struct kvm_host_map map; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5261 | struct vmcs12 *new_vmcs12; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5262 | |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5263 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmptr), &map)) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5264 | /* |
| 5265 | * Reads from an unbacked page return all 1s, |
| 5266 | * which means that the 32 bits located at the |
| 5267 | * given physical address won't match the required |
| 5268 | * VMCS12_REVISION identifier. |
| 5269 | */ |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5270 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5271 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5272 | } |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5273 | |
| 5274 | new_vmcs12 = map.hva; |
| 5275 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5276 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 5277 | (new_vmcs12->hdr.shadow_vmcs && |
| 5278 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5279 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5280 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5281 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5282 | } |
| 5283 | |
| 5284 | nested_release_vmcs12(vcpu); |
| 5285 | |
| 5286 | /* |
| 5287 | * Load VMCS12 from guest memory since it is not already |
| 5288 | * cached. |
| 5289 | */ |
| 5290 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
KarimAllah Ahmed | b146b83 | 2019-01-31 21:24:35 +0100 | [diff] [blame] | 5291 | kvm_vcpu_unmap(vcpu, &map, false); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5292 | |
| 5293 | set_current_vmptr(vmx, vmptr); |
| 5294 | } |
| 5295 | |
| 5296 | return nested_vmx_succeed(vcpu); |
| 5297 | } |
| 5298 | |
| 5299 | /* Emulate the VMPTRST instruction */ |
| 5300 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5301 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5302 | unsigned long exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5303 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5304 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
| 5305 | struct x86_exception e; |
| 5306 | gva_t gva; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5307 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5308 | |
| 5309 | if (!nested_vmx_check_permission(vcpu)) |
| 5310 | return 1; |
| 5311 | |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 5312 | if (unlikely(evmptr_is_valid(to_vmx(vcpu)->nested.hv_evmcs_vmptr))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5313 | return 1; |
| 5314 | |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5315 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, |
| 5316 | true, sizeof(gpa_t), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5317 | return 1; |
| 5318 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5319 | r = kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 5320 | sizeof(gpa_t), &e); |
| 5321 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5322 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5323 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5324 | return nested_vmx_succeed(vcpu); |
| 5325 | } |
| 5326 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5327 | #define EPTP_PA_MASK GENMASK_ULL(51, 12) |
| 5328 | |
| 5329 | static bool nested_ept_root_matches(hpa_t root_hpa, u64 root_eptp, u64 eptp) |
| 5330 | { |
| 5331 | return VALID_PAGE(root_hpa) && |
| 5332 | ((root_eptp & EPTP_PA_MASK) == (eptp & EPTP_PA_MASK)); |
| 5333 | } |
| 5334 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5335 | /* Emulate the INVEPT instruction */ |
| 5336 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 5337 | { |
| 5338 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5339 | u32 vmx_instruction_info, types; |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5340 | unsigned long type, roots_to_free; |
| 5341 | struct kvm_mmu *mmu; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5342 | gva_t gva; |
| 5343 | struct x86_exception e; |
| 5344 | struct { |
| 5345 | u64 eptp, gpa; |
| 5346 | } operand; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5347 | int i, r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5348 | |
| 5349 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5350 | SECONDARY_EXEC_ENABLE_EPT) || |
| 5351 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 5352 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5353 | return 1; |
| 5354 | } |
| 5355 | |
| 5356 | if (!nested_vmx_check_permission(vcpu)) |
| 5357 | return 1; |
| 5358 | |
| 5359 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5360 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5361 | |
| 5362 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 5363 | |
| 5364 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5365 | return nested_vmx_fail(vcpu, VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5366 | |
| 5367 | /* According to the Intel VMX instruction reference, the memory |
| 5368 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5369 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5370 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5371 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5372 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5373 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5374 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5375 | return kvm_handle_memory_failure(vcpu, r, &e); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5376 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5377 | /* |
| 5378 | * Nested EPT roots are always held through guest_mmu, |
| 5379 | * not root_mmu. |
| 5380 | */ |
| 5381 | mmu = &vcpu->arch.guest_mmu; |
| 5382 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5383 | switch (type) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5384 | case VMX_EPT_EXTENT_CONTEXT: |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5385 | if (!nested_vmx_check_eptp(vcpu, operand.eptp)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5386 | return nested_vmx_fail(vcpu, |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5387 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | f8aa7e3 | 2020-03-20 14:27:59 -0700 | [diff] [blame] | 5388 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5389 | roots_to_free = 0; |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5390 | if (nested_ept_root_matches(mmu->root_hpa, mmu->root_pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5391 | operand.eptp)) |
| 5392 | roots_to_free |= KVM_MMU_ROOT_CURRENT; |
| 5393 | |
| 5394 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) { |
| 5395 | if (nested_ept_root_matches(mmu->prev_roots[i].hpa, |
Sean Christopherson | be01e8e | 2020-03-20 14:28:32 -0700 | [diff] [blame] | 5396 | mmu->prev_roots[i].pgd, |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5397 | operand.eptp)) |
| 5398 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
| 5399 | } |
| 5400 | break; |
Sean Christopherson | eed0030 | 2020-03-20 14:27:58 -0700 | [diff] [blame] | 5401 | case VMX_EPT_EXTENT_GLOBAL: |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5402 | roots_to_free = KVM_MMU_ROOTS_ALL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5403 | break; |
| 5404 | default: |
Sean Christopherson | f9336e3 | 2020-05-04 08:35:06 -0700 | [diff] [blame] | 5405 | BUG(); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5406 | break; |
| 5407 | } |
| 5408 | |
Sean Christopherson | ce8fe7b | 2020-03-20 14:28:31 -0700 | [diff] [blame] | 5409 | if (roots_to_free) |
| 5410 | kvm_mmu_free_roots(vcpu, mmu, roots_to_free); |
| 5411 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5412 | return nested_vmx_succeed(vcpu); |
| 5413 | } |
| 5414 | |
| 5415 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 5416 | { |
| 5417 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5418 | u32 vmx_instruction_info; |
| 5419 | unsigned long type, types; |
| 5420 | gva_t gva; |
| 5421 | struct x86_exception e; |
| 5422 | struct { |
| 5423 | u64 vpid; |
| 5424 | u64 gla; |
| 5425 | } operand; |
| 5426 | u16 vpid02; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5427 | int r; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5428 | |
| 5429 | if (!(vmx->nested.msrs.secondary_ctls_high & |
| 5430 | SECONDARY_EXEC_ENABLE_VPID) || |
| 5431 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 5432 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5433 | return 1; |
| 5434 | } |
| 5435 | |
| 5436 | if (!nested_vmx_check_permission(vcpu)) |
| 5437 | return 1; |
| 5438 | |
| 5439 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5440 | type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5441 | |
| 5442 | types = (vmx->nested.msrs.vpid_caps & |
| 5443 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
| 5444 | |
| 5445 | if (type >= 32 || !(types & (1 << type))) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5446 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5447 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5448 | |
| 5449 | /* according to the intel vmx instruction reference, the memory |
| 5450 | * operand is read even if it isn't needed (e.g., for type==global) |
| 5451 | */ |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5452 | if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), |
Eugene Korenevsky | fdb2861 | 2019-06-06 00:19:16 +0300 | [diff] [blame] | 5453 | vmx_instruction_info, false, sizeof(operand), &gva)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5454 | return 1; |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5455 | r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); |
| 5456 | if (r != X86EMUL_CONTINUE) |
Babu Moger | 3f3393b | 2020-09-11 14:29:05 -0500 | [diff] [blame] | 5457 | return kvm_handle_memory_failure(vcpu, r, &e); |
Vitaly Kuznetsov | 7a35e51 | 2020-06-05 13:59:05 +0200 | [diff] [blame] | 5458 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5459 | if (operand.vpid >> 16) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5460 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5461 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 5462 | |
| 5463 | vpid02 = nested_get_vpid02(vcpu); |
| 5464 | switch (type) { |
| 5465 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
| 5466 | if (!operand.vpid || |
| 5467 | is_noncanonical_address(operand.gla, vcpu)) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5468 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5469 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | bc41d0c | 2020-03-20 14:28:09 -0700 | [diff] [blame] | 5470 | vpid_sync_vcpu_addr(vpid02, operand.gla); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5471 | break; |
| 5472 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 5473 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 5474 | if (!operand.vpid) |
Sean Christopherson | b2656e4 | 2020-06-08 18:56:07 -0700 | [diff] [blame] | 5475 | return nested_vmx_fail(vcpu, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5476 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5477 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5478 | break; |
| 5479 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Sean Christopherson | 446ace4 | 2020-03-20 14:28:05 -0700 | [diff] [blame] | 5480 | vpid_sync_context(vpid02); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5481 | break; |
| 5482 | default: |
| 5483 | WARN_ON_ONCE(1); |
| 5484 | return kvm_skip_emulated_instruction(vcpu); |
| 5485 | } |
| 5486 | |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5487 | /* |
| 5488 | * Sync the shadow page tables if EPT is disabled, L1 is invalidating |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5489 | * linear mappings for L2 (tagged with L2's VPID). Free all guest |
| 5490 | * roots as VPIDs are not tracked in the MMU role. |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5491 | * |
| 5492 | * Note, this operates on root_mmu, not guest_mmu, as L1 and L2 share |
| 5493 | * an MMU when EPT is disabled. |
| 5494 | * |
| 5495 | * TODO: sync only the affected SPTEs for INVDIVIDUAL_ADDR. |
| 5496 | */ |
| 5497 | if (!enable_ept) |
Sean Christopherson | 25b62c6 | 2021-06-09 16:42:29 -0700 | [diff] [blame] | 5498 | kvm_mmu_free_guest_mode_roots(vcpu, &vcpu->arch.root_mmu); |
Junaid Shahid | d6e3f83 | 2020-03-20 14:28:00 -0700 | [diff] [blame] | 5499 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5500 | return nested_vmx_succeed(vcpu); |
| 5501 | } |
| 5502 | |
| 5503 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 5504 | struct vmcs12 *vmcs12) |
| 5505 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5506 | u32 index = kvm_rcx_read(vcpu); |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5507 | u64 new_eptp; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5508 | |
| 5509 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 5510 | !nested_cpu_has_ept(vmcs12)) |
| 5511 | return 1; |
| 5512 | |
| 5513 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 5514 | return 1; |
| 5515 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5516 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5517 | &new_eptp, index * 8, 8)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5518 | return 1; |
| 5519 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5520 | /* |
| 5521 | * If the (L2) guest does a vmfunc to the currently |
| 5522 | * active ept pointer, we don't have to do anything else |
| 5523 | */ |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5524 | if (vmcs12->ept_pointer != new_eptp) { |
| 5525 | if (!nested_vmx_check_eptp(vcpu, new_eptp)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5526 | return 1; |
| 5527 | |
Sean Christopherson | ac6389a | 2020-03-02 18:02:38 -0800 | [diff] [blame] | 5528 | vmcs12->ept_pointer = new_eptp; |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5529 | nested_ept_new_eptp(vcpu); |
Sean Christopherson | c805f5d | 2021-03-04 17:10:57 -0800 | [diff] [blame] | 5530 | |
Sean Christopherson | 39353ab | 2021-06-09 16:42:31 -0700 | [diff] [blame] | 5531 | if (!nested_cpu_has_vpid(vmcs12)) |
| 5532 | kvm_make_request(KVM_REQ_TLB_FLUSH_GUEST, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | return 0; |
| 5536 | } |
| 5537 | |
| 5538 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 5539 | { |
| 5540 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5541 | struct vmcs12 *vmcs12; |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5542 | u32 function = kvm_rax_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5543 | |
| 5544 | /* |
| 5545 | * VMFUNC is only supported for nested guests, but we always enable the |
| 5546 | * secondary control for simplicity; for non-nested mode, fake that we |
| 5547 | * didn't by injecting #UD. |
| 5548 | */ |
| 5549 | if (!is_guest_mode(vcpu)) { |
| 5550 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5551 | return 1; |
| 5552 | } |
| 5553 | |
| 5554 | vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 0e75225 | 2021-06-09 16:42:22 -0700 | [diff] [blame] | 5555 | if (!(vmcs12->vm_function_control & BIT_ULL(function))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5556 | goto fail; |
| 5557 | |
| 5558 | switch (function) { |
| 5559 | case 0: |
| 5560 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 5561 | goto fail; |
| 5562 | break; |
| 5563 | default: |
| 5564 | goto fail; |
| 5565 | } |
| 5566 | return kvm_skip_emulated_instruction(vcpu); |
| 5567 | |
| 5568 | fail: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5569 | /* |
| 5570 | * This is effectively a reflected VM-Exit, as opposed to a synthesized |
| 5571 | * nested VM-Exit. Pass the original exit reason, i.e. don't hardcode |
| 5572 | * EXIT_REASON_VMFUNC as the exit reason. |
| 5573 | */ |
| 5574 | nested_vmx_vmexit(vcpu, vmx->exit_reason.full, |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5575 | vmx_get_intr_info(vcpu), |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5576 | vmx_get_exit_qual(vcpu)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5577 | return 1; |
| 5578 | } |
| 5579 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5580 | /* |
| 5581 | * Return true if an IO instruction with the specified port and size should cause |
| 5582 | * a VM-exit into L1. |
| 5583 | */ |
| 5584 | bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, |
| 5585 | int size) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5586 | { |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5587 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5588 | gpa_t bitmap, last_bitmap; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5589 | u8 b; |
| 5590 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5591 | last_bitmap = (gpa_t)-1; |
| 5592 | b = -1; |
| 5593 | |
| 5594 | while (size > 0) { |
| 5595 | if (port < 0x8000) |
| 5596 | bitmap = vmcs12->io_bitmap_a; |
| 5597 | else if (port < 0x10000) |
| 5598 | bitmap = vmcs12->io_bitmap_b; |
| 5599 | else |
| 5600 | return true; |
| 5601 | bitmap += (port & 0x7fff) / 8; |
| 5602 | |
| 5603 | if (last_bitmap != bitmap) |
| 5604 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
| 5605 | return true; |
| 5606 | if (b & (1 << (port & 7))) |
| 5607 | return true; |
| 5608 | |
| 5609 | port++; |
| 5610 | size--; |
| 5611 | last_bitmap = bitmap; |
| 5612 | } |
| 5613 | |
| 5614 | return false; |
| 5615 | } |
| 5616 | |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5617 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5618 | struct vmcs12 *vmcs12) |
| 5619 | { |
| 5620 | unsigned long exit_qualification; |
Oliver Upton | 35a5713 | 2020-02-04 15:26:31 -0800 | [diff] [blame] | 5621 | unsigned short port; |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5622 | int size; |
| 5623 | |
| 5624 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5625 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
| 5626 | |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5627 | exit_qualification = vmx_get_exit_qual(vcpu); |
Oliver Upton | e71237d | 2020-02-04 15:26:30 -0800 | [diff] [blame] | 5628 | |
| 5629 | port = exit_qualification >> 16; |
| 5630 | size = (exit_qualification & 7) + 1; |
| 5631 | |
| 5632 | return nested_vmx_check_io_bitmaps(vcpu, port, size); |
| 5633 | } |
| 5634 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5635 | /* |
Miaohe Lin | 463bfee | 2020-02-14 10:44:05 +0800 | [diff] [blame] | 5636 | * Return 1 if we should exit from L2 to L1 to handle an MSR access, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5637 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5638 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5639 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5640 | */ |
| 5641 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5642 | struct vmcs12 *vmcs12, |
| 5643 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5644 | { |
Sean Christopherson | 2b3eaf8 | 2019-04-30 10:36:19 -0700 | [diff] [blame] | 5645 | u32 msr_index = kvm_rcx_read(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5646 | gpa_t bitmap; |
| 5647 | |
| 5648 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 5649 | return true; |
| 5650 | |
| 5651 | /* |
| 5652 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5653 | * for the four combinations of read/write and low/high MSR numbers. |
| 5654 | * First we need to figure out which of the four to use: |
| 5655 | */ |
| 5656 | bitmap = vmcs12->msr_bitmap; |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5657 | if (exit_reason.basic == EXIT_REASON_MSR_WRITE) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5658 | bitmap += 2048; |
| 5659 | if (msr_index >= 0xc0000000) { |
| 5660 | msr_index -= 0xc0000000; |
| 5661 | bitmap += 1024; |
| 5662 | } |
| 5663 | |
| 5664 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5665 | if (msr_index < 1024*8) { |
| 5666 | unsigned char b; |
| 5667 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
| 5668 | return true; |
| 5669 | return 1 & (b >> (msr_index & 7)); |
| 5670 | } else |
| 5671 | return true; /* let L1 handle the wrong parameter */ |
| 5672 | } |
| 5673 | |
| 5674 | /* |
| 5675 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5676 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5677 | * intercept (via guest_host_mask etc.) the current event. |
| 5678 | */ |
| 5679 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5680 | struct vmcs12 *vmcs12) |
| 5681 | { |
Sean Christopherson | 5addc23 | 2020-04-15 13:34:53 -0700 | [diff] [blame] | 5682 | unsigned long exit_qualification = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5683 | int cr = exit_qualification & 15; |
| 5684 | int reg; |
| 5685 | unsigned long val; |
| 5686 | |
| 5687 | switch ((exit_qualification >> 4) & 3) { |
| 5688 | case 0: /* mov to cr */ |
| 5689 | reg = (exit_qualification >> 8) & 15; |
Sean Christopherson | 27b4a9c4 | 2021-04-21 19:21:28 -0700 | [diff] [blame] | 5690 | val = kvm_register_read(vcpu, reg); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5691 | switch (cr) { |
| 5692 | case 0: |
| 5693 | if (vmcs12->cr0_guest_host_mask & |
| 5694 | (val ^ vmcs12->cr0_read_shadow)) |
| 5695 | return true; |
| 5696 | break; |
| 5697 | case 3: |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5698 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5699 | return true; |
| 5700 | break; |
| 5701 | case 4: |
| 5702 | if (vmcs12->cr4_guest_host_mask & |
| 5703 | (vmcs12->cr4_read_shadow ^ val)) |
| 5704 | return true; |
| 5705 | break; |
| 5706 | case 8: |
| 5707 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5708 | return true; |
| 5709 | break; |
| 5710 | } |
| 5711 | break; |
| 5712 | case 2: /* clts */ |
| 5713 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5714 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5715 | return true; |
| 5716 | break; |
| 5717 | case 1: /* mov from cr */ |
| 5718 | switch (cr) { |
| 5719 | case 3: |
| 5720 | if (vmcs12->cpu_based_vm_exec_control & |
| 5721 | CPU_BASED_CR3_STORE_EXITING) |
| 5722 | return true; |
| 5723 | break; |
| 5724 | case 8: |
| 5725 | if (vmcs12->cpu_based_vm_exec_control & |
| 5726 | CPU_BASED_CR8_STORE_EXITING) |
| 5727 | return true; |
| 5728 | break; |
| 5729 | } |
| 5730 | break; |
| 5731 | case 3: /* lmsw */ |
| 5732 | /* |
| 5733 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5734 | * cr0. Other attempted changes are ignored, with no exit. |
| 5735 | */ |
| 5736 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
| 5737 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5738 | (val ^ vmcs12->cr0_read_shadow)) |
| 5739 | return true; |
| 5740 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5741 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5742 | (val & 0x1)) |
| 5743 | return true; |
| 5744 | break; |
| 5745 | } |
| 5746 | return false; |
| 5747 | } |
| 5748 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5749 | static bool nested_vmx_exit_handled_encls(struct kvm_vcpu *vcpu, |
| 5750 | struct vmcs12 *vmcs12) |
| 5751 | { |
| 5752 | u32 encls_leaf; |
| 5753 | |
| 5754 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SGX) || |
| 5755 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENCLS_EXITING)) |
| 5756 | return false; |
| 5757 | |
| 5758 | encls_leaf = kvm_rax_read(vcpu); |
| 5759 | if (encls_leaf > 62) |
| 5760 | encls_leaf = 63; |
| 5761 | return vmcs12->encls_exiting_bitmap & BIT_ULL(encls_leaf); |
| 5762 | } |
| 5763 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5764 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 5765 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 5766 | { |
| 5767 | u32 vmx_instruction_info; |
| 5768 | unsigned long field; |
| 5769 | u8 b; |
| 5770 | |
| 5771 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 5772 | return true; |
| 5773 | |
| 5774 | /* Decode instruction info and find the field to access */ |
| 5775 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5776 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5777 | |
| 5778 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 5779 | if (field >> 15) |
| 5780 | return true; |
| 5781 | |
| 5782 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 5783 | return true; |
| 5784 | |
| 5785 | return 1 & (b >> (field & 7)); |
| 5786 | } |
| 5787 | |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5788 | static bool nested_vmx_exit_handled_mtf(struct vmcs12 *vmcs12) |
| 5789 | { |
| 5790 | u32 entry_intr_info = vmcs12->vm_entry_intr_info_field; |
| 5791 | |
| 5792 | if (nested_cpu_has_mtf(vmcs12)) |
| 5793 | return true; |
| 5794 | |
| 5795 | /* |
| 5796 | * An MTF VM-exit may be injected into the guest by setting the |
| 5797 | * interruption-type to 7 (other event) and the vector field to 0. Such |
| 5798 | * is the case regardless of the 'monitor trap flag' VM-execution |
| 5799 | * control. |
| 5800 | */ |
| 5801 | return entry_intr_info == (INTR_INFO_VALID_MASK |
| 5802 | | INTR_TYPE_OTHER_EVENT); |
| 5803 | } |
| 5804 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5805 | /* |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5806 | * Return true if L0 wants to handle an exit from L2 regardless of whether or not |
| 5807 | * L1 wants the exit. Only call this when in is_guest_mode (L2). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5808 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5809 | static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu, |
| 5810 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5811 | { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5812 | u32 intr_info; |
| 5813 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5814 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5815 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5816 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5817 | if (is_nmi(intr_info)) |
| 5818 | return true; |
| 5819 | else if (is_page_fault(intr_info)) |
Vitaly Kuznetsov | 68fd66f | 2020-05-25 16:41:17 +0200 | [diff] [blame] | 5820 | return vcpu->arch.apf.host_apf_flags || !enable_ept; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5821 | else if (is_debug(intr_info) && |
| 5822 | vcpu->guest_debug & |
| 5823 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5824 | return true; |
| 5825 | else if (is_breakpoint(intr_info) && |
| 5826 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 5827 | return true; |
| 5828 | return false; |
| 5829 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5830 | return true; |
| 5831 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5832 | return true; |
| 5833 | case EXIT_REASON_EPT_VIOLATION: |
| 5834 | /* |
| 5835 | * L0 always deals with the EPT violation. If nested EPT is |
| 5836 | * used, and the nested mmu code discovers that the address is |
| 5837 | * missing in the guest EPT table (EPT12), the EPT violation |
| 5838 | * will be injected with nested_ept_inject_page_fault() |
| 5839 | */ |
| 5840 | return true; |
| 5841 | case EXIT_REASON_EPT_MISCONFIG: |
| 5842 | /* |
| 5843 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 5844 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 5845 | * (EPT on EPT). So any problems with the structure of the |
| 5846 | * table is L0's fault. |
| 5847 | */ |
| 5848 | return true; |
| 5849 | case EXIT_REASON_PREEMPTION_TIMER: |
| 5850 | return true; |
| 5851 | case EXIT_REASON_PML_FULL: |
Sean Christopherson | c3bb9a2 | 2021-02-12 16:50:07 -0800 | [diff] [blame] | 5852 | /* |
| 5853 | * PML is emulated for an L1 VMM and should never be enabled in |
| 5854 | * vmcs02, always "handle" PML_FULL by exiting to userspace. |
| 5855 | */ |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5856 | return true; |
| 5857 | case EXIT_REASON_VMFUNC: |
| 5858 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 5859 | return true; |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5860 | default: |
| 5861 | break; |
| 5862 | } |
| 5863 | return false; |
| 5864 | } |
| 5865 | |
| 5866 | /* |
| 5867 | * Return 1 if L1 wants to intercept an exit from L2. Only call this when in |
| 5868 | * is_guest_mode (L2). |
| 5869 | */ |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5870 | static bool nested_vmx_l1_wants_exit(struct kvm_vcpu *vcpu, |
| 5871 | union vmx_exit_reason exit_reason) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5872 | { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5873 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Sean Christopherson | 9bd4af2 | 2020-04-21 00:53:27 -0700 | [diff] [blame] | 5874 | u32 intr_info; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5875 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5876 | switch ((u16)exit_reason.basic) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5877 | case EXIT_REASON_EXCEPTION_NMI: |
Sean Christopherson | 8791585 | 2020-04-15 13:34:54 -0700 | [diff] [blame] | 5878 | intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5879 | if (is_nmi(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5880 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5881 | else if (is_page_fault(intr_info)) |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5882 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5883 | return vmcs12->exception_bitmap & |
| 5884 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5885 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5886 | return nested_exit_on_intr(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5887 | case EXIT_REASON_TRIPLE_FAULT: |
| 5888 | return true; |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 5889 | case EXIT_REASON_INTERRUPT_WINDOW: |
| 5890 | return nested_cpu_has(vmcs12, CPU_BASED_INTR_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5891 | case EXIT_REASON_NMI_WINDOW: |
Xiaoyao Li | 4e2a0bc | 2019-12-06 16:45:25 +0800 | [diff] [blame] | 5892 | return nested_cpu_has(vmcs12, CPU_BASED_NMI_WINDOW_EXITING); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5893 | case EXIT_REASON_TASK_SWITCH: |
| 5894 | return true; |
| 5895 | case EXIT_REASON_CPUID: |
| 5896 | return true; |
| 5897 | case EXIT_REASON_HLT: |
| 5898 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5899 | case EXIT_REASON_INVD: |
| 5900 | return true; |
| 5901 | case EXIT_REASON_INVLPG: |
| 5902 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5903 | case EXIT_REASON_RDPMC: |
| 5904 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5905 | case EXIT_REASON_RDRAND: |
| 5906 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
| 5907 | case EXIT_REASON_RDSEED: |
| 5908 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
| 5909 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
| 5910 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5911 | case EXIT_REASON_VMREAD: |
| 5912 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5913 | vmcs12->vmread_bitmap); |
| 5914 | case EXIT_REASON_VMWRITE: |
| 5915 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 5916 | vmcs12->vmwrite_bitmap); |
| 5917 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5918 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5919 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
| 5920 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5921 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
| 5922 | /* |
| 5923 | * VMX instructions trap unconditionally. This allows L1 to |
| 5924 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5925 | */ |
| 5926 | return true; |
| 5927 | case EXIT_REASON_CR_ACCESS: |
| 5928 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5929 | case EXIT_REASON_DR_ACCESS: |
| 5930 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5931 | case EXIT_REASON_IO_INSTRUCTION: |
| 5932 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
| 5933 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 5934 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
| 5935 | case EXIT_REASON_MSR_READ: |
| 5936 | case EXIT_REASON_MSR_WRITE: |
| 5937 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5938 | case EXIT_REASON_INVALID_STATE: |
| 5939 | return true; |
| 5940 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5941 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5942 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
Oliver Upton | b045ae9 | 2020-04-14 22:47:45 +0000 | [diff] [blame] | 5943 | return nested_vmx_exit_handled_mtf(vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5944 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5945 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5946 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5947 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5948 | nested_cpu_has2(vmcs12, |
| 5949 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5950 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 5951 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5952 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5953 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
| 5954 | case EXIT_REASON_APIC_ACCESS: |
| 5955 | case EXIT_REASON_APIC_WRITE: |
| 5956 | case EXIT_REASON_EOI_INDUCED: |
| 5957 | /* |
| 5958 | * The controls for "virtualize APIC accesses," "APIC- |
| 5959 | * register virtualization," and "virtual-interrupt |
| 5960 | * delivery" only come from vmcs12. |
| 5961 | */ |
| 5962 | return true; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5963 | case EXIT_REASON_INVPCID: |
| 5964 | return |
| 5965 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 5966 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5967 | case EXIT_REASON_WBINVD: |
| 5968 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5969 | case EXIT_REASON_XSETBV: |
| 5970 | return true; |
| 5971 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 5972 | /* |
| 5973 | * This should never happen, since it is not possible to |
| 5974 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 5975 | * If if it were, XSS would have to be checked against |
| 5976 | * the XSS exit bitmap in vmcs12. |
| 5977 | */ |
| 5978 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Tao Xu | bf653b7 | 2019-07-16 14:55:51 +0800 | [diff] [blame] | 5979 | case EXIT_REASON_UMWAIT: |
| 5980 | case EXIT_REASON_TPAUSE: |
| 5981 | return nested_cpu_has2(vmcs12, |
| 5982 | SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE); |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 5983 | case EXIT_REASON_ENCLS: |
| 5984 | return nested_vmx_exit_handled_encls(vcpu, vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 5985 | default: |
| 5986 | return true; |
| 5987 | } |
| 5988 | } |
| 5989 | |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5990 | /* |
| 5991 | * Conditionally reflect a VM-Exit into L1. Returns %true if the VM-Exit was |
| 5992 | * reflected into L1. |
| 5993 | */ |
Sean Christopherson | f47baae | 2020-04-15 10:55:16 -0700 | [diff] [blame] | 5994 | bool nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 5995 | { |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 5996 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 5997 | union vmx_exit_reason exit_reason = vmx->exit_reason; |
Sean Christopherson | 8779655 | 2020-04-22 17:11:27 -0700 | [diff] [blame] | 5998 | unsigned long exit_qual; |
| 5999 | u32 exit_intr_info; |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6000 | |
| 6001 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 6002 | |
| 6003 | /* |
| 6004 | * Late nested VM-Fail shares the same flow as nested VM-Exit since KVM |
| 6005 | * has already loaded L2's state. |
| 6006 | */ |
| 6007 | if (unlikely(vmx->fail)) { |
| 6008 | trace_kvm_nested_vmenter_failed( |
| 6009 | "hardware VM-instruction error: ", |
| 6010 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 6011 | exit_intr_info = 0; |
| 6012 | exit_qual = 0; |
| 6013 | goto reflect_vmexit; |
| 6014 | } |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6015 | |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6016 | trace_kvm_nested_vmexit(exit_reason.full, vcpu, KVM_ISA_VMX); |
Sean Christopherson | 236871b | 2020-04-15 10:55:13 -0700 | [diff] [blame] | 6017 | |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6018 | /* If L0 (KVM) wants the exit, it trumps L1's desires. */ |
| 6019 | if (nested_vmx_l0_wants_exit(vcpu, exit_reason)) |
| 6020 | return false; |
| 6021 | |
| 6022 | /* If L1 doesn't want the exit, handle it in L0. */ |
| 6023 | if (!nested_vmx_l1_wants_exit(vcpu, exit_reason)) |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6024 | return false; |
| 6025 | |
| 6026 | /* |
Sean Christopherson | 1d28306 | 2020-04-15 10:55:15 -0700 | [diff] [blame] | 6027 | * vmcs.VM_EXIT_INTR_INFO is only valid for EXCEPTION_NMI exits. For |
| 6028 | * EXTERNAL_INTERRUPT, the value for vmcs12->vm_exit_intr_info would |
| 6029 | * need to be synthesized by querying the in-kernel LAPIC, but external |
| 6030 | * interrupts are never reflected to L1 so it's a non-issue. |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6031 | */ |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6032 | exit_intr_info = vmx_get_intr_info(vcpu); |
Sean Christopherson | f315f2b | 2020-09-23 13:13:45 -0700 | [diff] [blame] | 6033 | if (is_exception_with_error_code(exit_intr_info)) { |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6034 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6035 | |
| 6036 | vmcs12->vm_exit_intr_error_code = |
| 6037 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6038 | } |
Sean Christopherson | 02f1965 | 2020-09-23 13:13:49 -0700 | [diff] [blame] | 6039 | exit_qual = vmx_get_exit_qual(vcpu); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6040 | |
Sean Christopherson | fbdd502 | 2020-04-15 10:55:12 -0700 | [diff] [blame] | 6041 | reflect_vmexit: |
Sean Christopherson | 8e53324 | 2020-11-06 17:03:12 +0800 | [diff] [blame] | 6042 | nested_vmx_vmexit(vcpu, exit_reason.full, exit_intr_info, exit_qual); |
Sean Christopherson | 7b7bd87 | 2020-04-15 10:55:11 -0700 | [diff] [blame] | 6043 | return true; |
| 6044 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6045 | |
| 6046 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 6047 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6048 | u32 user_data_size) |
| 6049 | { |
| 6050 | struct vcpu_vmx *vmx; |
| 6051 | struct vmcs12 *vmcs12; |
| 6052 | struct kvm_nested_state kvm_state = { |
| 6053 | .flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6054 | .format = KVM_STATE_NESTED_FORMAT_VMX, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6055 | .size = sizeof(kvm_state), |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6056 | .hdr.vmx.flags = 0, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6057 | .hdr.vmx.vmxon_pa = -1ull, |
| 6058 | .hdr.vmx.vmcs12_pa = -1ull, |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6059 | .hdr.vmx.preemption_timer_deadline = 0, |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6060 | }; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6061 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6062 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6063 | |
| 6064 | if (!vcpu) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6065 | return kvm_state.size + sizeof(*user_vmx_nested_state); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6066 | |
| 6067 | vmx = to_vmx(vcpu); |
| 6068 | vmcs12 = get_vmcs12(vcpu); |
| 6069 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6070 | if (nested_vmx_allowed(vcpu) && |
| 6071 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6072 | kvm_state.hdr.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 6073 | kvm_state.hdr.vmx.vmcs12_pa = vmx->nested.current_vmptr; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6074 | |
| 6075 | if (vmx_has_valid_vmcs12(vcpu)) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6076 | kvm_state.size += sizeof(user_vmx_nested_state->vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6077 | |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6078 | /* 'hv_evmcs_vmptr' can also be EVMPTR_MAP_PENDING here */ |
| 6079 | if (vmx->nested.hv_evmcs_vmptr != EVMPTR_INVALID) |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6080 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
| 6081 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6082 | if (is_guest_mode(vcpu) && |
| 6083 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6084 | vmcs12->vmcs_link_pointer != -1ull) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6085 | kvm_state.size += sizeof(user_vmx_nested_state->shadow_vmcs12); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | if (vmx->nested.smm.vmxon) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6089 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6090 | |
| 6091 | if (vmx->nested.smm.guest_mode) |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6092 | kvm_state.hdr.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6093 | |
| 6094 | if (is_guest_mode(vcpu)) { |
| 6095 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 6096 | |
| 6097 | if (vmx->nested.nested_run_pending) |
| 6098 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6099 | |
| 6100 | if (vmx->nested.mtf_pending) |
| 6101 | kvm_state.flags |= KVM_STATE_NESTED_MTF_PENDING; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6102 | |
| 6103 | if (nested_cpu_has_preemption_timer(vmcs12) && |
| 6104 | vmx->nested.has_preemption_timer_deadline) { |
| 6105 | kvm_state.hdr.vmx.flags |= |
| 6106 | KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE; |
| 6107 | kvm_state.hdr.vmx.preemption_timer_deadline = |
| 6108 | vmx->nested.preemption_timer_deadline; |
| 6109 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6110 | } |
| 6111 | } |
| 6112 | |
| 6113 | if (user_data_size < kvm_state.size) |
| 6114 | goto out; |
| 6115 | |
| 6116 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 6117 | return -EFAULT; |
| 6118 | |
| 6119 | if (!vmx_has_valid_vmcs12(vcpu)) |
| 6120 | goto out; |
| 6121 | |
| 6122 | /* |
| 6123 | * When running L2, the authoritative vmcs12 state is in the |
| 6124 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 6125 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6126 | * need_vmcs12_to_shadow_sync is set, in which case, the authoritative |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6127 | * vmcs12 state is in the vmcs12 already. |
| 6128 | */ |
| 6129 | if (is_guest_mode(vcpu)) { |
Sean Christopherson | 3731905ef | 2019-05-07 08:36:27 -0700 | [diff] [blame] | 6130 | sync_vmcs02_to_vmcs12(vcpu, vmcs12); |
Sean Christopherson | 7952d76 | 2019-05-07 08:36:29 -0700 | [diff] [blame] | 6131 | sync_vmcs02_to_vmcs12_rare(vcpu, vmcs12); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6132 | } else { |
| 6133 | copy_vmcs02_to_vmcs12_rare(vcpu, get_vmcs12(vcpu)); |
| 6134 | if (!vmx->nested.need_vmcs12_to_shadow_sync) { |
Vitaly Kuznetsov | 1e9dfbd | 2021-05-26 15:20:16 +0200 | [diff] [blame] | 6135 | if (evmptr_is_valid(vmx->nested.hv_evmcs_vmptr)) |
Vitaly Kuznetsov | d6bf71a | 2021-05-26 15:20:22 +0200 | [diff] [blame] | 6136 | /* |
| 6137 | * L1 hypervisor is not obliged to keep eVMCS |
| 6138 | * clean fields data always up-to-date while |
| 6139 | * not in guest mode, 'hv_clean_fields' is only |
| 6140 | * supposed to be actual upon vmentry so we need |
| 6141 | * to ignore it here and do full copy. |
| 6142 | */ |
| 6143 | copy_enlightened_to_vmcs12(vmx, 0); |
Maxim Levitsky | d51e1d3 | 2021-01-14 22:54:47 +0200 | [diff] [blame] | 6144 | else if (enable_shadow_vmcs) |
| 6145 | copy_shadow_to_vmcs12(vmx); |
| 6146 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6147 | } |
| 6148 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6149 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->vmcs12) < VMCS12_SIZE); |
| 6150 | BUILD_BUG_ON(sizeof(user_vmx_nested_state->shadow_vmcs12) < VMCS12_SIZE); |
| 6151 | |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6152 | /* |
| 6153 | * Copy over the full allocated size of vmcs12 rather than just the size |
| 6154 | * of the struct. |
| 6155 | */ |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6156 | if (copy_to_user(user_vmx_nested_state->vmcs12, vmcs12, VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6157 | return -EFAULT; |
| 6158 | |
| 6159 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6160 | vmcs12->vmcs_link_pointer != -1ull) { |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6161 | if (copy_to_user(user_vmx_nested_state->shadow_vmcs12, |
Tom Roeder | 3a33d03 | 2019-01-24 13:48:20 -0800 | [diff] [blame] | 6162 | get_shadow_vmcs12(vcpu), VMCS12_SIZE)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6163 | return -EFAULT; |
| 6164 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6165 | out: |
| 6166 | return kvm_state.size; |
| 6167 | } |
| 6168 | |
| 6169 | /* |
| 6170 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 6171 | */ |
| 6172 | void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 6173 | { |
| 6174 | if (is_guest_mode(vcpu)) { |
| 6175 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
| 6176 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 6177 | } |
| 6178 | free_nested(vcpu); |
| 6179 | } |
| 6180 | |
| 6181 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 6182 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 6183 | struct kvm_nested_state *kvm_state) |
| 6184 | { |
| 6185 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6186 | struct vmcs12 *vmcs12; |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6187 | enum vm_entry_failure_code ignored; |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6188 | struct kvm_vmx_nested_state_data __user *user_vmx_nested_state = |
| 6189 | &user_kvm_nested_state->data.vmx[0]; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6190 | int ret; |
| 6191 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6192 | if (kvm_state->format != KVM_STATE_NESTED_FORMAT_VMX) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6193 | return -EINVAL; |
| 6194 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6195 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) { |
| 6196 | if (kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6197 | return -EINVAL; |
| 6198 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6199 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6200 | return -EINVAL; |
| 6201 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6202 | /* |
| 6203 | * KVM_STATE_NESTED_EVMCS used to signal that KVM should |
| 6204 | * enable eVMCS capability on vCPU. However, since then |
| 6205 | * code was changed such that flag signals vmcs12 should |
| 6206 | * be copied into eVMCS in guest memory. |
| 6207 | * |
| 6208 | * To preserve backwards compatability, allow user |
| 6209 | * to set this flag even when there is no VMXON region. |
| 6210 | */ |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6211 | if (kvm_state->flags & ~KVM_STATE_NESTED_EVMCS) |
| 6212 | return -EINVAL; |
| 6213 | } else { |
| 6214 | if (!nested_vmx_allowed(vcpu)) |
| 6215 | return -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6216 | |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6217 | if (!page_address_valid(vcpu, kvm_state->hdr.vmx.vmxon_pa)) |
| 6218 | return -EINVAL; |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6219 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6220 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6221 | 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] | 6222 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6223 | return -EINVAL; |
| 6224 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6225 | if (kvm_state->hdr.vmx.smm.flags & |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6226 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 6227 | return -EINVAL; |
| 6228 | |
Paolo Bonzini | 5e105c8 | 2020-07-27 08:55:09 -0400 | [diff] [blame] | 6229 | if (kvm_state->hdr.vmx.flags & ~KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) |
| 6230 | return -EINVAL; |
| 6231 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6232 | /* |
| 6233 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 6234 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 6235 | * must be zero. |
| 6236 | */ |
Liran Alon | 65b712f1 | 2019-06-25 14:26:42 +0300 | [diff] [blame] | 6237 | if (is_smm(vcpu) ? |
| 6238 | (kvm_state->flags & |
| 6239 | (KVM_STATE_NESTED_GUEST_MODE | KVM_STATE_NESTED_RUN_PENDING)) |
| 6240 | : kvm_state->hdr.vmx.smm.flags) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6241 | return -EINVAL; |
| 6242 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6243 | if ((kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 6244 | !(kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6245 | return -EINVAL; |
| 6246 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6247 | if ((kvm_state->flags & KVM_STATE_NESTED_EVMCS) && |
| 6248 | (!nested_vmx_allowed(vcpu) || !vmx->nested.enlightened_vmcs_enabled)) |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6249 | return -EINVAL; |
| 6250 | |
Liran Alon | 323d73a | 2019-06-26 16:09:27 +0300 | [diff] [blame] | 6251 | vmx_leave_nested(vcpu); |
Paolo Bonzini | 9fd5887 | 2019-06-19 16:52:27 +0200 | [diff] [blame] | 6252 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6253 | if (kvm_state->hdr.vmx.vmxon_pa == -1ull) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6254 | return 0; |
| 6255 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6256 | vmx->nested.vmxon_ptr = kvm_state->hdr.vmx.vmxon_pa; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6257 | ret = enter_vmx_operation(vcpu); |
| 6258 | if (ret) |
| 6259 | return ret; |
| 6260 | |
Paolo Bonzini | 0f02bd0 | 2020-07-27 09:00:37 -0400 | [diff] [blame] | 6261 | /* Empty 'VMXON' state is permitted if no VMCS loaded */ |
| 6262 | if (kvm_state->size < sizeof(*kvm_state) + sizeof(*vmcs12)) { |
| 6263 | /* See vmx_has_valid_vmcs12. */ |
| 6264 | if ((kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE) || |
| 6265 | (kvm_state->flags & KVM_STATE_NESTED_EVMCS) || |
| 6266 | (kvm_state->hdr.vmx.vmcs12_pa != -1ull)) |
| 6267 | return -EINVAL; |
| 6268 | else |
| 6269 | return 0; |
| 6270 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6271 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6272 | if (kvm_state->hdr.vmx.vmcs12_pa != -1ull) { |
| 6273 | if (kvm_state->hdr.vmx.vmcs12_pa == kvm_state->hdr.vmx.vmxon_pa || |
| 6274 | !page_address_valid(vcpu, kvm_state->hdr.vmx.vmcs12_pa)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6275 | return -EINVAL; |
| 6276 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6277 | set_current_vmptr(vmx, kvm_state->hdr.vmx.vmcs12_pa); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6278 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 6279 | /* |
Vitaly Kuznetsov | e942dbf | 2020-03-09 16:52:12 +0100 | [diff] [blame] | 6280 | * nested_vmx_handle_enlightened_vmptrld() cannot be called |
| 6281 | * directly from here as HV_X64_MSR_VP_ASSIST_PAGE may not be |
| 6282 | * restored yet. EVMCS will be mapped from |
| 6283 | * nested_get_vmcs12_pages(). |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6284 | */ |
Vitaly Kuznetsov | 2784996 | 2021-05-26 15:20:20 +0200 | [diff] [blame] | 6285 | vmx->nested.hv_evmcs_vmptr = EVMPTR_MAP_PENDING; |
Paolo Bonzini | 729c15c | 2020-09-22 06:53:57 -0400 | [diff] [blame] | 6286 | kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6287 | } else { |
| 6288 | return -EINVAL; |
| 6289 | } |
| 6290 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6291 | if (kvm_state->hdr.vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6292 | vmx->nested.smm.vmxon = true; |
| 6293 | vmx->nested.vmxon = false; |
| 6294 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6295 | 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] | 6296 | vmx->nested.smm.guest_mode = true; |
| 6297 | } |
| 6298 | |
| 6299 | vmcs12 = get_vmcs12(vcpu); |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6300 | if (copy_from_user(vmcs12, user_vmx_nested_state->vmcs12, sizeof(*vmcs12))) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6301 | return -EFAULT; |
| 6302 | |
| 6303 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
| 6304 | return -EINVAL; |
| 6305 | |
| 6306 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 6307 | return 0; |
| 6308 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6309 | vmx->nested.nested_run_pending = |
| 6310 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 6311 | |
Oliver Upton | 5ef8acb | 2020-02-07 02:36:07 -0800 | [diff] [blame] | 6312 | vmx->nested.mtf_pending = |
| 6313 | !!(kvm_state->flags & KVM_STATE_NESTED_MTF_PENDING); |
| 6314 | |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6315 | ret = -EINVAL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6316 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 6317 | vmcs12->vmcs_link_pointer != -1ull) { |
| 6318 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 6319 | |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6320 | if (kvm_state->size < |
| 6321 | sizeof(*kvm_state) + |
| 6322 | sizeof(user_vmx_nested_state->vmcs12) + sizeof(*shadow_vmcs12)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6323 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6324 | |
| 6325 | if (copy_from_user(shadow_vmcs12, |
Liran Alon | 6ca00df | 2019-06-16 15:03:10 +0300 | [diff] [blame] | 6326 | user_vmx_nested_state->shadow_vmcs12, |
| 6327 | sizeof(*shadow_vmcs12))) { |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6328 | ret = -EFAULT; |
| 6329 | goto error_guest_mode; |
| 6330 | } |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6331 | |
| 6332 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 6333 | !shadow_vmcs12->hdr.shadow_vmcs) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6334 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6335 | } |
| 6336 | |
Paolo Bonzini | 83d31e5 | 2020-07-09 13:12:09 -0400 | [diff] [blame] | 6337 | vmx->nested.has_preemption_timer_deadline = false; |
Peter Shier | 850448f | 2020-05-26 14:51:06 -0700 | [diff] [blame] | 6338 | if (kvm_state->hdr.vmx.flags & KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE) { |
| 6339 | vmx->nested.has_preemption_timer_deadline = true; |
| 6340 | vmx->nested.preemption_timer_deadline = |
| 6341 | kvm_state->hdr.vmx.preemption_timer_deadline; |
| 6342 | } |
| 6343 | |
Sean Christopherson | 5478ba3 | 2019-04-11 12:18:06 -0700 | [diff] [blame] | 6344 | if (nested_vmx_check_controls(vcpu, vmcs12) || |
| 6345 | nested_vmx_check_host_state(vcpu, vmcs12) || |
Sean Christopherson | 68cda40 | 2020-05-11 15:05:29 -0700 | [diff] [blame] | 6346 | nested_vmx_check_guest_state(vcpu, vmcs12, &ignored)) |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6347 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6348 | |
| 6349 | vmx->nested.dirty_vmcs12 = true; |
| 6350 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6351 | if (ret) |
| 6352 | goto error_guest_mode; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6353 | |
| 6354 | return 0; |
Sean Christopherson | 21be4ca | 2019-05-08 11:04:32 -0700 | [diff] [blame] | 6355 | |
| 6356 | error_guest_mode: |
| 6357 | vmx->nested.nested_run_pending = 0; |
| 6358 | return ret; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6359 | } |
| 6360 | |
Xiaoyao Li | 1b84292 | 2019-10-20 17:11:01 +0800 | [diff] [blame] | 6361 | void nested_vmx_set_vmcs_shadowing_bitmap(void) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6362 | { |
| 6363 | if (enable_shadow_vmcs) { |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6364 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Sean Christopherson | fadcead | 2019-05-07 08:36:23 -0700 | [diff] [blame] | 6365 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6366 | } |
| 6367 | } |
| 6368 | |
| 6369 | /* |
| 6370 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 6371 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 6372 | * The same values should also be used to verify that vmcs12 control fields are |
| 6373 | * valid during nested entry from L1 to L2. |
| 6374 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 6375 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 6376 | * bit in the high half is on if the corresponding bit in the control field |
| 6377 | * may be on. See also vmx_control_verify(). |
| 6378 | */ |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6379 | void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, u32 ept_caps) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6380 | { |
| 6381 | /* |
| 6382 | * Note that as a general rule, the high half of the MSRs (bits in |
| 6383 | * the control fields which may be 1) should be initialized by the |
| 6384 | * intersection of the underlying hardware's MSR (i.e., features which |
| 6385 | * can be supported) and the list of features we want to expose - |
| 6386 | * because they are known to be properly supported in our code. |
| 6387 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 6388 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 6389 | * reason is that if one of these bits is necessary, it will appear |
| 6390 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 6391 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Sean Christopherson | 2c1f332 | 2020-04-15 10:55:14 -0700 | [diff] [blame] | 6392 | * nested_vmx_l1_wants_exit() will not pass related exits to L1. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6393 | * These rules have exceptions below. |
| 6394 | */ |
| 6395 | |
| 6396 | /* pin-based controls */ |
| 6397 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 6398 | msrs->pinbased_ctls_low, |
| 6399 | msrs->pinbased_ctls_high); |
| 6400 | msrs->pinbased_ctls_low |= |
| 6401 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6402 | msrs->pinbased_ctls_high &= |
| 6403 | PIN_BASED_EXT_INTR_MASK | |
| 6404 | PIN_BASED_NMI_EXITING | |
| 6405 | PIN_BASED_VIRTUAL_NMIS | |
Vitaly Kuznetsov | a444326 | 2020-02-20 18:22:04 +0100 | [diff] [blame] | 6406 | (enable_apicv ? PIN_BASED_POSTED_INTR : 0); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6407 | msrs->pinbased_ctls_high |= |
| 6408 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6409 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 6410 | |
| 6411 | /* exit controls */ |
| 6412 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 6413 | msrs->exit_ctls_low, |
| 6414 | msrs->exit_ctls_high); |
| 6415 | msrs->exit_ctls_low = |
| 6416 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6417 | |
| 6418 | msrs->exit_ctls_high &= |
| 6419 | #ifdef CONFIG_X86_64 |
| 6420 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
| 6421 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6422 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT | |
| 6423 | VM_EXIT_CLEAR_BNDCFGS | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6424 | msrs->exit_ctls_high |= |
| 6425 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6426 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
| 6427 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6428 | |
| 6429 | /* We support free control of debug control saving. */ |
| 6430 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 6431 | |
| 6432 | /* entry controls */ |
| 6433 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 6434 | msrs->entry_ctls_low, |
| 6435 | msrs->entry_ctls_high); |
| 6436 | msrs->entry_ctls_low = |
| 6437 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6438 | msrs->entry_ctls_high &= |
| 6439 | #ifdef CONFIG_X86_64 |
| 6440 | VM_ENTRY_IA32E_MODE | |
| 6441 | #endif |
Chenyi Qiang | efc8313 | 2020-08-28 16:56:18 +0800 | [diff] [blame] | 6442 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS | |
| 6443 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6444 | msrs->entry_ctls_high |= |
| 6445 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
| 6446 | |
| 6447 | /* We support free control of debug control loading. */ |
| 6448 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 6449 | |
| 6450 | /* cpu-based controls */ |
| 6451 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 6452 | msrs->procbased_ctls_low, |
| 6453 | msrs->procbased_ctls_high); |
| 6454 | msrs->procbased_ctls_low = |
| 6455 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 6456 | msrs->procbased_ctls_high &= |
Xiaoyao Li | 9dadc2f | 2019-12-06 16:45:24 +0800 | [diff] [blame] | 6457 | CPU_BASED_INTR_WINDOW_EXITING | |
Xiaoyao Li | 5e3d394 | 2019-12-06 16:45:26 +0800 | [diff] [blame] | 6458 | CPU_BASED_NMI_WINDOW_EXITING | CPU_BASED_USE_TSC_OFFSETTING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6459 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 6460 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 6461 | CPU_BASED_CR3_STORE_EXITING | |
| 6462 | #ifdef CONFIG_X86_64 |
| 6463 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 6464 | #endif |
| 6465 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 6466 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 6467 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 6468 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 6469 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 6470 | /* |
| 6471 | * We can allow some features even when not supported by the |
| 6472 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 6473 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 6474 | * without MSR bitmaps. |
| 6475 | */ |
| 6476 | msrs->procbased_ctls_high |= |
| 6477 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 6478 | CPU_BASED_USE_MSR_BITMAPS; |
| 6479 | |
| 6480 | /* We support free control of CR3 access interception. */ |
| 6481 | msrs->procbased_ctls_low &= |
| 6482 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 6483 | |
| 6484 | /* |
| 6485 | * secondary cpu-based controls. Do not include those that |
Xiaoyao Li | 7c1b761 | 2020-07-09 12:34:25 +0800 | [diff] [blame] | 6486 | * depend on CPUID bits, they are added later by |
| 6487 | * vmx_vcpu_after_set_cpuid. |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6488 | */ |
Vitaly Kuznetsov | 6b1971c | 2019-02-07 11:42:14 +0100 | [diff] [blame] | 6489 | if (msrs->procbased_ctls_high & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) |
| 6490 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 6491 | msrs->secondary_ctls_low, |
| 6492 | msrs->secondary_ctls_high); |
| 6493 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6494 | msrs->secondary_ctls_low = 0; |
| 6495 | msrs->secondary_ctls_high &= |
| 6496 | SECONDARY_EXEC_DESC | |
Sean Christopherson | 7f3603b | 2020-09-23 09:50:47 -0700 | [diff] [blame] | 6497 | SECONDARY_EXEC_ENABLE_RDTSCP | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6498 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6499 | SECONDARY_EXEC_WBINVD_EXITING | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6500 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6501 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 6defc59 | 2019-07-02 14:39:29 +0200 | [diff] [blame] | 6502 | SECONDARY_EXEC_RDRAND_EXITING | |
| 6503 | SECONDARY_EXEC_ENABLE_INVPCID | |
| 6504 | SECONDARY_EXEC_RDSEED_EXITING | |
Ilias Stamatis | d041b5e | 2021-05-26 19:44:17 +0100 | [diff] [blame] | 6505 | SECONDARY_EXEC_XSAVES | |
| 6506 | SECONDARY_EXEC_TSC_SCALING; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6507 | |
| 6508 | /* |
| 6509 | * We can emulate "VMCS shadowing," even if the hardware |
| 6510 | * doesn't support it. |
| 6511 | */ |
| 6512 | msrs->secondary_ctls_high |= |
| 6513 | SECONDARY_EXEC_SHADOW_VMCS; |
| 6514 | |
| 6515 | if (enable_ept) { |
| 6516 | /* nested EPT: emulate EPT also to L1 */ |
| 6517 | msrs->secondary_ctls_high |= |
| 6518 | SECONDARY_EXEC_ENABLE_EPT; |
Sean Christopherson | bb1fcc7 | 2020-03-02 18:02:36 -0800 | [diff] [blame] | 6519 | msrs->ept_caps = |
| 6520 | VMX_EPT_PAGE_WALK_4_BIT | |
| 6521 | VMX_EPT_PAGE_WALK_5_BIT | |
| 6522 | VMX_EPTP_WB_BIT | |
Sean Christopherson | 96d4701 | 2020-03-02 18:02:40 -0800 | [diff] [blame] | 6523 | VMX_EPT_INVEPT_BIT | |
| 6524 | VMX_EPT_EXECUTE_ONLY_BIT; |
| 6525 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6526 | msrs->ept_caps &= ept_caps; |
| 6527 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 6528 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 6529 | VMX_EPT_1GB_PAGE_BIT; |
| 6530 | if (enable_ept_ad_bits) { |
| 6531 | msrs->secondary_ctls_high |= |
| 6532 | SECONDARY_EXEC_ENABLE_PML; |
| 6533 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
| 6534 | } |
| 6535 | } |
| 6536 | |
| 6537 | if (cpu_has_vmx_vmfunc()) { |
| 6538 | msrs->secondary_ctls_high |= |
| 6539 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 6540 | /* |
| 6541 | * Advertise EPTP switching unconditionally |
| 6542 | * since we emulate it |
| 6543 | */ |
| 6544 | if (enable_ept) |
| 6545 | msrs->vmfunc_controls = |
| 6546 | VMX_VMFUNC_EPTP_SWITCHING; |
| 6547 | } |
| 6548 | |
| 6549 | /* |
| 6550 | * Old versions of KVM use the single-context version without |
| 6551 | * checking for support, so declare that it is supported even |
| 6552 | * though it is treated as global context. The alternative is |
| 6553 | * not failing the single-context invvpid, and it is worse. |
| 6554 | */ |
| 6555 | if (enable_vpid) { |
| 6556 | msrs->secondary_ctls_high |= |
| 6557 | SECONDARY_EXEC_ENABLE_VPID; |
| 6558 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
| 6559 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
| 6560 | } |
| 6561 | |
| 6562 | if (enable_unrestricted_guest) |
| 6563 | msrs->secondary_ctls_high |= |
| 6564 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 6565 | |
| 6566 | if (flexpriority_enabled) |
| 6567 | msrs->secondary_ctls_high |= |
| 6568 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6569 | |
Sean Christopherson | 72add91 | 2021-04-12 16:21:42 +1200 | [diff] [blame] | 6570 | if (enable_sgx) |
| 6571 | msrs->secondary_ctls_high |= SECONDARY_EXEC_ENCLS_EXITING; |
| 6572 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6573 | /* miscellaneous data */ |
| 6574 | rdmsr(MSR_IA32_VMX_MISC, |
| 6575 | msrs->misc_low, |
| 6576 | msrs->misc_high); |
| 6577 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 6578 | msrs->misc_low |= |
| 6579 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
| 6580 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Yadong Qi | bf0cd88 | 2020-11-06 14:51:22 +0800 | [diff] [blame] | 6581 | VMX_MISC_ACTIVITY_HLT | |
| 6582 | VMX_MISC_ACTIVITY_WAIT_SIPI; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6583 | msrs->misc_high = 0; |
| 6584 | |
| 6585 | /* |
| 6586 | * This MSR reports some information about VMX support. We |
| 6587 | * should return information about the VMX we emulate for the |
| 6588 | * guest, and the VMCS structure we give it - not about the |
| 6589 | * VMX support of the underlying hardware. |
| 6590 | */ |
| 6591 | msrs->basic = |
| 6592 | VMCS12_REVISION | |
| 6593 | VMX_BASIC_TRUE_CTLS | |
| 6594 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 6595 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 6596 | |
| 6597 | if (cpu_has_vmx_basic_inout()) |
| 6598 | msrs->basic |= VMX_BASIC_INOUT; |
| 6599 | |
| 6600 | /* |
| 6601 | * These MSRs specify bits which the guest must keep fixed on |
| 6602 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 6603 | * We picked the standard core2 setting. |
| 6604 | */ |
| 6605 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 6606 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 6607 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 6608 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
| 6609 | |
| 6610 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 6611 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 6612 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
| 6613 | |
| 6614 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
| 6615 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
| 6616 | } |
| 6617 | |
| 6618 | void nested_vmx_hardware_unsetup(void) |
| 6619 | { |
| 6620 | int i; |
| 6621 | |
| 6622 | if (enable_shadow_vmcs) { |
| 6623 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6624 | free_page((unsigned long)vmx_bitmap[i]); |
| 6625 | } |
| 6626 | } |
| 6627 | |
Sean Christopherson | 6c1c6e5 | 2020-05-06 13:46:53 -0700 | [diff] [blame] | 6628 | __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *)) |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6629 | { |
| 6630 | int i; |
| 6631 | |
| 6632 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6633 | enable_shadow_vmcs = 0; |
| 6634 | if (enable_shadow_vmcs) { |
| 6635 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
Ben Gardon | 4183683 | 2019-02-11 11:02:52 -0800 | [diff] [blame] | 6636 | /* |
| 6637 | * The vmx_bitmap is not tied to a VM and so should |
| 6638 | * not be charged to a memcg. |
| 6639 | */ |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6640 | vmx_bitmap[i] = (unsigned long *) |
| 6641 | __get_free_page(GFP_KERNEL); |
| 6642 | if (!vmx_bitmap[i]) { |
| 6643 | nested_vmx_hardware_unsetup(); |
| 6644 | return -ENOMEM; |
| 6645 | } |
| 6646 | } |
| 6647 | |
| 6648 | init_vmcs_shadow_fields(); |
| 6649 | } |
| 6650 | |
Liran Alon | cc87767 | 2019-11-18 21:11:21 +0200 | [diff] [blame] | 6651 | exit_handlers[EXIT_REASON_VMCLEAR] = handle_vmclear; |
| 6652 | exit_handlers[EXIT_REASON_VMLAUNCH] = handle_vmlaunch; |
| 6653 | exit_handlers[EXIT_REASON_VMPTRLD] = handle_vmptrld; |
| 6654 | exit_handlers[EXIT_REASON_VMPTRST] = handle_vmptrst; |
| 6655 | exit_handlers[EXIT_REASON_VMREAD] = handle_vmread; |
| 6656 | exit_handlers[EXIT_REASON_VMRESUME] = handle_vmresume; |
| 6657 | exit_handlers[EXIT_REASON_VMWRITE] = handle_vmwrite; |
| 6658 | exit_handlers[EXIT_REASON_VMOFF] = handle_vmoff; |
| 6659 | exit_handlers[EXIT_REASON_VMON] = handle_vmon; |
| 6660 | exit_handlers[EXIT_REASON_INVEPT] = handle_invept; |
| 6661 | exit_handlers[EXIT_REASON_INVVPID] = handle_invvpid; |
| 6662 | exit_handlers[EXIT_REASON_VMFUNC] = handle_vmfunc; |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6663 | |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6664 | return 0; |
| 6665 | } |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6666 | |
| 6667 | struct kvm_x86_nested_ops vmx_nested_ops = { |
| 6668 | .check_events = vmx_check_nested_events, |
Sean Christopherson | d2060bd | 2020-04-22 19:25:39 -0700 | [diff] [blame] | 6669 | .hv_timer_pending = nested_vmx_preemption_timer_pending, |
Sean Christopherson | cb6a32c | 2021-03-02 09:45:14 -0800 | [diff] [blame] | 6670 | .triple_fault = nested_vmx_triple_fault, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6671 | .get_state = vmx_get_nested_state, |
| 6672 | .set_state = vmx_set_nested_state, |
Paolo Bonzini | 9a78e15 | 2021-01-08 11:43:08 -0500 | [diff] [blame] | 6673 | .get_nested_state_pages = vmx_get_nested_state_pages, |
Sean Christopherson | 02f5fb2 | 2020-06-22 14:58:32 -0700 | [diff] [blame] | 6674 | .write_log_dirty = nested_vmx_write_pml_buffer, |
Paolo Bonzini | 33b2217 | 2020-04-17 10:24:18 -0400 | [diff] [blame] | 6675 | .enable_evmcs = nested_enable_evmcs, |
| 6676 | .get_evmcs_version = nested_get_evmcs_version, |
| 6677 | }; |